File size: 10,633 Bytes
4da4469
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python3
"""
Inferencia interactiva del modelo Base 260M (o Nano 42M) con PyTorch.
Descarga el checkpoint desde S3, carga el modelo y permite hacer preguntas.

Uso:
    python run_inference_base.py --model base --checkpoint s3://...
    python run_inference_base.py --model nano  --checkpoint s3://...

Requiere:
    pip install torch sentencepiece boto3
"""

import argparse
import json
import subprocess
import sys
import tempfile
from pathlib import Path

import torch
import sentencepiece as spm

# Añadir training_v2 al path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))

from training_v2.model.transformer import VectraYXNano, ModelConfig

# ─── Prompts de prueba ────────────────────────────────────────────────────────

TEST_PROMPTS = [
    # Bash básico
    ("qué hora es",                         "bash_exec", "date"),
    ("dame la fecha de hoy",                "bash_exec", "date +%Y-%m-%d"),
    ("quién soy",                           "bash_exec", "whoami"),
    ("cuánta memoria libre hay",            "bash_exec", "free -h"),
    ("uso de disco",                        "bash_exec", "df -h"),
    ("qué sistema operativo es",            "bash_exec", "uname -a"),
    ("cuál es mi IP",                       "bash_exec", "hostname -I"),
    ("lista los archivos aquí",             "bash_exec", "ls -lh"),
    # Bash intermedio
    ("qué puertos están escuchando",        "bash_exec", "ss -tuln"),
    ("procesos que más CPU usan",           "bash_exec", "ps aux --sort=-%cpu"),
    # MCP
    ("dame detalles de CVE-2021-44228",     "nvd_get_cve", None),
    ("busca CVEs de log4j",                 "nvd_search", None),
    ("está CVE-2021-44228 en KEV",          "cisa_kev_check", None),
    ("es maliciosa la IP 45.155.205.12",    "otx_check_ioc", None),
    # Conversacional (no debe llamar tool)
    ("qué es un zero-day",                  None, None),
    ("hola, cómo estás",                    None, None),
]

SYSTEM_PROMPT = (
    "Eres VectraYX, un asistente de ciberseguridad en español. "
    "Tienes 5 herramientas MCP disponibles:\n"
    "- nvd_get_cve(cve_id): obtener detalle de un CVE\n"
    "- nvd_search(query, limit): buscar CVEs por palabra clave\n"
    "- cisa_kev_check(cve_id): comprobar si un CVE está en el catálogo KEV\n"
    "- otx_check_ioc(ioc_type, value): reputación de IOC (ip, domain, hash)\n"
    "- bash_exec(cmd): ejecutar comando shell local\n"
    "Cuando la pregunta requiera datos externos o ejecutar algo, emite EXACTAMENTE:\n"
    '<|tool_call|>{"name":"<tool>","args":{...}}<|/tool_call|>\n'
    "Si la pregunta es conversacional o conceptual, responde en prosa SIN llamar herramientas."
)


def build_prompt(sp, question: str) -> torch.Tensor:
    text = (
        f"<|system|>{SYSTEM_PROMPT}<|end|>"
        f"<|user|>{question}<|end|>"
        f"<|assistant|>"
    )
    ids = sp.encode(text, out_type=int)
    return torch.tensor([ids], dtype=torch.long)


@torch.no_grad()
def generate(model, input_ids, sp, max_new=120, temperature=0.7,
             top_k=40, top_p=0.9, repeat_penalty=1.3):
    device = next(model.parameters()).device
    ids = input_ids.to(device)
    end_id = sp.piece_to_id("<|end|>")
    generated = []

    for _ in range(max_new):
        logits, _ = model(ids)
        logits = logits[0, -1, :]  # (vocab,)

        # Repeat penalty
        if repeat_penalty != 1.0 and generated:
            for tok in set(generated[-50:]):
                logits[tok] /= repeat_penalty

        logits = logits / temperature

        # Top-k
        if top_k > 0:
            vals, _ = torch.topk(logits, top_k)
            logits[logits < vals[-1]] = float('-inf')

        # Top-p
        probs = torch.softmax(logits, dim=-1)
        sorted_probs, sorted_idx = torch.sort(probs, descending=True)
        cumsum = torch.cumsum(sorted_probs, dim=0)
        mask = cumsum - sorted_probs > top_p
        sorted_probs[mask] = 0
        sorted_probs /= sorted_probs.sum()
        next_tok = sorted_idx[torch.multinomial(sorted_probs, 1)].item()

        generated.append(next_tok)
        ids = torch.cat([ids, torch.tensor([[next_tok]], device=device)], dim=1)

        if next_tok == end_id:
            break

    return sp.decode(generated)


def s3_download(s3_path: str, local_path: Path):
    print(f"[s3] Descargando {s3_path} ...", flush=True)
    r = subprocess.run(["aws", "s3", "cp", s3_path, str(local_path)],
                       capture_output=True, text=True)
    if r.returncode != 0:
        print(f"[ERROR] {r.stderr}")
        sys.exit(1)
    print(f"[s3] ✓ {local_path} ({local_path.stat().st_size/1e6:.1f}MB)")


def load_model(checkpoint_path: Path, config_path: Path, device: str):
    cfg = ModelConfig.from_json(str(config_path))
    model = VectraYXNano(cfg).to(device)
    print(f"[model] {model.num_params()/1e6:.2f}M params")

    payload = torch.load(checkpoint_path, map_location=device, weights_only=False)
    state = payload.get("model", payload)
    missing, unexpected = model.load_state_dict(state, strict=False)
    if missing:
        print(f"[load] missing keys: {missing[:3]}{'...' if len(missing)>3 else ''}")
    model.eval()
    return model


