#!/usr/bin/env python3
"""Fail-closed local-only QLoRA/SFT tutorial for Ornith 1.5.

The script never resolves a Hub id: ``--model`` must be a checkpoint directory
already on disk and every Transformers/Hub call uses ``local_files_only``.
Without ``--train`` it only validates metadata, tokenizer template and JSONL.
Training is deliberately opt-in and still needs a real GPU smoke test.

The 9B and 35B-A3B paths are separate. Targets are discovered from the actual
loaded module tree; ``all-linear`` is intentionally not used. Vision modules
and router parameters are excluded. MoE expert parameters require the explicit
``--include-experts`` opt-in and PEFT's ``target_parameters`` support.
"""
from __future__ import annotations

import argparse
import hashlib
import inspect
import json
import platform
import sys
from pathlib import Path
from typing import Any

ALLOWED_LINEAR_SUFFIXES = {
    "q_proj", "k_proj", "v_proj", "o_proj",
    "gate_proj", "up_proj", "down_proj",
    "in_proj_qkv", "in_proj_z", "in_proj_b", "in_proj_a", "out_proj",
}
EXPERT_PARAMETER_SUFFIXES = ("mlp.experts.gate_up_proj", "mlp.experts.down_proj")


def fail(message: str) -> None:
    raise SystemExit("FAIL-CLOSED: " + message)


def local_dir(path: Path) -> Path:
    if not path.is_dir():
        fail(f"--model must be an existing local checkpoint directory: {path}")
    if not (path / "config.json").is_file():
        fail(f"missing local config.json: {path}")
    return path


def load_config(model_path: Path):
    try:
        from transformers import AutoConfig
    except ImportError as exc:
        fail(f"Transformers is required for --check/--train and is unavailable: {exc}")

    return AutoConfig.from_pretrained(model_path, local_files_only=True, trust_remote_code=False)


def classify(config: Any, requested: str) -> str:
    model_type = str(getattr(config, "model_type", ""))
    architectures = [str(x) for x in (getattr(config, "architectures", None) or [])]
    text_config = getattr(config, "text_config", None)
    text_type = str(getattr(text_config, "model_type", ""))
    is_moe = "moe" in model_type.lower() or "moe" in text_type.lower() or any("Moe" in a for a in architectures)
    is_dense = model_type == "qwen3_5" or text_type == "qwen3_5_text" or any(a == "Qwen3_5ForConditionalGeneration" for a in architectures)
    if requested == "dense" and not is_dense:
        fail(f"requested dense but config is model_type={model_type!r}, architectures={architectures!r}")
    if requested == "moe" and not is_moe:
        fail(f"requested MoE but config is model_type={model_type!r}, architectures={architectures!r}")
    if not (is_dense or is_moe):
        fail("unsupported architecture; expected official Ornith Qwen3.5 dense or MoE config")
    if is_dense and is_moe:
        fail("ambiguous architecture: config matches dense and MoE checks")
    return "moe" if is_moe else "dense"


def tokenizer_check(model_path: Path, message_rows: list[list[dict[str, Any]]]) -> dict[str, Any]:
    try:
        from transformers import AutoTokenizer
    except ImportError as exc:
        fail(f"Transformers tokenizer support is unavailable: {exc}")

    tok = AutoTokenizer.from_pretrained(model_path, local_files_only=True, trust_remote_code=False, use_fast=True)
    if not getattr(tok, "chat_template", None):
        fail("tokenizer has no chat_template; add/review the checkpoint template before SFT")
    assistant_token_total = 0
    for row_number, messages in enumerate(message_rows, 1):
        try:
            encoded = tok.apply_chat_template(
                messages,
                tokenize=True,
                add_generation_prompt=False,
                return_dict=True,
                return_assistant_tokens_mask=True,
            )
        except TypeError:
            fail("chat_template does not expose return_assistant_tokens_mask; add Jinja generation tags or use a separately validated collator")
        except Exception as exc:
            fail(f"chat_template rendering failed on row {row_number}: {type(exc).__name__}: {exc}")
        mask = encoded.get("assistant_masks") or encoded.get("assistant_tokens_mask")
        if mask is None or not any(int(x) for x in mask):
            fail(f"assistant token mask is empty on row {row_number}; refusing to train on user/system tokens")
        input_ids = encoded.get("input_ids")
        if input_ids is None or len(mask) != len(input_ids):
            fail(f"assistant mask length does not match input_ids on row {row_number}")
        assistant_token_total += int(sum(int(x) for x in mask))
    return {
        "tokenizer_class": tok.__class__.__name__,
        "vocab_size": len(tok),
        "chat_template_has_generation_mask": True,
        "checked_rows": len(message_rows),
        "assistant_tokens": assistant_token_total,
    }


