Skip to content

Preserve small matrix weights during int8 export - #2176

Open
sylvesterkaczmarek wants to merge 1 commit into
openai:mainfrom
sylvesterkaczmarek:fix/preserve-small-matrix-quantization
Open

sylvesterkaczmarek wants to merge 1 commit into
openai:mainfrom
sylvesterkaczmarek:fix/preserve-small-matrix-quantization

Conversation

@sylvesterkaczmarek

@sylvesterkaczmarek sylvesterkaczmarek commented Oct 5, 2026 •

Copy link
Copy Markdown

Problem

Both matrix quantizers impose a minimum scale of 1/127, regardless of the row's magnitude. This forces a minimum effective range of one. In the actual state-dictionary export path, a 257-by-257 float32 matrix ranging from -0.003 to 0.003 is consequently exported as all zero int8 values.

Change

Use each row's computed range, with only the smallest positive representable float16 scale as a floor. Round the stored scale upward when necessary so the clipped endpoints remain representable, then quantize against that stored scale. Apply the same correction in PyTorch and MLX.

The format identifier, int8 values, float16 row-scale storage, metadata, and decoder remain unchanged. Vector/scalar scaling is unchanged. This affects future baseline exports and does not modify archived submissions.

Size and accuracy trade-off

The previous 1/127 minimum scale coarsely quantizes small-magnitude matrices. That can yield substantial reconstruction error, but creates repeated/zero int8 bytes which compress unusually well under zlib. Preserving those weights increases the entropy of the int8 payload and can increase serialized size under the 16 MB submission cap, even though the format itself is unchanged. A contributor's CPU experiment on synthetic 1024×1024 Gaussian matrices reported roughly 0.23 to 0.73 compressed bytes/parameter under the old floor for small standard deviations, versus about 0.92 after this patch, with much lower relative error. These are proxy inputs, not measured trained-weight or leaderboard results.

The existing baseline uses the default quantizer and this PR does not show that a real submission becomes smaller, faster or more accurate. Any proposed use should check the exact compressed artifact size against the cap and validate task accuracy using representative checkpoint weights. The patch intentionally prioritizes preserving small weights; adaptive precision/compression is separate future work.

Validation

  • 20 focused tests passed, covering both implementations on CPU. Fourteen fail on unchanged source; six controls pass before and after.
  • Coverage includes row magnitudes from 1e-5 to 100, different ranges within one matrix, zero/empty/tiny rows, stored-scale error bounds, and unchanged vector behavior.
  • Tests exercise matrices above the passthrough threshold through full state-dictionary quantization/dequantization, including a real in-memory torch.save/torch.load round trip.
  • Syntax/error checks and git diff --check passed.

Python 3.12.14, PyTorch 2.14.1, MLX 0.32.3 and NumPy 2.5.3 on macOS arm64. MLX operations used its CPU stream. No pretrained weights, datasets, CUDA/Metal execution, full training, or benchmark improvement are claimed. No dependency changes.

Reproduced against f5c079314c4877fbb0af378c0abade5a8ca33d3a.

Maintenance validation on 6 November 2026

Revalidated both export paths and serialized scale rounding. The existing description retains the accuracy/compressed-size trade-off and the risk of exceeding the 16 MB cap; no trained-checkpoint or benchmark benefit is established.

Focused validation at d2e65694f279: 20 CPU tests passed. The combined six-PR checkout passed 121 tests, including the actual PyTorch and MLX CPU implementations. The two baseline scripts remain below 1,500 lines; archived record scripts were not modified.

Python 3.12.14, PyTorch 2.15.0 and SentencePiece 0.2.3 on macOS arm64. Tests used synthetic inputs with external sockets disabled. No pretrained checkpoint, external dataset, GPU training, full validation benchmark or leaderboard measurement was used. There are no hosted test runs on these PR heads. Earlier validation counts above are historical.

@Evreu1pro

Copy link
Copy Markdown

Agree the 1/127 floor is surprising, but I think it's worth spelling out that it's a size/accuracy trade-off rather than a pure fix, because zlib was quietly profiting from it.

Quick check on CPU, quantize_float_tensor from train_gpt.py at f5c0793 vs. with this PR applied, 1024×1024 Gaussian matrices, int8 payload compressed with zlib.compress(..., 9):

σ zlib bytes/param (old) (PR) rel. error (old) (PR)
0.005 0.234 0.917 0.454 0.0079
0.01 0.360 0.916 0.227 0.0079
0.02 0.491 0.918 0.114 0.0078
0.05 0.648 0.917 0.045 0.0079
0.1 0.734 0.918 0.023 0.0079
0.3 0.913 0.917 0.008 0.0079

So the floor only bites when a row's clipped peak is < 1 (roughly σ < 0.2 for Gaussian rows), and there it gives a much coarser grid that compresses far better. With the PR every matrix row costs ~0.92 B/param regardless of magnitude.

For the current baseline this probably changes nothing. If I'm reading the 4h non-record log right (2026-03-18_Quasi10Bfrom50B...), it reports payload:17178912 → 15,762,519 bytes after zlib, a ratio of ~0.92, which is what you'd expect if the floor almost never binds on trained weights. But for anyone running the baseline with small-magnitude matrices (weight decay, smaller init, a lot more layers), this is a real artifact-size increase, up to ~4× per affected matrix in the table above, and can push a run that used to fit under 16 MB over the cap. Might be worth one line in the description so nobody is surprised by a bigger Serialized model int8+zlib number.

(Gaussian rows are only a proxy for trained weights, so take the exact numbers with a grain of salt.)

@sylvesterkaczmarek

Copy link
Copy Markdown
Author

Good point, thanks for measuring it. The old floor does buy much better zlib compression for small matrices, at the cost of wiping out useful weight detail. I've added the size/accuracy trade-off and 16 MB cap risk to the PR description, and made clear that your Gaussian numbers aren't a measured effect on the trained baseline. I'd want an actual checkpoint export before claiming this helps a submission.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants