@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,557 @@
|
|
|
1
|
+
import { ExpertIOSlabStore } from "../../artifacts/experts/io";
|
|
2
|
+
import {
|
|
3
|
+
ExpertUsageLedger,
|
|
4
|
+
planExpertAutoPins,
|
|
5
|
+
selectExpertLfruCandidates,
|
|
6
|
+
type ExpertAutoPinPlan,
|
|
7
|
+
} from "../../artifacts/experts/usage";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import {
|
|
10
|
+
buildExpertHintSegments,
|
|
11
|
+
ExpertResidencyManager,
|
|
12
|
+
DEFAULT_EXPERT_WORKING_SLOTS,
|
|
13
|
+
planExpertResidency,
|
|
14
|
+
type ExpertDemandTelemetry,
|
|
15
|
+
type ExpertLatencySummary,
|
|
16
|
+
type ExpertPolicyTelemetry,
|
|
17
|
+
type ExpertRepinEvent,
|
|
18
|
+
type ExpertResidencyPlan,
|
|
19
|
+
type ExpertResidencyManagerOptions,
|
|
20
|
+
} from "../../artifacts/experts/residency";
|
|
21
|
+
import type { Glm52Config } from "../../artifacts/glm52-config";
|
|
22
|
+
import { ColibriGlm52Container } from "../../artifacts/glm52/container";
|
|
23
|
+
import {
|
|
24
|
+
buildGlm52ExpertSlotLayout,
|
|
25
|
+
type Glm52ExpertSlotLayout,
|
|
26
|
+
} from "../../artifacts/glm52/expert-layout";
|
|
27
|
+
import type {
|
|
28
|
+
Glm52PilotTelemetry,
|
|
29
|
+
Glm52PilotTracker,
|
|
30
|
+
} from "./pilot";
|
|
31
|
+
import { Glm52StockStreamedExpertExecutor } from "./streamed-experts";
|
|
32
|
+
|
|
33
|
+
export interface Glm52ExpertRuntimeOptions {
|
|
34
|
+
readonly budgetBytes: number;
|
|
35
|
+
/** Dense/shared/router + KV/transient/allocator/Bun/OS reserve. */
|
|
36
|
+
readonly fixedBytes: number;
|
|
37
|
+
readonly workingSlots?: number;
|
|
38
|
+
readonly maxSlotsPerLayer?: number;
|
|
39
|
+
readonly pinned?: ReadonlyArray<{ layer: number; expertId: number }>;
|
|
40
|
+
readonly workers?: number;
|
|
41
|
+
readonly noCache?: boolean;
|
|
42
|
+
readonly libraryPath?: string;
|
|
43
|
+
readonly decodeKernel?: "stock" | "metal";
|
|
44
|
+
/** Native MTP verify width. The int8 working tier reserves topK*gamma
|
|
45
|
+
* scratch slots plus one persistent LRU slot. */
|
|
46
|
+
readonly mtpDraftTokens?: number;
|
|
47
|
+
readonly enableMtp?: boolean;
|
|
48
|
+
/** Persistent Colibri-compatible route profile. Defaults to
|
|
49
|
+
* `<modelDir>/.coli_usage`; false disables persistence. */
|
|
50
|
+
readonly usagePath?: string | false;
|
|
51
|
+
/** Opt-in G6 candidate. Explicit `pinned` entries take precedence. */
|
|
52
|
+
readonly autoPin?: boolean;
|
|
53
|
+
/** Opt-in G6 candidate: adapt at generation safe points, max four swaps. */
|
|
54
|
+
readonly liveRepin?: boolean;
|
|
55
|
+
/** Opt-in G6 observation: score next-layer router predictions, no I/O. */
|
|
56
|
+
readonly pilotMeasure?: boolean;
|
|
57
|
+
/** Opt-in G6 advisory prefetch width. Zero disables WILLNEED hints. */
|
|
58
|
+
readonly pilotHintK?: number;
|
|
59
|
+
/** Opt-in G6 measurement: score Colibri's shared-expert-corrected route. */
|
|
60
|
+
readonly pilotTwoStep?: boolean;
|
|
61
|
+
/** Opt-in G6 route trace observer for the main target tier only. */
|
|
62
|
+
readonly routeObserver?: ExpertResidencyManagerOptions["routeObserver"];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface Glm52MtpExpertRuntime {
|
|
66
|
+
readonly plan: ExpertResidencyPlan;
|
|
67
|
+
readonly store: ExpertIOSlabStore;
|
|
68
|
+
readonly manager: ExpertResidencyManager;
|
|
69
|
+
readonly executor: Glm52StockStreamedExpertExecutor;
|
|
70
|
+
readonly slotBytes: number;
|
|
71
|
+
readonly layer: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface Glm52ExpertTierTelemetry {
|
|
75
|
+
readonly demand: ExpertDemandTelemetry;
|
|
76
|
+
readonly policy: ExpertPolicyTelemetry;
|
|
77
|
+
readonly layerForward: ExpertLatencySummary;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface Glm52ExpertTurnTelemetry {
|
|
81
|
+
readonly main: Glm52ExpertTierTelemetry;
|
|
82
|
+
readonly mtp: Glm52ExpertTierTelemetry | null;
|
|
83
|
+
readonly pilot: Glm52PilotTelemetry | null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class Glm52ExpertRuntime {
|
|
87
|
+
readonly plan: ExpertResidencyPlan;
|
|
88
|
+
readonly store: ExpertIOSlabStore;
|
|
89
|
+
readonly manager: ExpertResidencyManager;
|
|
90
|
+
readonly executor: Glm52StockStreamedExpertExecutor;
|
|
91
|
+
readonly slotBytes: number;
|
|
92
|
+
readonly sparseLayerIds: readonly number[];
|
|
93
|
+
readonly mtp: Glm52MtpExpertRuntime | null;
|
|
94
|
+
readonly mtpExecutor: Glm52StockStreamedExpertExecutor | null;
|
|
95
|
+
readonly usage: ExpertUsageLedger | null;
|
|
96
|
+
readonly autoPin: ExpertAutoPinPlan | null;
|
|
97
|
+
readonly pilotMeasureEnabled: boolean;
|
|
98
|
+
readonly pilotHintK: number;
|
|
99
|
+
readonly pilotTwoStep: boolean;
|
|
100
|
+
lastRepin: readonly ExpertRepinEvent[] = Object.freeze([]);
|
|
101
|
+
lastTelemetry: Glm52ExpertTurnTelemetry | null = null;
|
|
102
|
+
#liveRepin: boolean;
|
|
103
|
+
#pilot: Glm52PilotTracker | null = null;
|
|
104
|
+
#layouts = new Map<string, Glm52ExpertSlotLayout>();
|
|
105
|
+
#mtpLayouts = new Map<string, Glm52ExpertSlotLayout>();
|
|
106
|
+
|
|
107
|
+
private constructor(
|
|
108
|
+
plan: ExpertResidencyPlan,
|
|
109
|
+
store: ExpertIOSlabStore,
|
|
110
|
+
manager: ExpertResidencyManager,
|
|
111
|
+
executor: Glm52StockStreamedExpertExecutor,
|
|
112
|
+
slotBytes: number,
|
|
113
|
+
sparseLayerIds: readonly number[],
|
|
114
|
+
mtp: Glm52MtpExpertRuntime | null,
|
|
115
|
+
usage: ExpertUsageLedger | null,
|
|
116
|
+
autoPin: ExpertAutoPinPlan | null,
|
|
117
|
+
liveRepin: boolean,
|
|
118
|
+
pilotMeasure: boolean,
|
|
119
|
+
pilotHintK: number,
|
|
120
|
+
pilotTwoStep: boolean,
|
|
121
|
+
) {
|
|
122
|
+
this.plan = plan;
|
|
123
|
+
this.store = store;
|
|
124
|
+
this.manager = manager;
|
|
125
|
+
this.executor = executor;
|
|
126
|
+
this.slotBytes = slotBytes;
|
|
127
|
+
this.sparseLayerIds = sparseLayerIds;
|
|
128
|
+
this.mtp = mtp;
|
|
129
|
+
this.mtpExecutor = mtp?.executor ?? null;
|
|
130
|
+
this.usage = usage;
|
|
131
|
+
this.autoPin = autoPin;
|
|
132
|
+
this.#liveRepin = liveRepin;
|
|
133
|
+
this.pilotMeasureEnabled = pilotMeasure;
|
|
134
|
+
this.pilotHintK = pilotHintK;
|
|
135
|
+
this.pilotTwoStep = pilotTwoStep;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
static async open(
|
|
139
|
+
modelDir: string,
|
|
140
|
+
config: Glm52Config,
|
|
141
|
+
options: Glm52ExpertRuntimeOptions,
|
|
142
|
+
): Promise<Glm52ExpertRuntime> {
|
|
143
|
+
const container = ColibriGlm52Container.open(modelDir);
|
|
144
|
+
const sparseLayerIds = Array.from(
|
|
145
|
+
{ length: config.numHiddenLayers - config.firstKDenseReplace },
|
|
146
|
+
(_, index) => config.firstKDenseReplace + index,
|
|
147
|
+
);
|
|
148
|
+
if (sparseLayerIds.length === 0)
|
|
149
|
+
throw new Error("GLM expert residency requires at least one sparse layer");
|
|
150
|
+
const representative = buildGlm52ExpertSlotLayout(
|
|
151
|
+
container,
|
|
152
|
+
config,
|
|
153
|
+
sparseLayerIds[0]!,
|
|
154
|
+
0,
|
|
155
|
+
);
|
|
156
|
+
const capabilities = container.capabilities(config);
|
|
157
|
+
const mtpLayer = config.numHiddenLayers;
|
|
158
|
+
const mtpRepresentative = capabilities.hasMtp && options.enableMtp !== false
|
|
159
|
+
? buildGlm52ExpertSlotLayout(container, config, mtpLayer, 0)
|
|
160
|
+
: null;
|
|
161
|
+
const mtpDraftTokens = options.mtpDraftTokens ?? 3;
|
|
162
|
+
if (!Number.isSafeInteger(mtpDraftTokens) || mtpDraftTokens < 1)
|
|
163
|
+
throw new Error("GLM MTP draft token count must be a positive safe integer");
|
|
164
|
+
const pilotHintK = options.pilotHintK ?? 0;
|
|
165
|
+
if (!Number.isSafeInteger(pilotHintK) || pilotHintK < 0 ||
|
|
166
|
+
pilotHintK > config.numExpertsPerToken) {
|
|
167
|
+
throw new Error(
|
|
168
|
+
`GLM PILOT hint K must be in 0..${config.numExpertsPerToken}`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
const pilotTwoStep = options.pilotTwoStep === true;
|
|
172
|
+
if (pilotTwoStep && pilotHintK > 0) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
"GLM PILOT two-step measurement cannot submit baseline-only hints",
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
const mtpWorkingSlots = mtpRepresentative
|
|
178
|
+
? Math.min(
|
|
179
|
+
config.numRoutedExperts,
|
|
180
|
+
config.numExpertsPerToken * mtpDraftTokens,
|
|
181
|
+
)
|
|
182
|
+
: 0;
|
|
183
|
+
const usagePath = options.usagePath === false
|
|
184
|
+
? null
|
|
185
|
+
: options.usagePath ?? join(modelDir, ".coli_usage");
|
|
186
|
+
const usage = usagePath === null
|
|
187
|
+
? null
|
|
188
|
+
: ExpertUsageLedger.open({
|
|
189
|
+
path: usagePath,
|
|
190
|
+
layers: mtpRepresentative
|
|
191
|
+
? [...sparseLayerIds, mtpLayer]
|
|
192
|
+
: sparseLayerIds,
|
|
193
|
+
expertsPerLayer: config.numRoutedExperts,
|
|
194
|
+
onWarning: (message) => console.warn(message),
|
|
195
|
+
});
|
|
196
|
+
if ((options.autoPin || options.liveRepin) && !usage)
|
|
197
|
+
throw new Error("GLM expert learning policies require persistent usage");
|
|
198
|
+
const autoPin = options.autoPin && options.pinned === undefined && usage
|
|
199
|
+
? planExpertAutoPins({
|
|
200
|
+
ledger: usage,
|
|
201
|
+
residentTierBudgetBytes: Math.max(
|
|
202
|
+
0,
|
|
203
|
+
options.budgetBytes - options.fixedBytes -
|
|
204
|
+
(options.workingSlots ?? DEFAULT_EXPERT_WORKING_SLOTS) *
|
|
205
|
+
representative.slotBytes -
|
|
206
|
+
mtpWorkingSlots * (mtpRepresentative?.slotBytes ?? 0),
|
|
207
|
+
),
|
|
208
|
+
mandatoryResidentBytes:
|
|
209
|
+
sparseLayerIds.length * representative.slotBytes +
|
|
210
|
+
(mtpRepresentative?.slotBytes ?? 0),
|
|
211
|
+
slotBytes: (layer) => {
|
|
212
|
+
if (layer === mtpLayer && mtpRepresentative)
|
|
213
|
+
return mtpRepresentative.slotBytes;
|
|
214
|
+
if (sparseLayerIds.includes(layer)) return representative.slotBytes;
|
|
215
|
+
throw new RangeError(`auto-pin layer ${layer} is not managed`);
|
|
216
|
+
},
|
|
217
|
+
})
|
|
218
|
+
: null;
|
|
219
|
+
const autoPins = autoPin?.pins ?? [];
|
|
220
|
+
const pinned = options.pinned ?? autoPins
|
|
221
|
+
.filter((item) => item.layer !== mtpLayer)
|
|
222
|
+
.map(({ layer, expertId }) => ({ layer, expertId }));
|
|
223
|
+
const mtpPinned = autoPins
|
|
224
|
+
.filter((item) => item.layer === mtpLayer)
|
|
225
|
+
.map(({ layer, expertId }) => ({ layer, expertId }));
|
|
226
|
+
for (const item of pinned) {
|
|
227
|
+
if (!sparseLayerIds.includes(item.layer) || item.expertId < 0 ||
|
|
228
|
+
item.expertId >= config.numRoutedExperts ||
|
|
229
|
+
!Number.isSafeInteger(item.expertId)) {
|
|
230
|
+
throw new RangeError(`invalid pinned expert ${item.layer}:${item.expertId}`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const mtpPlan = mtpRepresentative
|
|
234
|
+
? planExpertResidency({
|
|
235
|
+
// physicalFootprint() is process-wide, not slab-local. Give the
|
|
236
|
+
// auxiliary manager the same global ceiling as the main manager;
|
|
237
|
+
// the main plan below is the single source of accounting truth and
|
|
238
|
+
// includes this MTP slab in fixedBytes.
|
|
239
|
+
budgetBytes: options.budgetBytes,
|
|
240
|
+
fixedBytes: 0,
|
|
241
|
+
slotBytes: mtpRepresentative.slotBytes,
|
|
242
|
+
sparseLayers: 1,
|
|
243
|
+
workingSlots: mtpWorkingSlots,
|
|
244
|
+
pinnedExperts: mtpPinned.length,
|
|
245
|
+
maxSlotsPerLayer: 1,
|
|
246
|
+
})
|
|
247
|
+
: null;
|
|
248
|
+
const plan = planExpertResidency({
|
|
249
|
+
budgetBytes: options.budgetBytes,
|
|
250
|
+
fixedBytes: options.fixedBytes + (mtpPlan?.slabBytes ?? 0),
|
|
251
|
+
slotBytes: representative.slotBytes,
|
|
252
|
+
sparseLayers: sparseLayerIds.length,
|
|
253
|
+
workingSlots: options.workingSlots,
|
|
254
|
+
pinnedExperts: pinned.length,
|
|
255
|
+
maxSlotsPerLayer: options.maxSlotsPerLayer,
|
|
256
|
+
});
|
|
257
|
+
const mainFiles = container.files
|
|
258
|
+
.filter((file) => file.family === "main")
|
|
259
|
+
.map((file) => file.path);
|
|
260
|
+
const fileIndex = new Map(
|
|
261
|
+
mainFiles.map((path, index) => [path, index]),
|
|
262
|
+
);
|
|
263
|
+
const mtpFiles = container.files
|
|
264
|
+
.filter((file) => file.family === "mtp")
|
|
265
|
+
.map((file) => file.path);
|
|
266
|
+
const mtpFileIndex = new Map(
|
|
267
|
+
mtpFiles.map((path, index) => [path, index]),
|
|
268
|
+
);
|
|
269
|
+
const layouts = new Map<string, Glm52ExpertSlotLayout>();
|
|
270
|
+
const layout = (layer: number, expertId: number): Glm52ExpertSlotLayout => {
|
|
271
|
+
const key = `${layer}:${expertId}`;
|
|
272
|
+
let value = layouts.get(key);
|
|
273
|
+
if (!value) {
|
|
274
|
+
value = buildGlm52ExpertSlotLayout(
|
|
275
|
+
container,
|
|
276
|
+
config,
|
|
277
|
+
layer,
|
|
278
|
+
expertId,
|
|
279
|
+
);
|
|
280
|
+
if (value.slotBytes !== representative.slotBytes)
|
|
281
|
+
throw new Error(
|
|
282
|
+
`${key}: expert slot ${value.slotBytes} != ${representative.slotBytes}`,
|
|
283
|
+
);
|
|
284
|
+
layouts.set(key, value);
|
|
285
|
+
}
|
|
286
|
+
return value;
|
|
287
|
+
};
|
|
288
|
+
const store = new ExpertIOSlabStore(mainFiles, {
|
|
289
|
+
slots: plan.totalSlots,
|
|
290
|
+
slotBytes: representative.slotBytes,
|
|
291
|
+
workers: options.workers,
|
|
292
|
+
noCache: options.noCache,
|
|
293
|
+
// These slots are the bounded resident/working expert cache, not an
|
|
294
|
+
// expendable file cache. Letting macOS compress them turns every warm
|
|
295
|
+
// forward into decompression churn and invalidates the G5 equation.
|
|
296
|
+
wireSlots: true,
|
|
297
|
+
libraryPath: options.libraryPath,
|
|
298
|
+
});
|
|
299
|
+
let mtpStore: ExpertIOSlabStore | null = null;
|
|
300
|
+
let executor: Glm52StockStreamedExpertExecutor | null = null;
|
|
301
|
+
let mtpExecutor: Glm52StockStreamedExpertExecutor | null = null;
|
|
302
|
+
try {
|
|
303
|
+
if (mtpPlan && mtpRepresentative) {
|
|
304
|
+
mtpStore = new ExpertIOSlabStore(mtpFiles, {
|
|
305
|
+
slots: mtpPlan.totalSlots,
|
|
306
|
+
slotBytes: mtpRepresentative.slotBytes,
|
|
307
|
+
workers: options.workers,
|
|
308
|
+
noCache: options.noCache,
|
|
309
|
+
wireSlots: true,
|
|
310
|
+
libraryPath: options.libraryPath,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
const manager = new ExpertResidencyManager({
|
|
314
|
+
plan,
|
|
315
|
+
sparseLayerIds,
|
|
316
|
+
backend: store,
|
|
317
|
+
pinned,
|
|
318
|
+
usage: usage ?? undefined,
|
|
319
|
+
routeObserver: options.routeObserver,
|
|
320
|
+
locate: (layer, expertId) => {
|
|
321
|
+
const value = layout(layer, expertId);
|
|
322
|
+
const segments = value.segments.map((segment) => {
|
|
323
|
+
const file = fileIndex.get(segment.file);
|
|
324
|
+
if (file === undefined)
|
|
325
|
+
throw new Error(`${layer}:${expertId}: non-main expert shard`);
|
|
326
|
+
return {
|
|
327
|
+
file,
|
|
328
|
+
offset: segment.sourceOffset,
|
|
329
|
+
destination: segment.destinationOffset,
|
|
330
|
+
length: segment.length,
|
|
331
|
+
};
|
|
332
|
+
});
|
|
333
|
+
const hintSegments = buildExpertHintSegments(
|
|
334
|
+
segments,
|
|
335
|
+
value.scaleOffset,
|
|
336
|
+
options.noCache !== false,
|
|
337
|
+
);
|
|
338
|
+
return {
|
|
339
|
+
layer,
|
|
340
|
+
expertId,
|
|
341
|
+
segments,
|
|
342
|
+
hintSegments,
|
|
343
|
+
};
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
executor = new Glm52StockStreamedExpertExecutor({
|
|
347
|
+
manager,
|
|
348
|
+
store,
|
|
349
|
+
layout,
|
|
350
|
+
hiddenSize: config.hiddenSize,
|
|
351
|
+
// G1/G3 quiet-machine selection: custom Metal wins eligible M=1
|
|
352
|
+
// expert jobs; the hybrid executor falls back to stock MLX for
|
|
353
|
+
// multi-row ragged/prefill jobs. Keep an explicit "stock" override
|
|
354
|
+
// for oracle and diagnostic runs.
|
|
355
|
+
decodeKernel: options.decodeKernel ?? "metal",
|
|
356
|
+
});
|
|
357
|
+
let mtp: Glm52MtpExpertRuntime | null = null;
|
|
358
|
+
const mtpLayouts = new Map<string, Glm52ExpertSlotLayout>();
|
|
359
|
+
if (mtpPlan && mtpRepresentative && mtpStore) {
|
|
360
|
+
const mtpLayout = (
|
|
361
|
+
layer: number,
|
|
362
|
+
expertId: number,
|
|
363
|
+
): Glm52ExpertSlotLayout => {
|
|
364
|
+
if (layer !== mtpLayer)
|
|
365
|
+
throw new Error(`MTP expert layer ${layer} != ${mtpLayer}`);
|
|
366
|
+
const key = `${layer}:${expertId}`;
|
|
367
|
+
let value = mtpLayouts.get(key);
|
|
368
|
+
if (!value) {
|
|
369
|
+
value = buildGlm52ExpertSlotLayout(
|
|
370
|
+
container,
|
|
371
|
+
config,
|
|
372
|
+
layer,
|
|
373
|
+
expertId,
|
|
374
|
+
);
|
|
375
|
+
if (value.bits !== 8)
|
|
376
|
+
throw new Error(`${key}: MTP expert must be signed int8`);
|
|
377
|
+
if (value.slotBytes !== mtpRepresentative.slotBytes) {
|
|
378
|
+
throw new Error(
|
|
379
|
+
`${key}: MTP expert slot ${value.slotBytes} != ` +
|
|
380
|
+
`${mtpRepresentative.slotBytes}`,
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
mtpLayouts.set(key, value);
|
|
384
|
+
}
|
|
385
|
+
return value;
|
|
386
|
+
};
|
|
387
|
+
const mtpManager = new ExpertResidencyManager({
|
|
388
|
+
plan: mtpPlan,
|
|
389
|
+
sparseLayerIds: [mtpLayer],
|
|
390
|
+
backend: mtpStore,
|
|
391
|
+
pinned: mtpPinned,
|
|
392
|
+
usage: usage ?? undefined,
|
|
393
|
+
locate: (layer, expertId) => {
|
|
394
|
+
const value = mtpLayout(layer, expertId);
|
|
395
|
+
return {
|
|
396
|
+
layer,
|
|
397
|
+
expertId,
|
|
398
|
+
segments: value.segments.map((segment) => {
|
|
399
|
+
const file = mtpFileIndex.get(segment.file);
|
|
400
|
+
if (file === undefined)
|
|
401
|
+
throw new Error(`${layer}:${expertId}: non-MTP expert shard`);
|
|
402
|
+
return {
|
|
403
|
+
file,
|
|
404
|
+
offset: segment.sourceOffset,
|
|
405
|
+
destination: segment.destinationOffset,
|
|
406
|
+
length: segment.length,
|
|
407
|
+
};
|
|
408
|
+
}),
|
|
409
|
+
};
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
mtpExecutor = new Glm52StockStreamedExpertExecutor({
|
|
413
|
+
manager: mtpManager,
|
|
414
|
+
store: mtpStore,
|
|
415
|
+
layout: mtpLayout,
|
|
416
|
+
hiddenSize: config.hiddenSize,
|
|
417
|
+
// The signed-Q8 custom kernel is one fixed family for both S=1
|
|
418
|
+
// drafts and S>1 accepted-token absorption (SPEC_PIN equivalent).
|
|
419
|
+
// Preserve the explicit stock-kernel diagnostic escape hatch across
|
|
420
|
+
// both the target and MTP tiers.
|
|
421
|
+
decodeKernel: options.decodeKernel ?? "metal",
|
|
422
|
+
});
|
|
423
|
+
mtp = {
|
|
424
|
+
plan: mtpPlan,
|
|
425
|
+
store: mtpStore,
|
|
426
|
+
manager: mtpManager,
|
|
427
|
+
executor: mtpExecutor,
|
|
428
|
+
slotBytes: mtpRepresentative.slotBytes,
|
|
429
|
+
layer: mtpLayer,
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
const runtime = new Glm52ExpertRuntime(
|
|
433
|
+
plan,
|
|
434
|
+
store,
|
|
435
|
+
manager,
|
|
436
|
+
executor,
|
|
437
|
+
representative.slotBytes,
|
|
438
|
+
Object.freeze(sparseLayerIds),
|
|
439
|
+
mtp,
|
|
440
|
+
usage,
|
|
441
|
+
autoPin,
|
|
442
|
+
options.liveRepin === true,
|
|
443
|
+
options.pilotMeasure === true || pilotHintK > 0 || pilotTwoStep,
|
|
444
|
+
pilotHintK,
|
|
445
|
+
pilotTwoStep,
|
|
446
|
+
);
|
|
447
|
+
await manager.preloadPinned();
|
|
448
|
+
await mtp?.manager.preloadPinned();
|
|
449
|
+
runtime.#layouts = layouts;
|
|
450
|
+
runtime.#mtpLayouts = mtpLayouts;
|
|
451
|
+
return runtime;
|
|
452
|
+
} catch (error) {
|
|
453
|
+
mtpExecutor?.dispose();
|
|
454
|
+
executor?.dispose();
|
|
455
|
+
mtpStore?.close();
|
|
456
|
+
store.close();
|
|
457
|
+
throw error;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
close(): void {
|
|
462
|
+
let flushError: unknown = null;
|
|
463
|
+
try {
|
|
464
|
+
this.flushUsage();
|
|
465
|
+
} catch (error) {
|
|
466
|
+
flushError = error;
|
|
467
|
+
}
|
|
468
|
+
this.mtp?.executor.dispose();
|
|
469
|
+
this.mtp?.store.close();
|
|
470
|
+
this.executor.dispose();
|
|
471
|
+
this.store.close();
|
|
472
|
+
if (flushError) throw flushError;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** Generation safe-point: atomically publish target + MTP route counts. */
|
|
476
|
+
flushUsage(): void {
|
|
477
|
+
this.usage?.flush();
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
attachPilot(pilot: Glm52PilotTracker): void {
|
|
481
|
+
if (!this.pilotMeasureEnabled)
|
|
482
|
+
throw new Error("cannot attach GLM PILOT when measurement is disabled");
|
|
483
|
+
if (this.#pilot)
|
|
484
|
+
throw new Error("GLM PILOT tracker is already attached");
|
|
485
|
+
this.#pilot = pilot;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** Turn boundary: adapt the shared target/MTP tier, decay heat, then save. */
|
|
489
|
+
async finishUsage(): Promise<void> {
|
|
490
|
+
const mainDemand = this.manager.drainDemandTelemetry();
|
|
491
|
+
const mainForward = this.executor.drainForwardTelemetry();
|
|
492
|
+
const mtpDemand = this.mtp?.manager.drainDemandTelemetry() ?? null;
|
|
493
|
+
const mtpForward = this.mtp?.executor.drainForwardTelemetry() ?? null;
|
|
494
|
+
const pilot = this.#pilot?.drainTelemetry() ?? null;
|
|
495
|
+
const publishTelemetry = (): void => {
|
|
496
|
+
this.lastTelemetry = {
|
|
497
|
+
main: {
|
|
498
|
+
demand: mainDemand,
|
|
499
|
+
policy: this.manager.drainPolicyTelemetry(),
|
|
500
|
+
layerForward: mainForward,
|
|
501
|
+
},
|
|
502
|
+
mtp: this.mtp && mtpDemand && mtpForward
|
|
503
|
+
? {
|
|
504
|
+
demand: mtpDemand,
|
|
505
|
+
policy: this.mtp.manager.drainPolicyTelemetry(),
|
|
506
|
+
layerForward: mtpForward,
|
|
507
|
+
}
|
|
508
|
+
: null,
|
|
509
|
+
pilot,
|
|
510
|
+
};
|
|
511
|
+
};
|
|
512
|
+
if (!this.#liveRepin || !this.usage) {
|
|
513
|
+
this.lastRepin = Object.freeze([]);
|
|
514
|
+
publishTelemetry();
|
|
515
|
+
this.flushUsage();
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
const candidates = [
|
|
519
|
+
...this.manager.repinCandidates().map((candidate) => ({
|
|
520
|
+
manager: this.manager,
|
|
521
|
+
candidate,
|
|
522
|
+
})),
|
|
523
|
+
...(this.mtp?.manager.repinCandidates() ?? []).map((candidate) => ({
|
|
524
|
+
manager: this.mtp!.manager,
|
|
525
|
+
candidate,
|
|
526
|
+
})),
|
|
527
|
+
];
|
|
528
|
+
const selected = new Set(selectExpertLfruCandidates(
|
|
529
|
+
candidates.map((item) => item.candidate),
|
|
530
|
+
4,
|
|
531
|
+
));
|
|
532
|
+
const planned = candidates.filter((item) => selected.has(item.candidate));
|
|
533
|
+
const events: ExpertRepinEvent[] = [];
|
|
534
|
+
let repinError: unknown = null;
|
|
535
|
+
try {
|
|
536
|
+
for (const item of planned)
|
|
537
|
+
events.push(await item.manager.applyRepin(item.candidate));
|
|
538
|
+
} catch (error) {
|
|
539
|
+
repinError = error;
|
|
540
|
+
}
|
|
541
|
+
this.lastRepin = Object.freeze(events.slice());
|
|
542
|
+
this.usage.decayHeat();
|
|
543
|
+
publishTelemetry();
|
|
544
|
+
try {
|
|
545
|
+
this.flushUsage();
|
|
546
|
+
} catch (flushError) {
|
|
547
|
+
if (repinError) {
|
|
548
|
+
throw new AggregateError(
|
|
549
|
+
[repinError, flushError],
|
|
550
|
+
"live repin and expert usage flush both failed",
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
throw flushError;
|
|
554
|
+
}
|
|
555
|
+
if (repinError) throw repinError;
|
|
556
|
+
}
|
|
557
|
+
}
|