doppler-gpu 0.4.15 → 0.6.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/CHANGELOG.md +33 -577
- package/LICENSE +21 -201
- package/README.md +165 -213
- package/models/catalog.json +241 -33
- package/package.json +122 -86
- package/src/capsule-runtime.d.ts +23 -0
- package/src/capsule-runtime.js +23 -0
- package/src/capsule.d.ts +8 -0
- package/src/capsule.js +4 -0
- package/src/cli/argument-contract.js +200 -0
- package/src/cli/browser-report-output.js +24 -0
- package/src/cli/cli-model-resolution.js +2 -2
- package/src/cli/cli-output.js +10 -0
- package/src/cli/commands/bundle.js +828 -0
- package/src/cli/doppler-cli.js +101 -1349
- package/src/cli/doppler-serve.js +18 -244
- package/src/cli/output/manifest.js +368 -0
- package/src/cli/runtime-profiles.js +4 -8
- package/src/cli/serve/capsule-handler.d.ts +33 -0
- package/src/cli/serve/capsule-handler.js +105 -0
- package/src/cli/serve/capsule-http.js +102 -0
- package/src/cli/serve/receipt.js +269 -0
- package/src/client/capsule-host.browser.js +1 -0
- package/src/client/capsule-host.d.ts +3 -0
- package/src/client/capsule-host.js +1 -0
- package/src/client/doppler-api.browser.js +42 -0
- package/src/client/doppler-api.d.ts +64 -1
- package/src/client/doppler-api.js +42 -1
- package/src/client/doppler-registry.js +15 -7
- package/src/client/electron/index.d.ts +4 -0
- package/src/client/electron/index.js +13 -0
- package/src/client/electron/ipc-contract.d.ts +9 -0
- package/src/client/electron/ipc-contract.js +56 -0
- package/src/client/electron/release-state.d.ts +53 -0
- package/src/client/electron/release-state.js +425 -0
- package/src/client/electron/renderer-runtime.d.ts +19 -0
- package/src/client/electron/renderer-runtime.js +104 -0
- package/src/client/failure-taxonomy.js +1 -28
- package/src/client/fault-injection.js +0 -8
- package/src/client/inspection.d.ts +122 -0
- package/src/client/inspection.js +343 -0
- package/src/client/model-host/index.d.ts +140 -0
- package/src/client/model-host/index.js +548 -0
- package/src/client/model-host/model-evidence.js +255 -0
- package/src/client/model-host/model-session.d.ts +322 -0
- package/src/client/model-host/model-session.js +593 -0
- package/src/client/provider.d.ts +18 -2
- package/src/client/provider.js +26 -3
- package/src/client/receipt.js +30 -72
- package/src/client/runtime/capsule-acquisition.d.ts +18 -0
- package/src/client/runtime/capsule-acquisition.js +103 -0
- package/src/client/runtime/capsule-adapter-execution.d.ts +17 -0
- package/src/client/runtime/capsule-adapter-execution.js +63 -0
- package/src/client/runtime/capsule-artifact-source.js +62 -0
- package/src/client/runtime/capsule-embedding.d.ts +29 -0
- package/src/client/runtime/capsule-embedding.js +76 -0
- package/src/client/runtime/capsule-forecast-program.d.ts +2 -0
- package/src/client/runtime/capsule-forecast-program.js +85 -0
- package/src/client/runtime/capsule-forecast.d.ts +17 -0
- package/src/client/runtime/capsule-forecast.js +31 -0
- package/src/client/runtime/capsule-operation-adapters.d.ts +12 -0
- package/src/client/runtime/capsule-operation-adapters.js +68 -0
- package/src/client/runtime/capsule-operation-executor.d.ts +25 -0
- package/src/client/runtime/capsule-operation-executor.js +100 -0
- package/src/client/runtime/capsule-program-adapter.js +151 -0
- package/src/client/runtime/capsule-release-authorization.js +25 -0
- package/src/client/runtime/capsule-rerank.d.ts +50 -0
- package/src/client/runtime/capsule-rerank.js +105 -0
- package/src/client/runtime/capsule-session-execution.js +85 -0
- package/src/client/runtime/command-executor.d.ts +15 -0
- package/src/client/runtime/command-executor.js +128 -0
- package/src/client/runtime/composition-root.d.ts +74 -0
- package/src/client/runtime/composition-root.js +344 -0
- package/src/client/runtime/fetch-capsule-artifact-store.d.ts +10 -0
- package/src/client/runtime/fetch-capsule-artifact-store.js +25 -0
- package/src/client/runtime/index.d.ts +1 -63
- package/src/client/runtime/index.js +1 -261
- package/src/client/runtime/lora.d.ts +14 -0
- package/src/client/runtime/lora.js +19 -1
- package/src/client/runtime/model-manager.d.ts +0 -4
- package/src/client/runtime/model-manager.js +12 -28
- package/src/client/runtime/model-rerank.js +30 -0
- package/src/client/runtime/model-session.d.ts +1 -174
- package/src/client/runtime/model-source.d.ts +19 -1
- package/src/client/runtime/model-source.js +46 -17
- package/src/client/runtime/node-quickstart-cache.js +1 -1
- package/src/client/runtime/resolution-policy.d.ts +20 -0
- package/src/client/runtime/resolution-policy.js +84 -0
- package/src/client/runtime/resource-binder.d.ts +24 -0
- package/src/client/runtime/resource-binder.js +135 -0
- package/src/client/runtime/scoped-session.d.ts +126 -0
- package/src/client/runtime/scoped-session.js +276 -0
- package/src/client/runtime/session-controller.d.ts +30 -0
- package/src/client/runtime/session-controller.js +170 -0
- package/src/client/runtime/source-runtime.js +1 -1
- package/src/client/runtime/target-selector.d.ts +9 -0
- package/src/client/runtime/target-selector.js +2 -0
- package/src/client/runtime/types.d.ts +1 -1
- package/src/client/runtime/types.js +0 -1
- package/src/client/runtime/verified-capsule-artifact-store.js +72 -0
- package/src/client/wrap-pipeline-handle.js +3 -9
- package/src/config/backward-registry-loader.js +1 -1
- package/src/config/capsule-adapter-policy.js +30 -0
- package/src/config/capsule-adapters.d.ts +21 -0
- package/src/config/capsule-adapters.js +40 -0
- package/src/config/capsule-adapters.json +9 -0
- package/src/config/capsule-loading.d.ts +2 -0
- package/src/config/capsule-loading.js +14 -0
- package/src/config/capsule-loading.schema.json +11 -0
- package/src/config/capsule-operation.d.ts +19 -0
- package/src/config/capsule-operation.js +58 -0
- package/src/config/capsule-operations.json +18 -0
- package/src/config/capsule-release-contract.d.ts +66 -0
- package/src/config/capsule-release-contract.js +197 -0
- package/src/config/capsule-release-events.d.ts +53 -0
- package/src/config/capsule-release-events.js +172 -0
- package/src/config/capsule-sequence-receipt.js +19 -0
- package/src/config/capsule-serve.d.ts +9 -0
- package/src/config/capsule-serve.js +28 -0
- package/src/config/capsule-serve.schema.json +14 -0
- package/src/config/capsule-signature.d.ts +6 -0
- package/src/config/capsule-signature.js +50 -0
- package/src/config/capsule-v2.d.ts +70 -0
- package/src/config/capsule-v2.js +431 -0
- package/src/config/capsule-v3.d.ts +19 -0
- package/src/config/capsule-v3.js +72 -0
- package/src/config/capsule.d.ts +22 -0
- package/src/config/capsule.js +52 -0
- package/src/config/conversion/amplify/amplify-120m-f16-af32.json +1 -1
- package/src/config/conversion/embeddinggemma/google-embeddinggemma-300m-q4k-ehf16-af32.json +1 -1
- package/src/config/conversion/esm/esm2-t12-35m-ur50d-f32-af32.json +41 -1
- package/src/config/conversion/esmc/esmc-300m-f32-af32.json +1 -1
- package/src/config/conversion/gemma3/gemma-3-1b-it-q4k-ehf16-af32.json +1 -1
- package/src/config/conversion/gemma3/gemma-3-270m-it-f16-af32.json +1 -1
- package/src/config/conversion/gemma3/gemma-3-270m-it-q4k-ehf16-af16.json +1 -1
- package/src/config/conversion/gemma3/gemma-3-270m-it-q4k-ehf16-af32.json +1 -1
- package/src/config/conversion/gemma3/translategemma-4b-1b-enes-q4k-ehf16-af32.json +1 -1
- package/src/config/conversion/gemma3/translategemma-4b-it-q4k-ehf16-af32.json +1 -1
- package/src/config/conversion/gemma4/gemma-4-12b-it-text-q4k-ehf16-af16.json +2 -2
- package/src/config/conversion/gemma4/gemma-4-12b-it-text-q4k-ehf16-af32.json +1 -1
- package/src/config/conversion/gemma4/gemma-4-12b-it-text-q4k-ehf16-hq4k-af16.json +1 -1
- package/src/config/conversion/gemma4/gemma-4-12b-it-text-w4a16-ct-ehf16-af16.json +1 -1
- package/src/config/conversion/gemma4/gemma-4-31b-it-text-q4k-ehf16-af16.json +2 -2
- package/src/config/conversion/gemma4/gemma-4-31b-it-text-q4k-ehf16-af32.json +1 -1
- package/src/config/conversion/gemma4/gemma-4-e2b-it-q4k-ehf16-af16-int4ple.json +3 -3
- package/src/config/conversion/gemma4/gemma-4-e2b-it-q4k-ehf16-af32-int4ple.json +1 -1
- package/src/config/conversion/gemma4/gemma-4-e2b-it-q4k-ehf16-af32.json +1 -1
- package/src/config/conversion/glmocr/glm-ocr-f16-af32.json +442 -0
- package/src/config/conversion/minicpm/minicpm4-0-5b-f16-af32.json +1 -1
- package/src/config/conversion/muse-glimmer/muse-glimmer-30b-text-bf16-af32.json +529 -0
- package/src/config/conversion/muse-glimmer/muse-glimmer-30b-text-f16-af32.json +529 -0
- package/src/config/conversion/nucleotide-transformer/nucleotide-transformer-v2-50m-f32-af32.json +1 -1
- package/src/config/conversion/qwen3/qwen-3-5-0-8b-q4k-ehaf16.json +1 -1
- package/src/config/conversion/qwen3/qwen-3-5-2b-q4k-ehaf16.json +1 -1
- package/src/config/conversion/qwen3/qwen-3-5-9b-f16-af32.json +1 -1
- package/src/config/conversion/qwen3/qwen-3-5-9b-q4k-ehaf16.json +1 -1
- package/src/config/conversion/qwen3/qwen-3-6-27b-q4k-eaf16.json +2 -2
- package/src/config/conversion/qwen3/qwen-3-6-27b-q4k-ehaf16.json +1 -1
- package/src/config/conversion/qwen3/qwen-3-embedding-0-6b-f16-af32.json +463 -0
- package/src/config/conversion/qwen3/qwen-3-embedding-0-6b-q4k-ehf16-af32.json +1 -1
- package/src/config/conversion/qwen3/qwen-3-reranker-0-6b-f16-af32.json +1 -1
- package/src/config/conversion/qwen3/qwen-3-reranker-0-6b-f16-true-logit-af32.json +481 -0
- package/src/config/conversion/qwen3/qwen-3-reranker-0-6b-q4k-ehf16-af32.json +59 -7
- package/src/config/conversion/qwen3/qwen3.8-27b-text-f16-af32.json +561 -0
- package/src/config/conversion/qwen3/qwen3.8-27b-text-q4k-ehaf16.json +558 -0
- package/src/config/dtype-transition-contract.js +40 -0
- package/src/config/embedding-contract.js +26 -0
- package/src/config/embedding-reference.js +85 -0
- package/src/config/evidence/boundary-evidence-policy.json +54 -0
- package/src/config/evidence/source-intake-policy.json +122 -0
- package/src/config/execution-contract-check.d.ts +16 -73
- package/src/config/execution-contract-check.js +7 -429
- package/src/config/forecast-manifest.js +56 -0
- package/src/config/heterogeneous-attention-contract.js +46 -0
- package/src/config/initial-execution-identity.d.ts +58 -0
- package/src/config/initial-execution-identity.js +331 -0
- package/src/config/inspection/observation-policies.json +80 -0
- package/src/config/kernel-path-loader.d.ts +10 -0
- package/src/config/kernel-path-loader.js +23 -8
- package/src/config/kernel-registry-contract.d.ts +25 -0
- package/src/config/kernel-registry-contract.js +46 -0
- package/src/config/kernels/kernel-ref-digests.js +50 -2
- package/src/config/kernels/registry.js +1 -1
- package/src/config/kernels/registry.json +13592 -11377
- package/src/config/lora-layouts.d.ts +10 -0
- package/src/config/lora-layouts.js +17 -0
- package/src/config/lora-layouts.json +9 -0
- package/src/config/manifest-inference-validation.js +575 -0
- package/src/config/merge/execution-patches.js +12 -0
- package/src/config/{merge-helpers.js → merge/kernel-path-policy.js} +0 -52
- package/src/config/merge/value-selection.js +38 -0
- package/src/config/merge.js +43 -11
- package/src/config/model-ir-v2.d.ts +57 -0
- package/src/config/model-ir-v2.js +258 -0
- package/src/config/model-ir.d.ts +49 -0
- package/src/config/model-ir.js +180 -0
- package/src/config/param-validator.js +22 -37
- package/src/config/platforms/amd-strix-halo-gfx1151.json +17 -0
- package/src/config/platforms/loader.js +2 -1
- package/src/config/production-release-evidence.d.ts +23 -0
- package/src/config/production-release-evidence.js +423 -0
- package/src/config/production-release.js +321 -0
- package/src/{client/doppler-registry.json → config/quickstart-registry.json} +7 -7
- package/src/config/required-inference-fields-contract-check.js +2 -2
- package/src/config/rerank-reference.d.ts +28 -0
- package/src/config/rerank-reference.js +89 -0
- package/src/config/revocation-policy.d.ts +65 -0
- package/src/config/revocation-policy.js +290 -0
- package/src/config/revocation-registry.json +11 -0
- package/src/config/revocation-updates.d.ts +68 -0
- package/src/config/revocation-updates.js +440 -0
- package/src/config/runtime/compute/f16-activations.json +1 -0
- package/src/config/runtime/compute/f16-batched.json +1 -0
- package/src/config/runtime/default.json +1 -0
- package/src/config/runtime/diagnostics/debug-logits.json +1 -0
- package/src/config/runtime/experiments/bench/gemma3-bench-q4k-alt.json +1 -0
- package/src/config/runtime/experiments/bench/gemma3-bench-q4k-batched.json +1 -0
- package/src/config/runtime/experiments/bench/gemma3-bench-q4k.json +1 -0
- package/src/config/runtime/experiments/bench/gemma4-bench-q4k.json +1 -0
- package/src/config/runtime/experiments/bench/qwen3-bench-q4k-alt.json +1 -0
- package/src/config/runtime/experiments/bench/qwen3-bench-q4k-batched.json +1 -0
- package/src/config/runtime/experiments/debug/gemma3-browser-diagnose-q4k.json +1 -1
- package/src/config/runtime/experiments/debug/gemma3-debug-q4k.json +1 -3
- package/src/config/runtime/experiments/debug/gemma4-e2b-browser-diagnose-q4k.json +1 -1
- package/src/config/runtime/experiments/debug/qwen35-browser-diagnose-q4k.json +1 -1
- package/src/config/runtime/experiments/verify/gemma3-verify.json +1 -3
- package/src/config/runtime/experiments/verify/gemma4-e2b-verify.json +1 -3
- package/src/config/runtime/experiments/verify/lfm2-verify.json +1 -3
- package/src/config/runtime/experiments/verify/translategemma-verify.json +1 -3
- package/src/config/runtime/model/gemma2-debug.json +1 -0
- package/src/config/runtime/model/gemma2-pipeline-debug.json +1 -0
- package/src/config/runtime/model/gemma2-pipeline.json +1 -0
- package/src/config/runtime/model/gemma3-layer-probe.json +1 -0
- package/src/config/runtime/model/qwen3-5-layer-probe.json +1 -0
- package/src/config/runtime/model/qwen3-5-layer3-boundary-probe.json +1 -0
- package/src/config/runtime/model/qwen3-5-linear-attn-debug.json +1 -0
- package/src/config/runtime/optimization-candidates/registry.json +5 -0
- package/src/config/runtime/platform/metal-apple-q4k.json +1 -0
- package/src/config/runtime/profiles/default.json +1 -0
- package/src/config/runtime/profiles/diffusiongemma-26b-a4b-throughput.json +1 -0
- package/src/config/runtime/profiles/gemma3-270m-q4k-throughput-overlapped-probe.json +1 -0
- package/src/config/runtime/profiles/gemma3-270m-q4k-throughput-probe.json +1 -0
- package/src/config/runtime/profiles/gemma4-12b-rdrr-safe-b4.json +1 -3
- package/src/config/runtime/profiles/gemma4-12b-rdrr-safe-single.json +1 -3
- package/src/config/runtime/profiles/gemma4-31b-f16-activations-probe.json +2 -6
- package/src/config/runtime/profiles/gemma4-e2b-baseline-probe.json +1 -3
- package/src/config/runtime/profiles/gemma4-e2b-fused-rmsnorm-probe.json +1 -3
- package/src/config/runtime/profiles/gemma4-e2b-fused-rmsnorm-test.json +1 -0
- package/src/config/runtime/profiles/gemma4-e2b-int4ple-baseline-probe.json +1 -5
- package/src/config/runtime/profiles/gemma4-e2b-int4ple-f16a-probe.json +1 -5
- package/src/config/runtime/profiles/gemma4-e2b-int4ple-reference-export.json +1 -3
- package/src/config/runtime/profiles/gemma4-e2b-int4ple-retain-q4k-probe.json +1 -5
- package/src/config/runtime/profiles/gemma4-e2b-low-memory.json +1 -0
- package/src/config/runtime/profiles/gemma4-e2b-prefill-profile.json +1 -3
- package/src/config/runtime/profiles/gemma4-e2b-throughput-probe.json +1 -0
- package/src/config/runtime/profiles/low-memory.json +1 -0
- package/src/config/runtime/profiles/muse-glimmer-30b-source-parity-f32kv.json +30 -0
- package/src/config/runtime/profiles/muse-glimmer-30b-source-parity.json +110 -0
- package/src/config/runtime/profiles/production.json +1 -3
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-attn128-candidate.json +1 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-maxbatch64-candidate.json +1 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-normed-ffn-candidate.json +1 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-overlap-ringtokens1-candidate.json +1 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-parity.json +1 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-sequential-candidate.json +1 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-throughput.json +1 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-prefill-profile.json +1 -3
- package/src/config/runtime/profiles/qwen-3-5-0-8b-throughput-probe.json +1 -0
- package/src/config/runtime/profiles/qwen-3-5-2b-f16-primary-throughput.json +1 -5
- package/src/config/runtime/profiles/qwen-3-5-2b-metal-parity.json +1 -0
- package/src/config/runtime/profiles/qwen-3-5-2b-metal-throughput.json +1 -0
- package/src/config/runtime/profiles/qwen-3-5-9b-metal-correctness.json +1 -0
- package/src/config/runtime/profiles/qwen-3-embedding-0-6b-metal-stability.json +1 -3
- package/src/config/runtime/profiles/qwen-3-embedding-0-6b-metal-throughput.json +1 -0
- package/src/config/runtime/profiles/qwen-3-reranker-0-6b-metal-stability.json +1 -0
- package/src/config/runtime/profiles/qwen-3-reranker-0-6b-metal-throughput.json +1 -0
- package/src/config/runtime/profiles/qwen3-6-27b-f16-activations-probe.json +2 -6
- package/src/config/runtime/profiles/qwen3-8-27b-source-parity.json +87 -0
- package/src/config/runtime/profiles/rerank-stability.json +1 -3
- package/src/config/runtime/profiles/rerank-throughput.json +1 -3
- package/src/config/runtime/profiles/simulation.json +1 -3
- package/src/config/runtime/profiles/throughput.json +1 -3
- package/src/config/runtime/profiles/trace-layers.json +1 -3
- package/src/config/runtime/profiles/translategemma-1b-nativekd2-q4k-rdna3-throughput-probe.json +1 -0
- package/src/config/runtime/profiles/turboquant-contiguous-prod.json +1 -0
- package/src/config/runtime/profiles/turboquant-contiguous.json +1 -0
- package/src/config/runtime/profiles/turboquant.json +1 -0
- package/src/config/runtime/profiles/vector-stability.json +1 -3
- package/src/config/runtime/profiles/vector-throughput.json +1 -0
- package/src/config/runtime/profiles/verbose-trace.json +1 -3
- package/src/config/runtime/tiers/gemma4-128gb.json +1 -0
- package/src/config/runtime/tiers/gemma4-16gb.json +1 -0
- package/src/config/runtime/tiers/gemma4-24gb.json +1 -0
- package/src/config/runtime/tiers/gemma4-32gb.json +1 -0
- package/src/config/runtime-merge.js +1 -1
- package/src/config/runtime.js +1 -1
- package/src/config/schema/benchmark.schema.d.ts +7 -0
- package/src/config/schema/benchmark.schema.js +5 -0
- package/src/config/schema/browser-suite-metrics.schema.d.ts +1 -0
- package/src/config/schema/browser-suite-metrics.schema.js +2 -0
- package/src/config/schema/conversion-report.schema.d.ts +3 -0
- package/src/config/schema/conversion-report.schema.js +29 -0
- package/src/config/schema/converter.schema.d.ts +1 -0
- package/src/config/schema/debug.schema.d.ts +1 -0
- package/src/config/schema/doppler.schema.js +5 -5
- package/src/config/schema/emulation.schema.js +1 -1
- package/src/config/schema/execution-v1.schema.d.ts +3 -0
- package/src/config/schema/execution-v1.schema.js +19 -0
- package/src/config/schema/harness.schema.d.ts +13 -0
- package/src/config/schema/harness.schema.js +6 -0
- package/src/config/schema/index.d.ts +8 -16
- package/src/config/schema/index.js +5 -8
- package/src/config/schema/inference.schema.d.ts +21 -2
- package/src/config/schema/kernel-thresholds.schema.d.ts +0 -12
- package/src/config/schema/kernel-thresholds.schema.js +1 -13
- package/src/config/schema/kernel-warmup.schema.d.ts +1 -3
- package/src/config/schema/kernel-warmup.schema.js +0 -1
- package/src/config/schema/manifest.schema.d.ts +26 -7
- package/src/config/schema/manifest.schema.js +10 -1
- package/src/config/schema/program-bundle.schema.d.ts +52 -6
- package/src/config/schema/program-bundle.schema.js +145 -10
- package/src/config/schema/runtime-profile.schema.d.ts +24 -0
- package/src/config/schema/runtime-profile.schema.js +92 -0
- package/src/config/schema/shared-runtime.schema.d.ts +1 -4
- package/src/config/schema/shared-runtime.schema.js +0 -2
- package/src/config/schema/tooling.schema.d.ts +8 -5
- package/src/config/schema/tooling.schema.js +3 -2
- package/src/config/sequence-reference.d.ts +26 -0
- package/src/config/sequence-reference.js +100 -0
- package/src/config/target-plan.d.ts +108 -0
- package/src/config/target-plan.js +411 -0
- package/src/config/training-defaults.js +1 -1
- package/src/config/transforms/capability-transform-resolver.js +4 -105
- package/src/config/transforms/execution-graph/dtype.js +591 -0
- package/src/config/transforms/execution-graph/fusion.js +436 -0
- package/src/config/transforms/execution-graph/validation.js +414 -0
- package/src/config/transforms/execution-graph-transforms.js +13 -1419
- package/src/converter/artifact-identity.js +475 -0
- package/src/converter/conversion-plan.js +1 -1
- package/src/converter/core.js +94 -2096
- package/src/converter/forge-candidate-evaluation.js +271 -0
- package/src/converter/forge-qualification.js +162 -0
- package/src/converter/forge-stages.js +798 -0
- package/src/converter/manifest-builder.js +683 -0
- package/src/converter/quantization-info.js +4 -1
- package/src/converter/quantizer.js +58 -9
- package/src/converter/shard-packer.d.ts +1 -1
- package/src/converter/shard-packer.js +2 -2
- package/src/converter/tensor-transform.js +640 -0
- package/src/converter/tokenizer-contract.js +243 -0
- package/src/converter/tokenizer-manifest.js +243 -0
- package/src/debug/history.js +0 -8
- package/src/debug/index.js +0 -10
- package/src/debug/tensor.d.ts +2 -0
- package/src/debug/tensor.js +21 -2
- package/src/debug/trace.js +0 -7
- package/src/experimental/adapters/adapter-manager.js +2 -0
- package/src/experimental/adapters/adapter-manifest.d.ts +6 -0
- package/src/experimental/adapters/adapter-manifest.js +11 -0
- package/src/experimental/adapters/artifact-contract.d.ts +2 -2
- package/src/experimental/adapters/artifact-contract.js +5 -2
- package/src/experimental/adapters/lora-loader.d.ts +10 -1
- package/src/experimental/adapters/lora-loader.js +110 -48
- package/src/{inference/pipelines → experimental}/audio/gemma4.js +9 -31
- package/src/experimental/bdpa/basis-decomposed-paged.d.ts +50 -0
- package/src/experimental/bdpa/basis-decomposed-paged.js +276 -0
- package/src/experimental/bridge/trainer-artifact-bridge.js +3 -3
- package/src/experimental/browser/browser-converter.js +11 -11
- package/src/experimental/diffusion/index.d.ts +1 -1
- package/src/experimental/diffusion/index.js +1 -1
- package/src/{inference/pipelines → experimental}/diffusion/pipeline.d.ts +2 -2
- package/src/{inference/pipelines → experimental}/diffusion/pipeline.js +17 -17
- package/src/{inference/pipelines → experimental}/diffusion/text-encoder-gpu.js +21 -19
- package/src/experimental/diffusion/vae.js +596 -0
- package/src/experimental/distribution/shard-delivery/receipt.js +588 -0
- package/src/experimental/distribution/shard-delivery/retry.js +392 -0
- package/src/experimental/distribution/shard-delivery/transport.js +831 -0
- package/src/experimental/distribution/shard-delivery.js +4 -1709
- package/src/experimental/energy/index.d.ts +2 -2
- package/src/experimental/energy/index.js +2 -2
- package/src/{inference/pipelines → experimental}/energy/pipeline.js +13 -13
- package/src/{inference/pipelines → experimental}/energy/types.d.ts +2 -2
- package/src/experimental/logits/cpu-output.d.ts +27 -0
- package/src/{inference/pipelines/text/logits/utils.js → experimental/logits/cpu-output.js} +19 -27
- package/src/experimental/logits/cpu.d.ts +87 -0
- package/src/experimental/logits/cpu.js +95 -0
- package/src/experimental/orchestration/energy-row-head-pipeline.d.ts +103 -0
- package/src/{inference/pipelines/energy-head/row-head-pipeline.js → experimental/orchestration/energy-row-head-pipeline.js} +13 -13
- package/src/experimental/orchestration/index.d.ts +1 -1
- package/src/experimental/orchestration/index.js +1 -1
- package/src/experimental/training/artifacts.js +2 -2
- package/src/experimental/training/checkpoint.js +2 -2
- package/src/experimental/training/distillation/artifacts.js +1 -1
- package/src/experimental/training/distillation/dataset.js +2 -2
- package/src/experimental/training/distillation/eval.js +1 -1
- package/src/experimental/training/distillation/runtime.js +2 -2
- package/src/experimental/training/distillation/student-fixture/execution.js +166 -0
- package/src/experimental/training/distillation/student-fixture/model.js +353 -0
- package/src/experimental/training/distillation/student-fixture.js +2 -469
- package/src/experimental/training/lora/checkpoint.js +746 -0
- package/src/experimental/training/lora/execution.js +550 -0
- package/src/experimental/training/lora/plan.js +387 -0
- package/src/experimental/training/lora/recovery.js +418 -0
- package/src/experimental/training/lora-pipeline.js +7 -1949
- package/src/experimental/training/operator-artifacts.js +2 -2
- package/src/experimental/training/operator-command.js +4 -4
- package/src/experimental/training/runner/evaluation.js +62 -0
- package/src/experimental/training/runner/lifecycle.js +257 -0
- package/src/experimental/training/runner.js +5 -268
- package/src/experimental/training/suite/execution.js +879 -0
- package/src/experimental/training/suite/plan.js +855 -0
- package/src/experimental/training/suite.js +5 -1659
- package/src/experimental/training/workloads.d.ts +5 -5
- package/src/experimental/training/workloads.js +6 -6
- package/src/experimental/turboquant/codebook.js +470 -0
- package/src/formats/quantization-constants.d.ts +11 -0
- package/src/formats/quantization-constants.js +11 -0
- package/src/formats/rdrr/integrity.js +1 -1
- package/src/formats/rdrr/lowerings.js +0 -29
- package/src/formats/rdrr/merkle.js +1 -1
- package/src/formats/rdrr/tensor-config-validator.js +25 -5
- package/src/formats/rdrr/validation.js +3 -3
- package/src/gpu/command-recorder.d.ts +15 -1
- package/src/gpu/command-recorder.js +63 -2
- package/src/gpu/constant-buffer.js +29 -0
- package/src/gpu/device-state.js +36 -0
- package/src/gpu/device.js +6 -41
- package/src/gpu/kernel-runtime.js +3 -10
- package/src/gpu/kernels/activation-static-qdq.js +1 -1
- package/src/gpu/kernels/append_embedding.wgsl +14 -0
- package/src/gpu/kernels/attention/executors.js +960 -0
- package/src/gpu/kernels/attention/plan.js +472 -0
- package/src/gpu/kernels/attention.d.ts +4 -0
- package/src/gpu/kernels/attention.js +145 -1545
- package/src/gpu/kernels/attention_relative.wgsl +59 -0
- package/src/gpu/kernels/backward/adam.js +2 -1
- package/src/gpu/kernels/backward/attention_backward.js +1 -1
- package/src/gpu/kernels/backward/bias_add_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/bias_add_backward.js +1 -1
- package/src/gpu/kernels/backward/causal_conv1d_silu_backward.js +2 -1
- package/src/gpu/kernels/backward/conv2d_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/conv2d_backward.js +2 -1
- package/src/gpu/kernels/backward/cross_entropy_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/cross_entropy_backward.js +1 -1
- package/src/gpu/kernels/backward/embed_backward.js +2 -1
- package/src/gpu/kernels/backward/executor.d.ts +34 -0
- package/src/gpu/kernels/backward/executor.js +158 -0
- package/src/gpu/kernels/backward/gated_delta_recurrent_backward.js +2 -1
- package/src/gpu/kernels/backward/gated_rmsnorm_backward.js +2 -1
- package/src/gpu/kernels/backward/gelu_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/gelu_backward.js +1 -1
- package/src/gpu/kernels/backward/groupnorm_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/groupnorm_backward.js +1 -1
- package/src/gpu/kernels/backward/layernorm_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/layernorm_backward.js +2 -1
- package/src/gpu/kernels/backward/matmul.js +133 -0
- package/src/gpu/kernels/backward/matmul_backward.d.ts +0 -1
- package/src/gpu/kernels/backward/matmul_backward.js +1 -1
- package/src/gpu/kernels/backward/pixel_shuffle_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/pixel_shuffle_backward.js +1 -1
- package/src/gpu/kernels/backward/rmsnorm_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/rmsnorm_backward.js +2 -1
- package/src/gpu/kernels/backward/rope_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/rope_backward.js +2 -1
- package/src/gpu/kernels/backward/scale_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/scale_backward.js +2 -1
- package/src/gpu/kernels/backward/silu_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/silu_backward.js +1 -1
- package/src/gpu/kernels/backward/softmax_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/softmax_backward.js +1 -1
- package/src/gpu/kernels/backward/upsample2d_backward.d.ts +1 -1
- package/src/gpu/kernels/backward/upsample2d_backward.js +1 -1
- package/src/gpu/kernels/backward/utils.js +2 -392
- package/src/gpu/kernels/bind-group-validation.js +16 -0
- package/src/gpu/kernels/cast.js +2 -1
- package/src/gpu/kernels/check-finiteness.js +13 -49
- package/src/gpu/kernels/check-hot-vocab-stop.js +13 -39
- package/src/gpu/kernels/check-stop.js +14 -33
- package/src/gpu/kernels/check_finiteness.wgsl +33 -0
- package/src/gpu/kernels/check_hot_vocab_stop.wgsl +29 -0
- package/src/gpu/kernels/check_stop.wgsl +20 -0
- package/src/gpu/kernels/clamp.js +1 -1
- package/src/gpu/kernels/conv2d.js +1 -1
- package/src/gpu/kernels/cross_entropy_loss.js +1 -1
- package/src/gpu/kernels/depthwise_conv1d.js +1 -1
- package/src/gpu/kernels/depthwise_conv2d.js +1 -1
- package/src/gpu/kernels/dequant.js +7 -2
- package/src/gpu/kernels/diffusion-gemma-sampling.js +3 -5
- package/src/gpu/kernels/dispatch.js +12 -1
- package/src/gpu/kernels/embedding-pool.js +72 -0
- package/src/gpu/kernels/embedding_pool_f16.wgsl +38 -0
- package/src/gpu/kernels/embedding_pool_f32.wgsl +36 -0
- package/src/gpu/kernels/energy.js +2 -1
- package/src/gpu/kernels/forecast_output.wgsl +17 -0
- package/src/gpu/kernels/fused-gate-up-gelu.js +1 -1
- package/src/gpu/kernels/fused_ffn.js +10 -220
- package/src/gpu/kernels/fused_matmul_residual.js +2 -1
- package/src/gpu/kernels/fused_matmul_rmsnorm.js +2 -1
- package/src/gpu/kernels/fused_normed_ffn_q4k.wgsl +208 -0
- package/src/gpu/kernels/gated-short-conv.js +11 -69
- package/src/gpu/kernels/gated_delta_conv.wgsl +66 -0
- package/src/gpu/kernels/gated_delta_conv_f16.wgsl +68 -0
- package/src/gpu/kernels/gated_delta_fused_decode.wgsl +173 -0
- package/src/gpu/kernels/gated_delta_fused_decode_f16.wgsl +175 -0
- package/src/gpu/kernels/gated_delta_recurrent.wgsl +165 -0
- package/src/gpu/kernels/gated_delta_recurrent_f16.wgsl +167 -0
- package/src/gpu/kernels/gated_short_conv.wgsl +47 -0
- package/src/gpu/kernels/gather.d.ts +1 -1
- package/src/gpu/kernels/gather.js +30 -6
- package/src/gpu/kernels/gather_bf16.wgsl +69 -0
- package/src/gpu/kernels/gelu.js +1 -1
- package/src/gpu/kernels/gemma4-route-expert.js +1 -1
- package/src/gpu/kernels/grouped_pointwise_conv2d.js +1 -1
- package/src/gpu/kernels/groupnorm.js +1 -1
- package/src/gpu/kernels/index.d.ts +38 -7
- package/src/gpu/kernels/index.js +18 -6
- package/src/gpu/kernels/instance_stats.wgsl +26 -0
- package/src/gpu/kernels/kernel-base.js +1 -1
- package/src/gpu/kernels/kernel-cache-lifecycle.d.ts +9 -0
- package/src/gpu/kernels/kernel-cache-lifecycle.js +26 -0
- package/src/gpu/kernels/kernel-configs.d.ts +10 -61
- package/src/gpu/kernels/kernel-configs.js +26 -54
- package/src/gpu/kernels/kernel-execution.js +141 -0
- package/src/gpu/kernels/kernel-prewarm.d.ts +3 -0
- package/src/gpu/kernels/kernel-prewarm.js +60 -0
- package/src/gpu/kernels/kv-cache-write.js +4 -4
- package/src/gpu/kernels/kv-quantize.js +4 -1
- package/src/gpu/kernels/l2-normalize.js +36 -0
- package/src/gpu/kernels/l2_normalize.wgsl +53 -0
- package/src/gpu/kernels/layernorm.d.ts +4 -4
- package/src/gpu/kernels/layernorm.js +44 -7
- package/src/gpu/kernels/linear-attention-core.js +29 -449
- package/src/gpu/kernels/linear_attention.js +1 -1
- package/src/gpu/kernels/lm-head-argmax.js +2 -2
- package/src/gpu/kernels/lm-head-select-logits.js +2 -2
- package/src/gpu/kernels/logit-finalize.js +103 -0
- package/src/gpu/kernels/logit-merge.js +13 -125
- package/src/gpu/kernels/logit-scatter.js +40 -0
- package/src/gpu/kernels/logit_finalize_f16.wgsl +43 -0
- package/src/gpu/kernels/logit_finalize_f32.wgsl +41 -0
- package/src/gpu/kernels/logit_merge_geometric.wgsl +23 -0
- package/src/gpu/kernels/logit_merge_max.wgsl +23 -0
- package/src/gpu/kernels/logit_merge_weighted.wgsl +23 -0
- package/src/gpu/kernels/logit_scatter_f16.wgsl +26 -0
- package/src/gpu/kernels/logit_scatter_f32.wgsl +24 -0
- package/src/gpu/kernels/matmul-dispatch.js +6 -1
- package/src/gpu/kernels/matmul-selection.js +3 -2
- package/src/gpu/kernels/matmul.js +4 -15
- package/src/gpu/kernels/matmul_bf16w_f32a.wgsl +93 -0
- package/src/gpu/kernels/matmul_gemv_subgroup_bf16w.wgsl +433 -0
- package/src/gpu/kernels/modulate.js +1 -1
- package/src/gpu/kernels/moe.js +3 -1
- package/src/gpu/kernels/moe_router_bias_add_f16_f16.wgsl +23 -0
- package/src/gpu/kernels/moe_router_bias_add_f16_f32.wgsl +23 -0
- package/src/gpu/kernels/moe_router_bias_add_f32_f16.wgsl +23 -0
- package/src/gpu/kernels/moe_router_bias_add_f32_f32.wgsl +21 -0
- package/src/gpu/kernels/patch_context.wgsl +29 -0
- package/src/gpu/kernels/pipeline-cache.js +3 -1
- package/src/gpu/kernels/pixel_shuffle.js +1 -1
- package/src/gpu/kernels/probe_shader_f16.wgsl +7 -0
- package/src/gpu/kernels/relu.js +1 -1
- package/src/gpu/kernels/repeat_channels.js +1 -1
- package/src/gpu/kernels/residual.js +2 -2
- package/src/gpu/kernels/rmsnorm-pair.js +1 -1
- package/src/gpu/kernels/rmsnorm-qk.js +1 -1
- package/src/gpu/kernels/rmsnorm-stats.js +14 -113
- package/src/gpu/kernels/rmsnorm.js +1 -1
- package/src/gpu/kernels/rmsnorm_serial.wgsl +19 -0
- package/src/gpu/kernels/rmsnorm_stats.wgsl +59 -0
- package/src/gpu/kernels/rmsnorm_stats_subgroups.wgsl +76 -0
- package/src/gpu/kernels/rope-precompute.js +160 -0
- package/src/gpu/kernels/rope-qk.js +1 -1
- package/src/gpu/kernels/rope.js +2 -2
- package/src/gpu/kernels/rope_precompute.wgsl +74 -0
- package/src/gpu/kernels/sample.js +5 -1
- package/src/gpu/kernels/scale.js +1 -1
- package/src/gpu/kernels/shader-cache.js +8 -4
- package/src/gpu/kernels/shader-source-scope.js +68 -0
- package/src/gpu/kernels/silu.js +2 -1
- package/src/gpu/kernels/soft-embedding.js +3 -1
- package/src/gpu/kernels/softmax.js +4 -2
- package/src/gpu/kernels/split-qkv-rmsnorm-qk.js +1 -1
- package/src/gpu/kernels/split-qkv-rmsnorm-rope-qk.js +1 -1
- package/src/gpu/kernels/split_qg.js +1 -1
- package/src/gpu/kernels/split_qkv.d.ts +2 -0
- package/src/gpu/kernels/split_qkv.js +18 -3
- package/src/gpu/kernels/split_qkv.wgsl +5 -2
- package/src/gpu/kernels/split_qkv_f16.wgsl +5 -2
- package/src/gpu/kernels/submit_probe.wgsl +6 -0
- package/src/gpu/kernels/transpose.js +1 -1
- package/src/gpu/kernels/turboquant-codebook.js +4 -535
- package/src/gpu/kernels/uniform-utils.js +7 -2
- package/src/gpu/kernels/upsample2d.js +1 -1
- package/src/gpu/kernels/utils.js +9 -332
- package/src/gpu/kernels/vision-average-pool.d.ts +13 -0
- package/src/gpu/kernels/vision-average-pool.js +68 -0
- package/src/gpu/kernels/vision-patch-embed.d.ts +17 -0
- package/src/gpu/kernels/vision-patch-embed.js +117 -0
- package/src/gpu/kernels/vision-position-embedding.d.ts +13 -0
- package/src/gpu/kernels/vision-position-embedding.js +79 -0
- package/src/gpu/kernels/vision-rope-2d.d.ts +16 -0
- package/src/gpu/kernels/vision-rope-2d.js +73 -0
- package/src/gpu/kernels/vision-spatial-merge.d.ts +19 -0
- package/src/gpu/kernels/vision-spatial-merge.js +100 -0
- package/src/gpu/kernels/vision_average_pool.wgsl +42 -0
- package/src/gpu/kernels/vision_patch_embed.wgsl +59 -0
- package/src/gpu/kernels/vision_patch_embed_f16w.wgsl +61 -0
- package/src/gpu/kernels/vision_position_embedding.wgsl +33 -0
- package/src/gpu/kernels/vision_position_embedding_f16.wgsl +35 -0
- package/src/gpu/kernels/vision_rope_2d.wgsl +60 -0
- package/src/gpu/kernels/vision_spatial_merge.wgsl +48 -0
- package/src/gpu/readback-buffer.js +20 -0
- package/src/gpu/submit-probe.js +2 -26
- package/src/gpu/weight-buffer.d.ts +1 -0
- package/src/gpu/weight-buffer.js +7 -0
- package/src/index.d.ts +23 -1
- package/src/index.js +8 -1
- package/src/inference/browser-harness/benchmark-execution.js +872 -0
- package/src/inference/browser-harness/benchmark-rerank.js +284 -0
- package/src/inference/browser-harness/decode-diagnostics.js +241 -0
- package/src/inference/browser-harness/diffusion-performance.js +102 -0
- package/src/inference/browser-harness/inference-execution.js +543 -0
- package/src/inference/{browser-harness-model-helpers.js → browser-harness/model-initialization.js} +18 -145
- package/src/inference/browser-harness/model-resolution.js +137 -0
- package/src/inference/browser-harness/report.js +338 -0
- package/src/inference/browser-harness/request.js +263 -0
- package/src/inference/browser-harness/runtime-config.d.ts +25 -0
- package/src/inference/browser-harness/runtime-config.js +255 -0
- package/src/inference/browser-harness/runtime-isolation.d.ts +13 -0
- package/src/inference/browser-harness/runtime-isolation.js +45 -0
- package/src/inference/browser-harness/runtime-report.d.ts +26 -0
- package/src/inference/browser-harness/runtime-report.js +117 -0
- package/src/inference/browser-harness/suite-summary.js +39 -0
- package/src/inference/browser-harness/text-evidence.js +694 -0
- package/src/inference/browser-harness/text-execution.js +757 -0
- package/src/inference/browser-harness/text-input.js +725 -0
- package/src/inference/{browser-harness-suite-helpers.js → browser-harness/timing-diagnostics.js} +1 -379
- package/src/inference/browser-harness-contract-helpers.js +1 -1
- package/src/inference/browser-harness-diffusion-energy-suites.js +8 -3
- package/src/inference/browser-harness-runtime-helpers.d.ts +3 -56
- package/src/inference/browser-harness-runtime-helpers.js +3 -408
- package/src/inference/browser-harness-text-helpers.js +31 -2196
- package/src/inference/browser-harness.d.ts +19 -2
- package/src/inference/browser-harness.js +46 -1873
- package/src/inference/kv-cache/base.js +67 -381
- package/src/inference/kv-cache/basis-decomposed-paged.d.ts +2 -50
- package/src/inference/kv-cache/basis-decomposed-paged.js +1 -276
- package/src/inference/kv-cache/quantized.d.ts +2 -2
- package/src/inference/kv-cache/quantized.js +4 -8
- package/src/inference/kv-cache/update.js +309 -0
- package/src/inference/model-checkpoint-evidence.d.ts +75 -0
- package/src/inference/model-checkpoint-evidence.js +264 -0
- package/src/inference/moe-router.js +36 -216
- package/src/inference/multi-model-network.js +5 -341
- package/src/inference/observation-context.d.ts +22 -0
- package/src/inference/observation-context.js +41 -0
- package/src/inference/pipelines/audio/index.js +1 -20
- package/src/inference/pipelines/audio/mel.js +0 -38
- package/src/inference/pipelines/diffusion/profile-timing.js +10 -0
- package/src/inference/pipelines/diffusion/runtime-resources.js +54 -0
- package/src/inference/pipelines/diffusion/sd3/execution.js +805 -0
- package/src/inference/pipelines/diffusion/sd3/plan.js +125 -0
- package/src/inference/pipelines/diffusion/sd3-transformer.js +44 -895
- package/src/inference/pipelines/diffusion/{vae.js → vae/execution.js} +46 -618
- package/src/inference/pipelines/diffusion/{helpers.js → weight-contract.js} +1 -68
- package/src/inference/pipelines/energy-head/row-head-pipeline.d.ts +1 -103
- package/src/inference/pipelines/factory.js +14 -3
- package/src/inference/pipelines/shader-scoped-pipeline.js +29 -0
- package/src/inference/pipelines/structured/json-grammar-mask.js +0 -6
- package/src/inference/pipelines/text/abort-contract.js +22 -0
- package/src/inference/pipelines/text/attention/attn-config.js +1 -16
- package/src/inference/pipelines/text/attention/executor-immediate.js +42 -0
- package/src/inference/pipelines/text/attention/executor-recorded.js +29 -0
- package/src/inference/pipelines/text/attention/gate-projection.js +30 -0
- package/src/inference/pipelines/text/attention/heterogeneous-contract.js +8 -0
- package/src/inference/pipelines/text/attention/index.js +7 -0
- package/src/inference/pipelines/text/attention/interpreter/immediate.js +211 -0
- package/src/inference/pipelines/text/attention/interpreter/recorded.js +451 -0
- package/src/inference/pipelines/text/attention/interpreter.js +984 -0
- package/src/inference/pipelines/text/attention/plan.d.ts +37 -0
- package/src/inference/pipelines/text/attention/plan.js +345 -0
- package/src/inference/pipelines/text/attention/projection-executor.js +331 -0
- package/src/inference/pipelines/text/attention/projection-plan.js +259 -0
- package/src/inference/pipelines/text/attention/projections.js +20 -537
- package/src/inference/pipelines/text/attention/query-transform.js +11 -0
- package/src/inference/pipelines/text/attention/receipt.js +60 -0
- package/src/inference/pipelines/text/attention/record.js +1 -1277
- package/src/inference/pipelines/text/attention/recorded-health.js +21 -0
- package/src/inference/pipelines/text/attention/rope-observation.js +40 -0
- package/src/inference/pipelines/text/attention/run.js +1 -1385
- package/src/inference/pipelines/text/attention/types.js +3 -31
- package/src/inference/pipelines/text/chat-format.d.ts +3 -0
- package/src/inference/pipelines/text/chat-format.js +42 -0
- package/src/inference/pipelines/text/config/normalization.js +729 -0
- package/src/inference/pipelines/text/config/validation.js +9 -0
- package/src/inference/pipelines/text/config.d.ts +12 -1
- package/src/inference/pipelines/text/config.js +14 -1228
- package/src/inference/pipelines/text/debug-utils/health-metrics.js +57 -0
- package/src/inference/pipelines/text/debug-utils/index.js +3 -8
- package/src/inference/pipelines/text/debug-utils/profiles.js +8 -0
- package/src/inference/pipelines/text/debug-utils/readback-decoding.js +24 -0
- package/src/inference/pipelines/text/debug-utils/tensor.js +1 -1
- package/src/inference/pipelines/text/dtype-contract.js +1 -68
- package/src/inference/pipelines/text/embed.js +178 -241
- package/src/inference/pipelines/text/embedding-contract.d.ts +7 -0
- package/src/inference/pipelines/text/embedding-contract.js +21 -0
- package/src/inference/pipelines/text/embedding-extraction.js +225 -0
- package/src/inference/pipelines/text/embedding-normalization.js +39 -0
- package/src/inference/pipelines/text/execution-runtime-builders.js +0 -32
- package/src/inference/pipelines/text/execution-v1/plan.js +69 -0
- package/src/inference/pipelines/text/execution-v1/validation.js +233 -0
- package/src/inference/pipelines/text/execution-v1.js +7 -283
- package/src/inference/pipelines/text/execution.js +517 -0
- package/src/inference/pipelines/text/ffn/dense-executor.js +718 -0
- package/src/inference/pipelines/text/ffn/dense-plan.js +327 -0
- package/src/inference/pipelines/text/ffn/dense.js +4 -968
- package/src/inference/pipelines/text/ffn/sandwich.js +10 -9
- package/src/inference/pipelines/text/ffn/standard.js +2 -1
- package/src/inference/pipelines/text/generator/attention-lifecycle.js +24 -0
- package/src/inference/pipelines/text/generator/decode-runtime.js +991 -0
- package/src/inference/pipelines/text/generator/decode.js +952 -0
- package/src/inference/pipelines/text/generator/diffusion.js +607 -0
- package/src/inference/pipelines/text/generator/logits-config.js +63 -0
- package/src/inference/pipelines/text/generator/prefill-observation.js +29 -0
- package/src/inference/pipelines/text/generator/prefill-policy.js +56 -0
- package/src/inference/pipelines/text/generator/prefill-runtime.js +896 -0
- package/src/inference/pipelines/text/{generator-prefill-helpers.js → generator/prefix-embedding.js} +16 -121
- package/src/inference/pipelines/text/generator/prompt-input.js +47 -0
- package/src/inference/pipelines/text/generator/recovery.js +310 -0
- package/src/inference/pipelines/text/{generator-helpers.js → generator/session-context.js} +44 -110
- package/src/inference/pipelines/text/generator/text.js +448 -0
- package/src/inference/pipelines/text/generator/training.js +752 -0
- package/src/inference/pipelines/text/generator-decode-policy.js +5 -2
- package/src/inference/pipelines/text/generator-logits-observation.js +45 -0
- package/src/inference/pipelines/text/generator-runtime.js +5 -135
- package/src/inference/pipelines/text/generator-steps.js +37 -962
- package/src/inference/pipelines/text/generator.js +165 -3539
- package/src/inference/pipelines/text/image-transcription.js +304 -0
- package/src/inference/pipelines/text/init-qkv-fusion.js +18 -9
- package/src/inference/pipelines/text/init-rope.js +37 -189
- package/src/inference/pipelines/text/init.d.ts +11 -3
- package/src/inference/pipelines/text/init.js +6 -216
- package/src/inference/pipelines/text/layer-execution-contract.js +97 -0
- package/src/inference/pipelines/text/layer-input-execution.js +435 -0
- package/src/inference/pipelines/text/layer-plan-gpu.js +15 -6
- package/src/inference/pipelines/text/layer-plan.d.ts +21 -5
- package/src/inference/pipelines/text/layer-plan.js +45 -3
- package/src/inference/pipelines/text/layer.js +54 -522
- package/src/inference/pipelines/text/lifecycle.js +839 -0
- package/src/inference/pipelines/text/linear-attention/plan.js +554 -0
- package/src/inference/pipelines/text/linear-attention-ab-fusion.js +2 -2
- package/src/inference/pipelines/text/linear-attention.d.ts +2 -2
- package/src/inference/pipelines/text/linear-attention.js +7 -537
- package/src/inference/pipelines/text/logits/cpu-output.d.ts +1 -0
- package/src/inference/pipelines/text/logits/cpu-output.js +5 -0
- package/src/inference/pipelines/text/logits/cpu.d.ts +1 -87
- package/src/inference/pipelines/text/logits/cpu.js +8 -95
- package/src/inference/pipelines/text/logits/gpu-executor.js +368 -0
- package/src/inference/pipelines/text/logits/gpu.d.ts +27 -0
- package/src/inference/pipelines/text/logits/gpu.js +116 -579
- package/src/inference/pipelines/text/logits/index.d.ts +2 -1
- package/src/inference/pipelines/text/logits/index.js +62 -102
- package/src/inference/pipelines/text/logits/output-transform.js +47 -0
- package/src/inference/pipelines/text/logits/plan.js +184 -0
- package/src/inference/pipelines/text/logits/readback.d.ts +6 -0
- package/src/inference/pipelines/text/logits/readback.js +9 -0
- package/src/inference/pipelines/text/logits/scale-policy.js +20 -0
- package/src/inference/pipelines/text/logits/types.d.ts +1 -0
- package/src/inference/pipelines/text/lora-apply.js +12 -4
- package/src/inference/pipelines/text/lora-types.d.ts +12 -0
- package/src/inference/pipelines/text/modality-token-contract.js +115 -0
- package/src/inference/pipelines/text/model-load.d.ts +0 -1
- package/src/inference/pipelines/text/model-load.js +2 -31
- package/src/inference/pipelines/text/moe/expert-loading.js +9 -0
- package/src/inference/pipelines/text/moe/gpu-executor.js +513 -0
- package/src/inference/pipelines/text/moe/plan.js +47 -0
- package/src/inference/pipelines/text/moe-cpu.js +9 -135
- package/src/inference/pipelines/text/moe-gpu.js +21 -545
- package/src/inference/pipelines/text/normalization-contract.js +17 -0
- package/src/inference/pipelines/text/per-layer/materialize.js +736 -0
- package/src/inference/pipelines/text/per-layer/plan.js +362 -0
- package/src/inference/pipelines/text/per-layer-inputs.js +4 -1037
- package/src/inference/pipelines/text/pipeline-load-timing.js +77 -0
- package/src/inference/pipelines/text/probes.js +30 -0
- package/src/inference/pipelines/text/resolved-runtime-session.d.ts +56 -0
- package/src/inference/pipelines/text/resolved-runtime-session.js +175 -0
- package/src/inference/pipelines/text/state.d.ts +14 -0
- package/src/inference/pipelines/text/state.js +4 -0
- package/src/inference/pipelines/text/tsir-fixture-writer.js +88 -16
- package/src/inference/pipelines/text/types.d.ts +10 -0
- package/src/inference/pipelines/text/weight-loading-config.js +194 -0
- package/src/inference/pipelines/text.d.ts +2 -2
- package/src/inference/pipelines/text.js +48 -1620
- package/src/inference/pipelines/video/frame-sampler.js +0 -13
- package/src/inference/pipelines/video/index.js +0 -21
- package/src/inference/pipelines/vision/attention.js +59 -0
- package/src/inference/pipelines/vision/encoder.js +254 -369
- package/src/inference/pipelines/vision/gemma4.js +204 -318
- package/src/inference/pipelines/vision/glmocr-preprocess.js +273 -0
- package/src/inference/pipelines/vision/glmocr-rope.js +106 -0
- package/src/inference/pipelines/vision/glmocr.js +557 -0
- package/src/inference/pipelines/vision/image-preprocess.js +0 -27
- package/src/inference/pipelines/vision/index.js +8 -27
- package/src/inference/pipelines/vision/ops.js +11 -33
- package/src/inference/pipelines/vision/patch-embed.js +51 -145
- package/src/inference/pipelines/vision/spatial-merge.js +78 -0
- package/src/inference/pipelines/vision/weight-loading.js +79 -0
- package/src/inference/refactor-receipt.d.ts +49 -0
- package/src/inference/refactor-receipt.js +188 -0
- package/src/inference/rerank.js +182 -0
- package/src/inference/resource-scope.d.ts +33 -0
- package/src/inference/resource-scope.js +168 -0
- package/src/inference/test-harness.d.ts +3 -0
- package/src/inference/test-harness.js +1 -0
- package/src/inference/tokenizer.js +1 -1
- package/src/inference/tokenizers/behavior-flags.js +6 -3
- package/src/inference/tokenizers/bundled/decoders.js +133 -0
- package/src/inference/tokenizers/bundled/registry.js +216 -0
- package/src/inference/tokenizers/bundled.js +23 -353
- package/src/loader/doppler-loader.d.ts +4 -0
- package/src/loader/doppler-loader.js +119 -930
- package/src/loader/embedding-limit-preflight.js +11 -4
- package/src/loader/embedding-loader.js +1 -3
- package/src/loader/gpu-tensor-materialization.js +20 -0
- package/src/loader/layer-loader.js +15 -119
- package/src/loader/layer-tensor-aliases.js +99 -0
- package/src/loader/layer-weight-shape.js +29 -0
- package/src/loader/load-timing.js +64 -0
- package/src/loader/loader-types.d.ts +3 -0
- package/src/loader/model-executor.js +819 -0
- package/src/loader/model-load-validation.js +15 -0
- package/src/loader/quantization-constants.d.ts +11 -29
- package/src/loader/quantization-constants.js +1 -2
- package/src/loader/shard-resolver.js +0 -14
- package/src/loader/tensor-load-error.js +14 -0
- package/src/loader/tensor-shard-indices.js +8 -0
- package/src/loader/tensors/bf16-loader.js +125 -0
- package/src/loader/tensors/functional-descriptor-loader.js +3 -375
- package/src/loader/tensors/load-executor.js +577 -0
- package/src/loader/tensors/source-transform/executor.js +123 -0
- package/src/loader/tensors/source-transform/plan.js +107 -0
- package/src/loader/tensors/source-transform.js +15 -264
- package/src/loader/tensors/tensor-loader.d.ts +4 -4
- package/src/loader/tensors/tensor-loader.js +7 -653
- package/src/memory/buffer-pool.js +7 -29
- package/src/rules/capability-transform-resolver.js +80 -0
- package/src/rules/execution-contract.d.ts +73 -0
- package/src/rules/execution-contract.js +429 -0
- package/src/rules/execution-rules-contract-check.js +1 -1
- package/src/rules/generated/rule-bundle.json +4305 -0
- package/src/rules/inference/capability-transforms.rules.json +3 -3
- package/src/rules/layer-pattern-contract-check.js +1 -1
- package/src/rules/rule-registry.js +70 -57
- package/src/storage/artifact-storage-context.js +2 -2
- package/src/storage/backend-selection.js +43 -0
- package/src/storage/distribution-transport.js +5 -0
- package/src/storage/download/integrity.d.ts +33 -0
- package/src/storage/download/integrity.js +130 -0
- package/src/storage/download/resume-inspection.d.ts +22 -0
- package/src/storage/download/resume-inspection.js +134 -0
- package/src/storage/download/retry.js +57 -0
- package/src/storage/download/state.js +131 -0
- package/src/storage/download/transport.js +99 -0
- package/src/storage/downloader.js +74 -453
- package/src/storage/model-cache.d.ts +54 -0
- package/src/storage/model-cache.js +590 -0
- package/src/storage/shard-manager.d.ts +5 -0
- package/src/storage/shard-manager.js +50 -369
- package/src/storage/shards/index.js +60 -0
- package/src/storage/shards/integrity.js +225 -0
- package/src/storage/shards/lifecycle.js +22 -0
- package/src/storage/source-artifact-store.js +14 -4
- package/src/tooling/boundary-evidence.d.ts +51 -0
- package/src/tooling/boundary-evidence.js +408 -0
- package/src/tooling/browser-command-runner.js +17 -10
- package/src/tooling/capsule-v2.d.ts +11 -0
- package/src/tooling/capsule-v2.js +58 -0
- package/src/tooling/capsule.js +20 -0
- package/src/tooling/command-api-constants.js +1 -1
- package/src/tooling/command-api-family-normalizers.js +163 -5
- package/src/tooling/command-api.d.ts +42 -3
- package/src/tooling/command-api.js +7 -2
- package/src/tooling/command-context.d.ts +25 -0
- package/src/tooling/command-context.js +53 -0
- package/src/tooling/command-envelope.js +1 -1
- package/src/tooling/{command-api-helpers.js → command-request-normalization.js} +1 -1
- package/src/tooling/command-runner.html +49 -1
- package/src/tooling/{command-runner-shared.js → command-runtime-execution.js} +22 -5
- package/src/tooling/command-validation.js +1 -24
- package/src/tooling/conversion-run-timing.js +24 -0
- package/src/tooling/demo-receipts.d.ts +56 -0
- package/src/tooling/demo-receipts.js +113 -0
- package/src/tooling/diagnose-runner.js +13 -11
- package/src/tooling/distribution-shard-transport.js +11 -0
- package/src/tooling/execution-cost-ledger.d.ts +95 -0
- package/src/tooling/execution-cost-ledger.js +381 -0
- package/src/tooling/hf-registry/catalog-io.js +63 -0
- package/src/tooling/litert/package.js +863 -0
- package/src/tooling/litert-package-runtime.js +3 -849
- package/src/tooling/model-capsule-forge.js +478 -0
- package/src/tooling/node-browser/execution.d.ts +40 -0
- package/src/tooling/node-browser/execution.js +308 -0
- package/src/tooling/node-browser/static-server.js +188 -0
- package/src/tooling/node-browser/transport.d.ts +80 -0
- package/src/tooling/node-browser/transport.js +622 -0
- package/src/tooling/node-browser-command-runner.d.ts +18 -78
- package/src/tooling/node-browser-command-runner.js +10 -1112
- package/src/tooling/node-capsule-artifact-store.js +44 -0
- package/src/tooling/node-command-execution.js +237 -0
- package/src/tooling/node-command-runner.js +26 -205
- package/src/tooling/node-converter/input.js +521 -0
- package/src/tooling/node-converter/output.js +244 -0
- package/src/tooling/node-converter-chunk-batches.js +21 -0
- package/src/tooling/node-converter.js +7 -720
- package/src/tooling/node-source-runtime.js +16 -276
- package/src/tooling/node-webgpu-provider.v1.json +33 -0
- package/src/tooling/node-webgpu.js +178 -453
- package/src/tooling/opfs-cache.d.ts +15 -48
- package/src/tooling/opfs-cache.js +8 -510
- package/src/tooling/precision-replay-math.d.ts +42 -0
- package/src/tooling/precision-replay-math.js +174 -0
- package/src/tooling/production-release.d.ts +19 -0
- package/src/tooling/production-release.js +647 -0
- package/src/tooling/program-bundle/embedding-reference.js +19 -0
- package/src/tooling/program-bundle/materialize.js +324 -0
- package/src/tooling/program-bundle/rerank-reference.js +22 -0
- package/src/tooling/program-bundle/sequence-reference.js +42 -0
- package/src/tooling/program-bundle/validation.js +31 -0
- package/src/tooling/program-bundle/wgsl-closure.js +254 -0
- package/src/tooling/program-bundle-parity.d.ts +14 -4
- package/src/tooling/program-bundle-parity.js +267 -106
- package/src/tooling/program-bundle.d.ts +8 -52
- package/src/tooling/program-bundle.js +186 -730
- package/src/tooling/provider-v1-contract.d.ts +61 -0
- package/src/tooling/provider-v1-contract.js +192 -0
- package/src/tooling/registered-variant-calibration-job.d.ts +98 -0
- package/src/tooling/registered-variant-calibration-job.js +210 -0
- package/src/tooling/registered-variant-calibration.d.ts +184 -0
- package/src/tooling/registered-variant-calibration.js +336 -0
- package/src/tooling/runtime-input-composition.js +105 -53
- package/src/tooling/runtime-intent.js +41 -0
- package/src/tooling/runtime-optimization/candidates.js +664 -0
- package/src/tooling/runtime-optimization-campaign.js +166 -0
- package/src/tooling/runtime-optimization-index.d.ts +6 -0
- package/src/tooling/runtime-optimization-index.js +87 -0
- package/src/tooling/runtime-optimization.d.ts +91 -6
- package/src/tooling/runtime-optimization.js +290 -414
- package/src/tooling/source-intake.d.ts +49 -0
- package/src/tooling/source-intake.js +431 -0
- package/src/tooling/source-package-profiles.js +1 -1
- package/src/tooling/source-runtime/bundle-materialize.js +298 -0
- package/src/tooling/source-runtime/bundle-plan.js +121 -0
- package/src/tooling/source-runtime/node-loader.js +258 -0
- package/src/tooling/source-runtime-bundle.js +26 -411
- package/src/tooling/source-runtime-converter-config.js +1 -1
- package/src/tooling-experimental-exports.js +0 -5
- package/src/tooling-exports/evidence.d.ts +45 -0
- package/src/tooling-exports/evidence.js +45 -0
- package/src/tooling-exports/runtime.d.ts +7 -0
- package/src/tooling-exports/runtime.js +7 -0
- package/src/tooling-exports/storage.d.ts +5 -0
- package/src/tooling-exports/storage.js +4 -0
- package/src/tooling-exports.d.ts +14 -0
- package/src/tooling-exports.js +8 -3
- package/src/tooling-exports.shared.d.ts +46 -0
- package/src/tooling-exports.shared.js +37 -0
- package/src/training.d.ts +6 -6
- package/src/training.js +4 -4
- package/src/version.js +1 -1
- package/tests/kernels/browser/kernel-suite.js +512 -0
- package/tests/kernels/browser/test-page.js +169 -14
- package/tests/kernels/harness/tolerance.js +1 -0
- package/tests/kernels/reference/index.js +13 -1
- package/tests/kernels/reference/vision.js +160 -0
- package/src/client/runtime/model-session.js +0 -432
- package/src/config/schema/tuner.schema.d.ts +0 -39
- package/src/config/schema/tuner.schema.js +0 -13
- package/src/converter/quantizer.d.ts +0 -121
- package/src/gpu/kernel-tuner/benchmarks.js +0 -902
- package/src/gpu/kernel-tuner/cache.js +0 -145
- package/src/gpu/kernel-tuner/index.js +0 -38
- package/src/gpu/kernel-tuner/tuner.js +0 -247
- package/src/gpu/kernel-tuner.js +0 -12
- package/src/gpu/kernels/backward/utils.d.ts +0 -90
- package/src/gpu/kernels/kernel-tuning.d.ts +0 -42
- package/src/gpu/kernels/kernel-tuning.js +0 -158
- package/src/gpu/kernels/uniform-utils.d.ts +0 -66
- package/src/gpu/kernels/utils.d.ts +0 -149
- package/src/gpu/profiler.js +0 -382
- package/src/inference/pipelines/text/attention/output-gate-fusion.js +0 -37
- package/src/inference/pipelines/text/debug-utils/utils.js +0 -118
- package/src/inference/pipelines/text/logits/utils.d.ts +0 -58
- package/src/inference/pipelines/text/moe-cpu-gptoss.js +0 -115
- package/src/inference/pipelines/text/moe-helpers.js +0 -21
- package/src/rules/converter/execution.rules.json +0 -20
- package/src/rules/converter/tensor-roles.rules.json +0 -13
- package/src/rules/converter/tokenizer.rules.json +0 -12
- package/src/rules/inference/attention.rules.json +0 -69
- package/src/rules/inference/config.rules.json +0 -82
- package/src/rules/inference/dtype.rules.json +0 -113
- package/src/rules/inference/execution.rules.json +0 -195
- package/src/rules/inference/ffn.rules.json +0 -123
- package/src/rules/inference/layer-pattern.rules.json +0 -16
- package/src/rules/inference/layer.rules.json +0 -7
- package/src/rules/inference/moe.rules.json +0 -145
- package/src/rules/kernels/attention.rules.json +0 -68
- package/src/rules/kernels/conv2d.rules.json +0 -6
- package/src/rules/kernels/depthwise-conv2d.rules.json +0 -6
- package/src/rules/kernels/dequant.rules.json +0 -58
- package/src/rules/kernels/energy.rules.json +0 -22
- package/src/rules/kernels/fused-ffn.rules.json +0 -15
- package/src/rules/kernels/fused-matmul-residual.rules.json +0 -6
- package/src/rules/kernels/fused-matmul-rmsnorm.rules.json +0 -8
- package/src/rules/kernels/gather.rules.json +0 -93
- package/src/rules/kernels/gelu.rules.json +0 -11
- package/src/rules/kernels/grouped-pointwise-conv2d.rules.json +0 -6
- package/src/rules/kernels/groupnorm.rules.json +0 -10
- package/src/rules/kernels/kv_quantize.rules.json +0 -9
- package/src/rules/kernels/layernorm.rules.json +0 -6
- package/src/rules/kernels/linear-attention.rules.json +0 -6
- package/src/rules/kernels/lm-head-argmax.rules.json +0 -10
- package/src/rules/kernels/matmul.rules.json +0 -95
- package/src/rules/kernels/modulate.rules.json +0 -6
- package/src/rules/kernels/moe.rules.gptoss.json +0 -105
- package/src/rules/kernels/moe.rules.json +0 -14
- package/src/rules/kernels/moe.rules.mixtral.json +0 -75
- package/src/rules/kernels/pixel_shuffle.rules.json +0 -6
- package/src/rules/kernels/relu.rules.json +0 -6
- package/src/rules/kernels/rep-penalty.rules.json +0 -6
- package/src/rules/kernels/repeat-channels.rules.json +0 -6
- package/src/rules/kernels/residual.rules.json +0 -12
- package/src/rules/kernels/rmsnorm-qk.rules.json +0 -6
- package/src/rules/kernels/rmsnorm.rules.json +0 -46
- package/src/rules/kernels/rope-qk.rules.json +0 -6
- package/src/rules/kernels/rope.rules.json +0 -6
- package/src/rules/kernels/sample.rules.json +0 -6
- package/src/rules/kernels/scale.rules.json +0 -8
- package/src/rules/kernels/silu.rules.json +0 -22
- package/src/rules/kernels/softmax.rules.json +0 -25
- package/src/rules/kernels/split-qg.rules.json +0 -6
- package/src/rules/kernels/split-qkv.rules.json +0 -6
- package/src/rules/kernels/upsample2d.rules.json +0 -6
- package/src/rules/loader/tensor-loader.rules.json +0 -42
- package/src/rules/loader/weights.rules.json +0 -42
- package/src/rules/tooling/command-runtime.rules.json +0 -65
- package/src/tooling/hf-registry-utils.js +0 -395
- /package/src/converter/{tokenizer-utils.js → tokenizer-special-tokens.js} +0 -0
- /package/src/{inference/pipelines/text/bdpa-steamroller.js → experimental/bdpa/steamroller.js} +0 -0
- /package/src/{inference/pipelines → experimental}/diffusion/scheduler.js +0 -0
- /package/src/{inference/pipelines → experimental}/energy/pipeline.d.ts +0 -0
- /package/src/{inference/pipelines → experimental}/energy/quintel.d.ts +0 -0
- /package/src/{inference/pipelines → experimental}/energy/quintel.js +0 -0
- /package/src/{utils → formats}/array-buffer.js +0 -0
- /package/src/{utils → formats}/canonical-hash.js +0 -0
- /package/src/{utils → formats}/clone-json.js +0 -0
- /package/src/{utils → formats}/encode-utf8.js +0 -0
- /package/src/{utils → formats}/load-json.js +0 -0
- /package/src/{utils → formats}/plain-object.js +0 -0
- /package/src/{utils → formats}/sha256.js +0 -0
- /package/src/{utils → formats}/stable-sort-object.js +0 -0
- /package/src/{gpu/kernels → rules}/rule-matcher.js +0 -0
- /package/src/{utils → storage}/hf-resolve-url.js +0 -0
- /package/src/{utils → storage}/runtime-env.js +0 -0
|
@@ -1,1278 +1,2 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
3
|
-
import { isGpuBufferInstance, isWeightBuffer, getWeightDtype } from '../../../../gpu/weight-buffer.js';
|
|
4
|
-
import { getKernelCapabilities } from '../../../../gpu/device.js';
|
|
5
|
-
import { acquireBuffer, readBuffer } from '../../../../memory/buffer-pool.js';
|
|
6
|
-
import {
|
|
7
|
-
recordMatmul,
|
|
8
|
-
recordRMSNorm,
|
|
9
|
-
recordLayerNorm,
|
|
10
|
-
recordBiasAdd,
|
|
11
|
-
recordRoPE,
|
|
12
|
-
canUseRoPEQK,
|
|
13
|
-
recordRoPEQK,
|
|
14
|
-
recordAttention,
|
|
15
|
-
recordAttentionTiered,
|
|
16
|
-
recordAttentionTieredQuant,
|
|
17
|
-
recordAttentionContiguousQuant,
|
|
18
|
-
recordAttentionBDPA,
|
|
19
|
-
recordSiLU,
|
|
20
|
-
recordCastF16ToF32,
|
|
21
|
-
recordCastF32ToF16,
|
|
22
|
-
recordMatmulResidualFused,
|
|
23
|
-
shouldUseFusedMatmulResidual,
|
|
24
|
-
} from '../../../../gpu/kernel-selector.js';
|
|
25
|
-
import { createTensor } from '../../../../gpu/tensor.js';
|
|
26
|
-
import { applyLoRA } from '../lora-apply.js';
|
|
27
|
-
import { getLoRAModule } from '../lora.js';
|
|
28
|
-
import { log, trace, isTraceEnabled } from '../../../../debug/index.js';
|
|
29
|
-
import { selectRuleValue } from '../../../../rules/rule-registry.js';
|
|
30
|
-
import {
|
|
31
|
-
recordAttentionInputs,
|
|
32
|
-
shouldForceF32AttentionProjectionForRoPE,
|
|
33
|
-
resolveAttentionProjectionOutputDtype,
|
|
34
|
-
projectAttentionQKV,
|
|
35
|
-
applyAttentionQKNorm,
|
|
36
|
-
applyAttentionValueNorm,
|
|
37
|
-
hasAttentionProjectionDiagnostics,
|
|
38
|
-
hasAttentionStageDiagnostics,
|
|
39
|
-
resolveAttentionQKNormState,
|
|
40
|
-
} from './projections.js';
|
|
41
|
-
import { prepareAttentionProjectionInput } from './output-projection.js';
|
|
42
|
-
import { runProbes } from '../probes.js';
|
|
43
|
-
import { decodeReadback, getLogitsHealth } from '../debug-utils/index.js';
|
|
44
|
-
|
|
45
|
-
import { releaseOrTrack, shouldDebugLayer } from './types.js';
|
|
46
|
-
import {
|
|
47
|
-
getKernelPathMatmulPrecision,
|
|
48
|
-
getKernelPathMatmulVariant,
|
|
49
|
-
} from '../../../../config/kernel-path-loader.js';
|
|
50
|
-
import {
|
|
51
|
-
resolveKVCacheState,
|
|
52
|
-
createDiffusionGemmaDecoderKVState,
|
|
53
|
-
buildAttentionDispatchParams,
|
|
54
|
-
buildAttentionInputsData,
|
|
55
|
-
} from './dispatch-params.js';
|
|
56
|
-
import {
|
|
57
|
-
buildTieredQuantAttentionOptions,
|
|
58
|
-
buildContiguousQuantAttentionOptions,
|
|
59
|
-
} from './quant-options.js';
|
|
60
|
-
import { assertImplicitDtypeTransitionAllowed } from '../dtype-contract.js';
|
|
61
|
-
import { getRuntimeConfig } from '../../../../config/runtime.js';
|
|
62
|
-
import {
|
|
63
|
-
resolveAttentionPrecisionContract,
|
|
64
|
-
isAttentionKvDtypeExplicit,
|
|
65
|
-
} from './precision-contract.js';
|
|
66
|
-
import { canUseRmsNormWideTileProjectionFusion } from './rmsnorm-fusion-gate.js';
|
|
67
|
-
import { canUseAttentionOutputGateFusion } from './output-gate-fusion.js';
|
|
68
|
-
import { getVectorTensor } from '../weights.js';
|
|
69
|
-
|
|
70
|
-
const ATTENTION_DTYPE_LOGGED = new Set();
|
|
71
|
-
|
|
72
|
-
function isWideTileQ4KPhaseEnabled(session, phase) {
|
|
73
|
-
return phase === 'decode'
|
|
74
|
-
? session?.useWideTileQ4KDecode === true
|
|
75
|
-
: session?.useWideTileQ4KPrefill === true;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function canUseQ4KWideTileResidualFusion(options = {}) {
|
|
79
|
-
return options.hasResidual === true
|
|
80
|
-
&& options.oProjDtype === 'q4k'
|
|
81
|
-
&& options.inputDtype === 'f32'
|
|
82
|
-
&& options.outputDtype === 'f32'
|
|
83
|
-
&& options.residualMatches === true
|
|
84
|
-
&& options.hasLoRA !== true
|
|
85
|
-
&& getKernelCapabilities().hasF16 === true
|
|
86
|
-
&& getKernelCapabilities().hasSubgroups === true
|
|
87
|
-
&& options.session?.useWideTileResidualFusion === true
|
|
88
|
-
&& options.session?.retainQ4KMaterialization === true
|
|
89
|
-
&& isWideTileQ4KPhaseEnabled(options.session, options.phase);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function shouldTraceRecordedHealth(layerIdx, debugFlags) {
|
|
93
|
-
const debugLayers = debugFlags?.debugLayers;
|
|
94
|
-
return isTraceEnabled('logits')
|
|
95
|
-
&& Array.isArray(debugLayers)
|
|
96
|
-
&& shouldDebugLayer(layerIdx, debugLayers);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function enqueueRecordedTensorHealth(recorder, label, tensor, dtype, elementCount) {
|
|
100
|
-
if (!recorder || !tensor?.buffer || !Number.isFinite(elementCount) || elementCount <= 0) {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
const bytesPerElement = selectRuleValue('shared', 'dtype', 'bytesFromDtype', { dtype });
|
|
104
|
-
recorder.enqueueCompletionTask(async () => {
|
|
105
|
-
const data = await readBuffer(tensor.buffer, elementCount * bytesPerElement);
|
|
106
|
-
trace.logits(label, getLogitsHealth(decodeReadback(data, dtype)));
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function resolveDirectF16KVCacheWrite(options) {
|
|
111
|
-
const {
|
|
112
|
-
state,
|
|
113
|
-
layerIdx,
|
|
114
|
-
currentSeqLen,
|
|
115
|
-
numTokens,
|
|
116
|
-
numKVHeads,
|
|
117
|
-
headDim,
|
|
118
|
-
reusesSharedKV,
|
|
119
|
-
storeSharedKV,
|
|
120
|
-
diffusionGemmaDecoder,
|
|
121
|
-
valueNorm,
|
|
122
|
-
diagnosticsEnabled,
|
|
123
|
-
disableRoPE,
|
|
124
|
-
} = options;
|
|
125
|
-
if (
|
|
126
|
-
numTokens !== 1
|
|
127
|
-
|| reusesSharedKV === true
|
|
128
|
-
|| storeSharedKV === true
|
|
129
|
-
|| diffusionGemmaDecoder === true
|
|
130
|
-
|| valueNorm === true
|
|
131
|
-
|| diagnosticsEnabled === true
|
|
132
|
-
|| disableRoPE === true
|
|
133
|
-
) {
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
const cache = state?.kvCache;
|
|
137
|
-
if (
|
|
138
|
-
!cache?.hasGPUCache?.()
|
|
139
|
-
|| cache.kvDtype !== 'f16'
|
|
140
|
-
|| cache.layout !== 'contiguous'
|
|
141
|
-
|| cache.windowSize != null
|
|
142
|
-
|| cache.layerSpecs != null
|
|
143
|
-
|| typeof cache.recordF16UpdateAlreadyWrittenFromGPU !== 'function'
|
|
144
|
-
) {
|
|
145
|
-
return null;
|
|
146
|
-
}
|
|
147
|
-
const gpuBuffers = cache.getGPUBuffers(layerIdx);
|
|
148
|
-
const layout = gpuBuffers?.layout ?? cache.layout;
|
|
149
|
-
if (layout !== 'contiguous' || !gpuBuffers?.keysGPU || !gpuBuffers?.valuesGPU) {
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
return {
|
|
153
|
-
keysBuffer: gpuBuffers.keysGPU,
|
|
154
|
-
valuesBuffer: gpuBuffers.valuesGPU,
|
|
155
|
-
dstOffset: currentSeqLen * numKVHeads * headDim,
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function assertAttentionDtypeTransitionAllowed(state, fromDtype, toDtype, detail, transitionDeclaredBy = null) {
|
|
160
|
-
assertImplicitDtypeTransitionAllowed({
|
|
161
|
-
executionPolicies: state?.executionPolicies ?? null,
|
|
162
|
-
fromDtype,
|
|
163
|
-
toDtype,
|
|
164
|
-
op: 'attention',
|
|
165
|
-
detail,
|
|
166
|
-
transitionDeclaredBy,
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
export async function recordLayerAttentionGPU(
|
|
172
|
-
recorder,
|
|
173
|
-
input,
|
|
174
|
-
layerWeights,
|
|
175
|
-
config,
|
|
176
|
-
state,
|
|
177
|
-
debug = false,
|
|
178
|
-
debugFlags = {},
|
|
179
|
-
getWeightBuffer,
|
|
180
|
-
getNormWeightBuffer,
|
|
181
|
-
debugCheckBuffer,
|
|
182
|
-
lora
|
|
183
|
-
) {
|
|
184
|
-
const {
|
|
185
|
-
layerIdx,
|
|
186
|
-
numTokens,
|
|
187
|
-
isPrefill,
|
|
188
|
-
numHeads,
|
|
189
|
-
numKVHeads,
|
|
190
|
-
headDim,
|
|
191
|
-
hiddenSize,
|
|
192
|
-
rmsNormEps,
|
|
193
|
-
currentSeqLen,
|
|
194
|
-
slidingWindow,
|
|
195
|
-
layerType,
|
|
196
|
-
residualTensor,
|
|
197
|
-
attnSoftcap,
|
|
198
|
-
queryPreAttnScalar,
|
|
199
|
-
skipInputNorm = false,
|
|
200
|
-
tokenIds = null,
|
|
201
|
-
kernelPath = null,
|
|
202
|
-
disableRoPE = false,
|
|
203
|
-
multimodalBidirectionalSpan = null,
|
|
204
|
-
sharedKVSourceLayerIdx = null,
|
|
205
|
-
storeSharedKV = false,
|
|
206
|
-
diffusionGemmaDecoder = false,
|
|
207
|
-
precomputedInputNorm = null,
|
|
208
|
-
} = config;
|
|
209
|
-
|
|
210
|
-
const phase = isPrefill ? 'prefill' : 'decode';
|
|
211
|
-
const attentionPrecisionContract = resolveAttentionPrecisionContract(config, state);
|
|
212
|
-
const attentionActivationDtype = selectRuleValue('shared', 'dtype', 'f16OrF32FromDtype', {
|
|
213
|
-
dtype: attentionPrecisionContract.resolvedActivationDtype ?? config.activationDtype,
|
|
214
|
-
});
|
|
215
|
-
const oProjPrecision = getKernelPathMatmulPrecision('o_proj', phase, layerIdx, kernelPath);
|
|
216
|
-
const oProjInputDtype = selectRuleValue('shared', 'dtype', 'f16OrF32FromDtype', {
|
|
217
|
-
dtype: oProjPrecision?.inputDtype ?? attentionActivationDtype,
|
|
218
|
-
});
|
|
219
|
-
const oProjOutputDtype = selectRuleValue('shared', 'dtype', 'f16OrF32FromDtype', {
|
|
220
|
-
dtype: oProjPrecision?.outputDtype
|
|
221
|
-
?? attentionPrecisionContract.resolvedOutputDtype
|
|
222
|
-
?? config.activationDtype,
|
|
223
|
-
});
|
|
224
|
-
const wantsF16Output = oProjOutputDtype === 'f16';
|
|
225
|
-
const useF16Activations = attentionActivationDtype === 'f16';
|
|
226
|
-
const kvCacheFallback = selectRuleValue('inference', 'dtype', 'f16OrF32', { useF16: useF16Activations });
|
|
227
|
-
const kvCacheDtype = attentionPrecisionContract.resolvedKvCacheDtype ?? state.kvCache?.kvDtype ?? kvCacheFallback;
|
|
228
|
-
const allowF16Attention = useF16Activations && kvCacheDtype === 'f16';
|
|
229
|
-
const attentionInputTransitionDeclaredBy = attentionPrecisionContract.explicitInputDtype
|
|
230
|
-
? 'step_precision'
|
|
231
|
-
: null;
|
|
232
|
-
let attentionInput = input;
|
|
233
|
-
let attentionInputTemp = false;
|
|
234
|
-
let normed = attentionInput;
|
|
235
|
-
let qTensor = null;
|
|
236
|
-
let qGateTensor = null;
|
|
237
|
-
let kTensor = null;
|
|
238
|
-
let vTensor = null;
|
|
239
|
-
let attnOutput = null;
|
|
240
|
-
let attnForProjection = null;
|
|
241
|
-
let output = null;
|
|
242
|
-
let finalOutput = null;
|
|
243
|
-
let oProjInputTemp = null;
|
|
244
|
-
let retainSharedKvBuffers = false;
|
|
245
|
-
let valueAliasesKey = false;
|
|
246
|
-
let decoderKVState = null;
|
|
247
|
-
let decoderKTemp = null;
|
|
248
|
-
let decoderVTemp = null;
|
|
249
|
-
if (!allowF16Attention && input.dtype !== attentionActivationDtype) {
|
|
250
|
-
assertAttentionDtypeTransitionAllowed(
|
|
251
|
-
state,
|
|
252
|
-
input.dtype,
|
|
253
|
-
attentionActivationDtype,
|
|
254
|
-
'The attention kernel selection would widen the input implicitly.',
|
|
255
|
-
attentionInputTransitionDeclaredBy
|
|
256
|
-
);
|
|
257
|
-
attentionInput = attentionActivationDtype === 'f16'
|
|
258
|
-
? await recordCastF32ToF16(recorder, input)
|
|
259
|
-
: await recordCastF16ToF32(recorder, input);
|
|
260
|
-
attentionInputTemp = true;
|
|
261
|
-
normed = attentionInput;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (!layerWeights) {
|
|
265
|
-
const bytesPerElement = wantsF16Output ? 2 : 4;
|
|
266
|
-
const outputBuf = acquireBuffer(numTokens * hiddenSize * bytesPerElement, undefined, 'attn_output');
|
|
267
|
-
const output = createTensor(outputBuf, oProjOutputDtype, [numTokens, hiddenSize], 'attn_output');
|
|
268
|
-
return { output, residualFused: false };
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const qSize = numTokens * numHeads * headDim;
|
|
272
|
-
const kvSize = numTokens * numKVHeads * headDim;
|
|
273
|
-
|
|
274
|
-
// 1. Input norm
|
|
275
|
-
// Opt-in fusion: when useFusedRmsnormWideTile is set, defer the standalone
|
|
276
|
-
// rmsnorm into each q/k/v_proj matmul (fused kernel runs norm internally).
|
|
277
|
-
let fusedNormWeightRec = null;
|
|
278
|
-
let fusedNormEpsRec = null;
|
|
279
|
-
let fusedNormOffsetRec = false;
|
|
280
|
-
let fusedNormOwnedRec = false;
|
|
281
|
-
// state.runtimeConfig is a stale snapshot whose inference.session is an empty
|
|
282
|
-
// object; the live module-level runtime config carries the merged
|
|
283
|
-
// profile/override session. Read session-level flags from getRuntimeConfig().
|
|
284
|
-
const rmsNormFusionFlagRec = getRuntimeConfig()?.inference?.session?.useFusedRmsnormWideTile === true;
|
|
285
|
-
const canSelectFusedRmsNormProjectionRec = canUseRmsNormWideTileProjectionFusion(
|
|
286
|
-
layerWeights,
|
|
287
|
-
sharedKVSourceLayerIdx != null
|
|
288
|
-
);
|
|
289
|
-
const canFuseInputNormProjRec = rmsNormFusionFlagRec
|
|
290
|
-
&& config.normalizationType === 'rmsnorm'
|
|
291
|
-
&& canSelectFusedRmsNormProjectionRec
|
|
292
|
-
&& !skipInputNorm
|
|
293
|
-
&& layerWeights.inputNorm
|
|
294
|
-
&& getNormWeightBuffer
|
|
295
|
-
&& isPrefill
|
|
296
|
-
&& numTokens > 1
|
|
297
|
-
&& attentionInput.dtype === 'f32';
|
|
298
|
-
|
|
299
|
-
try {
|
|
300
|
-
if (precomputedInputNorm) {
|
|
301
|
-
if (
|
|
302
|
-
isPrefill
|
|
303
|
-
|| numTokens !== 1
|
|
304
|
-
|| skipInputNorm
|
|
305
|
-
|| !layerWeights.inputNorm
|
|
306
|
-
|| !getNormWeightBuffer
|
|
307
|
-
|| precomputedInputNorm.dtype !== attentionInput.dtype
|
|
308
|
-
|| precomputedInputNorm.dtype !== 'f32'
|
|
309
|
-
) {
|
|
310
|
-
throw new Error(`Layer ${layerIdx} received an incompatible precomputed input norm tensor.`);
|
|
311
|
-
}
|
|
312
|
-
normed = precomputedInputNorm;
|
|
313
|
-
await runProbes('post_input_norm', normed.buffer, {
|
|
314
|
-
layerIdx, numTokens, hiddenSize,
|
|
315
|
-
probes: state.debugProbes, recorder,
|
|
316
|
-
operatorDiagnostics: state.operatorDiagnostics, dtype: normed.dtype,
|
|
317
|
-
});
|
|
318
|
-
} else if (canFuseInputNormProjRec) {
|
|
319
|
-
fusedNormWeightRec = getNormWeightBuffer(layerWeights.inputNorm, 'input_norm');
|
|
320
|
-
fusedNormEpsRec = rmsNormEps;
|
|
321
|
-
fusedNormOffsetRec = config.rmsNormWeightOffset === true;
|
|
322
|
-
fusedNormOwnedRec = !isGpuBufferInstance(layerWeights.inputNorm) && !isWeightBuffer(layerWeights.inputNorm);
|
|
323
|
-
// Keep normed = attentionInput (raw). Each q/k/v_proj matmul runs norm internally.
|
|
324
|
-
} else if (!skipInputNorm && layerWeights.inputNorm && getNormWeightBuffer) {
|
|
325
|
-
const normWeightBuf = getNormWeightBuffer(layerWeights.inputNorm, 'input_norm');
|
|
326
|
-
let normBiasTensor = null;
|
|
327
|
-
let normBiasOwned = false;
|
|
328
|
-
if (config.normalizationType === 'layernorm') {
|
|
329
|
-
if (!layerWeights.inputNormBias) {
|
|
330
|
-
throw new Error(`Layer ${layerIdx} requires input_layernorm.bias for LayerNorm execution.`);
|
|
331
|
-
}
|
|
332
|
-
({ tensor: normBiasTensor, owned: normBiasOwned } = getVectorTensor(
|
|
333
|
-
layerWeights.inputNormBias,
|
|
334
|
-
`L${layerIdx}.input_norm_bias`,
|
|
335
|
-
hiddenSize,
|
|
336
|
-
{},
|
|
337
|
-
debugFlags
|
|
338
|
-
));
|
|
339
|
-
normed = await recordLayerNorm(
|
|
340
|
-
recorder,
|
|
341
|
-
attentionInput,
|
|
342
|
-
normWeightBuf,
|
|
343
|
-
normBiasTensor.buffer,
|
|
344
|
-
rmsNormEps,
|
|
345
|
-
{ batchSize: numTokens, hiddenSize, label: 'input_norm' }
|
|
346
|
-
);
|
|
347
|
-
} else {
|
|
348
|
-
normed = await recordRMSNorm(recorder, attentionInput, normWeightBuf, rmsNormEps, {
|
|
349
|
-
batchSize: numTokens,
|
|
350
|
-
hiddenSize,
|
|
351
|
-
rmsNormWeightOffset: config.rmsNormWeightOffset,
|
|
352
|
-
label: 'input_norm',
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
if (normBiasOwned && normBiasTensor) releaseOrTrack(recorder, normBiasTensor.buffer);
|
|
356
|
-
if (!isGpuBufferInstance(layerWeights.inputNorm) && !isWeightBuffer(layerWeights.inputNorm)) releaseOrTrack(recorder, normWeightBuf);
|
|
357
|
-
await runProbes('post_input_norm', normed.buffer, {
|
|
358
|
-
layerIdx, numTokens, hiddenSize,
|
|
359
|
-
probes: state.debugProbes, recorder,
|
|
360
|
-
operatorDiagnostics: state.operatorDiagnostics, dtype: normed.dtype,
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
const debugLayers = debugFlags.debugLayers;
|
|
365
|
-
const shouldLogLayer = debugLayers === null ? layerIdx === 0 : shouldDebugLayer(layerIdx, debugLayers);
|
|
366
|
-
if (shouldLogLayer) {
|
|
367
|
-
const phase = selectRuleValue('kernels', 'attention', 'phase', { isDecode: !isPrefill });
|
|
368
|
-
const logKey = `L${layerIdx}_${phase}_dtypes`;
|
|
369
|
-
if (!ATTENTION_DTYPE_LOGGED.has(logKey)) {
|
|
370
|
-
ATTENTION_DTYPE_LOGGED.add(logKey);
|
|
371
|
-
trace.attn(layerIdx, `dtypes: activation=${attentionActivationDtype}, input=${input.dtype}, normed=${normed.dtype}`);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
// 2. Q/K/V projections
|
|
376
|
-
const qProjVariant = getKernelPathMatmulVariant('q_proj', phase, layerIdx, kernelPath);
|
|
377
|
-
const kernelPathIsF16 = qProjVariant != null && qProjVariant.includes('f16') && !qProjVariant.includes('f32');
|
|
378
|
-
const matmulOutputDtype = resolveAttentionProjectionOutputDtype(attentionActivationDtype, {
|
|
379
|
-
forceF32: shouldForceF32AttentionProjectionForRoPE({
|
|
380
|
-
attentionInputDtype: attentionActivationDtype,
|
|
381
|
-
headDim,
|
|
382
|
-
rotaryDim: config.ropeRotaryDim,
|
|
383
|
-
interleaved: config.ropeInterleaved,
|
|
384
|
-
kernelPathIsF16,
|
|
385
|
-
}),
|
|
386
|
-
});
|
|
387
|
-
let usedFusedQKV = false;
|
|
388
|
-
const sharedKVEntry = sharedKVSourceLayerIdx == null
|
|
389
|
-
? null
|
|
390
|
-
: (state.sharedAttentionState?.get(sharedKVSourceLayerIdx) ?? null);
|
|
391
|
-
if (sharedKVSourceLayerIdx != null && !sharedKVEntry) {
|
|
392
|
-
throw new Error(
|
|
393
|
-
`Layer ${layerIdx} requires shared K/V from layer ${sharedKVSourceLayerIdx}, ` +
|
|
394
|
-
'but no shared K/V state was stored for that source layer.'
|
|
395
|
-
);
|
|
396
|
-
}
|
|
397
|
-
if (sharedKVEntry && (
|
|
398
|
-
sharedKVEntry.headDim !== headDim
|
|
399
|
-
|| sharedKVEntry.numKVHeads !== numKVHeads
|
|
400
|
-
)) {
|
|
401
|
-
throw new Error(
|
|
402
|
-
`Layer ${layerIdx} shared K/V geometry mismatch. ` +
|
|
403
|
-
`Expected numKVHeads=${numKVHeads}, headDim=${headDim}; ` +
|
|
404
|
-
`got numKVHeads=${sharedKVEntry.numKVHeads}, headDim=${sharedKVEntry.headDim}.`
|
|
405
|
-
);
|
|
406
|
-
}
|
|
407
|
-
const reusesSharedKV = sharedKVEntry != null;
|
|
408
|
-
retainSharedKvBuffers = reusesSharedKV || storeSharedKV;
|
|
409
|
-
let qkNormApplied = false;
|
|
410
|
-
let ropeApplied = false;
|
|
411
|
-
const qkNormState = resolveAttentionQKNormState({ config, layerWeights, layerIdx, reusesSharedKV });
|
|
412
|
-
const qkNormProjectionDiagnostics = hasAttentionProjectionDiagnostics(state)
|
|
413
|
-
|| shouldTraceRecordedHealth(layerIdx, debugFlags);
|
|
414
|
-
const qkNormRoPEDiagnostics = hasAttentionStageDiagnostics(
|
|
415
|
-
state,
|
|
416
|
-
['q_proj', 'k_proj', 'v_proj', 'q_norm', 'k_norm']
|
|
417
|
-
)
|
|
418
|
-
|| shouldTraceRecordedHealth(layerIdx, debugFlags);
|
|
419
|
-
const runtimeSession = getRuntimeConfig()?.inference?.session;
|
|
420
|
-
const qkNormFusionFlag = runtimeSession?.useFusedQKVSplitQKNorm === true;
|
|
421
|
-
const qkNormRoPEFusionFlag = runtimeSession?.useFusedQKVSplitQKNormRoPE === true;
|
|
422
|
-
const skipKVCacheWrites = state.skipKVCacheWrites === true;
|
|
423
|
-
const directF16KVCacheWrite = skipKVCacheWrites
|
|
424
|
-
? null
|
|
425
|
-
: resolveDirectF16KVCacheWrite({
|
|
426
|
-
state,
|
|
427
|
-
layerIdx,
|
|
428
|
-
currentSeqLen,
|
|
429
|
-
numTokens,
|
|
430
|
-
numKVHeads,
|
|
431
|
-
headDim,
|
|
432
|
-
reusesSharedKV,
|
|
433
|
-
storeSharedKV,
|
|
434
|
-
diffusionGemmaDecoder,
|
|
435
|
-
valueNorm: config.valueNorm === true,
|
|
436
|
-
diagnosticsEnabled: qkNormRoPEDiagnostics,
|
|
437
|
-
disableRoPE,
|
|
438
|
-
});
|
|
439
|
-
let kvCacheWriteFused = false;
|
|
440
|
-
({ qTensor, qGateTensor, kTensor, vTensor, usedFusedQKV, valueAliasesKey, qkNormApplied, ropeApplied, kvCacheWriteFused } = await projectAttentionQKV({
|
|
441
|
-
recorder,
|
|
442
|
-
normed,
|
|
443
|
-
layerWeights,
|
|
444
|
-
numTokens,
|
|
445
|
-
numHeads,
|
|
446
|
-
numKVHeads,
|
|
447
|
-
headDim,
|
|
448
|
-
hiddenSize,
|
|
449
|
-
layerIdx,
|
|
450
|
-
kernelPath,
|
|
451
|
-
matmulOutputDtype,
|
|
452
|
-
getWeightBuffer,
|
|
453
|
-
lora,
|
|
454
|
-
matmulDebug: state.runtimeConfig?.shared?.debug?.matmul ?? null,
|
|
455
|
-
attentionOutputGate: config.attentionOutputGate === true,
|
|
456
|
-
sharedKTensor: sharedKVEntry?.kTensor ?? null,
|
|
457
|
-
sharedVTensor: sharedKVEntry?.vTensor ?? null,
|
|
458
|
-
executionPolicies: state.executionPolicies ?? null,
|
|
459
|
-
releaseTemporary: (buffer) => releaseOrTrack(recorder, buffer),
|
|
460
|
-
onFusedQKV: layerIdx === 0 && isPrefill
|
|
461
|
-
? ({ qSize: qSizeFused, kSize: kSizeFused, vSize: vSizeFused, totalSize }) => {
|
|
462
|
-
trace.attn(layerIdx, `Using fused QKV path: ${qSizeFused}+${kSizeFused}+${vSizeFused}=${totalSize}`);
|
|
463
|
-
}
|
|
464
|
-
: null,
|
|
465
|
-
fusedNormWeight: fusedNormWeightRec,
|
|
466
|
-
fusedNormEps: fusedNormEpsRec,
|
|
467
|
-
fusedNormOffset: fusedNormOffsetRec,
|
|
468
|
-
qkNormFusion: {
|
|
469
|
-
enabled: qkNormFusionFlag
|
|
470
|
-
&& qkNormState.wantsQKNorm
|
|
471
|
-
&& config.queryKeyNormType === 'rmsnorm'
|
|
472
|
-
&& config.queryKeyNormAxis === 'head',
|
|
473
|
-
getNormWeightBuffer,
|
|
474
|
-
rmsNormEps,
|
|
475
|
-
rmsNormWeightOffset: config.rmsNormWeightOffset,
|
|
476
|
-
skipKNorm: qkNormState.skipKNorm,
|
|
477
|
-
allowUnitQKNorm: qkNormState.allowUnitQKNorm,
|
|
478
|
-
projectionDiagnosticsEnabled: qkNormProjectionDiagnostics,
|
|
479
|
-
},
|
|
480
|
-
qkNormRoPEFusion: {
|
|
481
|
-
enabled: qkNormRoPEFusionFlag
|
|
482
|
-
&& qkNormState.wantsQKNorm
|
|
483
|
-
&& config.queryKeyNormType === 'rmsnorm'
|
|
484
|
-
&& config.queryKeyNormAxis === 'head'
|
|
485
|
-
&& !disableRoPE
|
|
486
|
-
&& !!state.ropeFreqsCos
|
|
487
|
-
&& !!state.ropeFreqsSin,
|
|
488
|
-
getNormWeightBuffer,
|
|
489
|
-
rmsNormEps,
|
|
490
|
-
rmsNormWeightOffset: config.rmsNormWeightOffset,
|
|
491
|
-
skipKNorm: qkNormState.skipKNorm,
|
|
492
|
-
allowUnitQKNorm: qkNormState.allowUnitQKNorm,
|
|
493
|
-
projectionDiagnosticsEnabled: qkNormRoPEDiagnostics,
|
|
494
|
-
freqsCos: state.ropeFreqsCos,
|
|
495
|
-
freqsSin: state.ropeFreqsSin,
|
|
496
|
-
startPos: currentSeqLen,
|
|
497
|
-
rotaryDim: config.ropeRotaryDim,
|
|
498
|
-
pairSpanDim: config.ropeFrequencyBaseDim ?? config.ropeRotaryDim,
|
|
499
|
-
interleaved: config.ropeInterleaved,
|
|
500
|
-
reusesSharedKV,
|
|
501
|
-
f16KVCacheWrite: directF16KVCacheWrite,
|
|
502
|
-
},
|
|
503
|
-
}));
|
|
504
|
-
if (!kvCacheWriteFused && (!kTensor || !vTensor)) {
|
|
505
|
-
throw new Error('Recorded attention projection returned missing K/V tensors without a fused KV cache write.');
|
|
506
|
-
}
|
|
507
|
-
// Deferred release of the norm weight buffer for fused path.
|
|
508
|
-
if (fusedNormWeightRec && fusedNormOwnedRec) {
|
|
509
|
-
releaseOrTrack(recorder, fusedNormWeightRec);
|
|
510
|
-
}
|
|
511
|
-
if (qTensor) {
|
|
512
|
-
await runProbes('q_proj', qTensor.buffer, {
|
|
513
|
-
layerIdx, numTokens, hiddenSize: numHeads * headDim,
|
|
514
|
-
probes: state.debugProbes, recorder,
|
|
515
|
-
operatorDiagnostics: state.operatorDiagnostics, dtype: qTensor.dtype,
|
|
516
|
-
});
|
|
517
|
-
}
|
|
518
|
-
if (kTensor) {
|
|
519
|
-
await runProbes('k_proj', kTensor.buffer, {
|
|
520
|
-
layerIdx, numTokens, hiddenSize: numKVHeads * headDim,
|
|
521
|
-
probes: state.debugProbes, recorder,
|
|
522
|
-
operatorDiagnostics: state.operatorDiagnostics, dtype: kTensor.dtype,
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
if (vTensor) {
|
|
526
|
-
await runProbes('v_proj', vTensor.buffer, {
|
|
527
|
-
layerIdx, numTokens, hiddenSize: numKVHeads * headDim,
|
|
528
|
-
probes: state.debugProbes, recorder,
|
|
529
|
-
operatorDiagnostics: state.operatorDiagnostics, dtype: vTensor.dtype,
|
|
530
|
-
});
|
|
531
|
-
}
|
|
532
|
-
if (qTensor && shouldTraceRecordedHealth(layerIdx, debugFlags)) {
|
|
533
|
-
enqueueRecordedTensorHealth(
|
|
534
|
-
recorder,
|
|
535
|
-
`L${layerIdx}.q_proj_HEALTH`,
|
|
536
|
-
qTensor,
|
|
537
|
-
qTensor.dtype,
|
|
538
|
-
numTokens * numHeads * headDim
|
|
539
|
-
);
|
|
540
|
-
enqueueRecordedTensorHealth(
|
|
541
|
-
recorder,
|
|
542
|
-
`L${layerIdx}.k_proj_HEALTH`,
|
|
543
|
-
kTensor,
|
|
544
|
-
kTensor?.dtype ?? null,
|
|
545
|
-
numTokens * numKVHeads * headDim
|
|
546
|
-
);
|
|
547
|
-
enqueueRecordedTensorHealth(
|
|
548
|
-
recorder,
|
|
549
|
-
`L${layerIdx}.v_proj_HEALTH`,
|
|
550
|
-
vTensor,
|
|
551
|
-
vTensor?.dtype ?? null,
|
|
552
|
-
numTokens * numKVHeads * headDim
|
|
553
|
-
);
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
// Optional Q/K normalization. The manifest owns both its numerical family
|
|
557
|
-
// and whether it applies per head or across the complete projection.
|
|
558
|
-
if (qkNormState.wantsQKNorm && !qkNormApplied) {
|
|
559
|
-
({ qTensor, kTensor } = await applyAttentionQKNorm({
|
|
560
|
-
recorder,
|
|
561
|
-
qTensor,
|
|
562
|
-
kTensor,
|
|
563
|
-
layerWeights,
|
|
564
|
-
getNormWeightBuffer,
|
|
565
|
-
rmsNormEps,
|
|
566
|
-
numTokens,
|
|
567
|
-
numHeads,
|
|
568
|
-
numKVHeads,
|
|
569
|
-
headDim,
|
|
570
|
-
rmsNormWeightOffset: config.rmsNormWeightOffset,
|
|
571
|
-
normalizationType: config.queryKeyNormType,
|
|
572
|
-
normalizationAxis: config.queryKeyNormAxis,
|
|
573
|
-
releaseTemporary: (buffer) => releaseOrTrack(recorder, buffer),
|
|
574
|
-
skipKNorm: qkNormState.skipKNorm,
|
|
575
|
-
retainKInput: valueAliasesKey,
|
|
576
|
-
allowUnitQKNorm: qkNormState.allowUnitQKNorm,
|
|
577
|
-
}));
|
|
578
|
-
}
|
|
579
|
-
if (qkNormState.wantsQKNorm) {
|
|
580
|
-
await runProbes('q_norm', qTensor.buffer, {
|
|
581
|
-
layerIdx, numTokens, hiddenSize: numHeads * headDim,
|
|
582
|
-
probes: state.debugProbes, recorder,
|
|
583
|
-
operatorDiagnostics: state.operatorDiagnostics, dtype: qTensor.dtype,
|
|
584
|
-
});
|
|
585
|
-
if (kTensor) {
|
|
586
|
-
await runProbes('k_norm', kTensor.buffer, {
|
|
587
|
-
layerIdx, numTokens, hiddenSize: numKVHeads * headDim,
|
|
588
|
-
probes: state.debugProbes, recorder,
|
|
589
|
-
operatorDiagnostics: state.operatorDiagnostics, dtype: kTensor.dtype,
|
|
590
|
-
});
|
|
591
|
-
}
|
|
592
|
-
if (shouldTraceRecordedHealth(layerIdx, debugFlags)) {
|
|
593
|
-
enqueueRecordedTensorHealth(
|
|
594
|
-
recorder,
|
|
595
|
-
`L${layerIdx}.q_norm_HEALTH`,
|
|
596
|
-
qTensor,
|
|
597
|
-
qTensor.dtype,
|
|
598
|
-
numTokens * numHeads * headDim
|
|
599
|
-
);
|
|
600
|
-
enqueueRecordedTensorHealth(
|
|
601
|
-
recorder,
|
|
602
|
-
`L${layerIdx}.k_norm_HEALTH`,
|
|
603
|
-
kTensor,
|
|
604
|
-
kTensor?.dtype ?? null,
|
|
605
|
-
numTokens * numKVHeads * headDim
|
|
606
|
-
);
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
if (!kvCacheWriteFused && config.valueNorm === true && !reusesSharedKV) {
|
|
611
|
-
const valueNormInputAliasesKey = vTensor.buffer === kTensor.buffer;
|
|
612
|
-
vTensor = await applyAttentionValueNorm({
|
|
613
|
-
recorder,
|
|
614
|
-
vTensor,
|
|
615
|
-
rmsNormEps,
|
|
616
|
-
numTokens,
|
|
617
|
-
numKVHeads,
|
|
618
|
-
headDim,
|
|
619
|
-
releaseTemporary: (buffer) => {
|
|
620
|
-
if (!valueNormInputAliasesKey) {
|
|
621
|
-
releaseOrTrack(recorder, buffer);
|
|
622
|
-
}
|
|
623
|
-
},
|
|
624
|
-
});
|
|
625
|
-
await runProbes('v_norm', vTensor.buffer, {
|
|
626
|
-
layerIdx, numTokens, hiddenSize: numKVHeads * headDim,
|
|
627
|
-
probes: state.debugProbes, recorder,
|
|
628
|
-
operatorDiagnostics: state.operatorDiagnostics, dtype: vTensor.dtype,
|
|
629
|
-
});
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
if (normed !== attentionInput) releaseOrTrack(recorder, normed.buffer);
|
|
633
|
-
if (attentionInputTemp) recorder.trackTemporaryBuffer(attentionInput.buffer);
|
|
634
|
-
|
|
635
|
-
// 3. RoPE (modifies tensor in-place)
|
|
636
|
-
if (!ropeApplied && !disableRoPE && state.ropeFreqsCos && state.ropeFreqsSin) {
|
|
637
|
-
const ropeOptions = {
|
|
638
|
-
headDim,
|
|
639
|
-
rotaryDim: config.ropeRotaryDim,
|
|
640
|
-
pairSpanDim: config.ropeFrequencyBaseDim ?? config.ropeRotaryDim,
|
|
641
|
-
interleaved: config.ropeInterleaved,
|
|
642
|
-
startPos: currentSeqLen,
|
|
643
|
-
executionPolicies: state.executionPolicies ?? null,
|
|
644
|
-
};
|
|
645
|
-
if (canUseRoPEQK(qTensor, kTensor, { reusesSharedKV })) {
|
|
646
|
-
await recordRoPEQK(recorder, qTensor, kTensor, state.ropeFreqsCos, state.ropeFreqsSin, numTokens, {
|
|
647
|
-
...ropeOptions,
|
|
648
|
-
numQHeads: numHeads,
|
|
649
|
-
numKVHeads,
|
|
650
|
-
});
|
|
651
|
-
} else {
|
|
652
|
-
await recordRoPE(recorder, qTensor, state.ropeFreqsCos, state.ropeFreqsSin, numTokens, {
|
|
653
|
-
...ropeOptions,
|
|
654
|
-
numHeads,
|
|
655
|
-
});
|
|
656
|
-
if (!reusesSharedKV) {
|
|
657
|
-
await recordRoPE(recorder, kTensor, state.ropeFreqsCos, state.ropeFreqsSin, numTokens, {
|
|
658
|
-
...ropeOptions,
|
|
659
|
-
numHeads: numKVHeads,
|
|
660
|
-
});
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
if (shouldTraceRecordedHealth(layerIdx, debugFlags)) {
|
|
665
|
-
enqueueRecordedTensorHealth(
|
|
666
|
-
recorder,
|
|
667
|
-
`L${layerIdx}.q_rope_HEALTH`,
|
|
668
|
-
qTensor,
|
|
669
|
-
qTensor.dtype,
|
|
670
|
-
numTokens * numHeads * headDim
|
|
671
|
-
);
|
|
672
|
-
enqueueRecordedTensorHealth(
|
|
673
|
-
recorder,
|
|
674
|
-
`L${layerIdx}.k_rope_HEALTH`,
|
|
675
|
-
kTensor,
|
|
676
|
-
kTensor?.dtype ?? null,
|
|
677
|
-
numTokens * numKVHeads * headDim
|
|
678
|
-
);
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
if (storeSharedKV && state.sharedAttentionState) {
|
|
682
|
-
state.sharedAttentionState.set(layerIdx, {
|
|
683
|
-
kTensor,
|
|
684
|
-
vTensor,
|
|
685
|
-
headDim,
|
|
686
|
-
numKVHeads,
|
|
687
|
-
});
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
// 4. Update KV cache (cache stores raw GPUBuffers for memory efficiency)
|
|
691
|
-
if (!skipKVCacheWrites && !diffusionGemmaDecoder && state.kvCache?.hasGPUCache?.()) {
|
|
692
|
-
if (kvCacheWriteFused) {
|
|
693
|
-
state.kvCache.recordF16UpdateAlreadyWrittenFromGPU(layerIdx, currentSeqLen, numTokens, tokenIds);
|
|
694
|
-
} else if (state.kvCache.kvDtype === 'f16') {
|
|
695
|
-
const hasExplicitF16KvContract = isAttentionKvDtypeExplicit(attentionPrecisionContract, 'f16');
|
|
696
|
-
if (kTensor.dtype !== 'f16' && !hasExplicitF16KvContract) {
|
|
697
|
-
assertAttentionDtypeTransitionAllowed(state, kTensor.dtype, 'f16', 'K would be narrowed implicitly for KV cache storage.');
|
|
698
|
-
}
|
|
699
|
-
if (vTensor.dtype !== 'f16' && !hasExplicitF16KvContract) {
|
|
700
|
-
assertAttentionDtypeTransitionAllowed(state, vTensor.dtype, 'f16', 'V would be narrowed implicitly for KV cache storage.');
|
|
701
|
-
}
|
|
702
|
-
const canWriteDirectF32ToF16 = kTensor.dtype === 'f32'
|
|
703
|
-
&& vTensor.dtype === 'f32'
|
|
704
|
-
&& typeof state.kvCache.recordUpdateF32ToF16FromGPU === 'function';
|
|
705
|
-
if (canWriteDirectF32ToF16) {
|
|
706
|
-
await state.kvCache.recordUpdateF32ToF16FromGPU(
|
|
707
|
-
recorder,
|
|
708
|
-
layerIdx,
|
|
709
|
-
kTensor.buffer,
|
|
710
|
-
vTensor.buffer,
|
|
711
|
-
currentSeqLen,
|
|
712
|
-
numTokens,
|
|
713
|
-
tokenIds
|
|
714
|
-
);
|
|
715
|
-
} else {
|
|
716
|
-
const kCasted = kTensor.dtype === 'f16' ? kTensor : await recordCastF32ToF16(recorder, kTensor);
|
|
717
|
-
const vCasted = vTensor.dtype === 'f16' ? vTensor : await recordCastF32ToF16(recorder, vTensor);
|
|
718
|
-
|
|
719
|
-
await state.kvCache.recordUpdateFromGPU(recorder, layerIdx, kCasted.buffer, vCasted.buffer, currentSeqLen, numTokens, tokenIds);
|
|
720
|
-
|
|
721
|
-
if (kTensor.dtype !== 'f16') recorder.trackTemporaryBuffer(kCasted.buffer);
|
|
722
|
-
if (vTensor.dtype !== 'f16') recorder.trackTemporaryBuffer(vCasted.buffer);
|
|
723
|
-
}
|
|
724
|
-
} else {
|
|
725
|
-
await state.kvCache.recordUpdateFromGPU(recorder, layerIdx, kTensor.buffer, vTensor.buffer, currentSeqLen, numTokens, tokenIds);
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
// Resolve KV cache state and build dispatch parameters (shared with run.js)
|
|
730
|
-
let kvState;
|
|
731
|
-
if (diffusionGemmaDecoder) {
|
|
732
|
-
const decoderKVDtype = kvCacheDtype ?? kTensor.dtype;
|
|
733
|
-
let decoderKTensor = kTensor;
|
|
734
|
-
let decoderVTensor = vTensor;
|
|
735
|
-
if (decoderKVDtype === 'f16' && decoderKTensor.dtype !== 'f16') {
|
|
736
|
-
const hasExplicitF16KvContract = isAttentionKvDtypeExplicit(attentionPrecisionContract, 'f16');
|
|
737
|
-
if (!hasExplicitF16KvContract) {
|
|
738
|
-
assertAttentionDtypeTransitionAllowed(state, decoderKTensor.dtype, 'f16', 'DiffusionGemma decoder K would be narrowed implicitly.');
|
|
739
|
-
}
|
|
740
|
-
decoderKTensor = await recordCastF32ToF16(recorder, decoderKTensor);
|
|
741
|
-
decoderKTemp = decoderKTensor;
|
|
742
|
-
} else if (decoderKVDtype === 'f32' && decoderKTensor.dtype !== 'f32') {
|
|
743
|
-
assertAttentionDtypeTransitionAllowed(state, decoderKTensor.dtype, 'f32', 'DiffusionGemma decoder K would be widened implicitly.');
|
|
744
|
-
decoderKTensor = await recordCastF16ToF32(recorder, decoderKTensor);
|
|
745
|
-
decoderKTemp = decoderKTensor;
|
|
746
|
-
}
|
|
747
|
-
if (decoderKVDtype === 'f16' && decoderVTensor.dtype !== 'f16') {
|
|
748
|
-
const hasExplicitF16KvContract = isAttentionKvDtypeExplicit(attentionPrecisionContract, 'f16');
|
|
749
|
-
if (!hasExplicitF16KvContract) {
|
|
750
|
-
assertAttentionDtypeTransitionAllowed(state, decoderVTensor.dtype, 'f16', 'DiffusionGemma decoder V would be narrowed implicitly.');
|
|
751
|
-
}
|
|
752
|
-
decoderVTensor = await recordCastF32ToF16(recorder, decoderVTensor);
|
|
753
|
-
decoderVTemp = decoderVTensor;
|
|
754
|
-
} else if (decoderKVDtype === 'f32' && decoderVTensor.dtype !== 'f32') {
|
|
755
|
-
assertAttentionDtypeTransitionAllowed(state, decoderVTensor.dtype, 'f32', 'DiffusionGemma decoder V would be widened implicitly.');
|
|
756
|
-
decoderVTensor = await recordCastF16ToF32(recorder, decoderVTensor);
|
|
757
|
-
decoderVTemp = decoderVTensor;
|
|
758
|
-
}
|
|
759
|
-
kvState = await createDiffusionGemmaDecoderKVState({
|
|
760
|
-
state,
|
|
761
|
-
layerIdx,
|
|
762
|
-
kTensor: decoderKTensor,
|
|
763
|
-
vTensor: decoderVTensor,
|
|
764
|
-
currentSeqLen,
|
|
765
|
-
numTokens,
|
|
766
|
-
numKVHeads,
|
|
767
|
-
headDim,
|
|
768
|
-
layerType,
|
|
769
|
-
slidingWindow,
|
|
770
|
-
kvDtype: decoderKVDtype,
|
|
771
|
-
recorder,
|
|
772
|
-
});
|
|
773
|
-
if (decoderKTemp) {
|
|
774
|
-
recorder.trackTemporaryBuffer(decoderKTemp.buffer);
|
|
775
|
-
decoderKTemp = null;
|
|
776
|
-
}
|
|
777
|
-
if (decoderVTemp) {
|
|
778
|
-
recorder.trackTemporaryBuffer(decoderVTemp.buffer);
|
|
779
|
-
decoderVTemp = null;
|
|
780
|
-
}
|
|
781
|
-
decoderKVState = kvState;
|
|
782
|
-
} else {
|
|
783
|
-
kvState = resolveKVCacheState(state, layerIdx, kTensor, vTensor, currentSeqLen, numTokens, {
|
|
784
|
-
forceCurrentTensors: skipKVCacheWrites,
|
|
785
|
-
});
|
|
786
|
-
}
|
|
787
|
-
const dispatchConfig = {
|
|
788
|
-
layerIdx, numTokens, isPrefill, numHeads, numKVHeads, headDim, hiddenSize,
|
|
789
|
-
slidingWindow: diffusionGemmaDecoder ? null : slidingWindow,
|
|
790
|
-
layerType, layerTypes: config.layerTypes,
|
|
791
|
-
queryPreAttnScalar,
|
|
792
|
-
causalAttention: diffusionGemmaDecoder ? false : config.causalAttention,
|
|
793
|
-
activationDtype: attentionActivationDtype,
|
|
794
|
-
kvCacheDtype: attentionPrecisionContract.resolvedKvCacheDtype ?? state.kvCache?.kvDtype ?? null,
|
|
795
|
-
};
|
|
796
|
-
const dispatchParams = buildAttentionDispatchParams(dispatchConfig, state, kTensor, vTensor, kvState);
|
|
797
|
-
const {
|
|
798
|
-
effectiveSlidingWindow, attentionKernelVariant, attnScale,
|
|
799
|
-
cachedKDtype, cachedVDtype, cachedKTensor, cachedVTensor,
|
|
800
|
-
prefillFallbackNeedsCast, causalForAttention,
|
|
801
|
-
} = dispatchParams;
|
|
802
|
-
const attentionKernelPath = skipKVCacheWrites ? null : kernelPath;
|
|
803
|
-
|
|
804
|
-
// 5. Attention
|
|
805
|
-
|
|
806
|
-
recordAttentionInputs(state, buildAttentionInputsData(
|
|
807
|
-
dispatchConfig, input, normed, kvState, dispatchParams,
|
|
808
|
-
{ useF16Activations, matmulOutputDtype },
|
|
809
|
-
usedFusedQKV, qTensor, kTensor, vTensor,
|
|
810
|
-
));
|
|
811
|
-
const mergedSessionRec = getRuntimeConfig()?.inference?.session;
|
|
812
|
-
let attentionOutputGateFused = false;
|
|
813
|
-
|
|
814
|
-
const attentionKernelRunners = {
|
|
815
|
-
bdpa: async () => {
|
|
816
|
-
const basisKDtype = 'f16';
|
|
817
|
-
const basisVDtype = 'f16';
|
|
818
|
-
const numBasisVectors = Math.max(1, kvState.bdpaBasisCount);
|
|
819
|
-
const basisKTensor = createTensor(kvState.bdpaBasisK, basisKDtype, [numBasisVectors, numKVHeads * headDim], 'bdpa_basis_k');
|
|
820
|
-
const basisVTensor = createTensor(kvState.bdpaBasisV, basisVDtype, [numBasisVectors, numKVHeads * headDim], 'bdpa_basis_v');
|
|
821
|
-
|
|
822
|
-
let qForBDPA = qTensor;
|
|
823
|
-
if (qForBDPA.dtype !== 'f16') {
|
|
824
|
-
assertAttentionDtypeTransitionAllowed(state, qForBDPA.dtype, 'f16', 'BDPA attention would narrow Q implicitly.');
|
|
825
|
-
qForBDPA = await recordCastF32ToF16(recorder, qTensor);
|
|
826
|
-
recorder.trackTemporaryBuffer(qForBDPA.buffer);
|
|
827
|
-
}
|
|
828
|
-
|
|
829
|
-
return recordAttentionBDPA(recorder, qForBDPA, basisKTensor, basisVTensor, kvState.bdpaPagedK, kvState.bdpaPagedV, kvState.bdpaIndex, numHeads, headDim, {
|
|
830
|
-
seqLen: numTokens,
|
|
831
|
-
kvLen: kvState.kvLenForAttention,
|
|
832
|
-
numKVHeads,
|
|
833
|
-
causal: causalForAttention,
|
|
834
|
-
startPos: kvState.startPosForMask,
|
|
835
|
-
layerIdx,
|
|
836
|
-
slidingWindow: effectiveSlidingWindow,
|
|
837
|
-
attnSoftcap,
|
|
838
|
-
scale: attnScale,
|
|
839
|
-
ropeCos: state.ropeFreqsCos,
|
|
840
|
-
ropeSin: state.ropeFreqsSin,
|
|
841
|
-
});
|
|
842
|
-
},
|
|
843
|
-
tieredQuant: async () => {
|
|
844
|
-
let qForAttention = qTensor;
|
|
845
|
-
if (kvState.coldQuantMode !== 'none' && qTensor.dtype !== 'f32') {
|
|
846
|
-
assertAttentionDtypeTransitionAllowed(state, qTensor.dtype, 'f32', 'Tiered quant attention would widen Q implicitly.');
|
|
847
|
-
qForAttention = await recordCastF16ToF32(recorder, qTensor);
|
|
848
|
-
recorder.trackTemporaryBuffer(qForAttention.buffer);
|
|
849
|
-
}
|
|
850
|
-
if (kvState.coldQuantMode === 'none') {
|
|
851
|
-
throw new Error('Tiered quant attention requires cold quant mode.');
|
|
852
|
-
}
|
|
853
|
-
if (!kvState.coldScalesK || !kvState.coldScalesV) {
|
|
854
|
-
throw new Error('Tiered quant attention requires cold scale buffers.');
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
const cachedHotKTensor = createTensor(kvState.cachedKHot, cachedKDtype, [kvState.hotLen, numKVHeads * headDim], 'cached_K_hot');
|
|
858
|
-
const cachedHotVTensor = createTensor(kvState.cachedVHot, cachedVDtype, [kvState.hotLen, numKVHeads * headDim], 'cached_V_hot');
|
|
859
|
-
return recordAttentionTieredQuant(
|
|
860
|
-
recorder,
|
|
861
|
-
qForAttention,
|
|
862
|
-
cachedHotKTensor,
|
|
863
|
-
cachedHotVTensor,
|
|
864
|
-
kvState.cachedKCold,
|
|
865
|
-
kvState.cachedVCold,
|
|
866
|
-
kvState.coldScalesK,
|
|
867
|
-
kvState.coldScalesV,
|
|
868
|
-
numHeads,
|
|
869
|
-
headDim,
|
|
870
|
-
buildTieredQuantAttentionOptions(kvState, {
|
|
871
|
-
seqLen: numTokens,
|
|
872
|
-
numKVHeads,
|
|
873
|
-
causal: causalForAttention,
|
|
874
|
-
startPos: kvState.startPosForMask,
|
|
875
|
-
slidingWindow: effectiveSlidingWindow ?? 0,
|
|
876
|
-
attnSoftcap,
|
|
877
|
-
scale: attnScale,
|
|
878
|
-
})
|
|
879
|
-
);
|
|
880
|
-
},
|
|
881
|
-
contiguousQuant: async () => {
|
|
882
|
-
let qForAttention = qTensor;
|
|
883
|
-
if (qTensor.dtype !== 'f32') {
|
|
884
|
-
assertAttentionDtypeTransitionAllowed(state, qTensor.dtype, 'f32', 'Contiguous quant attention would widen Q implicitly.');
|
|
885
|
-
qForAttention = await recordCastF16ToF32(recorder, qTensor);
|
|
886
|
-
recorder.trackTemporaryBuffer(qForAttention.buffer);
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
if (!kvState.coldScalesK || !kvState.coldScalesV) {
|
|
890
|
-
throw new Error('Contiguous quant attention requires scale buffers.');
|
|
891
|
-
}
|
|
892
|
-
if (!kvState.rotationMatrixBuffer || !kvState.codebookCentroidsBuffer) {
|
|
893
|
-
throw new Error('Contiguous quant attention requires TurboQuant shared buffers.');
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
return recordAttentionContiguousQuant(
|
|
897
|
-
recorder,
|
|
898
|
-
qForAttention,
|
|
899
|
-
kvState.cachedKCold,
|
|
900
|
-
kvState.cachedVCold,
|
|
901
|
-
kvState.coldScalesK,
|
|
902
|
-
kvState.coldScalesV,
|
|
903
|
-
numHeads,
|
|
904
|
-
headDim,
|
|
905
|
-
buildContiguousQuantAttentionOptions(kvState, {
|
|
906
|
-
seqLen: numTokens,
|
|
907
|
-
kvLen: kvState.kvLenForAttention,
|
|
908
|
-
numKVHeads,
|
|
909
|
-
causal: causalForAttention,
|
|
910
|
-
startPos: kvState.startPosForMask,
|
|
911
|
-
slidingWindow: effectiveSlidingWindow ?? 0,
|
|
912
|
-
attnSoftcap,
|
|
913
|
-
scale: attnScale,
|
|
914
|
-
})
|
|
915
|
-
);
|
|
916
|
-
},
|
|
917
|
-
tiered: async () => {
|
|
918
|
-
const cachedHotKTensor = createTensor(kvState.cachedKHot, cachedKDtype, [kvState.hotLen, numKVHeads * headDim], 'cached_K_hot');
|
|
919
|
-
const cachedHotVTensor = createTensor(kvState.cachedVHot, cachedVDtype, [kvState.hotLen, numKVHeads * headDim], 'cached_V_hot');
|
|
920
|
-
const cachedColdKTensor = createTensor(kvState.cachedKCold, cachedKDtype, [kvState.coldLen, numKVHeads * headDim], 'cached_K_cold');
|
|
921
|
-
const cachedColdVTensor = createTensor(kvState.cachedVCold, cachedVDtype, [kvState.coldLen, numKVHeads * headDim], 'cached_V_cold');
|
|
922
|
-
return recordAttentionTiered(recorder, qTensor, cachedHotKTensor, cachedHotVTensor, cachedColdKTensor, cachedColdVTensor, numHeads, headDim, {
|
|
923
|
-
seqLen: numTokens,
|
|
924
|
-
coldLen: kvState.coldLen,
|
|
925
|
-
hotLen: kvState.hotLen,
|
|
926
|
-
numKVHeads,
|
|
927
|
-
causal: causalForAttention,
|
|
928
|
-
startPos: kvState.startPosForMask,
|
|
929
|
-
slidingWindow: effectiveSlidingWindow ?? 0,
|
|
930
|
-
attnSoftcap,
|
|
931
|
-
scale: attnScale,
|
|
932
|
-
hotWindow: kvState.hotWindow,
|
|
933
|
-
hotStart: kvState.hotStart,
|
|
934
|
-
coldPageTable: kvState.coldPageTable,
|
|
935
|
-
coldPageSize: kvState.coldPageSize,
|
|
936
|
-
coldLayout: kvState.coldPageTable ? 2 : 0,
|
|
937
|
-
hotLayout: kvState.hotWindow > 0 ? 1 : 0,
|
|
938
|
-
});
|
|
939
|
-
},
|
|
940
|
-
contiguous: async () => {
|
|
941
|
-
// Prefill fallback: quantized/tiered layouts use raw K/V for prefill, cast to f16 to match kernel path
|
|
942
|
-
let kForAttn = cachedKTensor;
|
|
943
|
-
let vForAttn = cachedVTensor;
|
|
944
|
-
if (prefillFallbackNeedsCast) {
|
|
945
|
-
const hasExplicitF16KvContract = isAttentionKvDtypeExplicit(attentionPrecisionContract, 'f16');
|
|
946
|
-
if (cachedKDtype === 'f16' && kTensor.dtype !== 'f16' && !hasExplicitF16KvContract) {
|
|
947
|
-
assertAttentionDtypeTransitionAllowed(state, kTensor.dtype, 'f16', 'Prefill fallback attention would narrow K implicitly.');
|
|
948
|
-
}
|
|
949
|
-
if (cachedVDtype === 'f16' && vTensor.dtype !== 'f16' && !hasExplicitF16KvContract) {
|
|
950
|
-
assertAttentionDtypeTransitionAllowed(state, vTensor.dtype, 'f16', 'Prefill fallback attention would narrow V implicitly.');
|
|
951
|
-
}
|
|
952
|
-
const kCasted = cachedKDtype === 'f16' && kTensor.dtype !== 'f16'
|
|
953
|
-
? await recordCastF32ToF16(recorder, kTensor) : kTensor;
|
|
954
|
-
const vCasted = cachedVDtype === 'f16' && vTensor.dtype !== 'f16'
|
|
955
|
-
? await recordCastF32ToF16(recorder, vTensor) : vTensor;
|
|
956
|
-
kForAttn = createTensor(kCasted.buffer, kCasted.dtype, [kvState.kvLenForAttention, numKVHeads * headDim], 'cached_K');
|
|
957
|
-
vForAttn = createTensor(vCasted.buffer, vCasted.dtype, [kvState.kvLenForAttention, numKVHeads * headDim], 'cached_V');
|
|
958
|
-
if (kTensor.dtype !== 'f16') recorder.trackTemporaryBuffer(kCasted.buffer);
|
|
959
|
-
if (vTensor.dtype !== 'f16') recorder.trackTemporaryBuffer(vCasted.buffer);
|
|
960
|
-
}
|
|
961
|
-
// Session precedence is runtime-over-manifest per config-style-guide
|
|
962
|
-
// §Category Rules. getRuntimeConfig() returns the merged session (manifest
|
|
963
|
-
// is the base layer, runtime fields win field-by-field via merge.js).
|
|
964
|
-
// Kernel enforces head_dim=256, f16 KV, contiguous layout; only applies
|
|
965
|
-
// when numTokens > 1 (prefill). Same flag semantics as the non-recorder
|
|
966
|
-
// path in ./run.js.
|
|
967
|
-
const useFlashPrefillRec = !diffusionGemmaDecoder && mergedSessionRec?.useFlashPrefillAttention === true && numTokens > 1;
|
|
968
|
-
const useOrtFlashPrefillRec = !diffusionGemmaDecoder && mergedSessionRec?.useOrtFlashPrefillAttention === true && numTokens > 1;
|
|
969
|
-
const useOutputGateFusion = canUseAttentionOutputGateFusion({
|
|
970
|
-
session: mergedSessionRec,
|
|
971
|
-
qGateTensor,
|
|
972
|
-
numTokens,
|
|
973
|
-
numHeads,
|
|
974
|
-
headDim,
|
|
975
|
-
cachedKDtype,
|
|
976
|
-
cachedVDtype,
|
|
977
|
-
kernelPath: attentionKernelPath,
|
|
978
|
-
diffusionGemmaDecoder,
|
|
979
|
-
});
|
|
980
|
-
const result = await recordAttention(recorder, qTensor, kForAttn, vForAttn, null, numHeads, headDim, {
|
|
981
|
-
seqLen: numTokens,
|
|
982
|
-
kvLen: kvState.kvLenForAttention,
|
|
983
|
-
numKVHeads,
|
|
984
|
-
causal: causalForAttention,
|
|
985
|
-
bidirectionalSpanStart: multimodalBidirectionalSpan?.start ?? 0,
|
|
986
|
-
bidirectionalSpanLength: multimodalBidirectionalSpan?.length ?? 0,
|
|
987
|
-
startPos: kvState.startPosForMask,
|
|
988
|
-
layerIdx,
|
|
989
|
-
slidingWindow: effectiveSlidingWindow,
|
|
990
|
-
attnSoftcap,
|
|
991
|
-
scale: attnScale,
|
|
992
|
-
kvStart: kvState.kvStart,
|
|
993
|
-
kvLayout: kvState.kvLayout,
|
|
994
|
-
kvPageTable: kvState.kvPageTable,
|
|
995
|
-
kvPageSize: kvState.kvPageSize,
|
|
996
|
-
kernelPath: attentionKernelPath,
|
|
997
|
-
useFlashPrefill: skipKVCacheWrites ? false : useFlashPrefillRec,
|
|
998
|
-
useOrtFlashPrefill: skipKVCacheWrites ? false : useOrtFlashPrefillRec,
|
|
999
|
-
outputGate: useOutputGateFusion ? qGateTensor : null,
|
|
1000
|
-
});
|
|
1001
|
-
attentionOutputGateFused = result?.outputGateFused === true;
|
|
1002
|
-
return result;
|
|
1003
|
-
},
|
|
1004
|
-
};
|
|
1005
|
-
const runAttentionKernel = attentionKernelRunners[attentionKernelVariant];
|
|
1006
|
-
if (!runAttentionKernel) {
|
|
1007
|
-
throw new Error(`Unsupported attention kernel variant "${attentionKernelVariant}" at layer ${layerIdx}`);
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
try {
|
|
1011
|
-
attnOutput = await runAttentionKernel();
|
|
1012
|
-
} finally {
|
|
1013
|
-
if (decoderKVState?.ownedBuffers) {
|
|
1014
|
-
for (const buffer of decoderKVState.ownedBuffers) {
|
|
1015
|
-
recorder.trackTemporaryBuffer(buffer);
|
|
1016
|
-
}
|
|
1017
|
-
decoderKVState.ownedBuffers = null;
|
|
1018
|
-
}
|
|
1019
|
-
}
|
|
1020
|
-
await runProbes('attn_core_out', attnOutput.buffer, {
|
|
1021
|
-
layerIdx,
|
|
1022
|
-
numTokens,
|
|
1023
|
-
hiddenSize: numHeads * headDim,
|
|
1024
|
-
probes: state.debugProbes,
|
|
1025
|
-
recorder,
|
|
1026
|
-
operatorDiagnostics: state.operatorDiagnostics,
|
|
1027
|
-
dtype: attnOutput.dtype,
|
|
1028
|
-
});
|
|
1029
|
-
if (shouldTraceRecordedHealth(layerIdx, debugFlags)) {
|
|
1030
|
-
enqueueRecordedTensorHealth(
|
|
1031
|
-
recorder,
|
|
1032
|
-
`L${layerIdx}.attn_core_out_HEALTH`,
|
|
1033
|
-
attnOutput,
|
|
1034
|
-
attnOutput.dtype,
|
|
1035
|
-
numTokens * numHeads * headDim
|
|
1036
|
-
);
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
attnForProjection = attnOutput;
|
|
1040
|
-
if (qGateTensor && !attentionOutputGateFused) {
|
|
1041
|
-
// Mirror run.js gate dispatch. Qwen3_5/Qwen 3.6 full attention applies
|
|
1042
|
-
// sigmoid(gate) even when the HF config surfaces `output_gate_type=swish`.
|
|
1043
|
-
const gateActivation = 'sigmoid';
|
|
1044
|
-
attnForProjection = await recordSiLU(recorder, attnOutput, {
|
|
1045
|
-
size: numTokens * numHeads * headDim,
|
|
1046
|
-
gate: qGateTensor,
|
|
1047
|
-
useVec4: (numTokens * numHeads * headDim) % 4 === 0,
|
|
1048
|
-
gateActivation,
|
|
1049
|
-
inputActivation: 'identity',
|
|
1050
|
-
swigluLimit: null,
|
|
1051
|
-
});
|
|
1052
|
-
recorder.trackTemporaryBuffer(attnOutput.buffer);
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
|
-
// 6. Output projection (with optional fused residual for decode)
|
|
1056
|
-
|
|
1057
|
-
output = null;
|
|
1058
|
-
let residualFused = false;
|
|
1059
|
-
let oProjInput = attnForProjection;
|
|
1060
|
-
oProjInputTemp = null;
|
|
1061
|
-
if (layerWeights.oProj && getWeightBuffer) {
|
|
1062
|
-
({ oProjInput, oProjInputTemp } = await prepareAttentionProjectionInput(
|
|
1063
|
-
attnForProjection,
|
|
1064
|
-
oProjInputDtype,
|
|
1065
|
-
(tensor) => {
|
|
1066
|
-
assertAttentionDtypeTransitionAllowed(
|
|
1067
|
-
state,
|
|
1068
|
-
tensor.dtype,
|
|
1069
|
-
oProjInputDtype,
|
|
1070
|
-
'Attention output projection would change activations implicitly.',
|
|
1071
|
-
'step_precision'
|
|
1072
|
-
);
|
|
1073
|
-
return oProjInputDtype === 'f16'
|
|
1074
|
-
? recordCastF32ToF16(recorder, tensor)
|
|
1075
|
-
: recordCastF16ToF32(recorder, tensor);
|
|
1076
|
-
}
|
|
1077
|
-
));
|
|
1078
|
-
const oProjBuf = getWeightBuffer(layerWeights.oProj, 'o_proj');
|
|
1079
|
-
const loraO = getLoRAModule(lora, layerIdx, 'o_proj');
|
|
1080
|
-
|
|
1081
|
-
// Use fused o_proj + residual for decode when possible
|
|
1082
|
-
// Note: dtype from WeightBuffer metadata (buffer-dtypes WeakMap removed)
|
|
1083
|
-
const oProjDtype = getWeightDtype(oProjBuf);
|
|
1084
|
-
const canUseFused = selectRuleValue('inference', 'attention', 'useFusedOProjResidual', {
|
|
1085
|
-
allowFusedResidual: shouldUseFusedMatmulResidual(numTokens),
|
|
1086
|
-
hasResidual: Boolean(residualTensor),
|
|
1087
|
-
residualMatches: Boolean(residualTensor && residualTensor.dtype === oProjInput.dtype),
|
|
1088
|
-
attnIsF32: oProjInput.dtype === 'f32',
|
|
1089
|
-
attnIsF16: oProjInput.dtype === 'f16',
|
|
1090
|
-
hasLoRA: Boolean(loraO),
|
|
1091
|
-
oProjIsF16: oProjDtype === 'f16',
|
|
1092
|
-
});
|
|
1093
|
-
const canUseWideTileResidual = canUseQ4KWideTileResidualFusion({
|
|
1094
|
-
phase,
|
|
1095
|
-
session: mergedSessionRec,
|
|
1096
|
-
hasResidual: Boolean(residualTensor),
|
|
1097
|
-
residualMatches: Boolean(residualTensor && residualTensor.dtype === oProjInput.dtype),
|
|
1098
|
-
inputDtype: oProjInput.dtype,
|
|
1099
|
-
outputDtype: oProjOutputDtype,
|
|
1100
|
-
oProjDtype,
|
|
1101
|
-
hasLoRA: Boolean(loraO),
|
|
1102
|
-
});
|
|
1103
|
-
if (canUseFused && residualTensor) {
|
|
1104
|
-
// FUSED PATH: o_proj matmul + residual add in one dispatch
|
|
1105
|
-
output = await recordMatmulResidualFused(recorder, oProjInput, oProjBuf, residualTensor, {
|
|
1106
|
-
N: hiddenSize,
|
|
1107
|
-
K: numHeads * headDim,
|
|
1108
|
-
});
|
|
1109
|
-
residualFused = true;
|
|
1110
|
-
} else {
|
|
1111
|
-
// STANDARD PATH: o_proj matmul only unless Q4K WideTile fuses residual in the epilogue
|
|
1112
|
-
output = await recordMatmul(recorder, oProjInput, oProjBuf, numTokens, hiddenSize, numHeads * headDim, {
|
|
1113
|
-
transposeB: 'auto',
|
|
1114
|
-
role: 'o_proj',
|
|
1115
|
-
layerIdx,
|
|
1116
|
-
kernelPath,
|
|
1117
|
-
outputDtype: oProjOutputDtype,
|
|
1118
|
-
executionPolicies: state.executionPolicies ?? null,
|
|
1119
|
-
residualTensor: canUseWideTileResidual ? residualTensor : null,
|
|
1120
|
-
});
|
|
1121
|
-
residualFused = canUseWideTileResidual;
|
|
1122
|
-
}
|
|
1123
|
-
// Release temporary buffer if we created it (original was not already on GPU)
|
|
1124
|
-
if (!isGpuBufferInstance(layerWeights.oProj) && !isWeightBuffer(layerWeights.oProj)) {
|
|
1125
|
-
releaseOrTrack(recorder, isWeightBuffer(oProjBuf) ? oProjBuf.buffer : oProjBuf);
|
|
1126
|
-
}
|
|
1127
|
-
} else {
|
|
1128
|
-
output = oProjInput;
|
|
1129
|
-
}
|
|
1130
|
-
|
|
1131
|
-
// Apply LoRA to output projection if present (only if not using fused path)
|
|
1132
|
-
if (!residualFused) {
|
|
1133
|
-
const loraO = getLoRAModule(lora, layerIdx, 'o_proj');
|
|
1134
|
-
if (loraO && getWeightBuffer) {
|
|
1135
|
-
const combined = await applyLoRA(
|
|
1136
|
-
oProjInput,
|
|
1137
|
-
output,
|
|
1138
|
-
loraO,
|
|
1139
|
-
{ M: numTokens, N: hiddenSize, K: numHeads * headDim },
|
|
1140
|
-
getWeightBuffer,
|
|
1141
|
-
recorder,
|
|
1142
|
-
{ kernelPath }
|
|
1143
|
-
);
|
|
1144
|
-
if (combined.buffer !== output.buffer) {
|
|
1145
|
-
recorder.trackTemporaryBuffer(output.buffer);
|
|
1146
|
-
output = combined;
|
|
1147
|
-
}
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
|
|
1151
|
-
if (layerWeights.oProjBias) {
|
|
1152
|
-
const { tensor: oProjBiasTensor, owned } = getVectorTensor(
|
|
1153
|
-
layerWeights.oProjBias,
|
|
1154
|
-
`L${layerIdx}.o_proj_bias`,
|
|
1155
|
-
hiddenSize,
|
|
1156
|
-
{},
|
|
1157
|
-
debugFlags
|
|
1158
|
-
);
|
|
1159
|
-
output = await recordBiasAdd(
|
|
1160
|
-
recorder,
|
|
1161
|
-
output,
|
|
1162
|
-
oProjBiasTensor,
|
|
1163
|
-
numTokens,
|
|
1164
|
-
hiddenSize,
|
|
1165
|
-
{
|
|
1166
|
-
label: `L${layerIdx}.o_proj_bias`,
|
|
1167
|
-
layerIdx,
|
|
1168
|
-
executionPolicies: state.executionPolicies ?? null,
|
|
1169
|
-
}
|
|
1170
|
-
);
|
|
1171
|
-
if (owned) releaseOrTrack(recorder, oProjBiasTensor.buffer);
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
finalOutput = output;
|
|
1175
|
-
if (shouldTraceRecordedHealth(layerIdx, debugFlags)) {
|
|
1176
|
-
enqueueRecordedTensorHealth(
|
|
1177
|
-
recorder,
|
|
1178
|
-
`L${layerIdx}.o_proj_HEALTH`,
|
|
1179
|
-
output,
|
|
1180
|
-
output.dtype,
|
|
1181
|
-
numTokens * hiddenSize
|
|
1182
|
-
);
|
|
1183
|
-
}
|
|
1184
|
-
|
|
1185
|
-
const buffersToTrack = [];
|
|
1186
|
-
if (output.buffer !== attnForProjection.buffer) {
|
|
1187
|
-
buffersToTrack.push(attnForProjection.buffer);
|
|
1188
|
-
}
|
|
1189
|
-
if (oProjInputTemp && oProjInputTemp.buffer !== attnForProjection.buffer) {
|
|
1190
|
-
buffersToTrack.push(oProjInputTemp.buffer);
|
|
1191
|
-
}
|
|
1192
|
-
if (output.dtype !== oProjOutputDtype) {
|
|
1193
|
-
assertAttentionDtypeTransitionAllowed(state, output.dtype, oProjOutputDtype, 'Attention output would change implicitly before leaving the layer.');
|
|
1194
|
-
const coercedOutput = oProjOutputDtype === 'f16'
|
|
1195
|
-
? await recordCastF32ToF16(recorder, output)
|
|
1196
|
-
: await recordCastF16ToF32(recorder, output);
|
|
1197
|
-
buffersToTrack.push(output.buffer);
|
|
1198
|
-
finalOutput = coercedOutput;
|
|
1199
|
-
}
|
|
1200
|
-
|
|
1201
|
-
// Track intermediate buffers for cleanup after submit (not release!)
|
|
1202
|
-
// These buffers are used by recorded operations that haven't executed yet.
|
|
1203
|
-
// Releasing them back to the pool would allow reuse before the encoder is submitted,
|
|
1204
|
-
// causing data corruption (especially for small decode buffers).
|
|
1205
|
-
if (qTensor) {
|
|
1206
|
-
recorder.trackTemporaryBuffer(qTensor.buffer);
|
|
1207
|
-
}
|
|
1208
|
-
if (qGateTensor) {
|
|
1209
|
-
recorder.trackTemporaryBuffer(qGateTensor.buffer);
|
|
1210
|
-
}
|
|
1211
|
-
if (!retainSharedKvBuffers) {
|
|
1212
|
-
if (kTensor) {
|
|
1213
|
-
recorder.trackTemporaryBuffer(kTensor.buffer);
|
|
1214
|
-
}
|
|
1215
|
-
if (vTensor?.buffer && vTensor.buffer !== kTensor?.buffer) {
|
|
1216
|
-
recorder.trackTemporaryBuffer(vTensor.buffer);
|
|
1217
|
-
}
|
|
1218
|
-
}
|
|
1219
|
-
for (const buffer of buffersToTrack) {
|
|
1220
|
-
recorder.trackTemporaryBuffer(buffer);
|
|
1221
|
-
}
|
|
1222
|
-
|
|
1223
|
-
return { output: finalOutput, residualFused };
|
|
1224
|
-
} catch (error) {
|
|
1225
|
-
const tracked = new Set();
|
|
1226
|
-
const trackOnce = (buffer) => {
|
|
1227
|
-
if (!buffer || tracked.has(buffer)) return;
|
|
1228
|
-
tracked.add(buffer);
|
|
1229
|
-
recorder.trackTemporaryBuffer(buffer);
|
|
1230
|
-
};
|
|
1231
|
-
if (finalOutput?.buffer && finalOutput.buffer !== output?.buffer) {
|
|
1232
|
-
trackOnce(finalOutput.buffer);
|
|
1233
|
-
}
|
|
1234
|
-
if (output?.buffer && output.buffer !== attnForProjection?.buffer) {
|
|
1235
|
-
trackOnce(output.buffer);
|
|
1236
|
-
}
|
|
1237
|
-
if (oProjInputTemp?.buffer) {
|
|
1238
|
-
trackOnce(oProjInputTemp.buffer);
|
|
1239
|
-
}
|
|
1240
|
-
if (attnForProjection?.buffer && attnForProjection.buffer !== attnOutput?.buffer) {
|
|
1241
|
-
trackOnce(attnForProjection.buffer);
|
|
1242
|
-
}
|
|
1243
|
-
if (attnOutput?.buffer) {
|
|
1244
|
-
trackOnce(attnOutput.buffer);
|
|
1245
|
-
}
|
|
1246
|
-
if (qGateTensor?.buffer) {
|
|
1247
|
-
trackOnce(qGateTensor.buffer);
|
|
1248
|
-
}
|
|
1249
|
-
if (qTensor?.buffer) {
|
|
1250
|
-
trackOnce(qTensor.buffer);
|
|
1251
|
-
}
|
|
1252
|
-
if (kTensor?.buffer && !retainSharedKvBuffers) {
|
|
1253
|
-
trackOnce(kTensor.buffer);
|
|
1254
|
-
}
|
|
1255
|
-
if (vTensor?.buffer && !retainSharedKvBuffers) {
|
|
1256
|
-
trackOnce(vTensor.buffer);
|
|
1257
|
-
}
|
|
1258
|
-
if (normed?.buffer && normed.buffer !== attentionInput?.buffer) {
|
|
1259
|
-
trackOnce(normed.buffer);
|
|
1260
|
-
}
|
|
1261
|
-
if (attentionInputTemp && attentionInput?.buffer) {
|
|
1262
|
-
trackOnce(attentionInput.buffer);
|
|
1263
|
-
}
|
|
1264
|
-
if (decoderKTemp?.buffer) {
|
|
1265
|
-
trackOnce(decoderKTemp.buffer);
|
|
1266
|
-
}
|
|
1267
|
-
if (decoderVTemp?.buffer) {
|
|
1268
|
-
trackOnce(decoderVTemp.buffer);
|
|
1269
|
-
}
|
|
1270
|
-
if (decoderKVState?.ownedBuffers) {
|
|
1271
|
-
for (const buffer of decoderKVState.ownedBuffers) {
|
|
1272
|
-
trackOnce(buffer);
|
|
1273
|
-
}
|
|
1274
|
-
decoderKVState.ownedBuffers = null;
|
|
1275
|
-
}
|
|
1276
|
-
throw error;
|
|
1277
|
-
}
|
|
1278
|
-
}
|
|
2
|
+
export { recordLayerAttentionGPU } from './executor-recorded.js';
|