doppler-gpu 0.2.0 → 0.2.1

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.
Files changed (607) hide show
  1. package/CHANGELOG.md +21 -1
  2. package/README.md +97 -29
  3. package/package.json +40 -27
  4. package/src/cli/config/doppler-quickstart.json +10 -0
  5. package/src/cli/doppler-cli.js +66 -22
  6. package/src/cli/doppler-quickstart.d.ts +61 -0
  7. package/src/cli/doppler-quickstart.js +299 -0
  8. package/src/cli/doppler-serve.js +395 -0
  9. package/src/client/doppler-api.browser.js +13 -377
  10. package/src/client/doppler-api.d.ts +35 -56
  11. package/src/client/doppler-api.js +12 -363
  12. package/src/client/doppler-registry.json +55 -2
  13. package/src/client/runtime/index.d.ts +63 -0
  14. package/src/client/runtime/index.js +173 -0
  15. package/src/client/runtime/lora.d.ts +36 -0
  16. package/src/client/runtime/lora.js +165 -0
  17. package/src/client/{doppler-provider → runtime}/model-manager.d.ts +1 -1
  18. package/src/client/{doppler-provider → runtime}/model-manager.js +56 -30
  19. package/src/client/runtime/model-session.d.ts +72 -0
  20. package/src/client/runtime/model-session.js +122 -0
  21. package/src/client/runtime/model-source.d.ts +43 -0
  22. package/src/client/runtime/model-source.js +101 -0
  23. package/src/client/{doppler-provider → runtime}/source-runtime.d.ts +8 -5
  24. package/src/client/runtime/source-runtime.js +919 -0
  25. package/src/client/runtime/storage.d.ts +5 -0
  26. package/src/client/runtime/storage.js +51 -0
  27. package/src/client/{doppler-provider → runtime}/types.d.ts +14 -2
  28. package/src/config/README.md +2 -0
  29. package/src/config/conversion/README.md +47 -7
  30. package/src/config/conversion/embeddinggemma/google-embeddinggemma-300m-q4k-ehf16-af32.json +67 -5
  31. package/src/config/conversion/gemma3/gemma-3-1b-it-f16-af32.json +62 -11
  32. package/src/config/conversion/gemma3/gemma-3-1b-it-q4k-ehf16-af32.json +79 -18
  33. package/src/config/conversion/gemma3/gemma-3-270m-it-q4k-ehf16-af32.json +61 -13
  34. package/src/config/conversion/gemma3/translategemma-4b-1b-enes-q4k-ehf16-af32.json +53 -10
  35. package/src/config/conversion/gemma3/translategemma-4b-it-q4k-ehf16-af32.json +66 -18
  36. package/src/config/conversion/gemma4/gemma-4-e2b-it-q4k-ehf16-af32.json +256 -0
  37. package/src/config/conversion/gemma4/gemma-4-moe-q4k-ehf16-af32.json +49 -8
  38. package/src/config/conversion/gpt-oss-20b-f16-xmxfp4.json +45 -6
  39. package/src/config/conversion/janus/janus-pro-1b-text-q4k-ehaf16.json +45 -6
  40. package/src/config/conversion/lfm2/lfm2.5-1.2b-instruct-q4k-ehf16-af32.json +57 -9
  41. package/src/config/conversion/qwen3/qwen-3-5-0-8b-q4k-ehaf16.json +105 -26
  42. package/src/config/conversion/qwen3/qwen-3-5-2b-q4k-ehaf16.json +83 -33
  43. package/src/config/conversion/sana/sana-sprint-0.6b-f16.json +33 -5
  44. package/src/config/execution-contract-check.d.ts +2 -1
  45. package/src/config/execution-contract-check.js +187 -53
  46. package/src/config/kernel-path-loader.d.ts +36 -0
  47. package/src/config/kernel-path-loader.js +97 -10
  48. package/src/config/kernels/kernel-ref-digests.js +40 -24
  49. package/src/config/kernels/registry.js +0 -1
  50. package/src/config/kernels/registry.json +772 -66
  51. package/src/config/merge-contract-check.js +3 -0
  52. package/src/config/merge.d.ts +18 -1
  53. package/src/config/merge.js +56 -0
  54. package/src/config/param-categories.js +5 -0
  55. package/src/config/param-validator.js +42 -26
  56. package/src/config/platforms/apple-m3.json +1 -1
  57. package/src/config/platforms/loader.js +21 -16
  58. package/src/config/required-inference-fields-contract-check.js +16 -0
  59. package/src/config/runtime/compute/f16-batched.json +2 -0
  60. package/src/config/runtime/default.json +1 -0
  61. package/src/config/runtime/experiments/bench/gemma3-bench-q4k-alt.json +57 -0
  62. package/src/config/runtime/experiments/bench/gemma3-bench-q4k-batched.json +57 -0
  63. package/src/config/runtime/experiments/bench/gemma3-bench-q4k.json +2 -0
  64. package/src/config/runtime/experiments/bench/qwen3-bench-q4k-alt.json +57 -0
  65. package/src/config/runtime/experiments/bench/qwen3-bench-q4k-batched.json +57 -0
  66. package/src/config/runtime/experiments/debug/gemma3-browser-diagnose-q4k.json +81 -0
  67. package/src/config/runtime/experiments/debug/gemma3-debug-q4k.json +5 -0
  68. package/src/config/runtime/experiments/debug/qwen35-browser-diagnose-q4k.json +81 -0
  69. package/src/config/runtime/experiments/verify/gemma4-e2b-verify.json +38 -0
  70. package/src/config/runtime/profiles/gemma4-e2b-low-memory.json +63 -0
  71. package/src/config/runtime/profiles/gemma4-e2b-throughput.json +39 -0
  72. package/src/config/runtime/profiles/low-memory.json +1 -0
  73. package/src/config/runtime/profiles/turboquant-contiguous-prod.json +29 -0
  74. package/src/config/runtime/profiles/turboquant-contiguous.json +29 -0
  75. package/src/config/runtime/profiles/turboquant.json +36 -0
  76. package/src/config/runtime/profiles/vector-stability.json +1 -0
  77. package/src/config/runtime/tiers/gemma4-128gb.json +79 -0
  78. package/src/config/runtime/tiers/gemma4-16gb.json +15 -2
  79. package/src/config/runtime/tiers/gemma4-24gb.json +15 -2
  80. package/src/config/runtime/tiers/gemma4-32gb.json +15 -2
  81. package/src/config/runtime.js +11 -4
  82. package/src/config/schema/converter.schema.d.ts +4 -2
  83. package/src/config/schema/converter.schema.js +7 -5
  84. package/src/config/schema/doppler.schema.d.ts +26 -2
  85. package/src/config/schema/doppler.schema.js +77 -28
  86. package/src/config/schema/execution-v1.schema.d.ts +78 -5
  87. package/src/config/schema/execution-v1.schema.js +62 -1
  88. package/src/config/schema/index.d.ts +22 -29
  89. package/src/config/schema/index.js +15 -17
  90. package/src/config/schema/inference.schema.d.ts +26 -4
  91. package/src/config/schema/kernel-path.schema.d.ts +30 -3
  92. package/src/config/schema/kernel-registry.schema.d.ts +1 -1
  93. package/src/config/schema/kernel-registry.schema.js +1 -0
  94. package/src/config/schema/kvcache.schema.js +7 -0
  95. package/src/config/schema/loading.schema.d.ts +28 -0
  96. package/src/config/schema/loading.schema.js +9 -0
  97. package/src/config/schema/manifest.schema.d.ts +79 -5
  98. package/src/config/schema/manifest.schema.js +7 -1
  99. package/src/config/schema/platform.schema.d.ts +10 -8
  100. package/src/config/schema/speculation-self.schema.js +0 -5
  101. package/src/config/schema/support-tiers.schema.d.ts +53 -0
  102. package/src/config/schema/support-tiers.schema.js +128 -0
  103. package/src/config/source-packages/litert/gemma-4-e2b-it.json +622 -0
  104. package/src/config/source-packages/registry.json +15 -0
  105. package/src/config/support-tiers/subsystems.json +502 -0
  106. package/src/config/transforms/capability-transform-resolver.d.ts +16 -5
  107. package/src/config/transforms/capability-transform-resolver.js +44 -9
  108. package/src/config/transforms/execution-graph-transforms.d.ts +113 -2
  109. package/src/config/transforms/execution-graph-transforms.js +1220 -8
  110. package/src/converter/conversion-plan.js +20 -8
  111. package/src/converter/core.d.ts +48 -1
  112. package/src/converter/core.js +249 -62
  113. package/src/converter/parsers/gguf.d.ts +10 -1
  114. package/src/converter/rope-config.d.ts +1 -0
  115. package/src/converter/rope-config.js +75 -5
  116. package/src/converter/shard-packer.js +2 -2
  117. package/src/debug/tensor.js +13 -1
  118. package/src/{adapters → experimental/adapters}/adapter-manager.d.ts +1 -1
  119. package/src/{adapters → experimental/adapters}/adapter-manager.js +2 -2
  120. package/src/{adapters → experimental/adapters}/adapter-manifest.d.ts +1 -1
  121. package/src/{adapters → experimental/adapters}/adapter-manifest.js +1 -1
  122. package/src/{adapters → experimental/adapters}/adapter-registry.d.ts +1 -1
  123. package/src/{adapters → experimental/adapters}/adapter-registry.js +1 -1
  124. package/src/{adapters → experimental/adapters}/lora-loader.d.ts +1 -1
  125. package/src/{adapters → experimental/adapters}/lora-loader.js +2 -2
  126. package/src/{bridge → experimental/bridge}/extension-client.js +2 -2
  127. package/src/{bridge → experimental/bridge}/node-bridge.js +2 -4
  128. package/src/{bridge → experimental/bridge}/protocol.js +1 -1
  129. package/src/{browser → experimental/browser}/browser-converter.d.ts +4 -4
  130. package/src/{browser → experimental/browser}/browser-converter.js +12 -12
  131. package/src/{browser → experimental/browser}/gguf-importer.d.ts +1 -1
  132. package/src/{browser → experimental/browser}/gguf-importer.js +8 -8
  133. package/src/{browser → experimental/browser}/gguf-parser-browser.d.ts +2 -2
  134. package/src/{browser → experimental/browser}/gguf-parser-browser.js +3 -3
  135. package/src/{browser → experimental/browser}/quantization.d.ts +3 -3
  136. package/src/{browser → experimental/browser}/quantization.js +4 -4
  137. package/src/{browser → experimental/browser}/safetensors-parser-browser.d.ts +3 -3
  138. package/src/{browser → experimental/browser}/safetensors-parser-browser.js +3 -3
  139. package/src/{browser → experimental/browser}/shard-io-browser.d.ts +1 -1
  140. package/src/{browser → experimental/browser}/shard-io-browser.js +2 -2
  141. package/src/{browser → experimental/browser}/tensor-source-download.js +3 -3
  142. package/src/experimental/diffusion/index.d.ts +8 -0
  143. package/src/experimental/diffusion/index.js +8 -0
  144. package/src/{distribution → experimental/distribution}/p2p-transport-contract.js +1 -1
  145. package/src/{distribution → experimental/distribution}/p2p-transport.js +1 -1
  146. package/src/{distribution → experimental/distribution}/shard-delivery.js +5 -5
  147. package/src/experimental/energy/index.d.ts +2 -0
  148. package/src/experimental/energy/index.js +2 -0
  149. package/src/{hotswap → experimental/hotswap}/intent-bundle.d.ts +1 -1
  150. package/src/{hotswap → experimental/hotswap}/intent-bundle.js +2 -2
  151. package/src/{hotswap → experimental/hotswap}/manifest.d.ts +2 -2
  152. package/src/{hotswap → experimental/hotswap}/manifest.js +1 -1
  153. package/src/{hotswap → experimental/hotswap}/model-swap.js +1 -1
  154. package/src/{hotswap → experimental/hotswap}/runtime.d.ts +1 -1
  155. package/src/experimental/orchestration/index.d.ts +12 -0
  156. package/src/experimental/orchestration/index.js +12 -0
  157. package/src/{training → experimental/training}/README.md +1 -1
  158. package/src/{training → experimental/training}/artifacts.d.ts +3 -3
  159. package/src/{training → experimental/training}/artifacts.js +3 -3
  160. package/src/{training → experimental/training}/attention-backward.d.ts +1 -1
  161. package/src/{training → experimental/training}/attention-backward.js +3 -3
  162. package/src/{training → experimental/training}/attention-forward.d.ts +2 -2
  163. package/src/{training → experimental/training}/attention-forward.js +1 -1
  164. package/src/{training → experimental/training}/autograd.d.ts +2 -2
  165. package/src/{training → experimental/training}/autograd.js +7 -7
  166. package/src/{training → experimental/training}/backward-kernels.js +1 -1
  167. package/src/{training → experimental/training}/checkpoint.js +1 -1
  168. package/src/{training → experimental/training}/clip.d.ts +2 -2
  169. package/src/{training → experimental/training}/clip.js +3 -3
  170. package/src/{training → experimental/training}/export.d.ts +2 -2
  171. package/src/{training → experimental/training}/export.js +2 -2
  172. package/src/{training → experimental/training}/lora-pipeline.js +4 -4
  173. package/src/{training → experimental/training}/lora.d.ts +1 -1
  174. package/src/{training → experimental/training}/lora.js +4 -4
  175. package/src/{training → experimental/training}/loss-scaling.d.ts +2 -2
  176. package/src/{training → experimental/training}/loss-scaling.js +3 -3
  177. package/src/{training → experimental/training}/loss.d.ts +2 -2
  178. package/src/{training → experimental/training}/loss.js +1 -1
  179. package/src/{training → experimental/training}/operator-artifacts.js +1 -1
  180. package/src/{training → experimental/training}/optimizer.d.ts +2 -2
  181. package/src/{training → experimental/training}/optimizer.js +3 -3
  182. package/src/{training → experimental/training}/runner.d.ts +2 -2
  183. package/src/{training → experimental/training}/runner.js +5 -5
  184. package/src/{training → experimental/training}/suite.js +16 -16
  185. package/src/{training → experimental/training}/tensor-factory.d.ts +1 -1
  186. package/src/{training → experimental/training}/tensor-factory.js +2 -2
  187. package/src/{training → experimental/training}/trainer.d.ts +3 -3
  188. package/src/{training → experimental/training}/trainer.js +3 -3
  189. package/src/{training → experimental/training}/ul_dataset.d.ts +2 -2
  190. package/src/{training → experimental/training}/ul_dataset.js +1 -1
  191. package/src/{training → experimental/training}/ul_schedule.d.ts +1 -1
  192. package/src/{training → experimental/training}/workloads.js +4 -4
  193. package/src/formats/index.d.ts +24 -0
  194. package/src/formats/index.js +6 -0
  195. package/src/formats/litert/index.d.ts +1 -0
  196. package/src/formats/litert/index.js +1 -0
  197. package/src/formats/litert/types.d.ts +87 -0
  198. package/src/formats/litert/types.js +568 -0
  199. package/src/formats/rdrr/classification.js +16 -8
  200. package/src/formats/rdrr/parsing.d.ts +3 -2
  201. package/src/formats/rdrr/types.d.ts +33 -0
  202. package/src/formats/rdrr/validation.js +16 -5
  203. package/src/formats/tflite/index.d.ts +1 -0
  204. package/src/formats/tflite/index.js +1 -0
  205. package/src/formats/tflite/types.d.ts +84 -0
  206. package/src/formats/tflite/types.js +633 -0
  207. package/src/generation/index.d.ts +16 -14
  208. package/src/generation/index.js +1 -32
  209. package/src/gpu/command-recorder.d.ts +17 -1
  210. package/src/gpu/command-recorder.js +47 -10
  211. package/src/gpu/device.d.ts +3 -1
  212. package/src/gpu/device.js +17 -11
  213. package/src/gpu/kernel-selector.js +3 -1
  214. package/src/gpu/kernels/attention.d.ts +2 -0
  215. package/src/gpu/kernels/attention.js +359 -14
  216. package/src/gpu/kernels/attention.wgsl +20 -1
  217. package/src/gpu/kernels/attention_decode_contiguous_turboquant_f16kv.wgsl +180 -0
  218. package/src/gpu/kernels/attention_decode_contiguous_turboquant_prod_f16kv.wgsl +192 -0
  219. package/src/gpu/kernels/attention_decode_online_f16.wgsl +36 -18
  220. package/src/gpu/kernels/attention_decode_online_f16kv.wgsl +36 -18
  221. package/src/gpu/kernels/attention_decode_tiered_turboquant_f16kv.wgsl +269 -0
  222. package/src/gpu/kernels/attention_decode_tiered_turboquant_prod_f16kv.wgsl +281 -0
  223. package/src/gpu/kernels/attention_head256_f16kv.wgsl +269 -0
  224. package/src/gpu/kernels/attention_streaming.wgsl +22 -3
  225. package/src/gpu/kernels/attention_streaming_f16.wgsl +1 -1
  226. package/src/gpu/kernels/attention_streaming_f16kv.wgsl +1 -1
  227. package/src/gpu/kernels/backward/attention_backward.d.ts +14 -1
  228. package/src/gpu/kernels/check-hot-vocab-stop.d.ts +18 -0
  229. package/src/gpu/kernels/check-hot-vocab-stop.js +108 -0
  230. package/src/gpu/kernels/codegen/patches/attention_streaming_f16.from.attention_streaming.diff +1 -1
  231. package/src/gpu/kernels/codegen/patches/attention_streaming_f16kv.from.attention_streaming.diff +1 -1
  232. package/src/gpu/kernels/codegen/patches/rope_f16.from.rope.diff +126 -194
  233. package/src/gpu/kernels/codegen/wgsl-variants.js +24 -6
  234. package/src/gpu/kernels/depthwise_conv1d.d.ts +23 -0
  235. package/src/gpu/kernels/depthwise_conv1d.js +51 -0
  236. package/src/gpu/kernels/depthwise_conv1d.wgsl +46 -0
  237. package/src/gpu/kernels/feature-check.js +0 -1
  238. package/src/gpu/kernels/fused_ffn.js +67 -22
  239. package/src/gpu/kernels/fused_ffn_q4k.wgsl +48 -43
  240. package/src/gpu/kernels/fused_ffn_q4k_f16.wgsl +329 -0
  241. package/src/gpu/kernels/fused_matmul_q4.wgsl +417 -0
  242. package/src/gpu/kernels/fused_matmul_q4_batched_multicol_shared.wgsl +174 -0
  243. package/src/gpu/kernels/gather.d.ts +4 -0
  244. package/src/gpu/kernels/gather.js +13 -2
  245. package/src/gpu/kernels/gather.wgsl +5 -3
  246. package/src/gpu/kernels/gather_f16.wgsl +5 -3
  247. package/src/gpu/kernels/index.d.ts +7 -0
  248. package/src/gpu/kernels/index.js +6 -0
  249. package/src/gpu/kernels/kernel-configs.js +1 -0
  250. package/src/gpu/kernels/kv-quantize.js +87 -55
  251. package/src/gpu/kernels/kv_quantize_turboquant.wgsl +153 -0
  252. package/src/gpu/kernels/kv_quantize_turboquant_prod.wgsl +230 -0
  253. package/src/gpu/kernels/linear-attention-core.d.ts +3 -0
  254. package/src/gpu/kernels/linear-attention-core.js +145 -56
  255. package/src/gpu/kernels/matmul-dispatch.js +3 -0
  256. package/src/gpu/kernels/matmul-selection.js +31 -42
  257. package/src/gpu/kernels/matmul.d.ts +3 -0
  258. package/src/gpu/kernels/matmul.js +72 -34
  259. package/src/gpu/kernels/residual.d.ts +2 -0
  260. package/src/gpu/kernels/residual.js +43 -4
  261. package/src/gpu/kernels/rope.d.ts +2 -0
  262. package/src/gpu/kernels/rope.js +19 -6
  263. package/src/gpu/kernels/rope.wgsl +24 -12
  264. package/src/gpu/kernels/rope_f16.wgsl +97 -38
  265. package/src/gpu/kernels/scale.d.ts +5 -0
  266. package/src/gpu/kernels/scale.js +6 -1
  267. package/src/gpu/kernels/scale.wgsl +3 -1
  268. package/src/gpu/kernels/scale_f16.wgsl +32 -0
  269. package/src/gpu/kernels/turboquant-codebook.js +564 -0
  270. package/src/gpu/kernels/uniform-utils.js +26 -0
  271. package/src/gpu/kernels/utils.js +6 -1
  272. package/src/gpu/submit-probe.d.ts +12 -0
  273. package/src/gpu/submit-probe.js +77 -0
  274. package/src/gpu/weight-buffer.d.ts +26 -3
  275. package/src/gpu/weight-buffer.js +62 -2
  276. package/src/index-browser.d.ts +1 -91
  277. package/src/index-browser.js +0 -66
  278. package/src/index.d.ts +1 -102
  279. package/src/index.js +0 -74
  280. package/src/inference/browser-harness-model-helpers.d.ts +3 -0
  281. package/src/inference/browser-harness-model-helpers.js +134 -11
  282. package/src/inference/browser-harness-runtime-helpers.js +1 -9
  283. package/src/inference/browser-harness-text-helpers.js +441 -70
  284. package/src/inference/browser-harness.js +61 -4
  285. package/src/inference/decode-ring.d.ts +2 -0
  286. package/src/inference/decode-ring.js +20 -0
  287. package/src/inference/kv-cache/index.d.ts +3 -0
  288. package/src/inference/kv-cache/index.js +2 -0
  289. package/src/inference/kv-cache/mixed-geometry.d.ts +84 -0
  290. package/src/inference/kv-cache/mixed-geometry.js +422 -0
  291. package/src/inference/kv-cache/quantized.d.ts +56 -0
  292. package/src/inference/kv-cache/quantized.js +383 -0
  293. package/src/inference/kv-cache/tiered.d.ts +20 -3
  294. package/src/inference/kv-cache/tiered.js +203 -65
  295. package/src/inference/kv-cache/types.d.ts +49 -6
  296. package/src/inference/kv-cache.d.ts +3 -0
  297. package/src/inference/kv-cache.js +2 -0
  298. package/src/inference/multi-model-network.js +2 -1
  299. package/src/inference/pipelines/audio/gemma4.js +655 -0
  300. package/src/inference/pipelines/audio/index.js +39 -0
  301. package/src/inference/pipelines/audio/mel.js +171 -0
  302. package/src/inference/pipelines/context.js +9 -1
  303. package/src/inference/pipelines/diffusion/text-encoder-gpu.js +2 -0
  304. package/src/inference/pipelines/shared/clipped-linear.js +46 -0
  305. package/src/inference/pipelines/text/attention/dispatch-params.d.ts +2 -1
  306. package/src/inference/pipelines/text/attention/dispatch-params.js +52 -8
  307. package/src/inference/pipelines/text/attention/output-projection.js +3 -3
  308. package/src/inference/pipelines/text/attention/precision-contract.d.ts +36 -0
  309. package/src/inference/pipelines/text/attention/precision-contract.js +115 -0
  310. package/src/inference/pipelines/text/attention/projections.d.ts +23 -0
  311. package/src/inference/pipelines/text/attention/projections.js +182 -15
  312. package/src/inference/pipelines/text/attention/quant-options.js +54 -0
  313. package/src/inference/pipelines/text/attention/record.js +257 -69
  314. package/src/inference/pipelines/text/attention/run.js +306 -94
  315. package/src/inference/pipelines/text/attention/types.d.ts +11 -0
  316. package/src/inference/pipelines/text/chat-format.d.ts +8 -1
  317. package/src/inference/pipelines/text/chat-format.js +89 -5
  318. package/src/inference/pipelines/text/config.d.ts +78 -26
  319. package/src/inference/pipelines/text/config.js +722 -28
  320. package/src/inference/pipelines/text/dtype-contract.d.ts +15 -0
  321. package/src/inference/pipelines/text/dtype-contract.js +38 -0
  322. package/src/inference/pipelines/text/embed.d.ts +4 -0
  323. package/src/inference/pipelines/text/embed.js +165 -172
  324. package/src/inference/pipelines/text/execution-plan.d.ts +19 -0
  325. package/src/inference/pipelines/text/execution-plan.js +112 -20
  326. package/src/inference/pipelines/text/execution-runtime-builders.d.ts +8 -4
  327. package/src/inference/pipelines/text/execution-runtime-builders.js +90 -49
  328. package/src/inference/pipelines/text/execution-v1.d.ts +23 -4
  329. package/src/inference/pipelines/text/execution-v1.js +411 -33
  330. package/src/inference/pipelines/text/ffn/dense.d.ts +59 -0
  331. package/src/inference/pipelines/text/ffn/dense.js +575 -63
  332. package/src/inference/pipelines/text/ffn/moe.js +1 -0
  333. package/src/inference/pipelines/text/ffn/sandwich.js +20 -4
  334. package/src/inference/pipelines/text/ffn/standard.js +28 -4
  335. package/src/inference/pipelines/text/finiteness-policy.d.ts +4 -0
  336. package/src/inference/pipelines/text/finiteness-policy.js +14 -11
  337. package/src/inference/pipelines/text/generator-helpers.d.ts +7 -0
  338. package/src/inference/pipelines/text/generator-helpers.js +39 -4
  339. package/src/inference/pipelines/text/generator-runtime.js +45 -8
  340. package/src/inference/pipelines/text/generator-steps.js +407 -75
  341. package/src/inference/pipelines/text/generator.d.ts +6 -0
  342. package/src/inference/pipelines/text/generator.js +1179 -305
  343. package/src/inference/pipelines/text/init.d.ts +39 -7
  344. package/src/inference/pipelines/text/init.js +525 -110
  345. package/src/inference/pipelines/text/layer-plan.d.ts +1 -0
  346. package/src/inference/pipelines/text/layer-plan.js +2 -0
  347. package/src/inference/pipelines/text/layer.js +402 -19
  348. package/src/inference/pipelines/text/linear-attention.d.ts +4 -0
  349. package/src/inference/pipelines/text/linear-attention.js +112 -41
  350. package/src/inference/pipelines/text/logits/gpu.d.ts +2 -1
  351. package/src/inference/pipelines/text/logits/gpu.js +334 -15
  352. package/src/inference/pipelines/text/logits/index.d.ts +11 -0
  353. package/src/inference/pipelines/text/logits/index.js +227 -29
  354. package/src/inference/pipelines/text/logits/types.d.ts +3 -0
  355. package/src/inference/pipelines/text/logits/utils.js +3 -1
  356. package/src/inference/pipelines/text/lora-apply.js +3 -3
  357. package/src/inference/pipelines/text/model-load.d.ts +2 -1
  358. package/src/inference/pipelines/text/model-load.js +205 -178
  359. package/src/inference/pipelines/text/moe-gpu.js +8 -0
  360. package/src/inference/pipelines/text/moe-impl.d.ts +2 -0
  361. package/src/inference/pipelines/text/ops.d.ts +16 -2
  362. package/src/inference/pipelines/text/ops.js +11 -2
  363. package/src/inference/pipelines/text/per-layer-inputs.d.ts +107 -0
  364. package/src/inference/pipelines/text/per-layer-inputs.js +1581 -0
  365. package/src/inference/pipelines/text/probes.js +6 -0
  366. package/src/inference/pipelines/text/sampling-config.js +9 -1
  367. package/src/inference/pipelines/text/stage-names.js +3 -0
  368. package/src/inference/pipelines/text/state.d.ts +12 -3
  369. package/src/inference/pipelines/text/state.js +19 -0
  370. package/src/inference/pipelines/text/types.d.ts +79 -7
  371. package/src/inference/pipelines/text/weights.d.ts +2 -2
  372. package/src/inference/pipelines/text/weights.js +4 -0
  373. package/src/inference/pipelines/text.d.ts +6 -0
  374. package/src/inference/pipelines/text.js +860 -11
  375. package/src/inference/pipelines/video/frame-sampler.js +36 -0
  376. package/src/inference/pipelines/video/index.js +87 -0
  377. package/src/inference/pipelines/vision/encoder.js +27 -6
  378. package/src/inference/pipelines/vision/gemma4.js +731 -0
  379. package/src/inference/pipelines/vision/image-preprocess.js +22 -7
  380. package/src/inference/pipelines/vision/index.js +35 -10
  381. package/src/inference/pipelines/vision/ops.js +31 -26
  382. package/src/inference/pipelines/vision/patch-embed.js +22 -10
  383. package/src/inference/runtime-model.d.ts +89 -0
  384. package/src/inference/runtime-model.js +152 -0
  385. package/src/inference/test-harness.d.ts +8 -1
  386. package/src/inference/test-harness.js +54 -20
  387. package/src/inference/tokenizer.d.ts +5 -0
  388. package/src/inference/tokenizer.js +43 -0
  389. package/src/inference/tokenizers/base.d.ts +5 -0
  390. package/src/inference/tokenizers/base.js +5 -0
  391. package/src/inference/tokenizers/behavior-flags.d.ts +9 -0
  392. package/src/inference/tokenizers/behavior-flags.js +100 -0
  393. package/src/inference/tokenizers/bundled.d.ts +4 -0
  394. package/src/inference/tokenizers/bundled.js +110 -0
  395. package/src/inference/tokenizers/sentencepiece.d.ts +2 -0
  396. package/src/inference/tokenizers/sentencepiece.js +16 -0
  397. package/src/inference/tokenizers/types.d.ts +2 -0
  398. package/src/loader/doppler-loader.d.ts +10 -3
  399. package/src/loader/doppler-loader.js +103 -5
  400. package/src/loader/embedding-loader.d.ts +5 -1
  401. package/src/loader/embedding-loader.js +57 -6
  402. package/src/loader/final-weights-loader.js +44 -5
  403. package/src/loader/layer-loader.js +210 -8
  404. package/src/loader/loader-state.js +2 -2
  405. package/src/loader/loader-types.d.ts +17 -1
  406. package/src/loader/memory-monitor.d.ts +8 -0
  407. package/src/loader/memory-monitor.js +21 -0
  408. package/src/loader/multi-model-loader.d.ts +9 -1
  409. package/src/loader/multi-model-loader.js +9 -1
  410. package/src/loader/per-layer-input-loader.d.ts +30 -0
  411. package/src/loader/per-layer-input-loader.js +478 -0
  412. package/src/loader/shard-cache.d.ts +4 -3
  413. package/src/loader/shard-resolver.js +2 -0
  414. package/src/loader/tensors/source-transform.d.ts +50 -0
  415. package/src/loader/tensors/source-transform.js +776 -0
  416. package/src/loader/tensors/tensor-loader.d.ts +12 -0
  417. package/src/loader/tensors/tensor-loader.js +208 -123
  418. package/src/loader/tensors/tensor-reader.d.ts +8 -0
  419. package/src/loader/tensors/tensor-reader.js +283 -15
  420. package/src/loaders/index.d.ts +4 -0
  421. package/src/loaders/index.js +4 -0
  422. package/src/memory/buffer-pool.d.ts +4 -0
  423. package/src/memory/buffer-pool.js +36 -0
  424. package/src/rules/execution-rules-contract-check.d.ts +4 -0
  425. package/src/rules/execution-rules-contract-check.js +150 -24
  426. package/src/rules/inference/attention.rules.json +15 -0
  427. package/src/rules/inference/capability-transforms.rules.json +30 -0
  428. package/src/rules/inference/execution.rules.json +54 -2
  429. package/src/rules/inference/ffn.rules.json +33 -0
  430. package/src/rules/kernels/attention.rules.json +7 -0
  431. package/src/rules/kernels/fused-ffn.rules.json +4 -2
  432. package/src/rules/kernels/kv_quantize.rules.json +2 -0
  433. package/src/rules/kernels/matmul.rules.json +14 -0
  434. package/src/rules/kernels/scale.rules.json +2 -0
  435. package/src/rules/loader/tensor-loader.rules.json +1 -0
  436. package/src/storage/artifact-storage-context.d.ts +53 -0
  437. package/src/storage/artifact-storage-context.js +560 -0
  438. package/src/storage/downloader.js +7 -1
  439. package/src/storage/index.d.ts +18 -0
  440. package/src/storage/index.js +13 -0
  441. package/src/storage/preflight.js +22 -0
  442. package/src/storage/quickstart-downloader.js +35 -2
  443. package/src/storage/registry.d.ts +1 -0
  444. package/src/storage/registry.js +26 -4
  445. package/src/storage/shard-manager.d.ts +4 -0
  446. package/src/storage/shard-manager.js +25 -6
  447. package/src/storage/source-artifact-store.d.ts +9 -0
  448. package/src/storage/source-artifact-store.js +134 -0
  449. package/src/tooling/browser-command-runner.js +12 -5
  450. package/src/tooling/command-api-family-normalizers.js +144 -0
  451. package/src/tooling/command-api-helpers.js +1 -0
  452. package/src/tooling/command-api.d.ts +32 -9
  453. package/src/tooling/command-api.js +0 -25
  454. package/src/tooling/command-runner-shared.js +1 -2
  455. package/src/tooling/command-runner.html +5 -2
  456. package/src/tooling/diagnose-runner.js +1 -1
  457. package/src/tooling/kernel-path-builder/index.js +48 -32
  458. package/src/tooling/lean-execution-contract.d.ts +1 -1
  459. package/src/tooling/lean-execution-contract.js +2 -149
  460. package/src/tooling/litert-package-runtime.d.ts +46 -0
  461. package/src/tooling/litert-package-runtime.js +950 -0
  462. package/src/tooling/node-browser-command-runner.d.ts +13 -0
  463. package/src/tooling/node-browser-command-runner.js +163 -3
  464. package/src/tooling/node-command-runner.js +14 -5
  465. package/src/tooling/node-converter.js +226 -63
  466. package/src/tooling/node-source-runtime.d.ts +8 -3
  467. package/src/tooling/node-source-runtime.js +561 -236
  468. package/src/tooling/node-webgpu.js +7 -30
  469. package/src/tooling/opfs-cache.d.ts +18 -1
  470. package/src/tooling/opfs-cache.js +126 -9
  471. package/src/tooling/runtime-input-composition.d.ts +0 -1
  472. package/src/tooling/runtime-input-composition.js +0 -5
  473. package/src/tooling/source-artifact-adapter.d.ts +134 -0
  474. package/src/tooling/source-artifact-adapter.js +366 -0
  475. package/src/tooling/source-package-profiles.d.ts +33 -0
  476. package/src/tooling/source-package-profiles.js +57 -0
  477. package/src/tooling/source-runtime-bundle.d.ts +46 -6
  478. package/src/tooling/source-runtime-bundle.js +371 -60
  479. package/src/tooling/source-runtime-converter-config.d.ts +24 -0
  480. package/src/tooling/source-runtime-converter-config.js +297 -0
  481. package/src/tooling-experimental-exports.browser.js +2 -0
  482. package/src/tooling-experimental-exports.d.ts +7 -0
  483. package/src/tooling-experimental-exports.js +7 -0
  484. package/src/tooling-experimental-exports.shared.d.ts +26 -0
  485. package/src/tooling-experimental-exports.shared.js +29 -0
  486. package/src/tooling-exports.shared.d.ts +0 -23
  487. package/src/tooling-exports.shared.js +6 -28
  488. package/src/version.js +1 -1
  489. package/src/client/doppler-provider/generation.d.ts +0 -25
  490. package/src/client/doppler-provider/generation.js +0 -126
  491. package/src/client/doppler-provider/index.d.ts +0 -2
  492. package/src/client/doppler-provider/index.js +0 -3
  493. package/src/client/doppler-provider/provider.d.ts +0 -5
  494. package/src/client/doppler-provider/provider.js +0 -102
  495. package/src/client/doppler-provider/source-runtime.js +0 -677
  496. package/src/client/doppler-provider.d.ts +0 -46
  497. package/src/client/doppler-provider.js +0 -36
  498. package/src/config/schema/inference-defaults.schema.d.ts +0 -276
  499. package/src/config/schema/inference-defaults.schema.js +0 -189
  500. package/src/diffusion/index.d.ts +0 -8
  501. package/src/diffusion/index.js +0 -8
  502. package/src/energy/index.d.ts +0 -2
  503. package/src/energy/index.js +0 -2
  504. package/src/index-internal.d.ts +0 -2
  505. package/src/index-internal.js +0 -2
  506. /package/src/client/{doppler-provider → runtime}/types.js +0 -0
  507. /package/src/{adapters → experimental/adapters}/index.d.ts +0 -0
  508. /package/src/{adapters → experimental/adapters}/index.js +0 -0
  509. /package/src/{bridge → experimental/bridge}/extension/background.d.ts +0 -0
  510. /package/src/{bridge → experimental/bridge}/extension/background.js +0 -0
  511. /package/src/{bridge → experimental/bridge}/extension/manifest.json +0 -0
  512. /package/src/{bridge → experimental/bridge}/extension-client.d.ts +0 -0
  513. /package/src/{bridge → experimental/bridge}/index.d.ts +0 -0
  514. /package/src/{bridge → experimental/bridge}/index.js +0 -0
  515. /package/src/{bridge → experimental/bridge}/protocol.d.ts +0 -0
  516. /package/src/{browser → experimental/browser}/file-picker.d.ts +0 -0
  517. /package/src/{browser → experimental/browser}/file-picker.js +0 -0
  518. /package/src/{browser → experimental/browser}/tensor-source-download.d.ts +0 -0
  519. /package/src/{browser → experimental/browser}/tensor-source-file.d.ts +0 -0
  520. /package/src/{browser → experimental/browser}/tensor-source-file.js +0 -0
  521. /package/src/{browser → experimental/browser}/tensor-source-http.d.ts +0 -0
  522. /package/src/{browser → experimental/browser}/tensor-source-http.js +0 -0
  523. /package/src/{diffusion → experimental/diffusion}/config/expansion-lanes.json +0 -0
  524. /package/src/{diffusion → experimental/diffusion}/image-regression.d.ts +0 -0
  525. /package/src/{diffusion → experimental/diffusion}/image-regression.js +0 -0
  526. /package/src/{distribution → experimental/distribution}/p2p-control-plane.d.ts +0 -0
  527. /package/src/{distribution → experimental/distribution}/p2p-control-plane.js +0 -0
  528. /package/src/{distribution → experimental/distribution}/p2p-observability.d.ts +0 -0
  529. /package/src/{distribution → experimental/distribution}/p2p-observability.js +0 -0
  530. /package/src/{distribution → experimental/distribution}/p2p-transport-contract.d.ts +0 -0
  531. /package/src/{distribution → experimental/distribution}/p2p-webrtc-browser.d.ts +0 -0
  532. /package/src/{distribution → experimental/distribution}/p2p-webrtc-browser.js +0 -0
  533. /package/src/{distribution → experimental/distribution}/shard-delivery.d.ts +0 -0
  534. /package/src/{hotswap → experimental/hotswap}/runtime.js +0 -0
  535. /package/src/{training → experimental/training}/checkpoint-watch.d.ts +0 -0
  536. /package/src/{training → experimental/training}/checkpoint-watch.js +0 -0
  537. /package/src/{training → experimental/training}/checkpoint.d.ts +0 -0
  538. /package/src/{training → experimental/training}/dataloader.d.ts +0 -0
  539. /package/src/{training → experimental/training}/dataloader.js +0 -0
  540. /package/src/{training → experimental/training}/datasets/index.d.ts +0 -0
  541. /package/src/{training → experimental/training}/datasets/index.js +0 -0
  542. /package/src/{training → experimental/training}/datasets/jsonl.d.ts +0 -0
  543. /package/src/{training → experimental/training}/datasets/jsonl.js +0 -0
  544. /package/src/{training → experimental/training}/datasets/reploid.d.ts +0 -0
  545. /package/src/{training → experimental/training}/datasets/reploid.js +0 -0
  546. /package/src/{training → experimental/training}/datasets/text-pairs.d.ts +0 -0
  547. /package/src/{training → experimental/training}/datasets/text-pairs.js +0 -0
  548. /package/src/{training → experimental/training}/datasets/token-batch.d.ts +0 -0
  549. /package/src/{training → experimental/training}/datasets/token-batch.js +0 -0
  550. /package/src/{training → experimental/training}/datasets/translation-pairs.d.ts +0 -0
  551. /package/src/{training → experimental/training}/datasets/translation-pairs.js +0 -0
  552. /package/src/{training → experimental/training}/distillation/artifacts.d.ts +0 -0
  553. /package/src/{training → experimental/training}/distillation/artifacts.js +0 -0
  554. /package/src/{training → experimental/training}/distillation/checkpoint-watch.d.ts +0 -0
  555. /package/src/{training → experimental/training}/distillation/checkpoint-watch.js +0 -0
  556. /package/src/{training → experimental/training}/distillation/dataset.d.ts +0 -0
  557. /package/src/{training → experimental/training}/distillation/dataset.js +0 -0
  558. /package/src/{training → experimental/training}/distillation/eval.d.ts +0 -0
  559. /package/src/{training → experimental/training}/distillation/eval.js +0 -0
  560. /package/src/{training → experimental/training}/distillation/index.d.ts +0 -0
  561. /package/src/{training → experimental/training}/distillation/index.js +0 -0
  562. /package/src/{training → experimental/training}/distillation/runtime.d.ts +0 -0
  563. /package/src/{training → experimental/training}/distillation/runtime.js +0 -0
  564. /package/src/{training → experimental/training}/distillation/scoreboard.d.ts +0 -0
  565. /package/src/{training → experimental/training}/distillation/scoreboard.js +0 -0
  566. /package/src/{training → experimental/training}/distillation/stage-a.d.ts +0 -0
  567. /package/src/{training → experimental/training}/distillation/stage-a.js +0 -0
  568. /package/src/{training → experimental/training}/distillation/stage-b.d.ts +0 -0
  569. /package/src/{training → experimental/training}/distillation/stage-b.js +0 -0
  570. /package/src/{training → experimental/training}/distillation/student-fixture.d.ts +0 -0
  571. /package/src/{training → experimental/training}/distillation/student-fixture.js +0 -0
  572. /package/src/{training → experimental/training}/distillation/suite-data.d.ts +0 -0
  573. /package/src/{training → experimental/training}/distillation/suite-data.js +0 -0
  574. /package/src/{training → experimental/training}/index.d.ts +0 -0
  575. /package/src/{training → experimental/training}/index.js +0 -0
  576. /package/src/{training → experimental/training}/lora-pipeline.d.ts +0 -0
  577. /package/src/{training → experimental/training}/objectives/base.d.ts +0 -0
  578. /package/src/{training → experimental/training}/objectives/base.js +0 -0
  579. /package/src/{training → experimental/training}/objectives/cross_entropy.d.ts +0 -0
  580. /package/src/{training → experimental/training}/objectives/cross_entropy.js +0 -0
  581. /package/src/{training → experimental/training}/objectives/distill_kd.d.ts +0 -0
  582. /package/src/{training → experimental/training}/objectives/distill_kd.js +0 -0
  583. /package/src/{training → experimental/training}/objectives/distill_triplet.d.ts +0 -0
  584. /package/src/{training → experimental/training}/objectives/distill_triplet.js +0 -0
  585. /package/src/{training → experimental/training}/objectives/index.d.ts +0 -0
  586. /package/src/{training → experimental/training}/objectives/index.js +0 -0
  587. /package/src/{training → experimental/training}/objectives/ul_stage1_joint.d.ts +0 -0
  588. /package/src/{training → experimental/training}/objectives/ul_stage1_joint.js +0 -0
  589. /package/src/{training → experimental/training}/objectives/ul_stage2_base.d.ts +0 -0
  590. /package/src/{training → experimental/training}/objectives/ul_stage2_base.js +0 -0
  591. /package/src/{training → experimental/training}/operator-artifacts.d.ts +0 -0
  592. /package/src/{training → experimental/training}/operator-command.d.ts +0 -0
  593. /package/src/{training → experimental/training}/operator-command.js +0 -0
  594. /package/src/{training → experimental/training}/operator-eval.d.ts +0 -0
  595. /package/src/{training → experimental/training}/operator-eval.js +0 -0
  596. /package/src/{training → experimental/training}/operator-scoreboard.d.ts +0 -0
  597. /package/src/{training → experimental/training}/operator-scoreboard.js +0 -0
  598. /package/src/{training → experimental/training}/suite.d.ts +0 -0
  599. /package/src/{training → experimental/training}/ul_schedule.js +0 -0
  600. /package/src/{training → experimental/training}/workload-packs/distill-translategemma-medium.json +0 -0
  601. /package/src/{training → experimental/training}/workload-packs/distill-translategemma-tiny.json +0 -0
  602. /package/src/{training → experimental/training}/workload-packs/lora-toy-medium.json +0 -0
  603. /package/src/{training → experimental/training}/workload-packs/lora-toy-tiny.json +0 -0
  604. /package/src/{training → experimental/training}/workload-packs/registry.json +0 -0
  605. /package/src/{training → experimental/training}/workload-packs/ul-training-medium.json +0 -0
  606. /package/src/{training → experimental/training}/workload-packs/ul-training-tiny.json +0 -0
  607. /package/src/{training → experimental/training}/workloads.d.ts +0 -0