def read_jsonl(path: Path) -> list[dict[str, Any]]:
    if not path.is_file():
        fail(f"missing JSONL: {path}")
    rows: list[dict[str, Any]] = []
    for line_no, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1):
        if not line.strip():
            continue
        try:
            item = json.loads(line)
        except json.JSONDecodeError as exc:
            fail(f"{path}:{line_no}: invalid JSON: {exc.msg}")
        if not isinstance(item, dict) or not isinstance(item.get("messages"), list):
            fail(f"{path}:{line_no}: expected object with messages list")
        if not any(m.get("role") == "assistant" for m in item["messages"] if isinstance(m, dict)):
            fail(f"{path}:{line_no}: no assistant message")
        for m in item["messages"]:
            if not isinstance(m, dict) or m.get("role") not in {"system", "user", "assistant", "tool"}:
                fail(f"{path}:{line_no}: invalid message role")
        rows.append(item)
    if not rows:
        fail("JSONL is empty")
    return rows


def is_text_path(name: str) -> bool:
    low = name.lower()
    blocked = ("visual", "vision", "patch_embed", "merger", "projector", "router")
    return not any(x in low for x in blocked) and ("language_model" in low or ".layers." in low or low.startswith("layers."))


def select_linear_target_names(names: list[str]) -> list[str]:
    """Return full module paths; suffix-only targets could hit vision modules."""
    return sorted({
        name for name in names
        if is_text_path(name) and name.rsplit(".", 1)[-1] in ALLOWED_LINEAR_SUFFIXES
    })


def discover_targets(model: Any, family: str, include_experts: bool) -> dict[str, Any]:
    import torch

    all_linear_names: list[str] = []
    for name, module in model.named_modules():
        if isinstance(module, torch.nn.Linear):
            all_linear_names.append(name)
    linear_names = select_linear_target_names(all_linear_names)
    if not linear_names:
        fail("no supported text nn.Linear targets found; inspect the exact Transformers model tree")
    parameter_names: list[str] = []
    if family == "moe" and include_experts:
        for name, parameter in model.named_parameters():
            if any(name.endswith(suffix) for suffix in EXPERT_PARAMETER_SUFFIXES) and len(parameter.shape) == 3 and is_text_path(name):
                parameter_names.append(name)
        if not parameter_names:
            fail("--include-experts requested but no 3-D MoE expert parameters were found")
    if family == "dense" and include_experts:
        fail("--include-experts is only valid for the 35B-A3B MoE")
    if any("router" in x.lower() or ".gate.weight" in x.lower() for x in linear_names + parameter_names):
        fail("router target leaked into discovered targets")
    return {
        # Full paths are intentional: PEFT suffix matching with q_proj/etc.
        # could otherwise also wrap a visual q_proj with the same suffix.
        "target_modules": linear_names,
        "target_parameters": parameter_names,
        "linear_module_names": linear_names,
        "excluded_vision_and_router": True,
    }


def load_model(model_path: Path, use_4bit: bool):
    try:
        from transformers import AutoModelForImageTextToText
    except ImportError:
        fail("compatible Transformers lacks AutoModelForImageTextToText; do not substitute a causal-only loader")
    kwargs: dict[str, Any] = {
        "local_files_only": True, "trust_remote_code": False,
        "torch_dtype": "auto", "device_map": "auto",
    }
    if use_4bit:
        try:
            from transformers import BitsAndBytesConfig
        except ImportError:
            fail("BitsAndBytesConfig is unavailable; install a compatible Transformers/bitsandbytes pair")
        import torch
        kwargs["quantization_config"] = BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_quant_type="nf4",
            bnb_4bit_use_double_quant=True,
            bnb_4bit_compute_dtype=torch.bfloat16,
        )
    return AutoModelForImageTextToText.from_pretrained(model_path, **kwargs)


def make_lora_config(targets: dict[str, Any], family: str, args: argparse.Namespace):
    from peft import LoraConfig

    kwargs: dict[str, Any] = {
        "r": args.rank,
        "lora_alpha": args.alpha,
        "lora_dropout": args.dropout,
        "bias": "none",
        "task_type": "CAUSAL_LM",
        "target_modules": targets["target_modules"],
    }
    if targets["target_parameters"]:
        if "target_parameters" not in inspect.signature(LoraConfig).parameters:
            fail("installed PEFT lacks target_parameters; cannot adapt fused 3-D MoE experts safely")
        kwargs["target_parameters"] = list(targets["target_parameters"])
    return LoraConfig(**kwargs)


