mlx-bun 0.0.13 → 0.3.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.
Files changed (427) hide show
  1. package/README.md +71 -578
  2. package/bin/mlx-bun.mjs +1 -1
  3. package/docs/README.md +91 -0
  4. package/docs/{design → archive}/hlg-sampling.md +3 -3
  5. package/docs/{design → archive/investigations}/audio-input-plan.md +4 -4
  6. package/docs/{design → archive/investigations}/colibri-glm52-port.md +1 -1
  7. package/docs/{design → archive/investigations}/diffusion-gemma-port.md +3 -3
  8. package/docs/{design → archive/investigations}/faithful-l1-consolidation.md +4 -4
  9. package/docs/{investigations → archive/investigations}/hlg-sampling-investigation.md +1 -1
  10. package/docs/{design → archive/investigations}/minicpm5-decode-megakernel.md +11 -11
  11. package/docs/{design → archive/investigations}/optimization_plan.md +1 -1
  12. package/docs/archive/investigations/pareto-specialized-runtime-findings.md +92 -0
  13. package/docs/{design → archive/investigations}/pre-colibri-stabilization.md +5 -5
  14. package/docs/{design → archive/investigations}/the-dreaming-handoff.md +2 -2
  15. package/docs/{planning → archive/planning}/chat-ui-vision.md +1 -1
  16. package/docs/{planning → archive/planning}/chunk-task-roadmap.md +1 -1
  17. package/docs/{planning → archive/planning}/curve-sampler-research-plan.md +4 -4
  18. package/docs/{planning → archive/planning}/memory-docs-and-dag-plan.md +1 -1
  19. package/docs/{planning → archive/planning}/release-notes-v0.0.9.md +1 -1
  20. package/docs/archive/planning/release-notes-v0.2.0.md +49 -0
  21. package/docs/{planning → archive/planning}/website-readme-pass-plan.md +1 -1
  22. package/docs/design/batching.md +569 -0
  23. package/docs/design/decode-speed-program.md +3178 -169
  24. package/docs/design/dreaming-nightly-pipeline.md +675 -282
  25. package/docs/design/generic-model-support.md +579 -334
  26. package/docs/design/kv-cache.md +680 -0
  27. package/docs/design/orpo-training.md +673 -1239
  28. package/docs/design/speculative-decoding.md +1627 -0
  29. package/docs/design/turboquant.md +2102 -0
  30. package/docs/design/unified-engine-frontier-plan.md +1831 -727
  31. package/docs/design/web-chat-redesign.md +666 -1083
  32. package/docs/planning/PRODUCT_ROADMAP.md +34 -1
  33. package/docs/planning/release-notes-v0.3.0.md +35 -0
  34. package/docs/planning/video-series.md +421 -0
  35. package/docs/reference/benchmarks.md +1162 -0
  36. package/docs/reference/cli.md +395 -149
  37. package/docs/reference/distribution.md +321 -181
  38. package/docs/reference/environment.md +348 -0
  39. package/docs/reference/glossary.md +39 -0
  40. package/docs/reference/library-api.md +580 -142
  41. package/docs/reference/memory.md +227 -64
  42. package/docs/reference/models.md +137 -6
  43. package/docs/reference/server-api.md +938 -422
  44. package/docs/reference/server-config.md +652 -320
  45. package/docs/reference/training.md +393 -254
  46. package/docs/reference/troubleshooting.md +166 -0
  47. package/package.json +10 -5
  48. package/src/admission-plan.ts +18 -0
  49. package/src/anthropic.ts +92 -3
  50. package/src/audio/decode.ts +2 -2
  51. package/src/audio/features.ts +2 -2
  52. package/src/backends/mlx/autoregressive.ts +108 -0
  53. package/src/backends/mlx/batch-group.ts +1646 -0
  54. package/src/backends/mlx/diffusion.ts +39 -0
  55. package/src/backends/mlx/draft-target.ts +47 -0
  56. package/src/backends/mlx/evaluation.ts +105 -0
  57. package/src/backends/mlx/fill-append.ts +45 -0
  58. package/src/backends/mlx/gateway-binding.ts +71 -0
  59. package/src/backends/mlx/graph.ts +38 -0
  60. package/src/backends/mlx/memory-client.ts +189 -0
  61. package/src/backends/mlx/memory-guard.ts +40 -0
  62. package/src/backends/mlx/methods.ts +90 -0
  63. package/src/backends/mlx/model-prompt.ts +164 -0
  64. package/src/backends/mlx/model-serving.ts +79 -0
  65. package/src/backends/mlx/prefill.ts +33 -0
  66. package/src/backends/mlx/request-policy.ts +20 -0
  67. package/src/backends/mlx/rollback.ts +51 -0
  68. package/src/backends/mlx/serial-executor.ts +273 -0
  69. package/src/backends/mlx/speculative.ts +114 -0
  70. package/src/backends/mlx/state-views.ts +26 -0
  71. package/src/backends/mlx/text-engine.ts +73 -0
  72. package/src/cli.ts +189 -61
  73. package/src/client.ts +66 -0
  74. package/src/config.ts +21 -4
  75. package/src/contracts/completion.ts +16 -0
  76. package/src/contracts/execution.ts +49 -0
  77. package/src/contracts/generation.ts +103 -0
  78. package/src/contracts/host.ts +7 -0
  79. package/src/contracts/jobs.ts +66 -0
  80. package/src/contracts/pi-provider.ts +30 -0
  81. package/src/contracts/pi.ts +259 -0
  82. package/src/contracts/resources.ts +18 -0
  83. package/src/contracts/scheduling.ts +27 -0
  84. package/src/contracts/tsconfig.json +14 -0
  85. package/src/diffusion/diffusion-generate.ts +218 -153
  86. package/src/embed.ts +1 -1
  87. package/src/engine/admission.ts +59 -0
  88. package/src/engine/cancellation.ts +38 -0
  89. package/src/engine/engine.ts +56 -0
  90. package/src/engine/execution-coordinator.ts +70 -0
  91. package/src/engine/execution-plan.ts +51 -0
  92. package/src/engine/resources.ts +42 -0
  93. package/src/engine/scheduler.ts +67 -0
  94. package/src/engine/session.ts +247 -0
  95. package/src/eval/README.md +44 -1
  96. package/src/eval/kl.ts +263 -23
  97. package/src/eval/reference-logits.ts +207 -0
  98. package/src/eval/runner.ts +10 -79
  99. package/src/eval/tasks/bfcl.ts +2 -1
  100. package/src/eval/tasks/gsm8k.ts +2 -1
  101. package/src/eval/tasks/hashhop.ts +2 -1
  102. package/src/eval/tasks/humaneval.ts +5 -4
  103. package/src/eval/tasks/ifeval.ts +2 -1
  104. package/src/eval/tasks/mmlu.ts +2 -1
  105. package/src/expert-io.ts +3 -1
  106. package/src/expert-offload.ts +3 -1
  107. package/src/fill/echo-index.ts +188 -0
  108. package/src/fill/fill-session.ts +452 -0
  109. package/src/fill/proposal.ts +68 -0
  110. package/src/fill/schema-rows.ts +506 -0
  111. package/src/fill/tool-boundary.ts +103 -0
  112. package/src/fit.ts +24 -43
  113. package/src/generate.ts +740 -337
  114. package/src/grammar.ts +14 -8
  115. package/src/harness-pi.ts +15 -34
  116. package/src/hub-rest.ts +4 -4
  117. package/src/index.ts +27 -0
  118. package/src/inference/denoising.ts +17 -0
  119. package/src/inference/graph.ts +33 -0
  120. package/src/inference/prefill.ts +39 -0
  121. package/src/inference/rollback.ts +7 -0
  122. package/src/jobs/db.ts +4 -0
  123. package/src/jobs/index.ts +2 -0
  124. package/src/jobs/job-entry.ts +15 -14
  125. package/src/jobs/runner.ts +98 -13
  126. package/src/jobs/task-client.ts +19 -0
  127. package/src/jobs/types.ts +2 -58
  128. package/src/kv-scheme.ts +199 -0
  129. package/src/kv-store.ts +459 -234
  130. package/src/lab/README.md +18 -0
  131. package/src/{assets → lab/curve}/curve-designer.html +2 -2
  132. package/src/{curve-sampler.ts → lab/curve/curve-sampler.ts} +2 -2
  133. package/src/{expert-trace.ts → lab/expert-trace/expert-trace.ts} +7 -5
  134. package/src/{model → lab/paged-kv}/paged-kv.ts +9 -6
  135. package/src/library.ts +40 -0
  136. package/src/lora.ts +18 -0
  137. package/src/media-fetch.ts +20 -4
  138. package/src/memory/article.ts +1 -1
  139. package/src/memory/chunk.ts +1 -1
  140. package/src/memory/model.ts +32 -170
  141. package/src/memory/prompts.ts +1 -1
  142. package/src/memory/rest.ts +1 -1
  143. package/src/memory/schedule.ts +1 -1
  144. package/src/memory/stages.ts +4 -1
  145. package/src/memory/vault.ts +3 -4
  146. package/src/mlx/array.ts +9 -10
  147. package/src/mlx/compile.ts +12 -2
  148. package/src/mlx/ffi.ts +66 -22
  149. package/src/mlx/materialize.ts +23 -0
  150. package/src/mlx/metal-kernel.ts +10 -11
  151. package/src/mlx/ops.ts +64 -19
  152. package/src/mlx/quantized-rows.ts +36 -0
  153. package/src/mlx/token-bitmask.ts +52 -0
  154. package/src/mlx/turboquant-kv-decode.ts +82 -0
  155. package/src/mlx/turboquant-kv-inverse.ts +122 -0
  156. package/src/mlx/turboquant-ops.ts +33 -3
  157. package/src/mlx/turboquant-tables.ts +2 -2
  158. package/src/model/batched-mask.ts +9 -3
  159. package/src/model/batched-quant.ts +2 -2
  160. package/src/model/batched-rotating-quant.ts +45 -99
  161. package/src/model/batched-rotating-state.ts +73 -0
  162. package/src/model/batched-rotating.ts +57 -106
  163. package/src/model/batched-row-storage.ts +127 -0
  164. package/src/model/compiled-decode.ts +4 -3
  165. package/src/model/diffusion-gemma.ts +8 -8
  166. package/src/model/factory.ts +87 -49
  167. package/src/model/fingerprint.ts +1 -1
  168. package/src/model/flash-attention.ts +3 -3
  169. package/src/model/gemma4-base.ts +187 -17
  170. package/src/model/gemma4.ts +6 -6
  171. package/src/model/generated/gemma4-12b.ts +52 -148
  172. package/src/model/generated/gemma4-26b.ts +34 -94
  173. package/src/model/generated/gemma4-e4b.ts +28 -76
  174. package/src/model/glm52-cache.ts +19 -9
  175. package/src/model/glm52-dsa.ts +13 -9
  176. package/src/model/glm52-memory.ts +28 -1
  177. package/src/model/implementation.ts +69 -0
  178. package/src/model/minicpm5.ts +7 -8
  179. package/src/model/profile.ts +443 -0
  180. package/src/model/qwen-append.ts +12 -0
  181. package/src/model/qwen-conv.ts +52 -0
  182. package/src/model/qwen3-delta.ts +98 -2
  183. package/src/model/qwen3-mrope.ts +252 -0
  184. package/src/model/qwen3_5-checkpoint.ts +164 -0
  185. package/src/model/qwen3_5.ts +336 -60
  186. package/src/model/support.ts +1 -1
  187. package/src/model/trellis-balanced-scatter.ts +51 -0
  188. package/src/model/trellis-linear.ts +672 -0
  189. package/src/model/trellis-shared-m.ts +151 -0
  190. package/src/model/trellis-shared-scatter.ts +144 -0
  191. package/src/model/trellis-splitk-prefill.ts +158 -0
  192. package/src/model/trellis-tiled-prefill.ts +136 -0
  193. package/src/model/trellis-vector-expand.ts +62 -0
  194. package/src/model/trellis-wide-prefill.ts +94 -0
  195. package/src/model/universal/dense.ts +1 -1
  196. package/src/model/universal/modules.ts +8 -1
  197. package/src/native/frame_extract.swift +140 -0
  198. package/src/native-pack.ts +15 -7
  199. package/src/pi-provider.ts +6 -64
  200. package/src/pi-web.ts +14 -320
  201. package/src/prompt-cache.ts +97 -54
  202. package/src/quantize/allocator.ts +1 -1
  203. package/src/quantize/atomic-output.ts +38 -0
  204. package/src/quantize/calibration.ts +2 -1
  205. package/src/quantize/config-writer.ts +16 -3
  206. package/src/quantize/index.ts +28 -1
  207. package/src/quantize/job.ts +11 -0
  208. package/src/quantize/quantizer.ts +265 -100
  209. package/src/quantize/rotate.ts +603 -0
  210. package/src/quantize/safetensors-writer.ts +79 -1
  211. package/src/quantize/sensitivity.ts +41 -59
  212. package/src/quantize/trellis.ts +586 -0
  213. package/src/quantize/weight-transform.ts +292 -0
  214. package/src/responses.ts +130 -3
  215. package/src/runtime-config.ts +81 -0
  216. package/src/sampler.ts +276 -4
  217. package/src/serve/admin-routes.ts +184 -0
  218. package/src/serve/aux-routes.ts +132 -0
  219. package/src/serve/batch-scheduler.ts +8 -1558
  220. package/src/serve/chat-request.ts +391 -0
  221. package/src/serve/chat-stage.ts +195 -0
  222. package/src/serve/checkpoint-identity.ts +64 -0
  223. package/src/serve/completion-executor.ts +354 -0
  224. package/src/serve/completion-sink.ts +157 -0
  225. package/src/serve/discovery-routes.ts +204 -0
  226. package/src/serve/generation-gateway.ts +211 -243
  227. package/src/serve/http.ts +159 -0
  228. package/src/serve/inference-request.ts +79 -0
  229. package/src/serve/isolate.ts +297 -105
  230. package/src/serve/kv-budget.ts +14 -0
  231. package/src/serve/lab-routes.ts +341 -0
  232. package/src/serve/lane-registry.ts +2 -1
  233. package/src/serve/model-admin-routes.ts +155 -0
  234. package/src/serve/model-binding.ts +43 -0
  235. package/src/serve/model-host.ts +512 -0
  236. package/src/serve/openai-wire.ts +103 -0
  237. package/src/serve/parent-application.ts +56 -0
  238. package/src/serve/pipeline.ts +33 -0
  239. package/src/serve/preparation.ts +33 -0
  240. package/src/serve/prompt-response-trace.ts +147 -0
  241. package/src/serve/request-plan.ts +162 -0
  242. package/src/serve/request-prep.ts +346 -0
  243. package/src/serve/response-lease.ts +24 -0
  244. package/src/serve/responses-client.ts +69 -0
  245. package/src/serve/session-completion-engine.ts +110 -0
  246. package/src/serve/session-search.ts +1 -1
  247. package/src/serve/static-routes.ts +101 -0
  248. package/src/serve/text-completion-stage.ts +104 -0
  249. package/src/serve/token-streams.ts +519 -0
  250. package/src/server.ts +523 -3551
  251. package/src/spec/assistant-source.ts +10 -44
  252. package/src/spec/deepspec-source.ts +6 -6
  253. package/src/spec/dflash-source.ts +9 -8
  254. package/src/spec/dspark/ab-stats.ts +3 -3
  255. package/src/spec/dspark/calibration.ts +2 -2
  256. package/src/spec/dspark/deepspec-module.ts +2 -2
  257. package/src/spec/dspark/generate-dflash.ts +2 -2
  258. package/src/spec/dspark/generate.ts +2 -2
  259. package/src/spec/dspark/loader.ts +1 -1
  260. package/src/spec/dspark/module-dflash.ts +5 -4
  261. package/src/spec/dspark/module.ts +3 -3
  262. package/src/spec/dspark/quantize-drafter.ts +1 -1
  263. package/src/spec/generate.ts +1 -1
  264. package/src/spec/glm52-mtp-source.ts +9 -1
  265. package/src/spec/ngram-source.ts +1 -1
  266. package/src/spec/prefix-state.ts +45 -0
  267. package/src/spec/qwen-mtp-source.ts +528 -0
  268. package/src/spec/serve-loop.ts +179 -213
  269. package/src/spec/source.ts +42 -10
  270. package/src/spec/two-model.ts +1 -1
  271. package/src/ssd-cache.ts +95 -24
  272. package/src/ssd-durability.ts +207 -0
  273. package/src/tool-call.ts +51 -14
  274. package/src/train/flash-cce.ts +14 -13
  275. package/src/train/forward.ts +38 -1
  276. package/src/train/loss.ts +3 -2
  277. package/src/train/prefix-shared.ts +5 -1
  278. package/src/train/segmented.ts +9 -6
  279. package/src/train/trainer.ts +9 -8
  280. package/src/vision/preprocess.ts +1 -1
  281. package/src/vision/prompt.ts +48 -2
  282. package/src/vision/qwen3vl-preprocess.ts +409 -0
  283. package/src/vision/qwen3vl-prompt.ts +196 -0
  284. package/src/vision/qwen3vl-tower.ts +555 -0
  285. package/src/vision/video-frames.ts +148 -0
  286. package/src/web/app.html +1 -1
  287. package/src/web/app.js +15 -6
  288. package/src/web/src/adapters-panel.ts +3 -3
  289. package/src/web/src/assistant.ts +2 -36
  290. package/src/web/src/chat.ts +16 -9
  291. package/src/web/src/composer.ts +4 -4
  292. package/src/web/src/hub.ts +1 -1
  293. package/src/web/src/markdown.ts +0 -0
  294. package/src/web/src/memory-panel.ts +1 -1
  295. package/src/web/src/model-picker.ts +1 -1
  296. package/src/web/src/palette.ts +1 -1
  297. package/src/web/src/protocol.ts +3 -39
  298. package/src/web/src/sessions.ts +1 -1
  299. package/src/web/src/ui-catalog.ts +4 -18
  300. package/src/web-assets.ts +47 -0
  301. package/src/weights.ts +134 -33
  302. package/docs/archive/mlx-bun-lab-report.html +0 -275
  303. package/docs/archive/wwdc-mlx-bun.html +0 -848
  304. package/docs/design/adapters-end-to-end.md +0 -205
  305. package/docs/design/batching-perf-path.md +0 -255
  306. package/docs/design/batching-v2-plan.md +0 -529
  307. package/docs/design/bucketing-stage.md +0 -197
  308. package/docs/design/compat-cli-surface-design.md +0 -230
  309. package/docs/design/docs-reorg-plan.md +0 -165
  310. package/docs/design/dspark-serving-program.md +0 -371
  311. package/docs/design/dspark-speculative-decoding.md +0 -268
  312. package/docs/design/grammar-spec-batching-integration.md +0 -301
  313. package/docs/design/memory-inference-path.md +0 -422
  314. package/docs/design/memory-synthesis.md +0 -287
  315. package/docs/design/memory-system.md +0 -237
  316. package/docs/design/mlx-lm-tool-parity-plan.md +0 -545
  317. package/docs/design/omlx-adoption-map.md +0 -98
  318. package/docs/design/orpo-dynamic-lambda.md +0 -220
  319. package/docs/design/orpo-future-enhancements.md +0 -340
  320. package/docs/design/paged-kv-cache.md +0 -214
  321. package/docs/design/parallel-slots.md +0 -567
  322. package/docs/design/parity-tier-dag.md +0 -161
  323. package/docs/design/repo-cleanup-plan.md +0 -189
  324. package/docs/design/runtime-isolation.md +0 -263
  325. package/docs/design/segmented-backward-training.md +0 -543
  326. package/docs/design/spec-decode-larger-targets.md +0 -229
  327. package/docs/design/ssd-kv-cold-tier.md +0 -288
  328. package/docs/design/structured-output.md +0 -484
  329. package/docs/design/tauri-desktop-app.md +0 -127
  330. package/docs/design/the-dreaming-master-plan.md +0 -445
  331. package/docs/design/turboquant-kv.md +0 -235
  332. package/docs/design/w4a16-compute-precision-spike.md +0 -232
  333. package/docs/design/web-chat-beat-matrix.md +0 -315
  334. package/docs/design/write-pipeline-entity.md +0 -468
  335. package/docs/investigations/curve-runs/bisector-route.json +0 -98
  336. package/docs/investigations/curve-runs/bisector-trace.json +0 -1393
  337. package/docs/investigations/curve-runs/curve-terrain.json +0 -1179
  338. package/docs/investigations/curve-runs/curve-terrain.svg +0 -42
  339. package/docs/investigations/hlg-ab.html +0 -34
  340. package/docs/investigations/hlg-explorer.html +0 -238
  341. package/docs/investigations/hlg-figs/cliff.svg +0 -1
  342. package/docs/investigations/hlg-figs/cross.svg +0 -1
  343. package/docs/investigations/hlg-figs/edges.svg +0 -1
  344. package/docs/investigations/hlg-figs/k20-grids.svg +0 -1
  345. package/docs/investigations/hlg-figs/k20-peaks.svg +0 -1
  346. package/docs/investigations/hlg-figs/lw-curve.svg +0 -1
  347. package/docs/investigations/hlg-figs/terrain.svg +0 -1
  348. package/docs/investigations/hlg-report.html +0 -262
  349. package/docs/investigations/hlg-runs/hlg-ab.json +0 -149
  350. package/docs/investigations/hlg-runs/hlg-blind-prompts.md +0 -87
  351. package/docs/investigations/hlg-runs/hlg-blind.json +0 -107
  352. package/docs/investigations/hlg-runs/hlg-cliff.json +0 -197
  353. package/docs/investigations/hlg-runs/hlg-cross.json +0 -505
  354. package/docs/investigations/hlg-runs/hlg-edge.json +0 -388
  355. package/docs/investigations/hlg-runs/hlg-eval1.json +0 -25
  356. package/docs/investigations/hlg-runs/hlg-frontier.json +0 -86
  357. package/docs/investigations/hlg-runs/hlg-grid.json +0 -242
  358. package/docs/investigations/hlg-runs/hlg-grid2.json +0 -343
  359. package/docs/investigations/hlg-runs/hlg-lw-coarse.json +0 -110
  360. package/docs/investigations/hlg-runs/hlg-map.json +0 -383
  361. package/docs/investigations/hlg-runs/run0-original.log +0 -159
  362. package/docs/investigations/hlg-runs/runA-recommended.log +0 -131
  363. package/docs/investigations/hlg-runs/runAA-cliff-cpm5.log +0 -26
  364. package/docs/investigations/hlg-runs/runAB-grid2-e4b.log +0 -18
  365. package/docs/investigations/hlg-runs/runAC-peak-e4b-partial.log +0 -25
  366. package/docs/investigations/hlg-runs/runAD-ab-samples.log +0 -8
  367. package/docs/investigations/hlg-runs/runAE-edge-e4b.log +0 -51
  368. package/docs/investigations/hlg-runs/runAF-logit-calibration.log +0 -10
  369. package/docs/investigations/hlg-runs/runAG-eval-config2.log +0 -16
  370. package/docs/investigations/hlg-runs/runB-gain-variants.log +0 -192
  371. package/docs/investigations/hlg-runs/runC-pivots.log +0 -162
  372. package/docs/investigations/hlg-runs/runD-oetf.log +0 -132
  373. package/docs/investigations/hlg-runs/runE-eotf.log +0 -162
  374. package/docs/investigations/hlg-runs/runF-shaper-working.log +0 -86
  375. package/docs/investigations/hlg-runs/runG-shaper-sweep.log +0 -101
  376. package/docs/investigations/hlg-runs/runH-wsweep.log +0 -176
  377. package/docs/investigations/hlg-runs/runI-asweep.log +0 -176
  378. package/docs/investigations/hlg-runs/runJ-knobmap.log +0 -49
  379. package/docs/investigations/hlg-runs/runK-frontier.log +0 -19
  380. package/docs/investigations/hlg-runs/runL-widemap.log +0 -65
  381. package/docs/investigations/hlg-runs/runM-frontier-emb.log +0 -15
  382. package/docs/investigations/hlg-runs/runN-verify.log +0 -116
  383. package/docs/investigations/hlg-runs/runO-frontier-seed5000.log +0 -15
  384. package/docs/investigations/hlg-runs/runP-grid-AxSm.log +0 -15
  385. package/docs/investigations/hlg-runs/runQ-grid-tgW.log +0 -15
  386. package/docs/investigations/hlg-runs/runR-blind-transcript.log +0 -79
  387. package/docs/investigations/hlg-runs/runS-cross-coherence.log +0 -33
  388. package/docs/investigations/hlg-runs/runT-cross-cpm5.log +0 -33
  389. package/docs/investigations/hlg-runs/runU-cross-12b.log +0 -33
  390. package/docs/investigations/hlg-runs/runV-lw-e4b.log +0 -22
  391. package/docs/investigations/hlg-runs/runW-lw-12b.log +0 -22
  392. package/docs/investigations/hlg-runs/runX-lw-cpm5.log +0 -22
  393. package/docs/investigations/hlg-runs/runY-cliff-e4b.log +0 -26
  394. package/docs/investigations/hlg-runs/runZ-cliff-12b.log +0 -26
  395. package/docs/planning/IDEAS.md +0 -69
  396. package/docs/reference/embedding.md +0 -197
  397. package/docs/reference/features-matrix.md +0 -100
  398. package/docs/reference/orpo-quickstart.md +0 -135
  399. package/src/flags.ts +0 -13
  400. /package/docs/{investigations → archive/investigations}/chunk-finetune-experiment.md +0 -0
  401. /package/docs/{investigations → archive/investigations}/colibri-oracle-pin.md +0 -0
  402. /package/docs/{investigations → archive/investigations}/curve-bisector-routing.md +0 -0
  403. /package/docs/{investigations → archive/investigations}/decode-roofline-lookagain.md +0 -0
  404. /package/docs/{investigations → archive/investigations}/dspark-handoff.md +0 -0
  405. /package/docs/{investigations → archive/investigations}/expert-offload-single-user-moe.md +0 -0
  406. /package/docs/{investigations → archive/investigations}/indexshare-performance-spike.md +0 -0
  407. /package/docs/{investigations → archive/investigations}/kernel-perf-review-2026-07.md +0 -0
  408. /package/docs/{investigations → archive/investigations}/lab-build-journal.md +0 -0
  409. /package/docs/{investigations → archive/investigations}/openwiki-evaluation.md +0 -0
  410. /package/docs/{investigations → archive/investigations}/orpo-base-uf-experiment-and-directions.md +0 -0
  411. /package/docs/{investigations → archive/investigations}/orpo-flash-cce-pin-leak.md +0 -0
  412. /package/docs/{investigations → archive/investigations}/orpo-uf-testing-handoff.md +0 -0
  413. /package/docs/{investigations → archive/investigations}/pi-builtin-investigation.md +0 -0
  414. /package/docs/{investigations → archive/investigations}/segmented-backward-handoff.md +0 -0
  415. /package/docs/{investigations → archive/investigations}/starter-model-port-handoff.md +0 -0
  416. /package/docs/{investigations → archive/investigations}/steel-flash-cce-handoff.md +0 -0
  417. /package/docs/{investigations → archive/investigations}/trainer-validation-experiment.md +0 -0
  418. /package/docs/{planning → archive/planning}/chat-ui-vision-memory-first.md +0 -0
  419. /package/docs/{planning → archive/planning}/journal.md +0 -0
  420. /package/docs/{planning → archive/planning}/pi-integration-review.md +0 -0
  421. /package/docs/{planning → archive/planning}/release-notes-v0.0.10.md +0 -0
  422. /package/docs/{planning → archive/planning}/release-notes-v0.0.11.md +0 -0
  423. /package/docs/{planning → archive/planning}/release-notes-v0.0.12.md +0 -0
  424. /package/docs/{planning → archive/planning}/release-notes-v0.0.13.md +0 -0
  425. /package/docs/{planning → archive/planning}/web-ui-pass-plan.md +0 -0
  426. /package/{docs/investigations → src/serve/assets}/curve-terrain.html +0 -0
  427. /package/{docs/dag → src/serve/assets}/training-inference-map.html +0 -0
