@mlx-bun/quantize 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Josh Rossi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,57 @@
1
- # Temporary Holding Version
1
+ # @mlx-bun/quantize
2
+
3
+ Produces the quantized checkpoints that [`@mlx-bun/inference`](../inference/README.md)
4
+ loads and builds its graphs for. Inference never quantizes; this package never serves.
5
+
6
+ | Module | Owns |
7
+ | --- | --- |
8
+ | `quantizer` | Quantize a checkpoint directory: per-module bit selection, dequantize/requantize through MLX, sharded output, config rewrite |
9
+ | `convert` | Non-quantizing rewrite of a checkpoint: cast floating tensors to one dtype and/or dequantize quantized modules to dense weights (`mlx_lm.convert` without `-q`) |
10
+ | `allocator` | Mixed-precision allocation from per-layer sensitivity under a bits-per-weight budget |
11
+ | `sensitivity` | Exact per-layer KL sensitivity on calibration text |
12
+ | `calibration` | Calibration sample loading and tokenization |
13
+ | `drafter` | Quantize a released DeepSpec (`Gemma4DSparkModel`) drafter: every matmul weight and gather table, `confidence_head` kept bf16, then a load smoke through the drafter loader |
14
+ | `rotate`, `weight-transform` | Rotation and fold plans for Llama and Qwen families before quantization |
15
+ | `trellis` | Trellis state packing, interleaving, and the host decoder. The 1MAD codebook and word geometry are imported from the inference kernels, which own the packed format |
16
+ | `trellis-quantizer` | `quantizeTrellisModelDir`: fold an HF-layout Qwen3.5-family checkpoint (γ + R1), code every MLP tensor with the Trellis (QTIP TCQ, L=12, 1MAD, T=256, tail-biting) and write the packed format the inference kernels serve; the rest keeps the shipped-compact affine tiers. `turboquant_fold.json` records the R1 seed and the final-norm gain the fold moved into the head, the residual basis a drafter reads through the graph |
17
+ | `trellis-encoder` | The Viterbi encode of one folded tensor into packed codes and fp16 row scales, unweighted or with BlockLDLQ error feedback against a Hessian factor |
18
+ | `trellis-allocation`, `qwen35-hf-fold` | Pure planning: which tensor gets which treatment (k-map, axis, layer limit) and the fold plan for the `model.language_model.*` layout with stored γ−1 norms |
19
+ | `config-writer`, `atomic-output` | Quantization metadata and atomic directory publication |
20
+
21
+ Sharded safetensors writing lives in `@mlx-bun/inference/artifacts` and is
22
+ re-exported here for convenience. Auxiliary checkpoint file copying lives in
23
+ `@mlx-bun/inference/artifacts/auxiliary-files`; `config-writer` re-exports
24
+ `copyAuxFiles` for compatibility.
25
+
26
+ Executed usage lives in the package tests: [weight-transform-plan](tests/weight-transform-plan.test.ts)
27
+ plans a fold, [weight-transform-numerics](tests/weight-transform-numerics.test.ts) applies one, and
28
+ [trellis-roundtrip](tests/trellis-roundtrip.test.ts) packs states here and expands them with the
29
+ inference kernels. `quantizeModelDir(sourceDirectory, outputDirectory, { bits, groupSize })`
30
+ is the entry point for a whole checkpoint; it needs a real one.
31
+
32
+ `quantizeDrafterDir(sourceDirectory, outputDirectory, { bits, groupSize })` applies the drafter policy
33
+ (the confidence head's sigmoid is compared against thresholds, so it stays bf16); drafter numerics move
34
+ acceptance, never correctness, so gate the result with `scripts/drafter-ab.ts`. A [synthetic DeepSpec
35
+ checkpoint test](tests/drafter.test.ts) covers the policy and refusals; a released drafter is not run here.
36
+
37
+ `quantizeTrellisModelDir(sourceDirectory, outputDirectory, options)` produces a packed
38
+ Trellis artifact from a full-precision `model.language_model.*` checkpoint (Qwen3.8-27B
39
+ layout; a quantized source is refused). `bits` sets k for every coded tensor; `kMap`
40
+ (an allocation file's `budgets[budget]`) sets it per tensor and may raise affine tiers;
41
+ `downAxis: "in"` codes down_proj along the un-rotated dim; `layers` codes only the first
42
+ N layers; `interleave` reorders eligible k=3 axis-0 codes for the scatter kernel; `reuse`
43
+ copies tensors of unchanged geometry from earlier packed artifacts, so a new allocation
44
+ encodes only the tensor/k pairs no source holds; `ldlq` names a directory of per-layer
45
+ Hessian factors (`layer-NNN-mlp.safetensors` for gate/up, `layer-NNN-down.safetensors`,
46
+ each holding one tensor `L`, the block-LDL factor of that site's input Hessian in the
47
+ folded basis) and switches the objective to BlockLDLQ with a guard that falls back to the
48
+ unweighted codes for any tensor whose feedback grows past 4x the unweighted magnitude.
49
+ The Hessian factors and the k-map are inputs: collecting activation Hessians runs a
50
+ calibration forward pass and is not part of this package. The encoder is numerically
51
+ the frozen producer's (`02d723a:scripts/turboquant/tq-quantize-trellis-packed.ts`);
52
+ [trellis-quantizer](tests/trellis-quantizer.test.ts) runs the whole path on a synthetic
53
+ checkpoint, [trellis-encoder](tests/trellis-encoder.test.ts) checks the codec and the
54
+ LDLQ arm, and [trellis-allocation](tests/trellis-allocation.test.ts) the planning.
55
+
56
+ Job orchestration and the CLI verbs belong to the app's modules: `mlx-bun convert --q-mode trellis` to [`@mlx-bun/module-quantize`](../module-quantize/README.md), `mlx-bun draft quantize` to [`@mlx-bun/module-train`](../module-train/README.md).
2
57
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
package/package.json CHANGED
@@ -1,6 +1,46 @@
1
1
  {
2
2
  "name": "@mlx-bun/quantize",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Quantization for MLX checkpoints on Apple Silicon: calibration, sensitivity, mixed-precision allocation, rotation, Trellis packing, and safetensors output",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "engines": {
8
+ "bun": ">=1.4.2"
9
+ },
10
+ "exports": {
11
+ ".": "./src/index.ts",
12
+ "./quantizer": "./src/quantizer.ts",
13
+ "./convert": "./src/convert.ts",
14
+ "./drafter": "./src/drafter.ts",
15
+ "./allocator": "./src/allocator.ts",
16
+ "./sensitivity": "./src/sensitivity.ts",
17
+ "./calibration": "./src/calibration.ts",
18
+ "./rotate": "./src/rotate.ts",
19
+ "./weight-transform": "./src/weight-transform.ts",
20
+ "./trellis": "./src/trellis.ts",
21
+ "./trellis-quantizer": "./src/trellis-quantizer.ts",
22
+ "./trellis-allocation": "./src/trellis-allocation.ts",
23
+ "./trellis-encoder": "./src/trellis-encoder.ts",
24
+ "./qwen35-hf-fold": "./src/qwen35-hf-fold.ts",
25
+ "./config-writer": "./src/config-writer.ts",
26
+ "./atomic-output": "./src/atomic-output.ts"
27
+ },
28
+ "files": [
29
+ "src",
30
+ "LICENSE"
31
+ ],
32
+ "scripts": {
33
+ "typecheck": "bun ../../scripts/typecheck.ts --noEmit",
34
+ "test": "bun test"
35
+ },
36
+ "dependencies": {
37
+ "@mlx-bun/inference": "1.0.0",
38
+ "@mlx-bun/mlx": "1.0.0"
39
+ },
40
+ "os": [
41
+ "darwin"
42
+ ],
43
+ "cpu": [
44
+ "arm64"
45
+ ]
46
+ }