@mlx-bun/inference 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1475 -2
- package/THIRD_PARTY_NOTICES.md +3404 -0
- package/dist/native/libmlx_bun_ane.dylib +0 -0
- package/dist/native/libmlx_bun_expert_io.dylib +0 -0
- package/dist/native/mlx-bun-frame-extract +0 -0
- package/examples/fit-model.ts +25 -0
- package/examples/qwen3-forward.ts +42 -0
- package/examples/qwen3-generate.ts +28 -0
- package/examples/trellis-expand.ts +23 -0
- package/package.json +129 -4
- package/src/adapters/available.ts +110 -0
- package/src/adapters/index.ts +3 -0
- package/src/adapters/manager.ts +316 -0
- package/src/artifacts/auxiliary-files.ts +45 -0
- package/src/artifacts/config-dialects.ts +94 -0
- package/src/artifacts/config.ts +341 -0
- package/src/artifacts/draft-vocabulary.ts +30 -0
- package/src/artifacts/expert-offload-build.ts +75 -0
- package/src/artifacts/expert-offload.ts +89 -0
- package/src/artifacts/experts/index.ts +3 -0
- package/src/artifacts/experts/io.ts +386 -0
- package/src/artifacts/experts/residency.ts +1134 -0
- package/src/artifacts/experts/usage.ts +415 -0
- package/src/artifacts/fingerprint.ts +44 -0
- package/src/artifacts/glm52/container.ts +305 -0
- package/src/artifacts/glm52/expert-layout.ts +249 -0
- package/src/artifacts/glm52/index.ts +7 -0
- package/src/artifacts/glm52/kernel-layout.ts +124 -0
- package/src/artifacts/glm52/layout.ts +152 -0
- package/src/artifacts/glm52/memory.ts +500 -0
- package/src/artifacts/glm52/resident-weights.ts +135 -0
- package/src/artifacts/glm52/weights.ts +503 -0
- package/src/artifacts/glm52-config.ts +262 -0
- package/src/artifacts/identity.ts +94 -0
- package/src/artifacts/index.ts +15 -0
- package/src/artifacts/mmap.ts +90 -0
- package/src/artifacts/safetensors-writer.ts +249 -0
- package/src/artifacts/safetensors.ts +164 -0
- package/src/artifacts/turboquant-fold.ts +42 -0
- package/src/artifacts/weight-audit.ts +43 -0
- package/src/artifacts/weights.ts +255 -0
- package/src/contracts/index.ts +2 -0
- package/src/contracts/mlx/attention.ts +8 -0
- package/src/contracts/mlx/cache.ts +510 -0
- package/src/contracts/mlx/checkpoint.ts +10 -0
- package/src/contracts/mlx/draft-checkpoint.ts +14 -0
- package/src/contracts/mlx/draft-projection.ts +27 -0
- package/src/contracts/mlx/draft-target.ts +84 -0
- package/src/contracts/mlx/drafter.ts +176 -0
- package/src/contracts/mlx/forward-work.ts +23 -0
- package/src/contracts/mlx/glm52-weights.ts +25 -0
- package/src/contracts/mlx/graph.ts +309 -0
- package/src/contracts/mlx/head.ts +6 -0
- package/src/contracts/mlx/index.ts +15 -0
- package/src/contracts/mlx/kv-codec.ts +9 -0
- package/src/contracts/mlx/media.ts +113 -0
- package/src/contracts/mlx/positions.ts +16 -0
- package/src/contracts/mlx/row-table.ts +2 -0
- package/src/contracts/mlx/trainable.ts +103 -0
- package/src/contracts/mlx/whisper.ts +96 -0
- package/src/contracts/portable/composition.ts +106 -0
- package/src/contracts/portable/denoising.ts +17 -0
- package/src/contracts/portable/execution.ts +79 -0
- package/src/contracts/portable/generation.ts +103 -0
- package/src/contracts/portable/graph.ts +80 -0
- package/src/contracts/portable/index.ts +15 -0
- package/src/contracts/portable/memory-plan.ts +26 -0
- package/src/contracts/portable/object-cache.ts +8 -0
- package/src/contracts/portable/prefill.ts +63 -0
- package/src/contracts/portable/prefix-cache.ts +33 -0
- package/src/contracts/portable/prompt.ts +2 -0
- package/src/contracts/portable/resources.ts +25 -0
- package/src/contracts/portable/rollback.ts +7 -0
- package/src/contracts/portable/sampling.ts +44 -0
- package/src/contracts/portable/scheduling.ts +54 -0
- package/src/contracts/portable/token-delivery.ts +4 -0
- package/src/contracts/portable/tsconfig.json +14 -0
- package/src/embeddings/index.ts +1 -0
- package/src/embeddings/text.ts +85 -0
- package/src/execution/admission.ts +59 -0
- package/src/execution/batch-group.ts +1240 -0
- package/src/execution/batch-types.ts +225 -0
- package/src/execution/cancellation.ts +38 -0
- package/src/execution/continuation-request.ts +45 -0
- package/src/execution/continuation-types.ts +10 -0
- package/src/execution/coordinator.ts +70 -0
- package/src/execution/denoising-group.ts +180 -0
- package/src/execution/engine.ts +56 -0
- package/src/execution/fill-group.ts +505 -0
- package/src/execution/fit.ts +184 -0
- package/src/execution/gateway-binding.ts +178 -0
- package/src/execution/grammar-group.ts +224 -0
- package/src/execution/index.ts +18 -0
- package/src/execution/methods.ts +75 -0
- package/src/execution/mixed-iteration.ts +96 -0
- package/src/execution/plan.ts +65 -0
- package/src/execution/prefill-cohort.ts +100 -0
- package/src/execution/prefill-rows.ts +263 -0
- package/src/execution/request-policy.ts +19 -0
- package/src/execution/row-sampling.ts +49 -0
- package/src/execution/scheduler.ts +92 -0
- package/src/execution/session.ts +247 -0
- package/src/execution/speculative-group.ts +797 -0
- package/src/execution/tasks.ts +44 -0
- package/src/generation/autoregressive.ts +840 -0
- package/src/generation/bindings/autoregressive.ts +104 -0
- package/src/generation/bindings/denoising.ts +60 -0
- package/src/generation/bindings/prefill-policy.ts +36 -0
- package/src/generation/checkpoint-identity.ts +60 -0
- package/src/generation/denoising.ts +85 -0
- package/src/generation/diffusion.ts +583 -0
- package/src/generation/fill/echo-index.ts +188 -0
- package/src/generation/fill/index.ts +3 -0
- package/src/generation/fill/proposal.ts +69 -0
- package/src/generation/fill/schema-rows.ts +506 -0
- package/src/generation/fill/session.ts +464 -0
- package/src/generation/fill/tool-boundary.ts +103 -0
- package/src/generation/fill-append.ts +58 -0
- package/src/generation/generate.ts +44 -0
- package/src/generation/grammar-step.ts +39 -0
- package/src/generation/index.ts +14 -0
- package/src/generation/phase-dispatch.ts +115 -0
- package/src/generation/prefill.ts +33 -0
- package/src/generation/result.ts +49 -0
- package/src/generation/scopes.ts +157 -0
- package/src/generation/speculative/acceptance.ts +41 -0
- package/src/generation/speculative/adaptive-draft-gate.ts +166 -0
- package/src/generation/speculative/bindings/assistant-rows.ts +102 -0
- package/src/generation/speculative/bindings/autoregressive-rows.ts +124 -0
- package/src/generation/speculative/bindings/binding.ts +106 -0
- package/src/generation/speculative/bindings/deepspec-rows.ts +33 -0
- package/src/generation/speculative/bindings/draft-target.ts +10 -0
- package/src/generation/speculative/bindings/glm52-mtp-rows.ts +128 -0
- package/src/generation/speculative/bindings/projected-context-rows.ts +78 -0
- package/src/generation/speculative/bindings/projected-draft-rows.ts +95 -0
- package/src/generation/speculative/bindings/qwen-mtp-rows.ts +336 -0
- package/src/generation/speculative/bindings/two-model-rows.ts +63 -0
- package/src/generation/speculative/cache-identity.ts +16 -0
- package/src/generation/speculative/draft-kind.ts +149 -0
- package/src/generation/speculative/draft-policy.ts +41 -0
- package/src/generation/speculative/draft-registry.ts +118 -0
- package/src/generation/speculative/draft-vocab.ts +110 -0
- package/src/generation/speculative/index.ts +17 -0
- package/src/generation/speculative/output.ts +49 -0
- package/src/generation/speculative/round.ts +307 -0
- package/src/generation/speculative/source.ts +144 -0
- package/src/generation/speculative/sources/assistant-source.ts +64 -0
- package/src/generation/speculative/sources/deepspec-source.ts +70 -0
- package/src/generation/speculative/sources/dflash-source.ts +84 -0
- package/src/generation/speculative/sources/dflash2-source.ts +86 -0
- package/src/generation/speculative/sources/glm52-mtp-source.ts +66 -0
- package/src/generation/speculative/sources/ngram-source.ts +165 -0
- package/src/generation/speculative/sources/qwen-mtp-source.ts +154 -0
- package/src/generation/speculative/sources/two-model.ts +75 -0
- package/src/generation/types.ts +194 -0
- package/src/index.ts +20 -0
- package/src/input/audio/audiotoolbox.ts +112 -0
- package/src/input/audio/decode.ts +133 -0
- package/src/input/audio/features.ts +202 -0
- package/src/input/audio/index.ts +6 -0
- package/src/input/audio/transcode.ts +67 -0
- package/src/input/audio/whisper-mel.ts +185 -0
- package/src/input/audio/whisper-tokenizer.ts +238 -0
- package/src/input/chat-template.ts +389 -0
- package/src/input/index.ts +6 -0
- package/src/input/media-fetch.ts +439 -0
- package/src/input/packed-mixed.ts +43 -0
- package/src/input/special-tokens.ts +75 -0
- package/src/input/token-groups.ts +49 -0
- package/src/input/tokenizer.ts +336 -0
- package/src/input/tool-call.ts +689 -0
- package/src/input/vision/index.ts +3 -0
- package/src/input/vision/preprocess.ts +247 -0
- package/src/input/vision/prompt.ts +398 -0
- package/src/input/vision/qwen3vl-preprocess.ts +409 -0
- package/src/input/vision/qwen3vl-prompt.ts +237 -0
- package/src/input/vision/video-frames.ts +100 -0
- package/src/kernels/ane/fill.ts +101 -0
- package/src/kernels/ane/linear.ts +416 -0
- package/src/kernels/attention/flash.ts +694 -0
- package/src/kernels/attention/kvq4-decode.ts +155 -0
- package/src/kernels/attention/masks.ts +40 -0
- package/src/kernels/attention/multi-query.ts +193 -0
- package/src/kernels/attention/paged.ts +130 -0
- package/src/kernels/delta/gated.ts +306 -0
- package/src/kernels/delta/index.ts +3 -0
- package/src/kernels/delta/silu-rms-heads.ts +69 -0
- package/src/kernels/delta/state.ts +74 -0
- package/src/kernels/gated-rms-norm.ts +64 -0
- package/src/kernels/glm52/layout.ts +37 -0
- package/src/kernels/glm52/streamed-experts.ts +360 -0
- package/src/kernels/logits.ts +38 -0
- package/src/kernels/quantization/affine-row.ts +154 -0
- package/src/kernels/quantization/affine-rows.ts +36 -0
- package/src/kernels/quantization/affine-split-mma.ts +144 -0
- package/src/kernels/quantization/affine3-mma.ts +117 -0
- package/src/kernels/quantization/affine3-rows.ts +103 -0
- package/src/kernels/quantization/affine4-mma.ts +113 -0
- package/src/kernels/quantization/affine4-rows.ts +102 -0
- package/src/kernels/residual-rms-norm.ts +65 -0
- package/src/kernels/sampling/index.ts +2 -0
- package/src/kernels/sampling/normalized-argmax.ts +116 -0
- package/src/kernels/sampling/token-bitmask.ts +52 -0
- package/src/kernels/softcap.ts +35 -0
- package/src/kernels/trellis/codebook.ts +123 -0
- package/src/kernels/trellis/down-codebook.ts +184 -0
- package/src/kernels/trellis/down-factored.ts +180 -0
- package/src/kernels/trellis/down-k3-interleaved-factored.ts +146 -0
- package/src/kernels/trellis/down-mma-wide.ts +195 -0
- package/src/kernels/trellis/down-mma.ts +145 -0
- package/src/kernels/trellis/expand.ts +42 -0
- package/src/kernels/trellis/gate-up-codebook-mma.ts +141 -0
- package/src/kernels/trellis/gate-up-codebook.ts +137 -0
- package/src/kernels/trellis/gate-up-factored.ts +197 -0
- package/src/kernels/trellis/gate-up-mma-wide.ts +147 -0
- package/src/kernels/trellis/gate-up-mma.ts +148 -0
- package/src/kernels/trellis/gate-up.ts +248 -0
- package/src/kernels/trellis/geometry.ts +22 -0
- package/src/kernels/trellis/index.ts +103 -0
- package/src/kernels/trellis/launch.ts +12 -0
- package/src/kernels/trellis/mixed-gate-up-factored.ts +119 -0
- package/src/kernels/trellis/mixed-gate-up.ts +146 -0
- package/src/kernels/trellis/qualified.ts +34 -0
- package/src/kernels/trellis/reduce.ts +150 -0
- package/src/kernels/trellis/scatter.ts +407 -0
- package/src/kernels/trellis/splitk-prefill.ts +152 -0
- package/src/kernels/trellis/tiled-prefill.ts +131 -0
- package/src/kernels/trellis/vector-expand.ts +58 -0
- package/src/kernels/trellis/wide-prefill.ts +89 -0
- package/src/kernels/turboquant/decode.ts +82 -0
- package/src/kernels/turboquant/index.ts +5 -0
- package/src/kernels/turboquant/inverse.ts +122 -0
- package/src/kernels/turboquant/ops.ts +860 -0
- package/src/kernels/turboquant/signs.ts +130 -0
- package/src/kernels/turboquant/tables.ts +124 -0
- package/src/layers/affine-verify-linear.ts +75 -0
- package/src/layers/ane-prefill-split.ts +283 -0
- package/src/layers/compiled-normalization.ts +41 -0
- package/src/layers/delta-heads.ts +23 -0
- package/src/layers/dense-embedding.ts +29 -0
- package/src/layers/dense-linear.ts +32 -0
- package/src/layers/dequantized-linear.ts +72 -0
- package/src/layers/folded-attention.ts +42 -0
- package/src/layers/geglu.ts +130 -0
- package/src/layers/helpers.ts +7 -0
- package/src/layers/index.ts +36 -0
- package/src/layers/kv4-decode-attention.ts +15 -0
- package/src/layers/layer-norm.ts +24 -0
- package/src/layers/loading.ts +92 -0
- package/src/layers/lora.ts +150 -0
- package/src/layers/normalization.ts +9 -0
- package/src/layers/prefix-layout.ts +33 -0
- package/src/layers/quantized-append-attention.ts +32 -0
- package/src/layers/quantized-attention.ts +353 -0
- package/src/layers/quantized-embedding.ts +39 -0
- package/src/layers/quantized-linear.ts +137 -0
- package/src/layers/quantized-switch-linear.ts +37 -0
- package/src/layers/qwen-append.ts +12 -0
- package/src/layers/qwen-mrope.ts +238 -0
- package/src/layers/rope-2d.ts +110 -0
- package/src/layers/rope.ts +389 -0
- package/src/layers/swiglu.ts +23 -0
- package/src/layers/trellis-codebook-down.ts +52 -0
- package/src/layers/trellis-codebook-gate-up.ts +42 -0
- package/src/layers/trellis-down.ts +117 -0
- package/src/layers/trellis-gate-up.ts +71 -0
- package/src/layers/trellis-linear.ts +211 -0
- package/src/layers/trellis-prompt.ts +102 -0
- package/src/layers/trellis-reduce-gate-up.ts +73 -0
- package/src/layers/trellis-scatter-down.ts +98 -0
- package/src/models/ane-install.ts +74 -0
- package/src/models/audio/conformer.ts +656 -0
- package/src/models/cache-composition.ts +44 -0
- package/src/models/capabilities.ts +89 -0
- package/src/models/chat-template.ts +10 -0
- package/src/models/composition.ts +107 -0
- package/src/models/diffusion-gemma/experts.ts +59 -0
- package/src/models/diffusion-gemma/family.ts +21 -0
- package/src/models/diffusion-gemma/model.ts +900 -0
- package/src/models/diffusion-gemma/vision.ts +549 -0
- package/src/models/drafter-loaders.ts +49 -0
- package/src/models/drafters.ts +39 -0
- package/src/models/factory.ts +351 -0
- package/src/models/families.ts +70 -0
- package/src/models/family.ts +125 -0
- package/src/models/gemma4/assistant-target.ts +46 -0
- package/src/models/gemma4/assistant.ts +327 -0
- package/src/models/gemma4/blocks.ts +755 -0
- package/src/models/gemma4/compiled-decode.ts +452 -0
- package/src/models/gemma4/family.ts +39 -0
- package/src/models/gemma4/gemma4-12b.ts +677 -0
- package/src/models/gemma4/gemma4-26b.ts +544 -0
- package/src/models/gemma4/gemma4-e4b.ts +599 -0
- package/src/models/gemma4/graphs.ts +147 -0
- package/src/models/gemma4/media.ts +88 -0
- package/src/models/gemma4/model.ts +860 -0
- package/src/models/gemma4/serving-blocks.ts +72 -0
- package/src/models/gemma4/trainable.ts +186 -0
- package/src/models/gemma4/written-blocks.ts +232 -0
- package/src/models/gemma4/written-forward.ts +159 -0
- package/src/models/gemma4/written-input.ts +29 -0
- package/src/models/glm52/atlas.ts +507 -0
- package/src/models/glm52/coupling.ts +388 -0
- package/src/models/glm52/dsa.ts +601 -0
- package/src/models/glm52/family.ts +20 -0
- package/src/models/glm52/mla.ts +812 -0
- package/src/models/glm52/model.ts +1127 -0
- package/src/models/glm52/moe.ts +223 -0
- package/src/models/glm52/mtp.ts +60 -0
- package/src/models/glm52/pilot.ts +537 -0
- package/src/models/glm52/reference.ts +445 -0
- package/src/models/glm52/residency.ts +557 -0
- package/src/models/glm52/streamed-experts.ts +447 -0
- package/src/models/graph.ts +59 -0
- package/src/models/implementation.ts +230 -0
- package/src/models/index.ts +15 -0
- package/src/models/media-input.ts +15 -0
- package/src/models/memory-plan.ts +39 -0
- package/src/models/minicpm5/family.ts +29 -0
- package/src/models/minicpm5/model.ts +394 -0
- package/src/models/minicpm5/trainable.ts +90 -0
- package/src/models/profile.ts +337 -0
- package/src/models/qwen/blocks.ts +658 -0
- package/src/models/qwen/checkpoint.ts +190 -0
- package/src/models/qwen/families.ts +57 -0
- package/src/models/qwen/media-input.ts +161 -0
- package/src/models/qwen/mtp.ts +101 -0
- package/src/models/qwen/qwen3-moe.ts +355 -0
- package/src/models/qwen/qwen3.ts +323 -0
- package/src/models/qwen/qwen38-27b-trellis-m1max.ts +1532 -0
- package/src/models/qwen/qwen38-27b-trellis-m4pro.ts +1092 -0
- package/src/models/qwen/qwen3_5.ts +433 -0
- package/src/models/qwen/written-layers.ts +285 -0
- package/src/models/row-table.ts +1 -0
- package/src/models/runtime.ts +73 -0
- package/src/models/speculative/deepspec-artifact.ts +10 -0
- package/src/models/speculative/deepspec.ts +744 -0
- package/src/models/speculative/dflash.ts +553 -0
- package/src/models/speculative/dflash2-artifact.ts +9 -0
- package/src/models/speculative/dflash2-operations.ts +1 -0
- package/src/models/speculative/dflash2.ts +576 -0
- package/src/models/speculative/loader.ts +29 -0
- package/src/models/support.ts +79 -0
- package/src/models/universal/archs.ts +365 -0
- package/src/models/universal/blocks.ts +479 -0
- package/src/models/universal/dense.ts +255 -0
- package/src/models/universal/family.ts +17 -0
- package/src/models/vision/qwen3vl.ts +566 -0
- package/src/models/vision/siglip.ts +520 -0
- package/src/models/vision/unified.ts +176 -0
- package/src/models/whisper/family.ts +19 -0
- package/src/models/whisper/fast.ts +734 -0
- package/src/models/whisper/model.ts +430 -0
- package/src/runtime/compiled-trace.ts +16 -0
- package/src/runtime/config.ts +77 -0
- package/src/runtime/expert-trace.ts +71 -0
- package/src/runtime/graph-forward.ts +65 -0
- package/src/runtime/integers.ts +6 -0
- package/src/runtime/native.ts +16 -0
- package/src/runtime/resources.ts +53 -0
- package/src/runtime/row-operations.ts +42 -0
- package/src/runtime/trace.ts +161 -0
- package/src/sampling/curve.ts +122 -0
- package/src/sampling/draft.ts +55 -0
- package/src/sampling/extras.ts +45 -0
- package/src/sampling/filters.ts +134 -0
- package/src/sampling/grammar.ts +630 -0
- package/src/sampling/hlg.ts +290 -0
- package/src/sampling/index.ts +9 -0
- package/src/sampling/processors.ts +142 -0
- package/src/sampling/sampler.ts +120 -0
- package/src/sampling/step.ts +343 -0
- package/src/sampling/types.ts +161 -0
- package/src/sampling/window-filters.ts +46 -0
- package/src/scoring/decisions.ts +96 -0
- package/src/scoring/full-sequence.ts +103 -0
- package/src/scoring/index.ts +9 -0
- package/src/scoring/kl.ts +29 -0
- package/src/scoring/perplexity.ts +165 -0
- package/src/scoring/response-logps.ts +65 -0
- package/src/state/affine-attention.ts +180 -0
- package/src/state/attachment.ts +68 -0
- package/src/state/attention-read.ts +215 -0
- package/src/state/batched-kv.ts +294 -0
- package/src/state/batched-mask.ts +445 -0
- package/src/state/batched-quant.ts +389 -0
- package/src/state/batched-quantized-kv.ts +209 -0
- package/src/state/batched-rotating-quant.ts +457 -0
- package/src/state/batched-rotating-state.ts +149 -0
- package/src/state/batched-rotating.ts +430 -0
- package/src/state/batched-row-storage.ts +179 -0
- package/src/state/batched-ssm.ts +54 -0
- package/src/state/batched-turboquant-kv.ts +144 -0
- package/src/state/bf16-first-kv.ts +663 -0
- package/src/state/cache-service.ts +592 -0
- package/src/state/capabilities.ts +65 -0
- package/src/state/checkpoint.ts +46 -0
- package/src/state/continuation-persistence.ts +126 -0
- package/src/state/decode-step-inputs.ts +31 -0
- package/src/state/decoded-kv-donor.ts +12 -0
- package/src/state/delayed-quantized-kv.ts +141 -0
- package/src/state/delayed-rotating-quantized-kv.ts +178 -0
- package/src/state/delayed-turboquant-kv.ts +123 -0
- package/src/state/durability.ts +214 -0
- package/src/state/encoder-cache.ts +26 -0
- package/src/state/full-kv-row-append.ts +33 -0
- package/src/state/full-kv-row-donor.ts +54 -0
- package/src/state/full-prefill-padding.ts +56 -0
- package/src/state/full-prefill-row.ts +112 -0
- package/src/state/full-transitioning-kv-rows.ts +70 -0
- package/src/state/glm52-cache.ts +783 -0
- package/src/state/grouped-heads-kv4-cache.ts +26 -0
- package/src/state/index.ts +38 -0
- package/src/state/kv-attention-view.ts +77 -0
- package/src/state/kv-budget.ts +14 -0
- package/src/state/kv-scheme.ts +315 -0
- package/src/state/kv-storage.ts +57 -0
- package/src/state/kv-tensor-rows.ts +212 -0
- package/src/state/kv.ts +324 -0
- package/src/state/kv4-head256-cache.ts +46 -0
- package/src/state/latent-legos.ts +895 -0
- package/src/state/layout.ts +277 -0
- package/src/state/leases.ts +26 -0
- package/src/state/paged/cache.ts +689 -0
- package/src/state/paged/codec.ts +36 -0
- package/src/state/paged/rows.ts +223 -0
- package/src/state/persistence-types.ts +173 -0
- package/src/state/persistence-worker.ts +83 -0
- package/src/state/persistence.ts +1036 -0
- package/src/state/persistence.worker.d.ts +3 -0
- package/src/state/persistence.worker.js +264 -0
- package/src/state/prefix-cache.ts +685 -0
- package/src/state/quantized-kv.ts +369 -0
- package/src/state/quantized-tensor.ts +26 -0
- package/src/state/request-policy.ts +48 -0
- package/src/state/retention-policy.ts +37 -0
- package/src/state/rollback.ts +131 -0
- package/src/state/rotating-kv-donor.ts +35 -0
- package/src/state/rotating-kv-layout.ts +341 -0
- package/src/state/rotating-kv.ts +529 -0
- package/src/state/rotating-quantized-kv.ts +616 -0
- package/src/state/rotating-row-transaction.ts +35 -0
- package/src/state/rows.ts +104 -0
- package/src/state/softcap-attention.ts +118 -0
- package/src/state/speculative/glm52-mtp-state.ts +38 -0
- package/src/state/speculative/qwen-mtp-state.ts +34 -0
- package/src/state/speculative-rotating-kv.ts +113 -0
- package/src/state/ssd-cache.ts +434 -0
- package/src/state/ssm-prefill-padding.ts +39 -0
- package/src/state/ssm.ts +422 -0
- package/src/state/target-layout.ts +7 -0
- package/src/state/tiered-prefix-cache.ts +45 -0
- package/src/state/training-cache.ts +300 -0
- package/src/state/transitioning-kv-rows.ts +267 -0
- package/src/state/turboquant-attention.ts +97 -0
- package/src/state/turboquant-codec.ts +90 -0
- package/src/state/turboquant-kv.ts +442 -0
- package/src/state/views.ts +26 -0
- package/src/transcription/index.ts +3 -0
- package/src/transcription/silero-vad.ts +379 -0
- package/src/transcription/whisper/decode.ts +773 -0
- package/src/transcription/whisper/format.ts +74 -0
- package/src/transcription/whisper/index.ts +4 -0
- package/src/transcription/whisper/timing.ts +274 -0
- package/src/transcription/whisper/transcribe.ts +394 -0
- package/src/transcription/whisper/types.ts +22 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// GENERATED by packages/inference/scripts/gen-turboquant-signs.ts; do not edit.
|
|
2
|
+
//
|
|
3
|
+
// TurboQuant's rotation signs per head dimension, from the construction the
|
|
4
|
+
// vendored vllm-metal reference defines (`02d723a:lab/repro/vllm-metal-turboquant/
|
|
5
|
+
// turboquant_reference.py`, `fwht()`): randint(0, 2, shape=(d,), key=key(42)),
|
|
6
|
+
// mapped to 1 - 2·bit. The rotation is these signs then MLX's hadamard_transform.
|
|
7
|
+
|
|
8
|
+
export const TURBOQUANT_HEAD_DIMS = [64, 96, 128, 256, 512] as const;
|
|
9
|
+
export type TurboQuantHeadDim = (typeof TURBOQUANT_HEAD_DIMS)[number];
|
|
10
|
+
|
|
11
|
+
export const SIGN_VECTORS: Record<TurboQuantHeadDim, number[]> = {
|
|
12
|
+
64: [
|
|
13
|
+
1, -1, -1, -1, 1, 1, 1, -1, 1, 1,
|
|
14
|
+
-1, 1, -1, 1, -1, 1, -1, 1, 1, -1,
|
|
15
|
+
1, -1, -1, 1, -1, -1, -1, 1, -1, -1,
|
|
16
|
+
1, -1, 1, -1, 1, 1, 1, 1, 1, 1,
|
|
17
|
+
1, 1, 1, -1, 1, 1, -1, 1, 1, -1,
|
|
18
|
+
-1, -1, -1, 1, -1, -1, 1, -1, 1, 1,
|
|
19
|
+
1, -1, -1, 1,
|
|
20
|
+
],
|
|
21
|
+
96: [
|
|
22
|
+
1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
|
|
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, -1, 1, 1, -1, -1, 1,
|
|
30
|
+
-1, 1, 1, 1, -1, 1, -1, 1, -1, 1,
|
|
31
|
+
1, -1, -1, -1, 1, 1,
|
|
32
|
+
],
|
|
33
|
+
128: [
|
|
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, -1, 1,
|
|
45
|
+
-1, 1, 1, 1, 1, 1, 1, -1, -1, 1,
|
|
46
|
+
1, 1, -1, 1, -1, 1, 1, 1,
|
|
47
|
+
],
|
|
48
|
+
256: [
|
|
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, 1, 1, -1, -1,
|
|
73
|
+
-1, -1, 1, -1, 1, 1, 1, -1, 1, -1,
|
|
74
|
+
-1, -1, -1, -1, -1, -1,
|
|
75
|
+
],
|
|
76
|
+
512: [
|
|
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, -1, 1, 1, -1, -1, -1, 1, -1,
|
|
127
|
+
1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
|
|
128
|
+
1, 1,
|
|
129
|
+
],
|
|
130
|
+
};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// GENERATED by `02d723a:scripts/regen.ts` turboquant — DO NOT EDIT.
|
|
2
|
+
// Regenerate (from a 02d723a checkout): bun scripts/regen.ts turboquant
|
|
3
|
+
//
|
|
4
|
+
// Source: `02d723a: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
|
+
// - LLOYD_MAX[bits]: Lloyd-Max (centroids, boundaries) for value
|
|
9
|
+
// quantization at the given bit width. bits=3 is the vendored reference's
|
|
10
|
+
// hardcoded fast-path table (CENTROIDS_3BIT/BOUNDARIES_3BIT); other widths
|
|
11
|
+
// are the reference's own iterative Lloyd-Max fit on a unit-normal source
|
|
12
|
+
// (mx.random.normal(key=mx.random.key(0)), 500 iterations), computed once
|
|
13
|
+
// here and hardcoded — no runtime iteration in the TS port.
|
|
14
|
+
//
|
|
15
|
+
// The rotation's sign tables are signs.ts (scripts/gen-turboquant-signs.ts).
|
|
16
|
+
|
|
17
|
+
export const TURBOQUANT_VALUE_BITS = [2, 3, 4, 5, 8] as const;
|
|
18
|
+
export type TurboQuantValueBits = (typeof TURBOQUANT_VALUE_BITS)[number];
|
|
19
|
+
|
|
20
|
+
export interface LloydMaxTable {
|
|
21
|
+
centroids: number[];
|
|
22
|
+
boundaries: number[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const LLOYD_MAX: Record<TurboQuantValueBits, LloydMaxTable> = {
|
|
26
|
+
2: {
|
|
27
|
+
centroids: [
|
|
28
|
+
-1.5144749879837036, -0.45822522044181824, 0.44863229990005493, 1.512152075767517,
|
|
29
|
+
],
|
|
30
|
+
boundaries: [
|
|
31
|
+
-0.9863501191139221, -0.004796460270881653, 0.9803922176361084,
|
|
32
|
+
],
|
|
33
|
+
},
|
|
34
|
+
3: {
|
|
35
|
+
centroids: [
|
|
36
|
+
-2.151949882507324, -1.3439099788665771, -0.7560099959373474, -0.24508999288082123, 0.24508999288082123, 0.7560099959373474, 1.3439099788665771, 2.151949882507324,
|
|
37
|
+
],
|
|
38
|
+
boundaries: [
|
|
39
|
+
-1.7479300498962402, -1.0499600172042847, -0.5005499720573425, 0, 0.5005499720573425, 1.0499600172042847, 1.7479300498962402,
|
|
40
|
+
],
|
|
41
|
+
},
|
|
42
|
+
4: {
|
|
43
|
+
centroids: [
|
|
44
|
+
-2.765662670135498, -2.0995230674743652, -1.6401931047439575, -1.27662992477417, -0.9602491855621338, -0.673130452632904, -0.4038185179233551, -0.14272712171077728, 0.11716290563344955, 0.3790881633758545,
|
|
45
|
+
0.6475776433944702, 0.9337777495384216, 1.2523075342178345, 1.6217625141143799, 2.0757482051849365, 2.7207279205322266,
|
|
46
|
+
],
|
|
47
|
+
boundaries: [
|
|
48
|
+
-2.4325928688049316, -1.8698580265045166, -1.458411455154419, -1.1184395551681519, -0.8166898488998413, -0.5384745001792908, -0.2732728123664856, -0.012782108038663864, 0.24812553822994232, 0.5133329033851624,
|
|
49
|
+
0.7906776666641235, 1.0930426120758057, 1.437035083770752, 1.8487553596496582, 2.398238182067871,
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
5: {
|
|
53
|
+
centroids: [
|
|
54
|
+
-3.3482043743133545, -2.7744688987731934, -2.3917815685272217, -2.0885653495788574, -1.8382762670516968, -1.6273508071899414, -1.436827301979065, -1.2599420547485352, -1.0958681106567383, -0.9411738514900208,
|
|
55
|
+
-0.7932286262512207, -0.6509044766426086, -0.5113211274147034, -0.37372007966041565, -0.23732160031795502, -0.10278966277837753, 0.031794287264347076, 0.16419854760169983, 0.3001006245613098, 0.4395425021648407,
|
|
56
|
+
0.5808256268501282, 0.7275802493095398, 0.8738965392112732, 1.029397964477539, 1.1942607164382935, 1.3728704452514648, 1.5676456689834595, 1.778881311416626, 2.0225136280059814, 2.3095157146453857,
|
|
57
|
+
2.666351556777954, 3.2329483032226562,
|
|
58
|
+
],
|
|
59
|
+
boundaries: [
|
|
60
|
+
-3.0613365173339844, -2.583125114440918, -2.24017333984375, -1.9634208679199219, -1.7328135967254639, -1.5320889949798584, -1.3483846187591553, -1.1779050827026367, -1.0185209512710571, -0.8672012090682983,
|
|
61
|
+
-0.7220665216445923, -0.581112802028656, -0.4425206184387207, -0.30552083253860474, -0.17005562782287598, -0.03549768775701523, 0.09799641370773315, 0.23214958608150482, 0.36982154846191406, 0.5101840496063232,
|
|
62
|
+
0.654202938079834, 0.8007383942604065, 0.9516472816467285, 1.1118292808532715, 1.2835655212402344, 1.4702579975128174, 1.6732635498046875, 1.9006974697113037, 2.1660146713256836, 2.48793363571167,
|
|
63
|
+
2.9496498107910156,
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
8: {
|
|
67
|
+
centroids: [
|
|
68
|
+
-4.505678653717041, -4.454618453979492, -4.436285972595215, -4.401589393615723, -4.363239765167236, -4.332197189331055, -4.2975006103515625, -4.26280403137207, -4.228107929229736, -4.193411827087402,
|
|
69
|
+
-4.15871524810791, -4.124018669128418, -4.094437599182129, -4.045758247375488, -4.002621173858643, -4.002171516418457, -3.942392110824585, -3.916801691055298, -3.8863255977630615, -3.8477535247802734,
|
|
70
|
+
-3.7988643646240234, -3.762794256210327, -3.7445151805877686, -3.706960439682007, -3.67462158203125, -3.6314854621887207, -3.5950119495391846, -3.580256938934326, -3.539952516555786, -3.5038421154022217,
|
|
71
|
+
-3.4671568870544434, -3.4281671047210693, -3.398128032684326, -3.363133192062378, -3.3274667263031006, -3.2919743061065674, -3.2577857971191406, -3.2200968265533447, -3.192981719970703, -3.157238006591797,
|
|
72
|
+
-3.111802577972412, -3.0821340084075928, -3.050640344619751, -3.0148110389709473, -2.980158805847168, -2.946451425552368, -2.9111905097961426, -2.8747212886810303, -2.8412270545959473, -2.8061022758483887,
|
|
73
|
+
-2.7716867923736572, -2.7376010417938232, -2.7029218673706055, -2.6634461879730225, -2.630377769470215, -2.5996179580688477, -2.560898780822754, -2.525275230407715, -2.4947545528411865, -2.461374044418335,
|
|
74
|
+
-2.424009323120117, -2.3877711296081543, -2.3505144119262695, -2.3163986206054688, -2.282374858856201, -2.243652820587158, -2.2097959518432617, -2.1770639419555664, -2.14471697807312, -2.1113693714141846,
|
|
75
|
+
-2.078673839569092, -2.044494390487671, -2.00639271736145, -1.969869613647461, -1.9300988912582397, -1.8931699991226196, -1.8589746952056885, -1.8212106227874756, -1.7848811149597168, -1.7465486526489258,
|
|
76
|
+
-1.7103406190872192, -1.6743196249008179, -1.6393754482269287, -1.6040517091751099, -1.5708216428756714, -1.5365480184555054, -1.5035663843154907, -1.469175100326538, -1.4350656270980835, -1.4022008180618286,
|
|
77
|
+
-1.3688998222351074, -1.3355891704559326, -1.3018449544906616, -1.2698367834091187, -1.235532283782959, -1.20119047164917, -1.1664694547653198, -1.1305049657821655, -1.0959333181381226, -1.0614327192306519,
|
|
78
|
+
-1.0274933576583862, -0.9940271377563477, -0.9609262347221375, -0.9273853898048401, -0.8929082751274109, -0.8593462705612183, -0.8261474967002869, -0.7937442660331726, -0.7588282823562622, -0.7230017185211182,
|
|
79
|
+
-0.6871297359466553, -0.6517745852470398, -0.6162516474723816, -0.5811514854431152, -0.5462668538093567, -0.5111281275749207, -0.4750610589981079, -0.4394458830356598, -0.404982328414917, -0.36981964111328125,
|
|
80
|
+
-0.334137886762619, -0.29919183254241943, -0.26465246081352234, -0.23168867826461792, -0.20013625919818878, -0.1672147512435913, -0.13413792848587036, -0.1003282368183136, -0.06571532040834427, -0.03132394701242447,
|
|
81
|
+
0.0043428270146250725, 0.04037246108055115, 0.07451506704092026, 0.1076153963804245, 0.14043505489826202, 0.17426857352256775, 0.20812825858592987, 0.2430444061756134, 0.27797263860702515, 0.31494686007499695,
|
|
82
|
+
0.35088568925857544, 0.38743987679481506, 0.4222983717918396, 0.4568648338317871, 0.49128350615501404, 0.5256633758544922, 0.5586227178573608, 0.5910427570343018, 0.6249552369117737, 0.6594945788383484,
|
|
83
|
+
0.6942835450172424, 0.7278474569320679, 0.7624561786651611, 0.7963849902153015, 0.8309887647628784, 0.8644483685493469, 0.8981063961982727, 0.9320245385169983, 0.96662437915802, 1.0006996393203735,
|
|
84
|
+
1.0343793630599976, 1.0680633783340454, 1.101860523223877, 1.135563611984253, 1.1707100868225098, 1.2038687467575073, 1.2388564348220825, 1.2744282484054565, 1.3102103471755981, 1.3441187143325806,
|
|
85
|
+
1.3769688606262207, 1.4091657400131226, 1.4424594640731812, 1.477047085762024, 1.5127589702606201, 1.5482075214385986, 1.58390474319458, 1.6193201541900635, 1.655400037765503, 1.6924738883972168,
|
|
86
|
+
1.7273176908493042, 1.7618744373321533, 1.7985631227493286, 1.8349602222442627, 1.871187686920166, 1.9062069654464722, 1.9427249431610107, 1.9807021617889404, 2.0164709091186523, 2.049800395965576,
|
|
87
|
+
2.083641529083252, 2.1192877292633057, 2.1556057929992676, 2.189804792404175, 2.2249133586883545, 2.257265329360962, 2.2943572998046875, 2.3308892250061035, 2.3646092414855957, 2.3981246948242188,
|
|
88
|
+
2.4336130619049072, 2.4672765731811523, 2.5033960342407227, 2.535766124725342, 2.5712199211120605, 2.606968879699707, 2.6403279304504395, 2.6730761528015137, 2.7094709873199463, 2.745285749435425,
|
|
89
|
+
2.7794594764709473, 2.8145666122436523, 2.851815938949585, 2.885251045227051, 2.919672727584839, 2.9549803733825684, 2.987560510635376, 3.021491289138794, 3.061659097671509, 3.102267026901245,
|
|
90
|
+
3.1455001831054688, 3.1724164485931396, 3.2052037715911865, 3.2377841472625732, 3.2674646377563477, 3.3014495372772217, 3.3345274925231934, 3.3695874214172363, 3.4068915843963623, 3.442040205001831,
|
|
91
|
+
3.4636645317077637, 3.5024828910827637, 3.5508108139038086, 3.5749337673187256, 3.604726791381836, 3.6401684284210205, 3.692674160003662, 3.7173542976379395, 3.7488019466400146, 3.7978620529174805,
|
|
92
|
+
3.8167099952697754, 3.856578826904297, 3.890836000442505, 3.9217264652252197, 3.974006175994873, 4.009003639221191, 4.029621601104736, 4.079561233520508, 4.099013805389404, 4.1337103843688965,
|
|
93
|
+
4.172705173492432, 4.198393821716309, 4.237799167633057, 4.272495746612549, 4.307192325592041, 4.341888427734375,
|
|
94
|
+
],
|
|
95
|
+
boundaries: [
|
|
96
|
+
-4.4801483154296875, -4.4454522132873535, -4.418937683105469, -4.382414817810059, -4.347718238830566, -4.314848899841309, -4.280152320861816, -4.245455741882324, -4.210760116577148, -4.176063537597656,
|
|
97
|
+
-4.141366958618164, -4.109228134155273, -4.070097923278809, -4.0241899490356445, -4.002396583557129, -3.9722819328308105, -3.9295969009399414, -3.9015636444091797, -3.867039680480957, -3.8233089447021484,
|
|
98
|
+
-3.780829429626465, -3.753654718399048, -3.7257378101348877, -3.690791130065918, -3.6530535221099854, -3.613248825073242, -3.587634563446045, -3.5601048469543457, -3.521897315979004, -3.485499382019043,
|
|
99
|
+
-3.447661876678467, -3.413147449493408, -3.3806304931640625, -3.3452999591827393, -3.309720516204834, -3.2748799324035645, -3.238941192626953, -3.2065391540527344, -3.17510986328125, -3.1345202922821045,
|
|
100
|
+
-3.096968173980713, -3.066387176513672, -3.0327258110046387, -2.9974849224090576, -2.9633049964904785, -2.928821086883545, -2.892955780029297, -2.857974052429199, -2.823664665222168, -2.7888946533203125,
|
|
101
|
+
-2.7546439170837402, -2.720261573791504, -2.6831841468811035, -2.646912097930908, -2.6149978637695312, -2.580258369445801, -2.5430870056152344, -2.5100150108337402, -2.4780642986297607, -2.4426918029785156,
|
|
102
|
+
-2.4058902263641357, -2.369142770767212, -2.333456516265869, -2.299386739730835, -2.2630138397216797, -2.22672438621521, -2.193429946899414, -2.160890579223633, -2.1280431747436523, -2.0950217247009277,
|
|
103
|
+
-2.061583995819092, -2.0254435539245605, -1.9881311655044556, -1.9499843120574951, -1.9116344451904297, -1.8760724067687988, -1.840092658996582, -1.8030458688735962, -1.7657148838043213, -1.7284445762634277,
|
|
104
|
+
-1.6923301219940186, -1.6568474769592285, -1.621713638305664, -1.5874366760253906, -1.5536848306655884, -1.520057201385498, -1.4863708019256592, -1.452120304107666, -1.418633222579956, -1.3855502605438232,
|
|
105
|
+
-1.35224449634552, -1.3187170028686523, -1.2858408689498901, -1.2526845932006836, -1.2183613777160645, -1.1838300228118896, -1.1484872102737427, -1.113219141960144, -1.0786830186843872, -1.044463038444519,
|
|
106
|
+
-1.0107603073120117, -0.9774767160415649, -0.9441558122634888, -0.9101468324661255, -0.8761272430419922, -0.8427468538284302, -0.8099458813667297, -0.776286244392395, -0.7409150004386902, -0.7050657272338867,
|
|
107
|
+
-0.6694521903991699, -0.6340131163597107, -0.5987015962600708, -0.5637091398239136, -0.5286974906921387, -0.4930945932865143, -0.45725345611572266, -0.4222140908241272, -0.3874009847640991, -0.35197877883911133,
|
|
108
|
+
-0.3166648745536804, -0.2819221615791321, -0.24817056953907013, -0.21591246128082275, -0.18367549777030945, -0.15067633986473083, -0.11723308265209198, -0.08302177488803864, -0.04851963371038437, -0.013490559533238411,
|
|
109
|
+
0.022357644513249397, 0.0574437640607357, 0.09106522798538208, 0.12402522563934326, 0.15735182166099548, 0.1911984086036682, 0.22558632493019104, 0.26050853729248047, 0.29645973443984985, 0.332916259765625,
|
|
110
|
+
0.36916279792785645, 0.4048691391944885, 0.43958160281181335, 0.47407418489456177, 0.5084734559059143, 0.5421430468559265, 0.5748327374458313, 0.6079989671707153, 0.642224907875061, 0.6768890619277954,
|
|
111
|
+
0.7110655307769775, 0.7451518177986145, 0.7794206142425537, 0.8136868476867676, 0.8477185964584351, 0.8812773823738098, 0.9150654673576355, 0.9493244886398315, 0.9836620092391968, 1.0175395011901855,
|
|
112
|
+
1.0512213706970215, 1.0849618911743164, 1.118712067604065, 1.1531368494033813, 1.1872894763946533, 1.221362590789795, 1.2566423416137695, 1.2923192977905273, 1.3271645307540894, 1.3605437278747559,
|
|
113
|
+
1.3930673599243164, 1.4258126020431519, 1.4597532749176025, 1.4949030876159668, 1.5304832458496094, 1.5660561323165894, 1.6016124486923218, 1.6373600959777832, 1.6739369630813599, 1.7098958492279053,
|
|
114
|
+
1.744596004486084, 1.7802188396453857, 1.8167617321014404, 1.8530739545822144, 1.8886973857879639, 1.9244658946990967, 1.9617135524749756, 1.9985865354537964, 2.0331356525421143, 2.066720962524414,
|
|
115
|
+
2.1014647483825684, 2.137446880340576, 2.1727051734924316, 2.2073590755462646, 2.241089344024658, 2.275811195373535, 2.3126232624053955, 2.3477492332458496, 2.3813669681549072, 2.4158687591552734,
|
|
116
|
+
2.4504446983337402, 2.4853363037109375, 2.5195810794830322, 2.553493022918701, 2.589094400405884, 2.6236484050750732, 2.6567020416259766, 2.6912736892700195, 2.7273783683776855, 2.7623724937438965,
|
|
117
|
+
2.7970130443573, 2.833191394805908, 2.8685336112976074, 2.9024620056152344, 2.937326431274414, 2.9712705612182617, 3.004525899887085, 3.0415751934051514, 3.081963062286377, 3.1238837242126465,
|
|
118
|
+
3.1589584350585938, 3.188810110092163, 3.22149395942688, 3.25262451171875, 3.284457206726074, 3.317988395690918, 3.352057456970215, 3.3882393836975098, 3.4244658946990967, 3.452852249145508,
|
|
119
|
+
3.4830737113952637, 3.526646852493286, 3.5628724098205566, 3.5898303985595703, 3.6224474906921387, 3.666421413421631, 3.705014228820801, 3.7330780029296875, 3.773332118988037, 3.807286024093628,
|
|
120
|
+
3.836644411087036, 3.8737072944641113, 3.9062812328338623, 3.947866439819336, 3.9915049076080322, 4.019312858581543, 4.054591178894043, 4.089287757873535, 4.11636209487915, 4.153207778930664,
|
|
121
|
+
4.185549736022949, 4.218096733093262, 4.255147457122803, 4.289844036102295, 4.324540138244629,
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { MlxArray } from "@mlx-bun/mlx/array";
|
|
2
|
+
import { affineRow, affineRowEligible } from "../kernels/quantization/affine-row";
|
|
3
|
+
import { affineSplitMma, affineSplitMmaEligible, affineSplitMmaPlan, type AffineSplitMmaPlan } from "../kernels/quantization/affine-split-mma";
|
|
4
|
+
import { affine3Rows, affine3RowsEligible } from "../kernels/quantization/affine3-rows";
|
|
5
|
+
import { affine3Mma, affine3MmaEligible } from "../kernels/quantization/affine3-mma";
|
|
6
|
+
import { affine4Rows, affine4RowsEligible } from "../kernels/quantization/affine4-rows";
|
|
7
|
+
import { affine4Mma, affine4MmaEligible } from "../kernels/quantization/affine4-mma";
|
|
8
|
+
import type { QuantizedLinear } from "./quantized-linear";
|
|
9
|
+
|
|
10
|
+
// Decode and verify projections borrow an affine carrier. Each class runs its
|
|
11
|
+
// fixed kernel and applies the request's external adapter delta afterward.
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
function check(name: string, lin: QuantizedLinear, eligible: boolean): void {
|
|
15
|
+
if (!lin.biases || lin.bias || !eligible) throw new Error(`${name}: unsupported projection (width, group 64, affine, no additive bias)`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** 2..8 activation rows sharing each decoded weight. */
|
|
19
|
+
export class Affine4RowsLinear {
|
|
20
|
+
constructor(readonly linear: QuantizedLinear) {
|
|
21
|
+
check("Affine4RowsLinear", linear, affine4RowsEligible(2, linear.w, linear.scales, linear.spec));
|
|
22
|
+
}
|
|
23
|
+
forward(x: MlxArray): MlxArray { return this.linear.delta.forward(x, affine4Rows(x, this.linear.w, this.linear.scales, this.linear.biases!)); }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** One activation row (3- or 4-bit, the stored width): QuantizedLinear's bits (MLX's one-row quantized matmul),
|
|
27
|
+
* each threadgroup computing its activation chunks once (M1 Max). */
|
|
28
|
+
export class AffineRowLinear {
|
|
29
|
+
readonly #bits: 3 | 4;
|
|
30
|
+
constructor(readonly linear: QuantizedLinear) {
|
|
31
|
+
check("AffineRowLinear", linear, affineRowEligible(1, linear.w, linear.scales, linear.spec));
|
|
32
|
+
this.#bits = linear.spec.bits as 3 | 4;
|
|
33
|
+
}
|
|
34
|
+
forward(x: MlxArray): MlxArray { return this.linear.delta.forward(x, affineRow(x, this.linear.w, this.linear.scales, this.linear.biases!, this.#bits)); }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 1..8 activation rows on the simdgroup matrix unit. */
|
|
38
|
+
export class Affine4MmaLinear {
|
|
39
|
+
constructor(readonly linear: QuantizedLinear) {
|
|
40
|
+
check("Affine4MmaLinear", linear, affine4MmaEligible(1, linear.w, linear.scales, linear.spec));
|
|
41
|
+
}
|
|
42
|
+
forward(x: MlxArray): MlxArray { return this.linear.delta.forward(x, affine4Mma(x, this.linear.w, this.linear.scales, this.linear.biases!)); }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 3-bit: 2..8 activation rows sharing each decoded weight. */
|
|
46
|
+
export class Affine3RowsLinear {
|
|
47
|
+
constructor(readonly linear: QuantizedLinear) {
|
|
48
|
+
check("Affine3RowsLinear", linear, affine3RowsEligible(2, linear.w, linear.scales, linear.spec));
|
|
49
|
+
}
|
|
50
|
+
forward(x: MlxArray): MlxArray { return this.linear.delta.forward(x, affine3Rows(x, this.linear.w, this.linear.scales, this.linear.biases!)); }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 3-bit: 1..8 activation rows on the simdgroup matrix unit. */
|
|
54
|
+
export class Affine3MmaLinear {
|
|
55
|
+
constructor(readonly linear: QuantizedLinear) {
|
|
56
|
+
check("Affine3MmaLinear", linear, affine3MmaEligible(1, linear.w, linear.scales, linear.spec));
|
|
57
|
+
}
|
|
58
|
+
forward(x: MlxArray): MlxArray { return this.linear.delta.forward(x, affine3Mma(x, this.linear.w, this.linear.scales, this.linear.biases!)); }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Any number of activation rows, in blocks of 8 (3- or 4-bit, the stored width), on the simdgroup matrix unit,
|
|
62
|
+
* output tiles sharing each activation fragment and K split across the threadgroup (M1 Max); the launch follows
|
|
63
|
+
* the output width. */
|
|
64
|
+
export class AffineSplitMmaLinear {
|
|
65
|
+
readonly #bits: 3 | 4;
|
|
66
|
+
readonly #plan: AffineSplitMmaPlan;
|
|
67
|
+
constructor(readonly linear: QuantizedLinear) {
|
|
68
|
+
check("AffineSplitMmaLinear", linear, affineSplitMmaEligible(1, linear.w, linear.scales, linear.spec));
|
|
69
|
+
this.#bits = linear.spec.bits as 3 | 4;
|
|
70
|
+
this.#plan = affineSplitMmaPlan(linear.outFeatures);
|
|
71
|
+
}
|
|
72
|
+
forward(x: MlxArray): MlxArray {
|
|
73
|
+
return this.linear.delta.forward(x, affineSplitMma(x, this.linear.w, this.linear.scales, this.linear.biases!, this.#bits, this.#plan));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import type { MlxArray } from "@mlx-bun/mlx/array";
|
|
2
|
+
import * as ops from "@mlx-bun/mlx/ops";
|
|
3
|
+
import { AneProgram, AneStreamedMatmul, mlpSliceProgram, streamedMatmulProgram, type AneProgramSpec } from "../kernels/ane/linear";
|
|
4
|
+
import { trellisFillHalf, trellisFillHalfK3Interleaved, writeHalf } from "../kernels/ane/fill";
|
|
5
|
+
import type { TrellisGeometry } from "../kernels/trellis/geometry";
|
|
6
|
+
import { expandTrellis } from "../kernels/trellis/index";
|
|
7
|
+
import { QuantizedLinear, QuantizedLinearPlain } from "./quantized-linear";
|
|
8
|
+
import { TrellisLinear, trellisRows, trellisStoredRows } from "./trellis-linear";
|
|
9
|
+
|
|
10
|
+
// Prompt-piece channel splits between the Apple Neural Engine and the GPU. The
|
|
11
|
+
// ANE computes the first n output channels from streamed fp16 weights the GPU
|
|
12
|
+
// writes into its program's buffers (zero-copy IOSurface wraps, made once per
|
|
13
|
+
// set of buffers), the GPU computes the rest at the same time, and the results
|
|
14
|
+
// combine. Each layer holds one program, built for the row count the layer is
|
|
15
|
+
// constructed with, and serves inputs of up to that many rows: it writes the
|
|
16
|
+
// rows it is given at the front of the program's input and reads as many back.
|
|
17
|
+
// The rows behind them hold whatever the last evaluation left; no row it returns
|
|
18
|
+
// reads them (each output row of the program depends only on its own input row).
|
|
19
|
+
// dispose releases the program. Layers that never run at once may share, named
|
|
20
|
+
// at construction (`share`): a layer whose program is identical to the other's
|
|
21
|
+
// holds that same loaded program, otherwise its program loads on the other's
|
|
22
|
+
// buffers (each at least as large); either lives until every layer holding it is
|
|
23
|
+
// disposed. Loaded programs are a machine-wide resource (AneProgram), so a
|
|
24
|
+
// graph's layers of one shape share one. Not bit-identical to the GPU path (fp16
|
|
25
|
+
// weights and activations on the ANE); gate by KL.
|
|
26
|
+
//
|
|
27
|
+
// A layer's program depends only on its widths, the fraction, its row count and
|
|
28
|
+
// its matmuls' reduction slices, never on the weights it streams, so the program
|
|
29
|
+
// each split loads is named here as a function of those (`aneMlpSplitProgram`,
|
|
30
|
+
// `aneAffineSplitProgram`): the install step compiles exactly what `build`
|
|
31
|
+
// loads.
|
|
32
|
+
|
|
33
|
+
/** Channels the ANE takes of R rows at `fraction` (64-aligned, at least 64 left on each side). */
|
|
34
|
+
export function aneChannels(R: number, fraction: number): number {
|
|
35
|
+
return Math.min(R - 64, Math.max(64, Math.round((fraction * R) / 64) * 64));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The program either whole-MLP split of hidden width D and intermediate width R
|
|
39
|
+
* loads at `fraction` for pieces of up to `rows` rows, its matmuls' reductions
|
|
40
|
+
* in `slices` slices (mlpSliceProgram's default when absent). */
|
|
41
|
+
export function aneMlpSplitProgram(D: number, R: number, fraction: number, rows: number, slices?: number): AneProgramSpec {
|
|
42
|
+
return mlpSliceProgram(D, aneChannels(R, fraction), rows, slices);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The program an affine projection split of K inputs and N outputs loads at
|
|
46
|
+
* `fraction` for pieces of up to `rows` rows, its reduction in `slices` slices
|
|
47
|
+
* (streamedMatmulProgram's default when absent). */
|
|
48
|
+
export function aneAffineSplitProgram(K: number, N: number, fraction: number, rows: number, slices?: number): AneProgramSpec {
|
|
49
|
+
return streamedMatmulProgram(K, aneChannels(N, fraction), rows, slices);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type Fill = (codes: MlxArray, scales: MlxArray, g: TrellisGeometry, rows: number, dst: MlxArray, offset: number) => MlxArray;
|
|
53
|
+
type Swiglu = (g: MlxArray, u: MlxArray) => MlxArray;
|
|
54
|
+
type Rest = { gate: TrellisLinear; up: TrellisLinear; down: TrellisLinear };
|
|
55
|
+
|
|
56
|
+
/** Dense axis-1 expansion queued with the ANE's GPU share, without an
|
|
57
|
+
* intermediate evaluation that would serialize gate, up and down. */
|
|
58
|
+
export class AneTrellisExpandReduce {
|
|
59
|
+
constructor(readonly linear: TrellisLinear) {}
|
|
60
|
+
forward(x: MlxArray): MlxArray {
|
|
61
|
+
const lead = x.shape.slice(0, -1), M = lead.reduce((a, b) => a * b, 1);
|
|
62
|
+
using input = ops.reshape(x, [M, this.linear.inFeatures]);
|
|
63
|
+
using stored = expandTrellis(this.linear.codes, this.linear.scales, this.linear.geometry, x.dtype, this.linear.variant);
|
|
64
|
+
using transposed = ops.transposeAxes(stored, [1, 0]);
|
|
65
|
+
using product = ops.matmul(input, transposed);
|
|
66
|
+
return this.linear.delta.forward(x, ops.reshape(product, [...lead, this.linear.outFeatures]));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Dense axis-0 expansion with the same submission boundary. */
|
|
70
|
+
export class AneTrellisExpandScatter {
|
|
71
|
+
constructor(readonly linear: TrellisLinear) {}
|
|
72
|
+
forward(x: MlxArray): MlxArray {
|
|
73
|
+
const lead = x.shape.slice(0, -1), M = lead.reduce((a, b) => a * b, 1);
|
|
74
|
+
using input = ops.reshape(x, [M, this.linear.inFeatures]);
|
|
75
|
+
using stored = expandTrellis(this.linear.codes, this.linear.scales, this.linear.geometry, x.dtype, this.linear.variant);
|
|
76
|
+
using product = ops.matmul(input, stored);
|
|
77
|
+
return this.linear.delta.forward(x, ops.reshape(product, [...lead, this.linear.outFeatures]));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
type GpuRest = { gate: { forward(x: MlxArray): MlxArray }; up: { forward(x: MlxArray): MlxArray }; down: { forward(x: MlxArray): MlxArray } };
|
|
81
|
+
/** The GPU projections attached to the split's stored row slices. */
|
|
82
|
+
export type AneGpuMlpRows = (rest: Rest) => GpuRest;
|
|
83
|
+
const expandedGpuRows: AneGpuMlpRows = rest => ({ gate: new AneTrellisExpandReduce(rest.gate),
|
|
84
|
+
up: new AneTrellisExpandReduce(rest.up), down: new AneTrellisExpandScatter(rest.down) });
|
|
85
|
+
|
|
86
|
+
// The public builder also supports standalone small inputs. Its compatibility
|
|
87
|
+
// projections preserve their original row-dependent kernels; written graph
|
|
88
|
+
// bindings use the fixed expanded builders below.
|
|
89
|
+
const legacyGpuRows: AneGpuMlpRows = rest => ({
|
|
90
|
+
gate: { forward: x => rest.gate.forward(x, true, false) },
|
|
91
|
+
up: { forward: x => rest.up.forward(x, true, false) },
|
|
92
|
+
down: { forward: x => rest.down.forward(x, true, false) },
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
/** Whole Trellis MLP: the ANE computes the first n intermediate channels end
|
|
96
|
+
* to end (gate and up rows, SwiGLU, and the down projection's partial sum over
|
|
97
|
+
* those channels); the GPU computes channels [n, R) through its attached projections; the
|
|
98
|
+
* two [M, D] partials add. Measured on M4 Pro: one fused slice program at
|
|
99
|
+
* 10.2 TFLOPS (D 5120, n 9600, S 2048). `hidden` holds M <= `rows` rows.
|
|
100
|
+
* The ANE starts as soon as the GPU has filled its buffers; the GPU's share is
|
|
101
|
+
* then queued whole (its weight expansions unbounded, one submission) while
|
|
102
|
+
* the host waits for the ANE. The sum is returned unevaluated: it reads the
|
|
103
|
+
* ANE output, which the program evaluates before its buffers' next run
|
|
104
|
+
* (`AneProgram.deferOutput`), unless the caller has by then. */
|
|
105
|
+
function mlpSplit(program: AneProgram, rows: number, n: number, gate: TrellisLinear, up: TrellisLinear, down: TrellisLinear,
|
|
106
|
+
rest: GpuRest, fillDown: Fill, hidden: MlxArray, swiglu: Swiglu): MlxArray {
|
|
107
|
+
const D = gate.geometry.inFeatures;
|
|
108
|
+
const lead = hidden.shape.slice(0, -1), M = lead.reduce((a, b) => a * b, 1);
|
|
109
|
+
if (M > rows) throw new Error(`ANE MLP split: ${M} rows exceed its program's ${rows}`);
|
|
110
|
+
{
|
|
111
|
+
// Inputs in symbol order: wd, wg, wu, x (borrowed wraps).
|
|
112
|
+
const wd = program.inputArray(0), wg = program.inputArray(1), wu = program.inputArray(2), x = program.inputArray(3);
|
|
113
|
+
using h2 = ops.reshape(hidden, [M * D]);
|
|
114
|
+
using d0 = fillDown(down.codes, down.scales, down.geometry, n, wd, 0);
|
|
115
|
+
using d1 = trellisFillHalf(gate.codes, gate.scales, gate.geometry, n, wg, 0);
|
|
116
|
+
using d2 = trellisFillHalf(up.codes, up.scales, up.geometry, n, wu, 0);
|
|
117
|
+
using d3 = writeHalf(x, h2, 0);
|
|
118
|
+
ops.evalAll([d0, d1, d2, d3]);
|
|
119
|
+
}
|
|
120
|
+
program.evalAsync();
|
|
121
|
+
let gpuOut: MlxArray;
|
|
122
|
+
try {
|
|
123
|
+
using g = rest.gate.forward(hidden);
|
|
124
|
+
using u = rest.up.forward(hidden);
|
|
125
|
+
using mid = swiglu(g, u);
|
|
126
|
+
gpuOut = rest.down.forward(mid);
|
|
127
|
+
ops.asyncEvalAll([gpuOut]);
|
|
128
|
+
} finally { program.wait(); }
|
|
129
|
+
try {
|
|
130
|
+
using used = program.output([M, D]); // zero-copy partial, the piece's rows
|
|
131
|
+
using b = used.astype(hidden.dtype);
|
|
132
|
+
using g2 = ops.reshape(gpuOut, [M, D]);
|
|
133
|
+
const sum = ops.add(b, g2);
|
|
134
|
+
program.deferOutput(sum); // evaluated (free once the result is) and freed by the program
|
|
135
|
+
return ops.reshape(sum, [...lead, D]);
|
|
136
|
+
} finally { gpuOut.dispose(); }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function checkMlp(name: string, gate: TrellisLinear, up: TrellisLinear, down: TrellisLinear, interleavedDown: boolean): void {
|
|
140
|
+
const g = gate.geometry, d = down.geometry, rowMajor = (x: TrellisGeometry) => !x.blockInterleave && x.L === 12 && x.T === 256;
|
|
141
|
+
if (g.axis !== 1 || up.geometry.axis !== 1 || !rowMajor(g) || !rowMajor(up.geometry) || d.axis !== 0 ||
|
|
142
|
+
d.rows !== g.rows || d.cols !== g.inFeatures || interleavedDown !== (d.blockInterleave === 2) ||
|
|
143
|
+
(interleavedDown ? d.k !== 3 : !rowMajor(d)))
|
|
144
|
+
throw new Error(`${name}: unsupported MLP geometry`);
|
|
145
|
+
}
|
|
146
|
+
function disposeLinear(lin: TrellisLinear): void { lin.codes.dispose(); lin.scales.dispose(); }
|
|
147
|
+
|
|
148
|
+
/** Either whole-MLP split; both build the same slice program (only the GPU
|
|
149
|
+
* fill differs), so either can share the other's program. */
|
|
150
|
+
export type AneMlpSplit = AneTrellisMlpSplit | AneTrellisMlpSplitK3i;
|
|
151
|
+
|
|
152
|
+
/** Whole-MLP split for a down projection with row-major codes. */
|
|
153
|
+
export class AneTrellisMlpSplit implements Disposable {
|
|
154
|
+
private constructor(readonly program: AneProgram, readonly rows: number, readonly channels: number, readonly gate: TrellisLinear,
|
|
155
|
+
readonly up: TrellisLinear, readonly down: TrellisLinear, readonly rest: Rest, readonly gpu: GpuRest) {}
|
|
156
|
+
|
|
157
|
+
/** Loads `aneMlpSplitProgram(D, R, fraction, rows, slices)` for pieces of up
|
|
158
|
+
* to `rows` rows now, from the compile cache the install step filled (a
|
|
159
|
+
* program not compiled on this Mac throws `AneBridgeError`). `share`: a split
|
|
160
|
+
* that never runs at the same time as this one; this split holds its program
|
|
161
|
+
* when the two are identical, otherwise loads one on its buffers (each at
|
|
162
|
+
* least as large). */
|
|
163
|
+
static build(gate: TrellisLinear, up: TrellisLinear, down: TrellisLinear, fraction: number, rows: number,
|
|
164
|
+
share?: AneMlpSplit, slices?: number, gpuRows: AneGpuMlpRows = legacyGpuRows): AneTrellisMlpSplit {
|
|
165
|
+
checkMlp("AneTrellisMlpSplit", gate, up, down, false);
|
|
166
|
+
const R = gate.geometry.rows, n = aneChannels(R, fraction);
|
|
167
|
+
const program = AneProgram.create(aneMlpSplitProgram(gate.geometry.inFeatures, R, fraction, rows, slices), share?.program);
|
|
168
|
+
const rest = { gate: trellisRows(gate, n, R), up: trellisRows(up, n, R), down: trellisStoredRows(down, n, R) };
|
|
169
|
+
return new AneTrellisMlpSplit(program, rows, n, gate, up, down, rest, gpuRows(rest));
|
|
170
|
+
}
|
|
171
|
+
/** hidden [.., M, D], M <= rows → MLP(hidden) [.., M, D], unevaluated (mlpSplit). */
|
|
172
|
+
forward(hidden: MlxArray, swiglu: Swiglu): MlxArray {
|
|
173
|
+
return mlpSplit(this.program, this.rows, this.channels, this.gate, this.up, this.down, this.gpu, trellisFillHalf, hidden, swiglu);
|
|
174
|
+
}
|
|
175
|
+
[Symbol.dispose](): void { this.dispose(); }
|
|
176
|
+
/** Releases this layer's program and frees its GPU rows; a shared program or buffers outlive it while another layer holds them. */
|
|
177
|
+
dispose(): void { this.program.dispose(); for (const lin of Object.values(this.rest)) disposeLinear(lin); }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Whole-MLP split for a 3-bit block-interleaved down projection. */
|
|
181
|
+
export class AneTrellisMlpSplitK3i implements Disposable {
|
|
182
|
+
private constructor(readonly program: AneProgram, readonly rows: number, readonly channels: number, readonly gate: TrellisLinear,
|
|
183
|
+
readonly up: TrellisLinear, readonly down: TrellisLinear, readonly rest: Rest, readonly gpu: GpuRest) {}
|
|
184
|
+
|
|
185
|
+
/** As AneTrellisMlpSplit.build. */
|
|
186
|
+
static build(gate: TrellisLinear, up: TrellisLinear, down: TrellisLinear, fraction: number, rows: number,
|
|
187
|
+
share?: AneMlpSplit, slices?: number, gpuRows: AneGpuMlpRows = legacyGpuRows): AneTrellisMlpSplitK3i {
|
|
188
|
+
checkMlp("AneTrellisMlpSplitK3i", gate, up, down, true);
|
|
189
|
+
const R = gate.geometry.rows, n = aneChannels(R, fraction);
|
|
190
|
+
const program = AneProgram.create(aneMlpSplitProgram(gate.geometry.inFeatures, R, fraction, rows, slices), share?.program);
|
|
191
|
+
const rest = { gate: trellisRows(gate, n, R), up: trellisRows(up, n, R), down: trellisStoredRows(down, n, R) };
|
|
192
|
+
return new AneTrellisMlpSplitK3i(program, rows, n, gate, up, down, rest, gpuRows(rest));
|
|
193
|
+
}
|
|
194
|
+
/** hidden [.., M, D], M <= rows → MLP(hidden) [.., M, D], unevaluated (mlpSplit). */
|
|
195
|
+
forward(hidden: MlxArray, swiglu: Swiglu): MlxArray {
|
|
196
|
+
return mlpSplit(this.program, this.rows, this.channels, this.gate, this.up, this.down, this.gpu, trellisFillHalfK3Interleaved,
|
|
197
|
+
hidden, swiglu);
|
|
198
|
+
}
|
|
199
|
+
[Symbol.dispose](): void { this.dispose(); }
|
|
200
|
+
/** As AneTrellisMlpSplit.dispose. */
|
|
201
|
+
dispose(): void { this.program.dispose(); for (const lin of Object.values(this.rest)) disposeLinear(lin); }
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Fixed dense GPU share for written row-major ANE graph bindings. */
|
|
205
|
+
export class AneTrellisExpandedMlpSplit {
|
|
206
|
+
static build(gate: TrellisLinear, up: TrellisLinear, down: TrellisLinear, fraction: number, rows: number,
|
|
207
|
+
share?: AneMlpSplit, slices?: number): AneTrellisMlpSplit {
|
|
208
|
+
return AneTrellisMlpSplit.build(gate, up, down, fraction, rows, share, slices, expandedGpuRows);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/** Fixed dense GPU share for written block-interleaved ANE graph bindings. */
|
|
212
|
+
export class AneTrellisExpandedMlpSplitK3i {
|
|
213
|
+
static build(gate: TrellisLinear, up: TrellisLinear, down: TrellisLinear, fraction: number, rows: number,
|
|
214
|
+
share?: AneMlpSplit, slices?: number): AneTrellisMlpSplitK3i {
|
|
215
|
+
return AneTrellisMlpSplitK3i.build(gate, up, down, fraction, rows, share, slices, expandedGpuRows);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The layer that computes an affine split's GPU rows from the projection's
|
|
220
|
+
* remaining rows (default: QuantizedLinearPlain). */
|
|
221
|
+
export type AneGpuRows = (rest: QuantizedLinear) => { forward(x: MlxArray): MlxArray };
|
|
222
|
+
|
|
223
|
+
/** Affine projection split: the first n output rows of an MLX affine
|
|
224
|
+
* projection run on the ANE from fp16 rows the GPU dequantizes straight into
|
|
225
|
+
* the streamed program's weight buffer; the GPU computes the remaining rows;
|
|
226
|
+
* outputs concatenate in row order. */
|
|
227
|
+
export class AneAffineSplit implements Disposable {
|
|
228
|
+
private constructor(readonly program: AneStreamedMatmul, readonly head: { w: MlxArray; scales: MlxArray; biases: MlxArray },
|
|
229
|
+
readonly rest: QuantizedLinear, readonly gpu: { forward(x: MlxArray): MlxArray }, readonly spec: ops.QuantSpec,
|
|
230
|
+
readonly linear: QuantizedLinear) {}
|
|
231
|
+
|
|
232
|
+
/** Loads `aneAffineSplitProgram(K, N, fraction, rows, slices)` for pieces of
|
|
233
|
+
* up to `rows` rows now, as AneTrellisMlpSplit.build loads its program.
|
|
234
|
+
* `share`: as AneTrellisMlpSplit.build. `gpuRows`: the layer over the GPU's
|
|
235
|
+
* rows. */
|
|
236
|
+
static build(lin: QuantizedLinear, fraction: number, rows: number, share?: AneAffineSplit, slices?: number,
|
|
237
|
+
gpuRows?: AneGpuRows): AneAffineSplit {
|
|
238
|
+
const N = lin.outFeatures, K = lin.inFeatures;
|
|
239
|
+
if (!lin.biases || lin.bias || lin.w.ndim !== 2 || lin.spec.mode !== "affine") throw new Error("AneAffineSplit: unsupported projection");
|
|
240
|
+
const n = aneChannels(N, fraction);
|
|
241
|
+
const program = AneStreamedMatmul.create(K, n, rows, share?.program, slices);
|
|
242
|
+
const rowsOf = (a: MlxArray, start: number, stop: number) => a.slice([start, 0], [stop, a.shape[1]!]);
|
|
243
|
+
const head = { w: rowsOf(lin.w, 0, n), scales: rowsOf(lin.scales, 0, n), biases: rowsOf(lin.biases, 0, n) };
|
|
244
|
+
const rest = new QuantizedLinear(rowsOf(lin.w, n, N), rowsOf(lin.scales, n, N), rowsOf(lin.biases, n, N), lin.spec);
|
|
245
|
+
return new AneAffineSplit(program, head, rest, gpuRows ? gpuRows(rest) : new QuantizedLinearPlain(rest), lin.spec, lin);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** x [.., M, K], M <= rows → [.., M, N], unevaluated as the MLP split's
|
|
249
|
+
* (mlpSplit): the ANE starts once its buffers are written, the GPU's rows
|
|
250
|
+
* queued after. */
|
|
251
|
+
forward(x: MlxArray): MlxArray {
|
|
252
|
+
const ane = this.program, K = ane.cin, n = ane.cout, rows = ane.seq;
|
|
253
|
+
const lead = x.shape.slice(0, -1), M = lead.reduce((a, b) => a * b, 1);
|
|
254
|
+
if (M > rows) throw new Error(`AneAffineSplit: ${M} rows exceed its program's ${rows}`);
|
|
255
|
+
{
|
|
256
|
+
using dense = ops.dequantize(this.head.w, this.head.scales, this.head.biases, this.spec);
|
|
257
|
+
using flat = ops.reshape(dense, [n * K]);
|
|
258
|
+
using x2 = ops.reshape(x, [M * K]);
|
|
259
|
+
using d0 = writeHalf(ane.weights(), flat, 0);
|
|
260
|
+
using d1 = writeHalf(ane.input(), x2, 0);
|
|
261
|
+
ops.evalAll([d0, d1]);
|
|
262
|
+
}
|
|
263
|
+
ane.evalAsync();
|
|
264
|
+
let gpu: MlxArray;
|
|
265
|
+
try { gpu = this.gpu.forward(x); ops.asyncEvalAll([gpu]); }
|
|
266
|
+
finally { ane.wait(); }
|
|
267
|
+
try {
|
|
268
|
+
using used = ane.output(M); // the piece's rows of [rows, n] fp16
|
|
269
|
+
using b = used.astype(x.dtype);
|
|
270
|
+
using g2 = ops.reshape(gpu, [M, this.rest.outFeatures]);
|
|
271
|
+
const both = ops.concatAxis([b, g2], -1);
|
|
272
|
+
ane.program.deferOutput(both); // evaluated (free once the result is) and freed by the program
|
|
273
|
+
return this.linear.delta.forward(x, ops.reshape(both, [...lead, n + this.rest.outFeatures]));
|
|
274
|
+
} finally { gpu.dispose(); }
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
[Symbol.dispose](): void { this.dispose(); }
|
|
278
|
+
/** Releases this layer's program and frees its weight rows; a shared program or buffers outlive it while another layer holds them. */
|
|
279
|
+
dispose(): void {
|
|
280
|
+
this.program.dispose();
|
|
281
|
+
for (const a of [this.head.w, this.head.scales, this.head.biases, this.rest.w, this.rest.scales, this.rest.biases!]) a.dispose();
|
|
282
|
+
}
|
|
283
|
+
}
|