@@ -1,1558 +1,8 @@
1
- // Continuous-batching scheduler for `--batch N` serving (phase S2, the engine
2
- // loop). Owns ONE running batch and drives it forward one decode step at a
3
- // time, admitting waiting requests and evicting finished ones between steps —
4
- // iteration-level (continuous) scheduling, not static batching. See
5
- // docs/design/parallel-slots.md and docs/design/batching-v2-plan.md.
6
- //
7
- // The numerically-hard parts are verified primitives:
8
- // - the batched FORWARD (per-row RoPE/mask) is bit-parity with mlx-lm B=N
9
- // across all 4 models (tests/batched-decode-parity);
10
- // - the dynamic-B FULL-attention ops mergeKVRows / filterKVRows match mlx-lm
11
- // BatchKVCache (tests/batched-decode-parity), and the SLIDING-window
12
- // BatchedRotatingCache (merge/filter/decode/make_mask incl. ring-wrap)
13
- // matches mlx-lm BatchRotatingKVCache (tests/batched-rotating).
14
- // This module is the ORCHESTRATION on top: admission, the step loop, per-row
15
- // sampling + token accounting, eviction, and assembling each layer's batched
16
- // cache by type. Gate: tests/batch-scheduler.test.ts (teacher-forced, KL).
17
- //
18
- // Per-layer cache types: a model interleaves full-attention layers (plain
19
- // KVCache, wrapped per step in a BatchedDecodeMaskCache) and sliding-window
20
- // layers (a persistent BatchedRotatingCache that is itself the batched cache +
21
- // mask). Full layers share one leftPad/offset (all rows advance together); the
22
- // rotating caches self-track per-row leftPad/offset as the ring wraps. The
23
- // per-row absolute position stays consistent across both (full: offset-leftPad;
24
- // rot: offsetArr) — see docs/design/parallel-slots.md. Hybrid gated-DeltaNet
25
- // models (Qwen3.5) add "ssm" layers: SSMCache state is plain [B,...] with no
26
- // temporal axis and no padding (rows solo-prefill unpadded, decode feeds one
27
- // real token per row), so merge/filter are B-axis concat/take and the cache
28
- // passes through the step unwrapped (no mask, no per-row RoPE — full layers
29
- // carry positions). Gate: GenerationGateway.willBatch on cache capability.
30
- //
31
- // Engine mechanics (the serial decode loop's hygiene, transplanted —
32
- // batching-v2-plan step 3):
33
- // - PIPELINED decode: each step builds the NEXT step's graph from the
34
- // still-unread sampled-token array (asyncEval), then reads the PREVIOUS
35
- // step's tokens while the new step computes — mlx-lm GenerationBatch._step.
36
- // The pipeline is flushed (read + emit) before a join merges, so admission
37
- // never has to reconcile an in-flight token array with a new row.
38
- // - clearCache every 256 steps (serial's cadence; mlx-lm batched uses 512),
39
- // not every step — per-step clears trashed the buffer pool each token.
40
- // - CHUNKED, INTERLEAVED admission: a joiner prefills prefillChunkSize
41
- // tokens per loop iteration with one batch decode step run in between, so
42
- // running rows stall at most one chunk per joiner (mlx-lm interleaves the
43
- // same way), and the prefill transient stays bounded.
44
- // - Failure containment: one row's onToken throwing evicts THAT row (its
45
- // promise rejects); siblings keep decoding (mlx-lm `remove` semantics). A
46
- // forward/sampling error still drops the whole batch (can't be attributed
47
- // to a row).
48
- //
49
- // Bun-async, NO threads: a single detached driver loop owns the GPU for batched
50
- // mode (an ExclusiveLock keeps the serial fallback off the GPU concurrently).
51
- // When `admissionHeld` reports a waiting serial-lane request, the loop stops
52
- // admitting, finishes the running rows, and releases the lock so the serial
53
- // request runs (mlx-lm's drain_batch) — resumed via kick().
54
- // Joins re-merge the whole batch; the keep-the-running-batch `extend`
55
- // optimization is a later refinement (batching-v2-plan item a).
56
-
57
- import { MlxArray } from "../mlx/array";
58
- import * as ops from "../mlx/ops";
59
- import { clearCache, Dtype } from "../mlx/ffi";
60
- import { flagOn } from "../flags";
61
- import { CompiledDecode } from "../model/compiled-decode";
62
- import type { Gemma4Model } from "../model/gemma4";
63
- import {
64
- KVCache, QuantizedKVCache, RotatingKVCache, RotatingQuantizedKVCache,
65
- isBatchableCache, type BatchableCache, type Cache,
66
- } from "../model/gemma4-base";
67
- import { BatchedDecodeMaskCache, mergeKVRows, extendKVRows, extractKVRow, filterKVRows } from "../model/batched-mask";
68
- import {
69
- BatchedQuantDecodeMaskCache, extendQuantRows, extractQuantRow, filterQuantRows, mergeQuantRows,
70
- type QuantRow,
71
- } from "../model/batched-quant";
72
- import { BatchedRotatingQuantCache } from "../model/batched-rotating-quant";
73
- import type { KvQuantSpec } from "../config";
74
- import { BatchedRotatingCache } from "../model/batched-rotating";
75
- import { cloneKvCaches } from "../kv-store";
76
- import { SSMCache } from "../model/qwen3-delta";
77
- import type { RuntimeModel } from "../model/factory";
78
- import type { GrammarController } from "../grammar";
79
- import { kvBytesAt } from "../fit";
80
- import { toLogprobs } from "../sampler";
81
-
82
- /** Decode-pipeline kill switch (read once at load, like the serial loop's
83
- * MLX_BUN_COMPILED_DECODE): 1 ⇒ read each step's tokens synchronously. */
84
- const NO_PIPELINE = process.env.MLX_BUN_BATCH_NO_PIPELINE === "1";
85
-
86
- /** Per-step phase timing (MLX_BUN_BATCH_STEP_TRACE=1, debug-only): where a
87
- * decode step's wall time goes — graph BUILD+dispatch (host), the pipelined
88
- * READ of the previous step's tokens (GPU wait), row EMIT (onToken/SSE), and
89
- * the GAP between consecutive steps (drive-loop + everything else). Sums
90
- * print via `stepTraceReport()` (the b1 profile experiment calls it). */
91
- const STEP_TRACE = process.env.MLX_BUN_BATCH_STEP_TRACE === "1";
92
- const STEP_T = { t0: 0, lastEnd: 0, build: 0, read: 0, emit: 0, gap: 0, n: 0 };
93
- export function stepTraceReport(): string {
94
- const per = (x: number) => (STEP_T.n ? (x / STEP_T.n).toFixed(3) : "0");
95
- const s = `steps=${STEP_T.n} build=${per(STEP_T.build)}ms read=${per(STEP_T.read)}ms emit=${per(STEP_T.emit)}ms gap=${per(STEP_T.gap)}ms (per step)`;
96
- STEP_T.t0 = STEP_T.lastEnd = STEP_T.build = STEP_T.read = STEP_T.emit = STEP_T.gap = STEP_T.n = 0;
97
- return s;
98
- }
99
-
100
- // NOTE — oMLX-style "adaptive burst decode" (several steps per event-loop
101
- // yield, omlx/engine_core.py _step_burst) was ported here 2026-07-02 and
102
- // REFUTED by measurement: on this runtime it REGRESSED cpm5 B=4 aggregate
103
- // 345→289 tok/s, batch-lane B=1 149→121, and TTFT ~+100 ms (first-token SSE
104
- // flush waits out the burst budget). Their win exists because each Python
105
- // step hand-off ping-pongs the GIL with asyncio/uvicorn (~1 ms/token); Bun's
106
- // setImmediate hop costs microseconds, so bursting here only delays socket
107
- // flushes. Don't re-add without new evidence — the per-yield step below is
108
- // the measured optimum (docs/design/batching-perf-path.md P4 notes).
109
-
110
- /** A token sampler for one row: (logits [1,V], step) → token array [1] on
111
- * device. Greedy is `(l) => ops.argmaxAxis(l, -1)`; richer closures fold in
112
- * temperature / top-p / logits processors + history (built per request from
113
- * its sampler options, exactly like generate()'s sampleStep). Called once per
114
- * step 0..maxTokens-1 for this row (never beyond maxTokens). */
115
- export type RowSampler = (logits1V: MlxArray, step: number) => MlxArray;
116
-
117
- export interface BatchRequest {
118
- promptIds: number[];
119
- maxTokens: number;
120
- eosTokenIds: number[];
121
- sample: RowSampler;
122
- /** Called per emitted (non-EOS) token, in order. Returning `false` halts this
123
- * row (a decoded-text stop sequence fired) — matches generate()'s onToken
124
- * contract. EOS terminates the row WITHOUT an onToken call. Throwing evicts
125
- * THIS row only (its submit promise rejects; siblings continue). May be
126
- * async; keep it cheap — it runs inline in the step loop. */
127
- onToken: (token: number) => void | boolean | Promise<void | boolean>;
128
- /** Client/request lifetime. An aborted pending row is removed before
129
- * admission; a row in prefill or decode is evicted at the next safe
130
- * scheduler boundary. */
131
- signal?: AbortSignal;
132
- /** The row's sampler is PLAIN GREEDY (temperature 0, no curve) with no
133
- * logits processors and no grammar — set by the gateway when true. Lets
134
- * the scheduler take the vectorized sampling fast path (ONE
135
- * log-softmax+argmax over [B,V] instead of B slice/sample/concat graphs)
136
- * when EVERY live row qualifies. Numerically identical per row
137
- * (row-independent ops, same per-row shapes); the per-row closure path
138
- * is the fallback and the MLX_BUN_BATCH_VEC_SAMPLE=0 kill switch. */
139
- plainGreedy?: boolean;
140
- /** Grammar controller for this row (B1: per-row matchers). When set, the
141
- * scheduler drives it: `accept()` after each emitted token (fires the async
142
- * bitmask fill), `await ready()` before the row's next sample (the sample
143
- * closure applies `applyMask`). Termination (grammar satisfied) is an
144
- * additional per-row stop source. The gateway OWNS disposal (finally around
145
- * submit()); the scheduler uses but never owns. Null on the degrade path. */
146
- grammar?: GrammarController;
147
- /** Stable cache boundary from the server's template probe (the serial
148
- * lane's options.snapshotAt): the prompt prefix the NEXT turn's render
149
- * preserves. The scheduler snapshots a trim-free prompt-cache entry at
150
- * min(this, promptIds.length-1) during the solo prefill — the oracle
151
- * invariant (mlx-lm insert_segments) that gives wrapped-ring models a
152
- * reuse path. Absent = still capped at promptIds.length-1. */
153
- snapshotAt?: number;
154
- }
155
-
156
- export interface BatchStats {
157
- promptTokens: number;
158
- generatedTokens: number;
159
- /** Prompt tokens served from the prompt cache (Phase 3.2): a joiner's solo
160
- * prefill starts from the longest usable cached prefix, exactly like the
161
- * serial lane's runGeneration take(). 0 on a cold prefill. */
162
- cachedTokens: number;
163
- finishReason: "stop" | "length";
164
- }
165
-
166
- /** The slice of PromptCache the scheduler drives (structural — the server's
167
- * PromptCache satisfies it). take() on admission (any joiner: a restored
168
- * prefix + suffix prefill is byte-safe whether the row later merges or
169
- * not). put() paths: rows that finish NEVER-MERGED (adopted lone rows,
170
- * Phase 3.2) hand their pristine serial caches over zero-copy; rows that
171
- * finish INSIDE a multi-row batch get their KV EXTRACTED per row into
172
- * fresh serial caches first (mlx-lm server.py:872 extract_cache — the
173
- * cross-request reuse concurrent agents live on). */
174
- export interface RowPromptCache {
175
- take(prompt: number[], ns?: string): {
176
- tokens: number[]; caches: Cache[]; retain?: () => void;
177
- } | null;
178
- put(tokens: number[], caches: Cache[], ns?: string, retain?: () => void): void;
179
- }
180
-
181
- interface Row {
182
- req: BatchRequest;
183
- resolve: (s: BatchStats) => void;
184
- reject: (e: unknown) => void;
185
- current: number; // last emitted token, fed at the next step (pipeline cold)
186
- generated: number; // tokens emitted so far (incl. a terminating EOS)
187
- sampled: number; // sample() calls so far (leads `generated` by 1 in-pipeline)
188
- promptTokens: number;
189
- /** Prompt tokens restored from the prompt cache at admission (stats). */
190
- cachedTokens: number;
191
- /** Generated tokens whose KV actually entered the cache (fed as a step
192
- * input) — serial generate()'s `forwarded` list. promptIds+fed is the
193
- * exact token coverage of the row's caches, the put() entry key.
194
- * PER-ROW EXACT for every row: pushes are gated on the pending
195
- * register's per-slot real/placeholder flags (#pendingReal), so a
196
- * doomed slot's placeholder is never recorded — and if one ever DID
197
- * feed a forward, `fedTainted` flips instead (see #step's read). */
198
- fed: number[];
199
- /** A placeholder slot value fed this row's KV (junk token in the cache):
200
- * the coverage key is no longer derivable — extraction refuses.
201
- * Structurally unreachable today (a doomed row always evicts at the
202
- * emit that follows its placeholder, filtering the slot before the
203
- * next forward; cold steps never contain doomed rows because a flush
204
- * emits every in-flight token) — this flag makes `fed` provably exact
205
- * instead of resting on that timing chain. */
206
- fedTainted: boolean;
207
- /** Ever merged into a multi-row batch (or poisoned by a reject): its KV
208
- * is interleaved in batched buffers — a finish put() must EXTRACT the
209
- * row (#extractRowCaches) rather than adopt the inners. */
210
- merged: boolean;
211
- }
212
-
213
- /** A joiner mid-prefill: `pos` prompt tokens are already in `solo`. Advanced
214
- * one chunk per loop iteration, interleaved with batch decode steps. */
215
- interface PrefillState {
216
- row: Row;
217
- solo: Cache[];
218
- pos: number;
219
- /** Release hook carried by an SSD-restored prompt-cache entry (must run
220
- * only after `solo`/the adopted caches are disposed — see PromptCacheEntry). */
221
- retain?: () => void;
222
- /** Pending boundary-snapshot position: #prefillChunk splits its chunking
223
- * exactly here, clones + put()s the trim-free prefix entry, then nulls
224
- * it. null = no snapshot for this row (short prompt / cached past it). */
225
- snapAt: number | null;
226
- }
227
-
228
- /** Held while the batch is active; the serial fallback acquires the same lock. */
229
- export interface ExclusiveLock {
230
- acquire(): Promise<() => void>;
231
- }
232
-
233
- export interface BatchSchedulerOptions {
234
- /** Max rows in the running batch (mlx-lm `--decode-concurrency`). */
235
- maxBatch: number;
236
- lock?: ExclusiveLock;
237
- /** Drain signal: while true, no NEW rows are admitted (running rows finish,
238
- * the lock is released) so a waiting serial-lane request can run — mlx-lm's
239
- * drain_batch. Pair with kick() when it flips back to false. */
240
- admissionHeld?: () => boolean;
241
- /** Joiner prefill chunk length (default 2048, the serial loop's constant). */
242
- prefillChunkSize?: number;
243
- /** Aggregate KV-byte budget across all running rows (batching-perf-path
244
- * P3 admission, via `--kv-budget`). A joiner whose PROJECTED KV
245
- * (kvBytesAt(config, prompt + maxTokens), sliding window already capped)
246
- * would push the batch's projected total over this ceiling WAITS in the
247
- * queue (FIFO, head-of-line — no starvation, no reorder) until rows
248
- * evict. A request that can NEVER fit (over budget alone, empty batch)
249
- * is rejected instead of deadlocking. Unset = unlimited (v1 behavior). */
250
- kvBudgetBytes?: number;
251
- /** Per-layer mixed-precision KV scheme (kv_config.json entries) — Phase
252
- * 3.1 batched quantized KV. Server-wide constant (one scheme per server
253
- * lifetime), so it lives here at construction, not on BatchRequest.
254
- * P1 scope: only layers whose cache is a plain full-attention KVCache
255
- * convert; the GATEWAY admits batching only when every configured
256
- * layerIdx is such a layer (rotating/uniform stay serial). Each joiner's
257
- * solo prefill converts at the SAME chunk boundaries as the serial
258
- * maybeQuantizeKv, so a row's quantized bytes are bit-exact vs serial
259
- * `--kv-quant config` by construction (the L2-oracle composition rule). */
260
- kvConfig?: KvQuantSpec[];
261
- /** Prompt-cache hook (Phase 3.2): admission take()s the longest usable
262
- * prefix into the joiner's solo caches (suffix-only prefill — the
263
- * multi-turn chat TTFT path); rows that finish never-merged put() their
264
- * caches back. Adapter requests never reach the batch lane, so the
265
- * namespace is always "" here. Runs under the gateway mutex domain, so
266
- * take/put never race the serial lane's use of the same cache. */
267
- promptCache?: RowPromptCache;
268
- }
269
-
270
- type LayerInner =
271
- | KVCache
272
- | QuantizedKVCache
273
- | BatchedRotatingCache
274
- | BatchedRotatingQuantCache // rot layer under a kv_config scheme (milestone 2)
275
- | RotatingKVCache // adopted lone-row state only (see #mergeJoiner adopt)
276
- | RotatingQuantizedKVCache // adopted lone-row state, quantized rot layer
277
- | SSMCache
278
- | BatchableCache;
279
- type Row1 = { keys: MlxArray; values: MlxArray };
280
-
281
- export class BatchScheduler {
282
- #running: Row[] = [];
283
- #inners: LayerInner[] | null = null; // per-layer batched KV; null when empty
284
- #fullLeftPad: number[] = []; // per-row padding for FULL layers (rot self-tracks)
285
- #pending: Row[] = [];
286
- #prefill: PrefillState | null = null; // the (single) joiner mid-prefill
287
- /** Sampled-but-unread token array [B], aligned with #running — the decode
288
- * pipeline register. Filtered/disposed alongside the batched KV. */
289
- #pendingToks: MlxArray | null = null;
290
- /** Per-slot flags for #pendingToks: true = a REAL sampled token of that
291
- * row's stream; false = a doomed/terminated row's placeholder (fed-token
292
- * accounting must skip it — see Row.fed/fedTainted). Filtered/cleared in
293
- * lockstep with the register. */
294
- #pendingReal: boolean[] | null = null;
295
- #steps = 0; // decode-step counter (clearCache cadence)
296
- #lastHop = 0; // last macrotask yield (B=1 fast-path rate limiting)
297
- #looping = false;
298
- #wake: (() => void) | null = null;
299
- readonly #maxBatch: number;
300
- readonly #lock: ExclusiveLock | undefined;
301
- readonly #admissionHeld: (() => boolean) | undefined;
302
- readonly #prefillChunkSize: number;
303
- readonly #kvBudgetBytes: number | undefined;
304
- readonly #promptCache: RowPromptCache | undefined;
305
- /** Retain hook of the currently-ADOPTED row's cache entry (at most one:
306
- * only a lone adopted row holds un-copied entry caches). Runs after the
307
- * adopted caches are disposed, or transfers back on put(). */
308
- #adoptedRetain: (() => void) | null = null;
309
- readonly #kinds: ("full" | "rot" | "ssm" | "owned-batch")[];
310
- readonly #rotMaxSize: number[]; // per-layer sliding window (rot layers only)
311
- readonly #compressedProjectors: Array<(tokens: number) => number> | null;
312
- readonly #batchCacheMaxTokens: number | null;
313
- /** layerIdx → mixed-precision spec (Phase 3.1); null = bf16 batch (v1). */
314
- readonly #kvByLayer: Map<number, KvQuantSpec> | null;
315
- /** Compiled decode runner for the B=1 serial-class case (Phase 3.2) —
316
- * same eligibility gate as generate.ts (gemma dense, kill switch
317
- * MLX_BUN_COMPILED_DECODE); adapters never reach the batch lane. Set
318
- * to null permanently on a failed step (serial disables per
319
- * generation; the scheduler is one long-lived "generation"). */
320
- #compiled: CompiledDecode | null;
321
-
322
- constructor(private readonly model: RuntimeModel, opts: BatchSchedulerOptions) {
323
- this.#maxBatch = Math.max(1, Math.floor(opts.maxBatch));
324
- this.#lock = opts.lock;
325
- this.#admissionHeld = opts.admissionHeld;
326
- this.#prefillChunkSize = Math.max(1, Math.floor(opts.prefillChunkSize ?? 2048));
327
- this.#kvBudgetBytes = opts.kvBudgetBytes;
328
- this.#promptCache = opts.promptCache;
329
- const proto = model.makeCache(); // fresh caches hold no buffers
330
- this.#kinds = proto.map((c) =>
331
- isBatchableCache(c)
332
- ? "owned-batch"
333
- : c instanceof RotatingKVCache
334
- ? "rot"
335
- : c instanceof SSMCache
336
- ? "ssm"
337
- : "full",
338
- );
339
- this.#compressedProjectors = proto.every(isBatchableCache)
340
- ? proto.map((cache) => (tokens: number) => cache.projectedBytes(tokens))
341
- : null;
342
- this.#batchCacheMaxTokens = proto.every(isBatchableCache)
343
- ? Math.min(...proto.map((cache) => cache.maxTokens ?? Number.MAX_SAFE_INTEGER))
344
- : null;
345
- this.#kvByLayer = opts.kvConfig?.length
346
- ? new Map(opts.kvConfig.map((e) => [e.layerIdx, e]))
347
- : null;
348
- this.#rotMaxSize = proto.map((c) => (c instanceof RotatingKVCache ? c.maxSize : 0));
349
- for (const c of proto) c.dispose();
350
- this.#compiled =
351
- flagOn("MLX_BUN_COMPILED_DECODE", true) &&
352
- model.config.modelType.startsWith("gemma4") &&
353
- !model.config.text.enableMoeBlock
354
- ? CompiledDecode.for(model as Gemma4Model)
355
- : null;
356
- }
357
-
358
- get activeRows(): number {
359
- return this.#running.length;
360
- }
361
-
362
- get pendingRows(): number {
363
- return this.#pending.length + (this.#prefill ? 1 : 0);
364
- }
365
-
366
- /** Projected KV bytes of one row at its worst case (full prompt + full
367
- * completion; the sliding-window term is window-capped by kvBytesAt). */
368
- #rowKvBytes(row: Row): number {
369
- const tokens = row.promptTokens + row.req.maxTokens;
370
- return this.#compressedProjectors
371
- ? this.#compressedProjectors.reduce((sum, project) => sum + project(tokens), 0)
372
- : kvBytesAt(this.model.config, tokens);
373
- }
374
-
375
- async #forwardHidden(ids: MlxArray, cache: Cache[]): Promise<MlxArray> {
376
- const asyncModel = this.model as RuntimeModel & {
377
- forwardHiddenAsync?: (ids: MlxArray, cache: Cache[]) => Promise<MlxArray>;
378
- };
379
- return typeof asyncModel.forwardHiddenAsync === "function"
380
- ? await asyncModel.forwardHiddenAsync(ids, cache)
381
- : this.model.forwardHidden(ids, cache);
382
- }
383
-
384
- /** Projected aggregate KV of everything admitted (running + mid-prefill). */
385
- get projectedKvBytes(): number {
386
- let total = this.#running.reduce((a, r) => a + this.#rowKvBytes(r), 0);
387
- if (this.#prefill) total += this.#rowKvBytes(this.#prefill.row);
388
- return total;
389
- }
390
-
391
- get kvBudgetBytes(): number | undefined {
392
- return this.#kvBudgetBytes;
393
- }
394
-
395
- /** KV-budget admission for the queue head. True = admit now. A candidate
396
- * that cannot fit even alone is rejected here (never deadlocks the
397
- * queue); one that fits alone but not alongside the current batch waits. */
398
- #kvAdmits(candidate: Row): boolean {
399
- const requestedTokens = candidate.promptTokens + candidate.req.maxTokens;
400
- if (
401
- this.#batchCacheMaxTokens !== null &&
402
- requestedTokens > this.#batchCacheMaxTokens
403
- ) {
404
- this.#pending.shift();
405
- candidate.reject(new RangeError(
406
- `context limit: prompt ${candidate.promptTokens} + max_tokens ` +
407
- `${candidate.req.maxTokens} exceeds ${this.#batchCacheMaxTokens}`,
408
- ));
409
- return false;
410
- }
411
- if (this.#kvBudgetBytes === undefined) return true;
412
- const need = this.#rowKvBytes(candidate);
413
- if (need > this.#kvBudgetBytes && this.#running.length === 0 && !this.#prefill) {
414
- this.#pending.shift();
415
- candidate.reject(
416
- new Error(
417
- `kv budget: request needs ~${(need / 1e9).toFixed(2)} GB KV ` +
418
- `(prompt ${candidate.promptTokens} + max_tokens ${candidate.req.maxTokens}), ` +
419
- `over --kv-budget ${(this.#kvBudgetBytes / 1e9).toFixed(2)} GB — ` +
420
- `lower max_tokens or raise the budget`,
421
- ),
422
- );
423
- return false;
424
- }
425
- return this.projectedKvBytes + need <= this.#kvBudgetBytes;
426
- }
427
-
428
- /** Submit a request; resolves when its row finishes (EOS, stop, or length). */
429
- submit(req: BatchRequest): Promise<BatchStats> {
430
- if (req.signal?.aborted) return Promise.reject(req.signal.reason);
431
- return new Promise<BatchStats>((resolve, reject) => {
432
- let abortListener: (() => void) | null = null;
433
- const cleanup = () => {
434
- if (abortListener) req.signal?.removeEventListener("abort", abortListener);
435
- abortListener = null;
436
- };
437
- this.#pending.push({
438
- req,
439
- resolve: (stats) => { cleanup(); resolve(stats); },
440
- reject: (error) => { cleanup(); reject(error); },
441
- current: 0, generated: 0, sampled: 0, promptTokens: req.promptIds.length,
442
- cachedTokens: 0, fed: [], fedTainted: false, merged: false,
443
- });
444
- if (req.signal) {
445
- abortListener = () => this.kick();
446
- req.signal.addEventListener("abort", abortListener, { once: true });
447
- }
448
- this.#ensureLoop();
449
- });
450
- }
451
-
452
- /** Wake the driver loop (e.g. after admissionHeld flips back to false). */
453
- kick(): void {
454
- this.#ensureLoop();
455
- }
456
-
457
- #ensureLoop(): void {
458
- if (this.#wake) { this.#wake(); return; }
459
- if (this.#looping) return;
460
- this.#looping = true;
461
- void this.#drive();
462
- }
463
-
464
- async #drive(): Promise<void> {
465
- let release: (() => void) | null = null;
466
- try {
467
- while (true) {
468
- for (let i = this.#pending.length - 1; i >= 0; i--) {
469
- const row = this.#pending[i]!;
470
- if (!row.req.signal?.aborted) continue;
471
- this.#pending.splice(i, 1);
472
- row.reject(row.req.signal.reason);
473
- }
474
- const held = this.#admissionHeld?.() === true;
475
- // Work = running rows, an in-flight prefill (finish it even under
476
- // drain — the row is already half-admitted), or admissible pending.
477
- const hasWork =
478
- this.#running.length > 0 ||
479
- this.#prefill !== null ||
480
- (!held && this.#pending.length > 0);
481
- if (!hasWork) {
482
- if (release) { release(); release = null; } // let the serial lane in
483
- await new Promise<void>((r) => { this.#wake = r; });
484
- this.#wake = null;
485
- continue;
486
- }
487
- if (!release && this.#lock) release = await this.#lock.acquire();
488
-
489
- // Start at most one joiner prefill (drain-aware); it advances one
490
- // chunk per iteration, interleaved with the decode step below.
491
- if (!this.#prefill && !held &&
492
- this.#pending.length > 0 && this.#running.length < this.#maxBatch &&
493
- this.#kvAdmits(this.#pending[0]!)) {
494
- const row = this.#pending.shift()!;
495
- // Prompt-cache reuse (Phase 3.2): start the solo prefill from the
496
- // longest usable cached prefix — the serial lane's take(), same
497
- // semantics (entry ownership transfers; put back or dispose).
498
- const hit = this.#promptCache?.take(row.req.promptIds) ?? null;
499
- if (hit) row.cachedTokens = hit.tokens.length;
500
- // Boundary snapshot (the serial lane's A1, ported): a trim-free
501
- // strict-prefix entry at min(stableLen, len-1) for every
502
- // substantial cold prefill — same gates as runGeneration
503
- // (>= 256 tokens, extends past the cached prefix). Without this
504
- // the batch lane's ONLY entry was the untrimmable [prompt+gen]
505
- // finish-time put — a total ctx-repeat miss once rings wrap
506
- // (12B batched 84 s vs serial 0.4 s, 2026-07-06).
507
- const len = row.req.promptIds.length;
508
- const boundary = Math.min(row.req.snapshotAt ?? len, len - 1);
509
- const snapAt =
510
- this.#promptCache && boundary >= 256 && boundary > (hit?.tokens.length ?? 0)
511
- ? boundary
512
- : null;
513
- this.#prefill = hit
514
- ? { row, solo: hit.caches, pos: hit.tokens.length, retain: hit.retain, snapAt }
515
- : { row, solo: this.model.makeCache(), pos: 0, snapAt };
516
- }
517
-
518
- if (this.#prefill) {
519
- const p = this.#prefill;
520
- try {
521
- if (await this.#prefillChunk(p)) this.#prefill = null;
522
- } catch (e) {
523
- // Prefill/admission failure is per-row: reject the joiner, keep
524
- // the running batch.
525
- this.#prefill = null;
526
- for (const c of p.solo) c.dispose();
527
- p.retain?.();
528
- p.row.reject(e);
529
- }
530
- }
531
-
532
- // Burst admission: when a joiner just COMPLETED and more batchable
533
- // requests are queued, admit them before the next decode step (the
534
- // admit-all-then-step grouping the mlx-lm goldens gate). Long prompts
535
- // still interleave: a mid-prefill joiner leaves #prefill non-null, so
536
- // the loop falls through to run a decode step between chunks.
537
- if (this.#prefill === null && !held &&
538
- this.#pending.length > 0 && this.#running.length < this.#maxBatch &&
539
- (this.#kvBudgetBytes === undefined ||
540
- this.projectedKvBytes + this.#rowKvBytes(this.#pending[0]!) <= this.#kvBudgetBytes))
541
- continue;
542
-
543
- if (this.#running.length > 0) {
544
- try {
545
- await this.#step();
546
- } catch (e) {
547
- // A forward/sampling error can't be attributed to one row — drop
548
- // the batch. (A row's onToken error is contained in #emitRows.)
549
- // dropOnly: mid-step caches may be inconsistent — never put().
550
- for (const row of this.#running) row.reject(e);
551
- this.#applyFilter([], true);
552
- }
553
- }
554
- // The per-step macrotask hop exists so joiners/admin interleave at
555
- // token granularity. With ONE row and an empty queue there is
556
- // nobody to interleave — rate-limit the hop to the serial loop's
557
- // 25 ms cadence (identical responsiveness budget; generate()'s
558
- // hoppingOnToken does exactly this). This was the B=1 drive-loop
559
- // tax: one setImmediate round-trip per token ≈ the 0.4–0.8% gap
560
- // vs pure serial (paired-measured 2026-07-05).
561
- if (
562
- this.#running.length === 1 && this.#pending.length === 0 &&
563
- !this.#prefill && this.#admissionHeld?.() !== true &&
564
- performance.now() - this.#lastHop < 25
565
- )
566
- continue;
567
- this.#lastHop = performance.now();
568
- await new Promise<void>((r) => setImmediate(r));
569
- }
570
- } finally {
571
- if (release) release();
572
- this.#looping = false;
573
- }
574
- }
575
-
576
- /** Per-layer mixed-precision conversion of a joiner's SOLO caches — the
577
- * scheduler-side mirror of the serial maybeQuantizeKv (generate.ts): same
578
- * per-layer map, same skip rules (empty cache, already quantized), same
579
- * streaming discipline (evalAll the converted layer's state so the bf16
580
- * source frees before the next layer converts). Called at every prefill
581
- * chunk boundary AND once before merge, exactly where the serial loop
582
- * calls maybeQuantizeKv — that placement is what makes a row's quantized
583
- * bytes bit-exact vs serial `--kv-quant config`. P1: plain KVCache
584
- * layers only (the gateway guarantees the config maps only to those). */
585
- #quantizeSolo(solo: Cache[]): void {
586
- if (!this.#kvByLayer) return;
587
- for (let i = 0; i < solo.length; i++) {
588
- const e = this.#kvByLayer.get(i);
589
- const c = solo[i]!;
590
- if (!e || c.offset === 0) continue;
591
- // Milestone 2: rotating layers convert too — RotatingKVCache.
592
- // toQuantized → RotatingQuantizedKVCache, exactly maybeQuantizeKv's
593
- // dispatch (generate.ts). Converted caches don't match either class
594
- // (the four cache classes are siblings), so re-conversion never fires.
595
- if (!(c instanceof KVCache || c instanceof RotatingKVCache)) continue;
596
- const q = c.toQuantized(e.groupSize, e.bits);
597
- solo[i] = q;
598
- ops.evalAll(q.state());
599
- clearCache();
600
- }
601
- }
602
-
603
- /** Advance a joiner's solo prefill by one chunk. Non-final chunks forward +
604
- * eval the cache and return false (the caller interleaves a decode step).
605
- * The final chunk samples token 0, emits it, and — if the row survives —
606
- * merges it into the running batch; returns true (admission complete). */
607
- async #prefillChunk(p: PrefillState): Promise<boolean> {
608
- p.row.req.signal?.throwIfAborted();
609
- const prompt = p.row.req.promptIds;
610
- // Boundary-snapshot chunking (mirrors generate.ts's snapshotAt split):
611
- // chunk edges land EXACTLY on snapAt so the clone is taken while the
612
- // caches hold precisely that prefix — slicing tokens against caches at
613
- // any other offset would store corrupt entries. The extra split only
614
- // changes chunk shapes for opted-in rows: the same numerics class as a
615
- // prompt-cache-hit continuation prefill (GEMV/GEMM reduction order).
616
- if (p.snapAt !== null && p.pos < p.snapAt) {
617
- const end = Math.min(p.pos + this.#prefillChunkSize, p.snapAt);
618
- const chunk = prompt.slice(p.pos, end);
619
- const ids = ops.fromInt32(chunk, [1, chunk.length]);
620
- const h = await this.#forwardHidden(ids, p.solo);
621
- ids.dispose();
622
- h.dispose();
623
- ops.evalAll(p.solo.flatMap((c) => c.state()));
624
- this.#quantizeSolo(p.solo); // serial converts at every chunk boundary
625
- clearCache();
626
- p.pos = end;
627
- if (p.pos === p.snapAt) {
628
- try {
629
- this.#promptCache!.put(prompt.slice(0, p.snapAt), cloneKvCaches(p.solo));
630
- } catch (e) {
631
- console.warn(`batch-lane boundary snapshot skipped: ${(e as Error).message}`);
632
- }
633
- p.snapAt = null;
634
- }
635
- return false;
636
- }
637
- // Oracle prefill convention (see generate.ts prefill): drain only to
638
- // len-1 in chunks, then step-0 logits from an L=1 forward of the LAST
639
- // prompt token — mirrors mlx-lm's batched engine (insert_segments'
640
- // forced final 1-token segment + GenerationBatch._step,
641
- // generate.py:1645/1327) and keeps this lane bit-exact with the serial
642
- // lane (unified == --batch 1 == oracle). The L=1 step runs on the solo
643
- // caches BEFORE merge, so per-row serial equivalence is by construction.
644
- // MLX_BUN_PREFILL_TAIL_SPLIT=0 restores the old convention.
645
- const tailSplit = flagOn("MLX_BUN_PREFILL_TAIL_SPLIT", true);
646
- if ((tailSplit ? prompt.length - p.pos - 1 : prompt.length - p.pos) >
647
- this.#prefillChunkSize) {
648
- const chunk = prompt.slice(p.pos, p.pos + this.#prefillChunkSize);
649
- const ids = ops.fromInt32(chunk, [1, chunk.length]);
650
- const h = await this.#forwardHidden(ids, p.solo);
651
- ids.dispose();
652
- h.dispose(); // logits never computed for non-final chunks
653
- ops.evalAll(p.solo.flatMap((c) => c.state()));
654
- this.#quantizeSolo(p.solo); // serial converts at every chunk boundary
655
- clearCache(); // serial prefill's per-chunk clear (generate.ts)
656
- p.pos += this.#prefillChunkSize;
657
- return false;
658
- }
659
-
660
- // Tail split: forward the remaining head [p.pos, len-1) as a plain chunk
661
- // (serial chunk-boundary semantics: eval + quantize + clear), leaving
662
- // exactly one token for the L=1 step-0 forward below. Head + step share
663
- // this tick — same chunk-tick count as before, plus one L=1 dispatch
664
- // (~a single decode step) in the admission tick's no-yield window.
665
- if (tailSplit && p.pos < prompt.length - 1) {
666
- const head = prompt.slice(p.pos, prompt.length - 1);
667
- const ids = ops.fromInt32(head, [1, head.length]);
668
- const h = await this.#forwardHidden(ids, p.solo);
669
- ids.dispose();
670
- h.dispose();
671
- ops.evalAll(p.solo.flatMap((c) => c.state()));
672
- this.#quantizeSolo(p.solo);
673
- clearCache();
674
- p.pos = prompt.length - 1;
675
- }
676
-
677
- // Final chunk: slice the LAST hidden position BEFORE the lm_head — running
678
- // logitsFromHidden on the whole [1,Lp,H] hidden materializes a [1,Lp,V]
679
- // transient (~4.3 GB bf16 at Gemma V=262k, 8k prompt). Same reorder as the
680
- // serial path (generate.ts prefill). Under tailSplit this chunk is exactly
681
- // the last prompt token (L=1).
682
- const chunk = prompt.slice(p.pos);
683
- const ids = ops.fromInt32(chunk, [1, chunk.length]);
684
- const h = await this.#forwardHidden(ids, p.solo);
685
- ids.dispose();
686
- const [, Lc, H] = h.shape as [number, number, number];
687
- const hLast = h.slice([0, Lc - 1, 0], [1, Lc, H]);
688
- h.dispose();
689
- const lg = this.model.logitsFromHidden(hLast); // [1,1,V]
690
- hLast.dispose();
691
- const V = lg.shape[2]!;
692
- const last2 = ops.reshape(lg, [1, V]);
693
- lg.dispose();
694
- const tok = this.#readToken(p.row.req.sample(last2, 0));
695
- last2.dispose();
696
- p.row.sampled = 1;
697
- p.row.generated = 1;
698
- clearCache(); // drop the prefill transients (serial's token-0 clear)
699
-
700
- // Grammar (B1): the mask0 was applied inside the sample closure (the
701
- // controller is primed at compile). After reading token 0, advance the
702
- // matcher — fires the async fill for the NEXT step, overlapping the merge
703
- // below. If the grammar is already satisfied at token 0 (a 1-token grammar,
704
- // e.g. guided_choice landing on a single-token option), emit + finish("stop")
705
- // WITHOUT merging into the batch — the row never joins #running.
706
- if (p.row.req.grammar) {
707
- p.row.req.grammar.accept(tok);
708
- if (p.row.req.grammar.isTerminated) {
709
- const stop = await this.#emit(p.row, tok);
710
- // Token 0 was sampled but never fed — the caches cover exactly the
711
- // prompt, a clean prompt-only entry (put-or-dispose).
712
- this.#putOrDispose(p.solo, p.row.req.promptIds, p.retain);
713
- this.#finish(p.row, stop === "continue" ? "stop" : stop);
714
- return true;
715
- }
716
- }
717
-
718
- const stop = await this.#emit(p.row, tok);
719
- if (stop !== "continue") {
720
- this.#putOrDispose(p.solo, p.row.req.promptIds, p.retain);
721
- this.#finish(p.row, stop);
722
- return true;
723
- }
724
- // Default tail-split path: the caches were already converted at the head
725
- // boundary above (oracle composition: prefill ids[:-1] → convert → L=1
726
- // step-0), so this call is an idempotent no-op (converted caches match
727
- // neither serial class). It is LOAD-BEARING only under
728
- // MLX_BUN_PREFILL_TAIL_SPLIT=0 — the old serial order: token 0 sampled
729
- // from the unconverted final-chunk logits, THEN the caches convert
730
- // (before decode step 1 == before the merge).
731
- this.#quantizeSolo(p.solo);
732
- await this.#mergeJoiner(p);
733
- return true;
734
- }
735
-
736
- /** Merge a fully-prefilled joiner with the running batch, layer by layer
737
- * (re-merge; `extend` is the later refinement). Flushes the decode pipeline
738
- * first so the row set is settled and the next step starts cold. */
739
- async #mergeJoiner(p: PrefillState): Promise<void> {
740
- await this.#flushPipeline();
741
-
742
- // ADOPT, don't copy (unified-engine plan Phase 3.2): a row joining an
743
- // EMPTY batch keeps its solo caches as the batch inners — a pointer
744
- // handoff, zero bytes moved (the old path ran the full merge machinery
745
- // to produce a byte-identical [1,...] copy). The copy now happens only
746
- // when a SECOND row joins and a genuinely new layout must exist. The
747
- // prize beyond the saved copy: the lone row's caches stay SERIAL-CLASS
748
- // (KVCache / RotatingKVCache / QuantizedKVCache), so the B=1 step is
749
- // literally the serial graph, and compiled decode + prompt-cache
750
- // take/put become possible for it. The rot branch below knows how to
751
- // treat an adopted RotatingKVCache as the merge's first row.
752
- if (!this.#inners) {
753
- this.#inners = p.solo as LayerInner[];
754
- this.#fullLeftPad = [0];
755
- this.#adoptedRetain = p.retain ?? null;
756
- this.#running.push(p.row);
757
- return;
758
- }
759
-
760
- const prev = this.#inners;
761
- const prevPad = this.#fullLeftPad;
762
- const B = this.#running.length;
763
- const newInners: LayerInner[] = [];
764
- let newFullPad = this.#fullLeftPad;
765
- for (let layer = 0; layer < this.#kinds.length; layer++) {
766
- if (this.#kinds[layer] === "owned-batch") {
767
- const solo = p.solo[layer]!;
768
- if (!isBatchableCache(solo))
769
- throw new Error(`batch-capable layer ${layer} lost its cache capability`);
770
- const merged = solo.makeEmptyBatch();
771
- const previous = prev?.[layer];
772
- merged.mergeRows(previous ? [previous, solo] : [solo]);
773
- newFullPad = [...merged.leftPad];
774
- newInners.push(merged);
775
- continue;
776
- }
777
- if (this.#kinds[layer] === "ssm") {
778
- // No temporal axis, no left-pad: B-axis concat of the state slots.
779
- // mergeRows steals the solo arrays when the batch starts cold, so the
780
- // unconditional p.solo dispose below stays safe either way.
781
- newInners.push(SSMCache.mergeRows(
782
- (prev?.[layer] as SSMCache | undefined) ?? null,
783
- p.solo[layer] as SSMCache,
784
- ));
785
- continue;
786
- }
787
- if (p.solo[layer] instanceof QuantizedKVCache) {
788
- // Phase 3.1 — quantized full layer: same merge/extend shapes as the
789
- // bf16 branch below, over (packed, scales, biases) triples. The solo
790
- // row was converted by #quantizeSolo with the serial ops, so its
791
- // bytes already bit-match serial `--kv-quant config`; this branch
792
- // only re-arranges rows along the batch axis.
793
- const qSolo = p.solo[layer] as QuantizedKVCache;
794
- const [qk, qv] = qSolo.temporalView();
795
- const qRow = { keys: qk, values: qv };
796
- const dispose3 = (t: { packed: MlxArray; scales: MlxArray; biases: MlxArray }) => {
797
- t.packed.dispose(); t.scales.dispose(); t.biases.dispose();
798
- };
799
- const prevQ = prev?.[layer] as QuantizedKVCache | undefined;
800
- if (prevQ && process.env.MLX_BUN_BATCH_EXTEND !== "0") {
801
- const [k0, v0] = prevQ.temporalView();
802
- const ext = extendQuantRows(k0, v0, prevPad, qRow);
803
- dispose3(k0); dispose3(v0);
804
- newFullPad = ext.leftPad;
805
- const c = new QuantizedKVCache(qSolo.groupSize, qSolo.bits);
806
- c.restoreState(ext.keys, ext.values, ext.width);
807
- newInners.push(c);
808
- } else {
809
- const rows: { keys: typeof qk; values: typeof qv }[] = [];
810
- if (prevQ) {
811
- const [k0, v0] = prevQ.temporalView(); // [B,H,off,*]
812
- const S = k0.packed.shape[2]!;
813
- for (let b = 0; b < B; b++) {
814
- const pad = prevPad[b]!;
815
- const cutRow = (t: typeof k0): typeof k0 => ({
816
- packed: t.packed.slice([b, 0, pad, 0], [b + 1, t.packed.shape[1]!, S, t.packed.shape[3]!]),
817
- scales: t.scales.slice([b, 0, pad, 0], [b + 1, t.scales.shape[1]!, S, t.scales.shape[3]!]),
818
- biases: t.biases.slice([b, 0, pad, 0], [b + 1, t.biases.shape[1]!, S, t.biases.shape[3]!]),
819
- });
820
- rows.push({ keys: cutRow(k0), values: cutRow(v0) });
821
- }
822
- dispose3(k0); dispose3(v0);
823
- }
824
- rows.push(qRow);
825
- const merged = mergeQuantRows(rows);
826
- newFullPad = merged.leftPad;
827
- const c = new QuantizedKVCache(qSolo.groupSize, qSolo.bits);
828
- c.restoreState(merged.keys, merged.values, merged.width);
829
- newInners.push(c);
830
- for (const r of rows) { dispose3(r.keys); dispose3(r.values); }
831
- }
832
- // qRow views are disposed via the rows loop above or here for extend
833
- if (prevQ && process.env.MLX_BUN_BATCH_EXTEND !== "0") { dispose3(qRow.keys); dispose3(qRow.values); }
834
- else if (!prevQ) { /* disposed in the rows loop */ }
835
- continue;
836
- }
837
- // NOTE: the quantized-rotating branch below never uses this layer's
838
- // bf16 temporalView — calling it before the branch leaked the pair
839
- // (six arrays per second-row join on quantized rotating layers,
840
- // introduced in 859572d; 2026-07-07 review fix). The view is taken
841
- // AFTER the branch, on the paths that actually consume it.
842
- const soloC = p.solo[layer] as KVCache | RotatingKVCache;
843
- if (this.#kinds[layer] === "rot" && p.solo[layer] instanceof RotatingQuantizedKVCache) {
844
- // Milestone 2 — QUANTIZED rotating layer: the solo row converted at
845
- // the serial boundaries (#quantizeSolo), so its ring bytes are the
846
- // serial oracle's; this branch re-arranges temporal triples across
847
- // the batch axis (the bf16 rot merge over triples).
848
- const qSolo = p.solo[layer] as RotatingQuantizedKVCache;
849
- const [qk, qv] = qSolo.temporalView();
850
- const rows: QuantRow[] = [];
851
- const offsets: number[] = [];
852
- const dispose3 = (t: QuantRow) => {
853
- t.keys.packed.dispose(); t.keys.scales.dispose(); t.keys.biases.dispose();
854
- t.values.packed.dispose(); t.values.scales.dispose(); t.values.biases.dispose();
855
- };
856
- const prevC = prev?.[layer];
857
- if (prevC instanceof BatchedRotatingQuantCache) {
858
- const [k0, v0] = prevC.temporalView(); // [B,H,valid,*] triples, temporal
859
- const valid = k0.packed.shape[2]!;
860
- for (let b = 0; b < B; b++) {
861
- const pad = Math.max(0, prevC.leftPad[b]!);
862
- const cutRow = (t: typeof k0): typeof k0 => ({
863
- packed: t.packed.slice([b, 0, pad, 0], [b + 1, t.packed.shape[1]!, valid, t.packed.shape[3]!]),
864
- scales: t.scales.slice([b, 0, pad, 0], [b + 1, t.scales.shape[1]!, valid, t.scales.shape[3]!]),
865
- biases: t.biases.slice([b, 0, pad, 0], [b + 1, t.biases.shape[1]!, valid, t.biases.shape[3]!]),
866
- });
867
- rows.push({ keys: cutRow(k0), values: cutRow(v0) });
868
- offsets.push(prevC.offsetArr[b]!);
869
- }
870
- for (const t of [k0, v0]) { t.packed.dispose(); t.scales.dispose(); t.biases.dispose(); }
871
- } else if (prevC instanceof RotatingQuantizedKVCache) {
872
- // Adopted lone row (Phase 3.2 adopt): its chronological triples
873
- // are the merge's first row, pad 0 by definition.
874
- const [k0, v0] = prevC.temporalView();
875
- rows.push({ keys: k0, values: v0 });
876
- offsets.push(prevC.offset);
877
- }
878
- rows.push({ keys: qk, values: qv });
879
- offsets.push(qSolo.offset);
880
- newInners.push(BatchedRotatingQuantCache.merge(
881
- rows, offsets, this.#rotMaxSize[layer]!, qSolo.groupSize, qSolo.bits,
882
- ));
883
- for (const r of rows) dispose3(r);
884
- continue;
885
- }
886
- const [sk, sv] = soloC.temporalView();
887
- const newRow: Row1 = { keys: sk, values: sv };
888
- if (this.#kinds[layer] === "rot") {
889
- const rows: Row1[] = [];
890
- const offsets: number[] = [];
891
- const prevC = prev?.[layer];
892
- if (prevC instanceof RotatingKVCache) {
893
- // Adopted lone row (Phase 3.2): a plain serial rotating cache —
894
- // its chronological view is the merge's first row, same as a
895
- // fresh solo (pad 0 by definition).
896
- const [k0, v0] = prevC.temporalView();
897
- rows.push({ keys: k0, values: v0 });
898
- offsets.push(prevC.offset);
899
- }
900
- const prevRot = prevC instanceof BatchedRotatingCache ? prevC : undefined;
901
- if (prevRot) {
902
- const [k0, v0] = prevRot.temporalView(); // [B,H,valid,D]
903
- const [, H, valid, D] = k0.shape as [number, number, number, number];
904
- const vD = v0.shape[3]!;
905
- for (let b = 0; b < B; b++) {
906
- const pad = Math.max(0, prevRot.leftPad[b]!);
907
- rows.push({
908
- keys: k0.slice([b, 0, pad, 0], [b + 1, H, valid, D]),
909
- values: v0.slice([b, 0, pad, 0], [b + 1, H, valid, vD]),
910
- });
911
- offsets.push(prevRot.offsetArr[b]!);
912
- }
913
- k0.dispose(); v0.dispose();
914
- }
915
- rows.push(newRow);
916
- offsets.push(soloC.offset);
917
- newInners.push(BatchedRotatingCache.merge(rows, offsets, this.#rotMaxSize[layer]!));
918
- for (const r of rows) { r.keys.dispose(); r.values.dispose(); }
919
- } else {
920
- const prevFull = prev?.[layer] as KVCache | undefined;
921
- if (prevFull && process.env.MLX_BUN_BATCH_EXTEND !== "0") {
922
- // extend-join (mlx-lm BatchKVCache.extend semantics, P0): append
923
- // the new right-justified row to the running buffer in ONE pad +
924
- // ONE concat — no per-row extraction. Existing pads grow, never
925
- // shrink (the re-merge below re-normalizes them instead; both are
926
- // masked, both token-exact vs their mlx-lm protocol twin).
927
- // MLX_BUN_BATCH_EXTEND=0 = the O(B·S) re-merge, kill switch/A-B.
928
- const [k0, v0] = prevFull.temporalView(); // [B,H,off,D]
929
- const ext = extendKVRows(k0, v0, prevPad, newRow);
930
- k0.dispose(); v0.dispose();
931
- newFullPad = ext.leftPad;
932
- const c = new KVCache();
933
- c.restoreState(ext.keys, ext.values, ext.width);
934
- newInners.push(c);
935
- newRow.keys.dispose(); newRow.values.dispose();
936
- } else {
937
- const rows: Row1[] = [];
938
- if (prevFull) {
939
- const [k0, v0] = prevFull.temporalView(); // [B,H,off,D]
940
- const [, H, off, D] = k0.shape as [number, number, number, number];
941
- const vD = v0.shape[3]!;
942
- for (let b = 0; b < B; b++) {
943
- const pad = prevPad[b]!;
944
- rows.push({
945
- keys: k0.slice([b, 0, pad, 0], [b + 1, H, off, D]),
946
- values: v0.slice([b, 0, pad, 0], [b + 1, H, off, vD]),
947
- });
948
- }
949
- k0.dispose(); v0.dispose();
950
- }
951
- rows.push(newRow);
952
- const merged = mergeKVRows(rows);
953
- newFullPad = merged.leftPad;
954
- const c = new KVCache();
955
- c.restoreState(merged.keys, merged.values, merged.width);
956
- newInners.push(c);
957
- for (const r of rows) { r.keys.dispose(); r.values.dispose(); }
958
- }
959
- }
960
- }
961
- if (prev) for (const c of prev) c.dispose();
962
- // An adopted row's entry-backed arrays are gone after the prev dispose;
963
- // run its retain now. The joiner's likewise after its solo dispose.
964
- this.#adoptedRetain?.();
965
- this.#adoptedRetain = null;
966
- for (const c of p.solo) c.dispose();
967
- p.retain?.();
968
- // Every row in a REAL merge has its KV interleaved in batched buffers —
969
- // no longer prompt-cache put() candidates.
970
- for (const r of this.#running) r.merged = true;
971
- p.row.merged = true;
972
- this.#inners = newInners;
973
- this.#fullLeftPad = newFullPad;
974
- this.#running.push(p.row);
975
- }
976
-
977
- /** One PIPELINED batched decode step (mlx-lm GenerationBatch._step):
978
- * 1. forward all rows from the UNREAD pending token array (or, pipeline
979
- * cold, from the rows' last emitted tokens), sample each live row's next
980
- * token on its [1,V] slice, asyncEval the new [B] token array;
981
- * 2. THEN sync-read the previous step's tokens (overlapping the readback
982
- * with this step's compute), emit them, and evict finished rows.
983
- * Rows that finish get one extra harmless KV write from the already-built
984
- * step; filter drops the row (mlx-lm behaves identically). Length-finished
985
- * rows are known in advance and are NOT sampled (placeholder slot). */
986
- async #step(): Promise<void> {
987
- if (STEP_TRACE) {
988
- const now = performance.now();
989
- if (STEP_T.lastEnd) STEP_T.gap += now - STEP_T.lastEnd;
990
- STEP_T.t0 = now;
991
- }
992
- const rows = this.#running;
993
- const B = rows.length;
994
- const inners = this.#inners!;
995
-
996
- // A row is live if it still needs tokens sampled; a row whose pending
997
- // unread token is its last (sampled == maxTokens) only awaits emission.
998
- const anyLive = rows.some((r) => r.sampled < r.req.maxTokens);
999
- // Grammar (B1): if any row has a LIVE grammar controller (not terminated,
1000
- // still sampling), take the read-before-build shape — the matcher's
1001
- // acceptToken needs the token id as a JS number, so the previous step's
1002
- // [B] token array is read back NOW (before building the next graph),
1003
- // accept()ed per row (firing async fills that overlap the forward), then
1004
- // ready() is awaited before this step's sample. This is the serial loop's
1005
- // grammar resolution transplanted to the batch. Batches with NO live
1006
- // grammar row keep the pipelined path byte-identical (zero cost when
1007
- // unused). The trade: while a grammar row is live the batch runs
1008
- // effectively NO_PIPELINE (readback no longer overlaps GPU compute) —
1009
- // bounded by the readback (~0.1 ms) + fills (0.004–0.19 ms/row, overlapped
1010
- // with the graph build). Serial grammar pays the identical trade today.
1011
- const hasLiveGrammar = anyLive && rows.some(
1012
- (r) => r.req.grammar && !r.req.grammar.isTerminated,
1013
- );
1014
- if (hasLiveGrammar) return this.#stepGrammar();
1015
-
1016
- let nextToks: MlxArray | null = null;
1017
- let nextReal: boolean[] | null = null;
1018
- if (anyLive) {
1019
- // Fed-token accounting (prompt-cache put): a COLD step feeds each
1020
- // row's `current` (values known now, always a real emitted token); a
1021
- // pipelined step feeds the pending array, whose values are pushed at
1022
- // the read below gated on the per-slot real flags.
1023
- if (!this.#pendingToks) for (const r of rows) r.fed.push(r.current);
1024
- // Per-layer forward cache: rot layers use the persistent
1025
- // BatchedRotatingCache directly; ssm layers are already [B,...] state
1026
- // with no padding (no mask, no per-row RoPE — pass through); full
1027
- // layers get a fresh BatchedDecodeMaskCache wrapper — UNLESS no row
1028
- // has left padding. UNPADDED FAST PATH (unified-engine plan Phase 2,
1029
- // the B=1 case above all): with every leftPad 0 the wrapper's two
1030
- // jobs vanish — the padding mask (KVCache.makeMask(1) is the empty
1031
- // mask, exactly the serial loop's) and the per-row rope positions
1032
- // (every row sits at the shared scalar offset). The bare cache then
1033
- // dispatches the SAME per-step graph serial builds; the wrapper
1034
- // otherwise costs a host mask build + ~8 device nodes PER FULL LAYER
1035
- // PER TOKEN (the Phase-0 constant ~4–6 ms/step host tax at B=1).
1036
- const unpadded = this.#fullLeftPad.every((p) => p === 0);
1037
- // Compiled decode at B=1 (Phase 3.2): after adopt-don't-copy, a lone
1038
- // row's caches are SERIAL-CLASS, so the serial engine's compiled step
1039
- // replays the same C++ graph here — closing the batch lane's last
1040
- // B=1 host-tax gap (e4b's ~7%). Guards: gemma dense (constructor),
1041
- // serial-class caches (supports — a merged batch's BatchedRotating
1042
- // layers fail it), unpadded, and a uint32 pipeline register (the
1043
- // trace signature; per-row int32 samplers take the graph path).
1044
- // Grammar batches use #stepGrammar and stay on the graph path.
1045
- let lg: MlxArray | null = null;
1046
- let evalWith: MlxArray[] = [];
1047
- if (
1048
- this.#compiled && B === 1 && unpadded &&
1049
- (!this.#pendingToks || this.#pendingToks.dtype === Dtype.uint32) &&
1050
- // A filtered-to-one BATCHED rot-quant cache subclasses the serial
1051
- // class (so supports() passes) but carries batched ring state —
1052
- // exclude it; only truly-adopted serial caches replay compiled.
1053
- !inners.some((c) => c instanceof BatchedRotatingQuantCache) &&
1054
- CompiledDecode.supports(inners as Cache[])
1055
- ) {
1056
- let cur = this.#pendingToks;
1057
- let owned = false;
1058
- if (!cur) {
1059
- const i = ops.fromInt32([rows[0]!.current], [1]);
1060
- cur = i.astype(Dtype.uint32);
1061
- i.dispose();
1062
- owned = true;
1063
- }
1064
- try {
1065
- const r = this.#compiled.step(cur, inners as Cache[]);
1066
- lg = r.logits; // [1,1,V]
1067
- evalWith = r.evalWith;
1068
- } catch (e) {
1069
- // A failed step is transactional (see generate.ts) — safe to
1070
- // re-forward the same token on the graph path below.
1071
- this.#compiled = null;
1072
- console.warn(`batch lane: compiled decode disabled: ${e}`);
1073
- } finally {
1074
- if (owned) cur.dispose();
1075
- }
1076
- }
1077
- let fwd: Cache[] | null = null;
1078
- try {
1079
- if (!lg) {
1080
- fwd = inners.map((c) =>
1081
- c instanceof BatchedRotatingCache || c instanceof BatchedRotatingQuantCache ||
1082
- c instanceof SSMCache || isBatchableCache(c) || unpadded
1083
- ? c
1084
- : c instanceof QuantizedKVCache
1085
- ? new BatchedQuantDecodeMaskCache(c, B, this.#fullLeftPad)
1086
- : new BatchedDecodeMaskCache(c, B, this.#fullLeftPad, null),
1087
- );
1088
- const ids = this.#pendingToks
1089
- ? ops.reshape(this.#pendingToks, [B, 1]) // feed the unread tokens
1090
- : ops.fromInt32(rows.map((r) => r.current), [B, 1]); // pipeline cold
1091
- const h = await this.#forwardHidden(ids, fwd);
1092
- ids.dispose();
1093
- lg = this.model.logitsFromHidden(h); // [B,1,V]
1094
- h.dispose();
1095
- }
1096
- const V = lg.shape[2]!;
1097
- // Vectorized homogeneous sampling (batching-perf-path P0): when every
1098
- // LIVE row is plain greedy, one log-softmax+argmax over [B,V] replaces
1099
- // B slice/sample/concat graphs. Per-row identical math (row-independent
1100
- // ops, same per-row shapes — argmax over log-softmax mirrors the
1101
- // closure's toLogprobs→argmax exactly, tie behavior included). Doomed
1102
- // rows get a real argmax instead of the placeholder 0 — equally
1103
- // harmless (the slot is filtered before it is ever emitted; its only
1104
- // use is one KV write on the row's own, about-to-evict row).
1105
- const vecOk =
1106
- process.env.MLX_BUN_BATCH_VEC_SAMPLE !== "0" &&
1107
- rows.every((r) => r.sampled >= r.req.maxTokens || r.req.plainGreedy);
1108
- // Doomed slots hold placeholders (vec path: a real argmax value,
1109
- // equally not part of the row's stream) — flag them so the fed
1110
- // accounting at the read stays per-row exact.
1111
- nextReal = rows.map((r) => r.sampled < r.req.maxTokens);
1112
- if (vecOk) {
1113
- const flat = ops.reshape(lg, [B, V]);
1114
- lg.dispose();
1115
- const lp = toLogprobs(flat);
1116
- flat.dispose();
1117
- nextToks = ops.argmaxAxis(lp, -1); // [B]
1118
- lp.dispose();
1119
- for (const row of rows) if (row.sampled < row.req.maxTokens) row.sampled++;
1120
- } else {
1121
- const sampled: MlxArray[] = [];
1122
- for (let b = 0; b < B; b++) {
1123
- const row = rows[b]!;
1124
- if (row.sampled >= row.req.maxTokens) {
1125
- // Length-doomed row: evicted right after the emission below ever
1126
- // uses this slot as input — placeholder keeps the [B] alignment.
1127
- sampled.push(ops.fromInt32([0], [1]));
1128
- continue;
1129
- }
1130
- const rl = lg.slice([b, 0, 0], [b + 1, 1, V]);
1131
- const rl2 = ops.reshape(rl, [1, V]);
1132
- rl.dispose();
1133
- sampled.push(row.req.sample(rl2, row.sampled));
1134
- row.sampled++;
1135
- rl2.dispose();
1136
- }
1137
- lg.dispose();
1138
- nextToks = ops.concatAxis(sampled, 0); // [B]
1139
- for (const t of sampled) t.dispose();
1140
- }
1141
- // dispatch; read NEXT iteration. evalWith: the compiled step's
1142
- // cache-update nodes must ride the same async_eval (generate.ts).
1143
- ops.asyncEvalAll([nextToks, ...evalWith]);
1144
- } finally {
1145
- // Free the step's RoPE arrays; do NOT dispose (full wrappers would free
1146
- // their persistent inner; rot caches persist across steps).
1147
- if (fwd) for (const c of fwd) (c as { releaseRopeArr?: () => void }).releaseRopeArr?.();
1148
- }
1149
- }
1150
- this.#steps++;
1151
- if (this.#steps % 256 === 0) clearCache(); // serial's cadence, not per-step
1152
- if (STEP_TRACE) STEP_T.build += performance.now() - STEP_T.t0;
1153
-
1154
- // Read + emit the PREVIOUS step's tokens while the new step computes.
1155
- const prev = this.#pendingToks;
1156
- const prevReal = this.#pendingReal;
1157
- this.#pendingToks = nextToks;
1158
- this.#pendingReal = nextReal;
1159
- // Kill switch / A-B lever (house style, cf. MLX_BUN_COMPILED_DECODE=0):
1160
- // MLX_BUN_BATCH_NO_PIPELINE=1 reads THIS step's tokens synchronously —
1161
- // set from process start `prev` is always null, so the flush below IS the
1162
- // whole phase 2. Same math either way (pipelining is scheduling).
1163
- if (NO_PIPELINE) {
1164
- await this.#flushPipeline();
1165
- return;
1166
- }
1167
- if (prev) {
1168
- const tRead = STEP_TRACE ? performance.now() : 0;
1169
- const toks = prev.toIntTokens();
1170
- prev.dispose();
1171
- // These values were the step's forward input (fed) iff a forward ran.
1172
- // Placeholder slots are excluded from `fed` — and taint their row if
1173
- // they ever fed (see Row.fedTainted; structurally unreachable today).
1174
- if (anyLive)
1175
- for (let b = 0; b < B; b++) {
1176
- if (!prevReal || prevReal[b]) rows[b]!.fed.push(toks[b]!);
1177
- else rows[b]!.fedTainted = true;
1178
- }
1179
- if (STEP_TRACE) STEP_T.read += performance.now() - tRead;
1180
- const tEmit = STEP_TRACE ? performance.now() : 0;
1181
- await this.#emitRows(toks); // also filters #pendingToks on eviction
1182
- if (STEP_TRACE) { STEP_T.emit += performance.now() - tEmit; STEP_T.n++; }
1183
- }
1184
- if (STEP_TRACE) STEP_T.lastEnd = performance.now();
1185
- }
1186
-
1187
- /** Read out the pipeline register (if any): emit its tokens and evict
1188
- * finished rows, leaving the pipeline cold. Called before a join merges. */
1189
- async #flushPipeline(): Promise<void> {
1190
- const prev = this.#pendingToks;
1191
- if (!prev) return;
1192
- this.#pendingToks = null;
1193
- this.#pendingReal = null; // flushed values never fed — nothing to account
1194
- const toks = prev.toIntTokens();
1195
- prev.dispose();
1196
- // Grammar rows: the flushed tokens are EMITTED below, so their matchers
1197
- // must advance here — #stepGrammar's accept only covers tokens it reads
1198
- // from a live pending array, and after this flush it cold-starts (no
1199
- // accept). Skipping this left the matcher one token behind its stream on
1200
- // every mid-decode join → one-step-stale masks → invalid output (found
1201
- // by the feature-matrix conformance gate, 2026-07-03; the fill fired
1202
- // here is awaited by the next #stepGrammar's ready()).
1203
- for (let b = 0; b < this.#running.length && b < toks.length; b++) {
1204
- const g = this.#running[b]!.req.grammar;
1205
- if (g && !g.isTerminated) g.accept(toks[b]!);
1206
- }
1207
- await this.#emitRows(toks);
1208
- }
1209
-
1210
- /** The read-before-build decode step for batches with ≥1 live grammar row
1211
- * (B1). Mirrors the serial loop's grammar resolution: read the previous
1212
- * step's [B] tokens NOW (acceptToken needs JS numbers), accept() per row
1213
- * (fires async fills), build the forward graph (fills overlap), await
1214
- * ready(), sample per live row (the closure applies applyMask), then emit
1215
- * the values read in step 1 — no second readback. Terminated grammar rows
1216
- * keep their [B] slot through the forward (placeholder, not sampled) and
1217
- * finish("stop") in #emitRows. On a cold start (first step after prefill)
1218
- * there is no pending array to read; the prefill already accepted token 0
1219
- * and fired the fill, so we just await ready() + sample. */
1220
- async #stepGrammar(): Promise<void> {
1221
- const rows = this.#running;
1222
- const B = rows.length;
1223
- const inners = this.#inners!;
1224
-
1225
- // (1) Read the pending [B] token array (host copy for accept). The device
1226
- // array is kept for the forward input below; disposed after sampling.
1227
- // Cold start: pendingToks null → prevVals empty (prefill handled tok0).
1228
- const prev = this.#pendingToks;
1229
- const prevVals: number[] =
1230
- prev ? prev.toIntTokens() : [];
1231
- if (process.env.MLX_BUN_GRAMMAR_DEBUG === "1")
1232
- console.log(`[sg] B=${B} prevVals=${JSON.stringify(prevVals)} current=${JSON.stringify(rows.map((r) => r.current))} sampled=${JSON.stringify(rows.map((r) => r.sampled))}`);
1233
-
1234
- // (2) accept() per live grammar row — fires that row's async bitmask fill.
1235
- for (let b = 0; b < B && prevVals.length; b++) {
1236
- const g = rows[b]!.req.grammar;
1237
- if (g && !g.isTerminated) g.accept(prevVals[b]!);
1238
- }
1239
-
1240
- let nextToks: MlxArray | null = null;
1241
- let nextReal: boolean[] | null = null;
1242
- const anyLive = rows.some((r) => r.sampled < r.req.maxTokens);
1243
- if (anyLive) {
1244
- // Fed-token accounting (prompt-cache put) — mirror of #step's read:
1245
- // real slots push, a placeholder that fed taints its row.
1246
- const prevReal = this.#pendingReal;
1247
- if (prev)
1248
- for (let b = 0; b < B; b++) {
1249
- if (!prevReal || prevReal[b]) rows[b]!.fed.push(prevVals[b]!);
1250
- else rows[b]!.fedTainted = true;
1251
- }
1252
- else for (const r of rows) r.fed.push(r.current);
1253
- // Unpadded fast path — same rule as #step (bare caches == serial graph).
1254
- const unpadded = this.#fullLeftPad.every((p) => p === 0);
1255
- const fwd: Cache[] = inners.map((c) =>
1256
- c instanceof BatchedRotatingCache || c instanceof BatchedRotatingQuantCache ||
1257
- c instanceof SSMCache || isBatchableCache(c) || unpadded
1258
- ? c
1259
- : c instanceof QuantizedKVCache
1260
- ? new BatchedQuantDecodeMaskCache(c, B, this.#fullLeftPad)
1261
- : new BatchedDecodeMaskCache(c, B, this.#fullLeftPad, null),
1262
- );
1263
- try {
1264
- // (3) Build the forward graph (host-side; the fills overlap it).
1265
- const ids = prev
1266
- ? ops.reshape(prev, [B, 1]) // feed the unread tokens (device array)
1267
- : ops.fromInt32(rows.map((r) => r.current), [B, 1]); // pipeline cold
1268
- const h = await this.#forwardHidden(ids, fwd);
1269
- ids.dispose();
1270
- const lg = this.model.logitsFromHidden(h); // [B,1,V]
1271
- h.dispose();
1272
- const V = lg.shape[2]!;
1273
-
1274
- // (4) await ready() on every live grammar row — the fills fired in (2)
1275
- // overlapped the graph build above. Usually already resolved.
1276
- const liveGrammar = rows.filter(
1277
- (r) => r.req.grammar && !r.req.grammar.isTerminated,
1278
- );
1279
- if (liveGrammar.length)
1280
- await Promise.all(liveGrammar.map((r) => r.req.grammar!.ready()));
1281
-
1282
- // (5) Sample per live row (the closure applies applyMask after the
1283
- // logits processors). Terminated grammar rows + length-doomed rows
1284
- // take a placeholder (one harmless KV write, then evicted).
1285
- const sampled: MlxArray[] = [];
1286
- nextReal = rows.map(
1287
- (r) => r.sampled < r.req.maxTokens && !r.req.grammar?.isTerminated,
1288
- );
1289
- for (let b = 0; b < B; b++) {
1290
- const row = rows[b]!;
1291
- if (
1292
- row.sampled >= row.req.maxTokens ||
1293
- row.req.grammar?.isTerminated
1294
- ) {
1295
- sampled.push(ops.fromInt32([0], [1]));
1296
- continue;
1297
- }
1298
- const rl = lg.slice([b, 0, 0], [b + 1, 1, V]);
1299
- const rl2 = ops.reshape(rl, [1, V]);
1300
- rl.dispose();
1301
- sampled.push(row.req.sample(rl2, row.sampled));
1302
- row.sampled++;
1303
- rl2.dispose();
1304
- }
1305
- lg.dispose();
1306
- nextToks = ops.concatAxis(sampled, 0); // [B]
1307
- for (const t of sampled) t.dispose();
1308
- ops.asyncEvalAll([nextToks]);
1309
- } finally {
1310
- for (const c of fwd)
1311
- (c as { releaseRopeArr?: () => void }).releaseRopeArr?.();
1312
- }
1313
- }
1314
- this.#steps++;
1315
- if (this.#steps % 256 === 0) clearCache();
1316
-
1317
- // The device array `prev` has now fed the forward + been read for accept;
1318
- // dispose it and install the new pending array. On cold start prev is null.
1319
- prev?.dispose();
1320
- this.#pendingToks = nextToks;
1321
- this.#pendingReal = nextReal;
1322
-
1323
- // (6) Emit the values read in (1) — no second readback. Terminated grammar
1324
- // rows finish("stop") via #emit's isTerminated check; the filter evicts.
1325
- // Cold start: prevVals empty (prefill emitted tok0) → nothing to emit.
1326
- if (prevVals.length) await this.#emitRows(prevVals);
1327
- }
1328
-
1329
- /** Emit one read-back token per running row; evict finished rows. A row's
1330
- * onToken throwing rejects THAT row and evicts it — siblings continue. */
1331
- async #emitRows(toks: number[]): Promise<void> {
1332
- const rows = this.#running;
1333
- const B = rows.length;
1334
- const keep: number[] = [];
1335
- const done: { b: number; row: Row }[] = []; // clean finishes (stop/length)
1336
- for (let b = 0; b < B; b++) {
1337
- const row = rows[b]!;
1338
- row.generated++;
1339
- let disp: "continue" | "stop" | "length";
1340
- try {
1341
- disp = await this.#emit(row, toks[b]!);
1342
- } catch (e) {
1343
- row.reject(e); // containment: this row only (mlx-lm `remove`)
1344
- row.merged = true; // poison: a rejected row is never put() back
1345
- row.fedTainted = true;
1346
- continue; // not kept → evicted by the filter below
1347
- }
1348
- if (disp === "continue") keep.push(b);
1349
- else {
1350
- done.push({ b, row });
1351
- this.#finish(row, disp);
1352
- }
1353
- }
1354
- if (keep.length < B) {
1355
- // Per-row prompt-cache extraction (mlx-lm server.py:864-880 →
1356
- // BatchGenerator.extract_cache): a MERGED row's KV lives interleaved
1357
- // in the batched inners, so it is pulled out into fresh serial caches
1358
- // BEFORE filter() mutates them. Never-merged lone rows skip this —
1359
- // #applyFilter's keep=[] path put()s their adopted caches zero-copy.
1360
- // Rejected rows aren't in `done`; the whole-batch error path calls
1361
- // #applyFilter(dropOnly) directly and never reaches here.
1362
- for (const { b, row } of done) this.#extractAndPut(b, row);
1363
- this.#applyFilter(keep);
1364
- }
1365
- }
1366
-
1367
- /** Account one sampled token for a row. Mirrors generate(): EOS terminates
1368
- * WITHOUT an onToken call; otherwise onToken(token) runs and `false` halts;
1369
- * reaching maxTokens ends with "length". Advances row.current on continue. */
1370
- async #emit(row: Row, token: number): Promise<"continue" | "stop" | "length"> {
1371
- row.req.signal?.throwIfAborted();
1372
- if (row.req.eosTokenIds.includes(token)) return "stop";
1373
- const cont = await row.req.onToken(token);
1374
- if (cont === false) return "stop";
1375
- // Grammar termination: the matcher is satisfied (e.g. closing `}` accepted).
1376
- // The final token has been delivered via onToken above; halt with "stop" so
1377
- // the row finishes + evicts rather than sampling into an all--inf mask.
1378
- if (row.req.grammar?.isTerminated) return "stop";
1379
- if (row.generated >= row.req.maxTokens) return "length";
1380
- row.current = token;
1381
- return "continue";
1382
- }
1383
-
1384
- #finish(row: Row, reason: "stop" | "length"): void {
1385
- row.resolve({
1386
- promptTokens: row.promptTokens,
1387
- generatedTokens: row.generated,
1388
- cachedTokens: row.cachedTokens,
1389
- finishReason: reason,
1390
- });
1391
- }
1392
-
1393
- /** Finish-time disposition of serial-class caches covering exactly
1394
- * `tokens`: put() into the prompt cache when the hook is present and the
1395
- * offset lines up (defensive — a mismatch means the accounting is wrong
1396
- * and the entry would corrupt future hits), else dispose. `retain` rides
1397
- * along per the PromptCacheEntry contract (runs after dispose). */
1398
- #putOrDispose(caches: Cache[], tokens: number[], retain?: () => void): void {
1399
- if (this.#promptCache) {
1400
- const withOff = caches.find(
1401
- (c) => typeof (c as { offset?: unknown }).offset === "number",
1402
- ) as { offset: number } | undefined;
1403
- if (withOff && withOff.offset === tokens.length) {
1404
- this.#promptCache.put(tokens, caches, "", retain);
1405
- return;
1406
- }
1407
- }
1408
- for (const c of caches) c.dispose();
1409
- retain?.();
1410
- }
1411
-
1412
- /** Extract a finishing MERGED row's KV into fresh serial caches and put()
1413
- * them keyed by [promptIds + fed] — the batch-lane mirror of mlx-lm
1414
- * server.py:872 (extract_cache → prompt_cache.insert_cache). Gates:
1415
- * promptTokens >= 256 (the boundary-snapshot substantiality gate,
1416
- * server.ts) and an exact coverage key (!fedTainted). Refusal
1417
- * (#extractRowCaches null) just disposes-by-omission — the row's KV dies
1418
- * with the filter, exactly the pre-extraction behavior. */
1419
- #extractAndPut(b: number, row: Row): void {
1420
- if (!this.#promptCache || !row.merged || row.fedTainted) return;
1421
- if (row.promptTokens < 256) return;
1422
- const tokens = [...row.req.promptIds, ...row.fed];
1423
- const caches = this.#extractRowCaches(b, tokens.length);
1424
- if (!caches) return;
1425
- // Materialize the owned copies without stalling the in-flight step (the
1426
- // slices depend on this step's KV writes): async — the batched source
1427
- // buffers free once the copies land, instead of being pinned by a lazy
1428
- // graph inside an idle cache entry.
1429
- ops.asyncEvalAll(caches.flatMap((c) => c.state()));
1430
- this.#putOrDispose(caches, tokens);
1431
- }
1432
-
1433
- /** Row `b` of every layer as OWNED serial-class caches, or null when a
1434
- * layer kind can't be extracted (then the caller drops the row's KV as
1435
- * before). Bit-exactness: merge/extend/filter/decode are byte-preserving
1436
- * per row (tests/batched-decode-parity, tests/batched-rotating,
1437
- * tests/batched-rotating-quant) and each extract is a pure slice+copy of
1438
- * those bytes (tests/batched-extract), so an extracted row's bytes ==
1439
- * the solo run's. */
1440
- #extractRowCaches(b: number, expectTokens: number): Cache[] | null {
1441
- const out: Cache[] = [];
1442
- for (const inner of this.#inners!) {
1443
- let c: Cache | null;
1444
- if (isBatchableCache(inner)) c = inner.extractRow(b);
1445
- else if (inner instanceof BatchedRotatingQuantCache) c = inner.extractRow(b);
1446
- else if (inner instanceof BatchedRotatingCache) c = inner.extractRow(b);
1447
- else if (inner instanceof SSMCache)
1448
- // Coverage gate: recurrent state is UNTRIMMABLE, so an entry is only
1449
- // valid when the row's own advance count equals the [promptIds+fed]
1450
- // key EXACTLY (a mismatched entry would silently corrupt every future
1451
- // exact-hit). Defensive — a miss here means the fed accounting broke.
1452
- c = inner.conv && inner.rowOffset(b) === expectTokens ? inner.extractRow(b) : null;
1453
- else if (
1454
- inner instanceof RotatingQuantizedKVCache || // adopted serial state never
1455
- inner instanceof RotatingKVCache // coexists with a merged row (defensive)
1456
- )
1457
- c = null;
1458
- else if (inner instanceof QuantizedKVCache)
1459
- c = inner.keys ? extractQuantRow(inner, this.#fullLeftPad[b]!, b) : null;
1460
- else c = inner.keys ? extractKVRow(inner, this.#fullLeftPad[b]!, b) : null;
1461
- if (!c) {
1462
- for (const d of out) d.dispose();
1463
- return null;
1464
- }
1465
- out.push(c);
1466
- }
1467
- return out;
1468
- }
1469
-
1470
- /** Evict rows not in `keep` (sorted ascending) from the batched KV and the
1471
- * pipeline register. */
1472
- #applyFilter(keep: number[], dropOnly = false): void {
1473
- const inners = this.#inners!;
1474
- if (keep.length === 0) {
1475
- // Prompt-cache put (Phase 3.2): a lone NEVER-MERGED row's inners are
1476
- // its adopted serial-class caches, covering exactly prompt+fed — hand
1477
- // them back to the cache instead of disposing. dropOnly (the batch-
1478
- // drop error path) and poisoned rows dispose as before; MERGED rows'
1479
- // KV was already extracted per row in #emitRows (owned copies), so
1480
- // disposing the batched inners here is safe either way.
1481
- const solo =
1482
- !dropOnly && this.#running.length === 1 && !this.#running[0]!.merged
1483
- ? this.#running[0]!
1484
- : null;
1485
- if (solo) {
1486
- this.#putOrDispose(
1487
- inners as Cache[],
1488
- [...solo.req.promptIds, ...solo.fed],
1489
- this.#adoptedRetain ?? undefined,
1490
- );
1491
- } else {
1492
- for (const c of inners) c.dispose();
1493
- this.#adoptedRetain?.();
1494
- }
1495
- this.#adoptedRetain = null;
1496
- this.#inners = null;
1497
- this.#fullLeftPad = [];
1498
- this.#running = [];
1499
- this.#pendingToks?.dispose();
1500
- this.#pendingToks = null;
1501
- this.#pendingReal = null;
1502
- return;
1503
- }
1504
- const out: LayerInner[] = [];
1505
- for (const inner of inners) {
1506
- if (isBatchableCache(inner)) {
1507
- inner.filterRows(keep);
1508
- out.push(inner);
1509
- } else if (inner instanceof BatchedRotatingCache) {
1510
- inner.filter(keep); // in-place (mutates + drops rows + reduces padding)
1511
- out.push(inner);
1512
- } else if (inner instanceof BatchedRotatingQuantCache) {
1513
- inner.filter(keep); // in-place B-axis take on all six components
1514
- out.push(inner);
1515
- } else if (inner instanceof RotatingQuantizedKVCache || inner instanceof RotatingKVCache) {
1516
- // Adopted lone-row state exists only at B=1, where the only filter
1517
- // is the keep=[] dispose-all handled above — unreachable.
1518
- throw new Error("applyFilter: adopted serial rotating cache cannot be row-filtered");
1519
- } else if (inner instanceof SSMCache) {
1520
- inner.filter(keep); // in-place B-axis take on both state slots
1521
- out.push(inner);
1522
- } else if (inner instanceof QuantizedKVCache) {
1523
- const [k0, v0] = inner.temporalView();
1524
- const f = filterQuantRows(k0, v0, keep);
1525
- for (const t of [k0, v0]) { t.packed.dispose(); t.scales.dispose(); t.biases.dispose(); }
1526
- const c = new QuantizedKVCache(inner.groupSize, inner.bits);
1527
- c.restoreState(f.keys, f.values, inner.offset);
1528
- out.push(c);
1529
- inner.dispose();
1530
- } else {
1531
- const [k0, v0] = inner.temporalView();
1532
- const f = filterKVRows(k0, v0, keep);
1533
- k0.dispose(); v0.dispose();
1534
- const c = new KVCache();
1535
- c.restoreState(f.keys, f.values, inner.offset);
1536
- out.push(c);
1537
- inner.dispose();
1538
- }
1539
- }
1540
- this.#inners = out;
1541
- this.#fullLeftPad = keep.map((i) => this.#fullLeftPad[i]!);
1542
- this.#running = keep.map((i) => this.#running[i]!);
1543
- if (this.#pendingToks) {
1544
- const idx = ops.fromInt32(keep, [keep.length]);
1545
- const next = ops.takeAxis(this.#pendingToks, idx, 0);
1546
- idx.dispose();
1547
- this.#pendingToks.dispose();
1548
- this.#pendingToks = next;
1549
- }
1550
- if (this.#pendingReal) this.#pendingReal = keep.map((i) => this.#pendingReal![i]!);
1551
- }
1552
-
1553
- #readToken(t: MlxArray): number {
1554
- const v = t.toIntTokens()[0]!;
1555
- t.dispose();
1556
- return v;
1557
- }
1558
- }
1
+ // Compatibility import. Scheduling policy lives in engine/scheduler; MLX
2
+ // batch state and numerical execution belong to the backend group.
3
+ export {
4
+ MlxBatchExecutionGroup as BatchScheduler,
5
+ stepTraceReport,
6
+ type MlxBatchExecutionGroupOptions as BatchSchedulerOptions,
7
+ type BatchRequest, type BatchStats, type RowSampler, type RowPromptCache, type ExclusiveLock,
8
+ } from "../backends/mlx/batch-group";