Skip to content

quantize::fp4_exporter

More...

Classes

Name
class quantize::fp4_exporter::FP4Exporter

Attributes

Name
MODE_FP4_AFFINE
MODE_T158_AFFINE
LAYOUT_UNIFORM_64
LAYOUT_UNIFORM_32
LAYOUT_UNIFORM_16
LAYOUT_UNIFORM_8
LAYOUT_MIXED
LAYOUT_FULL_4x4
dict DEFAULT_V2_THRESHOLDS
parser
required
True
help
action
args
project_root
exporter
float dummy_weights
str output_dir
bin_path
stats
adaptive
dict manifest
f
indent

Detailed Description

FP4 Ultra binary exporter — SGFP4 v1 (fixed 64x64) and v2 (adaptive quadtree).

Container layout v1: headers[B] | offsets[B] | codes_blob[B*2048]
Container layout v2: magic[4] | version[1] | num_superblocks[4] | pad[7] |
                     superblock_offsets[B] | superblock_data[0..B-1]

v1 (fixed, backward compatible):
- 64x64 macroblocks
- Fixed 2048-byte payload per block
- FP4_AFFINE (mode 0): 4-bit signed codes, 8 per uint32
- T158_AFFINE (mode 1): ternary as 2-bit symbols, 16 per uint32

v2 (adaptive, SGFP4 v2):
- Variable block sizes 4x4..64x64 selected by quadtree + Laplacian error
- Layout enum per superblock (0-5) identifies block structure
- Variable payloads scale with block area
- Dual-mode per-block: FP4_AFFINE vs T158_AFFINE via error comparison
- 4-byte magic header (b'SGF4') + version byte (0x02) for format detection
- Superblock offset table for paging
- 16-byte payload alignment per block
- Manifest generation via ManifestBuilder

Attributes Documentation

variable MODE_FP4_AFFINE

MODE_FP4_AFFINE =  CodeMode.FP4_AFFINE;

variable MODE_T158_AFFINE

MODE_T158_AFFINE =  CodeMode.T158_AFFINE;

variable LAYOUT_UNIFORM_64

LAYOUT_UNIFORM_64 =  Layout.UNIFORM_64;

variable LAYOUT_UNIFORM_32

LAYOUT_UNIFORM_32 =  Layout.UNIFORM_32;

variable LAYOUT_UNIFORM_16

LAYOUT_UNIFORM_16 =  Layout.UNIFORM_16;

variable LAYOUT_UNIFORM_8

LAYOUT_UNIFORM_8 =  Layout.UNIFORM_8;

variable LAYOUT_MIXED

LAYOUT_MIXED =  Layout.MIXED;

variable LAYOUT_FULL_4x4

LAYOUT_FULL_4x4 =  Layout.FULL_4X4;

variable DEFAULT_V2_THRESHOLDS

dict DEFAULT_V2_THRESHOLDS =  {
    64: {"max_mse": 0.01, "max_relative": 0.05},
    32: {"max_mse": 0.005, "max_relative": 0.03},
    16: {"max_mse": 0.002, "max_relative": 0.02},
    8:  {"max_mse": 0.001, "max_relative": 0.01},
    4:  {"max_mse": 0.0005, "max_relative": 0.005},
};

variable parser

parser =  argparse.ArgumentParser(
        description="Export specialist weights to FP4 Ultra format (v1 or v2)"
    );

variable required

required;

variable True

True;

variable help

help;

variable action

action;

variable args

args =  parser.parse_args();

variable project_root

project_root =  Path(__file__).resolve().parent.parent;

variable exporter

exporter =  FP4Exporter(project_root);

variable dummy_weights

float dummy_weights =  np.random.randn(512, 512).astype(np.float32) * 0.01;

variable output_dir

str output_dir =  project_root / "models" / "specialists_mlx" / args.niche / "fp4";

variable bin_path

bin_path;

variable stats

stats;

variable adaptive

adaptive;

variable manifest

dict manifest =  {
            "model_name": args.niche,
            "niche": args.niche,
            "base_model_ref": "",
            "adapter_ref": "",
            "quantization_params": {"format": "fp4_ultra"},
            "encoder_version": "0.1.0",
            "timestamp_utc": "",
        };

variable f

f;

variable indent

indent;

Updated on 2026-07-25 at 22:56:57 +0000