mlx-bun 0.5.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1449 -109
- package/THIRD_PARTY_NOTICES.md +882 -0
- package/bin/mlx-bun.convert.mjs +4 -0
- package/bin/mlx-bun.fuse.mjs +4 -0
- package/bin/mlx-bun.generate.mjs +4 -0
- package/bin/mlx-bun.lora.mjs +4 -0
- package/bin/mlx-bun.mjs +13 -21
- package/bin/mlx-bun.server.mjs +4 -0
- package/bin/mlx-bun.upload.mjs +4 -0
- package/dist/web/app.js +9584 -0
- package/package.json +50 -53
- package/src/cli/ane-install.ts +76 -0
- package/src/cli/args.ts +179 -0
- package/src/cli/engine-entry.ts +68 -0
- package/src/cli/inference.ts +214 -0
- package/src/cli/job-entry.ts +97 -0
- package/src/cli/library-host.ts +117 -0
- package/src/cli/main.ts +57 -0
- package/src/cli/memory-engine.ts +163 -0
- package/src/cli/memory.ts +36 -0
- package/src/cli/mlx-lm-aliases.ts +309 -0
- package/src/cli/model-choice.ts +33 -0
- package/src/cli/model-selection.ts +84 -0
- package/src/cli/module-host.ts +11 -0
- package/src/cli/module-verbs.ts +50 -0
- package/src/cli/numerical-policy.ts +12 -0
- package/src/cli/perplexity.ts +89 -0
- package/src/cli/serve-host.ts +423 -0
- package/src/cli/serve-isolated.ts +217 -0
- package/src/cli/serve-options.ts +106 -0
- package/src/cli/serve-state.ts +205 -0
- package/src/cli/serve.ts +500 -0
- package/src/cli/served-model-host.ts +102 -0
- package/src/cli/server-entry.ts +148 -0
- package/src/cli/serving-unit.ts +177 -0
- package/src/{tui.ts → cli/terminal.ts} +13 -8
- package/src/cli/worker-adapters.ts +34 -0
- package/src/cli/worker-entry.ts +272 -0
- package/src/cli/worker-unit.ts +179 -0
- package/src/engine/adapter-operation.ts +26 -0
- package/src/engine/cache-services.ts +114 -0
- package/src/engine/completion.ts +39 -0
- package/src/engine/generation-gateway.ts +358 -0
- package/src/engine/index.ts +59 -0
- package/src/engine/kv-budget.ts +120 -0
- package/src/engine/media-preparation.ts +232 -0
- package/src/engine/model-binding.ts +24 -0
- package/src/engine/model-host.ts +513 -0
- package/src/engine/model-serving.ts +89 -0
- package/src/{serve → engine}/preparation.ts +7 -5
- package/src/{serve → engine}/session-completion-engine.ts +6 -6
- package/src/engine/telemetry.ts +109 -0
- package/src/hub/downloads.ts +116 -0
- package/src/jobs/db.ts +16 -7
- package/src/jobs/events.ts +29 -0
- package/src/jobs/executable.ts +5 -0
- package/src/jobs/host.ts +51 -0
- package/src/jobs/in-process.ts +55 -0
- package/src/{contracts/jobs.ts → jobs/protocol.ts} +7 -9
- package/src/jobs/runner.ts +150 -231
- package/src/jobs/service.ts +75 -0
- package/src/jobs/sse.ts +59 -23
- package/src/jobs/worker-memory.ts +46 -0
- package/src/jobs/worker-process.ts +163 -0
- package/src/jobs/worker-supervisor.ts +258 -0
- package/src/modules.ts +58 -0
- package/src/publishing/catalog-hub.ts +18 -0
- package/src/publishing/credentials.ts +31 -0
- package/src/publishing/upload.ts +37 -0
- package/src/residency/leased-adapters.ts +21 -0
- package/src/residency/local-records.ts +16 -0
- package/src/residency/model-residency.ts +405 -0
- package/src/residency/record-index.ts +28 -0
- package/src/residency/resident-estimate.ts +50 -0
- package/src/{anthropic.ts → server/anthropic.ts} +3 -81
- package/src/server/cache-routes.ts +36 -0
- package/src/{serve → server}/chat-request.ts +10 -10
- package/src/{serve → server}/chat-stage.ts +14 -14
- package/src/{client.ts → server/client.ts} +28 -2
- package/src/{serve → server}/completion-executor.ts +33 -41
- package/src/server/decision-body.ts +55 -0
- package/src/server/decision-routes.ts +134 -0
- package/src/server/decision-wire.ts +228 -0
- package/src/{serve → server}/discovery-routes.ts +25 -82
- package/src/{serve → server}/http.ts +25 -13
- package/src/{serve → server}/inference-request.ts +5 -5
- package/src/server/job-routes.ts +20 -0
- package/src/server/media-prompt.ts +63 -0
- package/src/server/memory-completion-client.ts +46 -0
- package/src/server/model-routes.ts +192 -0
- package/src/{serve → server}/openai-wire.ts +3 -2
- package/src/{serve → server}/pipeline.ts +1 -1
- package/src/server/prompt-builder.ts +13 -0
- package/src/server/prompt-contracts.ts +17 -0
- package/src/server/proxy-routes.ts +187 -0
- package/src/server/publishing-routes.ts +41 -0
- package/src/{serve → server}/request-plan.ts +6 -8
- package/src/{serve → server}/request-prep.ts +47 -50
- package/src/{serve → server}/responses-client.ts +30 -13
- package/src/{responses.ts → server/responses.ts} +12 -93
- package/src/server/routes.ts +205 -0
- package/src/server/start.ts +72 -0
- package/src/server/status-routes.ts +108 -0
- package/src/{serve → server}/text-completion-stage.ts +25 -4
- package/src/server/text-prompt.ts +12 -0
- package/src/{serve → server}/token-streams.ts +36 -38
- package/src/server/worker-routes.ts +311 -0
- package/src/storage/paths.ts +61 -0
- package/src/web/assets.ts +37 -0
- package/src/web/{src → browser}/api.ts +5 -23
- package/src/web/browser/chat-host.ts +17 -0
- package/src/web/browser/installed-panels.ts +6 -0
- package/src/web/browser/main.ts +55 -0
- package/src/web/browser/markdown.ts +279 -0
- package/src/web/browser/memory-panel.ts +9 -0
- package/src/web/browser/palette.ts +64 -0
- package/src/web/browser/protocol.ts +13 -0
- package/src/web/browser/shell.ts +467 -0
- package/src/web/{src → browser}/status.ts +32 -36
- package/src/web/build.ts +51 -0
- package/src/web/public/app.html +1054 -0
- package/src/web/{sw.js → public/sw.js} +4 -3
- package/src/web/{vendor → public/vendor}/hljs-theme.css +2 -2
- package/src/web/{vendor → public/vendor}/hljs.js +2 -2
- package/THIRD_PARTY_LICENSES.md +0 -51
- package/docs/README.md +0 -107
- package/docs/archive/hlg-sampling.md +0 -350
- package/docs/archive/investigations/audio-input-plan.md +0 -356
- package/docs/archive/investigations/benchmark-ledger-through-v0.4.0.md +0 -3617
- package/docs/archive/investigations/chunk-finetune-experiment.md +0 -84
- package/docs/archive/investigations/colibri-glm52-port.md +0 -1486
- package/docs/archive/investigations/colibri-oracle-pin.md +0 -1284
- package/docs/archive/investigations/curve-bisector-routing.md +0 -123
- package/docs/archive/investigations/decode-roofline-lookagain.md +0 -348
- package/docs/archive/investigations/diffusion-gemma-port.md +0 -602
- package/docs/archive/investigations/dspark-handoff.md +0 -224
- package/docs/archive/investigations/expert-offload-single-user-moe.md +0 -292
- package/docs/archive/investigations/faithful-l1-consolidation.md +0 -310
- package/docs/archive/investigations/hlg-sampling-investigation.md +0 -970
- package/docs/archive/investigations/indexshare-performance-spike.md +0 -357
- package/docs/archive/investigations/kernel-perf-review-2026-07.md +0 -171
- package/docs/archive/investigations/lab-build-journal.md +0 -157
- package/docs/archive/investigations/minicpm5-decode-megakernel.md +0 -489
- package/docs/archive/investigations/openjarvis-fit.md +0 -198
- package/docs/archive/investigations/openwiki-evaluation.md +0 -249
- package/docs/archive/investigations/optimization_plan.md +0 -346
- package/docs/archive/investigations/orpo-base-uf-experiment-and-directions.md +0 -120
- package/docs/archive/investigations/orpo-flash-cce-pin-leak.md +0 -167
- package/docs/archive/investigations/orpo-uf-testing-handoff.md +0 -126
- package/docs/archive/investigations/pareto-specialized-runtime-findings.md +0 -92
- package/docs/archive/investigations/pi-builtin-investigation.md +0 -308
- package/docs/archive/investigations/pre-colibri-stabilization.md +0 -329
- package/docs/archive/investigations/qwen38-27b-campaign-log.md +0 -1092
- package/docs/archive/investigations/segmented-backward-handoff.md +0 -148
- package/docs/archive/investigations/starter-model-port-handoff.md +0 -342
- package/docs/archive/investigations/steel-flash-cce-handoff.md +0 -489
- package/docs/archive/investigations/the-dreaming-handoff.md +0 -141
- package/docs/archive/investigations/trainer-validation-experiment.md +0 -173
- package/docs/archive/planning/chat-ui-vision-memory-first.md +0 -199
- package/docs/archive/planning/chat-ui-vision.md +0 -50
- package/docs/archive/planning/chunk-task-roadmap.md +0 -77
- package/docs/archive/planning/curve-sampler-research-plan.md +0 -331
- package/docs/archive/planning/journal.md +0 -316
- package/docs/archive/planning/memory-docs-and-dag-plan.md +0 -194
- package/docs/archive/planning/pi-integration-review.md +0 -178
- package/docs/archive/planning/release-notes-v0.0.10.md +0 -57
- package/docs/archive/planning/release-notes-v0.0.11.md +0 -239
- package/docs/archive/planning/release-notes-v0.0.12.md +0 -80
- package/docs/archive/planning/release-notes-v0.0.13.md +0 -77
- package/docs/archive/planning/release-notes-v0.0.9.md +0 -122
- package/docs/archive/planning/release-notes-v0.2.0.md +0 -49
- package/docs/archive/planning/release-notes-v0.3.0.md +0 -35
- package/docs/archive/planning/release-notes-v0.4.0.md +0 -45
- package/docs/archive/planning/release-notes-v0.5.0.md +0 -37
- package/docs/archive/planning/web-ui-pass-plan.md +0 -213
- package/docs/archive/planning/website-readme-pass-plan.md +0 -224
- package/docs/design/batching.md +0 -2054
- package/docs/design/decode-speed-program.md +0 -2404
- package/docs/design/dreaming-nightly-pipeline.md +0 -684
- package/docs/design/generic-model-support.md +0 -821
- package/docs/design/kv-cache.md +0 -993
- package/docs/design/orpo-training.md +0 -680
- package/docs/design/speculative-decoding.md +0 -2129
- package/docs/design/turboquant.md +0 -2225
- package/docs/design/unified-engine-frontier-plan.md +0 -1987
- package/docs/design/web-chat-redesign.md +0 -695
- package/docs/planning/PRODUCT_ROADMAP.md +0 -593
- package/docs/planning/ResearchTopics.md +0 -119
- package/docs/planning/public-readiness-handoff.md +0 -244
- package/docs/planning/public-readiness.md +0 -149
- package/docs/planning/video-series.md +0 -421
- package/docs/reference/benchmarks.md +0 -3587
- package/docs/reference/cli.md +0 -646
- package/docs/reference/distribution.md +0 -380
- package/docs/reference/environment.md +0 -494
- package/docs/reference/glossary.md +0 -39
- package/docs/reference/library-api.md +0 -688
- package/docs/reference/memory.md +0 -260
- package/docs/reference/models.md +0 -346
- package/docs/reference/server-api.md +0 -1945
- package/docs/reference/server-config.md +0 -973
- package/docs/reference/training.md +0 -462
- package/docs/reference/troubleshooting.md +0 -158
- package/src/admission-plan.ts +0 -18
- package/src/audio/audiotoolbox.ts +0 -112
- package/src/audio/conformer.ts +0 -655
- package/src/audio/decode.ts +0 -133
- package/src/audio/features.ts +0 -202
- package/src/audio/mic-capture.ts +0 -98
- package/src/audio/silero-vad.ts +0 -379
- package/src/audio/transcode.ts +0 -67
- package/src/audio/whisper-decode.ts +0 -774
- package/src/audio/whisper-fast.ts +0 -732
- package/src/audio/whisper-format.ts +0 -73
- package/src/audio/whisper-mel.ts +0 -185
- package/src/audio/whisper-timing.ts +0 -274
- package/src/audio/whisper-tokenizer.ts +0 -239
- package/src/audio/whisper-transcribe.ts +0 -410
- package/src/backends/mlx/assistant-target.ts +0 -46
- package/src/backends/mlx/autoregressive-draft-rows.ts +0 -120
- package/src/backends/mlx/autoregressive.ts +0 -129
- package/src/backends/mlx/batch-group.ts +0 -1874
- package/src/backends/mlx/cache-attachment.ts +0 -67
- package/src/backends/mlx/cache-layout.ts +0 -31
- package/src/backends/mlx/checkpoint-state.ts +0 -53
- package/src/backends/mlx/continuation-persistence.ts +0 -107
- package/src/backends/mlx/continuation-request.ts +0 -43
- package/src/backends/mlx/continuation.ts +0 -77
- package/src/backends/mlx/diffusion.ts +0 -39
- package/src/backends/mlx/draft-target.ts +0 -32
- package/src/backends/mlx/encoder-cache.ts +0 -25
- package/src/backends/mlx/evaluation.ts +0 -105
- package/src/backends/mlx/fill-append.ts +0 -58
- package/src/backends/mlx/fill-group.ts +0 -497
- package/src/backends/mlx/full-kv-row-append.ts +0 -32
- package/src/backends/mlx/full-kv-row-donor.ts +0 -54
- package/src/backends/mlx/full-transitioning-kv-rows.ts +0 -65
- package/src/backends/mlx/gateway-binding.ts +0 -174
- package/src/backends/mlx/graph.ts +0 -38
- package/src/backends/mlx/kv-codec.ts +0 -9
- package/src/backends/mlx/kv-maintenance.ts +0 -161
- package/src/backends/mlx/kv-tensor-rows.ts +0 -212
- package/src/backends/mlx/memory-client.ts +0 -189
- package/src/backends/mlx/methods.ts +0 -90
- package/src/backends/mlx/mixed-iteration.ts +0 -62
- package/src/backends/mlx/model-prompt.ts +0 -178
- package/src/backends/mlx/model-serving.ts +0 -80
- package/src/backends/mlx/paged-cache-codec.ts +0 -27
- package/src/backends/mlx/prefill-cohort.ts +0 -100
- package/src/backends/mlx/prefill-policy.ts +0 -26
- package/src/backends/mlx/prefill-rows.ts +0 -244
- package/src/backends/mlx/prefill.ts +0 -33
- package/src/backends/mlx/projected-context-rows.ts +0 -72
- package/src/backends/mlx/prompt-input.ts +0 -29
- package/src/backends/mlx/qwen-prompt-input.ts +0 -40
- package/src/backends/mlx/request-policy.ts +0 -20
- package/src/backends/mlx/request-state-policy.ts +0 -54
- package/src/backends/mlx/rollback.ts +0 -92
- package/src/backends/mlx/row-sampling.ts +0 -49
- package/src/backends/mlx/serial-executor.ts +0 -251
- package/src/backends/mlx/speculative-group.ts +0 -438
- package/src/backends/mlx/speculative-round.ts +0 -161
- package/src/backends/mlx/speculative.ts +0 -131
- package/src/backends/mlx/state-rows.ts +0 -104
- package/src/backends/mlx/state-views.ts +0 -46
- package/src/backends/mlx/target-layout-capability.ts +0 -35
- package/src/backends/mlx/text-engine.ts +0 -73
- package/src/backends/mlx/transitioning-kv-rows.ts +0 -173
- package/src/chat-template.ts +0 -382
- package/src/cli.ts +0 -3490
- package/src/config.ts +0 -399
- package/src/contracts/completion.ts +0 -16
- package/src/contracts/execution.ts +0 -70
- package/src/contracts/generation.ts +0 -103
- package/src/contracts/host.ts +0 -7
- package/src/contracts/object-cache.ts +0 -8
- package/src/contracts/pi-provider.ts +0 -30
- package/src/contracts/pi.ts +0 -261
- package/src/contracts/prefix-cache.ts +0 -33
- package/src/contracts/resources.ts +0 -25
- package/src/contracts/sampling.ts +0 -23
- package/src/contracts/scheduling.ts +0 -54
- package/src/contracts/tsconfig.json +0 -14
- package/src/dataset/generators.ts +0 -1087
- package/src/dataset/index.ts +0 -12
- package/src/dataset/job.ts +0 -81
- package/src/dataset/llm.ts +0 -71
- package/src/dataset/registry.ts +0 -528
- package/src/diffusion/diffusion-generate.ts +0 -507
- package/src/download.ts +0 -505
- package/src/embed.ts +0 -77
- package/src/engine/admission.ts +0 -59
- package/src/engine/cancellation.ts +0 -38
- package/src/engine/engine.ts +0 -56
- package/src/engine/execution-coordinator.ts +0 -70
- package/src/engine/execution-plan.ts +0 -64
- package/src/engine/execution-tasks.ts +0 -44
- package/src/engine/namespaced-cache.ts +0 -13
- package/src/engine/resources.ts +0 -53
- package/src/engine/scheduler.ts +0 -92
- package/src/engine/session.ts +0 -247
- package/src/eval/README.md +0 -130
- package/src/eval/ifeval.ts +0 -81
- package/src/eval/kl-prompts.ts +0 -29
- package/src/eval/kl.ts +0 -590
- package/src/eval/perplexity.ts +0 -164
- package/src/eval/quality-db.ts +0 -92
- package/src/eval/reference-logits.ts +0 -207
- package/src/eval/runner.ts +0 -124
- package/src/eval/score.ts +0 -49
- package/src/eval/tasks/bfcl.ts +0 -535
- package/src/eval/tasks/gsm8k.ts +0 -98
- package/src/eval/tasks/hashhop.ts +0 -139
- package/src/eval/tasks/humaneval.ts +0 -275
- package/src/eval/tasks/ifeval.ts +0 -525
- package/src/eval/tasks/mmlu.ts +0 -157
- package/src/eval/ultrafeedback.ts +0 -60
- package/src/evaldb.ts +0 -199
- package/src/expert-io.ts +0 -393
- package/src/expert-offload-build.ts +0 -77
- package/src/expert-offload.ts +0 -76
- package/src/expert-residency.ts +0 -1134
- package/src/expert-usage.ts +0 -415
- package/src/fill/echo-index.ts +0 -188
- package/src/fill/fill-session.ts +0 -463
- package/src/fill/proposal.ts +0 -68
- package/src/fill/schema-rows.ts +0 -506
- package/src/fill/tool-boundary.ts +0 -103
- package/src/fit.ts +0 -249
- package/src/generate.ts +0 -1352
- package/src/grammar.ts +0 -604
- package/src/harness-pi.ts +0 -184
- package/src/hf-push.ts +0 -628
- package/src/hub-rest.ts +0 -242
- package/src/index.ts +0 -63
- package/src/inference/denoising.ts +0 -17
- package/src/inference/draft-acceptance.ts +0 -41
- package/src/inference/draft-output.ts +0 -49
- package/src/inference/graph.ts +0 -33
- package/src/inference/prefill.ts +0 -45
- package/src/inference/rollback.ts +0 -7
- package/src/jobs/index.ts +0 -31
- package/src/jobs/job-entry.ts +0 -107
- package/src/jobs/task-client.ts +0 -19
- package/src/jobs/types.ts +0 -2
- package/src/kv-scheme.ts +0 -279
- package/src/kv-store.ts +0 -1135
- package/src/lab/README.md +0 -22
- package/src/lab/curve/curve-designer.html +0 -303
- package/src/lab/curve/curve-sampler.ts +0 -122
- package/src/lab/expert-trace/expert-trace.ts +0 -69
- package/src/lab/paged-kv/paged-attention.ts +0 -117
- package/src/lab/paged-kv/paged-kv-rows.ts +0 -111
- package/src/lab/paged-kv/paged-kv.ts +0 -336
- package/src/library.ts +0 -40
- package/src/lora.ts +0 -408
- package/src/md-modules.d.ts +0 -54
- package/src/media-fetch.ts +0 -442
- package/src/memory/article.ts +0 -558
- package/src/memory/chunk-validation.ts +0 -150
- package/src/memory/chunk.ts +0 -318
- package/src/memory/cluster.ts +0 -351
- package/src/memory/coverage.ts +0 -134
- package/src/memory/crosslink.ts +0 -0
- package/src/memory/db.ts +0 -253
- package/src/memory/entity.ts +0 -203
- package/src/memory/gate.ts +0 -187
- package/src/memory/history.ts +0 -152
- package/src/memory/ingest.ts +0 -335
- package/src/memory/model.ts +0 -183
- package/src/memory/normalize.ts +0 -671
- package/src/memory/parse.ts +0 -22
- package/src/memory/pipeline.ts +0 -275
- package/src/memory/prompts.ts +0 -65
- package/src/memory/query.ts +0 -334
- package/src/memory/reconcile.ts +0 -734
- package/src/memory/redirect.ts +0 -108
- package/src/memory/reindex.ts +0 -325
- package/src/memory/resolve.ts +0 -307
- package/src/memory/rest.ts +0 -245
- package/src/memory/route.ts +0 -272
- package/src/memory/schedule.ts +0 -165
- package/src/memory/stages.ts +0 -777
- package/src/memory/synthesize.ts +0 -1638
- package/src/memory/tools.ts +0 -749
- package/src/memory/vault.ts +0 -1194
- package/src/memory/wikify.ts +0 -1064
- package/src/mlx/array.ts +0 -425
- package/src/mlx/autograd.ts +0 -304
- package/src/mlx/checkpoint.ts +0 -149
- package/src/mlx/compile.ts +0 -168
- package/src/mlx/custom-vjp.ts +0 -188
- package/src/mlx/ffi.ts +0 -531
- package/src/mlx/materialize.ts +0 -23
- package/src/mlx/metal-kernel.ts +0 -159
- package/src/mlx/normalized-argmax.ts +0 -116
- package/src/mlx/ops.ts +0 -1108
- package/src/mlx/quantized-rows.ts +0 -36
- package/src/mlx/token-bitmask.ts +0 -52
- package/src/mlx/turboquant-kv-decode.ts +0 -82
- package/src/mlx/turboquant-kv-inverse.ts +0 -122
- package/src/mlx/turboquant-ops.ts +0 -861
- package/src/mlx/turboquant-tables.ts +0 -237
- package/src/mmap.ts +0 -90
- package/src/model/artifact-identity.ts +0 -73
- package/src/model/batched-kv.ts +0 -235
- package/src/model/batched-mask.ts +0 -279
- package/src/model/batched-quant.ts +0 -212
- package/src/model/batched-quantized-kv.ts +0 -144
- package/src/model/batched-rotating-quant.ts +0 -353
- package/src/model/batched-rotating-state.ts +0 -149
- package/src/model/batched-rotating.ts +0 -338
- package/src/model/batched-row-storage.ts +0 -179
- package/src/model/batched-ssm.ts +0 -52
- package/src/model/batched-turboquant-kv.ts +0 -94
- package/src/model/compiled-decode.ts +0 -712
- package/src/model/decoded-kv-donor.ts +0 -12
- package/src/model/delayed-quantized-kv.ts +0 -72
- package/src/model/delayed-rotating-quantized-kv.ts +0 -93
- package/src/model/delayed-turboquant-kv.ts +0 -57
- package/src/model/diffusion-gemma.ts +0 -1026
- package/src/model/factory.ts +0 -215
- package/src/model/fingerprint.ts +0 -45
- package/src/model/flash-attention.ts +0 -694
- package/src/model/full-prefill-padding.ts +0 -56
- package/src/model/full-prefill-row.ts +0 -105
- package/src/model/gemma4-base.ts +0 -2370
- package/src/model/gemma4.ts +0 -1395
- package/src/model/generated/gemma4-12b.ts +0 -429
- package/src/model/generated/gemma4-26b.ts +0 -448
- package/src/model/generated/gemma4-e4b.ts +0 -717
- package/src/model/generated/index.ts +0 -18
- package/src/model/glm52-atlas.ts +0 -511
- package/src/model/glm52-cache.ts +0 -792
- package/src/model/glm52-config.ts +0 -262
- package/src/model/glm52-container.ts +0 -305
- package/src/model/glm52-coupling.ts +0 -388
- package/src/model/glm52-dsa.ts +0 -606
- package/src/model/glm52-expert-layout.ts +0 -253
- package/src/model/glm52-layout.ts +0 -152
- package/src/model/glm52-memory.ts +0 -500
- package/src/model/glm52-mla.ts +0 -792
- package/src/model/glm52-moe.ts +0 -223
- package/src/model/glm52-pilot.ts +0 -537
- package/src/model/glm52-reference.ts +0 -445
- package/src/model/glm52-residency.ts +0 -557
- package/src/model/glm52-resident-weights.ts +0 -135
- package/src/model/glm52-streamed-experts.ts +0 -453
- package/src/model/glm52-streamed-metal.ts +0 -610
- package/src/model/glm52-weights.ts +0 -503
- package/src/model/glm52.ts +0 -1132
- package/src/model/implementation.ts +0 -69
- package/src/model/kv-attention-view.ts +0 -70
- package/src/model/minicpm5.ts +0 -376
- package/src/model/profile.ts +0 -468
- package/src/model/quantized-append-attention.ts +0 -31
- package/src/model/qwen-append.ts +0 -12
- package/src/model/qwen-conv.ts +0 -52
- package/src/model/qwen3-delta-state.ts +0 -74
- package/src/model/qwen3-delta.ts +0 -451
- package/src/model/qwen3-moe.ts +0 -404
- package/src/model/qwen3-mrope.ts +0 -250
- package/src/model/qwen3.ts +0 -277
- package/src/model/qwen3_5-checkpoint.ts +0 -164
- package/src/model/qwen3_5.ts +0 -881
- package/src/model/rotating-kv-donor.ts +0 -35
- package/src/model/rotating-kv-layout.ts +0 -262
- package/src/model/rotating-row-transaction.ts +0 -35
- package/src/model/speculative-rotating-kv.ts +0 -101
- package/src/model/ssm-prefill-padding.ts +0 -39
- package/src/model/support.ts +0 -110
- package/src/model/token-groups.ts +0 -66
- package/src/model/trellis-balanced-scatter.ts +0 -51
- package/src/model/trellis-linear.ts +0 -672
- package/src/model/trellis-shared-m.ts +0 -151
- package/src/model/trellis-shared-scatter.ts +0 -144
- package/src/model/trellis-splitk-prefill.ts +0 -158
- package/src/model/trellis-tiled-prefill.ts +0 -136
- package/src/model/trellis-vector-expand.ts +0 -62
- package/src/model/trellis-wide-prefill.ts +0 -94
- package/src/model/turboquant-codec.ts +0 -89
- package/src/model/universal/archs.ts +0 -332
- package/src/model/universal/dense.ts +0 -612
- package/src/model/universal/modules.ts +0 -170
- package/src/model/universal/rope.ts +0 -381
- package/src/model/whisper.ts +0 -435
- package/src/native/expert_io.c +0 -523
- package/src/native/frame_extract.swift +0 -140
- package/src/native/mic_capture.swift +0 -92
- package/src/native-pack.ts +0 -163
- package/src/pi-provider.ts +0 -53
- package/src/pi-session.ts +0 -81
- package/src/pi-terminal.ts +0 -274
- package/src/pi-web.ts +0 -1940
- package/src/preflight.ts +0 -158
- package/src/prompt-cache.ts +0 -682
- package/src/quantize/allocator.ts +0 -627
- package/src/quantize/atomic-output.ts +0 -38
- package/src/quantize/calibration.ts +0 -181
- package/src/quantize/config-writer.ts +0 -145
- package/src/quantize/index.ts +0 -84
- package/src/quantize/job.ts +0 -162
- package/src/quantize/quantizer.ts +0 -666
- package/src/quantize/rotate.ts +0 -603
- package/src/quantize/safetensors-writer.ts +0 -249
- package/src/quantize/sensitivity.ts +0 -216
- package/src/quantize/trellis.ts +0 -586
- package/src/quantize/weight-transform.ts +0 -292
- package/src/registry.ts +0 -602
- package/src/runtime-config.ts +0 -77
- package/src/safetensors.ts +0 -165
- package/src/sampler.ts +0 -1012
- package/src/serve/admin-routes.ts +0 -184
- package/src/serve/assets/curve-terrain.html +0 -105
- package/src/serve/assets/training-inference-map.html +0 -575
- package/src/serve/audio-routes.ts +0 -323
- package/src/serve/aux-routes.ts +0 -132
- package/src/serve/batch-scheduler.ts +0 -8
- package/src/serve/checkpoint-identity.ts +0 -64
- package/src/serve/generation-gateway.ts +0 -537
- package/src/serve/isolate.ts +0 -609
- package/src/serve/kv-budget.ts +0 -14
- package/src/serve/lab-routes.ts +0 -341
- package/src/serve/lane-registry.ts +0 -45
- package/src/serve/model-admin-routes.ts +0 -155
- package/src/serve/model-binding.ts +0 -49
- package/src/serve/model-host.ts +0 -511
- package/src/serve/parent-application.ts +0 -56
- package/src/serve/prompt-response-trace.ts +0 -161
- package/src/serve/response-lease.ts +0 -24
- package/src/serve/session-search.ts +0 -326
- package/src/serve/static-routes.ts +0 -101
- package/src/serve/transcription-server.ts +0 -64
- package/src/serve/transcription-service.ts +0 -502
- package/src/server.ts +0 -1282
- package/src/spec/assistant-rows.ts +0 -93
- package/src/spec/assistant-source.ts +0 -126
- package/src/spec/cache-identity.ts +0 -22
- package/src/spec/deepspec-rows.ts +0 -29
- package/src/spec/deepspec-source.ts +0 -145
- package/src/spec/dflash-source.ts +0 -149
- package/src/spec/drafter.ts +0 -304
- package/src/spec/dspark/ab-stats.ts +0 -136
- package/src/spec/dspark/calibration.ts +0 -112
- package/src/spec/dspark/data-dflash.ts +0 -156
- package/src/spec/dspark/data.ts +0 -201
- package/src/spec/dspark/deepspec-module.ts +0 -776
- package/src/spec/dspark/generate-dflash.ts +0 -245
- package/src/spec/dspark/generate.ts +0 -290
- package/src/spec/dspark/loader.ts +0 -23
- package/src/spec/dspark/loss.ts +0 -213
- package/src/spec/dspark/module-dflash.ts +0 -560
- package/src/spec/dspark/module.ts +0 -540
- package/src/spec/dspark/quantize-drafter.ts +0 -91
- package/src/spec/dspark/sample.ts +0 -112
- package/src/spec/generate.ts +0 -276
- package/src/spec/glm52-mtp-graph.ts +0 -29
- package/src/spec/glm52-mtp-rows.ts +0 -132
- package/src/spec/glm52-mtp-source.ts +0 -318
- package/src/spec/glm52-mtp-state.ts +0 -33
- package/src/spec/ngram-source.ts +0 -226
- package/src/spec/projected-draft-rows.ts +0 -73
- package/src/spec/qwen-mtp-module.ts +0 -171
- package/src/spec/qwen-mtp-rows.ts +0 -230
- package/src/spec/qwen-mtp-source.ts +0 -386
- package/src/spec/qwen-mtp-state.ts +0 -30
- package/src/spec/serve-loop.ts +0 -587
- package/src/spec/source.ts +0 -228
- package/src/spec/two-model-rows.ts +0 -62
- package/src/spec/two-model.ts +0 -113
- package/src/ssd-cache.ts +0 -431
- package/src/ssd-durability.ts +0 -209
- package/src/storage/host-buffer.ts +0 -24
- package/src/storage/kv-writer.ts +0 -83
- package/src/storage/kv-writer.worker.js +0 -264
- package/src/storage/retention-policy.ts +0 -37
- package/src/tiered-prompt-cache.ts +0 -45
- package/src/tokenizer.ts +0 -335
- package/src/tool-approvals.ts +0 -131
- package/src/tool-call.ts +0 -695
- package/src/train/dataset.ts +0 -484
- package/src/train/diffusion-lora.ts +0 -216
- package/src/train/export.ts +0 -32
- package/src/train/flash-cce.ts +0 -1234
- package/src/train/forward.ts +0 -266
- package/src/train/fuse.ts +0 -243
- package/src/train/index.ts +0 -46
- package/src/train/job.ts +0 -205
- package/src/train/lora-params.ts +0 -254
- package/src/train/loss.ts +0 -1591
- package/src/train/merge.ts +0 -195
- package/src/train/optimizer.ts +0 -183
- package/src/train/prefix-shared.ts +0 -490
- package/src/train/rank.ts +0 -137
- package/src/train/segmented.ts +0 -1828
- package/src/train/steel-qmm-header.ts +0 -1461
- package/src/train/trainer.ts +0 -1221
- package/src/train/watch.ts +0 -542
- package/src/vision/diffusion-vision.ts +0 -620
- package/src/vision/embedder.ts +0 -176
- package/src/vision/preprocess.ts +0 -247
- package/src/vision/prompt.ts +0 -409
- package/src/vision/qwen3vl-preprocess.ts +0 -409
- package/src/vision/qwen3vl-prompt.ts +0 -233
- package/src/vision/qwen3vl-tower.ts +0 -566
- package/src/vision/siglip.ts +0 -607
- package/src/vision/video-frames.ts +0 -148
- package/src/web/app.html +0 -2155
- package/src/web/app.js +0 -5863
- package/src/web/skills/memory/SKILL.md +0 -120
- package/src/web/skills/web-research/SKILL.md +0 -46
- package/src/web/skills.ts +0 -97
- package/src/web/src/adapters-panel.ts +0 -288
- package/src/web/src/assistant.ts +0 -451
- package/src/web/src/chat.ts +0 -1060
- package/src/web/src/composer.ts +0 -962
- package/src/web/src/dataset.ts +0 -132
- package/src/web/src/finetune.ts +0 -267
- package/src/web/src/hub.ts +0 -428
- package/src/web/src/main.ts +0 -98
- package/src/web/src/markdown.ts +0 -0
- package/src/web/src/memory-panel.ts +0 -572
- package/src/web/src/model-picker.ts +0 -159
- package/src/web/src/palette.ts +0 -346
- package/src/web/src/protocol.ts +0 -26
- package/src/web/src/quantize.ts +0 -167
- package/src/web/src/rag.ts +0 -274
- package/src/web/src/sessions.ts +0 -469
- package/src/web/src/shell.ts +0 -804
- package/src/web/src/ui-catalog.ts +0 -78
- package/src/web/src/voice.ts +0 -141
- package/src/web/vendor/README.md +0 -61
- package/src/web-assets.ts +0 -47
- package/src/web-tools.ts +0 -478
- package/src/weights.ts +0 -255
- /package/src/{serve → server}/completion-sink.ts +0 -0
- /package/src/{serve → server}/generated-token-history.ts +0 -0
- /package/src/web/{icon.svg → public/icon.svg} +0 -0
- /package/src/web/{manifest.webmanifest → public/manifest.webmanifest} +0 -0
- /package/src/web/{vendor → public/vendor}/hljs-LICENSE +0 -0
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
// GENERATED by scripts/regen.ts turboquant — DO NOT EDIT.
|
|
2
|
-
// Regenerate: bun scripts/regen.ts turboquant
|
|
3
|
-
//
|
|
4
|
-
// Source: lab/repro/vllm-metal-turboquant/turboquant_reference.py (vendored
|
|
5
|
-
// vllm-metal oracle, see PROVENANCE.md in that directory), executed under
|
|
6
|
-
// the oracle venv (mlx 0.31.2) via gen_goldens.py.
|
|
7
|
-
//
|
|
8
|
-
// - SIGN_VECTORS[headDim]: the fwht() sign table (±1), derived from
|
|
9
|
-
// mx.random.randint(0, 2, shape=(headDim,), key=mx.random.key(42)) —
|
|
10
|
-
// deterministic across machines and spill/restore by construction.
|
|
11
|
-
// - LLOYD_MAX[bits]: Lloyd-Max (centroids, boundaries) for value
|
|
12
|
-
// quantization at the given bit width. bits=3 is the vendored reference's
|
|
13
|
-
// hardcoded fast-path table (CENTROIDS_3BIT/BOUNDARIES_3BIT); other widths
|
|
14
|
-
// are the reference's own iterative Lloyd-Max fit on a unit-normal source
|
|
15
|
-
// (mx.random.normal(key=mx.random.key(0)), 500 iterations), computed once
|
|
16
|
-
// here and hardcoded — no runtime iteration in the TS port.
|
|
17
|
-
|
|
18
|
-
export const TURBOQUANT_HEAD_DIMS = [64, 128, 256, 512] as const;
|
|
19
|
-
export type TurboQuantHeadDim = (typeof TURBOQUANT_HEAD_DIMS)[number];
|
|
20
|
-
|
|
21
|
-
export const SIGN_VECTORS: Record<TurboQuantHeadDim, number[]> = {
|
|
22
|
-
64: [
|
|
23
|
-
1, -1, -1, -1, 1, 1, 1, -1, 1, 1,
|
|
24
|
-
-1, 1, -1, 1, -1, 1, -1, 1, 1, -1,
|
|
25
|
-
1, -1, -1, 1, -1, -1, -1, 1, -1, -1,
|
|
26
|
-
1, -1, 1, -1, 1, 1, 1, 1, 1, 1,
|
|
27
|
-
1, 1, 1, -1, 1, 1, -1, 1, 1, -1,
|
|
28
|
-
-1, -1, -1, 1, -1, -1, 1, -1, 1, 1,
|
|
29
|
-
1, -1, -1, 1,
|
|
30
|
-
],
|
|
31
|
-
128: [
|
|
32
|
-
-1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
|
|
33
|
-
-1, -1, -1, 1, -1, -1, -1, 1, 1, -1,
|
|
34
|
-
-1, -1, 1, 1, 1, -1, 1, -1, 1, -1,
|
|
35
|
-
-1, 1, 1, 1, 1, 1, -1, 1, -1, 1,
|
|
36
|
-
1, -1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
37
|
-
-1, -1, -1, 1, 1, -1, 1, -1, 1, -1,
|
|
38
|
-
1, -1, -1, 1, -1, 1, -1, 1, -1, 1,
|
|
39
|
-
-1, -1, -1, -1, -1, 1, -1, -1, 1, 1,
|
|
40
|
-
-1, -1, 1, 1, 1, 1, 1, -1, 1, -1,
|
|
41
|
-
1, -1, 1, 1, -1, 1, 1, -1, -1, -1,
|
|
42
|
-
-1, 1, -1, -1, -1, 1, 1, -1, -1, 1,
|
|
43
|
-
-1, 1, 1, 1, 1, 1, 1, -1, -1, 1,
|
|
44
|
-
1, 1, -1, 1, -1, 1, 1, 1,
|
|
45
|
-
],
|
|
46
|
-
256: [
|
|
47
|
-
1, -1, 1, 1, -1, 1, 1, 1, 1, 1,
|
|
48
|
-
1, 1, -1, -1, 1, 1, 1, -1, -1, 1,
|
|
49
|
-
1, -1, -1, -1, 1, -1, 1, 1, 1, -1,
|
|
50
|
-
-1, 1, -1, 1, 1, 1, -1, 1, 1, 1,
|
|
51
|
-
1, -1, 1, -1, 1, 1, -1, 1, -1, 1,
|
|
52
|
-
-1, -1, 1, 1, -1, -1, 1, 1, 1, 1,
|
|
53
|
-
-1, 1, 1, 1, -1, -1, -1, -1, 1, -1,
|
|
54
|
-
-1, 1, 1, 1, -1, 1, -1, 1, 1, 1,
|
|
55
|
-
-1, 1, -1, -1, 1, -1, -1, 1, -1, 1,
|
|
56
|
-
1, -1, 1, -1, 1, -1, -1, 1, 1, -1,
|
|
57
|
-
1, 1, -1, -1, 1, -1, -1, -1, -1, 1,
|
|
58
|
-
-1, 1, 1, 1, -1, -1, -1, 1, 1, -1,
|
|
59
|
-
-1, -1, 1, 1, -1, 1, -1, -1, -1, -1,
|
|
60
|
-
1, -1, -1, 1, 1, -1, 1, -1, 1, 1,
|
|
61
|
-
-1, -1, -1, -1, -1, -1, -1, -1, 1, -1,
|
|
62
|
-
1, 1, 1, -1, 1, -1, -1, -1, 1, 1,
|
|
63
|
-
-1, -1, 1, 1, -1, -1, -1, 1, -1, 1,
|
|
64
|
-
-1, -1, -1, 1, -1, 1, -1, -1, -1, 1,
|
|
65
|
-
1, -1, 1, 1, 1, -1, 1, 1, -1, -1,
|
|
66
|
-
1, -1, -1, -1, 1, -1, 1, -1, -1, 1,
|
|
67
|
-
-1, 1, 1, -1, -1, 1, -1, -1, 1, 1,
|
|
68
|
-
1, 1, -1, -1, 1, 1, -1, -1, -1, 1,
|
|
69
|
-
-1, 1, -1, 1, -1, -1, -1, 1, -1, 1,
|
|
70
|
-
-1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
|
|
71
|
-
-1, -1, 1, -1, 1, 1, 1, -1, 1, -1,
|
|
72
|
-
-1, -1, -1, -1, -1, -1,
|
|
73
|
-
],
|
|
74
|
-
512: [
|
|
75
|
-
-1, 1, -1, -1, -1, -1, 1, -1, -1, -1,
|
|
76
|
-
1, -1, 1, 1, -1, 1, 1, -1, -1, 1,
|
|
77
|
-
-1, -1, 1, 1, 1, 1, 1, 1, -1, -1,
|
|
78
|
-
-1, 1, -1, 1, -1, -1, 1, 1, 1, 1,
|
|
79
|
-
-1, -1, 1, 1, 1, -1, -1, -1, -1, -1,
|
|
80
|
-
1, -1, -1, -1, -1, -1, 1, -1, 1, 1,
|
|
81
|
-
1, -1, -1, 1, -1, -1, 1, 1, 1, -1,
|
|
82
|
-
-1, 1, 1, 1, -1, 1, 1, -1, -1, -1,
|
|
83
|
-
-1, 1, 1, -1, -1, -1, 1, 1, 1, 1,
|
|
84
|
-
-1, 1, -1, 1, -1, -1, -1, 1, 1, 1,
|
|
85
|
-
1, -1, -1, 1, 1, -1, 1, 1, -1, 1,
|
|
86
|
-
-1, -1, 1, -1, 1, -1, 1, 1, -1, -1,
|
|
87
|
-
1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
|
|
88
|
-
-1, -1, 1, 1, 1, -1, 1, 1, -1, -1,
|
|
89
|
-
-1, -1, -1, 1, -1, -1, -1, 1, 1, 1,
|
|
90
|
-
-1, 1, -1, -1, 1, -1, -1, 1, 1, -1,
|
|
91
|
-
-1, -1, 1, 1, 1, 1, -1, 1, 1, -1,
|
|
92
|
-
1, 1, -1, 1, 1, 1, -1, 1, -1, -1,
|
|
93
|
-
-1, 1, -1, -1, -1, 1, 1, -1, 1, -1,
|
|
94
|
-
-1, -1, 1, 1, 1, 1, -1, -1, 1, 1,
|
|
95
|
-
1, -1, -1, -1, -1, 1, -1, -1, 1, -1,
|
|
96
|
-
1, -1, 1, -1, 1, 1, 1, 1, -1, 1,
|
|
97
|
-
-1, 1, -1, 1, 1, -1, 1, -1, -1, -1,
|
|
98
|
-
1, -1, 1, -1, 1, -1, 1, 1, -1, -1,
|
|
99
|
-
1, -1, 1, -1, -1, -1, 1, -1, -1, -1,
|
|
100
|
-
-1, 1, -1, -1, -1, 1, 1, 1, -1, 1,
|
|
101
|
-
1, 1, 1, -1, 1, -1, -1, -1, -1, -1,
|
|
102
|
-
1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
|
|
103
|
-
-1, 1, 1, 1, -1, 1, 1, 1, 1, 1,
|
|
104
|
-
1, -1, -1, 1, 1, 1, -1, 1, -1, 1,
|
|
105
|
-
-1, 1, 1, 1, 1, 1, -1, -1, 1, 1,
|
|
106
|
-
-1, 1, 1, -1, 1, -1, -1, 1, 1, -1,
|
|
107
|
-
-1, 1, -1, 1, 1, 1, -1, -1, -1, -1,
|
|
108
|
-
1, 1, 1, -1, 1, -1, 1, 1, -1, 1,
|
|
109
|
-
1, 1, 1, 1, 1, -1, 1, 1, -1, -1,
|
|
110
|
-
1, 1, -1, -1, -1, -1, -1, 1, 1, 1,
|
|
111
|
-
1, -1, 1, 1, -1, -1, -1, 1, -1, 1,
|
|
112
|
-
1, 1, 1, 1, 1, -1, -1, 1, -1, -1,
|
|
113
|
-
-1, 1, -1, 1, 1, -1, 1, 1, -1, 1,
|
|
114
|
-
-1, 1, 1, 1, -1, 1, -1, 1, 1, 1,
|
|
115
|
-
1, -1, 1, 1, 1, 1, 1, -1, 1, -1,
|
|
116
|
-
1, -1, 1, 1, 1, -1, 1, -1, 1, 1,
|
|
117
|
-
1, 1, -1, -1, -1, -1, 1, -1, -1, 1,
|
|
118
|
-
1, -1, -1, -1, -1, 1, 1, -1, 1, -1,
|
|
119
|
-
1, -1, -1, -1, 1, 1, 1, -1, -1, -1,
|
|
120
|
-
-1, -1, 1, -1, -1, -1, -1, -1, 1, -1,
|
|
121
|
-
1, 1, -1, 1, 1, -1, -1, -1, 1, 1,
|
|
122
|
-
1, 1, 1, -1, 1, 1, -1, 1, -1, 1,
|
|
123
|
-
-1, 1, -1, -1, 1, -1, -1, 1, -1, 1,
|
|
124
|
-
-1, -1, -1, 1, 1, -1, -1, -1, 1, -1,
|
|
125
|
-
1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
|
|
126
|
-
1, 1,
|
|
127
|
-
],
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
export const TURBOQUANT_VALUE_BITS = [2, 3, 4, 5, 8] as const;
|
|
131
|
-
export type TurboQuantValueBits = (typeof TURBOQUANT_VALUE_BITS)[number];
|
|
132
|
-
|
|
133
|
-
export interface LloydMaxTable {
|
|
134
|
-
centroids: number[];
|
|
135
|
-
boundaries: number[];
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export const LLOYD_MAX: Record<TurboQuantValueBits, LloydMaxTable> = {
|
|
139
|
-
2: {
|
|
140
|
-
centroids: [
|
|
141
|
-
-1.5144749879837036, -0.45822522044181824, 0.44863229990005493, 1.512152075767517,
|
|
142
|
-
],
|
|
143
|
-
boundaries: [
|
|
144
|
-
-0.9863501191139221, -0.004796460270881653, 0.9803922176361084,
|
|
145
|
-
],
|
|
146
|
-
},
|
|
147
|
-
3: {
|
|
148
|
-
centroids: [
|
|
149
|
-
-2.151949882507324, -1.3439099788665771, -0.7560099959373474, -0.24508999288082123, 0.24508999288082123, 0.7560099959373474, 1.3439099788665771, 2.151949882507324,
|
|
150
|
-
],
|
|
151
|
-
boundaries: [
|
|
152
|
-
-1.7479300498962402, -1.0499600172042847, -0.5005499720573425, 0, 0.5005499720573425, 1.0499600172042847, 1.7479300498962402,
|
|
153
|
-
],
|
|
154
|
-
},
|
|
155
|
-
4: {
|
|
156
|
-
centroids: [
|
|
157
|
-
-2.765662670135498, -2.0995230674743652, -1.6401931047439575, -1.27662992477417, -0.9602491855621338, -0.673130452632904, -0.4038185179233551, -0.14272712171077728, 0.11716290563344955, 0.3790881633758545,
|
|
158
|
-
0.6475776433944702, 0.9337777495384216, 1.2523075342178345, 1.6217625141143799, 2.0757482051849365, 2.7207279205322266,
|
|
159
|
-
],
|
|
160
|
-
boundaries: [
|
|
161
|
-
-2.4325928688049316, -1.8698580265045166, -1.458411455154419, -1.1184395551681519, -0.8166898488998413, -0.5384745001792908, -0.2732728123664856, -0.012782108038663864, 0.24812553822994232, 0.5133329033851624,
|
|
162
|
-
0.7906776666641235, 1.0930426120758057, 1.437035083770752, 1.8487553596496582, 2.398238182067871,
|
|
163
|
-
],
|
|
164
|
-
},
|
|
165
|
-
5: {
|
|
166
|
-
centroids: [
|
|
167
|
-
-3.3482043743133545, -2.7744688987731934, -2.3917815685272217, -2.0885653495788574, -1.8382762670516968, -1.6273508071899414, -1.436827301979065, -1.2599420547485352, -1.0958681106567383, -0.9411738514900208,
|
|
168
|
-
-0.7932286262512207, -0.6509044766426086, -0.5113211274147034, -0.37372007966041565, -0.23732160031795502, -0.10278966277837753, 0.031794287264347076, 0.16419854760169983, 0.3001006245613098, 0.4395425021648407,
|
|
169
|
-
0.5808256268501282, 0.7275802493095398, 0.8738965392112732, 1.029397964477539, 1.1942607164382935, 1.3728704452514648, 1.5676456689834595, 1.778881311416626, 2.0225136280059814, 2.3095157146453857,
|
|
170
|
-
2.666351556777954, 3.2329483032226562,
|
|
171
|
-
],
|
|
172
|
-
boundaries: [
|
|
173
|
-
-3.0613365173339844, -2.583125114440918, -2.24017333984375, -1.9634208679199219, -1.7328135967254639, -1.5320889949798584, -1.3483846187591553, -1.1779050827026367, -1.0185209512710571, -0.8672012090682983,
|
|
174
|
-
-0.7220665216445923, -0.581112802028656, -0.4425206184387207, -0.30552083253860474, -0.17005562782287598, -0.03549768775701523, 0.09799641370773315, 0.23214958608150482, 0.36982154846191406, 0.5101840496063232,
|
|
175
|
-
0.654202938079834, 0.8007383942604065, 0.9516472816467285, 1.1118292808532715, 1.2835655212402344, 1.4702579975128174, 1.6732635498046875, 1.9006974697113037, 2.1660146713256836, 2.48793363571167,
|
|
176
|
-
2.9496498107910156,
|
|
177
|
-
],
|
|
178
|
-
},
|
|
179
|
-
8: {
|
|
180
|
-
centroids: [
|
|
181
|
-
-4.505678653717041, -4.454618453979492, -4.436285972595215, -4.401589393615723, -4.363239765167236, -4.332197189331055, -4.2975006103515625, -4.26280403137207, -4.228107929229736, -4.193411827087402,
|
|
182
|
-
-4.15871524810791, -4.124018669128418, -4.094437599182129, -4.045758247375488, -4.002621173858643, -4.002171516418457, -3.942392110824585, -3.916801691055298, -3.8863255977630615, -3.8477535247802734,
|
|
183
|
-
-3.7988643646240234, -3.762794256210327, -3.7445151805877686, -3.706960439682007, -3.67462158203125, -3.6314854621887207, -3.5950119495391846, -3.580256938934326, -3.539952516555786, -3.5038421154022217,
|
|
184
|
-
-3.4671568870544434, -3.4281671047210693, -3.398128032684326, -3.363133192062378, -3.3274667263031006, -3.2919743061065674, -3.2577857971191406, -3.2200968265533447, -3.192981719970703, -3.157238006591797,
|
|
185
|
-
-3.111802577972412, -3.0821340084075928, -3.050640344619751, -3.0148110389709473, -2.980158805847168, -2.946451425552368, -2.9111905097961426, -2.8747212886810303, -2.8412270545959473, -2.8061022758483887,
|
|
186
|
-
-2.7716867923736572, -2.7376010417938232, -2.7029218673706055, -2.6634461879730225, -2.630377769470215, -2.5996179580688477, -2.560898780822754, -2.525275230407715, -2.4947545528411865, -2.461374044418335,
|
|
187
|
-
-2.424009323120117, -2.3877711296081543, -2.3505144119262695, -2.3163986206054688, -2.282374858856201, -2.243652820587158, -2.2097959518432617, -2.1770639419555664, -2.14471697807312, -2.1113693714141846,
|
|
188
|
-
-2.078673839569092, -2.044494390487671, -2.00639271736145, -1.969869613647461, -1.9300988912582397, -1.8931699991226196, -1.8589746952056885, -1.8212106227874756, -1.7848811149597168, -1.7465486526489258,
|
|
189
|
-
-1.7103406190872192, -1.6743196249008179, -1.6393754482269287, -1.6040517091751099, -1.5708216428756714, -1.5365480184555054, -1.5035663843154907, -1.469175100326538, -1.4350656270980835, -1.4022008180618286,
|
|
190
|
-
-1.3688998222351074, -1.3355891704559326, -1.3018449544906616, -1.2698367834091187, -1.235532283782959, -1.20119047164917, -1.1664694547653198, -1.1305049657821655, -1.0959333181381226, -1.0614327192306519,
|
|
191
|
-
-1.0274933576583862, -0.9940271377563477, -0.9609262347221375, -0.9273853898048401, -0.8929082751274109, -0.8593462705612183, -0.8261474967002869, -0.7937442660331726, -0.7588282823562622, -0.7230017185211182,
|
|
192
|
-
-0.6871297359466553, -0.6517745852470398, -0.6162516474723816, -0.5811514854431152, -0.5462668538093567, -0.5111281275749207, -0.4750610589981079, -0.4394458830356598, -0.404982328414917, -0.36981964111328125,
|
|
193
|
-
-0.334137886762619, -0.29919183254241943, -0.26465246081352234, -0.23168867826461792, -0.20013625919818878, -0.1672147512435913, -0.13413792848587036, -0.1003282368183136, -0.06571532040834427, -0.03132394701242447,
|
|
194
|
-
0.0043428270146250725, 0.04037246108055115, 0.07451506704092026, 0.1076153963804245, 0.14043505489826202, 0.17426857352256775, 0.20812825858592987, 0.2430444061756134, 0.27797263860702515, 0.31494686007499695,
|
|
195
|
-
0.35088568925857544, 0.38743987679481506, 0.4222983717918396, 0.4568648338317871, 0.49128350615501404, 0.5256633758544922, 0.5586227178573608, 0.5910427570343018, 0.6249552369117737, 0.6594945788383484,
|
|
196
|
-
0.6942835450172424, 0.7278474569320679, 0.7624561786651611, 0.7963849902153015, 0.8309887647628784, 0.8644483685493469, 0.8981063961982727, 0.9320245385169983, 0.96662437915802, 1.0006996393203735,
|
|
197
|
-
1.0343793630599976, 1.0680633783340454, 1.101860523223877, 1.135563611984253, 1.1707100868225098, 1.2038687467575073, 1.2388564348220825, 1.2744282484054565, 1.3102103471755981, 1.3441187143325806,
|
|
198
|
-
1.3769688606262207, 1.4091657400131226, 1.4424594640731812, 1.477047085762024, 1.5127589702606201, 1.5482075214385986, 1.58390474319458, 1.6193201541900635, 1.655400037765503, 1.6924738883972168,
|
|
199
|
-
1.7273176908493042, 1.7618744373321533, 1.7985631227493286, 1.8349602222442627, 1.871187686920166, 1.9062069654464722, 1.9427249431610107, 1.9807021617889404, 2.0164709091186523, 2.049800395965576,
|
|
200
|
-
2.083641529083252, 2.1192877292633057, 2.1556057929992676, 2.189804792404175, 2.2249133586883545, 2.257265329360962, 2.2943572998046875, 2.3308892250061035, 2.3646092414855957, 2.3981246948242188,
|
|
201
|
-
2.4336130619049072, 2.4672765731811523, 2.5033960342407227, 2.535766124725342, 2.5712199211120605, 2.606968879699707, 2.6403279304504395, 2.6730761528015137, 2.7094709873199463, 2.745285749435425,
|
|
202
|
-
2.7794594764709473, 2.8145666122436523, 2.851815938949585, 2.885251045227051, 2.919672727584839, 2.9549803733825684, 2.987560510635376, 3.021491289138794, 3.061659097671509, 3.102267026901245,
|
|
203
|
-
3.1455001831054688, 3.1724164485931396, 3.2052037715911865, 3.2377841472625732, 3.2674646377563477, 3.3014495372772217, 3.3345274925231934, 3.3695874214172363, 3.4068915843963623, 3.442040205001831,
|
|
204
|
-
3.4636645317077637, 3.5024828910827637, 3.5508108139038086, 3.5749337673187256, 3.604726791381836, 3.6401684284210205, 3.692674160003662, 3.7173542976379395, 3.7488019466400146, 3.7978620529174805,
|
|
205
|
-
3.8167099952697754, 3.856578826904297, 3.890836000442505, 3.9217264652252197, 3.974006175994873, 4.009003639221191, 4.029621601104736, 4.079561233520508, 4.099013805389404, 4.1337103843688965,
|
|
206
|
-
4.172705173492432, 4.198393821716309, 4.237799167633057, 4.272495746612549, 4.307192325592041, 4.341888427734375,
|
|
207
|
-
],
|
|
208
|
-
boundaries: [
|
|
209
|
-
-4.4801483154296875, -4.4454522132873535, -4.418937683105469, -4.382414817810059, -4.347718238830566, -4.314848899841309, -4.280152320861816, -4.245455741882324, -4.210760116577148, -4.176063537597656,
|
|
210
|
-
-4.141366958618164, -4.109228134155273, -4.070097923278809, -4.0241899490356445, -4.002396583557129, -3.9722819328308105, -3.9295969009399414, -3.9015636444091797, -3.867039680480957, -3.8233089447021484,
|
|
211
|
-
-3.780829429626465, -3.753654718399048, -3.7257378101348877, -3.690791130065918, -3.6530535221099854, -3.613248825073242, -3.587634563446045, -3.5601048469543457, -3.521897315979004, -3.485499382019043,
|
|
212
|
-
-3.447661876678467, -3.413147449493408, -3.3806304931640625, -3.3452999591827393, -3.309720516204834, -3.2748799324035645, -3.238941192626953, -3.2065391540527344, -3.17510986328125, -3.1345202922821045,
|
|
213
|
-
-3.096968173980713, -3.066387176513672, -3.0327258110046387, -2.9974849224090576, -2.9633049964904785, -2.928821086883545, -2.892955780029297, -2.857974052429199, -2.823664665222168, -2.7888946533203125,
|
|
214
|
-
-2.7546439170837402, -2.720261573791504, -2.6831841468811035, -2.646912097930908, -2.6149978637695312, -2.580258369445801, -2.5430870056152344, -2.5100150108337402, -2.4780642986297607, -2.4426918029785156,
|
|
215
|
-
-2.4058902263641357, -2.369142770767212, -2.333456516265869, -2.299386739730835, -2.2630138397216797, -2.22672438621521, -2.193429946899414, -2.160890579223633, -2.1280431747436523, -2.0950217247009277,
|
|
216
|
-
-2.061583995819092, -2.0254435539245605, -1.9881311655044556, -1.9499843120574951, -1.9116344451904297, -1.8760724067687988, -1.840092658996582, -1.8030458688735962, -1.7657148838043213, -1.7284445762634277,
|
|
217
|
-
-1.6923301219940186, -1.6568474769592285, -1.621713638305664, -1.5874366760253906, -1.5536848306655884, -1.520057201385498, -1.4863708019256592, -1.452120304107666, -1.418633222579956, -1.3855502605438232,
|
|
218
|
-
-1.35224449634552, -1.3187170028686523, -1.2858408689498901, -1.2526845932006836, -1.2183613777160645, -1.1838300228118896, -1.1484872102737427, -1.113219141960144, -1.0786830186843872, -1.044463038444519,
|
|
219
|
-
-1.0107603073120117, -0.9774767160415649, -0.9441558122634888, -0.9101468324661255, -0.8761272430419922, -0.8427468538284302, -0.8099458813667297, -0.776286244392395, -0.7409150004386902, -0.7050657272338867,
|
|
220
|
-
-0.6694521903991699, -0.6340131163597107, -0.5987015962600708, -0.5637091398239136, -0.5286974906921387, -0.4930945932865143, -0.45725345611572266, -0.4222140908241272, -0.3874009847640991, -0.35197877883911133,
|
|
221
|
-
-0.3166648745536804, -0.2819221615791321, -0.24817056953907013, -0.21591246128082275, -0.18367549777030945, -0.15067633986473083, -0.11723308265209198, -0.08302177488803864, -0.04851963371038437, -0.013490559533238411,
|
|
222
|
-
0.022357644513249397, 0.0574437640607357, 0.09106522798538208, 0.12402522563934326, 0.15735182166099548, 0.1911984086036682, 0.22558632493019104, 0.26050853729248047, 0.29645973443984985, 0.332916259765625,
|
|
223
|
-
0.36916279792785645, 0.4048691391944885, 0.43958160281181335, 0.47407418489456177, 0.5084734559059143, 0.5421430468559265, 0.5748327374458313, 0.6079989671707153, 0.642224907875061, 0.6768890619277954,
|
|
224
|
-
0.7110655307769775, 0.7451518177986145, 0.7794206142425537, 0.8136868476867676, 0.8477185964584351, 0.8812773823738098, 0.9150654673576355, 0.9493244886398315, 0.9836620092391968, 1.0175395011901855,
|
|
225
|
-
1.0512213706970215, 1.0849618911743164, 1.118712067604065, 1.1531368494033813, 1.1872894763946533, 1.221362590789795, 1.2566423416137695, 1.2923192977905273, 1.3271645307540894, 1.3605437278747559,
|
|
226
|
-
1.3930673599243164, 1.4258126020431519, 1.4597532749176025, 1.4949030876159668, 1.5304832458496094, 1.5660561323165894, 1.6016124486923218, 1.6373600959777832, 1.6739369630813599, 1.7098958492279053,
|
|
227
|
-
1.744596004486084, 1.7802188396453857, 1.8167617321014404, 1.8530739545822144, 1.8886973857879639, 1.9244658946990967, 1.9617135524749756, 1.9985865354537964, 2.0331356525421143, 2.066720962524414,
|
|
228
|
-
2.1014647483825684, 2.137446880340576, 2.1727051734924316, 2.2073590755462646, 2.241089344024658, 2.275811195373535, 2.3126232624053955, 2.3477492332458496, 2.3813669681549072, 2.4158687591552734,
|
|
229
|
-
2.4504446983337402, 2.4853363037109375, 2.5195810794830322, 2.553493022918701, 2.589094400405884, 2.6236484050750732, 2.6567020416259766, 2.6912736892700195, 2.7273783683776855, 2.7623724937438965,
|
|
230
|
-
2.7970130443573, 2.833191394805908, 2.8685336112976074, 2.9024620056152344, 2.937326431274414, 2.9712705612182617, 3.004525899887085, 3.0415751934051514, 3.081963062286377, 3.1238837242126465,
|
|
231
|
-
3.1589584350585938, 3.188810110092163, 3.22149395942688, 3.25262451171875, 3.284457206726074, 3.317988395690918, 3.352057456970215, 3.3882393836975098, 3.4244658946990967, 3.452852249145508,
|
|
232
|
-
3.4830737113952637, 3.526646852493286, 3.5628724098205566, 3.5898303985595703, 3.6224474906921387, 3.666421413421631, 3.705014228820801, 3.7330780029296875, 3.773332118988037, 3.807286024093628,
|
|
233
|
-
3.836644411087036, 3.8737072944641113, 3.9062812328338623, 3.947866439819336, 3.9915049076080322, 4.019312858581543, 4.054591178894043, 4.089287757873535, 4.11636209487915, 4.153207778930664,
|
|
234
|
-
4.185549736022949, 4.218096733093262, 4.255147457122803, 4.289844036102295, 4.324540138244629,
|
|
235
|
-
],
|
|
236
|
-
},
|
|
237
|
-
};
|
package/src/mmap.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
// libc mmap via bun:ffi.
|
|
2
|
-
//
|
|
3
|
-
// Bun.mmap panics (SIGTRAP, Bun 1.3.3) on files larger than 4 GB because
|
|
4
|
-
// JSC ArrayBuffers cap at 2^32 bytes — and weight shards routinely exceed
|
|
5
|
-
// that. We don't need a whole-file ArrayBuffer anyway: tensors go to mlx
|
|
6
|
-
// as raw pointers (base + offset), and only small ranges (headers) are
|
|
7
|
-
// ever viewed from JS.
|
|
8
|
-
|
|
9
|
-
import { dlopen, FFIType, toArrayBuffer } from "bun:ffi";
|
|
10
|
-
import { closeSync, fstatSync, openSync } from "node:fs";
|
|
11
|
-
|
|
12
|
-
const libc = dlopen("/usr/lib/libSystem.B.dylib", {
|
|
13
|
-
mmap: {
|
|
14
|
-
args: [FFIType.u64, FFIType.u64, FFIType.i32, FFIType.i32, FFIType.i32, FFIType.i64],
|
|
15
|
-
returns: FFIType.u64,
|
|
16
|
-
},
|
|
17
|
-
munmap: { args: [FFIType.u64, FFIType.u64], returns: FFIType.i32 },
|
|
18
|
-
madvise: { args: [FFIType.u64, FFIType.u64, FFIType.i32], returns: FFIType.i32 },
|
|
19
|
-
}).symbols;
|
|
20
|
-
|
|
21
|
-
const PROT_READ = 0x1;
|
|
22
|
-
const PROT_WRITE = 0x2;
|
|
23
|
-
const MAP_SHARED = 0x1;
|
|
24
|
-
const MAP_PRIVATE = 0x2;
|
|
25
|
-
const MAP_FAILED = 0xffffffffffffffffn;
|
|
26
|
-
|
|
27
|
-
// madvise advice (macOS <sys/mman.h>). DONTNEED drops clean file-backed
|
|
28
|
-
// pages (they re-fault from the file); FREE_REUSABLE is the Apple-specific
|
|
29
|
-
// lazy-reclaim variant.
|
|
30
|
-
export const MADV_DONTNEED = 4;
|
|
31
|
-
export const MADV_FREE = 5;
|
|
32
|
-
export const MADV_FREE_REUSABLE = 7;
|
|
33
|
-
|
|
34
|
-
export class MmapFile {
|
|
35
|
-
readonly path: string;
|
|
36
|
-
readonly base: bigint;
|
|
37
|
-
readonly size: number;
|
|
38
|
-
#mapped = true;
|
|
39
|
-
|
|
40
|
-
private constructor(path: string, base: bigint, size: number) {
|
|
41
|
-
this.path = path;
|
|
42
|
-
this.base = base;
|
|
43
|
-
this.size = size;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** Open read-only (MAP_SHARED) or copy-on-write (MAP_PRIVATE +
|
|
47
|
-
* PROT_WRITE — writes go to private pages, never the file; used for
|
|
48
|
-
* KV-cache reloads where mlx may donate buffers). */
|
|
49
|
-
static open(path: string, mode: "ro" | "cow" = "ro"): MmapFile {
|
|
50
|
-
const fd = openSync(path, "r");
|
|
51
|
-
try {
|
|
52
|
-
const size = fstatSync(fd).size;
|
|
53
|
-
if (size === 0) throw new Error(`${path}: empty file`);
|
|
54
|
-
const prot = mode === "cow" ? PROT_READ | PROT_WRITE : PROT_READ;
|
|
55
|
-
const flags = mode === "cow" ? MAP_PRIVATE : MAP_SHARED;
|
|
56
|
-
const base = libc.mmap(0n, BigInt(size), prot, flags, fd, 0n);
|
|
57
|
-
if (base === MAP_FAILED) throw new Error(`${path}: mmap failed`);
|
|
58
|
-
return new MmapFile(path, base, size);
|
|
59
|
-
} finally {
|
|
60
|
-
closeSync(fd); // mapping survives close
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Raw pointer to byte `offset` (for FFI consumers like mlx). */
|
|
65
|
-
pointer(offset: number): number {
|
|
66
|
-
if (offset < 0 || offset > this.size) throw new RangeError(`offset ${offset} out of range`);
|
|
67
|
-
return Number(this.base + BigInt(offset));
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** JS view of a range. Only for small ranges (headers, spot checks);
|
|
71
|
-
* the view aliases the mapping — do not retain past munmap. */
|
|
72
|
-
view(offset: number, length: number): Uint8Array {
|
|
73
|
-
if (offset + length > this.size) throw new RangeError("view out of range");
|
|
74
|
-
if (length >= 2 ** 32) throw new RangeError("JS views cap at 4 GB");
|
|
75
|
-
return new Uint8Array(toArrayBuffer(this.pointer(offset) as never, 0, length));
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** madvise a page-aligned sub-range (offset + length should be page
|
|
79
|
-
* multiples). e.g. advise(off, len, MADV_DONTNEED) to release clean
|
|
80
|
-
* expert pages back to the OS; they re-fault from the file on next read. */
|
|
81
|
-
advise(offset: number, length: number, advice: number): number {
|
|
82
|
-
return Number(libc.madvise(this.base + BigInt(offset), BigInt(length), advice));
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
unmap(): void {
|
|
86
|
-
if (!this.#mapped) return;
|
|
87
|
-
this.#mapped = false;
|
|
88
|
-
libc.munmap(this.base, BigInt(this.size));
|
|
89
|
-
}
|
|
90
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
-
import { mkdir, readFile, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
-
import { homedir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
|
|
6
|
-
/** Identity calculation owns bytes; this interface owns memo persistence. */
|
|
7
|
-
export interface ArtifactIdentityStore {
|
|
8
|
-
get(key: string): Promise<string | undefined>;
|
|
9
|
-
put(key: string, digest: string): void;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export class FileArtifactIdentityStore implements ArtifactIdentityStore {
|
|
13
|
-
readonly #memory = new Map<string, string>();
|
|
14
|
-
readonly #pending = new Set<Promise<void>>();
|
|
15
|
-
constructor(readonly directory: string) {}
|
|
16
|
-
|
|
17
|
-
async get(key: string): Promise<string | undefined> {
|
|
18
|
-
const memory = this.#memory.get(key);
|
|
19
|
-
if (memory) return memory;
|
|
20
|
-
try {
|
|
21
|
-
const digest = await readFile(join(this.directory, key), "utf8");
|
|
22
|
-
if (!/^[0-9a-f]{64}$/.test(digest)) return undefined;
|
|
23
|
-
this.#memory.set(key, digest);
|
|
24
|
-
return digest;
|
|
25
|
-
} catch { return undefined; }
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
put(key: string, digest: string): void {
|
|
29
|
-
this.#memory.set(key, digest);
|
|
30
|
-
const temporary = join(this.directory, `${key}.${process.pid}.${randomUUID()}.tmp`);
|
|
31
|
-
const write = async () => {
|
|
32
|
-
try {
|
|
33
|
-
await mkdir(this.directory, { recursive: true });
|
|
34
|
-
await writeFile(temporary, digest);
|
|
35
|
-
await rename(temporary, join(this.directory, key));
|
|
36
|
-
} catch { await rm(temporary, { force: true }).catch(() => {}); }
|
|
37
|
-
};
|
|
38
|
-
const pending = write().finally(() => this.#pending.delete(pending));
|
|
39
|
-
this.#pending.add(pending);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async flush(): Promise<void> { await Promise.all(this.#pending); }
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const defaultStore = new FileArtifactIdentityStore(join(homedir(), ".cache", "mlx-bun", "artifact-identities"));
|
|
46
|
-
export interface ArtifactIdentityFile { name: string; path: string; }
|
|
47
|
-
|
|
48
|
-
async function revision(files: readonly ArtifactIdentityFile[]): Promise<string> {
|
|
49
|
-
return JSON.stringify(await Promise.all(files.map(async file => {
|
|
50
|
-
const path = await realpath(file.path), info = await stat(path, { bigint: true });
|
|
51
|
-
return [file.name, path, info.dev, info.ino, info.size, info.mtimeNs, info.ctimeNs].map(String);
|
|
52
|
-
})));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Preserve the existing seed/name/byte digest, reusing it while file revisions
|
|
56
|
-
* are unchanged. ctime catches same-size edits even when mtime is restored.
|
|
57
|
-
* A memo miss computes the full identity; persistence never delays its caller. */
|
|
58
|
-
export async function artifactIdentity(seed: string, input: readonly ArtifactIdentityFile[],
|
|
59
|
-
store: ArtifactIdentityStore = defaultStore): Promise<string> {
|
|
60
|
-
const files = [...input].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
61
|
-
const before = await revision(files);
|
|
62
|
-
const key = createHash("sha256").update(JSON.stringify([seed, before])).digest("hex");
|
|
63
|
-
const cached = await store.get(key);
|
|
64
|
-
if (cached) return cached;
|
|
65
|
-
const digest = createHash("sha256").update(seed);
|
|
66
|
-
for (const file of files) {
|
|
67
|
-
digest.update(file.name);
|
|
68
|
-
for await (const chunk of Bun.file(file.path).stream()) digest.update(chunk);
|
|
69
|
-
}
|
|
70
|
-
const value = digest.digest("hex");
|
|
71
|
-
if (before === await revision(files)) store.put(key, value);
|
|
72
|
-
return value;
|
|
73
|
-
}
|
package/src/model/batched-kv.ts
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import { MlxArray } from "../mlx/array";
|
|
2
|
-
import { Dtype } from "../mlx/ffi";
|
|
3
|
-
import * as ops from "../mlx/ops";
|
|
4
|
-
import { materializeCopy } from "../mlx/materialize";
|
|
5
|
-
import { KVCache, isPlainKvCache, type BatchableCache, type Cache, type Mask, type PaddedPrefillCache, type PrefillPadding } from "./gemma4-base";
|
|
6
|
-
import { FullPrefillPadding } from "./full-prefill-padding";
|
|
7
|
-
|
|
8
|
-
/** Full-attention storage with independently advancing row positions.
|
|
9
|
-
* Speculative rollback changes coverage, without moving the retained KV.
|
|
10
|
-
* Subsequent appends overwrite each row's rejected suffix in place when MLX
|
|
11
|
-
* can donate the buffer. Queue membership is outside this layout. */
|
|
12
|
-
export class BatchedKVCache implements BatchableCache, PaddedPrefillCache {
|
|
13
|
-
keys: MlxArray | null = null;
|
|
14
|
-
values: MlxArray | null = null;
|
|
15
|
-
rowOffsets: number[] = [];
|
|
16
|
-
leftPad: number[] = [];
|
|
17
|
-
#rope: MlxArray | undefined;
|
|
18
|
-
#beforeRound: number[] | undefined;
|
|
19
|
-
readonly #padding = new FullPrefillPadding();
|
|
20
|
-
|
|
21
|
-
restorePrefillEnds(ends: readonly number[] | undefined): void { this.#padding.restoreEnds(ends); }
|
|
22
|
-
preparePrefill(padding: PrefillPadding): void {
|
|
23
|
-
this.#padding.prepare(this, padding); this.#positionsChanged();
|
|
24
|
-
}
|
|
25
|
-
finalizePrefill(): void {
|
|
26
|
-
const next = this.#padding.finalize(this.state(), this);
|
|
27
|
-
if (!next) return;
|
|
28
|
-
this.keys?.dispose(); this.values?.dispose();
|
|
29
|
-
this.keys = next[0] ?? null; this.values = next[1] ?? null;
|
|
30
|
-
this.#positionsChanged();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
captureDonorRows(): import("./gemma4-base").KvDonorRows {
|
|
34
|
-
const B = this.rowOffsets.length, width = this.offset;
|
|
35
|
-
const keys = this.keys!.slice([0,0,0,0], [B,this.keys!.shape[1]!,width,this.keys!.shape[3]!]);
|
|
36
|
-
let values: MlxArray;
|
|
37
|
-
try { values = this.values!.slice([0,0,0,0], [B,this.values!.shape[1]!,width,this.values!.shape[3]!]); }
|
|
38
|
-
catch (error) { keys.dispose(); throw error; }
|
|
39
|
-
return { keys, values, offsets: [...this.rowOffsets], starts: [...this.leftPad], ends: this.#ends() };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
signature(): string { return "kv:batched-positions"; }
|
|
43
|
-
get batchSize(): number | null { return this.rowOffsets.length || null; }
|
|
44
|
-
get offset(): number { return Math.max(0, ...this.#ends()); }
|
|
45
|
-
#ends(): number[] { return this.rowOffsets.map((offset, row) => offset + this.leftPad[row]!); }
|
|
46
|
-
get ropeOffsetArr(): MlxArray | undefined {
|
|
47
|
-
// A common unpadded position uses the existing scalar RoPE operation.
|
|
48
|
-
if (this.leftPad.every(pad => pad === 0) && this.rowOffsets.every(offset => offset === this.rowOffsets[0])) return undefined;
|
|
49
|
-
return this.#rope ??= ops.fromInt32(this.rowOffsets, [this.rowOffsets.length]);
|
|
50
|
-
}
|
|
51
|
-
#positionsChanged(): void { this.#rope?.dispose(); this.#rope = undefined; }
|
|
52
|
-
makeEmptyBatch(): BatchedKVCache { return new BatchedKVCache(); }
|
|
53
|
-
bytesPerToken(): number {
|
|
54
|
-
if (!this.keys || !this.values) return 0;
|
|
55
|
-
return (this.keys.nbytes + this.values.nbytes) / (this.rowOffsets.length * this.keys.shape[2]!);
|
|
56
|
-
}
|
|
57
|
-
projectedBytes(tokens: number): number { return this.bytesPerToken() * tokens; }
|
|
58
|
-
state(): MlxArray[] { return this.keys && this.values ? [this.keys, this.values] : []; }
|
|
59
|
-
isTrimmable(): boolean { return true; }
|
|
60
|
-
trim(count: number): void {
|
|
61
|
-
this.rowOffsets = this.rowOffsets.map(offset => offset - count);
|
|
62
|
-
this.#positionsChanged();
|
|
63
|
-
}
|
|
64
|
-
specRoundBegin(): void { this.#beforeRound = [...this.rowOffsets]; }
|
|
65
|
-
specRoundCommit(): void { this.#beforeRound = undefined; }
|
|
66
|
-
specRoundRollback(keep: number | readonly number[]): void {
|
|
67
|
-
this.rowOffsets = this.#beforeRound!.map((offset, row) => offset + (typeof keep === "number" ? keep : keep[row]!));
|
|
68
|
-
this.#beforeRound = undefined;
|
|
69
|
-
this.#positionsChanged();
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
makeMask(N: number, window: number | null): Mask {
|
|
73
|
-
const B = this.rowOffsets.length, ends = this.#ends(), S = this.offset + N;
|
|
74
|
-
if (window === null && this.leftPad.every(pad => pad === 0) && ends.every(end => end === ends[0]))
|
|
75
|
-
return { mode: N === 1 ? "" : "causal", arr: null };
|
|
76
|
-
using starts = ops.fromInt32(ends, [B, 1, 1, 1]);
|
|
77
|
-
using relative = ops.arange(0, N, 1, Dtype.int32);
|
|
78
|
-
using queries = ops.reshape(relative, [1, 1, N, 1]);
|
|
79
|
-
using position = ops.add(starts, queries);
|
|
80
|
-
using indices = ops.arange(0, S, 1, Dtype.int32);
|
|
81
|
-
using keys = ops.reshape(indices, [1, 1, 1, S]);
|
|
82
|
-
using pads = ops.fromInt32(this.leftPad, [B, 1, 1, 1]);
|
|
83
|
-
using causal = ops.lessEqual(keys, position);
|
|
84
|
-
using valid = ops.greaterEqual(keys, pads);
|
|
85
|
-
const mask = ops.logicalAnd(causal, valid);
|
|
86
|
-
if (window === null) return { mode: "array", arr: mask };
|
|
87
|
-
using windowSize = ops.fromInt32([window], []);
|
|
88
|
-
using limit = ops.add(keys, windowSize);
|
|
89
|
-
using within = ops.less(position, limit);
|
|
90
|
-
try { return { mode: "array", arr: ops.logicalAnd(mask, within) }; }
|
|
91
|
-
finally { mask.dispose(); }
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
updateAndFetch(k: MlxArray, v: MlxArray): [MlxArray, MlxArray] {
|
|
95
|
-
const B = this.rowOffsets.length, N = k.shape[2]!, ends = this.#ends();
|
|
96
|
-
const needed = Math.max(...ends) + N;
|
|
97
|
-
const grow = (array: MlxArray) => {
|
|
98
|
-
const [rows, heads, capacity, width] = array.shape as [number, number, number, number];
|
|
99
|
-
if (capacity >= needed) return array;
|
|
100
|
-
using padding = ops.zeros([rows, heads, Math.ceil(needed / 256) * 256 - capacity, width], array.dtype);
|
|
101
|
-
const grown = ops.concatAxis([array, padding], 2);
|
|
102
|
-
array.dispose();
|
|
103
|
-
return grown;
|
|
104
|
-
};
|
|
105
|
-
const allocate = (input: MlxArray) => ops.zeros(
|
|
106
|
-
[B, input.shape[1]!, Math.ceil(needed / 256) * 256, input.shape[3]!], input.dtype);
|
|
107
|
-
this.keys = this.keys ? grow(this.keys) : allocate(k);
|
|
108
|
-
this.values = this.values ? grow(this.values) : allocate(v);
|
|
109
|
-
const common = ends.every(end => end === ends[0]);
|
|
110
|
-
using positions = common ? null : ops.fromInt32(ends.flatMap(end =>
|
|
111
|
-
Array.from({ length: N }, (_, position) => end + position)), [B, 1, N, 1]);
|
|
112
|
-
const append = (array: MlxArray, values: MlxArray) => common
|
|
113
|
-
? ops.sliceUpdate(array, values, [0, 0, ends[0]!, 0], [B, array.shape[1]!, ends[0]! + N, array.shape[3]!])
|
|
114
|
-
: ops.putAlongAxis(array, positions!, values, 2);
|
|
115
|
-
const keys = append(this.keys, k);
|
|
116
|
-
let values: MlxArray;
|
|
117
|
-
try { values = append(this.values, v); }
|
|
118
|
-
catch (error) { keys.dispose(); throw error; }
|
|
119
|
-
this.keys.dispose(); this.values.dispose();
|
|
120
|
-
this.keys = keys; this.values = values;
|
|
121
|
-
this.rowOffsets = this.rowOffsets.map(offset => offset + N);
|
|
122
|
-
this.#positionsChanged();
|
|
123
|
-
return [keys.slice([0, 0, 0, 0], [B, keys.shape[1]!, needed, keys.shape[3]!]),
|
|
124
|
-
values.slice([0, 0, 0, 0], [B, values.shape[1]!, needed, values.shape[3]!])];
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
mergeRows(rows: readonly Cache[]): void {
|
|
128
|
-
const only = rows.length === 1 ? rows[0] : undefined;
|
|
129
|
-
if (only && (only instanceof BatchedKVCache || isPlainKvCache(only))) {
|
|
130
|
-
// Preserve allocated capacity when a request starts alone. Immutable
|
|
131
|
-
// aliases transfer ownership without copying its prefill storage.
|
|
132
|
-
const keys = only.keys ? ops.copyOf(only.keys) : null;
|
|
133
|
-
let values: MlxArray | null;
|
|
134
|
-
try { values = only.values ? ops.copyOf(only.values) : null; }
|
|
135
|
-
catch (error) { keys?.dispose(); throw error; }
|
|
136
|
-
const offsets = only instanceof BatchedKVCache ? [...only.rowOffsets] : [only.offset];
|
|
137
|
-
const pads = only instanceof BatchedKVCache ? [...only.leftPad] : [0];
|
|
138
|
-
this.dispose(); this.keys = keys; this.values = values;
|
|
139
|
-
this.rowOffsets = offsets; this.leftPad = pads;
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
const chunks: Array<{ keys: MlxArray | null; values: MlxArray | null; offsets: number[]; pads: number[]; width: number }> = [];
|
|
143
|
-
const held: MlxArray[] = [];
|
|
144
|
-
try {
|
|
145
|
-
for (const cache of rows) {
|
|
146
|
-
let keys: MlxArray | null, values: MlxArray | null, offsets: number[], pads: number[];
|
|
147
|
-
if (cache instanceof BatchedKVCache && cache.offset === 0) {
|
|
148
|
-
keys = values = null; offsets = [...cache.rowOffsets]; pads = [...cache.leftPad];
|
|
149
|
-
} else if (cache instanceof BatchedKVCache) {
|
|
150
|
-
const B = cache.rowOffsets.length, width = cache.offset;
|
|
151
|
-
keys = cache.keys!.slice([0, 0, 0, 0], [B, cache.keys!.shape[1]!, width, cache.keys!.shape[3]!]);
|
|
152
|
-
held.push(keys);
|
|
153
|
-
values = cache.values!.slice([0, 0, 0, 0], [B, cache.values!.shape[1]!, width, cache.values!.shape[3]!]);
|
|
154
|
-
held.push(values);
|
|
155
|
-
offsets = [...cache.rowOffsets]; pads = [...cache.leftPad];
|
|
156
|
-
} else if (isPlainKvCache(cache)) {
|
|
157
|
-
if (cache.offset === 0) keys = values = null;
|
|
158
|
-
else { [keys, values] = cache.temporalView(); held.push(keys, values); }
|
|
159
|
-
offsets = [cache.offset]; pads = [0];
|
|
160
|
-
} else throw new Error(`full-attention batch cannot merge ${cache.signature()}`);
|
|
161
|
-
chunks.push({ keys, values, offsets, pads, width: keys?.shape[2] ?? 0 });
|
|
162
|
-
}
|
|
163
|
-
const width = Math.max(0, ...chunks.map(chunk => chunk.width));
|
|
164
|
-
const padded = (array: MlxArray, count: number) => {
|
|
165
|
-
if (!count) return array;
|
|
166
|
-
using padding = ops.zeros([array.shape[0]!, array.shape[1]!, count, array.shape[3]!], array.dtype);
|
|
167
|
-
const result = ops.concatAxis([padding, array], 2); held.push(result); return result;
|
|
168
|
-
};
|
|
169
|
-
const merge = (field: "keys" | "values") => {
|
|
170
|
-
if (!width) return null; // Geometry arrives with the first append.
|
|
171
|
-
const prototype = chunks.find(chunk => chunk[field])![field]!;
|
|
172
|
-
return ops.concatAxis(chunks.map(chunk => {
|
|
173
|
-
const array = chunk[field];
|
|
174
|
-
if (array) return padded(array, width - chunk.width);
|
|
175
|
-
const empty = ops.zeros([chunk.offsets.length, prototype.shape[1]!, width, prototype.shape[3]!], prototype.dtype);
|
|
176
|
-
held.push(empty); return empty;
|
|
177
|
-
}), 0);
|
|
178
|
-
};
|
|
179
|
-
const keys = merge("keys");
|
|
180
|
-
let values: MlxArray | null;
|
|
181
|
-
try { values = merge("values"); }
|
|
182
|
-
catch (error) { keys?.dispose(); throw error; }
|
|
183
|
-
this.dispose();
|
|
184
|
-
this.keys = keys; this.values = values;
|
|
185
|
-
this.rowOffsets = chunks.flatMap(chunk => chunk.offsets);
|
|
186
|
-
this.leftPad = chunks.flatMap(chunk => chunk.pads.map(pad => pad + width - chunk.width));
|
|
187
|
-
} finally { for (const array of held) array.dispose(); }
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
filterRows(rows: readonly number[]): void {
|
|
191
|
-
if (!rows.length) { this.dispose(); return; }
|
|
192
|
-
const pads = rows.map(row => this.leftPad[row]!);
|
|
193
|
-
const sharedPadding = Math.min(...pads);
|
|
194
|
-
using indices = this.keys ? ops.fromInt32([...rows], [rows.length]) : null;
|
|
195
|
-
const select = (array: MlxArray | null): MlxArray | null => {
|
|
196
|
-
if (!array) return null;
|
|
197
|
-
const selected = ops.takeAxis(array, indices!, 0);
|
|
198
|
-
if (!sharedPadding) return selected;
|
|
199
|
-
// mlx-lm BatchKVCache.filter removes padding common to all survivors.
|
|
200
|
-
// Keeping it changes attention reduction geometry after retirement.
|
|
201
|
-
try { return selected.slice([0, 0, sharedPadding, 0], [...selected.shape]); }
|
|
202
|
-
finally { selected.dispose(); }
|
|
203
|
-
};
|
|
204
|
-
const keys = select(this.keys);
|
|
205
|
-
let values: MlxArray | null;
|
|
206
|
-
try { values = select(this.values); }
|
|
207
|
-
catch (error) { keys?.dispose(); throw error; }
|
|
208
|
-
this.keys?.dispose(); this.values?.dispose();
|
|
209
|
-
this.keys = keys; this.values = values;
|
|
210
|
-
this.rowOffsets = rows.map(row => this.rowOffsets[row]!);
|
|
211
|
-
this.leftPad = pads.map(pad => pad - sharedPadding);
|
|
212
|
-
this.#padding.filter(rows);
|
|
213
|
-
this.#positionsChanged();
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
extractRow(row: number): KVCache {
|
|
217
|
-
const start = this.leftPad[row]!, count = this.rowOffsets[row]!;
|
|
218
|
-
if (count === 0) return new KVCache();
|
|
219
|
-
const copy = (array: MlxArray) => {
|
|
220
|
-
using view = array.slice([row, 0, start, 0], [row + 1, array.shape[1]!, start + count, array.shape[3]!]);
|
|
221
|
-
return materializeCopy(view);
|
|
222
|
-
};
|
|
223
|
-
const keys = copy(this.keys!);
|
|
224
|
-
let values: MlxArray;
|
|
225
|
-
try { values = copy(this.values!); }
|
|
226
|
-
catch (error) { keys.dispose(); throw error; }
|
|
227
|
-
const cache = new KVCache(); cache.restoreState(keys, values, count); return cache;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
dispose(): void {
|
|
231
|
-
this.keys?.dispose(); this.values?.dispose(); this.#positionsChanged();
|
|
232
|
-
this.keys = this.values = null;
|
|
233
|
-
this.rowOffsets = []; this.leftPad = []; this.#beforeRound = undefined; this.#padding.clear();
|
|
234
|
-
}
|
|
235
|
-
}
|