# Tensor Roll — Recursive CUDA-Q Model Quantizer # Copyright (C) 2026 SnapKitty Collective # SPDX-License-Identifier: AGPL-3.0-or-later """The .trq container format (spec section 10) — a real binary codec. Layout (all integers little-endian): magic 4 bytes b'TRQ1' header_len u32 header JSON {version, arch, n_tensors, provenance, roll_meta, created} per tensor: name_len u16 name bytes ndim u8 shape ndim x u64 dtype_code u8 0=fp32 1=fp16 2=bf16 3=int8 4=int4-packed quant_code u8 0=none 1=int8-asym 2=int4-group32 3=fp16-cast 4=bf16-cast meta_len u32 meta JSON {quantization metadata: scales/zeros as base64 f64, roll metadata, codebooks} data_len u64 data bytes checksum 32 bytes sha256(data) footer 32 bytes sha256(all preceding bytes) read_trq() verifies the file checksum and every per-tensor checksum and raises on any mismatch. Corrupt files never load silently. """ from __future__ import annotations import base64 import hashlib import json import struct import numpy as np from .util import utc_now_iso MAGIC = b"TRQ1" VERSION = 1 _DTYPE_CODES = {"fp32": 0, "fp16": 1, "bf16": 2, "int8": 3, "int4": 4} _DTYPE_NAMES = {v: k for k, v in _DTYPE_CODES.items()} _NP_DTYPES = {"fp32": np.float32, "fp16": np.float16, "bf16": np.uint16, "int8": np.int8, "int4": np.uint8} _QUANT_CODES = {"none": 0, "int8-asym": 1, "int4-group32": 2, "fp16-cast": 3, "bf16-cast": 4} _QUANT_NAMES = {v: k for k, v in _QUANT_CODES.items()} def _b64(arr) -> str: return base64.b64encode(np.asarray(arr, dtype=np.float64).tobytes()).decode() def _unb64(s: str) -> np.ndarray: return np.frombuffer(base64.b64decode(s.encode()), dtype=np.float64) def _meta_to_json(meta: dict) -> dict: m = dict(meta) for k, v in list(m.items()): if isinstance(v, np.ndarray): # codebooks, scales, zero-points, or any ndarray metadata m[k] = {"__b64_f64__": _b64(np.asarray(v, dtype=np.float64)), "__shape__": list(v.shape), "__key__": k} return m def _meta_from_json(m: dict) -> dict: m = dict(m) for k, v in list(m.items()): if isinstance(v, dict) and "__b64_f64__" in v: arr = _unb64(v["__b64_f64__"]) if "__shape__" in v: arr = arr.reshape(v["__shape__"]) m[k] = arr return m class TrqError(ValueError): pass def write_trq(path: str, *, arch: dict, tensors: dict, provenance: dict, roll_meta: dict = None) -> dict: """tensors: {name: (payload_array, fmt_name, quant_meta_dict)}. Returns {"path":..., "bytes":..., "sha256":...}.""" buf = bytearray() buf += MAGIC header = { "version": VERSION, "arch": arch, "n_tensors": len(tensors), "provenance": provenance, "roll_meta": roll_meta or {}, "created": utc_now_iso(), } hbytes = json.dumps(header).encode() buf += struct.pack(" int: """Total logical parameters across tensors (uses recorded shapes, so packed INT4 counts its true parameters, not its packed bytes).""" total = 0 for _name, (arr, meta) in tensors.items(): shape = meta.get("shape") or list(arr.shape) total += int(np.prod(shape)) return total def param_budget_ok(tensors: dict, cap: float = 8_500_000_000) -> tuple: """Verify the exported artifact obeys the hard parameter budget. Returns (total_params, ok).""" total = total_params(tensors) return total, total <= cap