#!/usr/bin/env python3
"""Read-only header planner. Does not import torch, load tensor payloads or quantize."""
import json
from pathlib import Path
import re
import struct

SOURCE = Path("/home/lab/models/ornith-modular/original-text")
IGNORE = ["re:.*embed_tokens.*", "re:.*norm.*", "re:.*conv1d.*", "re:.*mtp.*", "re:.*visual.*"]

def main():
    quantized = []
    preserved = []
    original_bytes = packed_estimated_bytes = 0
    for file in sorted(SOURCE.glob("*.safetensors")):
        with file.open("rb") as stream:
            size = struct.unpack("<Q", stream.read(8))[0]
            header = json.loads(stream.read(size))
        for name, item in header.items():
            if name == "__metadata__":
                continue
            shape = item["shape"]
            size = item["data_offsets"][1] - item["data_offsets"][0]
            original_bytes += size
            module, _, param = name.rpartition(".")
            ignored = any(re.match(pattern[3:], module) for pattern in IGNORE)
            selected = param == "weight" and not ignored
            if selected:
                assert len(shape) == 2 and shape[1] % 128 == 0, (name, shape)
                rows, cols = shape
                # INT8 values packed in INT32 + BF16 scale per128 + int64shape.
                packed_estimated_bytes += rows * cols + rows * (cols // 128) * 2 + 16
                quantized.append({"name": name, "shape": shape})
            else:
                packed_estimated_bytes += size
                preserved.append({"name": name, "shape": shape, "dtype": item["dtype"]})
    print(json.dumps({
        "status": "PLAN_ONLY_NOT_CONVERTED",
        "source": str(SOURCE),
        "proposed_destination": "/home/lab/models/ornith-modular/full-int8-text",
        "scheme": {"targets": ["Linear", "re:.*lm_head$"], "format": "pack-quantized", "weights": {"type": "int", "num_bits": 8, "symmetric": True, "strategy": "group", "group_size": 128, "dynamic": False}, "input_activations": None, "output_activations": None},
        "ignore": IGNORE,
        "source_payload_bytes": original_bytes,
        "estimated_output_payload_bytes": packed_estimated_bytes,
        "estimated_output_GiB": packed_estimated_bytes / 2**30,
        "quantized_tensor_count": len(quantized),
        "preserved_tensor_count": len(preserved),
        "quantized": quantized,
        "preserved": preserved,
        "caveats": ["No runtime validation or quality guarantee", "TP1 only: in_proj_b/a fuse to64 output rows", "lm_head requires explicit regex target on SGLang", "Preserve non-linear SSM tensor dtypes and bytes exactly", "Do not execute conversion before parent approval following MLP quality tests"],
    }, indent=2))

if __name__ == "__main__":
    main()
