"""Experimental one-step BF16 LoRA for local Ornith 9B. Default: plan only.

NOT GPU-VALIDATED. No downloads, remote code, hub uploads or server changes.
Dependencies for an isolated training environment: torch, transformers>=5.8.1,
peft, accelerate. Pin the exact resolved versions before an actual run.
"""
import argparse
import json
import os
from pathlib import Path
import subprocess


def mask_prefix(full_ids, prefix_ids):
    if not prefix_ids or full_ids[:len(prefix_ids)] != prefix_ids:
        raise ValueError("Template/token prefix mismatch: no training performed")
    if len(full_ids) <= len(prefix_ids):
        raise ValueError("No answer tokens")
    return [-100] * len(prefix_ids) + full_ids[len(prefix_ids):]


def read_rows(path):
    rows = [json.loads(line) for line in path.read_text().splitlines() if line.strip()]
    if not rows:
        raise ValueError("Empty training data")
    for row in rows:
        messages = row.get("messages", [])
        if [m.get("role") for m in messages] != ["user", "assistant"]:
            raise ValueError("This smoke accepts only user/final-answer pairs, not tool trajectories")
        if any(not isinstance(m.get("content"), str) or not m["content"].strip() for m in messages):
            raise ValueError("Empty/non-text content")
    return rows


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--model-dir", type=Path, required=True)
    parser.add_argument("--data", type=Path, required=True)
    parser.add_argument("--output", type=Path, required=True)
    parser.add_argument("--max-tokens", type=int, default=256)
    parser.add_argument("--execute", action="store_true")
    parser.add_argument("--expected-gpu-uuid")
    args = parser.parse_args()
    config = json.loads((args.model_dir / "config.json").read_text())
    if config.get("architectures") != ["Qwen3_5ForConditionalGeneration"]:
        raise ValueError("Only the dense Qwen3.5 conditional-generation class is covered")
    if args.output.exists():
        raise ValueError("Output already exists; choose a new experiment directory")
    if not 32 <= args.max_tokens <= 2048:
        raise ValueError("Smoke token budget must be between 32 and 2048")
    rows = read_rows(args.data)
    plan = {"status": "PLAN_ONLY", "architecture": config["architectures"][0], "rows_available": len(rows), "rows_used": 1, "optimizer_steps": 1, "dtype": "bfloat16", "rank": 8, "max_tokens": args.max_tokens, "scope": "language q_proj/v_proj only; no vision/router/expert adapters"}
    if not args.execute:
        print(json.dumps(plan, indent=2))
        return
    gpu = args.expected_gpu_uuid
    if not gpu or not gpu.startswith("GPU-") or os.environ.get("CUDA_VISIBLE_DEVICES") != gpu:
        raise ValueError("Reserve an idle GPU and set its exact UUID in both flag and CUDA_VISIBLE_DEVICES")
    processes = subprocess.check_output(["nvidia-smi", "--query-compute-apps=gpu_uuid", "--format=csv,noheader"], text=True)
    if gpu in processes.splitlines():
        raise ValueError("GPU has compute processes; no loading attempted")
    # This is a preflight check, not an atomic GPU reservation or scheduler.
    import torch
    import transformers
    import peft
    from transformers import AutoTokenizer, Qwen3_5ForConditionalGeneration
    from peft import LoraConfig, get_peft_model
    if not torch.cuda.is_available() or not torch.cuda.is_bf16_supported():
        raise ValueError("BF16-capable CUDA GPU required")
    tokenizer = AutoTokenizer.from_pretrained(args.model_dir, local_files_only=True, trust_remote_code=False)
    messages = rows[0]["messages"]
    options = dict(tokenize=False, enable_thinking=False)
    prompt = tokenizer.apply_chat_template(messages[:-1], add_generation_prompt=True, **options)
    complete = tokenizer.apply_chat_template(messages, add_generation_prompt=False, **options)
    prefix_ids = tokenizer(prompt, add_special_tokens=False)["input_ids"]
    encoded = tokenizer(complete, add_special_tokens=False, return_tensors="pt")
    ids = encoded["input_ids"][0].tolist()
    if len(ids) > args.max_tokens:
        raise ValueError("Sample exceeds smoke token budget; select a shorter sample, do not silently truncate")
    labels = mask_prefix(ids, prefix_ids)
    model = Qwen3_5ForConditionalGeneration.from_pretrained(args.model_dir, local_files_only=True, trust_remote_code=False, dtype=torch.bfloat16, device_map={"": 0})
    targets = [name for name, module in model.named_modules() if ".language_model." in name and name.endswith((".q_proj", ".v_proj")) and isinstance(module, torch.nn.Linear)]
    if not targets:
        raise ValueError("No expected language projections; inspect architecture before adapting")
    model = get_peft_model(model, LoraConfig(r=8, lora_alpha=8, lora_dropout=0.05, target_modules=targets, task_type="CAUSAL_LM", bias="none"))
    model.config.use_cache = False
    model.gradient_checkpointing_enable()
    model.enable_input_require_grads()
    model.train()
    optimizer = torch.optim.AdamW([p for p in model.parameters() if p.requires_grad], lr=1e-4)
    batch = {key: value.to("cuda:0") for key, value in encoded.items()}
    batch["labels"] = torch.tensor([labels], device="cuda:0")
    torch.cuda.reset_peak_memory_stats()
    loss = model(**batch).loss
    if not torch.isfinite(loss):
        raise ValueError("Non-finite loss")
    loss.backward()
    norm = torch.nn.utils.clip_grad_norm_([p for p in model.parameters() if p.requires_grad], 1.0, error_if_nonfinite=True)
    optimizer.step()
    # Adapter only. This does not merge or replace base weights.
    args.output.mkdir(parents=True, exist_ok=False)
    model.save_pretrained(args.output)
    tokenizer.save_pretrained(args.output)
    report = {**plan, "status": "ONE_STEP_EXECUTED_NOT_QUALITY_VALIDATED", "loss": loss.item(), "gradient_norm": float(norm), "peak_cuda_bytes": torch.cuda.max_memory_allocated(), "targets": targets, "versions": {"torch": torch.__version__, "transformers": transformers.__version__, "peft": peft.__version__}}
    (args.output / "smoke.json").write_text(json.dumps(report, indent=2))
    print(json.dumps(report, indent=2))


if __name__ == "__main__":
    main()