@@ -0,0 +1,192 @@
1
+ // Contiguous Decode Attention Kernel (TURBOQUANTprod, f32 output)
2
+ //
3
+ // All KV entries use two-stage dequant (MSE + QJL residual).
4
+ // For full-attention models with unbiased inner product requirement.
5
+
6
+ enable f16;
7
+
8
+ const MAX_KV_LEN: u32 = 2048u;
9
+ const MAX_WORKGROUP_SIZE: u32 = 256u;
10
+ const MAX_HEAD_DIM: u32 = 256u;
11
+
12
+ override WORKGROUP_SIZE: u32 = 256u;
13
+ override BIT_WIDTH_MSE: u32 = 3u;
14
+ override PACK_FACTOR_MSE: u32 = 10u;
15
+ override NUM_CENTROIDS_MSE: u32 = 8u;
16
+
17
+ struct Uniforms {
18
+ num_heads: u32,
19
+ num_kv_heads: u32,
20
+ head_dim: u32,
21
+ kv_len: u32,
22
+ seq_len: u32,
23
+ scale: f32,
24
+ is_causal: u32,
25
+ start_pos: u32,
26
+ attn_softcap: f32,
27
+ sliding_window: u32,
28
+ packed_stride_mse: u32,
29
+ packed_stride_residual: u32,
30
+ }
31
+
32
+ @group(0) @binding(0) var<uniform> u: Uniforms;
33
+ @group(0) @binding(1) var<storage, read> Q: array<f32>;
34
+ @group(0) @binding(2) var<storage, read> K_packed_mse: array<u32>;
35
+ @group(0) @binding(3) var<storage, read> V_packed_mse: array<u32>;
36
+ @group(0) @binding(4) var<storage, read> scales_k: array<f16>;
37
+ @group(0) @binding(5) var<storage, read> scales_v: array<f16>;
38
+ @group(0) @binding(6) var<storage, read> residual_k: array<u32>;
39
+ @group(0) @binding(7) var<storage, read> residual_v: array<u32>;
40
+ @group(0) @binding(8) var<storage, read> residual_norms_k: array<f16>;
41
+ @group(0) @binding(9) var<storage, read> residual_norms_v: array<f16>;
42
+ @group(0) @binding(10) var<storage, read_write> output: array<f32>;
43
+ @group(0) @binding(11) var<storage, read> rotation_matrix: array<f32>;
44
+ @group(0) @binding(12) var<storage, read> codebook_centroids: array<f32>;
45
+ @group(0) @binding(13) var<storage, read> qjl_matrix: array<f32>;
46
+
47
+ var<workgroup> shared_scores: array<f32, MAX_KV_LEN>;
48
+ var<workgroup> shared_partial: array<f32, MAX_WORKGROUP_SIZE>;
49
+ var<workgroup> shared_max: f32;
50
+ var<workgroup> shared_sum: f32;
51
+ var<workgroup> shared_acc: array<f32, MAX_HEAD_DIM>;
52
+ var<workgroup> shared_rot_q: array<f32, MAX_HEAD_DIM>;
53
+
54
+ fn get_kv_head_idx(qhi: u32) -> u32 {
55
+ return qhi / (u.num_heads / u.num_kv_heads);
56
+ }
57
+
58
+ fn is_masked(abs_key: u32) -> bool {
59
+ let aq = u.start_pos;
60
+ if (u.is_causal != 0u && abs_key > aq) { return true; }
61
+ if (u.sliding_window > 0u && aq >= u.sliding_window) {
62
+ if (abs_key < aq - u.sliding_window + 1u) { return true; }
63
+ }
64
+ return false;
65
+ }
66
+
67
+ fn unpack_mse(packed: u32, lane: u32) -> u32 {
68
+ return (packed >> (BIT_WIDTH_MSE * lane)) & ((1u << BIT_WIDTH_MSE) - 1u);
69
+ }
70
+
71
+ fn sign_bit(packed: u32, bit: u32) -> f32 {
72
+ return select(-1.0, 1.0, ((packed >> bit) & 1u) == 1u);
73
+ }
74
+
75
+ fn dequant_k(kp: u32, kvhi: u32, tid: u32) -> f32 {
76
+ let pi = tid / PACK_FACTOR_MSE;
77
+ if (pi >= u.packed_stride_mse) { return 0.0; }
78
+ let lane = tid - pi * PACK_FACTOR_MSE;
79
+ let base = (kp * u.num_kv_heads + kvhi) * u.packed_stride_mse + pi;
80
+ let idx = unpack_mse(K_packed_mse[base], lane);
81
+ let norm = f32(scales_k[kp * u.num_kv_heads + kvhi]);
82
+ let mse_val = codebook_centroids[idx] * norm;
83
+
84
+ let rnorm = f32(residual_norms_k[kp * u.num_kv_heads + kvhi]);
85
+ let rb = (kp * u.num_kv_heads + kvhi) * u.packed_stride_residual;
86
+ var res: f32 = 0.0;
87
+ for (var j: u32 = 0u; j < u.head_dim; j++) {
88
+ res += qjl_matrix[j * u.head_dim + tid] * sign_bit(residual_k[rb + j / 32u], j % 32u);
89
+ }
90
+ return mse_val + res * rnorm * norm;
91
+ }
92
+
93
+ fn dequant_v(kp: u32, kvhi: u32, tid: u32) -> f32 {
94
+ let pi = tid / PACK_FACTOR_MSE;
95
+ if (pi >= u.packed_stride_mse) { return 0.0; }
96
+ let lane = tid - pi * PACK_FACTOR_MSE;
97
+ let base = (kp * u.num_kv_heads + kvhi) * u.packed_stride_mse + pi;
98
+ let idx = unpack_mse(V_packed_mse[base], lane);
99
+ let norm = f32(scales_v[kp * u.num_kv_heads + kvhi]);
100
+ let mse_val = codebook_centroids[idx] * norm;
101
+
102
+ let rnorm = f32(residual_norms_v[kp * u.num_kv_heads + kvhi]);
103
+ let rb = (kp * u.num_kv_heads + kvhi) * u.packed_stride_residual;
104
+ var res: f32 = 0.0;
105
+ for (var j: u32 = 0u; j < u.head_dim; j++) {
106
+ res += qjl_matrix[j * u.head_dim + tid] * sign_bit(residual_v[rb + j / 32u], j % 32u);
107
+ }
108
+ return mse_val + res * rnorm * norm;
109
+ }
110
+
111
+ @compute @workgroup_size(WORKGROUP_SIZE, 1, 1)
112
+ fn main(
113
+ @builtin(local_invocation_id) local_id: vec3<u32>,
114
+ @builtin(workgroup_id) workgroup_id: vec3<u32>,
115
+ ) {
116
+ let head_idx = workgroup_id.x;
117
+ let tid = local_id.x;
118
+ let hd = u.head_dim;
119
+ let kvl = u.kv_len;
120
+ let sc = u.scale;
121
+ let kvhi = get_kv_head_idx(head_idx);
122
+
123
+ if (WORKGROUP_SIZE > MAX_WORKGROUP_SIZE || hd > MAX_HEAD_DIM || kvl > MAX_KV_LEN) { return; }
124
+
125
+ let valid = tid < hd;
126
+
127
+ if (valid) {
128
+ var rq: f32 = 0.0;
129
+ let rb = tid * hd;
130
+ for (var j: u32 = 0u; j < hd; j++) {
131
+ rq += rotation_matrix[rb + j] * Q[head_idx * hd + j];
132
+ }
133
+ shared_rot_q[tid] = rq;
134
+ }
135
+ workgroupBarrier();
136
+
137
+ if (valid) { shared_acc[tid] = 0.0; }
138
+
139
+ for (var kp: u32 = 0u; kp < kvl; kp++) {
140
+ var kv: f32 = 0.0;
141
+ if (valid) { kv = dequant_k(kp, kvhi, tid); }
142
+ shared_partial[tid] = select(0.0, shared_rot_q[tid] * kv, valid);
143
+ workgroupBarrier();
144
+
145
+ if (tid == 0u) {
146
+ var dot: f32 = 0.0;
147
+ for (var d: u32 = 0u; d < hd; d++) { dot += shared_partial[d]; }
148
+ var s = dot * sc;
149
+ if (u.attn_softcap > 0.0) { s = tanh(s / u.attn_softcap) * u.attn_softcap; }
150
+ if (is_masked(kp)) { s = -3.402823e+38; }
151
+ shared_scores[kp] = s;
152
+ }
153
+ workgroupBarrier();
154
+ }
155
+
156
+ if (tid == 0u) {
157
+ var m: f32 = -3.402823e+38;
158
+ for (var k: u32 = 0u; k < kvl; k++) { m = max(m, shared_scores[k]); }
159
+ shared_max = m;
160
+ }
161
+ workgroupBarrier();
162
+
163
+ if (tid == 0u) {
164
+ var s: f32 = 0.0;
165
+ for (var k: u32 = 0u; k < kvl; k++) {
166
+ let w = exp(shared_scores[k] - shared_max);
167
+ shared_scores[k] = w;
168
+ s += w;
169
+ }
170
+ shared_sum = s;
171
+ }
172
+ workgroupBarrier();
173
+
174
+ let inv_sum = select(0.0, 1.0 / shared_sum, shared_sum > 0.0);
175
+
176
+ if (valid) {
177
+ var acc: f32 = 0.0;
178
+ for (var si: u32 = 0u; si < kvl; si++) {
179
+ acc += shared_scores[si] * dequant_v(si, kvhi, tid);
180
+ }
181
+ shared_acc[tid] = acc;
182
+ }
183
+ workgroupBarrier();
184
+
185
+ if (valid) {
186
+ var ir: f32 = 0.0;
187
+ for (var j: u32 = 0u; j < hd; j++) {
188
+ ir += rotation_matrix[j * hd + tid] * shared_acc[j];
189
+ }
190
+ output[head_idx * hd + tid] = ir * inv_sum;
191
+ }
192
+ }
@@ -7,6 +7,7 @@ enable subgroups;
7
7
 
