"""Lossless PQ2_0/PTQ1_0 to MLX affine 2-bit block transcoding. Preserves folded weights. Runtime Hadamard metadata remains mandatory; this module alone does not produce a loadable or validated model. """ import numpy as np def transcode(raw: bytes, shape: tuple[int, int], source: str): """Return packed uint32 weights and FP16 affine scales/biases, group size 128.""" rows, width = shape if rows <= 0 or width <= 0 or width % 128: raise ValueError( "Expected positive [output, input] shape with input divisible by 128" ) sizes = {"PQ2_0": 34, "PTQ1_0": 28} if source not in sizes: raise ValueError(f"Unsupported format: {source}") blocks = rows * width // 128 if len(raw) != blocks * sizes[source]: raise ValueError("Raw byte length does not match shape and format") data = np.frombuffer(raw, dtype=np.uint8).reshape(blocks, sizes[source]) scale_bytes = data[:, :2] if source == "PQ2_0" else data[:, 26:28] scales = scale_bytes.copy().view("> 8).astype(np.uint8)) codes = np.concatenate(pieces, axis=1) words = np.bitwise_or.reduce( codes.astype(np.uint32).reshape(rows, width // 16, 16) << (2 * np.arange(16, dtype=np.uint32)), axis=-1, ).astype("> (2 * lane)) & 3 groups = values.reshape(rows, -1, 128) return ( groups * scales.astype(np.float32)[..., None] + biases.astype(np.float32)[..., None] ).reshape(rows, -1)