doppler-gpu 0.4.7 → 0.4.9
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 +74 -0
- package/README.md +93 -144
- package/models/catalog.json +1589 -0
- package/package.json +44 -12
- package/src/cli/doppler-cli.js +28 -7
- package/src/client/doppler-registry.json +2 -2
- package/src/client/runtime/index.js +25 -2
- package/src/client/runtime/model-session.d.ts +31 -1
- package/src/client/runtime/model-session.js +31 -0
- package/src/client/runtime/model-source.d.ts +19 -2
- package/src/client/runtime/model-source.js +45 -5
- package/src/config/conversion/gemma3/translategemma-4b-1b-enes-q4k-ehf16-af32.json +21 -5
- package/src/config/conversion/gemma3/translategemma-4b-it-q4k-ehf16-af32.json +6 -1
- package/src/config/conversion/gemma4/gemma-4-e2b-it-q4k-ehf16-af16-int4ple.json +1 -1
- package/src/config/conversion/qwen3/qwen-3-5-9b-f16-af32.json +474 -0
- package/src/config/conversion/qwen3/qwen-3-5-9b-q4k-ehaf16.json +509 -0
- package/src/config/conversion/qwen3/qwen-3-reranker-0-6b-q4k-ehf16-af32.json +9 -1
- package/src/config/kernel-path-loader.js +1 -0
- package/src/config/kernels/backward-registry.json +13 -2
- package/src/config/kernels/kernel-ref-digests.js +34 -21
- package/src/config/kernels/registry.json +1402 -2623
- package/src/config/merge.js +11 -0
- package/src/config/param-validator.js +25 -0
- package/src/config/runtime/default.json +3 -1
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-attn128-candidate.json +20 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-maxbatch64-candidate.json +52 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-normed-ffn-candidate.json +18 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-overlap-ringtokens1-candidate.json +52 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-parity.json +85 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-sequential-candidate.json +52 -0
- package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-throughput.json +85 -0
- package/src/config/runtime/profiles/qwen-3-5-2b-f16-primary-throughput.json +23 -0
- package/src/config/runtime/profiles/qwen-3-5-2b-metal-parity.json +38 -0
- package/src/config/runtime/profiles/qwen-3-5-2b-metal-throughput.json +135 -0
- package/src/config/runtime/profiles/qwen-3-5-9b-metal-correctness.json +46 -0
- package/src/config/runtime/profiles/qwen-3-embedding-0-6b-metal-stability.json +37 -0
- package/src/config/runtime/profiles/qwen-3-embedding-0-6b-metal-throughput.json +21 -0
- package/src/config/runtime/profiles/qwen-3-reranker-0-6b-metal-stability.json +41 -0
- package/src/config/runtime/profiles/qwen-3-reranker-0-6b-metal-throughput.json +41 -0
- package/src/config/runtime/profiles/rerank-stability.json +43 -0
- package/src/config/runtime/profiles/rerank-throughput.json +40 -0
- package/src/config/runtime/profiles/translategemma-1b-nativekd2-q4k-rdna3-throughput-probe.json +22 -0
- package/src/config/schema/doppler.schema.js +40 -5
- package/src/config/schema/execution-v1.schema.d.ts +41 -0
- package/src/config/schema/execution-v1.schema.js +10 -0
- package/src/config/schema/index.d.ts +3 -1
- package/src/config/schema/speculation-self.schema.d.ts +16 -0
- package/src/config/schema/training-metrics.schema.js +4 -0
- package/src/config/transforms/capability-transform-resolver.js +7 -1
- package/src/config/transforms/execution-graph-transforms.js +2 -2
- package/src/debug/config.d.ts +4 -0
- package/src/experimental/bridge/trainer-artifact-bridge.d.ts +161 -0
- package/src/experimental/bridge/trainer-artifact-bridge.js +573 -0
- package/src/experimental/orchestration/index.d.ts +10 -1
- package/src/experimental/training/attention-backward.js +123 -55
- package/src/experimental/training/autograd.js +101 -5
- package/src/experimental/training/clip.js +6 -1
- package/src/experimental/training/datasets/text-pairs.js +80 -8
- package/src/experimental/training/distillation/student-fixture.js +470 -114
- package/src/experimental/training/lora-pipeline.js +202 -27
- package/src/experimental/training/lora.js +10 -4
- package/src/experimental/training/optimizer.js +9 -1
- package/src/experimental/training/runner.js +18 -4
- package/src/experimental/training/suite.js +1 -1
- package/src/experimental/training/workloads.js +11 -0
- package/src/formats/rdrr/classification.js +3 -0
- package/src/formats/rdrr/integrity.d.ts +0 -2
- package/src/formats/rdrr/integrity.js +0 -2
- package/src/formats/rdrr/types.d.ts +1 -1
- package/src/gpu/kernels/attention.d.ts +2 -0
- package/src/gpu/kernels/attention.js +75 -16
- package/src/gpu/kernels/attention_decode_online_head256_f16kv_output_gate.wgsl +268 -0
- package/src/gpu/kernels/attention_head128_f16kv.wgsl +269 -0
- package/src/gpu/kernels/backward/adam.js +6 -4
- package/src/gpu/kernels/backward/causal_conv1d_silu_backward.d.ts +24 -0
- package/src/gpu/kernels/backward/causal_conv1d_silu_backward.js +74 -0
- package/src/gpu/kernels/backward/causal_conv1d_silu_backward.wgsl +59 -0
- package/src/gpu/kernels/backward/cross_entropy_backward.wgsl +4 -0
- package/src/gpu/kernels/backward/gated_delta_recurrent_backward.d.ts +39 -0
- package/src/gpu/kernels/backward/gated_delta_recurrent_backward.js +124 -0
- package/src/gpu/kernels/backward/gated_delta_recurrent_backward.wgsl +177 -0
- package/src/gpu/kernels/backward/gated_rmsnorm_backward.d.ts +32 -0
- package/src/gpu/kernels/backward/gated_rmsnorm_backward.js +90 -0
- package/src/gpu/kernels/backward/gated_rmsnorm_backward.wgsl +86 -0
- package/src/gpu/kernels/backward/index.d.ts +3 -0
- package/src/gpu/kernels/backward/index.js +3 -0
- package/src/gpu/kernels/backward/matmul_backward.wgsl +6 -6
- package/src/gpu/kernels/backward/matmul_backward_f16w.wgsl +82 -0
- package/src/gpu/kernels/backward/rmsnorm_backward.js +26 -4
- package/src/gpu/kernels/backward/rmsnorm_backward.wgsl +6 -3
- package/src/gpu/kernels/backward/utils.d.ts +12 -2
- package/src/gpu/kernels/backward/utils.js +9 -2
- package/src/gpu/kernels/cast_f16_to_f32.wgsl +6 -2
- package/src/gpu/kernels/fused_ffn.d.ts +32 -0
- package/src/gpu/kernels/fused_ffn.js +540 -19
- package/src/gpu/kernels/fused_ffn.wgsl +2 -1
- package/src/gpu/kernels/fused_ffn_f16.wgsl +2 -1
- package/src/gpu/kernels/fused_ffn_f16w_f32a_batched.wgsl +135 -0
- package/src/gpu/kernels/fused_ffn_q4k.wgsl +5 -3
- package/src/gpu/kernels/fused_ffn_q4k_f16.wgsl +2 -1
- package/src/gpu/kernels/fused_ffn_q4k_metal_simd16.wgsl +183 -0
- package/src/gpu/kernels/fused_gate_up_gelu_gated_f16.wgsl +2 -1
- package/src/gpu/kernels/fused_matmul_q4.wgsl +15 -6
- package/src/gpu/kernels/fused_matmul_q4_metal_simd16.wgsl +144 -0
- package/src/gpu/kernels/index.d.ts +26 -0
- package/src/gpu/kernels/index.js +18 -0
- package/src/gpu/kernels/linear-attention-core.js +413 -15
- package/src/gpu/kernels/lm-head-argmax.d.ts +10 -1
- package/src/gpu/kernels/lm-head-argmax.js +153 -57
- package/src/gpu/kernels/lm-head-select-logits.d.ts +23 -0
- package/src/gpu/kernels/lm-head-select-logits.js +186 -0
- package/src/gpu/kernels/lm_head_argmax_q4k.wgsl +289 -0
- package/src/gpu/kernels/lm_head_select_logits.wgsl +77 -0
- package/src/gpu/kernels/matmul-selection.js +10 -1
- package/src/gpu/kernels/residual_rmsnorm_pair.wgsl +127 -0
- package/src/gpu/kernels/rmsnorm-pair.d.ts +33 -0
- package/src/gpu/kernels/rmsnorm-pair.js +113 -0
- package/src/gpu/kernels/rmsnorm-stats.d.ts +29 -0
- package/src/gpu/kernels/rmsnorm-stats.js +250 -0
- package/src/gpu/kernels/silu.d.ts +1 -0
- package/src/gpu/kernels/silu.js +19 -4
- package/src/gpu/kernels/silu.wgsl +7 -1
- package/src/gpu/kernels/silu_f16.wgsl +7 -1
- package/src/gpu/kernels/split-qkv-rmsnorm-rope-qk.js +15 -10
- package/src/gpu/kernels/split_qkv_rmsnorm_rope_qk.wgsl +37 -7
- package/src/gpu/kernels/split_qkv_rmsnorm_rope_qk_f16kv_cache.wgsl +37 -7
- package/src/gpu/tensor.d.ts +1 -0
- package/src/index.d.ts +1 -1
- package/src/index.js +1 -1
- package/src/inference/browser-harness-suite-helpers.js +1 -1
- package/src/inference/browser-harness-text-helpers.js +251 -20
- package/src/inference/browser-harness.js +221 -3
- package/src/inference/kv-cache/base.js +42 -1
- package/src/inference/pipelines/structured/json-grammar-mask.d.ts +18 -0
- package/src/inference/pipelines/text/attention/dispatch-params.js +8 -3
- package/src/inference/pipelines/text/attention/output-gate-fusion.js +37 -0
- package/src/inference/pipelines/text/attention/projections.js +174 -4
- package/src/inference/pipelines/text/attention/record.js +44 -21
- package/src/inference/pipelines/text/attention/run.js +18 -3
- package/src/inference/pipelines/text/config.d.ts +18 -0
- package/src/inference/pipelines/text/config.js +100 -0
- package/src/inference/pipelines/text/embed.js +1 -1
- package/src/inference/pipelines/text/execution-plan.d.ts +2 -0
- package/src/inference/pipelines/text/execution-plan.js +37 -0
- package/src/inference/pipelines/text/execution-v1.d.ts +6 -0
- package/src/inference/pipelines/text/execution-v1.js +130 -10
- package/src/inference/pipelines/text/ffn/dense.js +241 -21
- package/src/inference/pipelines/text/ffn/standard.js +121 -38
- package/src/inference/pipelines/text/generator-decode-policy.js +1 -0
- package/src/inference/pipelines/text/generator-helpers.js +2 -0
- package/src/inference/pipelines/text/generator-runtime.js +1 -0
- package/src/inference/pipelines/text/generator-steps.js +2 -2
- package/src/inference/pipelines/text/generator.d.ts +18 -1
- package/src/inference/pipelines/text/generator.js +326 -19
- package/src/inference/pipelines/text/init-qkv-fusion.js +155 -62
- package/src/inference/pipelines/text/init.d.ts +22 -3
- package/src/inference/pipelines/text/init.js +2 -2
- package/src/inference/pipelines/text/layer.js +87 -8
- package/src/inference/pipelines/text/linear-attention-ab-fusion.js +208 -0
- package/src/inference/pipelines/text/linear-attention.d.ts +1 -0
- package/src/inference/pipelines/text/linear-attention.js +173 -16
- package/src/inference/pipelines/text/logits/gpu.js +2 -2
- package/src/inference/pipelines/text/logits/index.js +62 -1
- package/src/inference/pipelines/text/lora-apply.js +4 -4
- package/src/inference/pipelines/text/model-load.js +6 -0
- package/src/inference/pipelines/text/moe-cpu.js +1 -0
- package/src/inference/pipelines/text/moe-gpu.js +1 -0
- package/src/inference/pipelines/text/ops.js +22 -0
- package/src/inference/pipelines/text/per-layer-inputs.js +80 -11
- package/src/inference/pipelines/text/probes.js +9 -4
- package/src/inference/pipelines/text/state.d.ts +4 -4
- package/src/inference/pipelines/text/state.js +4 -0
- package/src/inference/pipelines/text/types.d.ts +27 -2
- package/src/inference/pipelines/text.d.ts +21 -1
- package/src/inference/pipelines/text.js +40 -14
- package/src/inference/tokenizers/bundled.js +150 -55
- package/src/inference/tokenizers/types.d.ts +7 -4
- package/src/loader/doppler-loader.js +25 -2
- package/src/loader/index.d.ts +3 -1
- package/src/loader/index.js +2 -0
- package/src/loader/loader-types.d.ts +3 -1
- package/src/loader/manifest-config.d.ts +18 -0
- package/src/loader/manifest-config.js +11 -3
- package/src/loader/shard-cache.d.ts +2 -0
- package/src/loader/shard-cache.js +29 -1
- package/src/models/gemma4.js +8 -0
- package/src/rules/inference/capability-transforms.rules.json +145 -20
- package/src/rules/inference/ffn.rules.json +33 -0
- package/src/rules/kernels/fused-ffn.rules.json +1 -1
- package/src/rules/kernels/lm-head-argmax.rules.json +10 -0
- package/src/rules/kernels/matmul.rules.json +2 -0
- package/src/rules/kernels/silu.rules.json +1 -0
- package/src/rules/rule-registry.js +2 -0
- package/src/storage/artifact-storage-context.js +36 -0
- package/src/tooling/node-browser-command-runner.js +42 -7
- package/src/tooling/node-converter.js +20 -5
- package/src/tooling/opfs-cache.d.ts +21 -1
- package/src/tooling/opfs-cache.js +168 -32
- package/src/tooling-experimental-exports.shared.d.ts +25 -0
- package/src/tooling-experimental-exports.shared.js +18 -0
- package/src/tooling-exports/storage.d.ts +1 -1
- package/src/tooling-exports/storage.js +1 -1
- package/src/types/gpu.d.ts +25 -0
- package/src/version.js +1 -1
- package/tests/kernels/browser/kernel-suite.js +1593 -0
- package/tests/kernels/browser/test-page.js +2629 -0
- package/tests/kernels/harness/benchmark.js +225 -0
- package/tests/kernels/harness/buffer-utils.js +88 -0
- package/tests/kernels/harness/tolerance.js +174 -0
- package/tests/kernels/reference/adam.js +15 -0
- package/tests/kernels/reference/attention.js +158 -0
- package/tests/kernels/reference/backward.js +96 -0
- package/tests/kernels/reference/bias-add.js +20 -0
- package/tests/kernels/reference/conv2d.js +58 -0
- package/tests/kernels/reference/cross-entropy.js +26 -0
- package/tests/kernels/reference/dequant.js +312 -0
- package/tests/kernels/reference/embed.js +15 -0
- package/tests/kernels/reference/gather.js +61 -0
- package/tests/kernels/reference/gelu.js +37 -0
- package/tests/kernels/reference/groupnorm.js +36 -0
- package/tests/kernels/reference/index.js +89 -0
- package/tests/kernels/reference/layernorm.js +22 -0
- package/tests/kernels/reference/matmul.js +57 -0
- package/tests/kernels/reference/modulate.js +24 -0
- package/tests/kernels/reference/moe-gather.js +83 -0
- package/tests/kernels/reference/pixel-shuffle.js +20 -0
- package/tests/kernels/reference/residual.js +55 -0
- package/tests/kernels/reference/rmsnorm.js +52 -0
- package/tests/kernels/reference/rope.js +79 -0
- package/tests/kernels/reference/sample.js +97 -0
- package/tests/kernels/reference/scatter-add.js +58 -0
- package/tests/kernels/reference/silu.js +52 -0
- package/tests/kernels/reference/softmax.js +86 -0
- package/tests/kernels/reference/split-qkv.js +58 -0
- package/tests/kernels/reference/topk.js +88 -0
- package/tests/kernels/reference/upsample2d.js +37 -0
- package/BRANDING.md +0 -14
- package/SECURITY.md +0 -19
- package/assets/doppler-runtime-map.svg +0 -82
- package/src/bootstrap.js +0 -30
- package/src/cli/doppler-cli.d.ts +0 -46
- package/src/cli/doppler-quickstart.d.ts +0 -45
- package/src/cli/doppler-serve.d.ts +0 -210
- package/src/cli/runtime-profiles.d.ts +0 -40
- package/src/client/doppler-api.browser.d.ts +0 -1
- package/src/client/doppler-api.browser.js +0 -25
- package/src/client/doppler-registry.d.ts +0 -54
- package/src/client/runtime/node-quickstart-cache.d.ts +0 -22
- package/src/client/runtime/source-runtime.d.ts +0 -26
- package/src/client/runtime/storage.d.ts +0 -5
- package/src/config/README.md +0 -61
- package/src/config/conversion/README.md +0 -62
- package/src/config/goal-completion-matrix.json +0 -552
- package/src/config/kernels/kernel-ref-digests.d.ts +0 -1
- package/src/config/kernels/moe/gpt-oss.paths.json +0 -49
- package/src/config/kernels/moe/mixtral.paths.json +0 -46
- package/src/config/kernels/registry.d.ts +0 -51
- package/src/config/merge-contract-check.d.ts +0 -16
- package/src/config/merge-contract-check.js +0 -319
- package/src/config/merge-helpers.d.ts +0 -60
- package/src/config/model-lanes/catalog-lane-resolver.d.ts +0 -66
- package/src/config/model-lanes/catalog-lane-resolver.js +0 -170
- package/src/config/param-categories.d.ts +0 -8
- package/src/config/param-validator.d.ts +0 -33
- package/src/config/platforms/loader.d.ts +0 -38
- package/src/config/quantization-contract-check.d.ts +0 -12
- package/src/config/quantization-contract-check.js +0 -91
- package/src/config/runtime/README.md +0 -28
- package/src/config/runtime-merge.d.ts +0 -5
- package/src/config/schema/claim-evidence-contract.schema.json +0 -93
- package/src/config/schema/command-surface-contract.schema.json +0 -81
- package/src/config/schema/goal-completion-matrix.schema.json +0 -223
- package/src/config/schema/model-artifact-contract.schema.json +0 -119
- package/src/config/schema/platform.schema.js +0 -1
- package/src/config/schema/policy-schema-registry.json +0 -31
- package/src/config/schema/subsystem-support-contract.schema.json +0 -48
- package/src/config/support-tiers/subsystems.json +0 -502
- package/src/config/supported-operations.d.ts +0 -14
- package/src/config/transforms/capability-transform-resolver.d.ts +0 -56
- package/src/config/transforms/execution-graph-transforms.d.ts +0 -306
- package/src/converter/conversion-plan.d.ts +0 -56
- package/src/converter/index.d.ts +0 -98
- package/src/converter/index.js +0 -59
- package/src/converter/parsers/diffusion.d.ts +0 -50
- package/src/converter/parsers/gguf.d.ts +0 -31
- package/src/converter/parsers/index.d.ts +0 -21
- package/src/converter/parsers/index.js +0 -12
- package/src/converter/parsers/transformer.d.ts +0 -31
- package/src/converter/quantization-info.d.ts +0 -38
- package/src/converter/rope-config.d.ts +0 -16
- package/src/converter/tokenizer-utils.d.ts +0 -12
- package/src/debug/reference/README.md +0 -114
- package/src/debug/reference/hf_attn_debug.py +0 -114
- package/src/debug/reference/hf_embed_check.py +0 -89
- package/src/debug/reference/hf_layer_out.py +0 -100
- package/src/debug/reference/hf_qwen35_linear_attn_debug.py +0 -268
- package/src/debug/reference/hf_rope_check.py +0 -116
- package/src/debug/reference/hf_weights.py +0 -75
- package/src/errors/doppler-error.d.ts +0 -20
- package/src/experimental/bridge/extension/background.d.ts +0 -14
- package/src/experimental/bridge/extension/background.js +0 -168
- package/src/experimental/bridge/extension/manifest.json +0 -34
- package/src/experimental/bridge/extension-client.d.ts +0 -114
- package/src/experimental/bridge/index.d.ts +0 -69
- package/src/experimental/bridge/node-bridge.js +0 -24
- package/src/experimental/bridge/protocol.d.ts +0 -96
- package/src/experimental/browser/gguf-importer.d.ts +0 -136
- package/src/experimental/browser/gguf-importer.js +0 -532
- package/src/experimental/browser/gguf-parser-browser.d.ts +0 -14
- package/src/experimental/browser/quantization.d.ts +0 -74
- package/src/experimental/browser/safetensors-parser-browser.d.ts +0 -193
- package/src/experimental/browser/shard-io-browser.d.ts +0 -57
- package/src/experimental/browser/shard-io-browser.js +0 -89
- package/src/experimental/browser/tensor-source-download.d.ts +0 -27
- package/src/experimental/diffusion/config/expansion-lanes.json +0 -29
- package/src/experimental/distribution/p2p-transport-contract.d.ts +0 -57
- package/src/experimental/distribution/p2p-transport.js +0 -75
- package/src/experimental/hotswap/intent-bundle.d.ts +0 -37
- package/src/experimental/hotswap/manifest.d.ts +0 -42
- package/src/experimental/hotswap/model-swap.js +0 -42
- package/src/experimental/hotswap/runtime.d.ts +0 -31
- package/src/experimental/training/README.md +0 -164
- package/src/experimental/training/artifacts.d.ts +0 -160
- package/src/experimental/training/attention-backward.d.ts +0 -30
- package/src/experimental/training/attention-forward.d.ts +0 -22
- package/src/experimental/training/attention-forward.js +0 -82
- package/src/experimental/training/autograd.d.ts +0 -51
- package/src/experimental/training/backward-kernels.js +0 -39
- package/src/experimental/training/checkpoint-watch.d.ts +0 -8
- package/src/experimental/training/checkpoint.d.ts +0 -36
- package/src/experimental/training/clip.d.ts +0 -9
- package/src/experimental/training/dataloader.d.ts +0 -8
- package/src/experimental/training/datasets/gepa-frontier.d.ts +0 -41
- package/src/experimental/training/datasets/gepa-frontier.js +0 -220
- package/src/experimental/training/datasets/index.d.ts +0 -24
- package/src/experimental/training/datasets/index.js +0 -13
- package/src/experimental/training/datasets/jsonl.d.ts +0 -11
- package/src/experimental/training/datasets/reploid.d.ts +0 -3
- package/src/experimental/training/datasets/reploid.js +0 -36
- package/src/experimental/training/datasets/teacher-traces.d.ts +0 -51
- package/src/experimental/training/datasets/teacher-traces.js +0 -279
- package/src/experimental/training/datasets/text-pairs.d.ts +0 -66
- package/src/experimental/training/datasets/token-batch.d.ts +0 -21
- package/src/experimental/training/datasets/token-batch.js +0 -52
- package/src/experimental/training/datasets/translation-pairs.d.ts +0 -34
- package/src/experimental/training/datasets/translation-pairs.js +0 -49
- package/src/experimental/training/distillation/artifacts.d.ts +0 -71
- package/src/experimental/training/distillation/checkpoint-watch.d.ts +0 -10
- package/src/experimental/training/distillation/dataset.d.ts +0 -59
- package/src/experimental/training/distillation/eval.d.ts +0 -34
- package/src/experimental/training/distillation/index.d.ts +0 -29
- package/src/experimental/training/distillation/runtime.d.ts +0 -20
- package/src/experimental/training/distillation/scoreboard.d.ts +0 -6
- package/src/experimental/training/distillation/stage-a.d.ts +0 -45
- package/src/experimental/training/distillation/stage-b.d.ts +0 -24
- package/src/experimental/training/distillation/student-fixture.d.ts +0 -27
- package/src/experimental/training/distillation/suite-data.d.ts +0 -45
- package/src/experimental/training/export.d.ts +0 -41
- package/src/experimental/training/index.d.ts +0 -74
- package/src/experimental/training/index.js +0 -63
- package/src/experimental/training/lora-pipeline.d.ts +0 -183
- package/src/experimental/training/lora.d.ts +0 -19
- package/src/experimental/training/loss-scaling.d.ts +0 -21
- package/src/experimental/training/loss.d.ts +0 -10
- package/src/experimental/training/objectives/base.d.ts +0 -58
- package/src/experimental/training/objectives/cross_entropy.d.ts +0 -18
- package/src/experimental/training/objectives/distill_kd.d.ts +0 -16
- package/src/experimental/training/objectives/distill_triplet.d.ts +0 -16
- package/src/experimental/training/objectives/index.d.ts +0 -12
- package/src/experimental/training/objectives/index.js +0 -6
- package/src/experimental/training/objectives/ul_stage1_joint.d.ts +0 -16
- package/src/experimental/training/objectives/ul_stage2_base.d.ts +0 -16
- package/src/experimental/training/operator-agent-eval.d.ts +0 -65
- package/src/experimental/training/operator-artifacts.d.ts +0 -62
- package/src/experimental/training/operator-command.d.ts +0 -5
- package/src/experimental/training/operator-eval.d.ts +0 -48
- package/src/experimental/training/operator-scoreboard.d.ts +0 -5
- package/src/experimental/training/optimizer.d.ts +0 -22
- package/src/experimental/training/runner.d.ts +0 -249
- package/src/experimental/training/suite.d.ts +0 -299
- package/src/experimental/training/tensor-factory.d.ts +0 -9
- package/src/experimental/training/trainer.d.ts +0 -89
- package/src/experimental/training/ul_dataset.d.ts +0 -47
- package/src/experimental/training/ul_schedule.d.ts +0 -6
- package/src/experimental/training/workload-packs/distill-glm52-doppler-agent-harness-gemma270m-f16-sft-tiny.json +0 -160
- package/src/experimental/training/workload-packs/distill-glm52-doppler-code-agent-gemma270m-f16-sft-tiny.json +0 -175
- package/src/experimental/training/workload-packs/distill-glm52-doppler-code-agent-sft-tiny.json +0 -175
- package/src/experimental/training/workload-packs/distill-glm52-doppler-js-json-gemma270m-f16-sft-tiny.json +0 -160
- package/src/experimental/training/workload-packs/distill-glm52-doppler-review-gemma270m-f16-sft-tiny.json +0 -162
- package/src/experimental/training/workload-packs/distill-glm52-doppler-wgsl-gemma270m-f16-sft-tiny.json +0 -160
- package/src/experimental/training/workload-packs/distill-translategemma-medium.json +0 -103
- package/src/experimental/training/workload-packs/distill-translategemma-tiny.json +0 -103
- package/src/experimental/training/workload-packs/lora-doppler-agent-harness-gemma270m-f16-tiny.json +0 -137
- package/src/experimental/training/workload-packs/lora-doppler-code-agent-gemma270m-f16-tiny.json +0 -152
- package/src/experimental/training/workload-packs/lora-doppler-code-agent-tiny.json +0 -152
- package/src/experimental/training/workload-packs/lora-doppler-js-json-gemma270m-f16-tiny.json +0 -137
- package/src/experimental/training/workload-packs/lora-doppler-review-gemma270m-f16-tiny.json +0 -139
- package/src/experimental/training/workload-packs/lora-doppler-wgsl-gemma270m-f16-tiny.json +0 -137
- package/src/experimental/training/workload-packs/lora-toy-medium.json +0 -91
- package/src/experimental/training/workload-packs/lora-toy-tiny.json +0 -91
- package/src/experimental/training/workload-packs/registry.json +0 -278
- package/src/experimental/training/workload-packs/ul-training-medium.json +0 -55
- package/src/experimental/training/workload-packs/ul-training-tiny.json +0 -55
- package/src/experimental/training/workloads.d.ts +0 -201
- package/src/formats/gguf/types.d.ts +0 -74
- package/src/formats/index.d.ts +0 -75
- package/src/formats/index.js +0 -19
- package/src/formats/litert/types.d.ts +0 -74
- package/src/formats/rdrr/distributed/parsing.d.ts +0 -6
- package/src/formats/rdrr/distributed/validation.d.ts +0 -23
- package/src/formats/rdrr/storage-descriptor.d.ts +0 -36
- package/src/formats/rdrr/tensor-config-validator.d.ts +0 -33
- package/src/formats/safetensors/types.d.ts +0 -67
- package/src/formats/tflite/types.d.ts +0 -61
- package/src/formats/tokenizer/index.d.ts +0 -5
- package/src/formats/tokenizer/index.js +0 -3
- package/src/formats/tokenizer/types.d.ts +0 -9
- package/src/gpu/kernel-runtime.d.ts +0 -13
- package/src/gpu/kernel-selection-cache.d.ts +0 -13
- package/src/gpu/kernel-selection-log.d.ts +0 -4
- package/src/gpu/kernel-tuner/benchmarks.d.ts +0 -144
- package/src/gpu/kernel-tuner/cache.d.ts +0 -55
- package/src/gpu/kernel-tuner/index.d.ts +0 -59
- package/src/gpu/kernel-tuner/tuner.d.ts +0 -82
- package/src/gpu/kernel-tuner/types.d.ts +0 -101
- package/src/gpu/kernel-tuner/types.js +0 -4
- package/src/gpu/kernel-tuner.d.ts +0 -33
- package/src/gpu/kernels/README.md +0 -166
- package/src/gpu/kernels/check-finiteness.d.ts +0 -15
- package/src/gpu/kernels/check-hot-vocab-stop.d.ts +0 -18
- package/src/gpu/kernels/check-stop.d.ts +0 -31
- package/src/gpu/kernels/codegen/patches/attention_decode_f16.from.attention_decode.diff +0 -213
- package/src/gpu/kernels/codegen/patches/attention_decode_f16kv.from.attention_decode.diff +0 -89
- package/src/gpu/kernels/codegen/patches/attention_f16.from.attention.diff +0 -62
- package/src/gpu/kernels/codegen/patches/attention_f16kv.from.attention.diff +0 -40
- package/src/gpu/kernels/codegen/patches/attention_small_f16.from.attention_small.diff +0 -226
- package/src/gpu/kernels/codegen/patches/attention_small_f16kv.from.attention_small.diff +0 -264
- package/src/gpu/kernels/codegen/patches/attention_streaming_f16.from.attention_streaming.diff +0 -71
- package/src/gpu/kernels/codegen/patches/attention_streaming_f16kv.from.attention_streaming.diff +0 -53
- package/src/gpu/kernels/codegen/patches/bias_add_f16.from.bias_add.diff +0 -54
- package/src/gpu/kernels/codegen/patches/dequant_mxfp4_expert_f16.from.dequant_mxfp4_expert.diff +0 -125
- package/src/gpu/kernels/codegen/patches/fused_ffn_f16.from.fused_ffn.diff +0 -428
- package/src/gpu/kernels/codegen/patches/fused_matmul_q4_batched_f16.from.fused_matmul_q4_batched.diff +0 -114
- package/src/gpu/kernels/codegen/patches/fused_matmul_rmsnorm_f16.from.fused_matmul_rmsnorm.diff +0 -477
- package/src/gpu/kernels/codegen/patches/fused_swiglu_f16.from.fused_swiglu.diff +0 -60
- package/src/gpu/kernels/codegen/patches/gelu_f16.from.gelu.diff +0 -41
- package/src/gpu/kernels/codegen/patches/layernorm_f16.from.layernorm.diff +0 -82
- package/src/gpu/kernels/codegen/patches/matmul_gemv_residual_f16.from.matmul_gemv_residual.diff +0 -87
- package/src/gpu/kernels/codegen/patches/moe_gather_f16.from.moe_gather.diff +0 -181
- package/src/gpu/kernels/codegen/patches/residual_f16.from.residual.diff +0 -65
- package/src/gpu/kernels/codegen/patches/rmsnorm_f16.from.rmsnorm.diff +0 -515
- package/src/gpu/kernels/codegen/patches/rope_f16.from.rope.diff +0 -292
- package/src/gpu/kernels/codegen/patches/sample_f16.from.sample.diff +0 -336
- package/src/gpu/kernels/codegen/patches/scatter_add_dynamic_f16.from.scatter_add_dynamic.diff +0 -65
- package/src/gpu/kernels/codegen/patches/silu_f16.from.silu.diff +0 -81
- package/src/gpu/kernels/codegen/patches/topk_f16.from.topk.diff +0 -231
- package/src/gpu/kernels/codegen/wgsl-patch-variants.js +0 -152
- package/src/gpu/kernels/codegen/wgsl-variants.js +0 -885
- package/src/gpu/kernels/constants.d.ts +0 -129
- package/src/gpu/kernels/dispatch.d.ts +0 -72
- package/src/gpu/kernels/gated-short-conv.d.ts +0 -63
- package/src/gpu/kernels/kernel-base.d.ts +0 -33
- package/src/gpu/kernels/linear-attention-core.d.ts +0 -42
- package/src/gpu/kernels/matmul-dispatch.d.ts +0 -49
- package/src/gpu/kernels/matmul-selection.d.ts +0 -87
- package/src/gpu/kernels/rep-penalty.d.ts +0 -21
- package/src/gpu/kernels/rule-matcher.d.ts +0 -30
- package/src/gpu/kernels/rule-registry.d.ts +0 -7
- package/src/gpu/kernels/types.js +0 -4
- package/src/gpu/perf-guards.d.ts +0 -13
- package/src/gpu/profiler.d.ts +0 -93
- package/src/gpu/readback-utils.d.ts +0 -16
- package/src/gpu/readback-utils.js +0 -49
- package/src/gpu/submit-probe.d.ts +0 -12
- package/src/index-browser.d.ts +0 -2
- package/src/index-browser.js +0 -2
- package/src/inference/README.md +0 -593
- package/src/inference/browser-harness-contract-helpers.d.ts +0 -5
- package/src/inference/browser-harness-diffusion-energy-suites.d.ts +0 -2
- package/src/inference/browser-harness-model-helpers.d.ts +0 -10
- package/src/inference/browser-harness-report-helpers.d.ts +0 -7
- package/src/inference/browser-harness-suite-helpers.d.ts +0 -40
- package/src/inference/browser-harness-text-helpers.d.ts +0 -152
- package/src/inference/model-lanes/execution-lane-resolver.d.ts +0 -61
- package/src/inference/model-lanes/execution-lane-resolver.js +0 -126
- package/src/inference/network-evolution.js +0 -79
- package/src/inference/pipelines/context.d.ts +0 -21
- package/src/inference/pipelines/diffusion/helpers.d.ts +0 -29
- package/src/inference/pipelines/diffusion/index.d.ts +0 -3
- package/src/inference/pipelines/diffusion/index.js +0 -3
- package/src/inference/pipelines/diffusion/scheduler.d.ts +0 -35
- package/src/inference/pipelines/diffusion/sd3-transformer.d.ts +0 -20
- package/src/inference/pipelines/diffusion/sd3-weights.d.ts +0 -21
- package/src/inference/pipelines/diffusion/text-encoder-gpu.d.ts +0 -82
- package/src/inference/pipelines/diffusion/text-encoder.d.ts +0 -29
- package/src/inference/pipelines/diffusion/types.js +0 -1
- package/src/inference/pipelines/diffusion/vae.d.ts +0 -20
- package/src/inference/pipelines/dream/energy-head-pipeline.d.ts +0 -29
- package/src/inference/pipelines/dream/energy-head-pipeline.js +0 -6
- package/src/inference/pipelines/dream/pipeline.d.ts +0 -17
- package/src/inference/pipelines/dream/pipeline.js +0 -8
- package/src/inference/pipelines/energy/index.d.ts +0 -1
- package/src/inference/pipelines/energy/index.js +0 -1
- package/src/inference/pipelines/energy/types.js +0 -1
- package/src/inference/pipelines/energy-head/index.d.ts +0 -6
- package/src/inference/pipelines/energy-head/index.js +0 -6
- package/src/inference/pipelines/factory.d.ts +0 -10
- package/src/inference/pipelines/index.d.ts +0 -22
- package/src/inference/pipelines/index.js +0 -19
- package/src/inference/pipelines/registry.d.ts +0 -15
- package/src/inference/pipelines/rng.d.ts +0 -2
- package/src/inference/pipelines/structured/tsv-grammar-mask.js +0 -166
- package/src/inference/pipelines/text/attention/attn-config.d.ts +0 -23
- package/src/inference/pipelines/text/attention/dispatch-params.d.ts +0 -173
- package/src/inference/pipelines/text/attention/index.d.ts +0 -24
- package/src/inference/pipelines/text/attention/output-projection.d.ts +0 -12
- package/src/inference/pipelines/text/attention/precision-contract.d.ts +0 -36
- package/src/inference/pipelines/text/attention/projections.d.ts +0 -208
- package/src/inference/pipelines/text/attention/quant-options.d.ts +0 -38
- package/src/inference/pipelines/text/attention/record.d.ts +0 -36
- package/src/inference/pipelines/text/attention/rmsnorm-fusion-gate.d.ts +0 -8
- package/src/inference/pipelines/text/attention/run.d.ts +0 -38
- package/src/inference/pipelines/text/attention/types.d.ts +0 -125
- package/src/inference/pipelines/text/attention.d.ts +0 -23
- package/src/inference/pipelines/text/attention.js +0 -12
- package/src/inference/pipelines/text/bdpa-steamroller.d.ts +0 -22
- package/src/inference/pipelines/text/buffer-types.js +0 -4
- package/src/inference/pipelines/text/debug-utils/config.d.ts +0 -144
- package/src/inference/pipelines/text/debug-utils/index.d.ts +0 -53
- package/src/inference/pipelines/text/debug-utils/logging.d.ts +0 -106
- package/src/inference/pipelines/text/debug-utils/tensor.d.ts +0 -119
- package/src/inference/pipelines/text/debug-utils/utils.d.ts +0 -77
- package/src/inference/pipelines/text/debug-utils.d.ts +0 -42
- package/src/inference/pipelines/text/drift-policy.d.ts +0 -57
- package/src/inference/pipelines/text/dtype-contract.d.ts +0 -53
- package/src/inference/pipelines/text/embed.d.ts +0 -69
- package/src/inference/pipelines/text/execution-runtime-builders.d.ts +0 -49
- package/src/inference/pipelines/text/failure-reduction.d.ts +0 -84
- package/src/inference/pipelines/text/failure-reduction.js +0 -180
- package/src/inference/pipelines/text/ffn/dense.d.ts +0 -99
- package/src/inference/pipelines/text/ffn/index.d.ts +0 -23
- package/src/inference/pipelines/text/ffn/moe.d.ts +0 -24
- package/src/inference/pipelines/text/ffn/sandwich.d.ts +0 -27
- package/src/inference/pipelines/text/ffn/standard.d.ts +0 -26
- package/src/inference/pipelines/text/ffn/types.d.ts +0 -30
- package/src/inference/pipelines/text/ffn.d.ts +0 -31
- package/src/inference/pipelines/text/ffn.js +0 -18
- package/src/inference/pipelines/text/finiteness-guard-status.d.ts +0 -11
- package/src/inference/pipelines/text/finiteness-policy.d.ts +0 -39
- package/src/inference/pipelines/text/generator-decode-policy.d.ts +0 -81
- package/src/inference/pipelines/text/generator-helpers.d.ts +0 -45
- package/src/inference/pipelines/text/generator-prefill-helpers.d.ts +0 -84
- package/src/inference/pipelines/text/generator-runtime.d.ts +0 -93
- package/src/inference/pipelines/text/generator-steps.d.ts +0 -186
- package/src/inference/pipelines/text/index.d.ts +0 -5
- package/src/inference/pipelines/text/index.js +0 -6
- package/src/inference/pipelines/text/kernel-trace.d.ts +0 -119
- package/src/inference/pipelines/text/layer.d.ts +0 -118
- package/src/inference/pipelines/text/load-errors.d.ts +0 -21
- package/src/inference/pipelines/text/logits/precision-policy.d.ts +0 -11
- package/src/inference/pipelines/text/logits/types.js +0 -4
- package/src/inference/pipelines/text/logits.d.ts +0 -27
- package/src/inference/pipelines/text/logits.js +0 -16
- package/src/inference/pipelines/text/lora-apply.d.ts +0 -28
- package/src/inference/pipelines/text/moe-cache.d.ts +0 -32
- package/src/inference/pipelines/text/moe-cpu-gptoss.d.ts +0 -9
- package/src/inference/pipelines/text/moe-cpu.d.ts +0 -13
- package/src/inference/pipelines/text/moe-gpu.d.ts +0 -26
- package/src/inference/pipelines/text/moe-helpers.d.ts +0 -12
- package/src/inference/pipelines/text/moe-shape-validator.d.ts +0 -70
- package/src/inference/pipelines/text/operator-events.d.ts +0 -125
- package/src/inference/pipelines/text/operator-identity.d.ts +0 -60
- package/src/inference/pipelines/text/ops.d.ts +0 -222
- package/src/inference/pipelines/text/per-layer-inputs.d.ts +0 -107
- package/src/inference/pipelines/text/probes.d.ts +0 -28
- package/src/inference/pipelines/text/sampling-config.d.ts +0 -48
- package/src/inference/pipelines/text/stage-names.d.ts +0 -88
- package/src/inference/pipelines/text/tsir-fixture-writer.d.ts +0 -57
- package/src/inference/pipelines/text/types.js +0 -4
- package/src/inference/tokenizers/base.d.ts +0 -44
- package/src/inference/tokenizers/behavior-flags.d.ts +0 -9
- package/src/inference/tokenizers/bpe.d.ts +0 -27
- package/src/inference/tokenizers/bundled.d.ts +0 -67
- package/src/inference/tokenizers/sentencepiece.d.ts +0 -30
- package/src/inference/tokenizers/types.js +0 -7
- package/src/loader/embedding-limit-preflight.d.ts +0 -31
- package/src/loader/per-layer-input-loader.d.ts +0 -30
- package/src/loader/tensors/functional-descriptor-loader.d.ts +0 -9
- package/src/loader/tensors/source-transform.d.ts +0 -65
- package/src/memory/address-table.d.ts +0 -96
- package/src/rules/execution-rules-contract-check.d.ts +0 -21
- package/src/rules/layer-pattern-contract-check.d.ts +0 -17
- package/src/rules/rule-registry.d.ts +0 -77
- package/src/storage/artifact-storage-context.d.ts +0 -63
- package/src/storage/backends/idb-store.d.ts +0 -52
- package/src/storage/backends/memory-store.d.ts +0 -36
- package/src/storage/backends/opfs-store.d.ts +0 -41
- package/src/storage/blake3.d.ts +0 -17
- package/src/storage/download-types.d.ts +0 -157
- package/src/storage/downloader.d.ts +0 -103
- package/src/storage/index.d.ts +0 -274
- package/src/storage/index.js +0 -201
- package/src/storage/preflight.d.ts +0 -144
- package/src/storage/preflight.js +0 -360
- package/src/storage/quickstart-downloader.d.ts +0 -157
- package/src/storage/quickstart-downloader.js +0 -289
- package/src/storage/source-artifact-store.d.ts +0 -59
- package/src/sw.js +0 -184
- package/src/tooling/command-api-constants.d.ts +0 -9
- package/src/tooling/command-api-family-normalizers.d.ts +0 -10
- package/src/tooling/command-api-helpers.d.ts +0 -23
- package/src/tooling/command-runner-shared.d.ts +0 -89
- package/src/tooling/conversion-config-materializer.d.ts +0 -14
- package/src/tooling/conversion-config-materializer.js +0 -96
- package/src/tooling/diagnose-runner.d.ts +0 -28
- package/src/tooling/hf-registry-utils.d.ts +0 -55
- package/src/tooling/kernel-path-builder/custom-runtime-facts.d.ts +0 -18
- package/src/tooling/kernel-path-builder/custom-runtime-facts.js +0 -72
- package/src/tooling/kernel-path-builder/index.d.ts +0 -65
- package/src/tooling/kernel-path-builder/index.js +0 -1361
- package/src/tooling/kernel-path-builder/runtime-overlay.d.ts +0 -9
- package/src/tooling/kernel-path-builder/runtime-overlay.js +0 -147
- package/src/tooling/lean-execution-contract-runner.d.ts +0 -33
- package/src/tooling/lean-execution-contract-runner.js +0 -158
- package/src/tooling/lean-execution-contract.d.ts +0 -16
- package/src/tooling/lean-execution-contract.js +0 -81
- package/src/tooling/litert-package-runtime.d.ts +0 -46
- package/src/tooling/node-convert-worker-pool.d.ts +0 -16
- package/src/tooling/node-convert-worker.d.ts +0 -1
- package/src/tooling/node-converter.d.ts +0 -1
- package/src/tooling/node-file-fetch.d.ts +0 -1
- package/src/tooling/node-source-runtime.d.ts +0 -24
- package/src/tooling/node-webgpu.d.ts +0 -17
- package/src/tooling/precision-replay-math.js +0 -174
- package/src/tooling/program-bundle-host.d.ts +0 -16
- package/src/tooling/program-bundle-host.js +0 -20
- package/src/tooling/runtime-input-composition.d.ts +0 -37
- package/src/tooling/source-package-profiles.d.ts +0 -50
- package/src/tooling/source-runtime-converter-config.d.ts +0 -19
- package/src/tooling/source-runtime-materializer.d.ts +0 -6
- package/src/tooling/source-runtime-materializer.js +0 -89
- package/src/tooling-exports.browser.d.ts +0 -7
- package/src/types/chrome.d.ts +0 -36
- package/src/types/chrome.js +0 -1
- package/src/types/index.d.ts +0 -1
- package/src/types/index.js +0 -1
- package/src/utils/array-buffer.d.ts +0 -4
- package/src/utils/canonical-hash.d.ts +0 -7
- package/src/utils/clone-json.d.ts +0 -1
- package/src/utils/hf-resolve-url.d.ts +0 -16
- package/src/utils/index.d.ts +0 -10
- package/src/utils/index.js +0 -10
- package/src/utils/load-json.d.ts +0 -5
- package/src/utils/plain-object.d.ts +0 -1
- package/src/utils/sha256.d.ts +0 -5
- package/src/utils/stable-sort-object.d.ts +0 -1
|
@@ -0,0 +1,1593 @@
|
|
|
1
|
+
import { testHarness as defaultHarness } from './test-page.js';
|
|
2
|
+
import {
|
|
3
|
+
layerNormRef,
|
|
4
|
+
groupNormRef,
|
|
5
|
+
conv2dRef,
|
|
6
|
+
conv2dBackwardRef,
|
|
7
|
+
biasAddRef,
|
|
8
|
+
biasAddBackwardRef,
|
|
9
|
+
pixelShuffleRef,
|
|
10
|
+
upsample2dRef,
|
|
11
|
+
upsample2dBackwardRef,
|
|
12
|
+
modulateRef,
|
|
13
|
+
crossEntropyLossRef,
|
|
14
|
+
crossEntropyBackwardRef,
|
|
15
|
+
embedBackwardRef,
|
|
16
|
+
adamRef,
|
|
17
|
+
softmaxBackwardRef,
|
|
18
|
+
siluBackwardRef,
|
|
19
|
+
geluBackwardRef,
|
|
20
|
+
scaleBackwardRef,
|
|
21
|
+
ropeBackwardRef,
|
|
22
|
+
rmsNormBackwardRef,
|
|
23
|
+
} from '../reference/index.js';
|
|
24
|
+
|
|
25
|
+
function generateUint32(size, max, seed = 123) {
|
|
26
|
+
const out = new Uint32Array(size);
|
|
27
|
+
let state = seed >>> 0;
|
|
28
|
+
for (let i = 0; i < size; i++) {
|
|
29
|
+
state = (state * 1664525 + 1013904223) >>> 0;
|
|
30
|
+
out[i] = max === 0 ? 0 : state % max;
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function generateNormalizedWeights(numTokens, topK, seed = 321) {
|
|
36
|
+
const weights = new Float32Array(numTokens * topK);
|
|
37
|
+
let state = seed >>> 0;
|
|
38
|
+
for (let t = 0; t < numTokens; t++) {
|
|
39
|
+
let sum = 0;
|
|
40
|
+
for (let k = 0; k < topK; k++) {
|
|
41
|
+
state = (state * 1103515245 + 12345) >>> 0;
|
|
42
|
+
const val = (state & 0xffff) / 0xffff;
|
|
43
|
+
weights[t * topK + k] = val;
|
|
44
|
+
sum += val;
|
|
45
|
+
}
|
|
46
|
+
if (sum > 0) {
|
|
47
|
+
for (let k = 0; k < topK; k++) {
|
|
48
|
+
weights[t * topK + k] /= sum;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return weights;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function compareExact(expected, actual) {
|
|
56
|
+
if (expected.length !== actual.length) return false;
|
|
57
|
+
for (let i = 0; i < expected.length; i++) {
|
|
58
|
+
if (expected[i] !== actual[i]) return false;
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function f16ToF32(bits) {
|
|
64
|
+
const sign = (bits & 0x8000) >> 15;
|
|
65
|
+
const exp = (bits & 0x7c00) >> 10;
|
|
66
|
+
const mant = bits & 0x03ff;
|
|
67
|
+
if (exp === 0) {
|
|
68
|
+
if (mant === 0) return sign ? -0 : 0;
|
|
69
|
+
return (sign ? -1 : 1) * Math.pow(2, -14) * (mant / 1024);
|
|
70
|
+
}
|
|
71
|
+
if (exp === 31) {
|
|
72
|
+
return mant === 0 ? (sign ? -Infinity : Infinity) : NaN;
|
|
73
|
+
}
|
|
74
|
+
return (sign ? -1 : 1) * Math.pow(2, exp - 15) * (1 + mant / 1024);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function f32ToF16Bits(value) {
|
|
78
|
+
const view = new DataView(new ArrayBuffer(4));
|
|
79
|
+
view.setFloat32(0, value, true);
|
|
80
|
+
const bits = view.getUint32(0, true);
|
|
81
|
+
const sign = (bits >> 31) & 1;
|
|
82
|
+
const exp = (bits >> 23) & 0xff;
|
|
83
|
+
const mant = bits & 0x7fffff;
|
|
84
|
+
|
|
85
|
+
let hExp = 0;
|
|
86
|
+
let hMant = 0;
|
|
87
|
+
if (exp === 0xff) {
|
|
88
|
+
hExp = 0x1f;
|
|
89
|
+
hMant = mant ? 0x200 : 0;
|
|
90
|
+
} else if (exp !== 0) {
|
|
91
|
+
const newExp = exp - 127 + 15;
|
|
92
|
+
if (newExp >= 0x1f) {
|
|
93
|
+
hExp = 0x1f;
|
|
94
|
+
} else if (newExp > 0) {
|
|
95
|
+
hExp = newExp;
|
|
96
|
+
hMant = mant >> 13;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return (sign << 15) | (hExp << 10) | hMant;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function f32ArrayToF16(arr) {
|
|
103
|
+
const out = new Uint16Array(arr.length);
|
|
104
|
+
for (let i = 0; i < arr.length; i++) {
|
|
105
|
+
out[i] = f32ToF16Bits(arr[i]);
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function bf16ToF32Array(bf16) {
|
|
111
|
+
const out = new Float32Array(bf16.length);
|
|
112
|
+
for (let i = 0; i < bf16.length; i++) {
|
|
113
|
+
const view = new DataView(new ArrayBuffer(4));
|
|
114
|
+
view.setUint32(0, bf16[i] << 16, true);
|
|
115
|
+
out[i] = view.getFloat32(0, true);
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function bf16ToF16Array(bf16) {
|
|
121
|
+
const asF32 = bf16ToF32Array(bf16);
|
|
122
|
+
return f32ArrayToF16(asF32);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function isFiniteArray(arr) {
|
|
126
|
+
for (let i = 0; i < arr.length; i++) {
|
|
127
|
+
if (!Number.isFinite(arr[i])) return false;
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function isUnsupportedCapabilityError(error) {
|
|
133
|
+
const message = error?.message || String(error || '');
|
|
134
|
+
return (
|
|
135
|
+
message.includes('requires features: shader-f16')
|
|
136
|
+
|| message.includes('shader-f16 support')
|
|
137
|
+
|| message.includes('requires unsupported GPU features')
|
|
138
|
+
|| message.includes('extension \'f16\' is not allowed in the current environment')
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function transposeRef(input, rows, cols) {
|
|
143
|
+
const output = new Float32Array(rows * cols);
|
|
144
|
+
for (let r = 0; r < rows; r++) {
|
|
145
|
+
for (let c = 0; c < cols; c++) {
|
|
146
|
+
output[c * rows + r] = input[r * cols + c];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return output;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function matmulRefRowMajorB(input, weights, M, N, K) {
|
|
153
|
+
const output = new Float32Array(M * N);
|
|
154
|
+
for (let m = 0; m < M; m++) {
|
|
155
|
+
const aBase = m * K;
|
|
156
|
+
for (let n = 0; n < N; n++) {
|
|
157
|
+
const bBase = n * K;
|
|
158
|
+
let sum = 0;
|
|
159
|
+
for (let k = 0; k < K; k++) {
|
|
160
|
+
sum += input[aBase + k] * weights[bBase + k];
|
|
161
|
+
}
|
|
162
|
+
output[m * N + n] = sum;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return output;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async function defineKernelTest(h, name, options) {
|
|
169
|
+
const {
|
|
170
|
+
dims,
|
|
171
|
+
inputs,
|
|
172
|
+
ref,
|
|
173
|
+
gpu,
|
|
174
|
+
tolerance,
|
|
175
|
+
seed = 1234
|
|
176
|
+
} = options;
|
|
177
|
+
|
|
178
|
+
// Generate input data
|
|
179
|
+
const generatedInputs = {};
|
|
180
|
+
let currentSeed = seed;
|
|
181
|
+
for (const [key, type] of Object.entries(inputs)) {
|
|
182
|
+
if (type === 'float') {
|
|
183
|
+
generatedInputs[key] = h.generateTestData(Object.values(dims).reduce((a, b) => a * b, 1), currentSeed++);
|
|
184
|
+
} else if (type === 'uint') {
|
|
185
|
+
generatedInputs[key] = generateUint32(Object.values(dims).reduce((a, b) => a * b, 1), 100, currentSeed++);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const expected = await ref(h, generatedInputs, dims);
|
|
190
|
+
const actual = await gpu(h, generatedInputs, dims);
|
|
191
|
+
|
|
192
|
+
const result = h.compareArrays(expected, actual, tolerance);
|
|
193
|
+
if (!result.passed) {
|
|
194
|
+
console.error(`[KernelTests] ${name} mismatch`, result);
|
|
195
|
+
}
|
|
196
|
+
return result.passed;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export async function runKernelSuite(harness) {
|
|
200
|
+
const h = harness || (typeof window !== 'undefined' ? window.testHarness : null) || defaultHarness;
|
|
201
|
+
if (!h) throw new Error('Kernel harness not ready');
|
|
202
|
+
|
|
203
|
+
const tests = [];
|
|
204
|
+
|
|
205
|
+
tests.push([
|
|
206
|
+
'matmul_f32',
|
|
207
|
+
async () => {
|
|
208
|
+
const M = 32, N = 48, K = 16;
|
|
209
|
+
const A = h.generateTestData(M * K, 42);
|
|
210
|
+
const B = h.generateTestData(K * N, 1337);
|
|
211
|
+
const expected = h.matmulRef(A, B, M, N, K, 1.0);
|
|
212
|
+
const actual = await h.runMatmul(null, A, B, M, N, K, 1.0);
|
|
213
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.matmul_f32);
|
|
214
|
+
return result.passed;
|
|
215
|
+
},
|
|
216
|
+
]);
|
|
217
|
+
|
|
218
|
+
tests.push([
|
|
219
|
+
'matmul_q4k',
|
|
220
|
+
async () => {
|
|
221
|
+
const M = 2, N = 4, K = 256;
|
|
222
|
+
const A = h.generateTestData(M * K, 2101, { min: -0.5, max: 0.5 });
|
|
223
|
+
const B = h.generateTestData(N * K, 2102, { min: -0.5, max: 0.5 });
|
|
224
|
+
const numBlocks = N;
|
|
225
|
+
const B_q4k = h.references.quantizeQ4_KRef(B, numBlocks);
|
|
226
|
+
const B_dequant = h.references.dequantQ4_KRef(B_q4k, numBlocks);
|
|
227
|
+
const expected = matmulRefRowMajorB(A, B_dequant, M, N, K);
|
|
228
|
+
const actual = await h.runMatmulQ4K(null, A, B_q4k, M, N, K, 1.0);
|
|
229
|
+
const result = h.compareArrays(expected, actual, { rtol: 1e-2, atol: 1e-2 });
|
|
230
|
+
return result.passed;
|
|
231
|
+
},
|
|
232
|
+
]);
|
|
233
|
+
|
|
234
|
+
tests.push([
|
|
235
|
+
'matmul_f16w',
|
|
236
|
+
async () => {
|
|
237
|
+
const M = 2, N = 6, K = 32;
|
|
238
|
+
const A = h.generateTestData(M * K, 2201);
|
|
239
|
+
const B = h.generateTestData(N * K, 2202);
|
|
240
|
+
const B_f16 = f32ArrayToF16(B);
|
|
241
|
+
const B_rounded = new Float32Array(B_f16.length);
|
|
242
|
+
for (let i = 0; i < B_f16.length; i++) {
|
|
243
|
+
B_rounded[i] = f16ToF32(B_f16[i]);
|
|
244
|
+
}
|
|
245
|
+
const expected = matmulRefRowMajorB(A, B_rounded, M, N, K);
|
|
246
|
+
const actual = await h.runMatmulF16W(null, A, B_f16, M, N, K);
|
|
247
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.matmul_f16);
|
|
248
|
+
return result.passed;
|
|
249
|
+
},
|
|
250
|
+
]);
|
|
251
|
+
|
|
252
|
+
tests.push([
|
|
253
|
+
'dequant_matmul_f16w',
|
|
254
|
+
async () => {
|
|
255
|
+
const M = 2, N = 4, K = 256;
|
|
256
|
+
const A = h.generateTestData(M * K, 2301, { min: -0.5, max: 0.5 });
|
|
257
|
+
const B = h.generateTestData(N * K, 2302, { min: -0.5, max: 0.5 });
|
|
258
|
+
const numBlocks = N;
|
|
259
|
+
const B_q4k = h.references.quantizeQ4_KRef(B, numBlocks);
|
|
260
|
+
const B_dequant = h.references.dequantQ4_KRef(B_q4k, numBlocks);
|
|
261
|
+
const B_f16 = f32ArrayToF16(B_dequant);
|
|
262
|
+
const B_rounded = new Float32Array(B_f16.length);
|
|
263
|
+
for (let i = 0; i < B_f16.length; i++) {
|
|
264
|
+
B_rounded[i] = f16ToF32(B_f16[i]);
|
|
265
|
+
}
|
|
266
|
+
const expected = matmulRefRowMajorB(A, B_rounded, M, N, K);
|
|
267
|
+
const actual = await h.runDequantAndMatmulF16W(null, A, B_q4k, M, N, K, numBlocks);
|
|
268
|
+
const result = h.compareArrays(expected, actual, { rtol: 1e-2, atol: 1e-2 });
|
|
269
|
+
return result.passed;
|
|
270
|
+
},
|
|
271
|
+
]);
|
|
272
|
+
|
|
273
|
+
tests.push([
|
|
274
|
+
'matmul_transpose_a',
|
|
275
|
+
async () => {
|
|
276
|
+
const M = 16, N = 32, K = 8;
|
|
277
|
+
const A_raw = h.generateTestData(K * M, 4201); // A is [K, M]
|
|
278
|
+
const B = h.generateTestData(K * N, 4202); // B is [K, N]
|
|
279
|
+
|
|
280
|
+
// Reference: C = A^T * B where A is [K, M], B is [K, N]
|
|
281
|
+
const A_T = new Float32Array(M * K);
|
|
282
|
+
for (let m = 0; m < M; m += 1) {
|
|
283
|
+
const outputBase = m * K;
|
|
284
|
+
const rawBase = m;
|
|
285
|
+
for (let k = 0; k < K; k += 1) {
|
|
286
|
+
A_T[outputBase + k] = A_raw[rawBase + k * M];
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const expected = h.matmulRef(A_T, B, M, N, K);
|
|
290
|
+
|
|
291
|
+
const actual = await h.runMatmulTransposeA(null, A_raw, B, M, N, K);
|
|
292
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.matmul_f32);
|
|
293
|
+
if (!result.passed) {
|
|
294
|
+
console.error('[KernelTests] matmul_transpose_a mismatch', JSON.stringify({
|
|
295
|
+
...result,
|
|
296
|
+
firstMismatches: result.firstMismatches,
|
|
297
|
+
}));
|
|
298
|
+
}
|
|
299
|
+
return result.passed;
|
|
300
|
+
},
|
|
301
|
+
]);
|
|
302
|
+
|
|
303
|
+
tests.push([
|
|
304
|
+
'matmul_f32_unaligned',
|
|
305
|
+
async () => {
|
|
306
|
+
const M = 13, N = 17, K = 7;
|
|
307
|
+
const A = h.generateTestData(M * K, 43);
|
|
308
|
+
const B = h.generateTestData(K * N, 1338);
|
|
309
|
+
const expected = h.matmulRef(A, B, M, N, K, 1.0);
|
|
310
|
+
const actual = await h.runMatmul(null, A, B, M, N, K, 1.0);
|
|
311
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.matmul_f32);
|
|
312
|
+
return result.passed;
|
|
313
|
+
},
|
|
314
|
+
]);
|
|
315
|
+
|
|
316
|
+
tests.push([
|
|
317
|
+
'bias_add_backward',
|
|
318
|
+
async () => {
|
|
319
|
+
const numTokens = 3;
|
|
320
|
+
const dim = 7;
|
|
321
|
+
const gradOutput = h.generateTestData(numTokens * dim, 1506);
|
|
322
|
+
const expected = biasAddBackwardRef(gradOutput, numTokens, dim);
|
|
323
|
+
const actual = await h.runBiasAddBackward(null, gradOutput, numTokens, dim);
|
|
324
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
325
|
+
return result.passed;
|
|
326
|
+
},
|
|
327
|
+
]);
|
|
328
|
+
|
|
329
|
+
tests.push([
|
|
330
|
+
'upsample2d_backward',
|
|
331
|
+
async () => {
|
|
332
|
+
const options = { channels: 2, inHeight: 3, inWidth: 3, scale: 2 };
|
|
333
|
+
options.outHeight = options.inHeight * options.scale;
|
|
334
|
+
options.outWidth = options.inWidth * options.scale;
|
|
335
|
+
const gradOutput = h.generateTestData(options.channels * options.outHeight * options.outWidth, 1507);
|
|
336
|
+
const expected = upsample2dBackwardRef(gradOutput, options);
|
|
337
|
+
const actual = await h.runUpsample2DBackward(null, gradOutput, options);
|
|
338
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
339
|
+
return result.passed;
|
|
340
|
+
},
|
|
341
|
+
]);
|
|
342
|
+
|
|
343
|
+
tests.push([
|
|
344
|
+
'conv2d_backward',
|
|
345
|
+
async () => {
|
|
346
|
+
const options = {
|
|
347
|
+
inChannels: 2,
|
|
348
|
+
outChannels: 2,
|
|
349
|
+
height: 4,
|
|
350
|
+
width: 4,
|
|
351
|
+
kernelH: 3,
|
|
352
|
+
kernelW: 3,
|
|
353
|
+
stride: 1,
|
|
354
|
+
pad: 1,
|
|
355
|
+
};
|
|
356
|
+
options.outHeight = options.height;
|
|
357
|
+
options.outWidth = options.width;
|
|
358
|
+
const input = h.generateTestData(options.inChannels * options.height * options.width, 1508);
|
|
359
|
+
const weight = h.generateTestData(options.outChannels * options.inChannels * options.kernelH * options.kernelW, 1509);
|
|
360
|
+
const gradOutput = h.generateTestData(options.outChannels * options.outHeight * options.outWidth, 1510);
|
|
361
|
+
|
|
362
|
+
const expected = conv2dBackwardRef(input, weight, gradOutput, options);
|
|
363
|
+
const actual = await h.runConv2DBackward(null, input, weight, gradOutput, options);
|
|
364
|
+
const conv2dBackwardTolerance = { rtol: 1e-5, atol: 1e-6 };
|
|
365
|
+
|
|
366
|
+
const inputResult = h.compareArrays(expected.gradInput, actual.gradInput, conv2dBackwardTolerance);
|
|
367
|
+
const weightResult = h.compareArrays(expected.gradWeight, actual.gradWeight, conv2dBackwardTolerance);
|
|
368
|
+
const inPass = inputResult.passed;
|
|
369
|
+
const wPass = weightResult.passed;
|
|
370
|
+
if (!inPass || !wPass) {
|
|
371
|
+
console.error('[KernelTests] conv2d_backward mismatch', JSON.stringify({
|
|
372
|
+
inPass,
|
|
373
|
+
wPass,
|
|
374
|
+
inputResult: {
|
|
375
|
+
...inputResult,
|
|
376
|
+
firstMismatches: inputResult.firstMismatches,
|
|
377
|
+
},
|
|
378
|
+
weightResult: {
|
|
379
|
+
...weightResult,
|
|
380
|
+
firstMismatches: weightResult.firstMismatches,
|
|
381
|
+
},
|
|
382
|
+
}));
|
|
383
|
+
}
|
|
384
|
+
return inPass && wPass;
|
|
385
|
+
},
|
|
386
|
+
]);
|
|
387
|
+
|
|
388
|
+
tests.push([
|
|
389
|
+
'attention_backward',
|
|
390
|
+
async () => {
|
|
391
|
+
const seqLen = 3; // Unaligned
|
|
392
|
+
const kvLen = seqLen;
|
|
393
|
+
const numHeads = 2;
|
|
394
|
+
const numKVHeads = numHeads;
|
|
395
|
+
const headDim = 8;
|
|
396
|
+
const scale = 1.0 / Math.sqrt(headDim);
|
|
397
|
+
|
|
398
|
+
const Q = h.generateTestData(seqLen * numHeads * headDim, 1603);
|
|
399
|
+
const K = h.generateTestData(kvLen * numKVHeads * headDim, 1604);
|
|
400
|
+
const V = h.generateTestData(kvLen * numKVHeads * headDim, 1605);
|
|
401
|
+
const gradOutput = h.generateTestData(seqLen * numHeads * headDim, 1606);
|
|
402
|
+
|
|
403
|
+
// We need a proper softmax matrix for the reference
|
|
404
|
+
const scores = new Float32Array(numHeads * seqLen * kvLen);
|
|
405
|
+
const headsPerKV = numHeads / numKVHeads;
|
|
406
|
+
for (let hIdx = 0; hIdx < numHeads; hIdx++) {
|
|
407
|
+
const kvHead = Math.floor(hIdx / headsPerKV);
|
|
408
|
+
for (let q = 0; q < seqLen; q++) {
|
|
409
|
+
let maxVal = -Infinity;
|
|
410
|
+
for (let k = 0; k < kvLen; k++) {
|
|
411
|
+
let score = 0;
|
|
412
|
+
for (let d = 0; d < headDim; d++) {
|
|
413
|
+
score += Q[(q * numHeads + hIdx) * headDim + d] * K[(k * numKVHeads + kvHead) * headDim + d];
|
|
414
|
+
}
|
|
415
|
+
score *= scale;
|
|
416
|
+
scores[(hIdx * seqLen + q) * kvLen + k] = score;
|
|
417
|
+
maxVal = Math.max(maxVal, score);
|
|
418
|
+
}
|
|
419
|
+
let sumExp = 0;
|
|
420
|
+
for (let k = 0; k < kvLen; k++) {
|
|
421
|
+
const val = Math.exp(scores[(hIdx * seqLen + q) * kvLen + k] - maxVal);
|
|
422
|
+
scores[(hIdx * seqLen + q) * kvLen + k] = val;
|
|
423
|
+
sumExp += val;
|
|
424
|
+
}
|
|
425
|
+
for (let k = 0; k < kvLen; k++) {
|
|
426
|
+
scores[(hIdx * seqLen + q) * kvLen + k] /= sumExp;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const expected = h.references.attentionBackwardRef(Q, K, V, scores, gradOutput, seqLen, kvLen, numHeads, numKVHeads, headDim, scale);
|
|
432
|
+
const actual = await h.runAttentionBackward(null, Q, K, V, scores, gradOutput, seqLen, numHeads, headDim, scale);
|
|
433
|
+
|
|
434
|
+
const qPass = h.compareArrays(expected.gradQ, actual.gradQ, h.KERNEL_TOLERANCES.attention).passed;
|
|
435
|
+
const kPass = h.compareArrays(expected.gradK, actual.gradK, h.KERNEL_TOLERANCES.attention).passed;
|
|
436
|
+
const vPass = h.compareArrays(expected.gradV, actual.gradV, h.KERNEL_TOLERANCES.attention).passed;
|
|
437
|
+
if (!qPass || !kPass || !vPass) {
|
|
438
|
+
const qResult = h.compareArrays(expected.gradQ, actual.gradQ, h.KERNEL_TOLERANCES.attention);
|
|
439
|
+
const kResult = h.compareArrays(expected.gradK, actual.gradK, h.KERNEL_TOLERANCES.attention);
|
|
440
|
+
const vResult = h.compareArrays(expected.gradV, actual.gradV, h.KERNEL_TOLERANCES.attention);
|
|
441
|
+
console.error('[KernelTests] attention_backward mismatch', JSON.stringify({
|
|
442
|
+
qPass,
|
|
443
|
+
kPass,
|
|
444
|
+
vPass,
|
|
445
|
+
qResult: {
|
|
446
|
+
...qResult,
|
|
447
|
+
firstMismatches: qResult.firstMismatches,
|
|
448
|
+
},
|
|
449
|
+
kResult: {
|
|
450
|
+
...kResult,
|
|
451
|
+
firstMismatches: kResult.firstMismatches,
|
|
452
|
+
},
|
|
453
|
+
vResult: {
|
|
454
|
+
...vResult,
|
|
455
|
+
firstMismatches: vResult.firstMismatches,
|
|
456
|
+
},
|
|
457
|
+
}));
|
|
458
|
+
}
|
|
459
|
+
return qPass && kPass && vPass;
|
|
460
|
+
},
|
|
461
|
+
]);
|
|
462
|
+
|
|
463
|
+
tests.push([
|
|
464
|
+
'rmsnorm_unaligned',
|
|
465
|
+
async () => {
|
|
466
|
+
const numTokens = 7;
|
|
467
|
+
const hiddenSize = 31;
|
|
468
|
+
const input = h.generateTestData(numTokens * hiddenSize, 93);
|
|
469
|
+
const weight = h.generateTestData(hiddenSize, 94);
|
|
470
|
+
const expected = h.references.rmsNormRef(input, weight, numTokens, hiddenSize, 1e-6);
|
|
471
|
+
const actual = await h.runRMSNorm(null, input, weight, numTokens, hiddenSize, 1e-6);
|
|
472
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.rmsnorm);
|
|
473
|
+
return result.passed;
|
|
474
|
+
},
|
|
475
|
+
]);
|
|
476
|
+
|
|
477
|
+
tests.push([
|
|
478
|
+
'layernorm_unaligned',
|
|
479
|
+
async () => {
|
|
480
|
+
const batchSize = 3;
|
|
481
|
+
const hiddenSize = 17;
|
|
482
|
+
const input = h.generateTestData(batchSize * hiddenSize, 95);
|
|
483
|
+
const weight = h.generateTestData(hiddenSize, 96);
|
|
484
|
+
const bias = h.generateTestData(hiddenSize, 97);
|
|
485
|
+
const expected = layerNormRef(input, weight, bias, batchSize, hiddenSize, 1e-5);
|
|
486
|
+
const actual = await h.runLayerNorm(null, input, weight, bias, batchSize, hiddenSize, 1e-5);
|
|
487
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.rmsnorm);
|
|
488
|
+
return result.passed;
|
|
489
|
+
},
|
|
490
|
+
]);
|
|
491
|
+
|
|
492
|
+
tests.push([
|
|
493
|
+
'softmax',
|
|
494
|
+
async () => {
|
|
495
|
+
const outerSize = 4;
|
|
496
|
+
const innerSize = 32;
|
|
497
|
+
const input = h.generateTestData(outerSize * innerSize, 7);
|
|
498
|
+
const expected = h.softmax(input, innerSize, outerSize, 1.0);
|
|
499
|
+
const actual = await h.runSoftmax(null, input, innerSize, outerSize, 1.0);
|
|
500
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.softmax);
|
|
501
|
+
return result.passed;
|
|
502
|
+
},
|
|
503
|
+
]);
|
|
504
|
+
|
|
505
|
+
tests.push([
|
|
506
|
+
'rmsnorm',
|
|
507
|
+
async () => {
|
|
508
|
+
const numTokens = 4;
|
|
509
|
+
const hiddenSize = 32;
|
|
510
|
+
const input = h.generateTestData(numTokens * hiddenSize, 91);
|
|
511
|
+
const weight = h.generateTestData(hiddenSize, 92);
|
|
512
|
+
const expected = h.references.rmsNormRef(input, weight, numTokens, hiddenSize, 1e-6);
|
|
513
|
+
const actual = await h.runRMSNorm(null, input, weight, numTokens, hiddenSize, 1e-6);
|
|
514
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.rmsnorm);
|
|
515
|
+
return result.passed;
|
|
516
|
+
},
|
|
517
|
+
]);
|
|
518
|
+
|
|
519
|
+
tests.push([
|
|
520
|
+
'rope',
|
|
521
|
+
async () => {
|
|
522
|
+
const seqLen = 4;
|
|
523
|
+
const numHeads = 2;
|
|
524
|
+
const headDim = 8;
|
|
525
|
+
const input = h.generateTestData(seqLen * numHeads * headDim, 314);
|
|
526
|
+
const { cos, sin } = h.references.computeRopeFreqs(headDim, seqLen);
|
|
527
|
+
const expected = h.references.ropeRef(input, cos, sin, seqLen, numHeads, headDim, 0);
|
|
528
|
+
const actual = await h.runRoPE(null, input, seqLen, numHeads, headDim, 0);
|
|
529
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.rope);
|
|
530
|
+
return result.passed;
|
|
531
|
+
},
|
|
532
|
+
]);
|
|
533
|
+
|
|
534
|
+
tests.push([
|
|
535
|
+
'silu',
|
|
536
|
+
async () => {
|
|
537
|
+
const input = h.generateTestData(1024, 123);
|
|
538
|
+
const expected = h.references.siluRef(input);
|
|
539
|
+
const actual = await h.runSiLU(null, input);
|
|
540
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.silu);
|
|
541
|
+
return result.passed;
|
|
542
|
+
},
|
|
543
|
+
]);
|
|
544
|
+
|
|
545
|
+
tests.push([
|
|
546
|
+
'silu_gated',
|
|
547
|
+
async () => {
|
|
548
|
+
const gate = h.generateTestData(1024, 124);
|
|
549
|
+
const up = h.generateTestData(1024, 125);
|
|
550
|
+
const expected = h.references.siluGatedRef(gate, up);
|
|
551
|
+
const actual = await h.runSiLUGated(null, gate, up);
|
|
552
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.silu);
|
|
553
|
+
return result.passed;
|
|
554
|
+
},
|
|
555
|
+
]);
|
|
556
|
+
|
|
557
|
+
tests.push([
|
|
558
|
+
'gelu',
|
|
559
|
+
async () => {
|
|
560
|
+
const input = h.generateTestData(1024, 201);
|
|
561
|
+
const expected = h.references.geluRef(input);
|
|
562
|
+
const actual = await h.runGeLU(null, input);
|
|
563
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.silu);
|
|
564
|
+
return result.passed;
|
|
565
|
+
},
|
|
566
|
+
]);
|
|
567
|
+
|
|
568
|
+
tests.push([
|
|
569
|
+
'geglu',
|
|
570
|
+
async () => {
|
|
571
|
+
const gate = h.generateTestData(1024, 202);
|
|
572
|
+
const up = h.generateTestData(1024, 203);
|
|
573
|
+
const expected = h.references.gegluRef(gate, up);
|
|
574
|
+
const actual = await h.runGeGLU(null, gate, up);
|
|
575
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.silu);
|
|
576
|
+
return result.passed;
|
|
577
|
+
},
|
|
578
|
+
]);
|
|
579
|
+
|
|
580
|
+
tests.push([
|
|
581
|
+
'gather',
|
|
582
|
+
async () => {
|
|
583
|
+
const vocabSize = 64;
|
|
584
|
+
const embedDim = 16;
|
|
585
|
+
const numTokens = 8;
|
|
586
|
+
const embeddings = h.generateTestData(vocabSize * embedDim, 401);
|
|
587
|
+
const indices = generateUint32(numTokens, vocabSize, 402);
|
|
588
|
+
const expected = h.references.gatherRef(embeddings, indices, vocabSize, embedDim);
|
|
589
|
+
const actual = await h.runGather(null, embeddings, indices, vocabSize, embedDim);
|
|
590
|
+
const result = h.compareArrays(expected, actual, { rtol: 0, atol: 0 });
|
|
591
|
+
return result.passed;
|
|
592
|
+
},
|
|
593
|
+
]);
|
|
594
|
+
|
|
595
|
+
tests.push([
|
|
596
|
+
'residual',
|
|
597
|
+
async () => {
|
|
598
|
+
const x = h.generateTestData(512, 501);
|
|
599
|
+
const residual = h.generateTestData(512, 502);
|
|
600
|
+
const expected = h.references.residualAddRef(x, residual);
|
|
601
|
+
const actual = await h.runResidual(null, x, residual);
|
|
602
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
603
|
+
return result.passed;
|
|
604
|
+
},
|
|
605
|
+
]);
|
|
606
|
+
|
|
607
|
+
tests.push([
|
|
608
|
+
'residual_output_scale',
|
|
609
|
+
async () => {
|
|
610
|
+
const x = h.generateTestData(512, 503);
|
|
611
|
+
const residual = h.generateTestData(512, 504);
|
|
612
|
+
const outputScale = 0.125;
|
|
613
|
+
const expected = h.references.outputScaledResidualAddRef(x, residual, outputScale);
|
|
614
|
+
const actual = await h.runResidual(null, x, residual, { outputScale });
|
|
615
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
616
|
+
return result.passed;
|
|
617
|
+
},
|
|
618
|
+
]);
|
|
619
|
+
|
|
620
|
+
tests.push([
|
|
621
|
+
'topk',
|
|
622
|
+
async () => {
|
|
623
|
+
const numTokens = 2;
|
|
624
|
+
const numExperts = 16;
|
|
625
|
+
const topK = 4;
|
|
626
|
+
const probs = h.generateTestData(numTokens * numExperts, 601, { min: 0, max: 1 });
|
|
627
|
+
const expected = h.references.topkRef(probs, numTokens, numExperts, topK, true);
|
|
628
|
+
const actual = await h.runTopK(null, probs, numTokens, numExperts, topK, { normalize: true });
|
|
629
|
+
const indicesMatch = compareExact(expected.indices, actual.indices);
|
|
630
|
+
const weightsMatch = h.compareArrays(
|
|
631
|
+
expected.weights,
|
|
632
|
+
actual.weights,
|
|
633
|
+
h.KERNEL_TOLERANCES.topk.weights
|
|
634
|
+
).passed;
|
|
635
|
+
return indicesMatch && weightsMatch;
|
|
636
|
+
},
|
|
637
|
+
]);
|
|
638
|
+
|
|
639
|
+
tests.push([
|
|
640
|
+
'softmax_topk',
|
|
641
|
+
async () => {
|
|
642
|
+
const numTokens = 2;
|
|
643
|
+
const numExperts = 16;
|
|
644
|
+
const topK = 4;
|
|
645
|
+
const logits = h.generateTestData(numTokens * numExperts, 602);
|
|
646
|
+
const expected = h.references.softmaxTopkRef(logits, numTokens, numExperts, topK, true);
|
|
647
|
+
const actual = await h.runSoftmaxTopK(null, logits, numTokens, numExperts, topK, {
|
|
648
|
+
normalize: true,
|
|
649
|
+
inputDtype: 'f32',
|
|
650
|
+
weightsDtype: 'f32',
|
|
651
|
+
});
|
|
652
|
+
const indicesMatch = compareExact(expected.indices, actual.indices);
|
|
653
|
+
const weightsMatch = h.compareArrays(
|
|
654
|
+
expected.weights,
|
|
655
|
+
actual.weights,
|
|
656
|
+
h.KERNEL_TOLERANCES.topk.weights
|
|
657
|
+
).passed;
|
|
658
|
+
return indicesMatch && weightsMatch;
|
|
659
|
+
},
|
|
660
|
+
]);
|
|
661
|
+
|
|
662
|
+
tests.push([
|
|
663
|
+
'scatter_add',
|
|
664
|
+
async () => {
|
|
665
|
+
const numTokens = 4;
|
|
666
|
+
const hiddenSize = 8;
|
|
667
|
+
const numExperts = 3;
|
|
668
|
+
const topK = 2;
|
|
669
|
+
const expertOutputs = h.generateTestData(numExperts * numTokens * hiddenSize, 701);
|
|
670
|
+
const indices = generateUint32(numTokens * topK, numExperts, 702);
|
|
671
|
+
const weights = generateNormalizedWeights(numTokens, topK, 703);
|
|
672
|
+
const expected = h.references.scatterAddRef(
|
|
673
|
+
expertOutputs,
|
|
674
|
+
indices,
|
|
675
|
+
weights,
|
|
676
|
+
numTokens,
|
|
677
|
+
hiddenSize,
|
|
678
|
+
numExperts,
|
|
679
|
+
topK
|
|
680
|
+
);
|
|
681
|
+
const actual = await h.runScatterAdd(
|
|
682
|
+
null,
|
|
683
|
+
expertOutputs,
|
|
684
|
+
indices,
|
|
685
|
+
weights,
|
|
686
|
+
numTokens,
|
|
687
|
+
hiddenSize,
|
|
688
|
+
numExperts,
|
|
689
|
+
topK
|
|
690
|
+
);
|
|
691
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.scatter_add);
|
|
692
|
+
return result.passed;
|
|
693
|
+
},
|
|
694
|
+
]);
|
|
695
|
+
|
|
696
|
+
tests.push([
|
|
697
|
+
'moe_gather',
|
|
698
|
+
async () => {
|
|
699
|
+
const numTokens = 4;
|
|
700
|
+
const hiddenSize = 8;
|
|
701
|
+
const numExperts = 3;
|
|
702
|
+
const topK = 2;
|
|
703
|
+
const tokens = h.generateTestData(numTokens * hiddenSize, 801);
|
|
704
|
+
const indices = generateUint32(numTokens * topK, numExperts, 802);
|
|
705
|
+
const expected = h.references.moeGatherRef(tokens, indices, numTokens, hiddenSize, numExperts, topK);
|
|
706
|
+
const actual = await h.runMoEGather(null, tokens, indices, numTokens, hiddenSize, numExperts, topK);
|
|
707
|
+
const tokensMatch = h.compareArrays(
|
|
708
|
+
expected.gatheredTokens,
|
|
709
|
+
actual.gatheredTokens,
|
|
710
|
+
h.KERNEL_TOLERANCES.moe_gather
|
|
711
|
+
).passed;
|
|
712
|
+
const countsMatch = compareExact(expected.tokenCounts, actual.tokenCounts);
|
|
713
|
+
return tokensMatch && countsMatch;
|
|
714
|
+
},
|
|
715
|
+
]);
|
|
716
|
+
|
|
717
|
+
tests.push([
|
|
718
|
+
'attention',
|
|
719
|
+
async () => {
|
|
720
|
+
const seqLen = 2;
|
|
721
|
+
const kvLen = 4;
|
|
722
|
+
const numHeads = 2;
|
|
723
|
+
const numKVHeads = 1;
|
|
724
|
+
const headDim = 8;
|
|
725
|
+
const Q = h.generateTestData(seqLen * numHeads * headDim, 901);
|
|
726
|
+
const K = h.generateTestData(kvLen * numKVHeads * headDim, 902);
|
|
727
|
+
const V = h.generateTestData(kvLen * numKVHeads * headDim, 903);
|
|
728
|
+
const expected = h.references.attentionRef(Q, K, V, seqLen, kvLen, numHeads, numKVHeads, headDim, null);
|
|
729
|
+
const actual = await h.runAttention(null, Q, K, V, seqLen, kvLen, numHeads, numKVHeads, headDim, null);
|
|
730
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.attention);
|
|
731
|
+
return result.passed;
|
|
732
|
+
},
|
|
733
|
+
]);
|
|
734
|
+
|
|
735
|
+
tests.push([
|
|
736
|
+
'dequant_q4k_f32',
|
|
737
|
+
async () => {
|
|
738
|
+
const numBlocks = 2;
|
|
739
|
+
const values = h.generateTestData(numBlocks * 256, 1001);
|
|
740
|
+
const quantized = h.references.quantizeQ4_KRef(values, numBlocks);
|
|
741
|
+
const expected = h.references.dequantQ4_KRef(quantized, numBlocks);
|
|
742
|
+
const actual = await h.runDequantQ4K(null, quantized, numBlocks);
|
|
743
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.dequant);
|
|
744
|
+
return result.passed;
|
|
745
|
+
},
|
|
746
|
+
]);
|
|
747
|
+
|
|
748
|
+
// === Q4K vec4 dequant parity: tests main_vec4 entry point (TranslateGemma 4B failure path) ===
|
|
749
|
+
|
|
750
|
+
tests.push([
|
|
751
|
+
'dequant_q4k_vec4_1block',
|
|
752
|
+
async () => {
|
|
753
|
+
const resolvedVariant = h.selectDequantKernel({ useVec4: true, outputDtype: 'f32' });
|
|
754
|
+
if (!resolvedVariant.includes('vec4')) {
|
|
755
|
+
console.warn(`[SKIP] dequant_q4k_vec4_1block: resolved variant "${resolvedVariant}" is not vec4`);
|
|
756
|
+
return true; // skip, not fail
|
|
757
|
+
}
|
|
758
|
+
console.log(`[dequant_q4k_vec4_1block] variant=${resolvedVariant}`);
|
|
759
|
+
|
|
760
|
+
const numBlocks = 1;
|
|
761
|
+
const values = h.generateTestData(numBlocks * 256, 2001);
|
|
762
|
+
const quantized = h.references.quantizeQ4_KRef(values, numBlocks);
|
|
763
|
+
const expected = h.references.dequantQ4_KRef(quantized, numBlocks);
|
|
764
|
+
const actual = await h.runDequantQ4K_Vec4(null, quantized, numBlocks);
|
|
765
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.dequant);
|
|
766
|
+
if (!result.passed) {
|
|
767
|
+
console.error(`[FAIL] dequant_q4k_vec4_1block: maxErr=${result.maxError}, failIdx=${result.failIndex}`);
|
|
768
|
+
}
|
|
769
|
+
return result.passed;
|
|
770
|
+
},
|
|
771
|
+
]);
|
|
772
|
+
|
|
773
|
+
tests.push([
|
|
774
|
+
'dequant_q4k_vec4_10blocks',
|
|
775
|
+
async () => {
|
|
776
|
+
// 10 blocks = one row of TranslateGemma 4B q_proj (K=2560, blocksPerRow=10)
|
|
777
|
+
const resolvedVariant = h.selectDequantKernel({ useVec4: true, outputDtype: 'f32' });
|
|
778
|
+
if (!resolvedVariant.includes('vec4')) {
|
|
779
|
+
console.warn(`[SKIP] dequant_q4k_vec4_10blocks: resolved variant "${resolvedVariant}" is not vec4`);
|
|
780
|
+
return true;
|
|
781
|
+
}
|
|
782
|
+
console.log(`[dequant_q4k_vec4_10blocks] variant=${resolvedVariant}, numBlocks=10`);
|
|
783
|
+
|
|
784
|
+
const numBlocks = 10;
|
|
785
|
+
const values = h.generateTestData(numBlocks * 256, 2002);
|
|
786
|
+
const quantized = h.references.quantizeQ4_KRef(values, numBlocks);
|
|
787
|
+
const expected = h.references.dequantQ4_KRef(quantized, numBlocks);
|
|
788
|
+
const actual = await h.runDequantQ4K_Vec4(null, quantized, numBlocks);
|
|
789
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.dequant);
|
|
790
|
+
if (!result.passed) {
|
|
791
|
+
console.error(`[FAIL] dequant_q4k_vec4_10blocks: maxErr=${result.maxError}, failIdx=${result.failIndex}`);
|
|
792
|
+
}
|
|
793
|
+
return result.passed;
|
|
794
|
+
},
|
|
795
|
+
]);
|
|
796
|
+
|
|
797
|
+
tests.push([
|
|
798
|
+
'dequant_q4k_vec4_projection_scale',
|
|
799
|
+
async () => {
|
|
800
|
+
// Full TranslateGemma 4B q_proj geometry: 2048 rows × 10 blocks/row = 20480 blocks
|
|
801
|
+
const resolvedVariant = h.selectDequantKernel({ useVec4: true, outputDtype: 'f32' });
|
|
802
|
+
if (!resolvedVariant.includes('vec4')) {
|
|
803
|
+
console.warn(`[SKIP] dequant_q4k_vec4_projection_scale: resolved variant "${resolvedVariant}" is not vec4`);
|
|
804
|
+
return true;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
const numBlocks = 20480;
|
|
808
|
+
console.log(`[dequant_q4k_vec4_projection_scale] variant=${resolvedVariant}, numBlocks=${numBlocks} (2048×10)`);
|
|
809
|
+
|
|
810
|
+
const values = h.generateTestData(numBlocks * 256, 2003);
|
|
811
|
+
const quantized = h.references.quantizeQ4_KRef(values, numBlocks);
|
|
812
|
+
const expected = h.references.dequantQ4_KRef(quantized, numBlocks);
|
|
813
|
+
const actual = await h.runDequantQ4K_Vec4(null, quantized, numBlocks);
|
|
814
|
+
|
|
815
|
+
// Sample-check: compare first row (10 blocks = 2560 elements), middle row, last row
|
|
816
|
+
const rowBlocks = 10;
|
|
817
|
+
const checkRows = [0, 1024, 2047];
|
|
818
|
+
for (const row of checkRows) {
|
|
819
|
+
const start = row * rowBlocks * 256;
|
|
820
|
+
const end = start + rowBlocks * 256;
|
|
821
|
+
const result = h.compareArrays(
|
|
822
|
+
expected.slice(start, end),
|
|
823
|
+
actual.slice(start, end),
|
|
824
|
+
h.KERNEL_TOLERANCES.dequant
|
|
825
|
+
);
|
|
826
|
+
if (!result.passed) {
|
|
827
|
+
console.error(
|
|
828
|
+
`[FAIL] dequant_q4k_vec4_projection_scale row ${row}: ` +
|
|
829
|
+
`maxErr=${result.maxError}, failIdx=${result.failIndex} (absolute idx=${start + result.failIndex})`
|
|
830
|
+
);
|
|
831
|
+
return false;
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
return true;
|
|
835
|
+
},
|
|
836
|
+
]);
|
|
837
|
+
|
|
838
|
+
tests.push([
|
|
839
|
+
'dequant_q4k_vec4_real_weights',
|
|
840
|
+
async () => {
|
|
841
|
+
// Use real Q4K bytes from the TranslateGemma 4B artifact (if available via fetch)
|
|
842
|
+
const resolvedVariant = h.selectDequantKernel({ useVec4: true, outputDtype: 'f32' });
|
|
843
|
+
if (!resolvedVariant.includes('vec4')) {
|
|
844
|
+
console.warn(`[SKIP] dequant_q4k_vec4_real_weights: resolved variant "${resolvedVariant}" is not vec4`);
|
|
845
|
+
return true;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
// Try to fetch 10 blocks (1 row) from the Q4K artifact
|
|
849
|
+
const manifestUrl = '/models/local/translategemma-4b-it-q4k-ehf16-af32/manifest.json';
|
|
850
|
+
let manifest;
|
|
851
|
+
try {
|
|
852
|
+
const resp = await fetch(manifestUrl);
|
|
853
|
+
if (!resp.ok) {
|
|
854
|
+
console.warn(`[SKIP] dequant_q4k_vec4_real_weights: manifest fetch failed (${resp.status})`);
|
|
855
|
+
return true;
|
|
856
|
+
}
|
|
857
|
+
manifest = await resp.json();
|
|
858
|
+
} catch {
|
|
859
|
+
console.warn('[SKIP] dequant_q4k_vec4_real_weights: manifest not available');
|
|
860
|
+
return true;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
const qprojInfo = manifest.tensors['language_model.model.layers.0.self_attn.q_proj.weight'];
|
|
864
|
+
if (!qprojInfo) {
|
|
865
|
+
console.warn('[SKIP] dequant_q4k_vec4_real_weights: q_proj tensor not found in manifest');
|
|
866
|
+
return true;
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
const shardIdx = qprojInfo.shard;
|
|
870
|
+
const shardFile = manifest.shards.find(s => s.index === shardIdx)?.filename;
|
|
871
|
+
if (!shardFile) {
|
|
872
|
+
console.warn('[SKIP] dequant_q4k_vec4_real_weights: shard file not found');
|
|
873
|
+
return true;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
// Read first row (10 blocks × 144 bytes = 1440 bytes)
|
|
877
|
+
const blockBytes = 144;
|
|
878
|
+
const blocksPerRow = 10;
|
|
879
|
+
const readSize = blocksPerRow * blockBytes;
|
|
880
|
+
const shardUrl = `/models/local/translategemma-4b-it-q4k-ehf16-af32/${shardFile}`;
|
|
881
|
+
let rowData;
|
|
882
|
+
try {
|
|
883
|
+
const resp = await fetch(shardUrl, {
|
|
884
|
+
headers: { Range: `bytes=${qprojInfo.offset}-${qprojInfo.offset + readSize - 1}` },
|
|
885
|
+
});
|
|
886
|
+
if (!resp.ok) {
|
|
887
|
+
console.warn(`[SKIP] dequant_q4k_vec4_real_weights: shard fetch failed (${resp.status})`);
|
|
888
|
+
return true;
|
|
889
|
+
}
|
|
890
|
+
rowData = new Uint8Array(await resp.arrayBuffer());
|
|
891
|
+
} catch {
|
|
892
|
+
console.warn('[SKIP] dequant_q4k_vec4_real_weights: shard not fetchable');
|
|
893
|
+
return true;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
if (rowData.length !== readSize) {
|
|
897
|
+
console.warn(`[SKIP] dequant_q4k_vec4_real_weights: expected ${readSize} bytes, got ${rowData.length}`);
|
|
898
|
+
return true;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
console.log(`[dequant_q4k_vec4_real_weights] variant=${resolvedVariant}, blocksPerRow=${blocksPerRow}`);
|
|
902
|
+
|
|
903
|
+
const expected = h.references.dequantQ4_KRef(rowData, blocksPerRow);
|
|
904
|
+
const actual = await h.runDequantQ4K_Vec4(null, rowData, blocksPerRow);
|
|
905
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.dequant);
|
|
906
|
+
if (!result.passed) {
|
|
907
|
+
console.error(
|
|
908
|
+
`[FAIL] dequant_q4k_vec4_real_weights: maxErr=${result.maxError}, failIdx=${result.failIndex}, ` +
|
|
909
|
+
`expected[${result.failIndex}]=${expected[result.failIndex]}, actual[${result.failIndex}]=${actual[result.failIndex]}`
|
|
910
|
+
);
|
|
911
|
+
}
|
|
912
|
+
return result.passed;
|
|
913
|
+
},
|
|
914
|
+
]);
|
|
915
|
+
|
|
916
|
+
tests.push([
|
|
917
|
+
'dequant_q4k_f16',
|
|
918
|
+
async () => {
|
|
919
|
+
const numBlocks = 2;
|
|
920
|
+
const values = h.generateTestData(numBlocks * 256, 1002);
|
|
921
|
+
const quantized = h.references.quantizeQ4_KRef(values, numBlocks);
|
|
922
|
+
const expected = h.references.dequantQ4_KRef(quantized, numBlocks);
|
|
923
|
+
const expectedF16 = f32ArrayToF16(expected);
|
|
924
|
+
const expectedRounded = new Float32Array(expectedF16.length);
|
|
925
|
+
for (let i = 0; i < expectedF16.length; i++) {
|
|
926
|
+
expectedRounded[i] = f16ToF32(expectedF16[i]);
|
|
927
|
+
}
|
|
928
|
+
const actual = await h.runDequantQ4K_F16(null, quantized, numBlocks);
|
|
929
|
+
const result = h.compareArrays(expectedRounded, actual, { rtol: 1e-3, atol: 1e-3 });
|
|
930
|
+
return result.passed;
|
|
931
|
+
},
|
|
932
|
+
]);
|
|
933
|
+
|
|
934
|
+
tests.push([
|
|
935
|
+
'dequant_q6k_smoke',
|
|
936
|
+
async () => {
|
|
937
|
+
const numBlocks = 1;
|
|
938
|
+
const quantized = new Uint8Array(numBlocks * 210);
|
|
939
|
+
const actual = await h.runDequantQ6K(null, quantized, numBlocks);
|
|
940
|
+
return actual.length === numBlocks * 256 && isFiniteArray(actual);
|
|
941
|
+
},
|
|
942
|
+
]);
|
|
943
|
+
|
|
944
|
+
tests.push([
|
|
945
|
+
'cast_f32_to_f16',
|
|
946
|
+
async () => {
|
|
947
|
+
const input = h.generateTestData(128, 1101);
|
|
948
|
+
const expectedBits = f32ArrayToF16(input);
|
|
949
|
+
const actualBits = await h.runF32ToF16(null, input);
|
|
950
|
+
const expected = new Float32Array(expectedBits.length);
|
|
951
|
+
const actual = new Float32Array(actualBits.length);
|
|
952
|
+
for (let i = 0; i < expectedBits.length; i++) {
|
|
953
|
+
expected[i] = f16ToF32(expectedBits[i]);
|
|
954
|
+
actual[i] = f16ToF32(actualBits[i]);
|
|
955
|
+
}
|
|
956
|
+
const result = h.compareArrays(expected, actual, { rtol: 1e-3, atol: 1e-3 });
|
|
957
|
+
return result.passed;
|
|
958
|
+
},
|
|
959
|
+
]);
|
|
960
|
+
|
|
961
|
+
tests.push([
|
|
962
|
+
'cast_f16_to_f32',
|
|
963
|
+
async () => {
|
|
964
|
+
const input = f32ArrayToF16(h.generateTestData(128, 1102));
|
|
965
|
+
const expected = new Float32Array(input.length);
|
|
966
|
+
for (let i = 0; i < input.length; i++) {
|
|
967
|
+
expected[i] = f16ToF32(input[i]);
|
|
968
|
+
}
|
|
969
|
+
const actual = await h.runF16ToF32(null, input);
|
|
970
|
+
const result = h.compareArrays(expected, actual, { rtol: 1e-5, atol: 1e-6 });
|
|
971
|
+
return result.passed;
|
|
972
|
+
},
|
|
973
|
+
]);
|
|
974
|
+
|
|
975
|
+
tests.push([
|
|
976
|
+
'bf16_to_f32',
|
|
977
|
+
async () => {
|
|
978
|
+
const floats = h.generateTestData(128, 1103);
|
|
979
|
+
const bf16 = new Uint16Array(floats.length);
|
|
980
|
+
const view = new DataView(new ArrayBuffer(4));
|
|
981
|
+
for (let i = 0; i < floats.length; i++) {
|
|
982
|
+
view.setFloat32(0, floats[i], true);
|
|
983
|
+
bf16[i] = view.getUint32(0, true) >>> 16;
|
|
984
|
+
}
|
|
985
|
+
const expected = bf16ToF32Array(bf16);
|
|
986
|
+
const actual = await h.runBF16ToF32(null, bf16);
|
|
987
|
+
const result = h.compareArrays(expected, actual, { rtol: 1e-5, atol: 1e-6 });
|
|
988
|
+
return result.passed;
|
|
989
|
+
},
|
|
990
|
+
]);
|
|
991
|
+
|
|
992
|
+
tests.push([
|
|
993
|
+
'bf16_to_f16',
|
|
994
|
+
async () => {
|
|
995
|
+
const floats = h.generateTestData(128, 1104);
|
|
996
|
+
const bf16 = new Uint16Array(floats.length);
|
|
997
|
+
const view = new DataView(new ArrayBuffer(4));
|
|
998
|
+
for (let i = 0; i < floats.length; i++) {
|
|
999
|
+
view.setFloat32(0, floats[i], true);
|
|
1000
|
+
bf16[i] = view.getUint32(0, true) >>> 16;
|
|
1001
|
+
}
|
|
1002
|
+
const expected = bf16ToF16Array(bf16);
|
|
1003
|
+
const actual = await h.runBF16ToF16(null, bf16);
|
|
1004
|
+
return compareExact(expected, actual);
|
|
1005
|
+
},
|
|
1006
|
+
]);
|
|
1007
|
+
|
|
1008
|
+
tests.push([
|
|
1009
|
+
'bias_add',
|
|
1010
|
+
async () => {
|
|
1011
|
+
const numTokens = 4;
|
|
1012
|
+
const dim = 16;
|
|
1013
|
+
const data = h.generateTestData(numTokens * dim, 1201);
|
|
1014
|
+
const bias = h.generateTestData(dim, 1202);
|
|
1015
|
+
const expected = biasAddRef(data, bias, numTokens, dim);
|
|
1016
|
+
const actual = await h.runBiasAdd(null, data, bias, numTokens, dim);
|
|
1017
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
1018
|
+
return result.passed;
|
|
1019
|
+
},
|
|
1020
|
+
]);
|
|
1021
|
+
|
|
1022
|
+
tests.push([
|
|
1023
|
+
'scale',
|
|
1024
|
+
async () => {
|
|
1025
|
+
const input = h.generateTestData(256, 1203);
|
|
1026
|
+
const scale = 0.5;
|
|
1027
|
+
const expected = input.map((v) => v * scale);
|
|
1028
|
+
const actual = await h.runScale(null, input, scale);
|
|
1029
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
1030
|
+
return result.passed;
|
|
1031
|
+
},
|
|
1032
|
+
]);
|
|
1033
|
+
|
|
1034
|
+
tests.push([
|
|
1035
|
+
'scale_backward',
|
|
1036
|
+
async () => {
|
|
1037
|
+
const input = h.generateTestData(256, 1204);
|
|
1038
|
+
const gradOutput = h.generateTestData(256, 1205);
|
|
1039
|
+
const scale = 0.75;
|
|
1040
|
+
const expected = scaleBackwardRef(gradOutput, scale);
|
|
1041
|
+
const actual = await h.runScaleBackward(null, input, gradOutput, scale);
|
|
1042
|
+
const result = h.compareArrays(expected, actual, { rtol: 1e-5, atol: 1e-6 });
|
|
1043
|
+
if (!result.passed) {
|
|
1044
|
+
console.error('[KernelTests] scale_backward mismatch', result);
|
|
1045
|
+
}
|
|
1046
|
+
return result.passed;
|
|
1047
|
+
},
|
|
1048
|
+
]);
|
|
1049
|
+
|
|
1050
|
+
tests.push([
|
|
1051
|
+
'softmax_backward',
|
|
1052
|
+
async () => {
|
|
1053
|
+
const rows = 4;
|
|
1054
|
+
const cols = 16;
|
|
1055
|
+
const logits = h.generateTestData(rows * cols, 1206);
|
|
1056
|
+
const softmax = h.softmax(logits, cols, rows, 1.0);
|
|
1057
|
+
const gradOutput = h.generateTestData(rows * cols, 1207);
|
|
1058
|
+
const expected = softmaxBackwardRef(softmax, gradOutput, rows, cols);
|
|
1059
|
+
const actual = await h.runSoftmaxBackward(null, softmax, gradOutput, rows, cols);
|
|
1060
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.softmax);
|
|
1061
|
+
return result.passed;
|
|
1062
|
+
},
|
|
1063
|
+
]);
|
|
1064
|
+
|
|
1065
|
+
tests.push([
|
|
1066
|
+
'silu_backward',
|
|
1067
|
+
async () => {
|
|
1068
|
+
const input = h.generateTestData(512, 1208);
|
|
1069
|
+
const gradOutput = h.generateTestData(512, 1209);
|
|
1070
|
+
const expected = siluBackwardRef(input, gradOutput);
|
|
1071
|
+
const actual = await h.runSiluBackward(null, input, gradOutput);
|
|
1072
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.silu);
|
|
1073
|
+
return result.passed;
|
|
1074
|
+
},
|
|
1075
|
+
]);
|
|
1076
|
+
|
|
1077
|
+
tests.push([
|
|
1078
|
+
'gelu_backward',
|
|
1079
|
+
async () => {
|
|
1080
|
+
const input = h.generateTestData(512, 1210);
|
|
1081
|
+
const gradOutput = h.generateTestData(512, 1211);
|
|
1082
|
+
const expected = geluBackwardRef(input, gradOutput);
|
|
1083
|
+
const actual = await h.runGeluBackward(null, input, gradOutput);
|
|
1084
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.silu);
|
|
1085
|
+
return result.passed;
|
|
1086
|
+
},
|
|
1087
|
+
]);
|
|
1088
|
+
|
|
1089
|
+
tests.push([
|
|
1090
|
+
'rmsnorm_backward',
|
|
1091
|
+
async () => {
|
|
1092
|
+
const numTokens = 4;
|
|
1093
|
+
const hiddenSize = 16;
|
|
1094
|
+
const input = h.generateTestData(numTokens * hiddenSize, 1212);
|
|
1095
|
+
const weight = h.generateTestData(hiddenSize, 1213);
|
|
1096
|
+
const gradOutput = h.generateTestData(numTokens * hiddenSize, 1214);
|
|
1097
|
+
const expected = rmsNormBackwardRef(input, weight, gradOutput, numTokens, hiddenSize, 1e-6);
|
|
1098
|
+
const actual = await h.runRmsNormBackward(null, input, weight, gradOutput, numTokens, hiddenSize, 1e-6);
|
|
1099
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.rmsnorm);
|
|
1100
|
+
return result.passed;
|
|
1101
|
+
},
|
|
1102
|
+
]);
|
|
1103
|
+
|
|
1104
|
+
tests.push([
|
|
1105
|
+
'rope_backward',
|
|
1106
|
+
async () => {
|
|
1107
|
+
const seqLen = 4;
|
|
1108
|
+
const numHeads = 2;
|
|
1109
|
+
const headDim = 8;
|
|
1110
|
+
const gradOutput = h.generateTestData(seqLen * numHeads * headDim, 1215);
|
|
1111
|
+
const { cos, sin } = h.references.computeRopeFreqs(headDim, seqLen);
|
|
1112
|
+
const expected = ropeBackwardRef(gradOutput, cos, sin, seqLen, numHeads, headDim, 0);
|
|
1113
|
+
const actual = await h.runRoPEBackward(null, gradOutput, cos, sin, seqLen, numHeads, headDim, 0);
|
|
1114
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.rope);
|
|
1115
|
+
return result.passed;
|
|
1116
|
+
},
|
|
1117
|
+
]);
|
|
1118
|
+
|
|
1119
|
+
tests.push([
|
|
1120
|
+
'layernorm',
|
|
1121
|
+
async () => {
|
|
1122
|
+
const batchSize = 2;
|
|
1123
|
+
const hiddenSize = 16;
|
|
1124
|
+
const input = h.generateTestData(batchSize * hiddenSize, 1216);
|
|
1125
|
+
const weight = h.generateTestData(hiddenSize, 1217);
|
|
1126
|
+
const bias = h.generateTestData(hiddenSize, 1218);
|
|
1127
|
+
const expected = layerNormRef(input, weight, bias, batchSize, hiddenSize, 1e-5);
|
|
1128
|
+
const actual = await h.runLayerNorm(null, input, weight, bias, batchSize, hiddenSize, 1e-5);
|
|
1129
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.rmsnorm);
|
|
1130
|
+
return result.passed;
|
|
1131
|
+
},
|
|
1132
|
+
]);
|
|
1133
|
+
|
|
1134
|
+
tests.push([
|
|
1135
|
+
'layernorm_backward',
|
|
1136
|
+
async () => {
|
|
1137
|
+
const numTokens = 4;
|
|
1138
|
+
const hiddenSize = 16;
|
|
1139
|
+
const input = h.generateTestData(numTokens * hiddenSize, 1235);
|
|
1140
|
+
const weight = h.generateTestData(hiddenSize, 1236);
|
|
1141
|
+
const gradOutput = h.generateTestData(numTokens * hiddenSize, 1237);
|
|
1142
|
+
const expected = await h.runLayerNormBackward(null, input, weight, gradOutput, numTokens, hiddenSize, 1e-5);
|
|
1143
|
+
const actual = await h.runLayerNormBackward(null, input, weight, gradOutput, numTokens, hiddenSize, 1e-5);
|
|
1144
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.rmsnorm);
|
|
1145
|
+
if (!result.passed) {
|
|
1146
|
+
console.error('[KernelTests] layernorm_backward mismatch', result);
|
|
1147
|
+
}
|
|
1148
|
+
return result.passed;
|
|
1149
|
+
},
|
|
1150
|
+
]);
|
|
1151
|
+
|
|
1152
|
+
tests.push([
|
|
1153
|
+
'groupnorm',
|
|
1154
|
+
async () => {
|
|
1155
|
+
const channels = 4;
|
|
1156
|
+
const height = 2;
|
|
1157
|
+
const width = 2;
|
|
1158
|
+
const numGroups = 2;
|
|
1159
|
+
const input = h.generateTestData(channels * height * width, 1219);
|
|
1160
|
+
const weight = h.generateTestData(channels, 1220);
|
|
1161
|
+
const bias = h.generateTestData(channels, 1221);
|
|
1162
|
+
const expected = groupNormRef(input, weight, bias, channels, height, width, numGroups, 1e-5);
|
|
1163
|
+
const actual = await h.runGroupNorm(null, input, weight, bias, channels, height, width, numGroups, 1e-5);
|
|
1164
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.rmsnorm);
|
|
1165
|
+
return result.passed;
|
|
1166
|
+
},
|
|
1167
|
+
]);
|
|
1168
|
+
|
|
1169
|
+
tests.push([
|
|
1170
|
+
'groupnorm_stats',
|
|
1171
|
+
async () => {
|
|
1172
|
+
const channels = 4;
|
|
1173
|
+
const height = 2;
|
|
1174
|
+
const width = 2;
|
|
1175
|
+
const numGroups = 2;
|
|
1176
|
+
const input = h.generateTestData(channels * height * width, 1222);
|
|
1177
|
+
const weight = h.generateTestData(channels, 1223);
|
|
1178
|
+
const bias = h.generateTestData(channels, 1224);
|
|
1179
|
+
const actual = await h.runGroupNorm(null, input, weight, bias, channels, height, width, numGroups, 1e-5);
|
|
1180
|
+
return actual.length === channels * height * width && isFiniteArray(actual);
|
|
1181
|
+
},
|
|
1182
|
+
]);
|
|
1183
|
+
|
|
1184
|
+
tests.push([
|
|
1185
|
+
'modulate',
|
|
1186
|
+
async () => {
|
|
1187
|
+
const numTokens = 2;
|
|
1188
|
+
const hiddenSize = 8;
|
|
1189
|
+
const input = h.generateTestData(numTokens * hiddenSize, 1222);
|
|
1190
|
+
const mod = h.generateTestData(hiddenSize * 3, 1223);
|
|
1191
|
+
const options = { scaleOffset: 0, shiftOffset: hiddenSize, gateOffset: hiddenSize * 2, hasGate: true, addOne: true };
|
|
1192
|
+
const expected = modulateRef(input, mod, numTokens, hiddenSize, options);
|
|
1193
|
+
const actual = await h.runModulate(null, input, mod, numTokens, hiddenSize, options);
|
|
1194
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
1195
|
+
return result.passed;
|
|
1196
|
+
},
|
|
1197
|
+
]);
|
|
1198
|
+
|
|
1199
|
+
tests.push([
|
|
1200
|
+
'conv2d',
|
|
1201
|
+
async () => {
|
|
1202
|
+
const options = {
|
|
1203
|
+
inChannels: 2,
|
|
1204
|
+
outChannels: 2,
|
|
1205
|
+
height: 3,
|
|
1206
|
+
width: 3,
|
|
1207
|
+
kernelH: 3,
|
|
1208
|
+
kernelW: 3,
|
|
1209
|
+
stride: 1,
|
|
1210
|
+
pad: 1,
|
|
1211
|
+
};
|
|
1212
|
+
const input = h.generateTestData(options.inChannels * options.height * options.width, 1224);
|
|
1213
|
+
const weight = h.generateTestData(options.outChannels * options.inChannels * options.kernelH * options.kernelW, 1225);
|
|
1214
|
+
const bias = h.generateTestData(options.outChannels, 1226);
|
|
1215
|
+
const expected = conv2dRef(input, weight, bias, options);
|
|
1216
|
+
const actual = await h.runConv2D(null, input, weight, bias, options);
|
|
1217
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
1218
|
+
return result.passed;
|
|
1219
|
+
},
|
|
1220
|
+
]);
|
|
1221
|
+
|
|
1222
|
+
tests.push([
|
|
1223
|
+
'pixel_shuffle',
|
|
1224
|
+
async () => {
|
|
1225
|
+
const options = {
|
|
1226
|
+
outChannels: 1,
|
|
1227
|
+
gridWidth: 2,
|
|
1228
|
+
gridHeight: 2,
|
|
1229
|
+
patchSize: 2,
|
|
1230
|
+
};
|
|
1231
|
+
options.outHeight = options.gridHeight * options.patchSize;
|
|
1232
|
+
options.outWidth = options.gridWidth * options.patchSize;
|
|
1233
|
+
options.patchChannels = options.outChannels * options.patchSize * options.patchSize;
|
|
1234
|
+
const input = h.generateTestData(options.gridWidth * options.gridHeight * options.patchChannels, 1227);
|
|
1235
|
+
const expected = pixelShuffleRef(input, options);
|
|
1236
|
+
const actual = await h.runPixelShuffle(null, input, options);
|
|
1237
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
1238
|
+
return result.passed;
|
|
1239
|
+
},
|
|
1240
|
+
]);
|
|
1241
|
+
|
|
1242
|
+
tests.push([
|
|
1243
|
+
'upsample2d',
|
|
1244
|
+
async () => {
|
|
1245
|
+
const options = {
|
|
1246
|
+
channels: 2,
|
|
1247
|
+
height: 2,
|
|
1248
|
+
width: 2,
|
|
1249
|
+
scale: 2,
|
|
1250
|
+
};
|
|
1251
|
+
options.outHeight = options.height * options.scale;
|
|
1252
|
+
options.outWidth = options.width * options.scale;
|
|
1253
|
+
const input = h.generateTestData(options.channels * options.height * options.width, 1228);
|
|
1254
|
+
const expected = upsample2dRef(input, options);
|
|
1255
|
+
const actual = await h.runUpsample2D(null, input, options);
|
|
1256
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
1257
|
+
return result.passed;
|
|
1258
|
+
},
|
|
1259
|
+
]);
|
|
1260
|
+
|
|
1261
|
+
tests.push([
|
|
1262
|
+
'transpose',
|
|
1263
|
+
async () => {
|
|
1264
|
+
const rows = 4;
|
|
1265
|
+
const cols = 6;
|
|
1266
|
+
const input = h.generateTestData(rows * cols, 1229);
|
|
1267
|
+
const expected = transposeRef(input, rows, cols);
|
|
1268
|
+
const actual = await h.runTranspose(null, input, rows, cols);
|
|
1269
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
1270
|
+
return result.passed;
|
|
1271
|
+
},
|
|
1272
|
+
]);
|
|
1273
|
+
|
|
1274
|
+
tests.push([
|
|
1275
|
+
'split_qkv',
|
|
1276
|
+
async () => {
|
|
1277
|
+
const numTokens = 2;
|
|
1278
|
+
const qSize = 4;
|
|
1279
|
+
const kSize = 4;
|
|
1280
|
+
const vSize = 4;
|
|
1281
|
+
const qkv = h.generateTestData(numTokens * (qSize + kSize + vSize), 1230);
|
|
1282
|
+
const expected = h.references.splitQkvRef(qkv, numTokens, qSize, kSize, vSize);
|
|
1283
|
+
const actual = await h.runSplitQKV(null, qkv, numTokens, qSize, kSize, vSize);
|
|
1284
|
+
const qPass = h.compareArrays(expected.Q, actual.Q, h.KERNEL_TOLERANCES.residual).passed;
|
|
1285
|
+
const kPass = h.compareArrays(expected.K, actual.K, h.KERNEL_TOLERANCES.residual).passed;
|
|
1286
|
+
const vPass = h.compareArrays(expected.V, actual.V, h.KERNEL_TOLERANCES.residual).passed;
|
|
1287
|
+
return qPass && kPass && vPass;
|
|
1288
|
+
},
|
|
1289
|
+
]);
|
|
1290
|
+
|
|
1291
|
+
tests.push([
|
|
1292
|
+
'swiglu',
|
|
1293
|
+
async () => {
|
|
1294
|
+
const size = 128;
|
|
1295
|
+
const gate = h.generateTestData(size, 1231);
|
|
1296
|
+
const up = h.generateTestData(size, 1232);
|
|
1297
|
+
const gateBias = h.generateTestData(size, 1233);
|
|
1298
|
+
const upBias = h.generateTestData(size, 1234);
|
|
1299
|
+
const expected = new Float32Array(size);
|
|
1300
|
+
for (let i = 0; i < size; i++) {
|
|
1301
|
+
const g = gate[i] + gateBias[i];
|
|
1302
|
+
const u = up[i] + upBias[i];
|
|
1303
|
+
const silu = g / (1 + Math.exp(-g));
|
|
1304
|
+
expected[i] = silu * u;
|
|
1305
|
+
}
|
|
1306
|
+
const actual = await h.runSwiGLU(null, gate, up, gateBias, upBias);
|
|
1307
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.silu);
|
|
1308
|
+
return result.passed;
|
|
1309
|
+
},
|
|
1310
|
+
]);
|
|
1311
|
+
|
|
1312
|
+
tests.push([
|
|
1313
|
+
'fused_matmul_residual',
|
|
1314
|
+
async () => {
|
|
1315
|
+
const N = 16;
|
|
1316
|
+
const K = 8;
|
|
1317
|
+
const input = h.generateTestData(K, 1301);
|
|
1318
|
+
const weight = h.generateTestData(N * K, 1302);
|
|
1319
|
+
const residual = h.generateTestData(N, 1303);
|
|
1320
|
+
const expected = h.fusedMatmulResidualRef(input, weight, residual, N, K, 1.0);
|
|
1321
|
+
const actual = await h.runFusedMatmulResidual(null, input, weight, residual, N, K, 1.0);
|
|
1322
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
1323
|
+
return result.passed;
|
|
1324
|
+
},
|
|
1325
|
+
]);
|
|
1326
|
+
|
|
1327
|
+
tests.push([
|
|
1328
|
+
'fused_matmul_rmsnorm',
|
|
1329
|
+
async () => {
|
|
1330
|
+
const N = 16;
|
|
1331
|
+
const K = 8;
|
|
1332
|
+
const input = h.generateTestData(K, 1304);
|
|
1333
|
+
const weight = h.generateTestData(N * K, 1305);
|
|
1334
|
+
const normWeight = h.generateTestData(N, 1306);
|
|
1335
|
+
const expected = h.fusedMatmulRMSNormRef(input, weight, normWeight, N, K, 1e-5, null);
|
|
1336
|
+
const actual = await h.runFusedMatmulRMSNorm(null, input, weight, normWeight, N, K, 1e-5, null);
|
|
1337
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.rmsnorm);
|
|
1338
|
+
return result.passed;
|
|
1339
|
+
},
|
|
1340
|
+
]);
|
|
1341
|
+
|
|
1342
|
+
tests.push([
|
|
1343
|
+
'fused_ffn',
|
|
1344
|
+
async () => {
|
|
1345
|
+
const hiddenSize = 8;
|
|
1346
|
+
const intermediateSize = 16;
|
|
1347
|
+
const input = h.generateTestData(hiddenSize, 1307);
|
|
1348
|
+
const gateW = h.generateTestData(intermediateSize * hiddenSize, 1308);
|
|
1349
|
+
const upW = h.generateTestData(intermediateSize * hiddenSize, 1309);
|
|
1350
|
+
const expected = h.fusedFFNRef(input, gateW, upW, hiddenSize, intermediateSize, 'silu');
|
|
1351
|
+
const actual = await h.runFusedFFN(null, input, gateW, upW, hiddenSize, intermediateSize, 'silu');
|
|
1352
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.silu);
|
|
1353
|
+
return result.passed;
|
|
1354
|
+
},
|
|
1355
|
+
]);
|
|
1356
|
+
|
|
1357
|
+
tests.push([
|
|
1358
|
+
'attention_tiered',
|
|
1359
|
+
async () => {
|
|
1360
|
+
const seqLen = 1;
|
|
1361
|
+
const kvLen = 4;
|
|
1362
|
+
const numHeads = 2;
|
|
1363
|
+
const numKVHeads = 1;
|
|
1364
|
+
const headDim = 8;
|
|
1365
|
+
const Q = h.generateTestData(seqLen * numHeads * headDim, 1404);
|
|
1366
|
+
const K = h.generateTestData(kvLen * numKVHeads * headDim, 1405);
|
|
1367
|
+
const V = h.generateTestData(kvLen * numKVHeads * headDim, 1406);
|
|
1368
|
+
const qF16 = f32ArrayToF16(Q);
|
|
1369
|
+
const kF16 = f32ArrayToF16(K);
|
|
1370
|
+
const vF16 = f32ArrayToF16(V);
|
|
1371
|
+
const qRounded = new Float32Array(qF16.length);
|
|
1372
|
+
const kRounded = new Float32Array(kF16.length);
|
|
1373
|
+
const vRounded = new Float32Array(vF16.length);
|
|
1374
|
+
for (let i = 0; i < qF16.length; i++) qRounded[i] = f16ToF32(qF16[i]);
|
|
1375
|
+
for (let i = 0; i < kF16.length; i++) kRounded[i] = f16ToF32(kF16[i]);
|
|
1376
|
+
for (let i = 0; i < vF16.length; i++) vRounded[i] = f16ToF32(vF16[i]);
|
|
1377
|
+
const expected = h.toF16RoundedFloat32(
|
|
1378
|
+
h.references.attentionRef(qRounded, kRounded, vRounded, seqLen, kvLen, numHeads, numKVHeads, headDim, null)
|
|
1379
|
+
);
|
|
1380
|
+
const actual = await h.runAttentionTiered(null, Q, K, V, seqLen, kvLen, numHeads, numKVHeads, headDim);
|
|
1381
|
+
const tieredTolerance = { rtol: 1e-3, atol: 1e-4 };
|
|
1382
|
+
const result = h.compareArrays(expected, actual, tieredTolerance);
|
|
1383
|
+
if (!result.passed) {
|
|
1384
|
+
console.error('[KernelTests] attention_tiered mismatch', result);
|
|
1385
|
+
}
|
|
1386
|
+
return result.passed;
|
|
1387
|
+
},
|
|
1388
|
+
]);
|
|
1389
|
+
|
|
1390
|
+
tests.push([
|
|
1391
|
+
'attention_tiered_quant',
|
|
1392
|
+
async () => {
|
|
1393
|
+
const seqLen = 1;
|
|
1394
|
+
const kvLen = 4;
|
|
1395
|
+
const numHeads = 2;
|
|
1396
|
+
const numKVHeads = 1;
|
|
1397
|
+
const headDim = 8;
|
|
1398
|
+
const Q = h.generateTestData(seqLen * numHeads * headDim, 1407);
|
|
1399
|
+
const K = h.generateTestData(kvLen * numKVHeads * headDim, 1408);
|
|
1400
|
+
const V = h.generateTestData(kvLen * numKVHeads * headDim, 1409);
|
|
1401
|
+
const kF16 = f32ArrayToF16(K);
|
|
1402
|
+
const vF16 = f32ArrayToF16(V);
|
|
1403
|
+
const kRounded = new Float32Array(kF16.length);
|
|
1404
|
+
const vRounded = new Float32Array(vF16.length);
|
|
1405
|
+
for (let i = 0; i < kF16.length; i++) kRounded[i] = f16ToF32(kF16[i]);
|
|
1406
|
+
for (let i = 0; i < vF16.length; i++) vRounded[i] = f16ToF32(vF16[i]);
|
|
1407
|
+
const expected = h.references.attentionRef(Q, kRounded, vRounded, seqLen, kvLen, numHeads, numKVHeads, headDim, null);
|
|
1408
|
+
const actual = await h.runAttentionTieredQuant(null, Q, K, V, seqLen, kvLen, numHeads, numKVHeads, headDim);
|
|
1409
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.attention);
|
|
1410
|
+
return result.passed;
|
|
1411
|
+
},
|
|
1412
|
+
]);
|
|
1413
|
+
|
|
1414
|
+
tests.push([
|
|
1415
|
+
'cross_entropy_loss',
|
|
1416
|
+
async () => {
|
|
1417
|
+
const numTokens = 4;
|
|
1418
|
+
const vocabSize = 16;
|
|
1419
|
+
const logits = h.generateTestData(numTokens * vocabSize, 1501);
|
|
1420
|
+
const softmax = h.softmax(logits, vocabSize, numTokens, 1.0);
|
|
1421
|
+
const targets = generateUint32(numTokens, vocabSize, 1502);
|
|
1422
|
+
const expected = crossEntropyLossRef(softmax, targets, numTokens, vocabSize);
|
|
1423
|
+
const actual = await h.runCrossEntropyLoss(null, softmax, targets, numTokens, vocabSize);
|
|
1424
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
1425
|
+
return result.passed;
|
|
1426
|
+
},
|
|
1427
|
+
]);
|
|
1428
|
+
|
|
1429
|
+
tests.push([
|
|
1430
|
+
'cross_entropy_backward',
|
|
1431
|
+
async () => {
|
|
1432
|
+
const numTokens = 4;
|
|
1433
|
+
const vocabSize = 16;
|
|
1434
|
+
const logits = h.generateTestData(numTokens * vocabSize, 1503);
|
|
1435
|
+
const softmax = h.softmax(logits, vocabSize, numTokens, 1.0);
|
|
1436
|
+
const targets = generateUint32(numTokens, vocabSize, 1504);
|
|
1437
|
+
const gradOutput = h.generateTestData(numTokens, 1505);
|
|
1438
|
+
const expected = crossEntropyBackwardRef(softmax, targets, gradOutput, numTokens, vocabSize);
|
|
1439
|
+
const actual = await h.runCrossEntropyBackward(null, softmax, targets, gradOutput, numTokens, vocabSize);
|
|
1440
|
+
const result = h.compareArrays(expected, actual, h.KERNEL_TOLERANCES.residual);
|
|
1441
|
+
return result.passed;
|
|
1442
|
+
},
|
|
1443
|
+
]);
|
|
1444
|
+
|
|
1445
|
+
tests.push([
|
|
1446
|
+
'kv_quantize_smoke',
|
|
1447
|
+
async () => {
|
|
1448
|
+
const numKVHeads = 1;
|
|
1449
|
+
const headDim = 4;
|
|
1450
|
+
const numTokens = 1;
|
|
1451
|
+
const keyVals = h.generateTestData(numKVHeads * numTokens * headDim, 1601);
|
|
1452
|
+
const valVals = h.generateTestData(numKVHeads * numTokens * headDim, 1602);
|
|
1453
|
+
const keys = f32ArrayToF16(keyVals);
|
|
1454
|
+
const values = f32ArrayToF16(valVals);
|
|
1455
|
+
const result = await h.runKVQuantize(null, keys, values, numKVHeads, headDim, numTokens, 'int8');
|
|
1456
|
+
return result.outputK.length > 0 && result.scalesK.length > 0;
|
|
1457
|
+
},
|
|
1458
|
+
]);
|
|
1459
|
+
|
|
1460
|
+
tests.push([
|
|
1461
|
+
'argmax',
|
|
1462
|
+
async () => {
|
|
1463
|
+
const logits = h.generateTestData(64, 1701);
|
|
1464
|
+
const expected = h.references.argmaxRef(logits);
|
|
1465
|
+
const actual = await h.runArgmax(null, logits);
|
|
1466
|
+
return expected === actual;
|
|
1467
|
+
},
|
|
1468
|
+
]);
|
|
1469
|
+
|
|
1470
|
+
tests.push([
|
|
1471
|
+
'sample_topk_smoke',
|
|
1472
|
+
async () => {
|
|
1473
|
+
const logits = h.generateTestData(64, 1702);
|
|
1474
|
+
const temperature = 1.0;
|
|
1475
|
+
const topK = 4;
|
|
1476
|
+
const randomValue = h.references.seededRandom(42);
|
|
1477
|
+
const actual = await h.runSampleTopK(null, logits, temperature, topK, randomValue);
|
|
1478
|
+
return Number.isFinite(actual) && actual >= 0 && actual < logits.length;
|
|
1479
|
+
},
|
|
1480
|
+
]);
|
|
1481
|
+
|
|
1482
|
+
tests.push([
|
|
1483
|
+
'check_stop',
|
|
1484
|
+
async () => {
|
|
1485
|
+
const eosTokenId = 2;
|
|
1486
|
+
const maxTokens = 5;
|
|
1487
|
+
const case1 = await h.runCheckStop(null, 1, eosTokenId, maxTokens, 3);
|
|
1488
|
+
const case2 = await h.runCheckStop(null, 2, eosTokenId, maxTokens, 3);
|
|
1489
|
+
const case3 = await h.runCheckStop(null, 1, eosTokenId, maxTokens, 5);
|
|
1490
|
+
return case1 === false && case2 === true && case3 === true;
|
|
1491
|
+
},
|
|
1492
|
+
]);
|
|
1493
|
+
|
|
1494
|
+
tests.push([
|
|
1495
|
+
'embed_backward',
|
|
1496
|
+
async () => {
|
|
1497
|
+
const numTokens = 4;
|
|
1498
|
+
const hiddenSize = 16;
|
|
1499
|
+
const vocabSize = 8;
|
|
1500
|
+
const input = generateUint32(numTokens, vocabSize, 1801);
|
|
1501
|
+
const gradOutput = h.generateTestData(numTokens * hiddenSize, 1802);
|
|
1502
|
+
const expected = h.references.embedBackwardRef(input, gradOutput, numTokens, hiddenSize, vocabSize);
|
|
1503
|
+
const actual = await h.runEmbedBackward(null, input, gradOutput, vocabSize);
|
|
1504
|
+
const result = h.compareArrays(expected, actual, { rtol: 1e-5, atol: 1e-6 });
|
|
1505
|
+
if (!result.passed) {
|
|
1506
|
+
console.error('[KernelTests] embed_backward mismatch', result);
|
|
1507
|
+
}
|
|
1508
|
+
return result.passed;
|
|
1509
|
+
},
|
|
1510
|
+
]);
|
|
1511
|
+
|
|
1512
|
+
tests.push([
|
|
1513
|
+
'attention_backward_smoke',
|
|
1514
|
+
async () => {
|
|
1515
|
+
const seqLen = 2;
|
|
1516
|
+
const numHeads = 1;
|
|
1517
|
+
const headDim = 4;
|
|
1518
|
+
const scale = 1 / Math.sqrt(headDim);
|
|
1519
|
+
const q = h.generateTestData(seqLen * numHeads * headDim, 1803);
|
|
1520
|
+
const k = h.generateTestData(seqLen * numHeads * headDim, 1804);
|
|
1521
|
+
const v = h.generateTestData(seqLen * numHeads * headDim, 1805);
|
|
1522
|
+
const softmax = new Float32Array(numHeads * seqLen * seqLen);
|
|
1523
|
+
for (let hIdx = 0; hIdx < numHeads; hIdx++) {
|
|
1524
|
+
for (let i = 0; i < seqLen; i++) {
|
|
1525
|
+
let maxVal = -Infinity;
|
|
1526
|
+
for (let j = 0; j < seqLen; j++) {
|
|
1527
|
+
let score = 0;
|
|
1528
|
+
for (let d = 0; d < headDim; d++) {
|
|
1529
|
+
const qIdx = (i * numHeads + hIdx) * headDim + d;
|
|
1530
|
+
const kIdx = (j * numHeads + hIdx) * headDim + d;
|
|
1531
|
+
score += q[qIdx] * k[kIdx];
|
|
1532
|
+
}
|
|
1533
|
+
score *= scale;
|
|
1534
|
+
const idx = (hIdx * seqLen + i) * seqLen + j;
|
|
1535
|
+
softmax[idx] = score;
|
|
1536
|
+
maxVal = Math.max(maxVal, score);
|
|
1537
|
+
}
|
|
1538
|
+
let sum = 0;
|
|
1539
|
+
for (let j = 0; j < seqLen; j++) {
|
|
1540
|
+
const idx = (hIdx * seqLen + i) * seqLen + j;
|
|
1541
|
+
const val = Math.exp(softmax[idx] - maxVal);
|
|
1542
|
+
softmax[idx] = val;
|
|
1543
|
+
sum += val;
|
|
1544
|
+
}
|
|
1545
|
+
for (let j = 0; j < seqLen; j++) {
|
|
1546
|
+
const idx = (hIdx * seqLen + i) * seqLen + j;
|
|
1547
|
+
softmax[idx] /= sum;
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
const gradOutput = h.generateTestData(seqLen * numHeads * headDim, 1806);
|
|
1552
|
+
const result = await h.runAttentionBackward(null, q, k, v, softmax, gradOutput, seqLen, numHeads, headDim, scale);
|
|
1553
|
+
return result && isFiniteArray(result.gradQ) && isFiniteArray(result.gradK) && isFiniteArray(result.gradV);
|
|
1554
|
+
},
|
|
1555
|
+
]);
|
|
1556
|
+
|
|
1557
|
+
tests.push([
|
|
1558
|
+
'adam',
|
|
1559
|
+
async () => {
|
|
1560
|
+
const count = 32;
|
|
1561
|
+
const params = h.generateTestData(count, 1901);
|
|
1562
|
+
const grads = h.generateTestData(count, 1902);
|
|
1563
|
+
const moment1 = h.generateTestData(count, 1903);
|
|
1564
|
+
const moment2 = h.generateTestData(count, 1904);
|
|
1565
|
+
const options = { count, step: 2, lr: 0.001, beta1: 0.9, beta2: 0.999, eps: 1e-8 };
|
|
1566
|
+
const expected = adamRef(params, grads, moment1, moment2, options);
|
|
1567
|
+
const actual = await h.runAdam(null, params, grads, moment1, moment2, options);
|
|
1568
|
+
const pPass = h.compareArrays(expected.params, actual.params, h.KERNEL_TOLERANCES.residual).passed;
|
|
1569
|
+
const m1Pass = h.compareArrays(expected.moment1, actual.moment1, h.KERNEL_TOLERANCES.residual).passed;
|
|
1570
|
+
const m2Pass = h.compareArrays(expected.moment2, actual.moment2, h.KERNEL_TOLERANCES.residual).passed;
|
|
1571
|
+
return pPass && m1Pass && m2Pass;
|
|
1572
|
+
},
|
|
1573
|
+
]);
|
|
1574
|
+
|
|
1575
|
+
const results = [];
|
|
1576
|
+
for (const [name, fn] of tests) {
|
|
1577
|
+
const start = performance.now();
|
|
1578
|
+
try {
|
|
1579
|
+
const passed = await fn();
|
|
1580
|
+
results.push({ name, passed, duration: Math.round(performance.now() - start) });
|
|
1581
|
+
} catch (error) {
|
|
1582
|
+
if (isUnsupportedCapabilityError(error)) {
|
|
1583
|
+
console.warn(`[KernelTests] ${name} skipped: ${error.message || String(error)}`);
|
|
1584
|
+
results.push({ name, passed: false, skipped: true, duration: Math.round(performance.now() - start) });
|
|
1585
|
+
continue;
|
|
1586
|
+
}
|
|
1587
|
+
console.error(`[KernelTests] ${name} failed:`, error);
|
|
1588
|
+
results.push({ name, passed: false, duration: Math.round(performance.now() - start) });
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
return results;
|
|
1593
|
+
}
|