def train(args: argparse.Namespace, family: str, rows: list[dict[str, Any]], tokenizer_info: dict[str, Any]) -> None:
    try:
        from datasets import Dataset
        from peft import get_peft_model, prepare_model_for_kbit_training
        from trl import SFTConfig, SFTTrainer
    except ImportError as exc:
        fail(f"training dependencies unavailable: {exc}")
    try:
        import torch
    except ImportError as exc:
        fail(f"training requires PyTorch with CUDA; unavailable: {exc}")
    if not torch.cuda.is_available():
        fail("--train requires a CUDA device; use --check for the CPU-only validator and run GPU smoke separately")
    if args.bf16 and not torch.cuda.is_bf16_supported():
        fail("--bf16 requested but this CUDA device does not report BF16 support")
    model = load_model(args.model, use_4bit=not args.bf16)
    targets = discover_targets(model, family, args.include_experts)
    if not args.bf16:
        model = prepare_model_for_kbit_training(model)
    lora = make_lora_config(targets, family, args)
    model = get_peft_model(model, lora)
    model.print_trainable_parameters()
    ds = Dataset.from_list(rows)
    cfg_kwargs = {
        "output_dir": str(args.output), "num_train_epochs": args.epochs,
        "per_device_train_batch_size": args.batch_size, "gradient_accumulation_steps": args.grad_accum,
        "learning_rate": args.learning_rate, "logging_steps": 1, "save_strategy": "no",
        "bf16": True, "gradient_checkpointing": True, "packing": False,
        "assistant_only_loss": True, "max_length": args.max_length,
    }
    accepted = inspect.signature(SFTConfig).parameters
    unsupported = sorted(k for k in cfg_kwargs if k not in accepted)
    if unsupported:
        fail("installed TRL does not expose required SFTConfig fields: " + ", ".join(unsupported) + "; update/check the lock")
    trainer_kwargs: dict[str, Any] = {"model": model, "args": SFTConfig(**cfg_kwargs), "train_dataset": ds}
    trainer_sig = inspect.signature(SFTTrainer).parameters
    if "processing_class" in trainer_sig:
        trainer_kwargs["processing_class"] = args.model_tokenizer
    elif "tokenizer" in trainer_sig:
        trainer_kwargs["tokenizer"] = args.model_tokenizer
    else:
        fail("TRL SFTTrainer exposes neither processing_class nor tokenizer")
    trainer = SFTTrainer(**trainer_kwargs)
    trainer.train()
    args.output.mkdir(parents=True, exist_ok=True)
    trainer.save_model(args.output)
    args.model_tokenizer.save_pretrained(args.output)
    metadata = {
        "base_model_local_path": str(args.model), "family": family,
        "targets": targets, "tokenizer_check": tokenizer_info,
        "quantization": "nf4_double_quant" if not args.bf16 else "bf16_lora",
        "assistant_only_loss": True, "data_sha256": hashlib.sha256(args.data.read_bytes()).hexdigest(),
        "python": sys.version, "platform": platform.platform(),
    }
    (args.output / "atlas_train_metadata.json").write_text(json.dumps(metadata, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", type=Path, required=True)
    parser.add_argument("--data", type=Path, required=True)
    parser.add_argument("--output", type=Path, required=True)
    parser.add_argument("--family", choices=["auto", "dense", "moe"], default="auto")
    parser.add_argument("--check", action="store_true", help="validate local config/tokenizer/data; no model weights or GPU")
    parser.add_argument("--train", action="store_true", help="explicitly load local weights and run SFT")
    parser.add_argument("--include-experts", action="store_true", help="MoE-only; target fused expert parameters after inspection")
    parser.add_argument("--bf16", action="store_true", help="LoRA on BF16 base instead of QLoRA NF4")
    parser.add_argument("--rank", type=int, default=16)
    parser.add_argument("--alpha", type=int, default=32)
    parser.add_argument("--dropout", type=float, default=0.05)
    parser.add_argument("--epochs", type=float, default=1.0)
    parser.add_argument("--batch-size", type=int, default=1)
    parser.add_argument("--grad-accum", type=int, default=8)
    parser.add_argument("--learning-rate", type=float, default=1e-4)
    parser.add_argument("--max-length", type=int, default=256)
    args = parser.parse_args()
    if args.train and not args.bf16:
        fail("NF4 suspendido en kit v4.7: revisar Qwen3.5; usar ruta BF16 experimental separada")
    if args.check == args.train:
        fail("choose exactly one of --check or --train")
    args.model = local_dir(args.model)
    args.data = args.data.resolve()
    config = load_config(args.model)
    family = classify(config, args.family)
    rows = read_jsonl(args.data)
    tokenizer_info = tokenizer_check(args.model, [row["messages"] for row in rows])
    print(json.dumps({"ok": True, "family": family, "rows": len(rows), "tokenizer": tokenizer_info}, indent=2, ensure_ascii=False))
    if args.check:
        return
    # Train mode needs the tokenizer object for TRL and validates the model tree
    # only after weights are loaded. No Hub/network fallback is permitted.
    from transformers import AutoTokenizer
    args.model_tokenizer = AutoTokenizer.from_pretrained(args.model, local_files_only=True, trust_remote_code=False, use_fast=True)
    train(args, family, rows, tokenizer_info)


if __name__ == "__main__":
    main()
