#!/usr/bin/env python3 """Offline frozen-Qwen query-routing distillation, with separate teacher holdout. Trains only A in q'=q A. It does not synthesize cached keys. Targets are causal attention distributions from the ordinary normalized, RoPE Q/K path. All captures and generated routing weights must stay outside the repository. """ import argparse import hashlib import json import os from pathlib import Path import struct import numpy as np def read_trace(path): layers = {} with path.open('rb') as f: if f.read(8) != b'SKLTRACE': raise ValueError('invalid teacher capture') version, count, heads, kv, dim = struct.unpack('<5I', f.read(20)) if (version, count, heads, kv, dim) != (1, 12, 24, 2, 256): raise ValueError('unsupported teacher geometry') while header := f.read(12): layer, pos, tokens = struct.unpack('<3i', header) if not 0 <= pos <= 4096 or not 0 < tokens <= 4096 or pos + tokens > 4096: raise ValueError('teacher extent exceeds offline bound') arrays = [] for h in (heads, kv, kv): raw = f.read(tokens*h*dim*4) array = np.frombuffer(raw, dtype='