mlx-bun 0.0.4

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.
Files changed (210) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +362 -0
  3. package/THIRD_PARTY_LICENSES.md +41 -0
  4. package/bin/mlx-bun.mjs +32 -0
  5. package/docs/curve-designer.html +303 -0
  6. package/docs/design/adapters-end-to-end.md +201 -0
  7. package/docs/design/compat-cli-surface-design.md +227 -0
  8. package/docs/design/docs-reorg-plan.md +152 -0
  9. package/docs/design/hlg-sampling.md +340 -0
  10. package/docs/design/memory-system.md +339 -0
  11. package/docs/design/optimization_plan.md +344 -0
  12. package/docs/design/parallel-slots.md +515 -0
  13. package/docs/design/runtime-isolation.md +247 -0
  14. package/docs/design/segmented-backward-training.md +508 -0
  15. package/docs/investigations/chunk-finetune-experiment.md +84 -0
  16. package/docs/investigations/curve-bisector-routing.md +123 -0
  17. package/docs/investigations/curve-runs/bisector-route.json +98 -0
  18. package/docs/investigations/curve-runs/bisector-trace.json +1393 -0
  19. package/docs/investigations/curve-runs/curve-terrain.json +1179 -0
  20. package/docs/investigations/curve-runs/curve-terrain.svg +42 -0
  21. package/docs/investigations/curve-terrain.html +105 -0
  22. package/docs/investigations/expert-offload-single-user-moe.md +292 -0
  23. package/docs/investigations/hlg-ab.html +34 -0
  24. package/docs/investigations/hlg-explorer.html +238 -0
  25. package/docs/investigations/hlg-figs/cliff.svg +1 -0
  26. package/docs/investigations/hlg-figs/cross.svg +1 -0
  27. package/docs/investigations/hlg-figs/edges.svg +1 -0
  28. package/docs/investigations/hlg-figs/k20-grids.svg +1 -0
  29. package/docs/investigations/hlg-figs/k20-peaks.svg +1 -0
  30. package/docs/investigations/hlg-figs/lw-curve.svg +1 -0
  31. package/docs/investigations/hlg-figs/terrain.svg +1 -0
  32. package/docs/investigations/hlg-report.html +262 -0
  33. package/docs/investigations/hlg-runs/hlg-ab.json +149 -0
  34. package/docs/investigations/hlg-runs/hlg-blind-prompts.md +87 -0
  35. package/docs/investigations/hlg-runs/hlg-blind.json +107 -0
  36. package/docs/investigations/hlg-runs/hlg-cliff.json +197 -0
  37. package/docs/investigations/hlg-runs/hlg-cross.json +505 -0
  38. package/docs/investigations/hlg-runs/hlg-edge.json +388 -0
  39. package/docs/investigations/hlg-runs/hlg-eval1.json +25 -0
  40. package/docs/investigations/hlg-runs/hlg-frontier.json +86 -0
  41. package/docs/investigations/hlg-runs/hlg-grid.json +242 -0
  42. package/docs/investigations/hlg-runs/hlg-grid2.json +343 -0
  43. package/docs/investigations/hlg-runs/hlg-lw-coarse.json +110 -0
  44. package/docs/investigations/hlg-runs/hlg-map.json +383 -0
  45. package/docs/investigations/hlg-runs/run0-original.log +159 -0
  46. package/docs/investigations/hlg-runs/runA-recommended.log +131 -0
  47. package/docs/investigations/hlg-runs/runAA-cliff-cpm5.log +26 -0
  48. package/docs/investigations/hlg-runs/runAB-grid2-e4b.log +18 -0
  49. package/docs/investigations/hlg-runs/runAC-peak-e4b-partial.log +25 -0
  50. package/docs/investigations/hlg-runs/runAD-ab-samples.log +8 -0
  51. package/docs/investigations/hlg-runs/runAE-edge-e4b.log +51 -0
  52. package/docs/investigations/hlg-runs/runAF-logit-calibration.log +10 -0
  53. package/docs/investigations/hlg-runs/runAG-eval-config2.log +16 -0
  54. package/docs/investigations/hlg-runs/runB-gain-variants.log +192 -0
  55. package/docs/investigations/hlg-runs/runC-pivots.log +162 -0
  56. package/docs/investigations/hlg-runs/runD-oetf.log +132 -0
  57. package/docs/investigations/hlg-runs/runE-eotf.log +162 -0
  58. package/docs/investigations/hlg-runs/runF-shaper-working.log +86 -0
  59. package/docs/investigations/hlg-runs/runG-shaper-sweep.log +101 -0
  60. package/docs/investigations/hlg-runs/runH-wsweep.log +176 -0
  61. package/docs/investigations/hlg-runs/runI-asweep.log +176 -0
  62. package/docs/investigations/hlg-runs/runJ-knobmap.log +49 -0
  63. package/docs/investigations/hlg-runs/runK-frontier.log +19 -0
  64. package/docs/investigations/hlg-runs/runL-widemap.log +65 -0
  65. package/docs/investigations/hlg-runs/runM-frontier-emb.log +15 -0
  66. package/docs/investigations/hlg-runs/runN-verify.log +116 -0
  67. package/docs/investigations/hlg-runs/runO-frontier-seed5000.log +15 -0
  68. package/docs/investigations/hlg-runs/runP-grid-AxSm.log +15 -0
  69. package/docs/investigations/hlg-runs/runQ-grid-tgW.log +15 -0
  70. package/docs/investigations/hlg-runs/runR-blind-transcript.log +79 -0
  71. package/docs/investigations/hlg-runs/runS-cross-coherence.log +33 -0
  72. package/docs/investigations/hlg-runs/runT-cross-cpm5.log +33 -0
  73. package/docs/investigations/hlg-runs/runU-cross-12b.log +33 -0
  74. package/docs/investigations/hlg-runs/runV-lw-e4b.log +22 -0
  75. package/docs/investigations/hlg-runs/runW-lw-12b.log +22 -0
  76. package/docs/investigations/hlg-runs/runX-lw-cpm5.log +22 -0
  77. package/docs/investigations/hlg-runs/runY-cliff-e4b.log +26 -0
  78. package/docs/investigations/hlg-runs/runZ-cliff-12b.log +26 -0
  79. package/docs/investigations/hlg-sampling-investigation.md +970 -0
  80. package/docs/investigations/lab-build-journal.md +157 -0
  81. package/docs/investigations/pi-builtin-investigation.md +308 -0
  82. package/docs/investigations/starter-model-port-handoff.md +323 -0
  83. package/docs/planning/IDEAS.md +26 -0
  84. package/docs/planning/PRODUCT_ROADMAP.md +426 -0
  85. package/docs/planning/ResearchTopics.md +119 -0
  86. package/docs/planning/chunk-task-roadmap.md +77 -0
  87. package/docs/planning/journal.md +155 -0
  88. package/docs/reference/distribution.md +121 -0
  89. package/docs/reference/embedding.md +168 -0
  90. package/docs/reference/library-api.md +153 -0
  91. package/docs/reference/server-api.md +224 -0
  92. package/docs/reference/server-config.md +243 -0
  93. package/docs/segmented-backward-handoff.md +148 -0
  94. package/package.json +68 -0
  95. package/src/anthropic.ts +491 -0
  96. package/src/chat-template.ts +186 -0
  97. package/src/cli.ts +1075 -0
  98. package/src/config.ts +229 -0
  99. package/src/curve-sampler.ts +122 -0
  100. package/src/dataset/generators.ts +1087 -0
  101. package/src/dataset/index.ts +12 -0
  102. package/src/dataset/job.ts +81 -0
  103. package/src/dataset/llm.ts +71 -0
  104. package/src/dataset/registry.ts +528 -0
  105. package/src/download.ts +283 -0
  106. package/src/eval/README.md +86 -0
  107. package/src/eval/kl-prompts.ts +29 -0
  108. package/src/eval/kl.ts +294 -0
  109. package/src/eval/quality-db.ts +92 -0
  110. package/src/eval/runner.ts +102 -0
  111. package/src/eval/score.ts +49 -0
  112. package/src/eval/tasks/bfcl.ts +500 -0
  113. package/src/eval/tasks/gsm8k.ts +85 -0
  114. package/src/eval/tasks/hashhop.ts +109 -0
  115. package/src/eval/tasks/humaneval.ts +252 -0
  116. package/src/eval/tasks/ifeval.ts +354 -0
  117. package/src/eval/tasks/mmlu.ts +145 -0
  118. package/src/evaldb.ts +199 -0
  119. package/src/expert-offload-build.ts +77 -0
  120. package/src/expert-offload.ts +61 -0
  121. package/src/expert-trace.ts +67 -0
  122. package/src/fit.ts +254 -0
  123. package/src/generate.ts +451 -0
  124. package/src/harness-pi.ts +177 -0
  125. package/src/hf-push.ts +628 -0
  126. package/src/index.ts +22 -0
  127. package/src/jobs/db.ts +139 -0
  128. package/src/jobs/index.ts +29 -0
  129. package/src/jobs/job-entry.ts +106 -0
  130. package/src/jobs/runner.ts +304 -0
  131. package/src/jobs/sse.ts +114 -0
  132. package/src/jobs/types.ts +58 -0
  133. package/src/kv-store.ts +185 -0
  134. package/src/lora.ts +314 -0
  135. package/src/md-modules.d.ts +7 -0
  136. package/src/mlx/array.ts +237 -0
  137. package/src/mlx/autograd.ts +267 -0
  138. package/src/mlx/checkpoint.ts +116 -0
  139. package/src/mlx/compile.ts +158 -0
  140. package/src/mlx/custom-vjp.ts +140 -0
  141. package/src/mlx/ffi.ts +433 -0
  142. package/src/mlx/metal-kernel.ts +130 -0
  143. package/src/mlx/ops.ts +707 -0
  144. package/src/mmap.ts +90 -0
  145. package/src/model/batched-mask.ts +204 -0
  146. package/src/model/batched-rotating.ts +300 -0
  147. package/src/model/compiled-decode.ts +681 -0
  148. package/src/model/factory.ts +27 -0
  149. package/src/model/fingerprint.ts +33 -0
  150. package/src/model/flash-attention.ts +674 -0
  151. package/src/model/fused-decode-kernel.ts +305 -0
  152. package/src/model/fused-geglu-kernel.ts +87 -0
  153. package/src/model/gemma4-base.ts +1616 -0
  154. package/src/model/gemma4.ts +1012 -0
  155. package/src/model/generated/gemma4-12b.ts +526 -0
  156. package/src/model/generated/gemma4-26b.ts +500 -0
  157. package/src/model/generated/gemma4-e4b.ts +757 -0
  158. package/src/model/generated/index.ts +18 -0
  159. package/src/model/minicpm5.ts +278 -0
  160. package/src/model/qwen3-delta.ts +224 -0
  161. package/src/model/qwen3_5.ts +429 -0
  162. package/src/model/support.ts +46 -0
  163. package/src/native-pack.ts +152 -0
  164. package/src/pi-provider.ts +95 -0
  165. package/src/pi-terminal.ts +264 -0
  166. package/src/pi-web.ts +924 -0
  167. package/src/preflight.ts +123 -0
  168. package/src/prompt-cache.ts +113 -0
  169. package/src/quantize/allocator.ts +627 -0
  170. package/src/quantize/calibration.ts +180 -0
  171. package/src/quantize/config-writer.ts +132 -0
  172. package/src/quantize/index.ts +57 -0
  173. package/src/quantize/job.ts +136 -0
  174. package/src/quantize/quantizer.ts +462 -0
  175. package/src/quantize/safetensors-writer.ts +171 -0
  176. package/src/quantize/sensitivity.ts +234 -0
  177. package/src/registry.ts +336 -0
  178. package/src/responses.ts +774 -0
  179. package/src/safetensors.ts +158 -0
  180. package/src/sampler.ts +517 -0
  181. package/src/serve/batch-scheduler.ts +362 -0
  182. package/src/serve/generation-gateway.ts +160 -0
  183. package/src/server.ts +1891 -0
  184. package/src/spec/drafter.ts +261 -0
  185. package/src/spec/generate.ts +221 -0
  186. package/src/status-page.html +497 -0
  187. package/src/tokenizer.ts +38 -0
  188. package/src/tool-call.ts +242 -0
  189. package/src/train/dataset.ts +461 -0
  190. package/src/train/export.ts +32 -0
  191. package/src/train/forward.ts +229 -0
  192. package/src/train/index.ts +43 -0
  193. package/src/train/job.ts +148 -0
  194. package/src/train/lora-params.ts +194 -0
  195. package/src/train/loss.ts +342 -0
  196. package/src/train/merge.ts +195 -0
  197. package/src/train/optimizer.ts +176 -0
  198. package/src/train/rank.ts +101 -0
  199. package/src/train/segmented.ts +520 -0
  200. package/src/train/trainer.ts +600 -0
  201. package/src/tui.ts +161 -0
  202. package/src/vision/embedder.ts +168 -0
  203. package/src/vision/preprocess.ts +247 -0
  204. package/src/vision/prompt.ts +158 -0
  205. package/src/vision/siglip.ts +599 -0
  206. package/src/web/app.html +2471 -0
  207. package/src/web/skills/web-research/SKILL.md +44 -0
  208. package/src/web/skills.ts +59 -0
  209. package/src/web-tools.ts +426 -0
  210. package/src/weights.ts +91 -0
@@ -0,0 +1,526 @@
1
+ // GENERATED by scripts/gen-model.ts — DO NOT EDIT (regenerate instead):
2
+ // bun scripts/gen-model.ts <model-dir> gemma4-12b
3
+ // source: models--mlx-community--gemma-4-12B-it-OptiQ-4bit/snapshots/5b1101065d2094c8f12aa87fee80e0afa5b292b7
4
+ // fingerprint: 9f812d2eb461fcbe
5
+ //
6
+ // Branch-resolved, unrolled forward pass for this architecture
7
+ // (docs/design/optimization_plan.md Phase C). Bit-exactness vs the monolith is the
8
+ // generator's gate: tests/generated-parity.test.ts. The cache-signature
9
+ // guard falls back to the monolith for anything this file wasn't
10
+ // generated for (bf16 compat runs, vision bidir prefill).
11
+
12
+ import { MlxArray } from "../../mlx/array";
13
+ import * as ops from "../../mlx/ops";
14
+ import {
15
+ disposing,
16
+ fusedSdpaRuntimeOk,
17
+ KVCache,
18
+ QuantizedKVCache,
19
+ quantizedSdpaTiled,
20
+ quantizedSdpaUnfused,
21
+ RotatingKVCache,
22
+ RotatingQuantizedKVCache,
23
+ type Cache,
24
+ type Mask,
25
+ type SharedKv,
26
+ } from "../gemma4-base";
27
+ import {
28
+ fusedDecodeKernelSupported, fusedDecodeSdpa, perfKernelEnabled,
29
+ } from "../fused-decode-kernel";
30
+ import { Gemma4Model, type DecoderLayer } from "../gemma4";
31
+
32
+ export const FINGERPRINT = "9f812d2eb461fcbe";
33
+
34
+ /** Forward passes served by the generated fast path (vs monolith
35
+ * fallback) — the parity gate asserts the fast path actually ran. */
36
+ export let generatedForwardUses = 0;
37
+
38
+ function disposeSharedKv(s: SharedKv): void {
39
+ if (s.kind === "plain") {
40
+ s.keys.dispose();
41
+ s.values.dispose();
42
+ } else {
43
+ for (const t of [s.keys, s.values])
44
+ for (const a of [t.packed, t.scales, t.biases]) a.dispose();
45
+ }
46
+ }
47
+
48
+ function donorSlidQuantG64B4VScal(layer: DecoderLayer, x: MlxArray, mask: Mask, cache: RotatingQuantizedKVCache): MlxArray {
49
+ const a = layer.attn;
50
+ const [B, L] = x.shape as [number, number, number];
51
+ let h = layer.inputNorm.forward(x);
52
+ let q = a.qProj.forward(h);
53
+ q = disposing(q, ops.reshape(q, [B, L, a.nHeads, a.headDim]));
54
+ q = disposing(q, a.qNorm.forward(q));
55
+ const offset = cache.offset;
56
+ let k = a.kProj!.forward(h);
57
+ k = disposing(k, ops.reshape(k, [B, L, a.nKvHeads, a.headDim]));
58
+ let v = a.vProj!.forward(h);
59
+ v = disposing(v, ops.reshape(v, [B, L, a.nKvHeads, a.headDim]));
60
+ const kNormed = a.kNorm!.forward(k);
61
+ const kT = ops.transposeAxes(kNormed, [0, 2, 1, 3]);
62
+ kNormed.dispose();
63
+ const kRoped = a.rope(kT, cache.ropeOffsetArr ?? offset);
64
+ kT.dispose();
65
+ const vNormed = a.vNorm!.forward(v);
66
+ const vT = ops.transposeAxes(vNormed, [0, 2, 1, 3]);
67
+ vNormed.dispose();
68
+ v.dispose();
69
+ k.dispose();
70
+ const [kq, vq] = cache.updateAndFetchQuantized(kRoped, vT);
71
+ kRoped.dispose();
72
+ vT.dispose();
73
+ q = disposing(q, ops.transposeAxes(q, [0, 2, 1, 3]));
74
+ q = disposing(q, a.rope(q, cache.ropeOffsetArr ?? offset));
75
+ // dispatch-site constants: bits=4 group_size=64 nRep=2 head_dim=256
76
+ const attn = perfKernelEnabled() && mask.mode === "" && fusedDecodeKernelSupported(q, 4, 64)
77
+ ? fusedDecodeSdpa(q, kq, vq, 64, 4)
78
+ : (L > 1 || process.env.MLX_BUN_FUSED_DECODE === "1") && fusedSdpaRuntimeOk(q, mask)
79
+ ? quantizedSdpaTiled(q, kq, vq, 1.0, mask, 64, 4)
80
+ : quantizedSdpaUnfused(q, kq, vq, 1.0, mask, 64, 4);
81
+ q.dispose();
82
+ const attnT = ops.transposeAxes(attn, [0, 2, 1, 3]);
83
+ attn.dispose();
84
+ const merged = ops.reshape(attnT, [B, L, -1]);
85
+ attnT.dispose();
86
+ const out = a.oProj.forward(merged);
87
+ merged.dispose();
88
+ h.dispose();
89
+ h = out;
90
+ h = disposing(h, layer.postAttnNorm.forward(h));
91
+ h = disposing(h, ops.add(x, h));
92
+ const residual = h;
93
+ let f = layer.preFfNorm.forward(h);
94
+ f = disposing(f, layer.mlp.forward(f));
95
+ f = disposing(f, layer.postFfNorm.forward(f));
96
+ h = ops.add(residual, f);
97
+ residual.dispose();
98
+ f.dispose();
99
+ h = disposing(h, ops.mul(h, layer.layerScalar!));
100
+ for (const t of [kq, vq]) for (const c of [t.packed, t.scales, t.biases]) c.dispose();
101
+ return h;
102
+ }
103
+
104
+ function donorSlidQuantG64B8VScal(layer: DecoderLayer, x: MlxArray, mask: Mask, cache: RotatingQuantizedKVCache): MlxArray {
105
+ const a = layer.attn;
106
+ const [B, L] = x.shape as [number, number, number];
107
+ let h = layer.inputNorm.forward(x);
108
+ let q = a.qProj.forward(h);
109
+ q = disposing(q, ops.reshape(q, [B, L, a.nHeads, a.headDim]));
110
+ q = disposing(q, a.qNorm.forward(q));
111
+ const offset = cache.offset;
112
+ let k = a.kProj!.forward(h);
113
+ k = disposing(k, ops.reshape(k, [B, L, a.nKvHeads, a.headDim]));
114
+ let v = a.vProj!.forward(h);
115
+ v = disposing(v, ops.reshape(v, [B, L, a.nKvHeads, a.headDim]));
116
+ const kNormed = a.kNorm!.forward(k);
117
+ const kT = ops.transposeAxes(kNormed, [0, 2, 1, 3]);
118
+ kNormed.dispose();
119
+ const kRoped = a.rope(kT, cache.ropeOffsetArr ?? offset);
120
+ kT.dispose();
121
+ const vNormed = a.vNorm!.forward(v);
122
+ const vT = ops.transposeAxes(vNormed, [0, 2, 1, 3]);
123
+ vNormed.dispose();
124
+ v.dispose();
125
+ k.dispose();
126
+ const [kq, vq] = cache.updateAndFetchQuantized(kRoped, vT);
127
+ kRoped.dispose();
128
+ vT.dispose();
129
+ q = disposing(q, ops.transposeAxes(q, [0, 2, 1, 3]));
130
+ q = disposing(q, a.rope(q, cache.ropeOffsetArr ?? offset));
131
+ // dispatch-site constants: bits=8 group_size=64 nRep=2 head_dim=256
132
+ const attn = perfKernelEnabled() && mask.mode === "" && fusedDecodeKernelSupported(q, 8, 64)
133
+ ? fusedDecodeSdpa(q, kq, vq, 64, 8)
134
+ : (L > 1 || process.env.MLX_BUN_FUSED_DECODE === "1") && fusedSdpaRuntimeOk(q, mask)
135
+ ? quantizedSdpaTiled(q, kq, vq, 1.0, mask, 64, 8)
136
+ : quantizedSdpaUnfused(q, kq, vq, 1.0, mask, 64, 8);
137
+ q.dispose();
138
+ const attnT = ops.transposeAxes(attn, [0, 2, 1, 3]);
139
+ attn.dispose();
140
+ const merged = ops.reshape(attnT, [B, L, -1]);
141
+ attnT.dispose();
142
+ const out = a.oProj.forward(merged);
143
+ merged.dispose();
144
+ h.dispose();
145
+ h = out;
146
+ h = disposing(h, layer.postAttnNorm.forward(h));
147
+ h = disposing(h, ops.add(x, h));
148
+ const residual = h;
149
+ let f = layer.preFfNorm.forward(h);
150
+ f = disposing(f, layer.mlp.forward(f));
151
+ f = disposing(f, layer.postFfNorm.forward(f));
152
+ h = ops.add(residual, f);
153
+ residual.dispose();
154
+ f.dispose();
155
+ h = disposing(h, ops.mul(h, layer.layerScalar!));
156
+ for (const t of [kq, vq]) for (const c of [t.packed, t.scales, t.biases]) c.dispose();
157
+ return h;
158
+ }
159
+
160
+ function donorFullQuantG64B4KeqVScal(layer: DecoderLayer, x: MlxArray, mask: Mask, cache: QuantizedKVCache): MlxArray {
161
+ const a = layer.attn;
162
+ const [B, L] = x.shape as [number, number, number];
163
+ let h = layer.inputNorm.forward(x);
164
+ let q = a.qProj.forward(h);
165
+ q = disposing(q, ops.reshape(q, [B, L, a.nHeads, a.headDim]));
166
+ q = disposing(q, a.qNorm.forward(q));
167
+ const offset = cache.offset;
168
+ let k = a.kProj!.forward(h);
169
+ k = disposing(k, ops.reshape(k, [B, L, a.nKvHeads, a.headDim]));
170
+ const kNormed = a.kNorm!.forward(k);
171
+ const kT = ops.transposeAxes(kNormed, [0, 2, 1, 3]);
172
+ kNormed.dispose();
173
+ const kRoped = a.rope(kT, cache.ropeOffsetArr ?? offset);
174
+ kT.dispose();
175
+ const vNormed = a.vNorm!.forward(k);
176
+ const vT = ops.transposeAxes(vNormed, [0, 2, 1, 3]);
177
+ vNormed.dispose();
178
+ k.dispose();
179
+ const [kq, vq] = cache.updateAndFetchQuantized(kRoped, vT);
180
+ kRoped.dispose();
181
+ vT.dispose();
182
+ q = disposing(q, ops.transposeAxes(q, [0, 2, 1, 3]));
183
+ q = disposing(q, a.rope(q, cache.ropeOffsetArr ?? offset));
184
+ // dispatch-site constants: bits=4 group_size=64 nRep=16 head_dim=512
185
+ const attn = perfKernelEnabled() && mask.mode === "" && fusedDecodeKernelSupported(q, 4, 64)
186
+ ? fusedDecodeSdpa(q, kq, vq, 64, 4)
187
+ : (L > 1 || process.env.MLX_BUN_FUSED_DECODE === "1") && fusedSdpaRuntimeOk(q, mask)
188
+ ? quantizedSdpaTiled(q, kq, vq, 1.0, mask, 64, 4)
189
+ : quantizedSdpaUnfused(q, kq, vq, 1.0, mask, 64, 4);
190
+ q.dispose();
191
+ const attnT = ops.transposeAxes(attn, [0, 2, 1, 3]);
192
+ attn.dispose();
193
+ const merged = ops.reshape(attnT, [B, L, -1]);
194
+ attnT.dispose();
195
+ const out = a.oProj.forward(merged);
196
+ merged.dispose();
197
+ h.dispose();
198
+ h = out;
199
+ h = disposing(h, layer.postAttnNorm.forward(h));
200
+ h = disposing(h, ops.add(x, h));
201
+ const residual = h;
202
+ let f = layer.preFfNorm.forward(h);
203
+ f = disposing(f, layer.mlp.forward(f));
204
+ f = disposing(f, layer.postFfNorm.forward(f));
205
+ h = ops.add(residual, f);
206
+ residual.dispose();
207
+ f.dispose();
208
+ h = disposing(h, ops.mul(h, layer.layerScalar!));
209
+ for (const t of [kq, vq]) for (const c of [t.packed, t.scales, t.biases]) c.dispose();
210
+ return h;
211
+ }
212
+
213
+ export class GeneratedGemma4 extends Gemma4Model {
214
+ /** Per-layer cache classes must match what this file was generated
215
+ * for; anything else runs the monolith path. */
216
+ #matches(cache: Cache[]): boolean {
217
+ return (
218
+ cache.length === 48 &&
219
+ cache[0] instanceof RotatingQuantizedKVCache &&
220
+ cache[0].bits === 4 &&
221
+ cache[0].groupSize === 64 &&
222
+ cache[1] instanceof RotatingQuantizedKVCache &&
223
+ cache[1].bits === 4 &&
224
+ cache[1].groupSize === 64 &&
225
+ cache[2] instanceof RotatingQuantizedKVCache &&
226
+ cache[2].bits === 4 &&
227
+ cache[2].groupSize === 64 &&
228
+ cache[3] instanceof RotatingQuantizedKVCache &&
229
+ cache[3].bits === 8 &&
230
+ cache[3].groupSize === 64 &&
231
+ cache[4] instanceof RotatingQuantizedKVCache &&
232
+ cache[4].bits === 4 &&
233
+ cache[4].groupSize === 64 &&
234
+ cache[5] instanceof QuantizedKVCache &&
235
+ cache[5].bits === 4 &&
236
+ cache[5].groupSize === 64 &&
237
+ cache[6] instanceof RotatingQuantizedKVCache &&
238
+ cache[6].bits === 8 &&
239
+ cache[6].groupSize === 64 &&
240
+ cache[7] instanceof RotatingQuantizedKVCache &&
241
+ cache[7].bits === 8 &&
242
+ cache[7].groupSize === 64 &&
243
+ cache[8] instanceof RotatingQuantizedKVCache &&
244
+ cache[8].bits === 8 &&
245
+ cache[8].groupSize === 64 &&
246
+ cache[9] instanceof RotatingQuantizedKVCache &&
247
+ cache[9].bits === 4 &&
248
+ cache[9].groupSize === 64 &&
249
+ cache[10] instanceof RotatingQuantizedKVCache &&
250
+ cache[10].bits === 8 &&
251
+ cache[10].groupSize === 64 &&
252
+ cache[11] instanceof QuantizedKVCache &&
253
+ cache[11].bits === 4 &&
254
+ cache[11].groupSize === 64 &&
255
+ cache[12] instanceof RotatingQuantizedKVCache &&
256
+ cache[12].bits === 8 &&
257
+ cache[12].groupSize === 64 &&
258
+ cache[13] instanceof RotatingQuantizedKVCache &&
259
+ cache[13].bits === 4 &&
260
+ cache[13].groupSize === 64 &&
261
+ cache[14] instanceof RotatingQuantizedKVCache &&
262
+ cache[14].bits === 4 &&
263
+ cache[14].groupSize === 64 &&
264
+ cache[15] instanceof RotatingQuantizedKVCache &&
265
+ cache[15].bits === 4 &&
266
+ cache[15].groupSize === 64 &&
267
+ cache[16] instanceof RotatingQuantizedKVCache &&
268
+ cache[16].bits === 4 &&
269
+ cache[16].groupSize === 64 &&
270
+ cache[17] instanceof QuantizedKVCache &&
271
+ cache[17].bits === 4 &&
272
+ cache[17].groupSize === 64 &&
273
+ cache[18] instanceof RotatingQuantizedKVCache &&
274
+ cache[18].bits === 4 &&
275
+ cache[18].groupSize === 64 &&
276
+ cache[19] instanceof RotatingQuantizedKVCache &&
277
+ cache[19].bits === 4 &&
278
+ cache[19].groupSize === 64 &&
279
+ cache[20] instanceof RotatingQuantizedKVCache &&
280
+ cache[20].bits === 4 &&
281
+ cache[20].groupSize === 64 &&
282
+ cache[21] instanceof RotatingQuantizedKVCache &&
283
+ cache[21].bits === 4 &&
284
+ cache[21].groupSize === 64 &&
285
+ cache[22] instanceof RotatingQuantizedKVCache &&
286
+ cache[22].bits === 4 &&
287
+ cache[22].groupSize === 64 &&
288
+ cache[23] instanceof QuantizedKVCache &&
289
+ cache[23].bits === 4 &&
290
+ cache[23].groupSize === 64 &&
291
+ cache[24] instanceof RotatingQuantizedKVCache &&
292
+ cache[24].bits === 4 &&
293
+ cache[24].groupSize === 64 &&
294
+ cache[25] instanceof RotatingQuantizedKVCache &&
295
+ cache[25].bits === 4 &&
296
+ cache[25].groupSize === 64 &&
297
+ cache[26] instanceof RotatingQuantizedKVCache &&
298
+ cache[26].bits === 4 &&
299
+ cache[26].groupSize === 64 &&
300
+ cache[27] instanceof RotatingQuantizedKVCache &&
301
+ cache[27].bits === 4 &&
302
+ cache[27].groupSize === 64 &&
303
+ cache[28] instanceof RotatingQuantizedKVCache &&
304
+ cache[28].bits === 4 &&
305
+ cache[28].groupSize === 64 &&
306
+ cache[29] instanceof QuantizedKVCache &&
307
+ cache[29].bits === 4 &&
308
+ cache[29].groupSize === 64 &&
309
+ cache[30] instanceof RotatingQuantizedKVCache &&
310
+ cache[30].bits === 4 &&
311
+ cache[30].groupSize === 64 &&
312
+ cache[31] instanceof RotatingQuantizedKVCache &&
313
+ cache[31].bits === 4 &&
314
+ cache[31].groupSize === 64 &&
315
+ cache[32] instanceof RotatingQuantizedKVCache &&
316
+ cache[32].bits === 4 &&
317
+ cache[32].groupSize === 64 &&
318
+ cache[33] instanceof RotatingQuantizedKVCache &&
319
+ cache[33].bits === 4 &&
320
+ cache[33].groupSize === 64 &&
321
+ cache[34] instanceof RotatingQuantizedKVCache &&
322
+ cache[34].bits === 4 &&
323
+ cache[34].groupSize === 64 &&
324
+ cache[35] instanceof QuantizedKVCache &&
325
+ cache[35].bits === 4 &&
326
+ cache[35].groupSize === 64 &&
327
+ cache[36] instanceof RotatingQuantizedKVCache &&
328
+ cache[36].bits === 4 &&
329
+ cache[36].groupSize === 64 &&
330
+ cache[37] instanceof RotatingQuantizedKVCache &&
331
+ cache[37].bits === 4 &&
332
+ cache[37].groupSize === 64 &&
333
+ cache[38] instanceof RotatingQuantizedKVCache &&
334
+ cache[38].bits === 4 &&
335
+ cache[38].groupSize === 64 &&
336
+ cache[39] instanceof RotatingQuantizedKVCache &&
337
+ cache[39].bits === 4 &&
338
+ cache[39].groupSize === 64 &&
339
+ cache[40] instanceof RotatingQuantizedKVCache &&
340
+ cache[40].bits === 4 &&
341
+ cache[40].groupSize === 64 &&
342
+ cache[41] instanceof QuantizedKVCache &&
343
+ cache[41].bits === 4 &&
344
+ cache[41].groupSize === 64 &&
345
+ cache[42] instanceof RotatingQuantizedKVCache &&
346
+ cache[42].bits === 4 &&
347
+ cache[42].groupSize === 64 &&
348
+ cache[43] instanceof RotatingQuantizedKVCache &&
349
+ cache[43].bits === 4 &&
350
+ cache[43].groupSize === 64 &&
351
+ cache[44] instanceof RotatingQuantizedKVCache &&
352
+ cache[44].bits === 4 &&
353
+ cache[44].groupSize === 64 &&
354
+ cache[45] instanceof RotatingQuantizedKVCache &&
355
+ cache[45].bits === 4 &&
356
+ cache[45].groupSize === 64 &&
357
+ cache[46] instanceof RotatingQuantizedKVCache &&
358
+ cache[46].bits === 4 &&
359
+ cache[46].groupSize === 64 &&
360
+ cache[47] instanceof QuantizedKVCache &&
361
+ cache[47].bits === 4 &&
362
+ cache[47].groupSize === 64
363
+ );
364
+ }
365
+
366
+ protected override forwardLayers(
367
+ h0: MlxArray, cache: Cache[], bidir: MlxArray | null, ids: MlxArray | null,
368
+ ): MlxArray {
369
+ if (bidir !== null || !this.#matches(cache))
370
+ return super.forwardLayers(h0, cache, bidir, ids);
371
+ generatedForwardUses++;
372
+ const L = h0.shape[1]!;
373
+ const maskS = cache[0]!.makeMask(L, this.windowSize);
374
+ const maskF = cache[5]!.makeMask(L, null);
375
+ let h = h0;
376
+ let next: MlxArray;
377
+ next = donorSlidQuantG64B4VScal(this.layers[0]!, h, maskS, cache[0] as RotatingQuantizedKVCache);
378
+ h.dispose();
379
+ h = next;
380
+ next = donorSlidQuantG64B4VScal(this.layers[1]!, h, maskS, cache[1] as RotatingQuantizedKVCache);
381
+ h.dispose();
382
+ h = next;
383
+ next = donorSlidQuantG64B4VScal(this.layers[2]!, h, maskS, cache[2] as RotatingQuantizedKVCache);
384
+ h.dispose();
385
+ h = next;
386
+ next = donorSlidQuantG64B8VScal(this.layers[3]!, h, maskS, cache[3] as RotatingQuantizedKVCache);
387
+ h.dispose();
388
+ h = next;
389
+ next = donorSlidQuantG64B4VScal(this.layers[4]!, h, maskS, cache[4] as RotatingQuantizedKVCache);
390
+ h.dispose();
391
+ h = next;
392
+ next = donorFullQuantG64B4KeqVScal(this.layers[5]!, h, maskF, cache[5] as QuantizedKVCache);
393
+ h.dispose();
394
+ h = next;
395
+ next = donorSlidQuantG64B8VScal(this.layers[6]!, h, maskS, cache[6] as RotatingQuantizedKVCache);
396
+ h.dispose();
397
+ h = next;
398
+ next = donorSlidQuantG64B8VScal(this.layers[7]!, h, maskS, cache[7] as RotatingQuantizedKVCache);
399
+ h.dispose();
400
+ h = next;
401
+ next = donorSlidQuantG64B8VScal(this.layers[8]!, h, maskS, cache[8] as RotatingQuantizedKVCache);
402
+ h.dispose();
403
+ h = next;
404
+ next = donorSlidQuantG64B4VScal(this.layers[9]!, h, maskS, cache[9] as RotatingQuantizedKVCache);
405
+ h.dispose();
406
+ h = next;
407
+ next = donorSlidQuantG64B8VScal(this.layers[10]!, h, maskS, cache[10] as RotatingQuantizedKVCache);
408
+ h.dispose();
409
+ h = next;
410
+ next = donorFullQuantG64B4KeqVScal(this.layers[11]!, h, maskF, cache[11] as QuantizedKVCache);
411
+ h.dispose();
412
+ h = next;
413
+ next = donorSlidQuantG64B8VScal(this.layers[12]!, h, maskS, cache[12] as RotatingQuantizedKVCache);
414
+ h.dispose();
415
+ h = next;
416
+ next = donorSlidQuantG64B4VScal(this.layers[13]!, h, maskS, cache[13] as RotatingQuantizedKVCache);
417
+ h.dispose();
418
+ h = next;
419
+ next = donorSlidQuantG64B4VScal(this.layers[14]!, h, maskS, cache[14] as RotatingQuantizedKVCache);
420
+ h.dispose();
421
+ h = next;
422
+ next = donorSlidQuantG64B4VScal(this.layers[15]!, h, maskS, cache[15] as RotatingQuantizedKVCache);
423
+ h.dispose();
424
+ h = next;
425
+ next = donorSlidQuantG64B4VScal(this.layers[16]!, h, maskS, cache[16] as RotatingQuantizedKVCache);
426
+ h.dispose();
427
+ h = next;
428
+ next = donorFullQuantG64B4KeqVScal(this.layers[17]!, h, maskF, cache[17] as QuantizedKVCache);
429
+ h.dispose();
430
+ h = next;
431
+ next = donorSlidQuantG64B4VScal(this.layers[18]!, h, maskS, cache[18] as RotatingQuantizedKVCache);
432
+ h.dispose();
433
+ h = next;
434
+ next = donorSlidQuantG64B4VScal(this.layers[19]!, h, maskS, cache[19] as RotatingQuantizedKVCache);
435
+ h.dispose();
436
+ h = next;
437
+ next = donorSlidQuantG64B4VScal(this.layers[20]!, h, maskS, cache[20] as RotatingQuantizedKVCache);
438
+ h.dispose();
439
+ h = next;
440
+ next = donorSlidQuantG64B4VScal(this.layers[21]!, h, maskS, cache[21] as RotatingQuantizedKVCache);
441
+ h.dispose();
442
+ h = next;
443
+ next = donorSlidQuantG64B4VScal(this.layers[22]!, h, maskS, cache[22] as RotatingQuantizedKVCache);
444
+ h.dispose();
445
+ h = next;
446
+ next = donorFullQuantG64B4KeqVScal(this.layers[23]!, h, maskF, cache[23] as QuantizedKVCache);
447
+ h.dispose();
448
+ h = next;
449
+ next = donorSlidQuantG64B4VScal(this.layers[24]!, h, maskS, cache[24] as RotatingQuantizedKVCache);
450
+ h.dispose();
451
+ h = next;
452
+ next = donorSlidQuantG64B4VScal(this.layers[25]!, h, maskS, cache[25] as RotatingQuantizedKVCache);
453
+ h.dispose();
454
+ h = next;
455
+ next = donorSlidQuantG64B4VScal(this.layers[26]!, h, maskS, cache[26] as RotatingQuantizedKVCache);
456
+ h.dispose();
457
+ h = next;
458
+ next = donorSlidQuantG64B4VScal(this.layers[27]!, h, maskS, cache[27] as RotatingQuantizedKVCache);
459
+ h.dispose();
460
+ h = next;
461
+ next = donorSlidQuantG64B4VScal(this.layers[28]!, h, maskS, cache[28] as RotatingQuantizedKVCache);
462
+ h.dispose();
463
+ h = next;
464
+ next = donorFullQuantG64B4KeqVScal(this.layers[29]!, h, maskF, cache[29] as QuantizedKVCache);
465
+ h.dispose();
466
+ h = next;
467
+ next = donorSlidQuantG64B4VScal(this.layers[30]!, h, maskS, cache[30] as RotatingQuantizedKVCache);
468
+ h.dispose();
469
+ h = next;
470
+ next = donorSlidQuantG64B4VScal(this.layers[31]!, h, maskS, cache[31] as RotatingQuantizedKVCache);
471
+ h.dispose();
472
+ h = next;
473
+ next = donorSlidQuantG64B4VScal(this.layers[32]!, h, maskS, cache[32] as RotatingQuantizedKVCache);
474
+ h.dispose();
475
+ h = next;
476
+ next = donorSlidQuantG64B4VScal(this.layers[33]!, h, maskS, cache[33] as RotatingQuantizedKVCache);
477
+ h.dispose();
478
+ h = next;
479
+ next = donorSlidQuantG64B4VScal(this.layers[34]!, h, maskS, cache[34] as RotatingQuantizedKVCache);
480
+ h.dispose();
481
+ h = next;
482
+ next = donorFullQuantG64B4KeqVScal(this.layers[35]!, h, maskF, cache[35] as QuantizedKVCache);
483
+ h.dispose();
484
+ h = next;
485
+ next = donorSlidQuantG64B4VScal(this.layers[36]!, h, maskS, cache[36] as RotatingQuantizedKVCache);
486
+ h.dispose();
487
+ h = next;
488
+ next = donorSlidQuantG64B4VScal(this.layers[37]!, h, maskS, cache[37] as RotatingQuantizedKVCache);
489
+ h.dispose();
490
+ h = next;
491
+ next = donorSlidQuantG64B4VScal(this.layers[38]!, h, maskS, cache[38] as RotatingQuantizedKVCache);
492
+ h.dispose();
493
+ h = next;
494
+ next = donorSlidQuantG64B4VScal(this.layers[39]!, h, maskS, cache[39] as RotatingQuantizedKVCache);
495
+ h.dispose();
496
+ h = next;
497
+ next = donorSlidQuantG64B4VScal(this.layers[40]!, h, maskS, cache[40] as RotatingQuantizedKVCache);
498
+ h.dispose();
499
+ h = next;
500
+ next = donorFullQuantG64B4KeqVScal(this.layers[41]!, h, maskF, cache[41] as QuantizedKVCache);
501
+ h.dispose();
502
+ h = next;
503
+ next = donorSlidQuantG64B4VScal(this.layers[42]!, h, maskS, cache[42] as RotatingQuantizedKVCache);
504
+ h.dispose();
505
+ h = next;
506
+ next = donorSlidQuantG64B4VScal(this.layers[43]!, h, maskS, cache[43] as RotatingQuantizedKVCache);
507
+ h.dispose();
508
+ h = next;
509
+ next = donorSlidQuantG64B4VScal(this.layers[44]!, h, maskS, cache[44] as RotatingQuantizedKVCache);
510
+ h.dispose();
511
+ h = next;
512
+ next = donorSlidQuantG64B4VScal(this.layers[45]!, h, maskS, cache[45] as RotatingQuantizedKVCache);
513
+ h.dispose();
514
+ h = next;
515
+ next = donorSlidQuantG64B4VScal(this.layers[46]!, h, maskS, cache[46] as RotatingQuantizedKVCache);
516
+ h.dispose();
517
+ h = next;
518
+ next = donorFullQuantG64B4KeqVScal(this.layers[47]!, h, maskF, cache[47] as QuantizedKVCache);
519
+ h.dispose();
520
+ h = next;
521
+
522
+ maskS.arr?.dispose();
523
+ maskF.arr?.dispose();
524
+ return disposing(h, this.finalNorm.forward(h));
525
+ }
526
+ }