Spaces:
Sleeping
Sleeping
Download training/generate_sft_data.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 11.7 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/training/generate_sft_data.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/training/generate_sft_data.py
-
curl -L -o generate_sft_data.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/training/generate_sft_data.py
11.7 kB
| """Generate SFT training data from expert rollouts through the sim environment. | |
| Runs expert policy (ground_truth solutions) through the Python sim with varied | |
| topology parameters to create diverse observation data. Each episode follows: | |
| investigate (randomized order) -> diagnose -> remediate -> verify -> confirm_fix. | |
| Usage: | |
| NETOPS_SIM=1 python training/generate_sft_data.py [--seeds N] [--output PATH] | |
| """ | |
| import argparse | |
| import json | |
| import logging | |
| import os | |
| import random | |
| import sys | |
| import time | |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) | |
| os.environ["NETOPS_SIM"] = "1" | |
| from models import NetOpsAction | |
| from server.environment import NetOpsEnvironment | |
| from server.ground_truth import SOLUTIONS, resolve_steps, resolve_compound_steps | |
| from training.data_utils import episode_to_chat, FAULT_TO_DOMAIN, validate_dataset | |
| logger = logging.getLogger(__name__) | |
| TOPOLOGY_SCALES = [(3, 3), (4, 2), (3, 4), (4, 3), (5, 2)] | |
| INVESTIGATION_COMMANDS = { | |
| "link": ["query_topology", "query_device"], | |
| "traffic": ["query_device", "query_traffic"], | |
| "ospf": ["query_routing", "query_topology"], | |
| "bgp": ["query_routing", "query_topology"], | |
| "firewall": ["query_device", "query_topology"], | |
| "system": ["query_device", "query_events"], | |
| "compound": ["query_topology", "query_device", "query_routing"], | |
| } | |
| EXTRA_INVESTIGATION = ["query_topology", "query_routing", "query_device", | |
| "query_traffic", "query_events"] | |
| def pick_investigation_steps(fault_type: str, details: dict, rng: random.Random, | |
| all_routers: list[str]) -> list[dict]: | |
| """Generate randomized investigation steps appropriate for the fault domain.""" | |
| domain = FAULT_TO_DOMAIN.get(fault_type, "link") | |
| primary_cmds = list(INVESTIGATION_COMMANDS.get(domain, ["query_topology"])) | |
| rng.shuffle(primary_cmds) | |
| affected_router = (details.get("router") or details.get("router_a") | |
| or details.get("border_router") or "r1") | |
| steps = [] | |
| used_cmds = set() | |
| for cmd in primary_cmds[:2]: | |
| if cmd in ("query_routing", "query_device"): | |
| if rng.random() < 0.3 and len(all_routers) > 1: | |
| wrong_router = rng.choice([r for r in all_routers if r != affected_router] | |
| or [affected_router]) | |
| steps.append({"action": cmd, "args": {"router": wrong_router}}) | |
| used_cmds.add(cmd) | |
| steps.append({"action": cmd, "args": {"router": affected_router}}) | |
| else: | |
| steps.append({"action": cmd, "args": {"router": affected_router}}) | |
| else: | |
| steps.append({"action": cmd, "args": {}}) | |
| used_cmds.add(cmd) | |
| if rng.random() < 0.4: | |
| extras = [c for c in EXTRA_INVESTIGATION if c not in used_cmds] | |
| if extras: | |
| extra_cmd = rng.choice(extras) | |
| if extra_cmd in ("query_routing", "query_device"): | |
| router = rng.choice(all_routers) | |
| steps.append({"action": extra_cmd, "args": {"router": router}}) | |
| else: | |
| steps.append({"action": extra_cmd, "args": {}}) | |
| return steps | |
| def pick_verify_step(investigation_cmds: list[str], affected_router: str, | |
| rng: random.Random) -> dict: | |
| """Pick a verification query different from the first investigation step.""" | |
| candidates = ["query_topology", "query_routing", "query_device"] | |
| first_inv = investigation_cmds[0] if investigation_cmds else "" | |
| preferred = [c for c in candidates if c != first_inv] or candidates | |
| cmd = rng.choice(preferred) | |
| if cmd in ("query_routing", "query_device"): | |
| return {"action": cmd, "args": {"router": affected_router}} | |
| return {"action": cmd, "args": {}} | |
| def run_expert_episode(env: NetOpsEnvironment, fault_type: str, seed: int, | |
| routers_per_as: int, num_ases: int, | |
| rng: random.Random) -> dict | None: | |
| """Run one expert episode and return the episode data, or None on failure.""" | |
| difficulty = SOLUTIONS[fault_type]["difficulty"] | |
| is_compound = SOLUTIONS[fault_type].get("compound", False) | |
| try: | |
| obs = env.reset(difficulty=difficulty, fault_type=fault_type, seed=seed, | |
| routers_per_as=routers_per_as, num_ases=num_ases) | |
| except Exception as e: | |
| logger.warning("Reset failed for %s seed=%d scale=%dx%d: %s", | |
| fault_type, seed, routers_per_as, num_ases, e) | |
| return None | |
| alert = obs.alert | |
| ground_truths = env._injector.ground_truths | |
| gt = ground_truths[0] | |
| if is_compound: | |
| fix_steps = resolve_compound_steps(fault_type, ground_truths) | |
| else: | |
| fix_steps = resolve_steps(fault_type, gt.details) | |
| if not fix_steps: | |
| logger.warning("No solution steps for %s", fault_type) | |
| return None | |
| diag_steps = [s for s in fix_steps if s["action"] == "diagnose"] | |
| remediation_steps = [s for s in fix_steps | |
| if s["action"] not in ("diagnose", "query_topology", | |
| "query_device", "query_routing", | |
| "query_traffic", "query_events")] | |
| all_routers = [] | |
| for asn_cfg in env._net_mgr._topo_config.values() if hasattr(env._net_mgr, '_topo_config') else []: | |
| all_routers.extend(asn_cfg.get("routers", [])) | |
| if not all_routers: | |
| from server.constants import AS_CONFIGS | |
| for cfg in AS_CONFIGS.values(): | |
| all_routers.extend(cfg["routers"]) | |
| affected_router = (gt.details.get("router") or gt.details.get("router_a") | |
| or gt.details.get("border_router") or "r1") | |
| inv_steps = pick_investigation_steps(fault_type, gt.details, rng, all_routers) | |
| history = [] | |
| # Phase 1: Investigation | |
| for step_def in inv_steps: | |
| action = NetOpsAction(command=step_def["action"], args=step_def["args"]) | |
| obs = env.step(action) | |
| history.append({ | |
| "action": {"command": step_def["action"], "args": step_def["args"]}, | |
| "observation": obs.observation, | |
| }) | |
| if obs.done: | |
| return None | |
| # Phase 2: Diagnosis | |
| for step_def in diag_steps: | |
| action = NetOpsAction(command=step_def["action"], args=step_def["args"]) | |
| obs = env.step(action) | |
| history.append({ | |
| "action": {"command": step_def["action"], "args": step_def["args"]}, | |
| "observation": obs.observation, | |
| }) | |
| if obs.done: | |
| return None | |
| # Phase 3: Remediation | |
| for step_def in remediation_steps: | |
| action = NetOpsAction(command=step_def["action"], args=step_def["args"]) | |
| obs = env.step(action) | |
| history.append({ | |
| "action": {"command": step_def["action"], "args": step_def["args"]}, | |
| "observation": obs.observation, | |
| }) | |
| if obs.done: | |
| return None | |
| # Phase 4: Verify | |
| inv_cmds_used = [s["action"] for s in inv_steps] | |
| verify = pick_verify_step(inv_cmds_used, affected_router, rng) | |
| action = NetOpsAction(command=verify["action"], args=verify["args"]) | |
| obs = env.step(action) | |
| history.append({ | |
| "action": {"command": verify["action"], "args": verify["args"]}, | |
| "observation": obs.observation, | |
| }) | |
| if obs.done: | |
| return None | |
| # Phase 5: Confirm fix | |
| action = NetOpsAction(command="confirm_fix", args={}) | |
| obs = env.step(action) | |
| history.append({ | |
| "action": {"command": "confirm_fix", "args": {}}, | |
| "observation": obs.observation, | |
| }) | |
| fault_fixed = "Fault fixed: True" in obs.observation | |
| # Apply 6-turn sliding window (matching inference) | |
| if len(history) > 7: | |
| # Keep first turn (has alert context) + last 6 turns | |
| # Actually: at inference, history is built incrementally, and the | |
| # sliding window keeps the last 6 entries. The first user message | |
| # always contains the alert. So we keep last 6 action-observation | |
| # pairs; the alert is prepended to the first visible observation. | |
| history = history[-7:] | |
| chat = episode_to_chat(alert, history) | |
| chat["metadata"] = { | |
| "fault_type": fault_type, | |
| "difficulty": difficulty, | |
| "domain": FAULT_TO_DOMAIN.get(fault_type, "unknown"), | |
| "source": "expert", | |
| "routers_per_as": routers_per_as, | |
| "num_ases": num_ases, | |
| "fault_fixed": fault_fixed, | |
| "steps": len(history), | |
| "seed": seed, | |
| } | |
| return chat | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Generate SFT training data") | |
| parser.add_argument("--seeds", type=int, default=7, | |
| help="Seeds per fault type (default: 7)") | |
| parser.add_argument("--output", default="training/data/sft_train.jsonl") | |
| parser.add_argument("--verbose", action="store_true") | |
| args = parser.parse_args() | |
| logging.basicConfig(level=logging.INFO if args.verbose else logging.WARNING, | |
| format="%(levelname)s %(message)s") | |
| env = NetOpsEnvironment() | |
| fault_types = list(SOLUTIONS.keys()) | |
| total = len(fault_types) * args.seeds | |
| print(f"Generating {total} episodes ({len(fault_types)} faults x {args.seeds} seeds)") | |
| episodes = [] | |
| failures = [] | |
| t0 = time.time() | |
| for fi, fault_type in enumerate(fault_types): | |
| for seed_idx in range(args.seeds): | |
| seed = fi * 1000 + seed_idx | |
| rng = random.Random(seed) | |
| scale = TOPOLOGY_SCALES[seed_idx % len(TOPOLOGY_SCALES)] | |
| routers_per_as, num_ases = scale | |
| result = run_expert_episode(env, fault_type, seed, | |
| routers_per_as, num_ases, rng) | |
| if result is None: | |
| failures.append(f"{fault_type} seed={seed} scale={scale}") | |
| continue | |
| episodes.append(result) | |
| if (fi * args.seeds + seed_idx + 1) % 50 == 0: | |
| print(f" {fi * args.seeds + seed_idx + 1}/{total} episodes " | |
| f"({len(episodes)} ok, {len(failures)} failed)") | |
| env.close() | |
| elapsed = time.time() - t0 | |
| os.makedirs(os.path.dirname(args.output), exist_ok=True) | |
| with open(args.output, "w") as f: | |
| for ep in episodes: | |
| f.write(json.dumps(ep, ensure_ascii=False) + "\n") | |
| fixed_count = sum(1 for e in episodes if e["metadata"]["fault_fixed"]) | |
| domains = {} | |
| for e in episodes: | |
| d = e["metadata"]["domain"] | |
| domains[d] = domains.get(d, 0) + 1 | |
| print(f"\nDone in {elapsed:.1f}s") | |
| print(f" Episodes: {len(episodes)}/{total} ({len(failures)} failed)") | |
| print(f" Fault fixed: {fixed_count}/{len(episodes)}") | |
| print(f" Domains: {domains}") | |
| print(f" Output: {args.output}") | |
| if failures: | |
| print(f"\n Failed ({len(failures)}):") | |
| for f_msg in failures[:10]: | |
| print(f" {f_msg}") | |
| if len(failures) > 10: | |
| print(f" ... and {len(failures) - 10} more") | |
| # Validate | |
| print(f"\nValidating {args.output}...") | |
| stats = validate_dataset(args.output) | |
| print(f" Valid: {stats['valid']}/{stats['total']}") | |
| print(f" confirm_fix: {stats['has_confirm_fix']}/{stats['valid']}") | |
| print(f" Steps: min={min(stats['step_counts'])}, " | |
| f"median={sorted(stats['step_counts'])[len(stats['step_counts'])//2]}, " | |
| f"max={max(stats['step_counts'])}") | |
| print(f" Fault types: {len(stats['fault_types'])}") | |
| if stats["errors"]: | |
| print(f" Errors: {stats['errors'][:5]}") | |
| if __name__ == "__main__": | |
| main() | |