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.
- package/LICENSE +21 -0
- package/README.md +362 -0
- package/THIRD_PARTY_LICENSES.md +41 -0
- package/bin/mlx-bun.mjs +32 -0
- package/docs/curve-designer.html +303 -0
- package/docs/design/adapters-end-to-end.md +201 -0
- package/docs/design/compat-cli-surface-design.md +227 -0
- package/docs/design/docs-reorg-plan.md +152 -0
- package/docs/design/hlg-sampling.md +340 -0
- package/docs/design/memory-system.md +339 -0
- package/docs/design/optimization_plan.md +344 -0
- package/docs/design/parallel-slots.md +515 -0
- package/docs/design/runtime-isolation.md +247 -0
- package/docs/design/segmented-backward-training.md +508 -0
- package/docs/investigations/chunk-finetune-experiment.md +84 -0
- package/docs/investigations/curve-bisector-routing.md +123 -0
- package/docs/investigations/curve-runs/bisector-route.json +98 -0
- package/docs/investigations/curve-runs/bisector-trace.json +1393 -0
- package/docs/investigations/curve-runs/curve-terrain.json +1179 -0
- package/docs/investigations/curve-runs/curve-terrain.svg +42 -0
- package/docs/investigations/curve-terrain.html +105 -0
- package/docs/investigations/expert-offload-single-user-moe.md +292 -0
- package/docs/investigations/hlg-ab.html +34 -0
- package/docs/investigations/hlg-explorer.html +238 -0
- package/docs/investigations/hlg-figs/cliff.svg +1 -0
- package/docs/investigations/hlg-figs/cross.svg +1 -0
- package/docs/investigations/hlg-figs/edges.svg +1 -0
- package/docs/investigations/hlg-figs/k20-grids.svg +1 -0
- package/docs/investigations/hlg-figs/k20-peaks.svg +1 -0
- package/docs/investigations/hlg-figs/lw-curve.svg +1 -0
- package/docs/investigations/hlg-figs/terrain.svg +1 -0
- package/docs/investigations/hlg-report.html +262 -0
- package/docs/investigations/hlg-runs/hlg-ab.json +149 -0
- package/docs/investigations/hlg-runs/hlg-blind-prompts.md +87 -0
- package/docs/investigations/hlg-runs/hlg-blind.json +107 -0
- package/docs/investigations/hlg-runs/hlg-cliff.json +197 -0
- package/docs/investigations/hlg-runs/hlg-cross.json +505 -0
- package/docs/investigations/hlg-runs/hlg-edge.json +388 -0
- package/docs/investigations/hlg-runs/hlg-eval1.json +25 -0
- package/docs/investigations/hlg-runs/hlg-frontier.json +86 -0
- package/docs/investigations/hlg-runs/hlg-grid.json +242 -0
- package/docs/investigations/hlg-runs/hlg-grid2.json +343 -0
- package/docs/investigations/hlg-runs/hlg-lw-coarse.json +110 -0
- package/docs/investigations/hlg-runs/hlg-map.json +383 -0
- package/docs/investigations/hlg-runs/run0-original.log +159 -0
- package/docs/investigations/hlg-runs/runA-recommended.log +131 -0
- package/docs/investigations/hlg-runs/runAA-cliff-cpm5.log +26 -0
- package/docs/investigations/hlg-runs/runAB-grid2-e4b.log +18 -0
- package/docs/investigations/hlg-runs/runAC-peak-e4b-partial.log +25 -0
- package/docs/investigations/hlg-runs/runAD-ab-samples.log +8 -0
- package/docs/investigations/hlg-runs/runAE-edge-e4b.log +51 -0
- package/docs/investigations/hlg-runs/runAF-logit-calibration.log +10 -0
- package/docs/investigations/hlg-runs/runAG-eval-config2.log +16 -0
- package/docs/investigations/hlg-runs/runB-gain-variants.log +192 -0
- package/docs/investigations/hlg-runs/runC-pivots.log +162 -0
- package/docs/investigations/hlg-runs/runD-oetf.log +132 -0
- package/docs/investigations/hlg-runs/runE-eotf.log +162 -0
- package/docs/investigations/hlg-runs/runF-shaper-working.log +86 -0
- package/docs/investigations/hlg-runs/runG-shaper-sweep.log +101 -0
- package/docs/investigations/hlg-runs/runH-wsweep.log +176 -0
- package/docs/investigations/hlg-runs/runI-asweep.log +176 -0
- package/docs/investigations/hlg-runs/runJ-knobmap.log +49 -0
- package/docs/investigations/hlg-runs/runK-frontier.log +19 -0
- package/docs/investigations/hlg-runs/runL-widemap.log +65 -0
- package/docs/investigations/hlg-runs/runM-frontier-emb.log +15 -0
- package/docs/investigations/hlg-runs/runN-verify.log +116 -0
- package/docs/investigations/hlg-runs/runO-frontier-seed5000.log +15 -0
- package/docs/investigations/hlg-runs/runP-grid-AxSm.log +15 -0
- package/docs/investigations/hlg-runs/runQ-grid-tgW.log +15 -0
- package/docs/investigations/hlg-runs/runR-blind-transcript.log +79 -0
- package/docs/investigations/hlg-runs/runS-cross-coherence.log +33 -0
- package/docs/investigations/hlg-runs/runT-cross-cpm5.log +33 -0
- package/docs/investigations/hlg-runs/runU-cross-12b.log +33 -0
- package/docs/investigations/hlg-runs/runV-lw-e4b.log +22 -0
- package/docs/investigations/hlg-runs/runW-lw-12b.log +22 -0
- package/docs/investigations/hlg-runs/runX-lw-cpm5.log +22 -0
- package/docs/investigations/hlg-runs/runY-cliff-e4b.log +26 -0
- package/docs/investigations/hlg-runs/runZ-cliff-12b.log +26 -0
- package/docs/investigations/hlg-sampling-investigation.md +970 -0
- package/docs/investigations/lab-build-journal.md +157 -0
- package/docs/investigations/pi-builtin-investigation.md +308 -0
- package/docs/investigations/starter-model-port-handoff.md +323 -0
- package/docs/planning/IDEAS.md +26 -0
- package/docs/planning/PRODUCT_ROADMAP.md +426 -0
- package/docs/planning/ResearchTopics.md +119 -0
- package/docs/planning/chunk-task-roadmap.md +77 -0
- package/docs/planning/journal.md +155 -0
- package/docs/reference/distribution.md +121 -0
- package/docs/reference/embedding.md +168 -0
- package/docs/reference/library-api.md +153 -0
- package/docs/reference/server-api.md +224 -0
- package/docs/reference/server-config.md +243 -0
- package/docs/segmented-backward-handoff.md +148 -0
- package/package.json +68 -0
- package/src/anthropic.ts +491 -0
- package/src/chat-template.ts +186 -0
- package/src/cli.ts +1075 -0
- package/src/config.ts +229 -0
- package/src/curve-sampler.ts +122 -0
- package/src/dataset/generators.ts +1087 -0
- package/src/dataset/index.ts +12 -0
- package/src/dataset/job.ts +81 -0
- package/src/dataset/llm.ts +71 -0
- package/src/dataset/registry.ts +528 -0
- package/src/download.ts +283 -0
- package/src/eval/README.md +86 -0
- package/src/eval/kl-prompts.ts +29 -0
- package/src/eval/kl.ts +294 -0
- package/src/eval/quality-db.ts +92 -0
- package/src/eval/runner.ts +102 -0
- package/src/eval/score.ts +49 -0
- package/src/eval/tasks/bfcl.ts +500 -0
- package/src/eval/tasks/gsm8k.ts +85 -0
- package/src/eval/tasks/hashhop.ts +109 -0
- package/src/eval/tasks/humaneval.ts +252 -0
- package/src/eval/tasks/ifeval.ts +354 -0
- package/src/eval/tasks/mmlu.ts +145 -0
- package/src/evaldb.ts +199 -0
- package/src/expert-offload-build.ts +77 -0
- package/src/expert-offload.ts +61 -0
- package/src/expert-trace.ts +67 -0
- package/src/fit.ts +254 -0
- package/src/generate.ts +451 -0
- package/src/harness-pi.ts +177 -0
- package/src/hf-push.ts +628 -0
- package/src/index.ts +22 -0
- package/src/jobs/db.ts +139 -0
- package/src/jobs/index.ts +29 -0
- package/src/jobs/job-entry.ts +106 -0
- package/src/jobs/runner.ts +304 -0
- package/src/jobs/sse.ts +114 -0
- package/src/jobs/types.ts +58 -0
- package/src/kv-store.ts +185 -0
- package/src/lora.ts +314 -0
- package/src/md-modules.d.ts +7 -0
- package/src/mlx/array.ts +237 -0
- package/src/mlx/autograd.ts +267 -0
- package/src/mlx/checkpoint.ts +116 -0
- package/src/mlx/compile.ts +158 -0
- package/src/mlx/custom-vjp.ts +140 -0
- package/src/mlx/ffi.ts +433 -0
- package/src/mlx/metal-kernel.ts +130 -0
- package/src/mlx/ops.ts +707 -0
- package/src/mmap.ts +90 -0
- package/src/model/batched-mask.ts +204 -0
- package/src/model/batched-rotating.ts +300 -0
- package/src/model/compiled-decode.ts +681 -0
- package/src/model/factory.ts +27 -0
- package/src/model/fingerprint.ts +33 -0
- package/src/model/flash-attention.ts +674 -0
- package/src/model/fused-decode-kernel.ts +305 -0
- package/src/model/fused-geglu-kernel.ts +87 -0
- package/src/model/gemma4-base.ts +1616 -0
- package/src/model/gemma4.ts +1012 -0
- package/src/model/generated/gemma4-12b.ts +526 -0
- package/src/model/generated/gemma4-26b.ts +500 -0
- package/src/model/generated/gemma4-e4b.ts +757 -0
- package/src/model/generated/index.ts +18 -0
- package/src/model/minicpm5.ts +278 -0
- package/src/model/qwen3-delta.ts +224 -0
- package/src/model/qwen3_5.ts +429 -0
- package/src/model/support.ts +46 -0
- package/src/native-pack.ts +152 -0
- package/src/pi-provider.ts +95 -0
- package/src/pi-terminal.ts +264 -0
- package/src/pi-web.ts +924 -0
- package/src/preflight.ts +123 -0
- package/src/prompt-cache.ts +113 -0
- package/src/quantize/allocator.ts +627 -0
- package/src/quantize/calibration.ts +180 -0
- package/src/quantize/config-writer.ts +132 -0
- package/src/quantize/index.ts +57 -0
- package/src/quantize/job.ts +136 -0
- package/src/quantize/quantizer.ts +462 -0
- package/src/quantize/safetensors-writer.ts +171 -0
- package/src/quantize/sensitivity.ts +234 -0
- package/src/registry.ts +336 -0
- package/src/responses.ts +774 -0
- package/src/safetensors.ts +158 -0
- package/src/sampler.ts +517 -0
- package/src/serve/batch-scheduler.ts +362 -0
- package/src/serve/generation-gateway.ts +160 -0
- package/src/server.ts +1891 -0
- package/src/spec/drafter.ts +261 -0
- package/src/spec/generate.ts +221 -0
- package/src/status-page.html +497 -0
- package/src/tokenizer.ts +38 -0
- package/src/tool-call.ts +242 -0
- package/src/train/dataset.ts +461 -0
- package/src/train/export.ts +32 -0
- package/src/train/forward.ts +229 -0
- package/src/train/index.ts +43 -0
- package/src/train/job.ts +148 -0
- package/src/train/lora-params.ts +194 -0
- package/src/train/loss.ts +342 -0
- package/src/train/merge.ts +195 -0
- package/src/train/optimizer.ts +176 -0
- package/src/train/rank.ts +101 -0
- package/src/train/segmented.ts +520 -0
- package/src/train/trainer.ts +600 -0
- package/src/tui.ts +161 -0
- package/src/vision/embedder.ts +168 -0
- package/src/vision/preprocess.ts +247 -0
- package/src/vision/prompt.ts +158 -0
- package/src/vision/siglip.ts +599 -0
- package/src/web/app.html +2471 -0
- package/src/web/skills/web-research/SKILL.md +44 -0
- package/src/web/skills.ts +59 -0
- package/src/web-tools.ts +426 -0
- package/src/weights.ts +91 -0
|
@@ -0,0 +1,757 @@
|
|
|
1
|
+
// GENERATED by scripts/gen-model.ts — DO NOT EDIT (regenerate instead):
|
|
2
|
+
// bun scripts/gen-model.ts <model-dir> gemma4-e4b
|
|
3
|
+
// source: models--mlx-community--gemma-4-e4b-it-OptiQ-4bit/snapshots/fcdb12d740cd813634064567fc7cb51159b34253
|
|
4
|
+
// fingerprint: 418e9adc386ea67c
|
|
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 { Gemma4Model, type DecoderLayer } from "../gemma4";
|
|
28
|
+
import { isCompiledTrace } from "../gemma4-base";
|
|
29
|
+
|
|
30
|
+
export const FINGERPRINT = "418e9adc386ea67c";
|
|
31
|
+
|
|
32
|
+
/** Forward passes served by the generated fast path (vs monolith
|
|
33
|
+
* fallback) — the parity gate asserts the fast path actually ran. */
|
|
34
|
+
export let generatedForwardUses = 0;
|
|
35
|
+
|
|
36
|
+
function disposeSharedKv(s: SharedKv): void {
|
|
37
|
+
if (s.kind === "plain") {
|
|
38
|
+
s.keys.dispose();
|
|
39
|
+
s.values.dispose();
|
|
40
|
+
} else {
|
|
41
|
+
for (const t of [s.keys, s.values])
|
|
42
|
+
for (const a of [t.packed, t.scales, t.biases]) a.dispose();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Per-layer-input split with the layer index baked; DynamicSlice under
|
|
47
|
+
* a compiled-decode trace (mlx Slice lacks output_shapes), the exact
|
|
48
|
+
* monolith ops otherwise. */
|
|
49
|
+
function perLayerSlice(perLayer: MlxArray, i: number, L: number, width: number): MlxArray {
|
|
50
|
+
let pls: MlxArray;
|
|
51
|
+
if (isCompiledTrace()) {
|
|
52
|
+
const start = ops.fromInt32([i], [1]);
|
|
53
|
+
pls = ops.sliceDynamic(perLayer, start, [2], [1, L, 1, width]);
|
|
54
|
+
start.dispose();
|
|
55
|
+
} else {
|
|
56
|
+
pls = perLayer.slice([0, 0, i, 0], [1, L, i + 1, width]);
|
|
57
|
+
}
|
|
58
|
+
const r = ops.reshape(pls, [1, L, width]);
|
|
59
|
+
pls.dispose();
|
|
60
|
+
return r;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function donorSlidQuantG64B4VPliScal(layer: DecoderLayer, x: MlxArray, mask: Mask, cache: RotatingQuantizedKVCache, pli: MlxArray): MlxArray {
|
|
64
|
+
const a = layer.attn;
|
|
65
|
+
const [B, L] = x.shape as [number, number, number];
|
|
66
|
+
let h = layer.inputNorm.forward(x);
|
|
67
|
+
let q = a.qProj.forward(h);
|
|
68
|
+
q = disposing(q, ops.reshape(q, [B, L, a.nHeads, a.headDim]));
|
|
69
|
+
q = disposing(q, a.qNorm.forward(q));
|
|
70
|
+
const offset = cache.offset;
|
|
71
|
+
let k = a.kProj!.forward(h);
|
|
72
|
+
k = disposing(k, ops.reshape(k, [B, L, a.nKvHeads, a.headDim]));
|
|
73
|
+
let v = a.vProj!.forward(h);
|
|
74
|
+
v = disposing(v, ops.reshape(v, [B, L, a.nKvHeads, a.headDim]));
|
|
75
|
+
const kNormed = a.kNorm!.forward(k);
|
|
76
|
+
const kT = ops.transposeAxes(kNormed, [0, 2, 1, 3]);
|
|
77
|
+
kNormed.dispose();
|
|
78
|
+
const kRoped = a.rope(kT, cache.ropeOffsetArr ?? offset);
|
|
79
|
+
kT.dispose();
|
|
80
|
+
const vNormed = a.vNorm!.forward(v);
|
|
81
|
+
const vT = ops.transposeAxes(vNormed, [0, 2, 1, 3]);
|
|
82
|
+
vNormed.dispose();
|
|
83
|
+
v.dispose();
|
|
84
|
+
k.dispose();
|
|
85
|
+
const [kq, vq] = cache.updateAndFetchQuantized(kRoped, vT);
|
|
86
|
+
kRoped.dispose();
|
|
87
|
+
vT.dispose();
|
|
88
|
+
q = disposing(q, ops.transposeAxes(q, [0, 2, 1, 3]));
|
|
89
|
+
q = disposing(q, a.rope(q, cache.ropeOffsetArr ?? offset));
|
|
90
|
+
// dispatch-site constants: bits=4 group_size=64 nRep=4 head_dim=256
|
|
91
|
+
const attn = (L > 1 || process.env.MLX_BUN_FUSED_DECODE === "1") && fusedSdpaRuntimeOk(q, mask)
|
|
92
|
+
? quantizedSdpaTiled(q, kq, vq, 1.0, mask, 64, 4)
|
|
93
|
+
: quantizedSdpaUnfused(q, kq, vq, 1.0, mask, 64, 4);
|
|
94
|
+
q.dispose();
|
|
95
|
+
const attnT = ops.transposeAxes(attn, [0, 2, 1, 3]);
|
|
96
|
+
attn.dispose();
|
|
97
|
+
const merged = ops.reshape(attnT, [B, L, -1]);
|
|
98
|
+
attnT.dispose();
|
|
99
|
+
const out = a.oProj.forward(merged);
|
|
100
|
+
merged.dispose();
|
|
101
|
+
h.dispose();
|
|
102
|
+
h = out;
|
|
103
|
+
h = disposing(h, layer.postAttnNorm.forward(h));
|
|
104
|
+
h = disposing(h, ops.add(x, h));
|
|
105
|
+
const residual = h;
|
|
106
|
+
let f = layer.preFfNorm.forward(h);
|
|
107
|
+
f = disposing(f, layer.mlp.forward(f));
|
|
108
|
+
f = disposing(f, layer.postFfNorm.forward(f));
|
|
109
|
+
h = ops.add(residual, f);
|
|
110
|
+
residual.dispose();
|
|
111
|
+
f.dispose();
|
|
112
|
+
const res2 = h;
|
|
113
|
+
let gate = layer.perLayerGate!.forward(h);
|
|
114
|
+
gate = disposing(gate, ops.geluApprox(gate));
|
|
115
|
+
gate = disposing(gate, ops.mul(gate, pli));
|
|
116
|
+
gate = disposing(gate, layer.perLayerProjection!.forward(gate));
|
|
117
|
+
gate = disposing(gate, layer.postPerLayerNorm!.forward(gate));
|
|
118
|
+
h = ops.add(res2, gate);
|
|
119
|
+
res2.dispose();
|
|
120
|
+
gate.dispose();
|
|
121
|
+
h = disposing(h, ops.mul(h, layer.layerScalar!));
|
|
122
|
+
for (const t of [kq, vq]) for (const c of [t.packed, t.scales, t.biases]) c.dispose();
|
|
123
|
+
return h;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function donorSlidQuantG64B8VPliScal(layer: DecoderLayer, x: MlxArray, mask: Mask, cache: RotatingQuantizedKVCache, pli: MlxArray): MlxArray {
|
|
127
|
+
const a = layer.attn;
|
|
128
|
+
const [B, L] = x.shape as [number, number, number];
|
|
129
|
+
let h = layer.inputNorm.forward(x);
|
|
130
|
+
let q = a.qProj.forward(h);
|
|
131
|
+
q = disposing(q, ops.reshape(q, [B, L, a.nHeads, a.headDim]));
|
|
132
|
+
q = disposing(q, a.qNorm.forward(q));
|
|
133
|
+
const offset = cache.offset;
|
|
134
|
+
let k = a.kProj!.forward(h);
|
|
135
|
+
k = disposing(k, ops.reshape(k, [B, L, a.nKvHeads, a.headDim]));
|
|
136
|
+
let v = a.vProj!.forward(h);
|
|
137
|
+
v = disposing(v, ops.reshape(v, [B, L, a.nKvHeads, a.headDim]));
|
|
138
|
+
const kNormed = a.kNorm!.forward(k);
|
|
139
|
+
const kT = ops.transposeAxes(kNormed, [0, 2, 1, 3]);
|
|
140
|
+
kNormed.dispose();
|
|
141
|
+
const kRoped = a.rope(kT, cache.ropeOffsetArr ?? offset);
|
|
142
|
+
kT.dispose();
|
|
143
|
+
const vNormed = a.vNorm!.forward(v);
|
|
144
|
+
const vT = ops.transposeAxes(vNormed, [0, 2, 1, 3]);
|
|
145
|
+
vNormed.dispose();
|
|
146
|
+
v.dispose();
|
|
147
|
+
k.dispose();
|
|
148
|
+
const [kq, vq] = cache.updateAndFetchQuantized(kRoped, vT);
|
|
149
|
+
kRoped.dispose();
|
|
150
|
+
vT.dispose();
|
|
151
|
+
q = disposing(q, ops.transposeAxes(q, [0, 2, 1, 3]));
|
|
152
|
+
q = disposing(q, a.rope(q, cache.ropeOffsetArr ?? offset));
|
|
153
|
+
// dispatch-site constants: bits=8 group_size=64 nRep=4 head_dim=256
|
|
154
|
+
const attn = (L > 1 || process.env.MLX_BUN_FUSED_DECODE === "1") && fusedSdpaRuntimeOk(q, mask)
|
|
155
|
+
? quantizedSdpaTiled(q, kq, vq, 1.0, mask, 64, 8)
|
|
156
|
+
: quantizedSdpaUnfused(q, kq, vq, 1.0, mask, 64, 8);
|
|
157
|
+
q.dispose();
|
|
158
|
+
const attnT = ops.transposeAxes(attn, [0, 2, 1, 3]);
|
|
159
|
+
attn.dispose();
|
|
160
|
+
const merged = ops.reshape(attnT, [B, L, -1]);
|
|
161
|
+
attnT.dispose();
|
|
162
|
+
const out = a.oProj.forward(merged);
|
|
163
|
+
merged.dispose();
|
|
164
|
+
h.dispose();
|
|
165
|
+
h = out;
|
|
166
|
+
h = disposing(h, layer.postAttnNorm.forward(h));
|
|
167
|
+
h = disposing(h, ops.add(x, h));
|
|
168
|
+
const residual = h;
|
|
169
|
+
let f = layer.preFfNorm.forward(h);
|
|
170
|
+
f = disposing(f, layer.mlp.forward(f));
|
|
171
|
+
f = disposing(f, layer.postFfNorm.forward(f));
|
|
172
|
+
h = ops.add(residual, f);
|
|
173
|
+
residual.dispose();
|
|
174
|
+
f.dispose();
|
|
175
|
+
const res2 = h;
|
|
176
|
+
let gate = layer.perLayerGate!.forward(h);
|
|
177
|
+
gate = disposing(gate, ops.geluApprox(gate));
|
|
178
|
+
gate = disposing(gate, ops.mul(gate, pli));
|
|
179
|
+
gate = disposing(gate, layer.perLayerProjection!.forward(gate));
|
|
180
|
+
gate = disposing(gate, layer.postPerLayerNorm!.forward(gate));
|
|
181
|
+
h = ops.add(res2, gate);
|
|
182
|
+
res2.dispose();
|
|
183
|
+
gate.dispose();
|
|
184
|
+
h = disposing(h, ops.mul(h, layer.layerScalar!));
|
|
185
|
+
for (const t of [kq, vq]) for (const c of [t.packed, t.scales, t.biases]) c.dispose();
|
|
186
|
+
return h;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function donorFullQuantG64B4VPliScal(layer: DecoderLayer, x: MlxArray, mask: Mask, cache: QuantizedKVCache, pli: MlxArray): MlxArray {
|
|
190
|
+
const a = layer.attn;
|
|
191
|
+
const [B, L] = x.shape as [number, number, number];
|
|
192
|
+
let h = layer.inputNorm.forward(x);
|
|
193
|
+
let q = a.qProj.forward(h);
|
|
194
|
+
q = disposing(q, ops.reshape(q, [B, L, a.nHeads, a.headDim]));
|
|
195
|
+
q = disposing(q, a.qNorm.forward(q));
|
|
196
|
+
const offset = cache.offset;
|
|
197
|
+
let k = a.kProj!.forward(h);
|
|
198
|
+
k = disposing(k, ops.reshape(k, [B, L, a.nKvHeads, a.headDim]));
|
|
199
|
+
let v = a.vProj!.forward(h);
|
|
200
|
+
v = disposing(v, ops.reshape(v, [B, L, a.nKvHeads, a.headDim]));
|
|
201
|
+
const kNormed = a.kNorm!.forward(k);
|
|
202
|
+
const kT = ops.transposeAxes(kNormed, [0, 2, 1, 3]);
|
|
203
|
+
kNormed.dispose();
|
|
204
|
+
const kRoped = a.rope(kT, cache.ropeOffsetArr ?? offset);
|
|
205
|
+
kT.dispose();
|
|
206
|
+
const vNormed = a.vNorm!.forward(v);
|
|
207
|
+
const vT = ops.transposeAxes(vNormed, [0, 2, 1, 3]);
|
|
208
|
+
vNormed.dispose();
|
|
209
|
+
v.dispose();
|
|
210
|
+
k.dispose();
|
|
211
|
+
const [kq, vq] = cache.updateAndFetchQuantized(kRoped, vT);
|
|
212
|
+
kRoped.dispose();
|
|
213
|
+
vT.dispose();
|
|
214
|
+
q = disposing(q, ops.transposeAxes(q, [0, 2, 1, 3]));
|
|
215
|
+
q = disposing(q, a.rope(q, cache.ropeOffsetArr ?? offset));
|
|
216
|
+
// dispatch-site constants: bits=4 group_size=64 nRep=4 head_dim=512
|
|
217
|
+
const attn = (L > 1 || process.env.MLX_BUN_FUSED_DECODE === "1") && fusedSdpaRuntimeOk(q, mask)
|
|
218
|
+
? quantizedSdpaTiled(q, kq, vq, 1.0, mask, 64, 4)
|
|
219
|
+
: quantizedSdpaUnfused(q, kq, vq, 1.0, mask, 64, 4);
|
|
220
|
+
q.dispose();
|
|
221
|
+
const attnT = ops.transposeAxes(attn, [0, 2, 1, 3]);
|
|
222
|
+
attn.dispose();
|
|
223
|
+
const merged = ops.reshape(attnT, [B, L, -1]);
|
|
224
|
+
attnT.dispose();
|
|
225
|
+
const out = a.oProj.forward(merged);
|
|
226
|
+
merged.dispose();
|
|
227
|
+
h.dispose();
|
|
228
|
+
h = out;
|
|
229
|
+
h = disposing(h, layer.postAttnNorm.forward(h));
|
|
230
|
+
h = disposing(h, ops.add(x, h));
|
|
231
|
+
const residual = h;
|
|
232
|
+
let f = layer.preFfNorm.forward(h);
|
|
233
|
+
f = disposing(f, layer.mlp.forward(f));
|
|
234
|
+
f = disposing(f, layer.postFfNorm.forward(f));
|
|
235
|
+
h = ops.add(residual, f);
|
|
236
|
+
residual.dispose();
|
|
237
|
+
f.dispose();
|
|
238
|
+
const res2 = h;
|
|
239
|
+
let gate = layer.perLayerGate!.forward(h);
|
|
240
|
+
gate = disposing(gate, ops.geluApprox(gate));
|
|
241
|
+
gate = disposing(gate, ops.mul(gate, pli));
|
|
242
|
+
gate = disposing(gate, layer.perLayerProjection!.forward(gate));
|
|
243
|
+
gate = disposing(gate, layer.postPerLayerNorm!.forward(gate));
|
|
244
|
+
h = ops.add(res2, gate);
|
|
245
|
+
res2.dispose();
|
|
246
|
+
gate.dispose();
|
|
247
|
+
h = disposing(h, ops.mul(h, layer.layerScalar!));
|
|
248
|
+
for (const t of [kq, vq]) for (const c of [t.packed, t.scales, t.biases]) c.dispose();
|
|
249
|
+
return h;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function donorFullQuantG64B8VPliScal(layer: DecoderLayer, x: MlxArray, mask: Mask, cache: QuantizedKVCache, pli: MlxArray): MlxArray {
|
|
253
|
+
const a = layer.attn;
|
|
254
|
+
const [B, L] = x.shape as [number, number, number];
|
|
255
|
+
let h = layer.inputNorm.forward(x);
|
|
256
|
+
let q = a.qProj.forward(h);
|
|
257
|
+
q = disposing(q, ops.reshape(q, [B, L, a.nHeads, a.headDim]));
|
|
258
|
+
q = disposing(q, a.qNorm.forward(q));
|
|
259
|
+
const offset = cache.offset;
|
|
260
|
+
let k = a.kProj!.forward(h);
|
|
261
|
+
k = disposing(k, ops.reshape(k, [B, L, a.nKvHeads, a.headDim]));
|
|
262
|
+
let v = a.vProj!.forward(h);
|
|
263
|
+
v = disposing(v, ops.reshape(v, [B, L, a.nKvHeads, a.headDim]));
|
|
264
|
+
const kNormed = a.kNorm!.forward(k);
|
|
265
|
+
const kT = ops.transposeAxes(kNormed, [0, 2, 1, 3]);
|
|
266
|
+
kNormed.dispose();
|
|
267
|
+
const kRoped = a.rope(kT, cache.ropeOffsetArr ?? offset);
|
|
268
|
+
kT.dispose();
|
|
269
|
+
const vNormed = a.vNorm!.forward(v);
|
|
270
|
+
const vT = ops.transposeAxes(vNormed, [0, 2, 1, 3]);
|
|
271
|
+
vNormed.dispose();
|
|
272
|
+
v.dispose();
|
|
273
|
+
k.dispose();
|
|
274
|
+
const [kq, vq] = cache.updateAndFetchQuantized(kRoped, vT);
|
|
275
|
+
kRoped.dispose();
|
|
276
|
+
vT.dispose();
|
|
277
|
+
q = disposing(q, ops.transposeAxes(q, [0, 2, 1, 3]));
|
|
278
|
+
q = disposing(q, a.rope(q, cache.ropeOffsetArr ?? offset));
|
|
279
|
+
// dispatch-site constants: bits=8 group_size=64 nRep=4 head_dim=512
|
|
280
|
+
const attn = (L > 1 || process.env.MLX_BUN_FUSED_DECODE === "1") && fusedSdpaRuntimeOk(q, mask)
|
|
281
|
+
? quantizedSdpaTiled(q, kq, vq, 1.0, mask, 64, 8)
|
|
282
|
+
: quantizedSdpaUnfused(q, kq, vq, 1.0, mask, 64, 8);
|
|
283
|
+
q.dispose();
|
|
284
|
+
const attnT = ops.transposeAxes(attn, [0, 2, 1, 3]);
|
|
285
|
+
attn.dispose();
|
|
286
|
+
const merged = ops.reshape(attnT, [B, L, -1]);
|
|
287
|
+
attnT.dispose();
|
|
288
|
+
const out = a.oProj.forward(merged);
|
|
289
|
+
merged.dispose();
|
|
290
|
+
h.dispose();
|
|
291
|
+
h = out;
|
|
292
|
+
h = disposing(h, layer.postAttnNorm.forward(h));
|
|
293
|
+
h = disposing(h, ops.add(x, h));
|
|
294
|
+
const residual = h;
|
|
295
|
+
let f = layer.preFfNorm.forward(h);
|
|
296
|
+
f = disposing(f, layer.mlp.forward(f));
|
|
297
|
+
f = disposing(f, layer.postFfNorm.forward(f));
|
|
298
|
+
h = ops.add(residual, f);
|
|
299
|
+
residual.dispose();
|
|
300
|
+
f.dispose();
|
|
301
|
+
const res2 = h;
|
|
302
|
+
let gate = layer.perLayerGate!.forward(h);
|
|
303
|
+
gate = disposing(gate, ops.geluApprox(gate));
|
|
304
|
+
gate = disposing(gate, ops.mul(gate, pli));
|
|
305
|
+
gate = disposing(gate, layer.perLayerProjection!.forward(gate));
|
|
306
|
+
gate = disposing(gate, layer.postPerLayerNorm!.forward(gate));
|
|
307
|
+
h = ops.add(res2, gate);
|
|
308
|
+
res2.dispose();
|
|
309
|
+
gate.dispose();
|
|
310
|
+
h = disposing(h, ops.mul(h, layer.layerScalar!));
|
|
311
|
+
for (const t of [kq, vq]) for (const c of [t.packed, t.scales, t.biases]) c.dispose();
|
|
312
|
+
return h;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function donorSlidQuantG64B4VPliScalExp(layer: DecoderLayer, x: MlxArray, mask: Mask, cache: RotatingQuantizedKVCache, pli: MlxArray): { h: MlxArray; shared: SharedKv } {
|
|
316
|
+
const a = layer.attn;
|
|
317
|
+
const [B, L] = x.shape as [number, number, number];
|
|
318
|
+
let h = layer.inputNorm.forward(x);
|
|
319
|
+
let q = a.qProj.forward(h);
|
|
320
|
+
q = disposing(q, ops.reshape(q, [B, L, a.nHeads, a.headDim]));
|
|
321
|
+
q = disposing(q, a.qNorm.forward(q));
|
|
322
|
+
const offset = cache.offset;
|
|
323
|
+
let k = a.kProj!.forward(h);
|
|
324
|
+
k = disposing(k, ops.reshape(k, [B, L, a.nKvHeads, a.headDim]));
|
|
325
|
+
let v = a.vProj!.forward(h);
|
|
326
|
+
v = disposing(v, ops.reshape(v, [B, L, a.nKvHeads, a.headDim]));
|
|
327
|
+
const kNormed = a.kNorm!.forward(k);
|
|
328
|
+
const kT = ops.transposeAxes(kNormed, [0, 2, 1, 3]);
|
|
329
|
+
kNormed.dispose();
|
|
330
|
+
const kRoped = a.rope(kT, cache.ropeOffsetArr ?? offset);
|
|
331
|
+
kT.dispose();
|
|
332
|
+
const vNormed = a.vNorm!.forward(v);
|
|
333
|
+
const vT = ops.transposeAxes(vNormed, [0, 2, 1, 3]);
|
|
334
|
+
vNormed.dispose();
|
|
335
|
+
v.dispose();
|
|
336
|
+
k.dispose();
|
|
337
|
+
const [kq, vq] = cache.updateAndFetchQuantized(kRoped, vT);
|
|
338
|
+
kRoped.dispose();
|
|
339
|
+
vT.dispose();
|
|
340
|
+
q = disposing(q, ops.transposeAxes(q, [0, 2, 1, 3]));
|
|
341
|
+
q = disposing(q, a.rope(q, cache.ropeOffsetArr ?? offset));
|
|
342
|
+
// dispatch-site constants: bits=4 group_size=64 nRep=4 head_dim=256
|
|
343
|
+
const attn = (L > 1 || process.env.MLX_BUN_FUSED_DECODE === "1") && fusedSdpaRuntimeOk(q, mask)
|
|
344
|
+
? quantizedSdpaTiled(q, kq, vq, 1.0, mask, 64, 4)
|
|
345
|
+
: quantizedSdpaUnfused(q, kq, vq, 1.0, mask, 64, 4);
|
|
346
|
+
q.dispose();
|
|
347
|
+
const attnT = ops.transposeAxes(attn, [0, 2, 1, 3]);
|
|
348
|
+
attn.dispose();
|
|
349
|
+
const merged = ops.reshape(attnT, [B, L, -1]);
|
|
350
|
+
attnT.dispose();
|
|
351
|
+
const out = a.oProj.forward(merged);
|
|
352
|
+
merged.dispose();
|
|
353
|
+
h.dispose();
|
|
354
|
+
h = out;
|
|
355
|
+
h = disposing(h, layer.postAttnNorm.forward(h));
|
|
356
|
+
h = disposing(h, ops.add(x, h));
|
|
357
|
+
const residual = h;
|
|
358
|
+
let f = layer.preFfNorm.forward(h);
|
|
359
|
+
f = disposing(f, layer.mlp.forward(f));
|
|
360
|
+
f = disposing(f, layer.postFfNorm.forward(f));
|
|
361
|
+
h = ops.add(residual, f);
|
|
362
|
+
residual.dispose();
|
|
363
|
+
f.dispose();
|
|
364
|
+
const res2 = h;
|
|
365
|
+
let gate = layer.perLayerGate!.forward(h);
|
|
366
|
+
gate = disposing(gate, ops.geluApprox(gate));
|
|
367
|
+
gate = disposing(gate, ops.mul(gate, pli));
|
|
368
|
+
gate = disposing(gate, layer.perLayerProjection!.forward(gate));
|
|
369
|
+
gate = disposing(gate, layer.postPerLayerNorm!.forward(gate));
|
|
370
|
+
h = ops.add(res2, gate);
|
|
371
|
+
res2.dispose();
|
|
372
|
+
gate.dispose();
|
|
373
|
+
h = disposing(h, ops.mul(h, layer.layerScalar!));
|
|
374
|
+
const shared: SharedKv = { kind: "quant", keys: kq, values: vq, offset, groupSize: 64, bits: 4, offsetArr: cache.ropeOffsetArr };
|
|
375
|
+
return { h, shared };
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function donorFullQuantG64B4VPliScalExp(layer: DecoderLayer, x: MlxArray, mask: Mask, cache: QuantizedKVCache, pli: MlxArray): { h: MlxArray; shared: SharedKv } {
|
|
379
|
+
const a = layer.attn;
|
|
380
|
+
const [B, L] = x.shape as [number, number, number];
|
|
381
|
+
let h = layer.inputNorm.forward(x);
|
|
382
|
+
let q = a.qProj.forward(h);
|
|
383
|
+
q = disposing(q, ops.reshape(q, [B, L, a.nHeads, a.headDim]));
|
|
384
|
+
q = disposing(q, a.qNorm.forward(q));
|
|
385
|
+
const offset = cache.offset;
|
|
386
|
+
let k = a.kProj!.forward(h);
|
|
387
|
+
k = disposing(k, ops.reshape(k, [B, L, a.nKvHeads, a.headDim]));
|
|
388
|
+
let v = a.vProj!.forward(h);
|
|
389
|
+
v = disposing(v, ops.reshape(v, [B, L, a.nKvHeads, a.headDim]));
|
|
390
|
+
const kNormed = a.kNorm!.forward(k);
|
|
391
|
+
const kT = ops.transposeAxes(kNormed, [0, 2, 1, 3]);
|
|
392
|
+
kNormed.dispose();
|
|
393
|
+
const kRoped = a.rope(kT, cache.ropeOffsetArr ?? offset);
|
|
394
|
+
kT.dispose();
|
|
395
|
+
const vNormed = a.vNorm!.forward(v);
|
|
396
|
+
const vT = ops.transposeAxes(vNormed, [0, 2, 1, 3]);
|
|
397
|
+
vNormed.dispose();
|
|
398
|
+
v.dispose();
|
|
399
|
+
k.dispose();
|
|
400
|
+
const [kq, vq] = cache.updateAndFetchQuantized(kRoped, vT);
|
|
401
|
+
kRoped.dispose();
|
|
402
|
+
vT.dispose();
|
|
403
|
+
q = disposing(q, ops.transposeAxes(q, [0, 2, 1, 3]));
|
|
404
|
+
q = disposing(q, a.rope(q, cache.ropeOffsetArr ?? offset));
|
|
405
|
+
// dispatch-site constants: bits=4 group_size=64 nRep=4 head_dim=512
|
|
406
|
+
const attn = (L > 1 || process.env.MLX_BUN_FUSED_DECODE === "1") && fusedSdpaRuntimeOk(q, mask)
|
|
407
|
+
? quantizedSdpaTiled(q, kq, vq, 1.0, mask, 64, 4)
|
|
408
|
+
: quantizedSdpaUnfused(q, kq, vq, 1.0, mask, 64, 4);
|
|
409
|
+
q.dispose();
|
|
410
|
+
const attnT = ops.transposeAxes(attn, [0, 2, 1, 3]);
|
|
411
|
+
attn.dispose();
|
|
412
|
+
const merged = ops.reshape(attnT, [B, L, -1]);
|
|
413
|
+
attnT.dispose();
|
|
414
|
+
const out = a.oProj.forward(merged);
|
|
415
|
+
merged.dispose();
|
|
416
|
+
h.dispose();
|
|
417
|
+
h = out;
|
|
418
|
+
h = disposing(h, layer.postAttnNorm.forward(h));
|
|
419
|
+
h = disposing(h, ops.add(x, h));
|
|
420
|
+
const residual = h;
|
|
421
|
+
let f = layer.preFfNorm.forward(h);
|
|
422
|
+
f = disposing(f, layer.mlp.forward(f));
|
|
423
|
+
f = disposing(f, layer.postFfNorm.forward(f));
|
|
424
|
+
h = ops.add(residual, f);
|
|
425
|
+
residual.dispose();
|
|
426
|
+
f.dispose();
|
|
427
|
+
const res2 = h;
|
|
428
|
+
let gate = layer.perLayerGate!.forward(h);
|
|
429
|
+
gate = disposing(gate, ops.geluApprox(gate));
|
|
430
|
+
gate = disposing(gate, ops.mul(gate, pli));
|
|
431
|
+
gate = disposing(gate, layer.perLayerProjection!.forward(gate));
|
|
432
|
+
gate = disposing(gate, layer.postPerLayerNorm!.forward(gate));
|
|
433
|
+
h = ops.add(res2, gate);
|
|
434
|
+
res2.dispose();
|
|
435
|
+
gate.dispose();
|
|
436
|
+
h = disposing(h, ops.mul(h, layer.layerScalar!));
|
|
437
|
+
const shared: SharedKv = { kind: "quant", keys: kq, values: vq, offset, groupSize: 64, bits: 4, offsetArr: cache.ropeOffsetArr };
|
|
438
|
+
return { h, shared };
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function sharerSlidQuantG64B4VPliScal(layer: DecoderLayer, x: MlxArray, mask: Mask, shared: SharedKv, pli: MlxArray): MlxArray {
|
|
442
|
+
const a = layer.attn;
|
|
443
|
+
const [B, L] = x.shape as [number, number, number];
|
|
444
|
+
let h = layer.inputNorm.forward(x);
|
|
445
|
+
let q = a.qProj.forward(h);
|
|
446
|
+
q = disposing(q, ops.reshape(q, [B, L, a.nHeads, a.headDim]));
|
|
447
|
+
q = disposing(q, a.qNorm.forward(q));
|
|
448
|
+
q = disposing(q, ops.transposeAxes(q, [0, 2, 1, 3]));
|
|
449
|
+
q = disposing(q, a.rope(q, shared.offsetArr ?? shared.offset));
|
|
450
|
+
if (shared.kind !== "quant") throw new Error("generated sharer expected quant shared KV");
|
|
451
|
+
// dispatch-site constants: bits=4 group_size=64 nRep=4 head_dim=256
|
|
452
|
+
const attn = (L > 1 || process.env.MLX_BUN_FUSED_DECODE === "1") && fusedSdpaRuntimeOk(q, mask)
|
|
453
|
+
? quantizedSdpaTiled(q, shared.keys, shared.values, 1.0, mask, 64, 4)
|
|
454
|
+
: quantizedSdpaUnfused(q, shared.keys, shared.values, 1.0, mask, 64, 4);
|
|
455
|
+
q.dispose();
|
|
456
|
+
const attnT = ops.transposeAxes(attn, [0, 2, 1, 3]);
|
|
457
|
+
attn.dispose();
|
|
458
|
+
const merged = ops.reshape(attnT, [B, L, -1]);
|
|
459
|
+
attnT.dispose();
|
|
460
|
+
const out = a.oProj.forward(merged);
|
|
461
|
+
merged.dispose();
|
|
462
|
+
h.dispose();
|
|
463
|
+
h = out;
|
|
464
|
+
h = disposing(h, layer.postAttnNorm.forward(h));
|
|
465
|
+
h = disposing(h, ops.add(x, h));
|
|
466
|
+
const residual = h;
|
|
467
|
+
let f = layer.preFfNorm.forward(h);
|
|
468
|
+
f = disposing(f, layer.mlp.forward(f));
|
|
469
|
+
f = disposing(f, layer.postFfNorm.forward(f));
|
|
470
|
+
h = ops.add(residual, f);
|
|
471
|
+
residual.dispose();
|
|
472
|
+
f.dispose();
|
|
473
|
+
const res2 = h;
|
|
474
|
+
let gate = layer.perLayerGate!.forward(h);
|
|
475
|
+
gate = disposing(gate, ops.geluApprox(gate));
|
|
476
|
+
gate = disposing(gate, ops.mul(gate, pli));
|
|
477
|
+
gate = disposing(gate, layer.perLayerProjection!.forward(gate));
|
|
478
|
+
gate = disposing(gate, layer.postPerLayerNorm!.forward(gate));
|
|
479
|
+
h = ops.add(res2, gate);
|
|
480
|
+
res2.dispose();
|
|
481
|
+
gate.dispose();
|
|
482
|
+
h = disposing(h, ops.mul(h, layer.layerScalar!));
|
|
483
|
+
return h;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function sharerFullQuantG64B4VPliScal(layer: DecoderLayer, x: MlxArray, mask: Mask, shared: SharedKv, pli: MlxArray): MlxArray {
|
|
487
|
+
const a = layer.attn;
|
|
488
|
+
const [B, L] = x.shape as [number, number, number];
|
|
489
|
+
let h = layer.inputNorm.forward(x);
|
|
490
|
+
let q = a.qProj.forward(h);
|
|
491
|
+
q = disposing(q, ops.reshape(q, [B, L, a.nHeads, a.headDim]));
|
|
492
|
+
q = disposing(q, a.qNorm.forward(q));
|
|
493
|
+
q = disposing(q, ops.transposeAxes(q, [0, 2, 1, 3]));
|
|
494
|
+
q = disposing(q, a.rope(q, shared.offsetArr ?? shared.offset));
|
|
495
|
+
if (shared.kind !== "quant") throw new Error("generated sharer expected quant shared KV");
|
|
496
|
+
// dispatch-site constants: bits=4 group_size=64 nRep=4 head_dim=512
|
|
497
|
+
const attn = (L > 1 || process.env.MLX_BUN_FUSED_DECODE === "1") && fusedSdpaRuntimeOk(q, mask)
|
|
498
|
+
? quantizedSdpaTiled(q, shared.keys, shared.values, 1.0, mask, 64, 4)
|
|
499
|
+
: quantizedSdpaUnfused(q, shared.keys, shared.values, 1.0, mask, 64, 4);
|
|
500
|
+
q.dispose();
|
|
501
|
+
const attnT = ops.transposeAxes(attn, [0, 2, 1, 3]);
|
|
502
|
+
attn.dispose();
|
|
503
|
+
const merged = ops.reshape(attnT, [B, L, -1]);
|
|
504
|
+
attnT.dispose();
|
|
505
|
+
const out = a.oProj.forward(merged);
|
|
506
|
+
merged.dispose();
|
|
507
|
+
h.dispose();
|
|
508
|
+
h = out;
|
|
509
|
+
h = disposing(h, layer.postAttnNorm.forward(h));
|
|
510
|
+
h = disposing(h, ops.add(x, h));
|
|
511
|
+
const residual = h;
|
|
512
|
+
let f = layer.preFfNorm.forward(h);
|
|
513
|
+
f = disposing(f, layer.mlp.forward(f));
|
|
514
|
+
f = disposing(f, layer.postFfNorm.forward(f));
|
|
515
|
+
h = ops.add(residual, f);
|
|
516
|
+
residual.dispose();
|
|
517
|
+
f.dispose();
|
|
518
|
+
const res2 = h;
|
|
519
|
+
let gate = layer.perLayerGate!.forward(h);
|
|
520
|
+
gate = disposing(gate, ops.geluApprox(gate));
|
|
521
|
+
gate = disposing(gate, ops.mul(gate, pli));
|
|
522
|
+
gate = disposing(gate, layer.perLayerProjection!.forward(gate));
|
|
523
|
+
gate = disposing(gate, layer.postPerLayerNorm!.forward(gate));
|
|
524
|
+
h = ops.add(res2, gate);
|
|
525
|
+
res2.dispose();
|
|
526
|
+
gate.dispose();
|
|
527
|
+
h = disposing(h, ops.mul(h, layer.layerScalar!));
|
|
528
|
+
return h;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
export class GeneratedGemma4 extends Gemma4Model {
|
|
532
|
+
/** Per-layer cache classes must match what this file was generated
|
|
533
|
+
* for; anything else runs the monolith path. */
|
|
534
|
+
#matches(cache: Cache[]): boolean {
|
|
535
|
+
return (
|
|
536
|
+
cache.length === 24 &&
|
|
537
|
+
cache[0] instanceof RotatingQuantizedKVCache &&
|
|
538
|
+
cache[0].bits === 4 &&
|
|
539
|
+
cache[0].groupSize === 64 &&
|
|
540
|
+
cache[1] instanceof RotatingQuantizedKVCache &&
|
|
541
|
+
cache[1].bits === 4 &&
|
|
542
|
+
cache[1].groupSize === 64 &&
|
|
543
|
+
cache[2] instanceof RotatingQuantizedKVCache &&
|
|
544
|
+
cache[2].bits === 4 &&
|
|
545
|
+
cache[2].groupSize === 64 &&
|
|
546
|
+
cache[3] instanceof RotatingQuantizedKVCache &&
|
|
547
|
+
cache[3].bits === 8 &&
|
|
548
|
+
cache[3].groupSize === 64 &&
|
|
549
|
+
cache[4] instanceof RotatingQuantizedKVCache &&
|
|
550
|
+
cache[4].bits === 8 &&
|
|
551
|
+
cache[4].groupSize === 64 &&
|
|
552
|
+
cache[5] instanceof QuantizedKVCache &&
|
|
553
|
+
cache[5].bits === 4 &&
|
|
554
|
+
cache[5].groupSize === 64 &&
|
|
555
|
+
cache[6] instanceof RotatingQuantizedKVCache &&
|
|
556
|
+
cache[6].bits === 4 &&
|
|
557
|
+
cache[6].groupSize === 64 &&
|
|
558
|
+
cache[7] instanceof RotatingQuantizedKVCache &&
|
|
559
|
+
cache[7].bits === 4 &&
|
|
560
|
+
cache[7].groupSize === 64 &&
|
|
561
|
+
cache[8] instanceof RotatingQuantizedKVCache &&
|
|
562
|
+
cache[8].bits === 4 &&
|
|
563
|
+
cache[8].groupSize === 64 &&
|
|
564
|
+
cache[9] instanceof RotatingQuantizedKVCache &&
|
|
565
|
+
cache[9].bits === 8 &&
|
|
566
|
+
cache[9].groupSize === 64 &&
|
|
567
|
+
cache[10] instanceof RotatingQuantizedKVCache &&
|
|
568
|
+
cache[10].bits === 8 &&
|
|
569
|
+
cache[10].groupSize === 64 &&
|
|
570
|
+
cache[11] instanceof QuantizedKVCache &&
|
|
571
|
+
cache[11].bits === 8 &&
|
|
572
|
+
cache[11].groupSize === 64 &&
|
|
573
|
+
cache[12] instanceof RotatingQuantizedKVCache &&
|
|
574
|
+
cache[12].bits === 4 &&
|
|
575
|
+
cache[12].groupSize === 64 &&
|
|
576
|
+
cache[13] instanceof RotatingQuantizedKVCache &&
|
|
577
|
+
cache[13].bits === 4 &&
|
|
578
|
+
cache[13].groupSize === 64 &&
|
|
579
|
+
cache[14] instanceof RotatingQuantizedKVCache &&
|
|
580
|
+
cache[14].bits === 8 &&
|
|
581
|
+
cache[14].groupSize === 64 &&
|
|
582
|
+
cache[15] instanceof RotatingQuantizedKVCache &&
|
|
583
|
+
cache[15].bits === 4 &&
|
|
584
|
+
cache[15].groupSize === 64 &&
|
|
585
|
+
cache[16] instanceof RotatingQuantizedKVCache &&
|
|
586
|
+
cache[16].bits === 4 &&
|
|
587
|
+
cache[16].groupSize === 64 &&
|
|
588
|
+
cache[17] instanceof QuantizedKVCache &&
|
|
589
|
+
cache[17].bits === 4 &&
|
|
590
|
+
cache[17].groupSize === 64 &&
|
|
591
|
+
cache[18] instanceof RotatingQuantizedKVCache &&
|
|
592
|
+
cache[18].bits === 4 &&
|
|
593
|
+
cache[18].groupSize === 64 &&
|
|
594
|
+
cache[19] instanceof RotatingQuantizedKVCache &&
|
|
595
|
+
cache[19].bits === 4 &&
|
|
596
|
+
cache[19].groupSize === 64 &&
|
|
597
|
+
cache[20] instanceof RotatingQuantizedKVCache &&
|
|
598
|
+
cache[20].bits === 4 &&
|
|
599
|
+
cache[20].groupSize === 64 &&
|
|
600
|
+
cache[21] instanceof RotatingQuantizedKVCache &&
|
|
601
|
+
cache[21].bits === 4 &&
|
|
602
|
+
cache[21].groupSize === 64 &&
|
|
603
|
+
cache[22] instanceof RotatingQuantizedKVCache &&
|
|
604
|
+
cache[22].bits === 4 &&
|
|
605
|
+
cache[22].groupSize === 64 &&
|
|
606
|
+
cache[23] instanceof QuantizedKVCache &&
|
|
607
|
+
cache[23].bits === 4 &&
|
|
608
|
+
cache[23].groupSize === 64
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
protected override forwardLayers(
|
|
613
|
+
h0: MlxArray, cache: Cache[], bidir: MlxArray | null, ids: MlxArray | null,
|
|
614
|
+
): MlxArray {
|
|
615
|
+
if (bidir !== null || ids === null || !this.#matches(cache))
|
|
616
|
+
return super.forwardLayers(h0, cache, bidir, ids);
|
|
617
|
+
generatedForwardUses++;
|
|
618
|
+
const L = h0.shape[1]!;
|
|
619
|
+
const maskS = cache[0]!.makeMask(L, this.windowSize);
|
|
620
|
+
const maskF = cache[5]!.makeMask(L, null);
|
|
621
|
+
const perLayer = this.computePerLayerInputs(ids!, h0);
|
|
622
|
+
let h = h0;
|
|
623
|
+
let next: MlxArray;
|
|
624
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[0]!, h, maskS, cache[0] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 0, L, this.perLayerWidth));
|
|
625
|
+
h.dispose();
|
|
626
|
+
h = next;
|
|
627
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[1]!, h, maskS, cache[1] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 1, L, this.perLayerWidth));
|
|
628
|
+
h.dispose();
|
|
629
|
+
h = next;
|
|
630
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[2]!, h, maskS, cache[2] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 2, L, this.perLayerWidth));
|
|
631
|
+
h.dispose();
|
|
632
|
+
h = next;
|
|
633
|
+
next = donorSlidQuantG64B8VPliScal(this.layers[3]!, h, maskS, cache[3] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 3, L, this.perLayerWidth));
|
|
634
|
+
h.dispose();
|
|
635
|
+
h = next;
|
|
636
|
+
next = donorSlidQuantG64B8VPliScal(this.layers[4]!, h, maskS, cache[4] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 4, L, this.perLayerWidth));
|
|
637
|
+
h.dispose();
|
|
638
|
+
h = next;
|
|
639
|
+
next = donorFullQuantG64B4VPliScal(this.layers[5]!, h, maskF, cache[5] as QuantizedKVCache, perLayerSlice(perLayer, 5, L, this.perLayerWidth));
|
|
640
|
+
h.dispose();
|
|
641
|
+
h = next;
|
|
642
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[6]!, h, maskS, cache[6] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 6, L, this.perLayerWidth));
|
|
643
|
+
h.dispose();
|
|
644
|
+
h = next;
|
|
645
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[7]!, h, maskS, cache[7] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 7, L, this.perLayerWidth));
|
|
646
|
+
h.dispose();
|
|
647
|
+
h = next;
|
|
648
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[8]!, h, maskS, cache[8] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 8, L, this.perLayerWidth));
|
|
649
|
+
h.dispose();
|
|
650
|
+
h = next;
|
|
651
|
+
next = donorSlidQuantG64B8VPliScal(this.layers[9]!, h, maskS, cache[9] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 9, L, this.perLayerWidth));
|
|
652
|
+
h.dispose();
|
|
653
|
+
h = next;
|
|
654
|
+
next = donorSlidQuantG64B8VPliScal(this.layers[10]!, h, maskS, cache[10] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 10, L, this.perLayerWidth));
|
|
655
|
+
h.dispose();
|
|
656
|
+
h = next;
|
|
657
|
+
next = donorFullQuantG64B8VPliScal(this.layers[11]!, h, maskF, cache[11] as QuantizedKVCache, perLayerSlice(perLayer, 11, L, this.perLayerWidth));
|
|
658
|
+
h.dispose();
|
|
659
|
+
h = next;
|
|
660
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[12]!, h, maskS, cache[12] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 12, L, this.perLayerWidth));
|
|
661
|
+
h.dispose();
|
|
662
|
+
h = next;
|
|
663
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[13]!, h, maskS, cache[13] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 13, L, this.perLayerWidth));
|
|
664
|
+
h.dispose();
|
|
665
|
+
h = next;
|
|
666
|
+
next = donorSlidQuantG64B8VPliScal(this.layers[14]!, h, maskS, cache[14] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 14, L, this.perLayerWidth));
|
|
667
|
+
h.dispose();
|
|
668
|
+
h = next;
|
|
669
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[15]!, h, maskS, cache[15] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 15, L, this.perLayerWidth));
|
|
670
|
+
h.dispose();
|
|
671
|
+
h = next;
|
|
672
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[16]!, h, maskS, cache[16] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 16, L, this.perLayerWidth));
|
|
673
|
+
h.dispose();
|
|
674
|
+
h = next;
|
|
675
|
+
next = donorFullQuantG64B4VPliScal(this.layers[17]!, h, maskF, cache[17] as QuantizedKVCache, perLayerSlice(perLayer, 17, L, this.perLayerWidth));
|
|
676
|
+
h.dispose();
|
|
677
|
+
h = next;
|
|
678
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[18]!, h, maskS, cache[18] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 18, L, this.perLayerWidth));
|
|
679
|
+
h.dispose();
|
|
680
|
+
h = next;
|
|
681
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[19]!, h, maskS, cache[19] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 19, L, this.perLayerWidth));
|
|
682
|
+
h.dispose();
|
|
683
|
+
h = next;
|
|
684
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[20]!, h, maskS, cache[20] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 20, L, this.perLayerWidth));
|
|
685
|
+
h.dispose();
|
|
686
|
+
h = next;
|
|
687
|
+
next = donorSlidQuantG64B4VPliScal(this.layers[21]!, h, maskS, cache[21] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 21, L, this.perLayerWidth));
|
|
688
|
+
h.dispose();
|
|
689
|
+
h = next;
|
|
690
|
+
const r22 = donorSlidQuantG64B4VPliScalExp(this.layers[22]!, h, maskS, cache[22] as RotatingQuantizedKVCache, perLayerSlice(perLayer, 22, L, this.perLayerWidth));
|
|
691
|
+
h.dispose();
|
|
692
|
+
h = r22.h;
|
|
693
|
+
const r23 = donorFullQuantG64B4VPliScalExp(this.layers[23]!, h, maskF, cache[23] as QuantizedKVCache, perLayerSlice(perLayer, 23, L, this.perLayerWidth));
|
|
694
|
+
h.dispose();
|
|
695
|
+
h = r23.h;
|
|
696
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[24]!, h, maskS, r22.shared, perLayerSlice(perLayer, 24, L, this.perLayerWidth));
|
|
697
|
+
h.dispose();
|
|
698
|
+
h = next;
|
|
699
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[25]!, h, maskS, r22.shared, perLayerSlice(perLayer, 25, L, this.perLayerWidth));
|
|
700
|
+
h.dispose();
|
|
701
|
+
h = next;
|
|
702
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[26]!, h, maskS, r22.shared, perLayerSlice(perLayer, 26, L, this.perLayerWidth));
|
|
703
|
+
h.dispose();
|
|
704
|
+
h = next;
|
|
705
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[27]!, h, maskS, r22.shared, perLayerSlice(perLayer, 27, L, this.perLayerWidth));
|
|
706
|
+
h.dispose();
|
|
707
|
+
h = next;
|
|
708
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[28]!, h, maskS, r22.shared, perLayerSlice(perLayer, 28, L, this.perLayerWidth));
|
|
709
|
+
h.dispose();
|
|
710
|
+
h = next;
|
|
711
|
+
next = sharerFullQuantG64B4VPliScal(this.layers[29]!, h, maskF, r23.shared, perLayerSlice(perLayer, 29, L, this.perLayerWidth));
|
|
712
|
+
h.dispose();
|
|
713
|
+
h = next;
|
|
714
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[30]!, h, maskS, r22.shared, perLayerSlice(perLayer, 30, L, this.perLayerWidth));
|
|
715
|
+
h.dispose();
|
|
716
|
+
h = next;
|
|
717
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[31]!, h, maskS, r22.shared, perLayerSlice(perLayer, 31, L, this.perLayerWidth));
|
|
718
|
+
h.dispose();
|
|
719
|
+
h = next;
|
|
720
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[32]!, h, maskS, r22.shared, perLayerSlice(perLayer, 32, L, this.perLayerWidth));
|
|
721
|
+
h.dispose();
|
|
722
|
+
h = next;
|
|
723
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[33]!, h, maskS, r22.shared, perLayerSlice(perLayer, 33, L, this.perLayerWidth));
|
|
724
|
+
h.dispose();
|
|
725
|
+
h = next;
|
|
726
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[34]!, h, maskS, r22.shared, perLayerSlice(perLayer, 34, L, this.perLayerWidth));
|
|
727
|
+
h.dispose();
|
|
728
|
+
h = next;
|
|
729
|
+
next = sharerFullQuantG64B4VPliScal(this.layers[35]!, h, maskF, r23.shared, perLayerSlice(perLayer, 35, L, this.perLayerWidth));
|
|
730
|
+
h.dispose();
|
|
731
|
+
h = next;
|
|
732
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[36]!, h, maskS, r22.shared, perLayerSlice(perLayer, 36, L, this.perLayerWidth));
|
|
733
|
+
h.dispose();
|
|
734
|
+
h = next;
|
|
735
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[37]!, h, maskS, r22.shared, perLayerSlice(perLayer, 37, L, this.perLayerWidth));
|
|
736
|
+
h.dispose();
|
|
737
|
+
h = next;
|
|
738
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[38]!, h, maskS, r22.shared, perLayerSlice(perLayer, 38, L, this.perLayerWidth));
|
|
739
|
+
h.dispose();
|
|
740
|
+
h = next;
|
|
741
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[39]!, h, maskS, r22.shared, perLayerSlice(perLayer, 39, L, this.perLayerWidth));
|
|
742
|
+
h.dispose();
|
|
743
|
+
h = next;
|
|
744
|
+
next = sharerSlidQuantG64B4VPliScal(this.layers[40]!, h, maskS, r22.shared, perLayerSlice(perLayer, 40, L, this.perLayerWidth));
|
|
745
|
+
h.dispose();
|
|
746
|
+
h = next;
|
|
747
|
+
next = sharerFullQuantG64B4VPliScal(this.layers[41]!, h, maskF, r23.shared, perLayerSlice(perLayer, 41, L, this.perLayerWidth));
|
|
748
|
+
h.dispose();
|
|
749
|
+
h = next;
|
|
750
|
+
disposeSharedKv(r22.shared);
|
|
751
|
+
disposeSharedKv(r23.shared);
|
|
752
|
+
perLayer.dispose();
|
|
753
|
+
maskS.arr?.dispose();
|
|
754
|
+
maskF.arr?.dispose();
|
|
755
|
+
return disposing(h, this.finalNorm.forward(h));
|
|
756
|
+
}
|
|
757
|
+
}
|