Download scripts/compress_k3_layer.py from kiruluta/SPECTRA-K3-HF-Scaling-Benchmark: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/kiruluta/SPECTRA-K3-HF-Scaling-Benchmark/resolve/main/scripts/compress_k3_layer.py
- Command line
-
hf download hf://kiruluta/SPECTRA-K3-HF-Scaling-Benchmark/scripts/compress_k3_layer.py
-
curl -L -o compress_k3_layer.py https://huggingface.co/kiruluta/SPECTRA-K3-HF-Scaling-Benchmark/resolve/main/scripts/compress_k3_layer.py
2.8 kB
| #!/usr/bin/env python3 | |
| """Compress one Kimi K3 MoE layer end to end and write a SPECTRA artifact. | |
| A single K3 layer holds 896 experts; at the published widths one projection of | |
| one layer is roughly 20 GB in MXFP4, so this operates per layer and per | |
| projection and appends into a shared artifact. Run it in a loop over layers on | |
| a machine with fast external storage. | |
| Usage: | |
| python scripts/compress_k3_layer.py /path/to/Kimi-K3 --layer 0 --out k3.spectra | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| from spectra_moe.config import load_compression, load_hardware | |
| from spectra_moe.formats.artifact import ArtifactWriter | |
| from spectra_moe.models.k3_loader import group_experts_by_layer, load_config | |
| from spectra_moe.pipeline import compress_expert_layer | |
| def main() -> int: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("checkpoint") | |
| ap.add_argument("--layer", type=int, required=True) | |
| ap.add_argument("--projection", default="down", choices=["gate", "up", "down"]) | |
| ap.add_argument("--compression", default="spectra_default") | |
| ap.add_argument("--hardware", default="macos_32gb") | |
| ap.add_argument("--routes", default=None, | |
| help="npy file of routing traces (token x topk) for this layer") | |
| ap.add_argument("--out", required=True) | |
| args = ap.parse_args() | |
| cfg = load_compression(args.compression) | |
| hw = load_hardware(args.hardware) | |
| arch = load_config(args.checkpoint) | |
| print(f"loading layer {args.layer} '{args.projection}' experts ...") | |
| experts, ids = group_experts_by_layer(args.checkpoint, args.layer, args.projection) | |
| print(f" {experts.shape[0]} experts of shape {experts.shape[1:]} " | |
| f"({experts.nbytes / 1024**3:.2f} GiB in fp32)") | |
| routes = np.load(args.routes) if args.routes else None | |
| if routes is None: | |
| print(" NOTE: no routing trace supplied; clustering falls back to " | |
| "functional similarity only. Capture real routes for best results.") | |
| budget = hw.resident_weight_budget_gib * 1024 ** 3 / max(arch.n_layers, 1) | |
| with ArtifactWriter(args.out) as writer: | |
| stages, plan, recon, bits = compress_expert_layer( | |
| experts, cfg, routes=routes, writer=writer, | |
| layer=args.layer, budget_bytes=budget, | |
| ) | |
| writer.close(meta={"arch": arch.to_dict(), "compression": cfg.to_dict(), | |
| "layer": args.layer, "projection": args.projection, | |
| "expert_ids": ids}) | |
| for s in stages: | |
| print(json.dumps(s.to_dict(), indent=2, default=float)) | |
| print(f"\neffective bits/weight: {bits / experts.size:.3f}") | |
| print(f"reconstruction: {recon}") | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |