@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,544 @@
|
|
|
1
|
+
// Written Gemma 4 26b graph. Layer order and every operation binding are literal.
|
|
2
|
+
import type { MlxArray } from "@mlx-bun/mlx/array";
|
|
3
|
+
import * as ops from "@mlx-bun/mlx/ops";
|
|
4
|
+
import type { ModelConfig } from "../../artifacts/config";
|
|
5
|
+
import type { Weights } from "../../artifacts/weights";
|
|
6
|
+
import type { Cache } from "../../contracts/mlx/cache";
|
|
7
|
+
import type { MlxDeclaredGraph, MlxTokenPhases, MlxRowTable, MlxCapturingForward, MlxHeadForward, MlxPhaseForward, MlxKnownOperations, MlxLayerCapture, MlxPromptRange, MlxMixedRange, MlxPhaseGroup, MlxCacheLayer } from "../../contracts/mlx/graph";
|
|
8
|
+
import type { Vision, MediaSidecarProbes } from "../../contracts/mlx/media";
|
|
9
|
+
import { QuantizedEmbedding } from "../../layers/quantized-embedding";
|
|
10
|
+
import { RMSNorm } from "../../layers/normalization";
|
|
11
|
+
import { LoraState } from "../../layers/lora";
|
|
12
|
+
import { disposing } from "../../layers/helpers";
|
|
13
|
+
import { compiledLogitSoftcap } from "../../kernels/softcap";
|
|
14
|
+
import { logitSoftcap } from "../../kernels/logits";
|
|
15
|
+
import { ownResource, disposeResources } from "../../runtime/resources";
|
|
16
|
+
import { declareGraph } from "../capabilities";
|
|
17
|
+
import { GEMMA_GRAPH_FINGERPRINTS } from "./family";
|
|
18
|
+
import { CompiledDecode } from "./compiled-decode";
|
|
19
|
+
import { bindGemma4MediaInput, gemma4DraftTarget, gemma4MediaEncoders } from "./media";
|
|
20
|
+
import { type Gemma4Phase } from "./blocks";
|
|
21
|
+
import { SlidingAttention, FullAttention, FullKEqVAttention, SharedSlidingAttention, SharedFullAttention, GemmaLayerWeights, GemmaInputLayerWeights, GemmaMoeLayerWeights, GemmaDenseLayer, GemmaInputLayer, GemmaMoeLayer, CompiledGegluMLP, PlainGegluMLP, CompiledGeglu, SortedSwitchGLU, UnsortedSwitchGLU, FixedExperts } from "./written-blocks";
|
|
22
|
+
import { GemmaForwardScope, GemmaInputForwardScope, GemmaMixedScope, ownMixed, donorMixed, sharedMixed, denseLoraTargets, inputLoraTargets, weightBytes, GemmaBuildScope } from "./written-forward";
|
|
23
|
+
export const FINGERPRINT = GEMMA_GRAPH_FINGERPRINTS["26b"];
|
|
24
|
+
|
|
25
|
+
/** One decoder layer as the composition fixes it: its attention type; the layer
|
|
26
|
+
* whose cache it reads (itself when it owns one, else the last donor of its type,
|
|
27
|
+
* whose read it attends in the same forward); that cache's affine width and group
|
|
28
|
+
* under the composition's KV scheme (null: bf16); whether its keys are its values;
|
|
29
|
+
* whether it carries a layer scalar. */
|
|
30
|
+
export interface Gemma4Layer {
|
|
31
|
+
readonly attention: "sliding" | "full";
|
|
32
|
+
readonly cache: number;
|
|
33
|
+
readonly kv: { readonly bits: number; readonly groupSize: number } | null;
|
|
34
|
+
readonly kEqV: boolean;
|
|
35
|
+
readonly layerScalar: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The artifact's layers and kv_config, from mlx-community/gemma-4-26B-A4B-it-OptiQ-4bit@dbfd2a779b038b267bb20ff95dad717f42e4de16. */
|
|
39
|
+
export const LAYERS: readonly Gemma4Layer[] = Object.freeze([
|
|
40
|
+
/* 0 */ { attention: "sliding", cache: 0, kv: { bits: 8, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
41
|
+
/* 1 */ { attention: "sliding", cache: 1, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
42
|
+
/* 2 */ { attention: "sliding", cache: 2, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
43
|
+
/* 3 */ { attention: "sliding", cache: 3, kv: { bits: 8, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
44
|
+
/* 4 */ { attention: "sliding", cache: 4, kv: { bits: 8, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
45
|
+
/* 5 */ { attention: "full", cache: 5, kv: { bits: 8, groupSize: 64 }, kEqV: true, layerScalar: true },
|
|
46
|
+
/* 6 */ { attention: "sliding", cache: 6, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
47
|
+
/* 7 */ { attention: "sliding", cache: 7, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
48
|
+
/* 8 */ { attention: "sliding", cache: 8, kv: { bits: 8, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
49
|
+
/* 9 */ { attention: "sliding", cache: 9, kv: { bits: 8, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
50
|
+
/* 10 */ { attention: "sliding", cache: 10, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
51
|
+
/* 11 */ { attention: "full", cache: 11, kv: { bits: 8, groupSize: 64 }, kEqV: true, layerScalar: true },
|
|
52
|
+
/* 12 */ { attention: "sliding", cache: 12, kv: { bits: 8, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
53
|
+
/* 13 */ { attention: "sliding", cache: 13, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
54
|
+
/* 14 */ { attention: "sliding", cache: 14, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
55
|
+
/* 15 */ { attention: "sliding", cache: 15, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
56
|
+
/* 16 */ { attention: "sliding", cache: 16, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
57
|
+
/* 17 */ { attention: "full", cache: 17, kv: { bits: 4, groupSize: 64 }, kEqV: true, layerScalar: true },
|
|
58
|
+
/* 18 */ { attention: "sliding", cache: 18, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
59
|
+
/* 19 */ { attention: "sliding", cache: 19, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
60
|
+
/* 20 */ { attention: "sliding", cache: 20, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
61
|
+
/* 21 */ { attention: "sliding", cache: 21, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
62
|
+
/* 22 */ { attention: "sliding", cache: 22, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
63
|
+
/* 23 */ { attention: "full", cache: 23, kv: { bits: 4, groupSize: 64 }, kEqV: true, layerScalar: true },
|
|
64
|
+
/* 24 */ { attention: "sliding", cache: 24, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
65
|
+
/* 25 */ { attention: "sliding", cache: 25, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
66
|
+
/* 26 */ { attention: "sliding", cache: 26, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
67
|
+
/* 27 */ { attention: "sliding", cache: 27, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
68
|
+
/* 28 */ { attention: "sliding", cache: 28, kv: { bits: 4, groupSize: 64 }, kEqV: false, layerScalar: true },
|
|
69
|
+
/* 29 */ { attention: "full", cache: 29, kv: { bits: 4, groupSize: 64 }, kEqV: true, layerScalar: true },
|
|
70
|
+
]);
|
|
71
|
+
|
|
72
|
+
/** Layers [0, CACHES) own a cache. */
|
|
73
|
+
const CACHES = 30;
|
|
74
|
+
const PREFIX = "language_model.model";
|
|
75
|
+
const FINAL_LOGIT_SOFTCAP = 30;
|
|
76
|
+
|
|
77
|
+
export const DECODE_ROWS = 8;
|
|
78
|
+
export const VERIFY_ROWS = 64;
|
|
79
|
+
|
|
80
|
+
export class Gemma4Moe implements MlxDeclaredGraph, MlxTokenPhases {
|
|
81
|
+
readonly prefixBase = PREFIX;
|
|
82
|
+
readonly loraState = new LoraState();
|
|
83
|
+
readonly weightsBytes: number;
|
|
84
|
+
readonly embed: QuantizedEmbedding;
|
|
85
|
+
readonly finalNorm: RMSNorm;
|
|
86
|
+
readonly embedScale: number;
|
|
87
|
+
readonly windowSize: number;
|
|
88
|
+
readonly numDonors = CACHES;
|
|
89
|
+
readonly perLayerWidth = 0;
|
|
90
|
+
readonly graphCapabilities = declareGraph({ media: { input: "embeddings", video: false }, hiddenLayerTaps: true });
|
|
91
|
+
makeCache!: () => Cache[];
|
|
92
|
+
readonly cacheLayers: readonly MlxCacheLayer[];
|
|
93
|
+
readonly layers: readonly GemmaMoeLayer[];
|
|
94
|
+
readonly traceLayers: readonly GemmaDenseLayer[];
|
|
95
|
+
readonly #constants: ReturnType<typeof ownResource<MlxArray[]>>;
|
|
96
|
+
readonly #parts: readonly GemmaMoeLayerWeights[];
|
|
97
|
+
readonly prompt: readonly MlxPromptRange[];
|
|
98
|
+
readonly promptChunk: typeof this.prompt;
|
|
99
|
+
readonly multiSequencePromptChunk: typeof this.multiSequencePrompt;
|
|
100
|
+
readonly multiSequencePrompt: readonly MlxPromptRange[];
|
|
101
|
+
readonly decode: MlxRowTable<MlxCapturingForward>;
|
|
102
|
+
readonly verify: MlxRowTable<MlxCapturingForward>;
|
|
103
|
+
readonly appendKnown: MlxKnownOperations;
|
|
104
|
+
readonly logits: MlxRowTable<MlxHeadForward>;
|
|
105
|
+
readonly mixed: readonly MlxMixedRange[];
|
|
106
|
+
readonly #l0: GemmaMoeLayer;
|
|
107
|
+
readonly #t0: GemmaMoeLayer;
|
|
108
|
+
readonly #l1: GemmaMoeLayer;
|
|
109
|
+
readonly #t1: GemmaMoeLayer;
|
|
110
|
+
readonly #l2: GemmaMoeLayer;
|
|
111
|
+
readonly #t2: GemmaMoeLayer;
|
|
112
|
+
readonly #l3: GemmaMoeLayer;
|
|
113
|
+
readonly #t3: GemmaMoeLayer;
|
|
114
|
+
readonly #l4: GemmaMoeLayer;
|
|
115
|
+
readonly #t4: GemmaMoeLayer;
|
|
116
|
+
readonly #l5: GemmaMoeLayer;
|
|
117
|
+
readonly #t5: GemmaMoeLayer;
|
|
118
|
+
readonly #l6: GemmaMoeLayer;
|
|
119
|
+
readonly #t6: GemmaMoeLayer;
|
|
120
|
+
readonly #l7: GemmaMoeLayer;
|
|
121
|
+
readonly #t7: GemmaMoeLayer;
|
|
122
|
+
readonly #l8: GemmaMoeLayer;
|
|
123
|
+
readonly #t8: GemmaMoeLayer;
|
|
124
|
+
readonly #l9: GemmaMoeLayer;
|
|
125
|
+
readonly #t9: GemmaMoeLayer;
|
|
126
|
+
readonly #l10: GemmaMoeLayer;
|
|
127
|
+
readonly #t10: GemmaMoeLayer;
|
|
128
|
+
readonly #l11: GemmaMoeLayer;
|
|
129
|
+
readonly #t11: GemmaMoeLayer;
|
|
130
|
+
readonly #l12: GemmaMoeLayer;
|
|
131
|
+
readonly #t12: GemmaMoeLayer;
|
|
132
|
+
readonly #l13: GemmaMoeLayer;
|
|
133
|
+
readonly #t13: GemmaMoeLayer;
|
|
134
|
+
readonly #l14: GemmaMoeLayer;
|
|
135
|
+
readonly #t14: GemmaMoeLayer;
|
|
136
|
+
readonly #l15: GemmaMoeLayer;
|
|
137
|
+
readonly #t15: GemmaMoeLayer;
|
|
138
|
+
readonly #l16: GemmaMoeLayer;
|
|
139
|
+
readonly #t16: GemmaMoeLayer;
|
|
140
|
+
readonly #l17: GemmaMoeLayer;
|
|
141
|
+
readonly #t17: GemmaMoeLayer;
|
|
142
|
+
readonly #l18: GemmaMoeLayer;
|
|
143
|
+
readonly #t18: GemmaMoeLayer;
|
|
144
|
+
readonly #l19: GemmaMoeLayer;
|
|
145
|
+
readonly #t19: GemmaMoeLayer;
|
|
146
|
+
readonly #l20: GemmaMoeLayer;
|
|
147
|
+
readonly #t20: GemmaMoeLayer;
|
|
148
|
+
readonly #l21: GemmaMoeLayer;
|
|
149
|
+
readonly #t21: GemmaMoeLayer;
|
|
150
|
+
readonly #l22: GemmaMoeLayer;
|
|
151
|
+
readonly #t22: GemmaMoeLayer;
|
|
152
|
+
readonly #l23: GemmaMoeLayer;
|
|
153
|
+
readonly #t23: GemmaMoeLayer;
|
|
154
|
+
readonly #l24: GemmaMoeLayer;
|
|
155
|
+
readonly #t24: GemmaMoeLayer;
|
|
156
|
+
readonly #l25: GemmaMoeLayer;
|
|
157
|
+
readonly #t25: GemmaMoeLayer;
|
|
158
|
+
readonly #l26: GemmaMoeLayer;
|
|
159
|
+
readonly #t26: GemmaMoeLayer;
|
|
160
|
+
readonly #l27: GemmaMoeLayer;
|
|
161
|
+
readonly #t27: GemmaMoeLayer;
|
|
162
|
+
readonly #l28: GemmaMoeLayer;
|
|
163
|
+
readonly #t28: GemmaMoeLayer;
|
|
164
|
+
readonly #l29: GemmaMoeLayer;
|
|
165
|
+
readonly #t29: GemmaMoeLayer;
|
|
166
|
+
constructor(weights: Weights, readonly config: ModelConfig) {
|
|
167
|
+
using resources = new GemmaBuildScope();
|
|
168
|
+
this.weightsBytes = weightBytes(weights);
|
|
169
|
+
this.embed = QuantizedEmbedding.load(weights, `${PREFIX}.embed_tokens`, config);
|
|
170
|
+
this.finalNorm = new RMSNorm(weights.tensor(`${PREFIX}.norm.weight`), config.text.rmsNormEps);
|
|
171
|
+
this.embedScale = Math.sqrt(config.text.hiddenSize);
|
|
172
|
+
this.windowSize = config.text.slidingWindow;
|
|
173
|
+
const p0 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.0`, new SlidingAttention(weights, config, `${PREFIX}.layers.0.self_attn`)));
|
|
174
|
+
this.#l0 = new GemmaMoeLayer(p0, new CompiledGegluMLP(p0.mlp), new FixedExperts(new UnsortedSwitchGLU(p0.experts, new CompiledGeglu())));
|
|
175
|
+
this.#t0 = new GemmaMoeLayer(p0, new CompiledGegluMLP(p0.mlp), new FixedExperts(new SortedSwitchGLU(p0.experts, new CompiledGeglu())));
|
|
176
|
+
const p1 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.1`, new SlidingAttention(weights, config, `${PREFIX}.layers.1.self_attn`)));
|
|
177
|
+
this.#l1 = new GemmaMoeLayer(p1, new CompiledGegluMLP(p1.mlp), new FixedExperts(new UnsortedSwitchGLU(p1.experts, new CompiledGeglu())));
|
|
178
|
+
this.#t1 = new GemmaMoeLayer(p1, new CompiledGegluMLP(p1.mlp), new FixedExperts(new SortedSwitchGLU(p1.experts, new CompiledGeglu())));
|
|
179
|
+
const p2 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.2`, new SlidingAttention(weights, config, `${PREFIX}.layers.2.self_attn`)));
|
|
180
|
+
this.#l2 = new GemmaMoeLayer(p2, new CompiledGegluMLP(p2.mlp), new FixedExperts(new UnsortedSwitchGLU(p2.experts, new CompiledGeglu())));
|
|
181
|
+
this.#t2 = new GemmaMoeLayer(p2, new CompiledGegluMLP(p2.mlp), new FixedExperts(new SortedSwitchGLU(p2.experts, new CompiledGeglu())));
|
|
182
|
+
const p3 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.3`, new SlidingAttention(weights, config, `${PREFIX}.layers.3.self_attn`)));
|
|
183
|
+
this.#l3 = new GemmaMoeLayer(p3, new CompiledGegluMLP(p3.mlp), new FixedExperts(new UnsortedSwitchGLU(p3.experts, new CompiledGeglu())));
|
|
184
|
+
this.#t3 = new GemmaMoeLayer(p3, new CompiledGegluMLP(p3.mlp), new FixedExperts(new SortedSwitchGLU(p3.experts, new CompiledGeglu())));
|
|
185
|
+
const p4 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.4`, new SlidingAttention(weights, config, `${PREFIX}.layers.4.self_attn`)));
|
|
186
|
+
this.#l4 = new GemmaMoeLayer(p4, new CompiledGegluMLP(p4.mlp), new FixedExperts(new UnsortedSwitchGLU(p4.experts, new CompiledGeglu())));
|
|
187
|
+
this.#t4 = new GemmaMoeLayer(p4, new CompiledGegluMLP(p4.mlp), new FixedExperts(new SortedSwitchGLU(p4.experts, new CompiledGeglu())));
|
|
188
|
+
const p5 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.5`, new FullKEqVAttention(weights, config, `${PREFIX}.layers.5.self_attn`)));
|
|
189
|
+
this.#l5 = new GemmaMoeLayer(p5, new CompiledGegluMLP(p5.mlp), new FixedExperts(new UnsortedSwitchGLU(p5.experts, new CompiledGeglu())));
|
|
190
|
+
this.#t5 = new GemmaMoeLayer(p5, new CompiledGegluMLP(p5.mlp), new FixedExperts(new SortedSwitchGLU(p5.experts, new CompiledGeglu())));
|
|
191
|
+
const p6 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.6`, new SlidingAttention(weights, config, `${PREFIX}.layers.6.self_attn`)));
|
|
192
|
+
this.#l6 = new GemmaMoeLayer(p6, new CompiledGegluMLP(p6.mlp), new FixedExperts(new UnsortedSwitchGLU(p6.experts, new CompiledGeglu())));
|
|
193
|
+
this.#t6 = new GemmaMoeLayer(p6, new CompiledGegluMLP(p6.mlp), new FixedExperts(new SortedSwitchGLU(p6.experts, new CompiledGeglu())));
|
|
194
|
+
const p7 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.7`, new SlidingAttention(weights, config, `${PREFIX}.layers.7.self_attn`)));
|
|
195
|
+
this.#l7 = new GemmaMoeLayer(p7, new CompiledGegluMLP(p7.mlp), new FixedExperts(new UnsortedSwitchGLU(p7.experts, new CompiledGeglu())));
|
|
196
|
+
this.#t7 = new GemmaMoeLayer(p7, new CompiledGegluMLP(p7.mlp), new FixedExperts(new SortedSwitchGLU(p7.experts, new CompiledGeglu())));
|
|
197
|
+
const p8 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.8`, new SlidingAttention(weights, config, `${PREFIX}.layers.8.self_attn`)));
|
|
198
|
+
this.#l8 = new GemmaMoeLayer(p8, new CompiledGegluMLP(p8.mlp), new FixedExperts(new UnsortedSwitchGLU(p8.experts, new CompiledGeglu())));
|
|
199
|
+
this.#t8 = new GemmaMoeLayer(p8, new CompiledGegluMLP(p8.mlp), new FixedExperts(new SortedSwitchGLU(p8.experts, new CompiledGeglu())));
|
|
200
|
+
const p9 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.9`, new SlidingAttention(weights, config, `${PREFIX}.layers.9.self_attn`)));
|
|
201
|
+
this.#l9 = new GemmaMoeLayer(p9, new CompiledGegluMLP(p9.mlp), new FixedExperts(new UnsortedSwitchGLU(p9.experts, new CompiledGeglu())));
|
|
202
|
+
this.#t9 = new GemmaMoeLayer(p9, new CompiledGegluMLP(p9.mlp), new FixedExperts(new SortedSwitchGLU(p9.experts, new CompiledGeglu())));
|
|
203
|
+
const p10 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.10`, new SlidingAttention(weights, config, `${PREFIX}.layers.10.self_attn`)));
|
|
204
|
+
this.#l10 = new GemmaMoeLayer(p10, new CompiledGegluMLP(p10.mlp), new FixedExperts(new UnsortedSwitchGLU(p10.experts, new CompiledGeglu())));
|
|
205
|
+
this.#t10 = new GemmaMoeLayer(p10, new CompiledGegluMLP(p10.mlp), new FixedExperts(new SortedSwitchGLU(p10.experts, new CompiledGeglu())));
|
|
206
|
+
const p11 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.11`, new FullKEqVAttention(weights, config, `${PREFIX}.layers.11.self_attn`)));
|
|
207
|
+
this.#l11 = new GemmaMoeLayer(p11, new CompiledGegluMLP(p11.mlp), new FixedExperts(new UnsortedSwitchGLU(p11.experts, new CompiledGeglu())));
|
|
208
|
+
this.#t11 = new GemmaMoeLayer(p11, new CompiledGegluMLP(p11.mlp), new FixedExperts(new SortedSwitchGLU(p11.experts, new CompiledGeglu())));
|
|
209
|
+
const p12 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.12`, new SlidingAttention(weights, config, `${PREFIX}.layers.12.self_attn`)));
|
|
210
|
+
this.#l12 = new GemmaMoeLayer(p12, new CompiledGegluMLP(p12.mlp), new FixedExperts(new UnsortedSwitchGLU(p12.experts, new CompiledGeglu())));
|
|
211
|
+
this.#t12 = new GemmaMoeLayer(p12, new CompiledGegluMLP(p12.mlp), new FixedExperts(new SortedSwitchGLU(p12.experts, new CompiledGeglu())));
|
|
212
|
+
const p13 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.13`, new SlidingAttention(weights, config, `${PREFIX}.layers.13.self_attn`)));
|
|
213
|
+
this.#l13 = new GemmaMoeLayer(p13, new CompiledGegluMLP(p13.mlp), new FixedExperts(new UnsortedSwitchGLU(p13.experts, new CompiledGeglu())));
|
|
214
|
+
this.#t13 = new GemmaMoeLayer(p13, new CompiledGegluMLP(p13.mlp), new FixedExperts(new SortedSwitchGLU(p13.experts, new CompiledGeglu())));
|
|
215
|
+
const p14 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.14`, new SlidingAttention(weights, config, `${PREFIX}.layers.14.self_attn`)));
|
|
216
|
+
this.#l14 = new GemmaMoeLayer(p14, new CompiledGegluMLP(p14.mlp), new FixedExperts(new UnsortedSwitchGLU(p14.experts, new CompiledGeglu())));
|
|
217
|
+
this.#t14 = new GemmaMoeLayer(p14, new CompiledGegluMLP(p14.mlp), new FixedExperts(new SortedSwitchGLU(p14.experts, new CompiledGeglu())));
|
|
218
|
+
const p15 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.15`, new SlidingAttention(weights, config, `${PREFIX}.layers.15.self_attn`)));
|
|
219
|
+
this.#l15 = new GemmaMoeLayer(p15, new CompiledGegluMLP(p15.mlp), new FixedExperts(new UnsortedSwitchGLU(p15.experts, new CompiledGeglu())));
|
|
220
|
+
this.#t15 = new GemmaMoeLayer(p15, new CompiledGegluMLP(p15.mlp), new FixedExperts(new SortedSwitchGLU(p15.experts, new CompiledGeglu())));
|
|
221
|
+
const p16 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.16`, new SlidingAttention(weights, config, `${PREFIX}.layers.16.self_attn`)));
|
|
222
|
+
this.#l16 = new GemmaMoeLayer(p16, new CompiledGegluMLP(p16.mlp), new FixedExperts(new UnsortedSwitchGLU(p16.experts, new CompiledGeglu())));
|
|
223
|
+
this.#t16 = new GemmaMoeLayer(p16, new CompiledGegluMLP(p16.mlp), new FixedExperts(new SortedSwitchGLU(p16.experts, new CompiledGeglu())));
|
|
224
|
+
const p17 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.17`, new FullKEqVAttention(weights, config, `${PREFIX}.layers.17.self_attn`)));
|
|
225
|
+
this.#l17 = new GemmaMoeLayer(p17, new CompiledGegluMLP(p17.mlp), new FixedExperts(new UnsortedSwitchGLU(p17.experts, new CompiledGeglu())));
|
|
226
|
+
this.#t17 = new GemmaMoeLayer(p17, new CompiledGegluMLP(p17.mlp), new FixedExperts(new SortedSwitchGLU(p17.experts, new CompiledGeglu())));
|
|
227
|
+
const p18 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.18`, new SlidingAttention(weights, config, `${PREFIX}.layers.18.self_attn`)));
|
|
228
|
+
this.#l18 = new GemmaMoeLayer(p18, new CompiledGegluMLP(p18.mlp), new FixedExperts(new UnsortedSwitchGLU(p18.experts, new CompiledGeglu())));
|
|
229
|
+
this.#t18 = new GemmaMoeLayer(p18, new CompiledGegluMLP(p18.mlp), new FixedExperts(new SortedSwitchGLU(p18.experts, new CompiledGeglu())));
|
|
230
|
+
const p19 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.19`, new SlidingAttention(weights, config, `${PREFIX}.layers.19.self_attn`)));
|
|
231
|
+
this.#l19 = new GemmaMoeLayer(p19, new CompiledGegluMLP(p19.mlp), new FixedExperts(new UnsortedSwitchGLU(p19.experts, new CompiledGeglu())));
|
|
232
|
+
this.#t19 = new GemmaMoeLayer(p19, new CompiledGegluMLP(p19.mlp), new FixedExperts(new SortedSwitchGLU(p19.experts, new CompiledGeglu())));
|
|
233
|
+
const p20 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.20`, new SlidingAttention(weights, config, `${PREFIX}.layers.20.self_attn`)));
|
|
234
|
+
this.#l20 = new GemmaMoeLayer(p20, new CompiledGegluMLP(p20.mlp), new FixedExperts(new UnsortedSwitchGLU(p20.experts, new CompiledGeglu())));
|
|
235
|
+
this.#t20 = new GemmaMoeLayer(p20, new CompiledGegluMLP(p20.mlp), new FixedExperts(new SortedSwitchGLU(p20.experts, new CompiledGeglu())));
|
|
236
|
+
const p21 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.21`, new SlidingAttention(weights, config, `${PREFIX}.layers.21.self_attn`)));
|
|
237
|
+
this.#l21 = new GemmaMoeLayer(p21, new CompiledGegluMLP(p21.mlp), new FixedExperts(new UnsortedSwitchGLU(p21.experts, new CompiledGeglu())));
|
|
238
|
+
this.#t21 = new GemmaMoeLayer(p21, new CompiledGegluMLP(p21.mlp), new FixedExperts(new SortedSwitchGLU(p21.experts, new CompiledGeglu())));
|
|
239
|
+
const p22 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.22`, new SlidingAttention(weights, config, `${PREFIX}.layers.22.self_attn`)));
|
|
240
|
+
this.#l22 = new GemmaMoeLayer(p22, new CompiledGegluMLP(p22.mlp), new FixedExperts(new UnsortedSwitchGLU(p22.experts, new CompiledGeglu())));
|
|
241
|
+
this.#t22 = new GemmaMoeLayer(p22, new CompiledGegluMLP(p22.mlp), new FixedExperts(new SortedSwitchGLU(p22.experts, new CompiledGeglu())));
|
|
242
|
+
const p23 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.23`, new FullKEqVAttention(weights, config, `${PREFIX}.layers.23.self_attn`)));
|
|
243
|
+
this.#l23 = new GemmaMoeLayer(p23, new CompiledGegluMLP(p23.mlp), new FixedExperts(new UnsortedSwitchGLU(p23.experts, new CompiledGeglu())));
|
|
244
|
+
this.#t23 = new GemmaMoeLayer(p23, new CompiledGegluMLP(p23.mlp), new FixedExperts(new SortedSwitchGLU(p23.experts, new CompiledGeglu())));
|
|
245
|
+
const p24 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.24`, new SlidingAttention(weights, config, `${PREFIX}.layers.24.self_attn`)));
|
|
246
|
+
this.#l24 = new GemmaMoeLayer(p24, new CompiledGegluMLP(p24.mlp), new FixedExperts(new UnsortedSwitchGLU(p24.experts, new CompiledGeglu())));
|
|
247
|
+
this.#t24 = new GemmaMoeLayer(p24, new CompiledGegluMLP(p24.mlp), new FixedExperts(new SortedSwitchGLU(p24.experts, new CompiledGeglu())));
|
|
248
|
+
const p25 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.25`, new SlidingAttention(weights, config, `${PREFIX}.layers.25.self_attn`)));
|
|
249
|
+
this.#l25 = new GemmaMoeLayer(p25, new CompiledGegluMLP(p25.mlp), new FixedExperts(new UnsortedSwitchGLU(p25.experts, new CompiledGeglu())));
|
|
250
|
+
this.#t25 = new GemmaMoeLayer(p25, new CompiledGegluMLP(p25.mlp), new FixedExperts(new SortedSwitchGLU(p25.experts, new CompiledGeglu())));
|
|
251
|
+
const p26 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.26`, new SlidingAttention(weights, config, `${PREFIX}.layers.26.self_attn`)));
|
|
252
|
+
this.#l26 = new GemmaMoeLayer(p26, new CompiledGegluMLP(p26.mlp), new FixedExperts(new UnsortedSwitchGLU(p26.experts, new CompiledGeglu())));
|
|
253
|
+
this.#t26 = new GemmaMoeLayer(p26, new CompiledGegluMLP(p26.mlp), new FixedExperts(new SortedSwitchGLU(p26.experts, new CompiledGeglu())));
|
|
254
|
+
const p27 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.27`, new SlidingAttention(weights, config, `${PREFIX}.layers.27.self_attn`)));
|
|
255
|
+
this.#l27 = new GemmaMoeLayer(p27, new CompiledGegluMLP(p27.mlp), new FixedExperts(new UnsortedSwitchGLU(p27.experts, new CompiledGeglu())));
|
|
256
|
+
this.#t27 = new GemmaMoeLayer(p27, new CompiledGegluMLP(p27.mlp), new FixedExperts(new SortedSwitchGLU(p27.experts, new CompiledGeglu())));
|
|
257
|
+
const p28 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.28`, new SlidingAttention(weights, config, `${PREFIX}.layers.28.self_attn`)));
|
|
258
|
+
this.#l28 = new GemmaMoeLayer(p28, new CompiledGegluMLP(p28.mlp), new FixedExperts(new UnsortedSwitchGLU(p28.experts, new CompiledGeglu())));
|
|
259
|
+
this.#t28 = new GemmaMoeLayer(p28, new CompiledGegluMLP(p28.mlp), new FixedExperts(new SortedSwitchGLU(p28.experts, new CompiledGeglu())));
|
|
260
|
+
const p29 = resources.holdMoe(new GemmaMoeLayerWeights(weights, config, `${PREFIX}.layers.29`, new FullKEqVAttention(weights, config, `${PREFIX}.layers.29.self_attn`)));
|
|
261
|
+
this.#l29 = new GemmaMoeLayer(p29, new CompiledGegluMLP(p29.mlp), new FixedExperts(new UnsortedSwitchGLU(p29.experts, new CompiledGeglu())));
|
|
262
|
+
this.#t29 = new GemmaMoeLayer(p29, new CompiledGegluMLP(p29.mlp), new FixedExperts(new SortedSwitchGLU(p29.experts, new CompiledGeglu())));
|
|
263
|
+
this.layers = Object.freeze([this.#l0, this.#l1, this.#l2, this.#l3, this.#l4, this.#l5, this.#l6, this.#l7, this.#l8, this.#l9, this.#l10, this.#l11, this.#l12, this.#l13, this.#l14, this.#l15, this.#l16, this.#l17, this.#l18, this.#l19, this.#l20, this.#l21, this.#l22, this.#l23, this.#l24, this.#l25, this.#l26, this.#l27, this.#l28, this.#l29]);
|
|
264
|
+
this.traceLayers = Object.freeze([]);
|
|
265
|
+
this.#parts = Object.freeze([p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29]);
|
|
266
|
+
this.#constants = resources.finish();
|
|
267
|
+
this.cacheLayers = Object.freeze([
|
|
268
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
269
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
270
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
271
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
272
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
273
|
+
{ kind: "full", turboQuant: "deferred" },
|
|
274
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
275
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
276
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
277
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
278
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
279
|
+
{ kind: "full", turboQuant: "deferred" },
|
|
280
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
281
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
282
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
283
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
284
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
285
|
+
{ kind: "full", turboQuant: "deferred" },
|
|
286
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
287
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
288
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
289
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
290
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
291
|
+
{ kind: "full", turboQuant: "deferred" },
|
|
292
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
293
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
294
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
295
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
296
|
+
{ kind: "sliding", window: config.text.slidingWindow },
|
|
297
|
+
{ kind: "full", turboQuant: "deferred" },
|
|
298
|
+
]);
|
|
299
|
+
this.prompt = Object.freeze([{ from: 1, to: 7, operation: this.#window }, { from: 8, to: Infinity, operation: this.#sortedWindow }]);
|
|
300
|
+
this.promptChunk = this.prompt;
|
|
301
|
+
this.multiSequencePrompt = this.prompt;
|
|
302
|
+
this.multiSequencePromptChunk = this.multiSequencePrompt;
|
|
303
|
+
this.decode = Object.freeze({ 1: this.#decode, 2: this.#decode, 3: this.#decode, 4: this.#decode, 5: this.#decode, 6: this.#decode, 7: this.#decode, 8: this.#sortedDecode ,
|
|
304
|
+
default: this.#sortedDecode,
|
|
305
|
+
});
|
|
306
|
+
this.verify = Object.freeze({ 1: this.#window, 2: this.#window, 3: this.#window, 4: this.#window, 5: this.#window, 6: this.#window, 7: this.#window, 8: this.#sortedWindow, 9: this.#sortedWindow, 10: this.#sortedWindow, 11: this.#sortedWindow, 12: this.#sortedWindow, 13: this.#sortedWindow, 14: this.#sortedWindow, 15: this.#sortedWindow, 16: this.#sortedWindow, 17: this.#sortedWindow, 18: this.#sortedWindow, 19: this.#sortedWindow, 20: this.#sortedWindow, 21: this.#sortedWindow, 22: this.#sortedWindow, 23: this.#sortedWindow, 24: this.#sortedWindow, 25: this.#sortedWindow, 26: this.#sortedWindow, 27: this.#sortedWindow, 28: this.#sortedWindow, 29: this.#sortedWindow, 30: this.#sortedWindow, 31: this.#sortedWindow, 32: this.#sortedWindow, 33: this.#sortedWindow, 34: this.#sortedWindow, 35: this.#sortedWindow, 36: this.#sortedWindow, 37: this.#sortedWindow, 38: this.#sortedWindow, 39: this.#sortedWindow, 40: this.#sortedWindow, 41: this.#sortedWindow, 42: this.#sortedWindow, 43: this.#sortedWindow, 44: this.#sortedWindow, 45: this.#sortedWindow, 46: this.#sortedWindow, 47: this.#sortedWindow, 48: this.#sortedWindow, 49: this.#sortedWindow, 50: this.#sortedWindow, 51: this.#sortedWindow, 52: this.#sortedWindow, 53: this.#sortedWindow, 54: this.#sortedWindow, 55: this.#sortedWindow, 56: this.#sortedWindow, 57: this.#sortedWindow, 58: this.#sortedWindow, 59: this.#sortedWindow, 60: this.#sortedWindow, 61: this.#sortedWindow, 62: this.#sortedWindow, 63: this.#sortedWindow, 64: this.#sortedWindow ,
|
|
307
|
+
default: this.#sortedWindow,
|
|
308
|
+
});
|
|
309
|
+
this.appendKnown = [{ from: 1, to: 7, operation: this.#known }, { from: 8, to: Infinity, operation: this.#sortedKnown }];
|
|
310
|
+
this.logits = Object.freeze({ 1: this.#head, 2: this.#head, 3: this.#head, 4: this.#head, 5: this.#head, 6: this.#head, 7: this.#head, 8: this.#head, 9: this.#head, 10: this.#head, 11: this.#head, 12: this.#head, 13: this.#head, 14: this.#head, 15: this.#head, 16: this.#head, 17: this.#head, 18: this.#head, 19: this.#head, 20: this.#head, 21: this.#head, 22: this.#head, 23: this.#head, 24: this.#head, 25: this.#head, 26: this.#head, 27: this.#head, 28: this.#head, 29: this.#head, 30: this.#head, 31: this.#head, 32: this.#head, 33: this.#head, 34: this.#head, 35: this.#head, 36: this.#head, 37: this.#head, 38: this.#head, 39: this.#head, 40: this.#head, 41: this.#head, 42: this.#head, 43: this.#head, 44: this.#head, 45: this.#head, 46: this.#head, 47: this.#head, 48: this.#head, 49: this.#head, 50: this.#head, 51: this.#head, 52: this.#head, 53: this.#head, 54: this.#head, 55: this.#head, 56: this.#head, 57: this.#head, 58: this.#head, 59: this.#head, 60: this.#head, 61: this.#head, 62: this.#head, 63: this.#head, 64: this.#head ,
|
|
311
|
+
default: this.#head,
|
|
312
|
+
});
|
|
313
|
+
this.mixed = Object.freeze([{ from: 1, to: 7, operation: this.#mixed }, { from: 8, to: Infinity, operation: this.#sortedMixed }]);
|
|
314
|
+
}
|
|
315
|
+
readonly #window = (ids: MlxArray, caches: Cache[], capture: MlxLayerCapture | null): MlxArray => this.#windowHidden(this.#embedTokens(ids), caches, "window", ids, capture);
|
|
316
|
+
#windowHidden(hidden: MlxArray, caches: Cache[], phase: Gemma4Phase, ids: MlxArray, capture: MlxLayerCapture | null): MlxArray {
|
|
317
|
+
using scope = new GemmaForwardScope(hidden, capture, CACHES);
|
|
318
|
+
scope.own(0, this.#l0.forward(scope.hidden, caches[0]!, phase, null));
|
|
319
|
+
scope.own(1, this.#l1.forward(scope.hidden, caches[1]!, phase, null));
|
|
320
|
+
scope.own(2, this.#l2.forward(scope.hidden, caches[2]!, phase, null));
|
|
321
|
+
scope.own(3, this.#l3.forward(scope.hidden, caches[3]!, phase, null));
|
|
322
|
+
scope.own(4, this.#l4.forward(scope.hidden, caches[4]!, phase, null));
|
|
323
|
+
scope.own(5, this.#l5.forward(scope.hidden, caches[5]!, phase, null));
|
|
324
|
+
scope.own(6, this.#l6.forward(scope.hidden, caches[6]!, phase, null));
|
|
325
|
+
scope.own(7, this.#l7.forward(scope.hidden, caches[7]!, phase, null));
|
|
326
|
+
scope.own(8, this.#l8.forward(scope.hidden, caches[8]!, phase, null));
|
|
327
|
+
scope.own(9, this.#l9.forward(scope.hidden, caches[9]!, phase, null));
|
|
328
|
+
scope.own(10, this.#l10.forward(scope.hidden, caches[10]!, phase, null));
|
|
329
|
+
scope.own(11, this.#l11.forward(scope.hidden, caches[11]!, phase, null));
|
|
330
|
+
scope.own(12, this.#l12.forward(scope.hidden, caches[12]!, phase, null));
|
|
331
|
+
scope.own(13, this.#l13.forward(scope.hidden, caches[13]!, phase, null));
|
|
332
|
+
scope.own(14, this.#l14.forward(scope.hidden, caches[14]!, phase, null));
|
|
333
|
+
scope.own(15, this.#l15.forward(scope.hidden, caches[15]!, phase, null));
|
|
334
|
+
scope.own(16, this.#l16.forward(scope.hidden, caches[16]!, phase, null));
|
|
335
|
+
scope.own(17, this.#l17.forward(scope.hidden, caches[17]!, phase, null));
|
|
336
|
+
scope.own(18, this.#l18.forward(scope.hidden, caches[18]!, phase, null));
|
|
337
|
+
scope.own(19, this.#l19.forward(scope.hidden, caches[19]!, phase, null));
|
|
338
|
+
scope.own(20, this.#l20.forward(scope.hidden, caches[20]!, phase, null));
|
|
339
|
+
scope.own(21, this.#l21.forward(scope.hidden, caches[21]!, phase, null));
|
|
340
|
+
scope.own(22, this.#l22.forward(scope.hidden, caches[22]!, phase, null));
|
|
341
|
+
scope.own(23, this.#l23.forward(scope.hidden, caches[23]!, phase, null));
|
|
342
|
+
scope.own(24, this.#l24.forward(scope.hidden, caches[24]!, phase, null));
|
|
343
|
+
scope.own(25, this.#l25.forward(scope.hidden, caches[25]!, phase, null));
|
|
344
|
+
scope.own(26, this.#l26.forward(scope.hidden, caches[26]!, phase, null));
|
|
345
|
+
scope.own(27, this.#l27.forward(scope.hidden, caches[27]!, phase, null));
|
|
346
|
+
scope.own(28, this.#l28.forward(scope.hidden, caches[28]!, phase, null));
|
|
347
|
+
scope.own(29, this.#l29.forward(scope.hidden, caches[29]!, phase, null));
|
|
348
|
+
return scope.finish(30, this.finalNorm.forward(scope.hidden));
|
|
349
|
+
}
|
|
350
|
+
readonly #decode = (ids: MlxArray, caches: Cache[], capture: MlxLayerCapture | null): MlxArray => this.#decodeHidden(this.#embedTokens(ids), caches, "decode", ids, capture);
|
|
351
|
+
#decodeHidden(hidden: MlxArray, caches: Cache[], phase: Gemma4Phase, ids: MlxArray, capture: MlxLayerCapture | null): MlxArray {
|
|
352
|
+
using scope = new GemmaForwardScope(hidden, capture, CACHES);
|
|
353
|
+
scope.own(0, this.#l0.forward(scope.hidden, caches[0]!, phase, null));
|
|
354
|
+
scope.own(1, this.#l1.forward(scope.hidden, caches[1]!, phase, null));
|
|
355
|
+
scope.own(2, this.#l2.forward(scope.hidden, caches[2]!, phase, null));
|
|
356
|
+
scope.own(3, this.#l3.forward(scope.hidden, caches[3]!, phase, null));
|
|
357
|
+
scope.own(4, this.#l4.forward(scope.hidden, caches[4]!, phase, null));
|
|
358
|
+
scope.own(5, this.#l5.forward(scope.hidden, caches[5]!, phase, null));
|
|
359
|
+
scope.own(6, this.#l6.forward(scope.hidden, caches[6]!, phase, null));
|
|
360
|
+
scope.own(7, this.#l7.forward(scope.hidden, caches[7]!, phase, null));
|
|
361
|
+
scope.own(8, this.#l8.forward(scope.hidden, caches[8]!, phase, null));
|
|
362
|
+
scope.own(9, this.#l9.forward(scope.hidden, caches[9]!, phase, null));
|
|
363
|
+
scope.own(10, this.#l10.forward(scope.hidden, caches[10]!, phase, null));
|
|
364
|
+
scope.own(11, this.#l11.forward(scope.hidden, caches[11]!, phase, null));
|
|
365
|
+
scope.own(12, this.#l12.forward(scope.hidden, caches[12]!, phase, null));
|
|
366
|
+
scope.own(13, this.#l13.forward(scope.hidden, caches[13]!, phase, null));
|
|
367
|
+
scope.own(14, this.#l14.forward(scope.hidden, caches[14]!, phase, null));
|
|
368
|
+
scope.own(15, this.#l15.forward(scope.hidden, caches[15]!, phase, null));
|
|
369
|
+
scope.own(16, this.#l16.forward(scope.hidden, caches[16]!, phase, null));
|
|
370
|
+
scope.own(17, this.#l17.forward(scope.hidden, caches[17]!, phase, null));
|
|
371
|
+
scope.own(18, this.#l18.forward(scope.hidden, caches[18]!, phase, null));
|
|
372
|
+
scope.own(19, this.#l19.forward(scope.hidden, caches[19]!, phase, null));
|
|
373
|
+
scope.own(20, this.#l20.forward(scope.hidden, caches[20]!, phase, null));
|
|
374
|
+
scope.own(21, this.#l21.forward(scope.hidden, caches[21]!, phase, null));
|
|
375
|
+
scope.own(22, this.#l22.forward(scope.hidden, caches[22]!, phase, null));
|
|
376
|
+
scope.own(23, this.#l23.forward(scope.hidden, caches[23]!, phase, null));
|
|
377
|
+
scope.own(24, this.#l24.forward(scope.hidden, caches[24]!, phase, null));
|
|
378
|
+
scope.own(25, this.#l25.forward(scope.hidden, caches[25]!, phase, null));
|
|
379
|
+
scope.own(26, this.#l26.forward(scope.hidden, caches[26]!, phase, null));
|
|
380
|
+
scope.own(27, this.#l27.forward(scope.hidden, caches[27]!, phase, null));
|
|
381
|
+
scope.own(28, this.#l28.forward(scope.hidden, caches[28]!, phase, null));
|
|
382
|
+
scope.own(29, this.#l29.forward(scope.hidden, caches[29]!, phase, null));
|
|
383
|
+
return scope.finish(30, this.finalNorm.forward(scope.hidden));
|
|
384
|
+
}
|
|
385
|
+
readonly #sortedWindow = (ids: MlxArray, caches: Cache[], capture: MlxLayerCapture | null): MlxArray => this.#sortedWindowHidden(this.#embedTokens(ids), caches, "window", ids, capture);
|
|
386
|
+
#sortedWindowHidden(hidden: MlxArray, caches: Cache[], phase: Gemma4Phase, ids: MlxArray, capture: MlxLayerCapture | null): MlxArray {
|
|
387
|
+
using scope = new GemmaForwardScope(hidden, capture, CACHES);
|
|
388
|
+
scope.own(0, this.#t0.forward(scope.hidden, caches[0]!, phase, null));
|
|
389
|
+
scope.own(1, this.#t1.forward(scope.hidden, caches[1]!, phase, null));
|
|
390
|
+
scope.own(2, this.#t2.forward(scope.hidden, caches[2]!, phase, null));
|
|
391
|
+
scope.own(3, this.#t3.forward(scope.hidden, caches[3]!, phase, null));
|
|
392
|
+
scope.own(4, this.#t4.forward(scope.hidden, caches[4]!, phase, null));
|
|
393
|
+
scope.own(5, this.#t5.forward(scope.hidden, caches[5]!, phase, null));
|
|
394
|
+
scope.own(6, this.#t6.forward(scope.hidden, caches[6]!, phase, null));
|
|
395
|
+
scope.own(7, this.#t7.forward(scope.hidden, caches[7]!, phase, null));
|
|
396
|
+
scope.own(8, this.#t8.forward(scope.hidden, caches[8]!, phase, null));
|
|
397
|
+
scope.own(9, this.#t9.forward(scope.hidden, caches[9]!, phase, null));
|
|
398
|
+
scope.own(10, this.#t10.forward(scope.hidden, caches[10]!, phase, null));
|
|
399
|
+
scope.own(11, this.#t11.forward(scope.hidden, caches[11]!, phase, null));
|
|
400
|
+
scope.own(12, this.#t12.forward(scope.hidden, caches[12]!, phase, null));
|
|
401
|
+
scope.own(13, this.#t13.forward(scope.hidden, caches[13]!, phase, null));
|
|
402
|
+
scope.own(14, this.#t14.forward(scope.hidden, caches[14]!, phase, null));
|
|
403
|
+
scope.own(15, this.#t15.forward(scope.hidden, caches[15]!, phase, null));
|
|
404
|
+
scope.own(16, this.#t16.forward(scope.hidden, caches[16]!, phase, null));
|
|
405
|
+
scope.own(17, this.#t17.forward(scope.hidden, caches[17]!, phase, null));
|
|
406
|
+
scope.own(18, this.#t18.forward(scope.hidden, caches[18]!, phase, null));
|
|
407
|
+
scope.own(19, this.#t19.forward(scope.hidden, caches[19]!, phase, null));
|
|
408
|
+
scope.own(20, this.#t20.forward(scope.hidden, caches[20]!, phase, null));
|
|
409
|
+
scope.own(21, this.#t21.forward(scope.hidden, caches[21]!, phase, null));
|
|
410
|
+
scope.own(22, this.#t22.forward(scope.hidden, caches[22]!, phase, null));
|
|
411
|
+
scope.own(23, this.#t23.forward(scope.hidden, caches[23]!, phase, null));
|
|
412
|
+
scope.own(24, this.#t24.forward(scope.hidden, caches[24]!, phase, null));
|
|
413
|
+
scope.own(25, this.#t25.forward(scope.hidden, caches[25]!, phase, null));
|
|
414
|
+
scope.own(26, this.#t26.forward(scope.hidden, caches[26]!, phase, null));
|
|
415
|
+
scope.own(27, this.#t27.forward(scope.hidden, caches[27]!, phase, null));
|
|
416
|
+
scope.own(28, this.#t28.forward(scope.hidden, caches[28]!, phase, null));
|
|
417
|
+
scope.own(29, this.#t29.forward(scope.hidden, caches[29]!, phase, null));
|
|
418
|
+
return scope.finish(30, this.finalNorm.forward(scope.hidden));
|
|
419
|
+
}
|
|
420
|
+
readonly #sortedDecode = (ids: MlxArray, caches: Cache[], capture: MlxLayerCapture | null): MlxArray => this.#sortedDecodeHidden(this.#embedTokens(ids), caches, "decode", ids, capture);
|
|
421
|
+
#sortedDecodeHidden(hidden: MlxArray, caches: Cache[], phase: Gemma4Phase, ids: MlxArray, capture: MlxLayerCapture | null): MlxArray {
|
|
422
|
+
using scope = new GemmaForwardScope(hidden, capture, CACHES);
|
|
423
|
+
scope.own(0, this.#t0.forward(scope.hidden, caches[0]!, phase, null));
|
|
424
|
+
scope.own(1, this.#t1.forward(scope.hidden, caches[1]!, phase, null));
|
|
425
|
+
scope.own(2, this.#t2.forward(scope.hidden, caches[2]!, phase, null));
|
|
426
|
+
scope.own(3, this.#t3.forward(scope.hidden, caches[3]!, phase, null));
|
|
427
|
+
scope.own(4, this.#t4.forward(scope.hidden, caches[4]!, phase, null));
|
|
428
|
+
scope.own(5, this.#t5.forward(scope.hidden, caches[5]!, phase, null));
|
|
429
|
+
scope.own(6, this.#t6.forward(scope.hidden, caches[6]!, phase, null));
|
|
430
|
+
scope.own(7, this.#t7.forward(scope.hidden, caches[7]!, phase, null));
|
|
431
|
+
scope.own(8, this.#t8.forward(scope.hidden, caches[8]!, phase, null));
|
|
432
|
+
scope.own(9, this.#t9.forward(scope.hidden, caches[9]!, phase, null));
|
|
433
|
+
scope.own(10, this.#t10.forward(scope.hidden, caches[10]!, phase, null));
|
|
434
|
+
scope.own(11, this.#t11.forward(scope.hidden, caches[11]!, phase, null));
|
|
435
|
+
scope.own(12, this.#t12.forward(scope.hidden, caches[12]!, phase, null));
|
|
436
|
+
scope.own(13, this.#t13.forward(scope.hidden, caches[13]!, phase, null));
|
|
437
|
+
scope.own(14, this.#t14.forward(scope.hidden, caches[14]!, phase, null));
|
|
438
|
+
scope.own(15, this.#t15.forward(scope.hidden, caches[15]!, phase, null));
|
|
439
|
+
scope.own(16, this.#t16.forward(scope.hidden, caches[16]!, phase, null));
|
|
440
|
+
scope.own(17, this.#t17.forward(scope.hidden, caches[17]!, phase, null));
|
|
441
|
+
scope.own(18, this.#t18.forward(scope.hidden, caches[18]!, phase, null));
|
|
442
|
+
scope.own(19, this.#t19.forward(scope.hidden, caches[19]!, phase, null));
|
|
443
|
+
scope.own(20, this.#t20.forward(scope.hidden, caches[20]!, phase, null));
|
|
444
|
+
scope.own(21, this.#t21.forward(scope.hidden, caches[21]!, phase, null));
|
|
445
|
+
scope.own(22, this.#t22.forward(scope.hidden, caches[22]!, phase, null));
|
|
446
|
+
scope.own(23, this.#t23.forward(scope.hidden, caches[23]!, phase, null));
|
|
447
|
+
scope.own(24, this.#t24.forward(scope.hidden, caches[24]!, phase, null));
|
|
448
|
+
scope.own(25, this.#t25.forward(scope.hidden, caches[25]!, phase, null));
|
|
449
|
+
scope.own(26, this.#t26.forward(scope.hidden, caches[26]!, phase, null));
|
|
450
|
+
scope.own(27, this.#t27.forward(scope.hidden, caches[27]!, phase, null));
|
|
451
|
+
scope.own(28, this.#t28.forward(scope.hidden, caches[28]!, phase, null));
|
|
452
|
+
scope.own(29, this.#t29.forward(scope.hidden, caches[29]!, phase, null));
|
|
453
|
+
return scope.finish(30, this.finalNorm.forward(scope.hidden));
|
|
454
|
+
}
|
|
455
|
+
readonly #known = (ids: MlxArray, caches: Cache[]) => this.#window(ids, caches, null);
|
|
456
|
+
readonly #sortedKnown = (ids: MlxArray, caches: Cache[]) => this.#sortedWindow(ids, caches, null);
|
|
457
|
+
readonly #head = (hidden: MlxArray) => { const logits = this.embed.asLinear(hidden); return disposing(logits, compiledLogitSoftcap(logits, FINAL_LOGIT_SOFTCAP)); };
|
|
458
|
+
readonly traceLogits = (hidden: MlxArray) => { const logits = this.embed.asLinear(hidden); return disposing(logits, logitSoftcap(logits, FINAL_LOGIT_SOFTCAP)); };
|
|
459
|
+
readonly #mixed = (work: Parameters<MlxMixedRange["operation"]>[0]): MlxArray[] => {
|
|
460
|
+
using scope = new GemmaMixedScope(work, ids => this.#embedTokens(ids), CACHES);
|
|
461
|
+
scope.layer(0, (x, group) => ownMixed(this.#l0.forwardAttn(x, group.cache[0]!, group.phase, null)), x => this.#l0.feedforward(x));
|
|
462
|
+
scope.layer(1, (x, group) => ownMixed(this.#l1.forwardAttn(x, group.cache[1]!, group.phase, null)), x => this.#l1.feedforward(x));
|
|
463
|
+
scope.layer(2, (x, group) => ownMixed(this.#l2.forwardAttn(x, group.cache[2]!, group.phase, null)), x => this.#l2.feedforward(x));
|
|
464
|
+
scope.layer(3, (x, group) => ownMixed(this.#l3.forwardAttn(x, group.cache[3]!, group.phase, null)), x => this.#l3.feedforward(x));
|
|
465
|
+
scope.layer(4, (x, group) => ownMixed(this.#l4.forwardAttn(x, group.cache[4]!, group.phase, null)), x => this.#l4.feedforward(x));
|
|
466
|
+
scope.layer(5, (x, group) => ownMixed(this.#l5.forwardAttn(x, group.cache[5]!, group.phase, null)), x => this.#l5.feedforward(x));
|
|
467
|
+
scope.layer(6, (x, group) => ownMixed(this.#l6.forwardAttn(x, group.cache[6]!, group.phase, null)), x => this.#l6.feedforward(x));
|
|
468
|
+
scope.layer(7, (x, group) => ownMixed(this.#l7.forwardAttn(x, group.cache[7]!, group.phase, null)), x => this.#l7.feedforward(x));
|
|
469
|
+
scope.layer(8, (x, group) => ownMixed(this.#l8.forwardAttn(x, group.cache[8]!, group.phase, null)), x => this.#l8.feedforward(x));
|
|
470
|
+
scope.layer(9, (x, group) => ownMixed(this.#l9.forwardAttn(x, group.cache[9]!, group.phase, null)), x => this.#l9.feedforward(x));
|
|
471
|
+
scope.layer(10, (x, group) => ownMixed(this.#l10.forwardAttn(x, group.cache[10]!, group.phase, null)), x => this.#l10.feedforward(x));
|
|
472
|
+
scope.layer(11, (x, group) => ownMixed(this.#l11.forwardAttn(x, group.cache[11]!, group.phase, null)), x => this.#l11.feedforward(x));
|
|
473
|
+
scope.layer(12, (x, group) => ownMixed(this.#l12.forwardAttn(x, group.cache[12]!, group.phase, null)), x => this.#l12.feedforward(x));
|
|
474
|
+
scope.layer(13, (x, group) => ownMixed(this.#l13.forwardAttn(x, group.cache[13]!, group.phase, null)), x => this.#l13.feedforward(x));
|
|
475
|
+
scope.layer(14, (x, group) => ownMixed(this.#l14.forwardAttn(x, group.cache[14]!, group.phase, null)), x => this.#l14.feedforward(x));
|
|
476
|
+
scope.layer(15, (x, group) => ownMixed(this.#l15.forwardAttn(x, group.cache[15]!, group.phase, null)), x => this.#l15.feedforward(x));
|
|
477
|
+
scope.layer(16, (x, group) => ownMixed(this.#l16.forwardAttn(x, group.cache[16]!, group.phase, null)), x => this.#l16.feedforward(x));
|
|
478
|
+
scope.layer(17, (x, group) => ownMixed(this.#l17.forwardAttn(x, group.cache[17]!, group.phase, null)), x => this.#l17.feedforward(x));
|
|
479
|
+
scope.layer(18, (x, group) => ownMixed(this.#l18.forwardAttn(x, group.cache[18]!, group.phase, null)), x => this.#l18.feedforward(x));
|
|
480
|
+
scope.layer(19, (x, group) => ownMixed(this.#l19.forwardAttn(x, group.cache[19]!, group.phase, null)), x => this.#l19.feedforward(x));
|
|
481
|
+
scope.layer(20, (x, group) => ownMixed(this.#l20.forwardAttn(x, group.cache[20]!, group.phase, null)), x => this.#l20.feedforward(x));
|
|
482
|
+
scope.layer(21, (x, group) => ownMixed(this.#l21.forwardAttn(x, group.cache[21]!, group.phase, null)), x => this.#l21.feedforward(x));
|
|
483
|
+
scope.layer(22, (x, group) => ownMixed(this.#l22.forwardAttn(x, group.cache[22]!, group.phase, null)), x => this.#l22.feedforward(x));
|
|
484
|
+
scope.layer(23, (x, group) => ownMixed(this.#l23.forwardAttn(x, group.cache[23]!, group.phase, null)), x => this.#l23.feedforward(x));
|
|
485
|
+
scope.layer(24, (x, group) => ownMixed(this.#l24.forwardAttn(x, group.cache[24]!, group.phase, null)), x => this.#l24.feedforward(x));
|
|
486
|
+
scope.layer(25, (x, group) => ownMixed(this.#l25.forwardAttn(x, group.cache[25]!, group.phase, null)), x => this.#l25.feedforward(x));
|
|
487
|
+
scope.layer(26, (x, group) => ownMixed(this.#l26.forwardAttn(x, group.cache[26]!, group.phase, null)), x => this.#l26.feedforward(x));
|
|
488
|
+
scope.layer(27, (x, group) => ownMixed(this.#l27.forwardAttn(x, group.cache[27]!, group.phase, null)), x => this.#l27.feedforward(x));
|
|
489
|
+
scope.layer(28, (x, group) => ownMixed(this.#l28.forwardAttn(x, group.cache[28]!, group.phase, null)), x => this.#l28.feedforward(x));
|
|
490
|
+
scope.layer(29, (x, group) => ownMixed(this.#l29.forwardAttn(x, group.cache[29]!, group.phase, null)), x => this.#l29.feedforward(x));
|
|
491
|
+
return scope.finish(30, this.finalNorm);
|
|
492
|
+
};
|
|
493
|
+
readonly #sortedMixed = (work: Parameters<MlxMixedRange["operation"]>[0]): MlxArray[] => {
|
|
494
|
+
using scope = new GemmaMixedScope(work, ids => this.#embedTokens(ids), CACHES);
|
|
495
|
+
scope.layer(0, (x, group) => ownMixed(this.#t0.forwardAttn(x, group.cache[0]!, group.phase, null)), x => this.#t0.feedforward(x));
|
|
496
|
+
scope.layer(1, (x, group) => ownMixed(this.#t1.forwardAttn(x, group.cache[1]!, group.phase, null)), x => this.#t1.feedforward(x));
|
|
497
|
+
scope.layer(2, (x, group) => ownMixed(this.#t2.forwardAttn(x, group.cache[2]!, group.phase, null)), x => this.#t2.feedforward(x));
|
|
498
|
+
scope.layer(3, (x, group) => ownMixed(this.#t3.forwardAttn(x, group.cache[3]!, group.phase, null)), x => this.#t3.feedforward(x));
|
|
499
|
+
scope.layer(4, (x, group) => ownMixed(this.#t4.forwardAttn(x, group.cache[4]!, group.phase, null)), x => this.#t4.feedforward(x));
|
|
500
|
+
scope.layer(5, (x, group) => ownMixed(this.#t5.forwardAttn(x, group.cache[5]!, group.phase, null)), x => this.#t5.feedforward(x));
|
|
501
|
+
scope.layer(6, (x, group) => ownMixed(this.#t6.forwardAttn(x, group.cache[6]!, group.phase, null)), x => this.#t6.feedforward(x));
|
|
502
|
+
scope.layer(7, (x, group) => ownMixed(this.#t7.forwardAttn(x, group.cache[7]!, group.phase, null)), x => this.#t7.feedforward(x));
|
|
503
|
+
scope.layer(8, (x, group) => ownMixed(this.#t8.forwardAttn(x, group.cache[8]!, group.phase, null)), x => this.#t8.feedforward(x));
|
|
504
|
+
scope.layer(9, (x, group) => ownMixed(this.#t9.forwardAttn(x, group.cache[9]!, group.phase, null)), x => this.#t9.feedforward(x));
|
|
505
|
+
scope.layer(10, (x, group) => ownMixed(this.#t10.forwardAttn(x, group.cache[10]!, group.phase, null)), x => this.#t10.feedforward(x));
|
|
506
|
+
scope.layer(11, (x, group) => ownMixed(this.#t11.forwardAttn(x, group.cache[11]!, group.phase, null)), x => this.#t11.feedforward(x));
|
|
507
|
+
scope.layer(12, (x, group) => ownMixed(this.#t12.forwardAttn(x, group.cache[12]!, group.phase, null)), x => this.#t12.feedforward(x));
|
|
508
|
+
scope.layer(13, (x, group) => ownMixed(this.#t13.forwardAttn(x, group.cache[13]!, group.phase, null)), x => this.#t13.feedforward(x));
|
|
509
|
+
scope.layer(14, (x, group) => ownMixed(this.#t14.forwardAttn(x, group.cache[14]!, group.phase, null)), x => this.#t14.feedforward(x));
|
|
510
|
+
scope.layer(15, (x, group) => ownMixed(this.#t15.forwardAttn(x, group.cache[15]!, group.phase, null)), x => this.#t15.feedforward(x));
|
|
511
|
+
scope.layer(16, (x, group) => ownMixed(this.#t16.forwardAttn(x, group.cache[16]!, group.phase, null)), x => this.#t16.feedforward(x));
|
|
512
|
+
scope.layer(17, (x, group) => ownMixed(this.#t17.forwardAttn(x, group.cache[17]!, group.phase, null)), x => this.#t17.feedforward(x));
|
|
513
|
+
scope.layer(18, (x, group) => ownMixed(this.#t18.forwardAttn(x, group.cache[18]!, group.phase, null)), x => this.#t18.feedforward(x));
|
|
514
|
+
scope.layer(19, (x, group) => ownMixed(this.#t19.forwardAttn(x, group.cache[19]!, group.phase, null)), x => this.#t19.feedforward(x));
|
|
515
|
+
scope.layer(20, (x, group) => ownMixed(this.#t20.forwardAttn(x, group.cache[20]!, group.phase, null)), x => this.#t20.feedforward(x));
|
|
516
|
+
scope.layer(21, (x, group) => ownMixed(this.#t21.forwardAttn(x, group.cache[21]!, group.phase, null)), x => this.#t21.feedforward(x));
|
|
517
|
+
scope.layer(22, (x, group) => ownMixed(this.#t22.forwardAttn(x, group.cache[22]!, group.phase, null)), x => this.#t22.feedforward(x));
|
|
518
|
+
scope.layer(23, (x, group) => ownMixed(this.#t23.forwardAttn(x, group.cache[23]!, group.phase, null)), x => this.#t23.feedforward(x));
|
|
519
|
+
scope.layer(24, (x, group) => ownMixed(this.#t24.forwardAttn(x, group.cache[24]!, group.phase, null)), x => this.#t24.feedforward(x));
|
|
520
|
+
scope.layer(25, (x, group) => ownMixed(this.#t25.forwardAttn(x, group.cache[25]!, group.phase, null)), x => this.#t25.feedforward(x));
|
|
521
|
+
scope.layer(26, (x, group) => ownMixed(this.#t26.forwardAttn(x, group.cache[26]!, group.phase, null)), x => this.#t26.feedforward(x));
|
|
522
|
+
scope.layer(27, (x, group) => ownMixed(this.#t27.forwardAttn(x, group.cache[27]!, group.phase, null)), x => this.#t27.feedforward(x));
|
|
523
|
+
scope.layer(28, (x, group) => ownMixed(this.#t28.forwardAttn(x, group.cache[28]!, group.phase, null)), x => this.#t28.feedforward(x));
|
|
524
|
+
scope.layer(29, (x, group) => ownMixed(this.#t29.forwardAttn(x, group.cache[29]!, group.phase, null)), x => this.#t29.feedforward(x));
|
|
525
|
+
return scope.finish(30, this.finalNorm);
|
|
526
|
+
};
|
|
527
|
+
#embedTokens(ids: MlxArray): MlxArray { const hidden = this.embed.encode(ids); return disposing(hidden, ops.mulScalar(hidden, this.embedScale)); }
|
|
528
|
+
loraTargets() { return denseLoraTargets(this.#parts); }
|
|
529
|
+
materializeGraphConstants(): void { ops.evalAll(this.#constants.borrow()); }
|
|
530
|
+
dispose(): void { this.#constants.close(); }
|
|
531
|
+
bindMediaInput(input: Vision) { return bindGemma4MediaInput(this, input); }
|
|
532
|
+
mediaEncoders(modelDir: string, probes: MediaSidecarProbes) { return gemma4MediaEncoders(this, modelDir, probes); }
|
|
533
|
+
draftTarget(caches: Cache[]) { return gemma4DraftTarget(this, caches); }
|
|
534
|
+
logitsFromHidden(hidden: MlxArray): MlxArray { return this.#head(hidden); }
|
|
535
|
+
readonly mixedSeparate = Object.freeze({
|
|
536
|
+
decode: Object.freeze([{ from: 1, to: 7, operation: (group: MlxPhaseGroup) => this.#decode(group.ids, group.state, group.capture) }, { from: 8, to: Infinity, operation: (group: MlxPhaseGroup) => this.#sortedDecode(group.ids, group.state, group.capture) }]),
|
|
537
|
+
window: Object.freeze([{ from: 1, to: 7, operation: (group: MlxPhaseGroup) => this.#window(group.ids, group.state, group.capture) }, { from: 8, to: Infinity, operation: (group: MlxPhaseGroup) => this.#sortedWindow(group.ids, group.state, group.capture) }]),
|
|
538
|
+
verify: Object.freeze([{ from: 1, to: 7, operation: (group: MlxPhaseGroup) => this.#window(group.ids, group.state, group.capture) }, { from: 8, to: Infinity, operation: (group: MlxPhaseGroup) => this.#sortedWindow(group.ids, group.state, group.capture) }]),
|
|
539
|
+
});
|
|
540
|
+
readonly sequence = Object.freeze([{ from: 1, to: 7, operation: (ids: MlxArray, caches: Cache[]) => this.#window(ids, caches, null) }, { from: 8, to: Infinity, operation: (ids: MlxArray, caches: Cache[]) => this.#sortedWindow(ids, caches, null) }]);
|
|
541
|
+
|
|
542
|
+
readonly mediaPrefill = Object.freeze([{ from: 1, to: 7, operation: (embeds: MlxArray, caches: Cache[], bidir: MlxArray | null, ids: MlxArray, multimodal: MlxArray | null) => writtenMedia(this.embedScale, (hidden, cache, phase, tokens) => this.#windowHidden(hidden, cache, phase, tokens, null), embeds, caches, bidir, ids) }, { from: 8, to: Infinity, operation: (embeds: MlxArray, caches: Cache[], bidir: MlxArray | null, ids: MlxArray, multimodal: MlxArray | null) => writtenMedia(this.embedScale, (hidden, cache, phase, tokens) => this.#sortedWindowHidden(hidden, cache, phase, tokens, null), embeds, caches, bidir, ids) }]);
|
|
543
|
+
}
|
|
544
|
+
import { writtenMedia, writtenInputMedia } from "./written-input";
|