#!/usr/bin/env python3 """Deterministically convert the pinned DSpark tensors into one GGUF v3 file.""" from __future__ import annotations import argparse import math import os from concurrent.futures import ThreadPoolExecutor from pathlib import Path from typing import BinaryIO import numpy as np from formats import SafeTensorSet, sha256_path, verify_source_files, write_gguf from recipe import ( DEFAULT_RECIPE, KIND_FP8_TO_Q8_0, KIND_FUSED_EXPERTS_MXFP4, KIND_FUSED_EXPERTS_Q2_K, KIND_MARKOV_F16, KIND_PLAIN_F32, KIND_RELABEL_F32, N_EXPERTS, RECIPES, ArtifactRecipe, TensorRecipe, build_plan, resolve_recipe, source_paths, ) def _e4m3_value(byte: int) -> np.float32: sign = -1.0 if byte & 0x80 else 1.0 exponent = (byte >> 3) & 0x0F mantissa = byte & 0x07 if exponent == 0x0F and mantissa == 0x07: return np.float32(np.nan) if exponent == 0: return np.float32(sign * (mantissa / 8.0) * math.ldexp(1.0, 1 - 7)) return np.float32(sign * (1.0 + mantissa / 8.0) * math.ldexp(1.0, exponent - 7)) E4M3_TABLE = np.array([_e4m3_value(byte) for byte in range(256)], dtype=np.float32) E8M0_TABLE = np.array( [ np.float32(np.nan) if byte == 0xFF else np.float32(math.ldexp(1.0, byte - 127)) for byte in range(256) ], dtype=np.float32, ) FP4_TABLE = np.array( [ 0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0, 0.0, -0.5, -1.0, -1.5, -2.0, -3.0, -4.0, -6.0, ], dtype=np.float32, ) Q2_K_BLOCK_SIZE = 256 Q2_K_BLOCK_BYTES = 84 # Both routed shapes contain at most 4,096 rows. Each task keeps one decoded # expert resident at a time; the bounded worker count limits the larger set of # NumPy fitting temporaries while avoiding tiny-band dispatch overhead across # the 2,304 experts in the full artifact. EXPERT_ROW_BATCH = 4096 Q2_K_EXPERT_WORKERS = max(1, min(8, os.cpu_count() or 1)) def bf16_bits_to_f32(bits: np.ndarray) -> np.ndarray: """Convert little-endian BF16 payloads exactly by placing them in F32 high bits.""" wide = np.asarray(bits, dtype=" np.ndarray: values = np.asarray(values, dtype=np.float32) truncated = np.trunc(values).astype(np.float32, copy=False) fraction = np.abs(values - truncated) return np.where( fraction >= np.float32(0.5), truncated + np.copysign(np.float32(1.0), values), truncated, ).astype(np.float32, copy=False) def nearest_int_f32(values: np.ndarray) -> np.ndarray: """Port ds4q_nearest_int's binary32 round-to-nearest-even bit trick.""" f32 = np.asarray(values, dtype=np.float32) if not np.isfinite(f32).all() or np.any(np.abs(f32) > np.float32(4_194_303.0)): raise ValueError("nearest-int input is outside the DS4 reference range") shifted = np.add(f32, np.float32(12_582_912.0), dtype=np.float32) bits = shifted.view(np.int32) return ((bits & np.int32(0x007F_FFFF)) - np.int32(0x0040_0000)).astype( np.int32, copy=False ) def _make_qkx2_quants( groups: np.ndarray, ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: """Vectorized binary32 port of DS4's unweighted 16-value Q2_K fit.""" x = np.asarray(groups, dtype=np.float32) if x.ndim != 2 or x.shape[1] != 16: raise ValueError("Q2_K fitting groups must have shape (n, 16)") weights = np.abs(x).astype(np.float32, copy=False) minimum = x[:, 0].copy() maximum = x[:, 0].copy() for index in range(1, 16): minimum = np.minimum(minimum, x[:, index]) maximum = np.maximum(maximum, x[:, index]) minimum = np.where(minimum > 0, np.float32(0.0), minimum).astype( np.float32, copy=False ) constant = maximum == minimum value_range = np.subtract(maximum, minimum, dtype=np.float32) safe_range = np.where(constant, np.float32(1.0), value_range).astype( np.float32, copy=False ) inverse_scale = np.divide(np.float32(3.0), safe_range, dtype=np.float32) scale = np.divide(np.float32(1.0), inverse_scale, dtype=np.float32) normalized = np.multiply( np.subtract(x, minimum[:, None], dtype=np.float32), inverse_scale[:, None], dtype=np.float32, ) labels = np.clip(nearest_int_f32(normalized), 0, 3).astype(np.uint8) scale[constant] = np.float32(0.0) labels[constant] = np.uint8(0) sum_w = weights[:, 0].copy() sum_x = np.multiply(weights[:, 0], x[:, 0], dtype=np.float32) for index in range(1, 16): sum_w = np.add(sum_w, weights[:, index], dtype=np.float32) sum_x = np.add( sum_x, np.multiply(weights[:, index], x[:, index], dtype=np.float32), dtype=np.float32, ) best_error = np.zeros(x.shape[0], dtype=np.float32) for index in range(16): reconstructed = np.add( np.multiply(scale, labels[:, index], dtype=np.float32), minimum, dtype=np.float32, ) difference = np.abs( np.subtract(reconstructed, x[:, index], dtype=np.float32) ).astype(np.float32, copy=False) best_error = np.add( best_error, np.multiply(weights[:, index], difference, dtype=np.float32), dtype=np.float32, ) for step in range(16): candidate_numerator = np.add( np.add( np.float32(-0.5), np.multiply(np.float32(0.1), np.float32(step), dtype=np.float32), dtype=np.float32, ), np.float32(3.0), dtype=np.float32, ) candidate_range = np.subtract(maximum, minimum, dtype=np.float32) safe_candidate_range = np.where( constant, np.float32(1.0), candidate_range ).astype(np.float32, copy=False) candidate_inverse = np.divide( candidate_numerator, safe_candidate_range, dtype=np.float32 ) candidate_labels = np.clip( nearest_int_f32( np.multiply( np.subtract(x, minimum[:, None], dtype=np.float32), candidate_inverse[:, None], dtype=np.float32, ) ), 0, 3, ).astype(np.uint8) sum_l = np.zeros(x.shape[0], dtype=np.float32) sum_l2 = np.zeros(x.shape[0], dtype=np.float32) sum_xl = np.zeros(x.shape[0], dtype=np.float32) for index in range(16): weighted_label = np.multiply( weights[:, index], candidate_labels[:, index], dtype=np.float32 ) sum_l = np.add(sum_l, weighted_label, dtype=np.float32) sum_l2 = np.add( sum_l2, np.multiply( weighted_label, candidate_labels[:, index], dtype=np.float32 ), dtype=np.float32, ) sum_xl = np.add( sum_xl, np.multiply(weighted_label, x[:, index], dtype=np.float32), dtype=np.float32, ) determinant = np.subtract( np.multiply(sum_w, sum_l2, dtype=np.float32), np.multiply(sum_l, sum_l, dtype=np.float32), dtype=np.float32, ) valid = np.logical_and(~constant, determinant > 0) safe_determinant = np.where(valid, determinant, np.float32(1.0)).astype( np.float32, copy=False ) candidate_scale = np.divide( np.subtract( np.multiply(sum_w, sum_xl, dtype=np.float32), np.multiply(sum_x, sum_l, dtype=np.float32), dtype=np.float32, ), safe_determinant, dtype=np.float32, ) candidate_minimum = np.divide( np.subtract( np.multiply(sum_l2, sum_x, dtype=np.float32), np.multiply(sum_l, sum_xl, dtype=np.float32), dtype=np.float32, ), safe_determinant, dtype=np.float32, ) positive_minimum = np.logical_and(valid, candidate_minimum > 0) safe_sum_l2 = np.where(positive_minimum, sum_l2, np.float32(1.0)).astype( np.float32, copy=False ) zero_minimum_scale = np.divide(sum_xl, safe_sum_l2, dtype=np.float32) candidate_scale = np.where( positive_minimum, zero_minimum_scale, candidate_scale ).astype(np.float32, copy=False) candidate_minimum = np.where( positive_minimum, np.float32(0.0), candidate_minimum ).astype(np.float32, copy=False) current_error = np.zeros(x.shape[0], dtype=np.float32) for index in range(16): reconstructed = np.add( np.multiply( candidate_scale, candidate_labels[:, index], dtype=np.float32 ), candidate_minimum, dtype=np.float32, ) difference = np.abs( np.subtract(reconstructed, x[:, index], dtype=np.float32) ).astype(np.float32, copy=False) current_error = np.add( current_error, np.multiply(weights[:, index], difference, dtype=np.float32), dtype=np.float32, ) improved = np.logical_and(valid, current_error < best_error) labels[improved] = candidate_labels[improved] best_error[improved] = current_error[improved] scale[improved] = candidate_scale[improved] minimum[improved] = candidate_minimum[improved] return scale, np.negative(minimum, dtype=np.float32), labels def quantize_q2_k(values: np.ndarray) -> np.ndarray: """Encode rows as unweighted GGML Q2_K blocks using the DS4 reference path.""" rows = np.asarray(values, dtype=np.float32) if rows.ndim != 2 or rows.shape[1] % Q2_K_BLOCK_SIZE: raise ValueError("Q2_K input must be 2D with columns divisible by 256") if not np.isfinite(rows).all(): raise ValueError("Q2_K input contains NaN or infinity") blocks = rows.reshape(-1, Q2_K_BLOCK_SIZE) groups = blocks.reshape(-1, 16) scales, mins, labels = _make_qkx2_quants(groups) scales = scales.reshape(-1, 16) mins = mins.reshape(-1, 16) labels = labels.reshape(-1, Q2_K_BLOCK_SIZE) max_scale = np.maximum(np.max(scales, axis=1), np.float32(0.0)).astype( np.float32, copy=False ) max_min = np.maximum(np.max(mins, axis=1), np.float32(0.0)).astype( np.float32, copy=False ) scale_inverse = np.zeros_like(max_scale) min_inverse = np.zeros_like(max_min) np.divide( np.float32(15.0), max_scale, out=scale_inverse, where=max_scale > 0, ) np.divide(np.float32(15.0), max_min, out=min_inverse, where=max_min > 0) scale_codes = nearest_int_f32( np.multiply(scales, scale_inverse[:, None], dtype=np.float32) ) min_codes = nearest_int_f32( np.multiply(mins, min_inverse[:, None], dtype=np.float32) ) if np.any(scale_codes < 0) or np.any(scale_codes > 15): raise ValueError("Q2_K scale code is outside four bits") if np.any(min_codes < 0) or np.any(min_codes > 15): raise ValueError("Q2_K minimum code is outside four bits") packed_scales = scale_codes.astype(np.uint8) | ( min_codes.astype(np.uint8) << np.uint8(4) ) d_bits = ( np.divide(max_scale, np.float32(15.0), dtype=np.float32) .astype("> np.uint8(4), dtype=np.float32 ).reshape(-1) fitted_groups = labels.reshape(-1, 16) nonzero = group_d != 0 normalized = np.divide( np.add(groups[nonzero], group_min[nonzero, None], dtype=np.float32), group_d[nonzero, None], dtype=np.float32, ) fitted_groups[nonzero] = np.clip(nearest_int_f32(normalized), 0, 3).astype(np.uint8) encoded = np.zeros((blocks.shape[0], Q2_K_BLOCK_BYTES), dtype=np.uint8) encoded[:, :16] = packed_scales for start in (0, 128): packed = ( labels[:, start : start + 32] | (labels[:, start + 32 : start + 64] << np.uint8(2)) | (labels[:, start + 64 : start + 96] << np.uint8(4)) | (labels[:, start + 96 : start + 128] << np.uint8(6)) ) offset = 16 + start // 4 encoded[:, offset : offset + 32] = packed encoded[:, 80:82] = d_bits.astype(" np.ndarray: """Decode one bounded row band of the pinned OCP E2M1/E8M0 source.""" if cols % 32: raise ValueError("source MXFP4 column count must be divisible by 32") blocks = cols // 32 packed_blocks = np.asarray(packed, dtype=np.uint8).reshape(rows, blocks, 16) scale_codes = np.asarray(scales, dtype=np.uint8).reshape(rows, blocks) if np.any(scale_codes == 0xFF): raise ValueError("source MXFP4 contains reserved E8M0 NaN scale 0xff") codes = np.empty((rows, blocks, 32), dtype=np.uint8) codes[..., 0::2] = packed_blocks & np.uint8(0x0F) codes[..., 1::2] = packed_blocks >> np.uint8(4) decoded = FP4_TABLE[codes] decoded *= E8M0_TABLE[scale_codes][..., None] return decoded.reshape(rows, cols) def quantize_q8_0(values: np.ndarray) -> np.ndarray: """Encode flat F32 data as GGML Q8_0 using Rust/C-style half-away rounding.""" flat = np.asarray(values, dtype=np.float32).reshape(-1) if flat.size % 32: raise ValueError("Q8_0 input length must be divisible by 32") blocks = flat.reshape(-1, 32) if not np.isfinite(blocks).all(): raise ValueError("Q8_0 input contains NaN or infinity") maxima = np.max(np.abs(blocks), axis=1).astype(np.float32, copy=False) scales = maxima / np.float32(127.0) inverse = np.zeros_like(scales) np.divide(np.float32(1.0), scales, out=inverse, where=scales != 0) quantized = round_away_from_zero(blocks * inverse[:, None]) if np.any(quantized < -127) or np.any(quantized > 127): raise ValueError("Q8_0 quantization produced an out-of-range code") codes = quantized.astype(np.int8) encoded = np.empty((blocks.shape[0], 34), dtype=np.uint8) encoded[:, :2] = scales.astype(" np.ndarray: """Purely permute source FP4 nibbles into GGML's 17-byte MXFP4 blocks.""" if cols % 32: raise ValueError("MXFP4 column count must be divisible by 32") blocks = cols // 32 packed_blocks = np.asarray(packed, dtype=np.uint8).reshape(rows, blocks, 16) scale_rows = np.asarray(scales, dtype=np.uint8).reshape(rows, blocks) if np.any(scale_rows == 0xFF): raise ValueError("MXFP4 source contains reserved E8M0 NaN scale 0xff") low_codes = np.empty((rows, blocks, 16), dtype=np.uint8) high_codes = np.empty((rows, blocks, 16), dtype=np.uint8) low_bytes = packed_blocks[..., :8] high_bytes = packed_blocks[..., 8:] low_codes[..., 0::2] = low_bytes & np.uint8(0x0F) low_codes[..., 1::2] = low_bytes >> np.uint8(4) high_codes[..., 0::2] = high_bytes & np.uint8(0x0F) high_codes[..., 1::2] = high_bytes >> np.uint8(4) encoded = np.empty((rows, blocks, 17), dtype=np.uint8) encoded[..., 0] = scale_rows encoded[..., 1:] = low_codes | (high_codes << np.uint8(4)) return encoded class TensorProducer: def __init__(self, source: SafeTensorSet): self.source = source def __call__(self, tensor: TensorRecipe, handle: BinaryIO) -> int: print(f"[convert] {tensor.out_name} ({tensor.byte_len:,} bytes)", flush=True) if tensor.kind == KIND_PLAIN_F32: return self._plain_f32(tensor, handle) if tensor.kind == KIND_RELABEL_F32: return self._copy_f32(tensor, handle) if tensor.kind == KIND_FP8_TO_Q8_0: return self._fp8_to_q8_0(tensor, handle) if tensor.kind == KIND_FUSED_EXPERTS_MXFP4: return self._fused_experts_mxfp4(tensor, handle) if tensor.kind == KIND_FUSED_EXPERTS_Q2_K: return self._fused_experts_q2_k(tensor, handle) if tensor.kind == KIND_MARKOV_F16: return self._markov_f16(tensor, handle) raise ValueError(f"unsupported tensor recipe kind: {tensor.kind}") def _plain_f32(self, tensor: TensorRecipe, handle: BinaryIO) -> int: if not tensor.src_is_bf16: raw = self.source.raw(tensor.src_name) try: return handle.write(raw) finally: raw.release() bits = self.source.array(tensor.src_name, " int: raw = self.source.raw(tensor.src_name) try: return handle.write(raw) finally: raw.release() def _fp8_to_q8_0(self, tensor: TensorRecipe, handle: BinaryIO) -> int: weights = self.source.array(tensor.weight_name, np.uint8).reshape( tensor.rows, tensor.cols ) scale_codes = self.source.array(tensor.scale_name, np.uint8).reshape( (tensor.rows + 127) // 128, (tensor.cols + 127) // 128, ) written = 0 for block_row in range(scale_codes.shape[0]): row_start = block_row * 128 row_end = min(row_start + 128, tensor.rows) decoded = E4M3_TABLE[weights[row_start:row_end]] column_scales = np.repeat(E8M0_TABLE[scale_codes[block_row]], 128)[ : tensor.cols ] if not np.isfinite(column_scales).all(): raise ValueError(f"{tensor.scale_name}: contains reserved E8M0 NaN") decoded *= column_scales[None, :] encoded = quantize_q8_0(decoded) written += handle.write(encoded.tobytes(order="C")) return written def _fused_experts_mxfp4(self, tensor: TensorRecipe, handle: BinaryIO) -> int: written = 0 for expert in range(N_EXPERTS): prefix = f"{tensor.name_prefix}.ffn.experts.{expert}.{tensor.expert_kind}" packed = self.source.array(f"{prefix}.weight", np.uint8) scales = self.source.array(f"{prefix}.scale", np.uint8) encoded = repack_mxfp4( packed, scales, tensor.rows, tensor.cols, ) written += handle.write(encoded.tobytes(order="C")) return written def _fused_experts_q2_k(self, tensor: TensorRecipe, handle: BinaryIO) -> int: written = 0 with ThreadPoolExecutor(max_workers=Q2_K_EXPERT_WORKERS) as executor: for start in range(0, N_EXPERTS, Q2_K_EXPERT_WORKERS): futures = [ executor.submit(self._encode_q2_k_expert, tensor, expert) for expert in range( start, min(start + Q2_K_EXPERT_WORKERS, N_EXPERTS) ) ] # Results are written in expert order, independent of worker # completion order, so concurrency cannot change GGUF bytes. for future in futures: written += handle.write(future.result()) return written def _encode_q2_k_expert(self, tensor: TensorRecipe, expert: int) -> bytes: prefix = f"{tensor.name_prefix}.ffn.experts.{expert}.{tensor.expert_kind}" packed = self.source.array(f"{prefix}.weight", np.uint8).reshape( tensor.rows, tensor.cols // 2 ) scales = self.source.array(f"{prefix}.scale", np.uint8).reshape( tensor.rows, tensor.cols // 32 ) chunks: list[bytes] = [] for row_start in range(0, tensor.rows, EXPERT_ROW_BATCH): row_end = min(row_start + EXPERT_ROW_BATCH, tensor.rows) decoded = decode_source_mxfp4( packed[row_start:row_end], scales[row_start:row_end], row_end - row_start, tensor.cols, ) chunks.append(quantize_q2_k(decoded).tobytes(order="C")) return b"".join(chunks) def _markov_f16(self, tensor: TensorRecipe, handle: BinaryIO) -> int: bits = self.source.array(tensor.src_name, " tuple[int, str]: artifact_recipe = resolve_recipe(recipe) source_dir = source_dir.resolve() output = output.resolve() if output.exists() and not force: raise FileExistsError( f"output already exists: {output}; pass --force to replace it" ) output.parent.mkdir(parents=True, exist_ok=True) print("[convert] validating pinned source sizes and SHA-256 values", flush=True) verify_source_files(source_dir) plan = build_plan(artifact_recipe) temporary = output.with_name(f".{output.name}.partial-{os.getpid()}") if temporary.exists(): raise FileExistsError(f"temporary output already exists: {temporary}") try: with SafeTensorSet(source_paths(source_dir)) as source: source.validate_plan(plan) print( f"[convert] source layout valid; writing {len(plan)} tensors", flush=True, ) write_gguf(temporary, plan, TensorProducer(source)) size = temporary.stat().st_size digest = sha256_path(temporary) os.replace(temporary, output) directory_fd = os.open(output.parent, os.O_RDONLY | os.O_DIRECTORY) try: os.fsync(directory_fd) finally: os.close(directory_fd) except Exception: temporary.unlink(missing_ok=True) raise print(f"[convert] wrote {output} ({size:,} bytes)", flush=True) print(f"[convert] SHA-256 {digest}", flush=True) return size, digest def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "--sources", type=Path, required=True, help="Pinned shard directory" ) parser.add_argument( "--recipe", choices=sorted(RECIPES), default=DEFAULT_RECIPE.name, help=f"Artifact recipe (default: {DEFAULT_RECIPE.name})", ) parser.add_argument( "--output", type=Path, help="Output GGUF path (default: the selected recipe's canonical filename)", ) parser.add_argument( "--force", action="store_true", help="Replace an existing output" ) return parser.parse_args() def main() -> None: args = parse_args() recipe = resolve_recipe(args.recipe) output = args.output if args.output is not None else Path(recipe.output_filename) convert(args.sources, output, force=args.force, recipe=recipe) if __name__ == "__main__": main()