def run_benchmark(model, sp, results_path: Path):
    """Corre los prompts de prueba y guarda resultados."""
    results = []
    correct = 0
    total_tool = 0

    print("\n" + "="*70)
    print("BENCHMARK DE INFERENCIA")
    print("="*70)

    for question, expected_tool, expected_cmd in TEST_PROMPTS:
        input_ids = build_prompt(sp, question)
        response = generate(model, input_ids, sp)

        # Detectar tool-call
        detected_tool = None
        detected_args = None
        if "<|tool_call|>" in response:
            try:
                start = response.index("<|tool_call|>") + len("<|tool_call|>")
                end = response.index("<|/tool_call|>")
                call = json.loads(response[start:end])
                detected_tool = call.get("name")
                detected_args = call.get("args", {})
            except Exception:
                pass

        # Evaluar
        if expected_tool is not None:
            total_tool += 1
            ok = detected_tool == expected_tool
            if ok:
                correct += 1
            status = "✅" if ok else "❌"
        else:
            # Conversacional — no debe llamar tool
            ok = detected_tool is None
            status = "✅" if ok else "⚠️ (llamó tool innecesariamente)"

        print(f"\n[{status}] Q: {question}")
        print(f"     Expected: {expected_tool or 'ninguna'}")
        print(f"     Got tool: {detected_tool or 'ninguna'}")
        if detected_args:
            print(f"     Args: {detected_args}")
        print(f"     Response: {response[:120].strip()}")

        results.append({
            "question": question,
            "expected_tool": expected_tool,
            "expected_cmd": expected_cmd,
            "detected_tool": detected_tool,
            "detected_args": detected_args,
            "response": response,
            "correct": ok,
        })

    # Resumen
    b4_score = correct / total_tool if total_tool > 0 else 0
    print("\n" + "="*70)
    print(f"RESULTADO B4: {correct}/{total_tool} = {b4_score:.3f}")
    print("="*70)

    with open(results_path, "w", encoding="utf-8") as f:
        json.dump({
            "b4_score": b4_score,
            "correct": correct,
            "total_tool": total_tool,
            "results": results,
        }, f, indent=2, ensure_ascii=False)
    print(f"\n[saved] {results_path}")
    return b4_score


def interactive_mode(model, sp):
    """Modo interactivo para hacer preguntas manualmente."""
    print("\n" + "="*70)
    print("MODO INTERACTIVO — escribe 'exit' para salir")
    print("="*70)

    while True:
        try:
            question = input("\n> ").strip()
        except (EOFError, KeyboardInterrupt):
            break
        if question.lower() in ("exit", "quit", "q"):
            break
        if not question:
            continue

        input_ids = build_prompt(sp, question)
        response = generate(model, input_ids, sp)
        print(f"\n{response}")


def main():
    p = argparse.ArgumentParser()
    p.add_argument("--model", choices=["nano", "base"], default="base")
    p.add_argument("--checkpoint", help="Path local o s3:// al checkpoint")
    p.add_argument("--config", help="Path al JSON de config (opcional)")
    p.add_argument("--tokenizer", help="Path al tokenizer .model (opcional)")
    p.add_argument("--device", default="cpu")
    p.add_argument("--benchmark", action="store_true", help="Correr benchmark automático")
    p.add_argument("--interactive", action="store_true", help="Modo interactivo")
    p.add_argument("--out", default="inference_results.json")
    args = p.parse_args()

    # Defaults por modelo
    model_defaults = {
        "nano": {
            "checkpoint": "s3://vectrayx-sagemaker-792811916323/checkpoints/nano_sft_v5.pt",
            "config": "training_v2/configs/nano.json",
        },
        "base": {
            "checkpoint": "s3://vectrayx-sagemaker-792811916323/checkpoints/vectrayx-base-20260506-1901/phase3_last.pt",
            "config": "training_v2/configs/base.json",
        },
    }

    checkpoint = args.checkpoint or model_defaults[args.model]["checkpoint"]
    config_path = Path(args.config or model_defaults[args.model]["config"])

    # Descargar checkpoint si es S3
    if checkpoint.startswith("s3://"):
        local_ckpt = Path(f"/tmp/vectrayx_{args.model}_ckpt.pt")
        if not local_ckpt.exists():
            s3_download(checkpoint, local_ckpt)
        else:
            print(f"[cache] Usando checkpoint en caché: {local_ckpt}")
        checkpoint = local_ckpt

    # Tokenizer
    tokenizer_path = Path(args.tokenizer or "/tmp/vectrayx_bpe.model")
    if not tokenizer_path.exists():
        s3_download(
            "s3://vectrayx-sagemaker-792811916323/tokenizers/vectrayx_bpe.model",
            tokenizer_path
        )

    # Cargar
    print(f"\n[init] Cargando modelo {args.model} en {args.device}...")
    sp = spm.SentencePieceProcessor()
    sp.load(str(tokenizer_path))

    model = load_model(Path(checkpoint), config_path, args.device)

    # Ejecutar
    if args.benchmark or not args.interactive:
        run_benchmark(model, sp, Path(args.out))

    if args.interactive:
        interactive_mode(model, sp)


if __name__ == "__main__":
    main()