8
8
  const MAX_WORKGROUP_SIZE: u32 = 256u;
9
9
  const MAX_SUBGROUPS: u32 = 256u;
10
+ const MAX_HEAD_DIM: u32 = 512u;
10
11
 
11
12
  override WORKGROUP_SIZE: u32 = 256u;
12
13
 
@@ -36,7 +37,7 @@ struct Uniforms {
36
37
  @group(0) @binding(5) var<storage, read> kv_len_buffer: array<u32>;
37
38
  @group(0) @binding(6) var<storage, read> page_table: array<u32>;
38
39
 
39
- var<workgroup> shared_q: array<f32, MAX_WORKGROUP_SIZE>;
40
+ var<workgroup> shared_q: array<f32, MAX_HEAD_DIM>;
40
41
  var<workgroup> shared_scores: array<f32, MAX_WORKGROUP_SIZE>;
41
42
  var<workgroup> sg_max: array<f32, MAX_SUBGROUPS>;
42
43
  var<workgroup> sg_sum: array<f32, MAX_SUBGROUPS>;
@@ -89,15 +90,22 @@ fn main(
89
90
  let tid = local_id.x;
90
91
  let head_dim = u.head_dim;
91
92
  let kv_len = get_kv_len();
93
+ let q_offset = head_idx * head_dim;
94
+ let out_dim0 = tid;
95
+ let out_dim1 = tid + WORKGROUP_SIZE;
96
+ let has_out_dim0 = out_dim0 < head_dim;
97
+ let has_out_dim1 = out_dim1 < head_dim;
92
98
 
93
- if (WORKGROUP_SIZE > MAX_WORKGROUP_SIZE || head_dim > WORKGROUP_SIZE) {
99
+ if (WORKGROUP_SIZE > MAX_WORKGROUP_SIZE || head_dim > MAX_HEAD_DIM) {
94
100
  return;
95
101
  }
96
102
 
97
103
  if (kv_len == 0u) {
98
- if (tid < head_dim) {
99
- let out_offset = head_idx * head_dim + tid;
100
- output[out_offset] = f16(0.0);
104
+ if (has_out_dim0) {
105
+ output[q_offset + out_dim0] = f16(0.0);
106
+ }
107
+ if (has_out_dim1) {
108
+ output[q_offset + out_dim1] = f16(0.0);
101
109
  }
102
110
  return;
103
111
  }
@@ -110,15 +118,18 @@ fn main(
110
118
 
111
119
  let kv_head_idx = get_kv_head_idx(head_idx);
112
120
 
113
- if (tid < head_dim) {
114
- let q_offset = head_idx * head_dim + tid;
115
- shared_q[tid] = f32(Q[q_offset]);
121
+ if (has_out_dim0) {
122
+ shared_q[out_dim0] = f32(Q[q_offset + out_dim0]);
123
+ }
124
+ if (has_out_dim1) {
125
+ shared_q[out_dim1] = f32(Q[q_offset + out_dim1]);
116
126
  }
117
127
  workgroupBarrier();
118
128
 
119
129
  var running_max: f32 = -3.402823e+38;
120
130
  var running_sum: f32 = 0.0;
121
- var out_accum: f32 = 0.0;
131
+ var out_accum0: f32 = 0.0;
132
+ var out_accum1: f32 = 0.0;
122
133
 
123
134
  var start_k: u32 = 0u;
124
135
  if (u.sliding_window > 0u && kv_len > u.sliding_window) {
@@ -200,24 +211,31 @@ fn main(
200
211
  running_sum = running_sum * rescale + global_sum;
201
212
  running_max = new_max;
202
213
 
203
- if (tid < head_dim) {
204
- out_accum = out_accum * rescale;
214
+ if (has_out_dim0 || has_out_dim1) {
215
+ out_accum0 = out_accum0 * rescale;
216
+ out_accum1 = out_accum1 * rescale;
205
217
  let chunk_len = min(WORKGROUP_SIZE, kv_len - k_start);
206
218
  for (var k: u32 = 0u; k < chunk_len; k = k + 1u) {
207
219
  let score_idx = k;
208
220
  let k_pos_inner = k_start + k;
209
221
  let v_idx = get_kv_pos(k_pos_inner);
210
- let v_offset = v_idx * u.num_kv_heads * head_dim + kv_head_idx * head_dim + tid;
211
- let v_val = f32(V[v_offset]);
212
- out_accum = out_accum + shared_scores[score_idx] * v_val;
222
+ let v_base = v_idx * u.num_kv_heads * head_dim + kv_head_idx * head_dim;
223
+ if (has_out_dim0) {
224
+ out_accum0 = out_accum0 + shared_scores[score_idx] * f32(V[v_base + out_dim0]);
225
+ }
226
+ if (has_out_dim1) {
227
+ out_accum1 = out_accum1 + shared_scores[score_idx] * f32(V[v_base + out_dim1]);
228
+ }
213
229
  }
214
230
  }
215
231
  workgroupBarrier();
216
232
  }
217
233
 
218
- if (tid < head_dim) {
219
- let out_offset = head_idx * head_dim + tid;
220
- let inv_sum = select(0.0, 1.0 / running_sum, running_sum > 0.0);
221
- output[out_offset] = f16(out_accum * inv_sum);
234
+ let inv_sum = select(0.0, 1.0 / running_sum, running_sum > 0.0);
235
+ if (has_out_dim0) {
236
+ output[q_offset + out_dim0] = f16(out_accum0 * inv_sum);
237
+ }
238
+ if (has_out_dim1) {
239
+ output[q_offset + out_dim1] = f16(out_accum1 * inv_sum);
222
240
  }
223
241
  }
@@ -9,6 +9,7 @@ enable subgroups;
9
9
 
10
10
  const MAX_WORKGROUP_SIZE: u32 = 256u;
11
11
  const MAX_SUBGROUPS: u32 = 256u;
12
+ const MAX_HEAD_DIM: u32 = 512u;
12
13
 
13
14
  override WORKGROUP_SIZE: u32 = 256u;
14
15
 
@@ -38,7 +39,7 @@ struct Uniforms {
38
39
  @group(0) @binding(5) var<storage, read> kv_len_buffer: array<u32>;
39
40
  @group(0) @binding(6) var<storage, read> page_table: array<u32>;
40
41
 
41
- var<workgroup> shared_q: array<f32, MAX_WORKGROUP_SIZE>;
42
+ var<workgroup> shared_q: array<f32, MAX_HEAD_DIM>;
42
43
  var<workgroup> shared_scores: array<f32, MAX_WORKGROUP_SIZE>;
43
44
  var<workgroup> sg_max: array<f32, MAX_SUBGROUPS>;
44
45
  var<workgroup> sg_sum: array<f32, MAX_SUBGROUPS>;
@@ -91,15 +92,22 @@ fn main(
91
92
  let tid = local_id.x;
92
93
  let head_dim = u.head_dim;
93
94
  let kv_len = get_kv_len();
95
+ let q_offset = head_idx * head_dim;
96
+ let out_dim0 = tid;
97
+ let out_dim1 = tid + WORKGROUP_SIZE;
98
+ let has_out_dim0 = out_dim0 < head_dim;
99
+ let has_out_dim1 = out_dim1 < head_dim;
94
100
 
95
- if (WORKGROUP_SIZE > MAX_WORKGROUP_SIZE || head_dim > WORKGROUP_SIZE) {
101
+ if (WORKGROUP_SIZE > MAX_WORKGROUP_SIZE || head_dim > MAX_HEAD_DIM) {
96
102
  return;
97
103
  }
98
104
 
99
105
  if (kv_len == 0u) {
100
- if (tid < head_dim) {
101
- let out_offset = head_idx * head_dim + tid;
102
- output[out_offset] = 0.0;
106
+ if (has_out_dim0) {
107
+ output[q_offset + out_dim0] = 0.0;
108
+ }
109
+ if (has_out_dim1) {
110
+ output[q_offset + out_dim1] = 0.0;
103
111
  }
104
112
  return;
105
113
  }
@@ -112,15 +120,18 @@ fn main(
112
120
 
113
121
  let kv_head_idx = get_kv_head_idx(head_idx);
114
122
 
115
- if (tid < head_dim) {
116
- let q_offset = head_idx * head_dim + tid;
117
- shared_q[tid] = Q[q_offset];
123
+ if (has_out_dim0) {
124
+ shared_q[out_dim0] = Q[q_offset + out_dim0];
125
+ }
126
+ if (has_out_dim1) {
127
+ shared_q[out_dim1] = Q[q_offset + out_dim1];
118
128
  }
119
129
  workgroupBarrier();
120
130
 
121
131
  var running_max: f32 = -3.402823e+38;
122
132
  var running_sum: f32 = 0.0;
123
- var out_accum: f32 = 0.0;
133
+ var out_accum0: f32 = 0.0;
134
+ var out_accum1: f32 = 0.0;
124
135
 
125
136
  var start_k: u32 = 0u;
126
137
  if (u.sliding_window > 0u && kv_len > u.sliding_window) {
@@ -202,24 +213,31 @@ fn main(
202
213
  running_sum = running_sum * rescale + global_sum;
203
214
  running_max = new_max;
204
215
 
205
- if (tid < head_dim) {
206
- out_accum = out_accum * rescale;
216
+ if (has_out_dim0 || has_out_dim1) {
217
+ out_accum0 = out_accum0 * rescale;
218
+ out_accum1 = out_accum1 * rescale;
207
219
  let chunk_len = min(WORKGROUP_SIZE, kv_len - k_start);
208
220
  for (var k: u32 = 0u; k < chunk_len; k = k + 1u) {
209
221
  let score_idx = k;
210
222
  let k_pos_inner = k_start + k;
211
223
  let v_idx = get_kv_pos(k_pos_inner);
212
- let v_offset = v_idx * u.num_kv_heads * head_dim + kv_head_idx * head_dim + tid;
213
- let v_val = f32(V[v_offset]);
214
- out_accum = out_accum + shared_scores[score_idx] * v_val;
224
+ let v_base = v_idx * u.num_kv_heads * head_dim + kv_head_idx * head_dim;
225
+ if (has_out_dim0) {
226
+ out_accum0 = out_accum0 + shared_scores[score_idx] * f32(V[v_base + out_dim0]);
227
+ }
228
+ if (has_out_dim1) {
229
+ out_accum1 = out_accum1 + shared_scores[score_idx] * f32(V[v_base + out_dim1]);
230
+ }
215
231
  }
216
232
  }
217
233
  workgroupBarrier();
218
234
  }
219
235
 
220
- if (tid < head_dim) {
221
- let out_offset = head_idx * head_dim + tid;
222
- let inv_sum = select(0.0, 1.0 / running_sum, running_sum > 0.0);
223
- output[out_offset] = out_accum * inv_sum;
236
+ let inv_sum = select(0.0, 1.0 / running_sum, running_sum > 0.0);
237
+ if (has_out_dim0) {
238
+ output[q_offset + out_dim0] = out_accum0 * inv_sum;
239
+ }
240
+ if (has_out_dim1) {
241
+ output[q_offset + out_dim1] = out_accum1 * inv_sum;
224
242
  }
225
243
  }
@@ -0,0 +1,269 @@
1
+ // Tiered Decode Attention Kernel (TurboQuant MSE cold, f16 hot, f32 output)
2
+ //
3
+ // Cold tier: rotation-based codebook dequant with inverse rotation for V.
4
+ // Hot tier: direct f16 reads.
5
+ // Key insight: ⟨Πq, Πk⟩ = ⟨q, k⟩ — no inverse rotation needed for QK scores.
6
+ // V accumulation in rotated domain, then apply Π^T to final output.
7
+
8
+ enable f16;
9
+
10
+ const MAX_KV_LEN: u32 = 2048u;
11
+ const MAX_WORKGROUP_SIZE: u32 = 256u;
12
+ const MAX_HEAD_DIM: u32 = 256u;
13
+
14
+ override WORKGROUP_SIZE: u32 = 256u;
15
+ override BIT_WIDTH: u32 = 4u;
16
+ override PACK_FACTOR: u32 = 8u;
17
+ override NUM_CENTROIDS: u32 = 16u;
18
+
19
+ struct Uniforms {
20
+ num_heads: u32,
21
+ num_kv_heads: u32,
22
+ head_dim: u32,
23
+ cold_len: u32,
24
+ hot_len: u32,
25
+ seq_len: u32,
26
+ scale: f32,
27
+ is_causal: u32,
28
+ start_pos: u32,
29
+ attn_softcap: f32,
30
+ sliding_window: u32,
31
+ hot_window: u32,
32
+ hot_start: u32,
33
+ packed_stride: u32,
34
+ _pad0: u32,
35
+ _pad1: u32,
36
+ }
37
+
38
+ @group(0) @binding(0) var<uniform> u: Uniforms;
39
+ @group(0) @binding(1) var<storage, read> Q: array<f32>;
40
+ @group(0) @binding(2) var<storage, read> K_hot: array<f16>;
41
+ @group(0) @binding(3) var<storage, read> V_hot: array<f16>;
42
+ @group(0) @binding(4) var<storage, read> K_cold: array<u32>;
43
+ @group(0) @binding(5) var<storage, read> V_cold: array<u32>;
44
+ @group(0) @binding(6) var<storage, read> scales_k: array<f16>;
45
+ @group(0) @binding(7) var<storage, read> scales_v: array<f16>;
46
+ @group(0) @binding(8) var<storage, read_write> output: array<f32>;
47
+ @group(0) @binding(9) var<storage, read> rotation_matrix: array<f32>;
48
+ @group(0) @binding(10) var<storage, read> codebook_centroids: array<f32>;
49
+
50
+ var<workgroup> shared_scores: array<f32, MAX_KV_LEN>;
51
+ var<workgroup> shared_partial: array<f32, MAX_WORKGROUP_SIZE>;
52
+ var<workgroup> shared_max: f32;
53
+ var<workgroup> shared_sum: f32;
54
+ var<workgroup> shared_acc: array<f32, MAX_HEAD_DIM>;
55
+ var<workgroup> shared_rot_q: array<f32, MAX_HEAD_DIM>;
56
+
57
+ fn get_kv_head_idx(query_head_idx: u32) -> u32 {
58
+ return query_head_idx / (u.num_heads / u.num_kv_heads);
59
+ }
60
+
61
+ fn is_masked(abs_key: u32) -> bool {
62
+ let abs_query = u.start_pos;
63
+ if (u.is_causal != 0u && abs_key > abs_query) { return true; }
64
+ if (u.sliding_window > 0u && abs_query >= u.sliding_window) {
65
+ if (abs_key < abs_query - u.sliding_window + 1u) { return true; }
66
+ }
67
+ return false;
68
+ }
69
+
70
+ fn get_hot_pos(hot_pos: u32) -> u32 {
71
+ if (u.hot_window > 0u) {
72
+ return (u.hot_start + hot_pos) % u.hot_window;
73
+ }
74
+ return hot_pos;
75
+ }
76
+
77
+ fn unpack_index(packed_val: u32, lane: u32) -> u32 {
78
+ let bit_mask = (1u << BIT_WIDTH) - 1u;
79
+ return (packed_val >> (BIT_WIDTH * lane)) & bit_mask;
80
+ }
81
+
82
+ fn dequant_cold_k(key_pos: u32, kv_head_idx: u32, tid: u32) -> f32 {
83
+ let pack_idx = tid / PACK_FACTOR;
84
+ if (pack_idx >= u.packed_stride) { return 0.0; }
85
+ let lane = tid - pack_idx * PACK_FACTOR;
86
+ let base = (key_pos * u.num_kv_heads + kv_head_idx) * u.packed_stride + pack_idx;
87
+ let packed_val = K_cold[base];
88
+ let idx = unpack_index(packed_val, lane);
89
+ let centroid = codebook_centroids[idx];
90
+ let norm = f32(scales_k[key_pos * u.num_kv_heads + kv_head_idx]);
91
+ return centroid * norm;
92
+ }
93
+
94
+ fn dequant_cold_v(key_pos: u32, kv_head_idx: u32, tid: u32) -> f32 {
95
+ let pack_idx = tid / PACK_FACTOR;
96
+ if (pack_idx >= u.packed_stride) { return 0.0; }
97
+ let lane = tid - pack_idx * PACK_FACTOR;
98
+ let base = (key_pos * u.num_kv_heads + kv_head_idx) * u.packed_stride + pack_idx;
99
+ let packed_val = V_cold[base];
100
+ let idx = unpack_index(packed_val, lane);
101
+ let centroid = codebook_centroids[idx];
102
+ let norm = f32(scales_v[key_pos * u.num_kv_heads + kv_head_idx]);
103
+ return centroid * norm;
104
+ }
105
+
106
+ @compute @workgroup_size(WORKGROUP_SIZE, 1, 1)
107
+ fn main(
108
+ @builtin(local_invocation_id) local_id: vec3<u32>,
109
+ @builtin(workgroup_id) workgroup_id: vec3<u32>,
110
+ ) {
111
+ let head_idx = workgroup_id.x;
112
+ let tid = local_id.x;
113
+ let head_dim = u.head_dim;
114
+ let cold_len = u.cold_len;
115
+ let hot_len = u.hot_len;
116
+ let total_len = cold_len + hot_len;
117
+ let scale = u.scale;
118
+ let kv_head_idx = get_kv_head_idx(head_idx);
119
+
120
+ if (WORKGROUP_SIZE > MAX_WORKGROUP_SIZE || head_dim > MAX_HEAD_DIM || total_len > MAX_KV_LEN) {
121
+ return;
122
+ }
123
+
124
+ let valid = tid < head_dim;
125
+
126
+ // Load Q and compute rotated Q for cold-tier dot products
127
+ // ⟨Πq, Πk⟩ = ⟨q, k⟩, so we rotate Q by Π to match rotated-quantized K
128
+ var q_val: f32 = 0.0;
129
+ if (valid) {
130
+ q_val = Q[head_idx * head_dim + tid];
131
+ }
132
+
133
+ // Rotate Q for cold-tier comparisons
134
+ if (valid) {
135
+ var rot_q: f32 = 0.0;
136
+ let row_base = tid * head_dim;
137
+ for (var j: u32 = 0u; j < head_dim; j++) {
138
+ rot_q += rotation_matrix[row_base + j] * Q[head_idx * head_dim + j];
139
+ }
140
+ shared_rot_q[tid] = rot_q;
141
+ }
142
+ workgroupBarrier();
143
+
144
+ if (valid) {
145
+ shared_acc[tid] = 0.0;
146
+ }
147
+
148
+ // Cold tier: dot product in rotated domain (dequant gives rotated*norm values)
149
+ for (var k_pos: u32 = 0u; k_pos < cold_len; k_pos++) {
150
+ var k_val: f32 = 0.0;
151
+ if (valid) {
152
+ k_val = dequant_cold_k(k_pos, kv_head_idx, tid);
153
+ }
154
+
155
+ // Dot product of rotated Q and dequanted (rotated*norm) K
156
+ let partial = shared_rot_q[tid] * k_val;
157
+ shared_partial[tid] = select(0.0, partial, valid);
158
+ workgroupBarrier();
159
+
160
+ if (tid == 0u) {
161
+ var dot: f32 = 0.0;
162
+ for (var d: u32 = 0u; d < head_dim; d++) {
163
+ dot += shared_partial[d];
164
+ }
165
+ var s = dot * scale;
166
+ if (u.attn_softcap > 0.0) {
167
+ s = tanh(s / u.attn_softcap) * u.attn_softcap;
168
+ }
169
+ if (is_masked(k_pos)) { s = -3.402823e+38; }
170
+ shared_scores[k_pos] = s;
171
+ }
172
+ workgroupBarrier();
173
+ }
174
+
175
+ // Hot tier: standard f16 dot product (no rotation needed since stored as f16)
176
+ for (var k_pos: u32 = 0u; k_pos < hot_len; k_pos++) {
177
+ var k_val: f32 = 0.0;
178
+ if (valid) {
179
+ let k_idx = get_hot_pos(k_pos);
180
+ let k_offset = k_idx * u.num_kv_heads * head_dim + kv_head_idx * head_dim + tid;
181
+ k_val = f32(K_hot[k_offset]);
182
+ }
183
+
184
+ let partial = q_val * k_val;
185
+ shared_partial[tid] = select(0.0, partial, valid);
186
+ workgroupBarrier();
187
+
188
+ if (tid == 0u) {
189
+ var dot: f32 = 0.0;
190
+ for (var d: u32 = 0u; d < head_dim; d++) {
191
+ dot += shared_partial[d];
192
+ }
193
+ var s = dot * scale;
194
+ if (u.attn_softcap > 0.0) {
195
+ s = tanh(s / u.attn_softcap) * u.attn_softcap;
196
+ }
197
+ let abs_key = u.hot_start + k_pos;
198
+ if (is_masked(abs_key)) { s = -3.402823e+38; }
199
+ shared_scores[cold_len + k_pos] = s;
200
+ }
201
+ workgroupBarrier();
202
+ }
203
+
204
+ // Softmax
205
+ if (tid == 0u) {
206
+ var max_score: f32 = -3.402823e+38;
207
+ for (var k: u32 = 0u; k < total_len; k++) {
208
+ max_score = max(max_score, shared_scores[k]);
209
+ }
210
+ shared_max = max_score;
211
+ }
212
+ workgroupBarrier();
213
+
214
+ let max_score = shared_max;
215
+ if (tid == 0u) {
216
+ var sum_exp: f32 = 0.0;
217
+ for (var k: u32 = 0u; k < total_len; k++) {
218
+ let w = exp(shared_scores[k] - max_score);
219
+ shared_scores[k] = w;
220
+ sum_exp += w;
221
+ }
222
+ shared_sum = sum_exp;
223
+ }
224
+ workgroupBarrier();
225
+
226
+ let inv_sum = select(0.0, 1.0 / shared_sum, shared_sum > 0.0);
227
+
228
+ // Value accumulation
229
+ // Cold V: accumulated in rotated domain, then inverse-rotated
230
+ // Hot V: accumulated directly
231
+ if (valid) {
232
+ var acc_cold_rotated: f32 = 0.0;
233
+ var acc_hot: f32 = 0.0;
234
+
235
+ // Cold tier V (in rotated domain)
236
+ for (var score_idx: u32 = 0u; score_idx < cold_len; score_idx++) {
237
+ let v_val = dequant_cold_v(score_idx, kv_head_idx, tid);
238
+ acc_cold_rotated += shared_scores[score_idx] * v_val;
239
+ }
240
+
241
+ // Hot tier V (direct f16)
242
+ for (var score_idx: u32 = cold_len; score_idx < total_len; score_idx++) {
243
+ let hot_pos = score_idx - cold_len;
244
+ let v_idx = get_hot_pos(hot_pos);
245
+ let v_offset = v_idx * u.num_kv_heads * head_dim + kv_head_idx * head_dim + tid;
246
+ let v_val = f32(V_hot[v_offset]);
247
+ acc_hot += shared_scores[score_idx] * v_val;
248
+ }
249
+
250
+ // Store rotated cold accumulation for inverse rotation
251
+ shared_acc[tid] = acc_cold_rotated;
252
+ shared_partial[tid] = acc_hot;
253
+ }
254
+ workgroupBarrier();
255
+
256
+ // Apply inverse rotation Π^T to cold V accumulation
257
+ // out[tid] = Σ_j Π^T[tid][j] * acc_cold_rotated[j] = Σ_j Π[j][tid] * acc_cold_rotated[j]
258
+ if (valid) {
259
+ var inv_rot_cold: f32 = 0.0;
260
+ for (var j: u32 = 0u; j < head_dim; j++) {
261
+ // Π^T[tid][j] = Π[j][tid]
262
+ inv_rot_cold += rotation_matrix[j * head_dim + tid] * shared_acc[j];
263
+ }
264
+
265
+ // Final output: inverse-rotated cold + direct hot, normalized
266
+ let final_val = (inv_rot_cold + shared_partial[tid]) * inv_sum;
267
+ output[head_idx * head_dim + tid] = final_val;
268
+ }
269
+ }