Xenova HF Staff commited on
Commit
ba62316
·
verified ·
1 Parent(s): eba4596

sync 4385228f788d

Browse files
README.md CHANGED
@@ -60,61 +60,57 @@ Attributes and default values (overridable per request):
60
 
61
  One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.
62
 
63
- - `sgmat_grouped_routed_fc1plain_fc3none_fc2plain` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
64
- - `sgmat_grouped_routed_fc1plain_fc3none_fc2bias` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
65
- - `sgmat_grouped_routed_fc1plain_fc3plain_fc2plain` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
66
- - `sgmat_grouped_routed_fc1plain_fc3plain_fc2bias` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
67
- - `sgmat_grouped_routed_fc1plain_fc3biased_fc2plain` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
68
- - `sgmat_grouped_routed_fc1plain_fc3biased_fc2bias` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
69
- - `sgmat_grouped_routed_fc1bias_fc3none_fc2plain` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
70
- - `sgmat_grouped_routed_fc1bias_fc3none_fc2bias` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
71
- - `sgmat_grouped_routed_fc1bias_fc3plain_fc2plain` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
72
- - `sgmat_grouped_routed_fc1bias_fc3plain_fc2bias` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
73
- - `sgmat_grouped_routed_fc1bias_fc3biased_fc2plain` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
74
- - `sgmat_grouped_routed_fc1bias_fc3biased_fc2bias` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile is reused for result publication. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
75
 
76
  ## Device requirements
77
 
78
- Some implementation variants require `subgroup-matrix` and `subgroups`. These are route-specific capabilities, not package-wide requirements; availability also depends on the request shape and dtype.
79
 
80
  ## Files
81
 
82
- - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, provenance)
83
- - [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)
84
- - [`test.json`](build/webgpu/test.json) — correctness cases
85
- - [`bench.json`](build/webgpu/bench.json) — benchmark cases
86
- - [`expert-group-slots.wgsl.jinja`](build/webgpu/expert-group-slots.wgsl.jinja)
87
- - [`expert-slot-mix.wgsl.jinja`](build/webgpu/expert-slot-mix.wgsl.jinja)
88
- - [`moe-ffn-gemv.wgsl.jinja`](build/webgpu/moe-ffn-gemv.wgsl.jinja)
89
- - [`moe-ffn-grouped.wgsl.jinja`](build/webgpu/moe-ffn-grouped.wgsl.jinja)
90
- - [`moe-ffn-stage.wgsl.jinja`](build/webgpu/moe-ffn-stage.wgsl.jinja)
91
- - [`moe-grouped-sgmat.wgsl.jinja`](build/webgpu/moe-grouped-sgmat.wgsl.jinja)
92
- - [`moe-output-gemv.wgsl.jinja`](build/webgpu/moe-output-gemv.wgsl.jinja)
93
- - [`moe-output-grouped.wgsl.jinja`](build/webgpu/moe-output-grouped.wgsl.jinja)
94
- - [`moe-output-stage.wgsl.jinja`](build/webgpu/moe-output-stage.wgsl.jinja)
95
- - [`moe-route-stage.wgsl.jinja`](build/webgpu/moe-route-stage.wgsl.jinja)
96
 
97
  ## Use with `@huggingface/kernels`
98
 
99
  ```sh
100
- npm install --save-exact @huggingface/kernels@0.0.1-preview.3
101
  ```
102
 
 
 
103
  Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
104
 
105
  The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.
106
  It follows the `v1` branch as fixes land. To pin exact artifact bytes, pass a 40-character commit `revision` instead of `version`.
107
 
108
- Replace each `*Data` placeholder with a typed array containing the corresponding input data.
109
-
110
  ```js
111
  import { getKernel } from "@huggingface/kernels";
112
 
113
  const kernel = await getKernel("webgpu-kernels/com.microsoft.MoE", { version: 1 });
 
 
 
 
 
 
114
  const { outputT } = await kernel({
115
  inputT: { data: inputTData, shape: [1, 1] },
116
  routerT: { data: routerTData, shape: [1, 2] },
117
  fc1T: { data: fc1TData, shape: [2, 1, 1] },
118
  fc2T: { data: fc2TData, shape: [2, 1, 1] },
119
  });
 
120
  ```
 
60
 
61
  One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.
62
 
63
+ - `sgmat_grouped_routed` — Expert-grouped f32 matrix projections load public weight layouts directly. Two statically interleaved accumulation chains limit rounding growth; the input tile's workgroup memory is reused to stage the output. Requires compatible subgroups, f32 fragments, workgroup limits, and complete weight tiles.
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  ## Device requirements
66
 
67
+ Some implementation variants require `subgroup-matrix` and `subgroups`. Other variants run without them; which variant runs also depends on the request's shapes and dtypes.
68
 
69
  ## Files
70
 
71
+ - [`metadata.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/metadata.json) — kernel metadata (id, file digests, manifest spec, provenance)
72
+ - [`manifest.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/manifest.json) — the op contract (source of truth)
73
+ - [`test.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/test.json) — correctness cases
74
+ - [`bench.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/bench.json) — benchmark cases
75
+ - [`expert-group-slots.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/expert-group-slots.wgsl.jinja)
76
+ - [`expert-slot-mix.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/expert-slot-mix.wgsl.jinja)
77
+ - [`moe-ffn-gemv.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/moe-ffn-gemv.wgsl.jinja)
78
+ - [`moe-ffn-grouped.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/moe-ffn-grouped.wgsl.jinja)
79
+ - [`moe-ffn-stage.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/moe-ffn-stage.wgsl.jinja)
80
+ - [`moe-grouped-sgmat.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/moe-grouped-sgmat.wgsl.jinja)
81
+ - [`moe-output-gemv.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/moe-output-gemv.wgsl.jinja)
82
+ - [`moe-output-grouped.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/moe-output-grouped.wgsl.jinja)
83
+ - [`moe-output-stage.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/moe-output-stage.wgsl.jinja)
84
+ - [`moe-route-stage.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MoE/blob/v1/build/webgpu/moe-route-stage.wgsl.jinja)
85
 
86
  ## Use with `@huggingface/kernels`
87
 
88
  ```sh
89
+ npm install --save-exact @huggingface/kernels@0.0.1-preview.4
90
  ```
91
 
92
+ Paste the example into a browser app that resolves npm imports. Serve the app over HTTPS or localhost with WebGPU available.
93
+
94
  Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
95
 
96
  The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.
97
  It follows the `v1` branch as fixes land. To pin exact artifact bytes, pass a 40-character commit `revision` instead of `version`.
98
 
 
 
99
  ```js
100
  import { getKernel } from "@huggingface/kernels";
101
 
102
  const kernel = await getKernel("webgpu-kernels/com.microsoft.MoE", { version: 1 });
103
+
104
+ const inputTData = Float32Array.of(1);
105
+ const routerTData = new Float32Array(2);
106
+ const fc1TData = Float32Array.of(1, 2);
107
+ const fc2TData = Float32Array.of(1, 3);
108
+
109
  const { outputT } = await kernel({
110
  inputT: { data: inputTData, shape: [1, 1] },
111
  routerT: { data: routerTData, shape: [1, 2] },
112
  fc1T: { data: fc1TData, shape: [2, 1, 1] },
113
  fc2T: { data: fc2TData, shape: [2, 1, 1] },
114
  });
115
+ console.log({ outputT });
116
  ```
build/webgpu/bench.json CHANGED
The diff for this file is too large to render. See raw diff
 
build/webgpu/expert-slot-mix.wgsl.jinja CHANGED
@@ -1,8 +1,9 @@
1
- {% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}
2
  {% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
3
  // 2D-folded flat index: gid.y carries the high bits past the dispatch's
4
  // per-axis workgroup fold width.
5
- let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};{% endmacro %}
 
6
  {{ env.wgsl.resourceDeclarations }}
7
 
8
  // Routed sum for the grouped schedule. The grouped FC2 stage produces one projected row per
 
1
+ {% macro flat_index_2d(workgroupSize, name="i", bound="params.count") %}
2
  {% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
3
  // 2D-folded flat index: gid.y carries the high bits past the dispatch's
4
  // per-axis workgroup fold width.
5
+ let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};
6
+ {% endmacro %}
7
  {{ env.wgsl.resourceDeclarations }}
8
 
9
  // Routed sum for the grouped schedule. The grouped FC2 stage produces one projected row per
build/webgpu/manifest.json CHANGED
The diff for this file is too large to render. See raw diff
 
build/webgpu/metadata.json CHANGED
@@ -1,79 +1 @@
1
- {
2
- "name": "com.microsoft.MoE",
3
- "id": "_com_microsoft_moe_webgpu_81aa200",
4
- "version": 1,
5
- "license": "Apache-2.0",
6
- "backend": { "type": "webgpu" },
7
- "digest": {
8
- "algorithm": "sha256",
9
- "files": {
10
- "bench.json": "dk7Y+0dMicwCWAn3BjOAjL0VyY0CqCabC1DuWCMDMis=",
11
- "expert-group-slots.wgsl.jinja": "Ta+3H2FA1qRRzksdkwgKLZiO+AokmuM+B9JVN8cv1EQ=",
12
- "expert-slot-mix.wgsl.jinja": "VwX7A1f2r+lxMzG/I5r0KUpGr0Y5gW/CrI/wdwx0Ah4=",
13
- "manifest.json": "wBjvzcYwg6LKo5NBDt5ygh3hYrahgWaBsqIBLN2bfXY=",
14
- "moe-ffn-gemv.wgsl.jinja": "jeIxUFT5qPPAB55dEtF1kV897NLEOxKBMGMwrpYuOgQ=",
15
- "moe-ffn-grouped.wgsl.jinja": "20KqFJ5ukAEOmtA+p3FuttT6GHFWuoyq3bMIT+O3vyQ=",
16
- "moe-ffn-stage.wgsl.jinja": "Abb7HoT+GLy160X3+CX2pOLyQPIF3rNxq/0xRPkwHM0=",
17
- "moe-grouped-sgmat.wgsl.jinja": "blIiUWR5EYpFXm2Gdgs8gw3G/Ttq8yuaqiRJNSoeQgo=",
18
- "moe-output-gemv.wgsl.jinja": "s3IrdnP/Os57QXA1w6f//dMa4pK5uarFK0ur3tiJipw=",
19
- "moe-output-grouped.wgsl.jinja": "9m4bid355i/L86ZqBjNMBS8YhU6Ta+RaIy8cg7t+rgk=",
20
- "moe-output-stage.wgsl.jinja": "lII4acbshM29uYlxAlJU+CcR+vRTREMc6CrJoYEnl5M=",
21
- "moe-route-stage.wgsl.jinja": "uenMx8qmkRe13OPiwga5rGndXTGuqIJKrurm1Shd8xM=",
22
- "test.json": "ififQJvtLtWJR4hKuaZHpgwomm3UdMab9oorgFBZN74="
23
- }
24
- },
25
- "provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } },
26
- "webgpu": {
27
- "manifestSpec": "2.1",
28
- "variants": {
29
- "split_routed_fc1plain_fc3none_fc2plain": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
30
- "gemv_routed_fc1plain_fc3none_fc2plain": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
31
- "sgmat_grouped_routed_fc1plain_fc3none_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
32
- "grouped_routed_fc1plain_fc3none_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
33
- "split_routed_fc1plain_fc3none_fc2bias": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
34
- "gemv_routed_fc1plain_fc3none_fc2bias": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
35
- "sgmat_grouped_routed_fc1plain_fc3none_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
36
- "grouped_routed_fc1plain_fc3none_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
37
- "split_routed_fc1plain_fc3plain_fc2plain": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
38
- "gemv_routed_fc1plain_fc3plain_fc2plain": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
39
- "sgmat_grouped_routed_fc1plain_fc3plain_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
40
- "grouped_routed_fc1plain_fc3plain_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
41
- "split_routed_fc1plain_fc3plain_fc2bias": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
42
- "gemv_routed_fc1plain_fc3plain_fc2bias": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
43
- "sgmat_grouped_routed_fc1plain_fc3plain_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
44
- "grouped_routed_fc1plain_fc3plain_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
45
- "split_routed_fc1plain_fc3biased_fc2plain": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
46
- "gemv_routed_fc1plain_fc3biased_fc2plain": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
47
- "sgmat_grouped_routed_fc1plain_fc3biased_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
48
- "grouped_routed_fc1plain_fc3biased_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
49
- "split_routed_fc1plain_fc3biased_fc2bias": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
50
- "gemv_routed_fc1plain_fc3biased_fc2bias": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
51
- "sgmat_grouped_routed_fc1plain_fc3biased_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
52
- "grouped_routed_fc1plain_fc3biased_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
53
- "split_routed_fc1bias_fc3none_fc2plain": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
54
- "gemv_routed_fc1bias_fc3none_fc2plain": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
55
- "sgmat_grouped_routed_fc1bias_fc3none_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
56
- "grouped_routed_fc1bias_fc3none_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
57
- "split_routed_fc1bias_fc3none_fc2bias": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
58
- "gemv_routed_fc1bias_fc3none_fc2bias": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
59
- "sgmat_grouped_routed_fc1bias_fc3none_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
60
- "grouped_routed_fc1bias_fc3none_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
61
- "split_routed_fc1bias_fc3plain_fc2plain": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
62
- "gemv_routed_fc1bias_fc3plain_fc2plain": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
63
- "sgmat_grouped_routed_fc1bias_fc3plain_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
64
- "grouped_routed_fc1bias_fc3plain_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
65
- "split_routed_fc1bias_fc3plain_fc2bias": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
66
- "gemv_routed_fc1bias_fc3plain_fc2bias": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
67
- "sgmat_grouped_routed_fc1bias_fc3plain_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
68
- "grouped_routed_fc1bias_fc3plain_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
69
- "split_routed_fc1bias_fc3biased_fc2plain": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
70
- "gemv_routed_fc1bias_fc3biased_fc2plain": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
71
- "sgmat_grouped_routed_fc1bias_fc3biased_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
72
- "grouped_routed_fc1bias_fc3biased_fc2plain": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
73
- "split_routed_fc1bias_fc3biased_fc2bias": ["moe-ffn-stage.wgsl.jinja", "moe-output-stage.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
74
- "gemv_routed_fc1bias_fc3biased_fc2bias": ["moe-ffn-gemv.wgsl.jinja", "moe-output-gemv.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
75
- "sgmat_grouped_routed_fc1bias_fc3biased_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-grouped-sgmat.wgsl.jinja", "moe-route-stage.wgsl.jinja"],
76
- "grouped_routed_fc1bias_fc3biased_fc2bias": ["expert-group-slots.wgsl.jinja", "expert-slot-mix.wgsl.jinja", "moe-ffn-grouped.wgsl.jinja", "moe-output-grouped.wgsl.jinja", "moe-route-stage.wgsl.jinja"]
77
- }
78
- }
79
- }
 
1
+ {"name":"com.microsoft.MoE","id":"_com_microsoft_moe_webgpu_bcd5189","version":1,"license":"Apache-2.0","backend":{"type":"webgpu"},"digest":{"algorithm":"sha256","files":{"bench.json":"Qt6rSMj9/+T+nYBhe1796uu+zqVOqx5RGL0jdYkyD+o=","expert-group-slots.wgsl.jinja":"Ta+3H2FA1qRRzksdkwgKLZiO+AokmuM+B9JVN8cv1EQ=","expert-slot-mix.wgsl.jinja":"lhY7Tb3YL+NnoQ9UP/M+ee8I7VVQccOVciYGDuosDeU=","manifest.json":"QHz8uiL/rNB7SGb5oiM6z16mr0Sw3oW8zKIuN2sObZs=","moe-ffn-gemv.wgsl.jinja":"Fcks7FwjcVTrzaLhwQ9u0Q4YUURrsM1wftd+6f4ZIWM=","moe-ffn-grouped.wgsl.jinja":"T2Vfm2E6RY5/YNgpPDfzQ0fDSS2rDTH7llWAhbG1GPk=","moe-ffn-stage.wgsl.jinja":"4k9rrXy2L3MB/iJ8FQQ1TwmxWWj0BPnWW5QIOGYrTdY=","moe-grouped-sgmat.wgsl.jinja":"24jiwcHdiQqUX34qJVUK9Ddr+VtzQroOyGtZWZRlOHA=","moe-output-gemv.wgsl.jinja":"s3IrdnP/Os57QXA1w6f//dMa4pK5uarFK0ur3tiJipw=","moe-output-grouped.wgsl.jinja":"9DiR6jtk81Zpso8Rtzgk+ouNag8hOxuwKyhxbBxcwS8=","moe-output-stage.wgsl.jinja":"KK/VbLeWEnR1jfOjH9kc6s1Q0JYPpU/pi0Qie49dVjE=","moe-route-stage.wgsl.jinja":"BFVYxqpNTsJnAJ/E2Se83/8WvNzpcmOGGlU15WC2c1E=","test.json":"mQHngK95Nt/+AMvhTcj5mGF6vVfEd0U/C5v+DL8A0Xg="}},"provenance":{"kernel":{"sha":"4385228f788ddaa1bd7aec6a80a20f3ddffd8011","dirty":false}},"webgpu":{"manifestSpec":"2.2"}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
build/webgpu/moe-ffn-gemv.wgsl.jinja CHANGED
@@ -48,7 +48,7 @@ fn swiglu(gate_in: f32, up_in: f32) -> f32 {
48
  let gate = gate_in;
49
  let up = up_in;
50
  {% endif %}
51
- return gate / (1.0 + exp(-{{ matrixActivationAlpha if matrixActivationAlpha is defined else "params.activationAlpha" }} * gate)) * (up + {{ matrixActivationBeta if matrixActivationBeta is defined else "params.activationBeta" }});
52
  }
53
  {% endif %}
54
 
 
48
  let gate = gate_in;
49
  let up = up_in;
50
  {% endif %}
51
+ return gate / (1.0 + exp(-{{ "params.activationAlpha" }} * gate)) * (up + {{ "params.activationBeta" }});
52
  }
53
  {% endif %}
54
 
build/webgpu/moe-ffn-grouped.wgsl.jinja CHANGED
@@ -54,12 +54,17 @@ fn swiglu(gate_in: f32, up_in: f32) -> f32 {
54
  let gate = gate_in;
55
  let up = up_in;
56
  {% endif %}
57
- return gate / (1.0 + exp(-{{ matrixActivationAlpha if matrixActivationAlpha is defined else "params.activationAlpha" }} * gate)) * (up + {{ matrixActivationBeta if matrixActivationBeta is defined else "params.activationBeta" }});
58
  }
59
  {% endif %}
60
 
61
  const KVEC: u32 = {{ groupTileKVec }}u;
62
- {% macro stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, guarded, bLoadVec4="", b2LoadVec4="", aLoadVec4="") %}
 
 
 
 
 
63
  for (var idx = tid; idx < MTILE * KVEC; idx = idx + THREADS) {
64
  let m = idx / KVEC;
65
  let kb = (idx % KVEC) * 4u;
@@ -76,9 +81,51 @@ const KVEC: u32 = {{ groupTileKVec }}u;
76
  {% endif %}
77
  a_tile[idx] = a_vec;
78
  }
79
- for (var idx = tid; idx < NTILE * KVEC; idx = idx + THREADS) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  let n = idx / KVEC;
81
  let kb = (idx % KVEC) * 4u;
 
82
  let col = n_base + n;
83
  {% if guarded %}
84
  let col_live = col < {{ nExtent }};
@@ -106,19 +153,22 @@ const KVEC: u32 = {{ groupTileKVec }}u;
106
  }
107
  {% endfor %}
108
  {% endif %}
109
- b_tile[idx] = b_vec;
110
  {% if b2Load %}
111
- b2_tile[idx] = b2_vec;
112
  {% endif %}
113
- }{% endmacro %}
114
 
115
- {% macro group_tile_loop(aLoad, bLoad, b2Load, kExtent, nExtent, regM, regN, bLoadVec4="", b2LoadVec4="", aLoadVec4="") %}
116
  {% for r in range(regM) %}
117
  {% for c in range(regN) %}
118
  var acc_{{ r }}_{{ c }} = 0.0;
119
  {% if b2Load %}
120
  var acc2_{{ r }}_{{ c }} = 0.0;
121
  {% endif %}
 
 
 
122
  {% endfor %}
123
  {% endfor %}
124
 
@@ -135,32 +185,67 @@ const KVEC: u32 = {{ groupTileKVec }}u;
135
  break;
136
  }
137
  if (n_full && k_base + KTILE <= {{ kExtent }}) {
138
- {{ stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, false, bLoadVec4, b2LoadVec4, aLoadVec4) }}
139
  } else {
140
- {{ stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, true) }}
141
  }
142
  workgroupBarrier();
143
 
 
 
 
 
 
 
 
 
 
144
  for (var kv = 0u; kv < KVEC; kv = kv + 1u) {
 
145
  {% for r in range(regM) %}
146
  let a{{ r }} = a_tile[(lid.y * {{ regM }}u + {{ r }}u) * KVEC + kv];
147
  {% endfor %}
148
- {% for c in range(regN) %}
149
- let b{{ c }} = b_tile[(lid.x * {{ regN }}u + {{ c }}u) * KVEC + kv];
 
 
 
 
 
150
  {% if b2Load %}
151
- let s{{ c }} = b2_tile[(lid.x * {{ regN }}u + {{ c }}u) * KVEC + kv];
152
  {% endif %}
153
  {% endfor %}
154
  {% for r in range(regM) %}
155
  {% for c in range(regN) %}
156
- acc_{{ r }}_{{ c }} = acc_{{ r }}_{{ c }} + dot(a{{ r }}, b{{ c }});
 
 
 
 
 
 
157
  {% if b2Load %}
158
- acc2_{{ r }}_{{ c }} = acc2_{{ r }}_{{ c }} + dot(a{{ r }}, s{{ c }});
 
 
 
 
 
 
159
  {% endif %}
160
  {% endfor %}
161
  {% endfor %}
162
  }
163
- // Orders this step's tile reads before the next step overwrites them.
 
 
 
 
 
 
 
 
164
  workgroupBarrier();
165
  k_base = k_base + KTILE;
166
  }{% endmacro %}
@@ -200,7 +285,7 @@ fn main(@builtin(workgroup_id) wid: vec3<u32>,
200
  "HIDDEN", "INTER", regM, regN,
201
  at4("fc1_experts_weights", bPre ~ "kb") if groupVec4 else "",
202
  (at4(secondBuffer, cPre ~ "kb") if hasSecond else "") if groupVec4 else "",
203
- at4("input", aPre ~ "kb") if groupVec4 else "") }}
204
 
205
  {% for r in range(regM) %}
206
  {% for c in range(regN) %}
 
54
  let gate = gate_in;
55
  let up = up_in;
56
  {% endif %}
57
+ return gate / (1.0 + exp(-{{ "params.activationAlpha" }} * gate)) * (up + {{ "params.activationBeta" }});
58
  }
59
  {% endif %}
60
 
61
  const KVEC: u32 = {{ groupTileKVec }}u;
62
+ {% macro stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, guarded, bLoadVec4="", b2LoadVec4="", aLoadVec4="", bKMajor=false, bLaneInterleaved=false, regN=1, staticK=false, bStageInterleaved=false, packedStreams=[]) %}{% set bStoreIndex =
63
+ "idx" if bStageInterleaved and bKMajor and bLaneInterleaved else
64
+ ("(idx % KVEC) * NTILE + n / " ~ regN ~ "u + (n % " ~ regN ~ "u) * " ~ ((groupTileN / regN) | int) ~ "u")
65
+ if bKMajor and bLaneInterleaved and regN > 1
66
+ else ("(idx % KVEC) * NTILE + idx / KVEC" if bKMajor else "idx")
67
+ %}
68
  for (var idx = tid; idx < MTILE * KVEC; idx = idx + THREADS) {
69
  let m = idx / KVEC;
70
  let kb = (idx % KVEC) * 4u;
 
81
  {% endif %}
82
  a_tile[idx] = a_vec;
83
  }
84
+ {% if packedStreams and not guarded and bKMajor and bLaneInterleaved %}
85
+ // Stage columns in shared-memory lane order for contiguous constant-K stores.
86
+ for (var slot = tid; slot < NTILE; slot = slot + THREADS) {
87
+ let local_col = (slot % THREADS_SIDE) * {{ regN }}u + slot / THREADS_SIDE;
88
+ let col = n_base + local_col;
89
+ {% if packedStreams[0].packing == "word32" %}
90
+ {% for stream in packedStreams %}
91
+ let {{ stream.prefix }}_packed = {{ stream.buffer }}[col * ({{ kExtent }} / 32u) + k_base / 32u];
92
+ let {{ stream.prefix }}_scale = f32({{ stream.scales }}[col * {{ stream.scaleBlocks }} + k_base / {{ stream.quantBlock }}]);
93
+ {% endfor %}
94
+ {% for chunk in range(8) %}
95
+ {% for stream in packedStreams %}
96
+ let {{ stream.prefix }}_word{{ chunk }} = {{ stream.prefix }}_packed[{{ (chunk / 2) | int }}] >> {{ (chunk % 2) * 16 }}u;
97
+ let {{ stream.prefix }}_values{{ chunk }} = (vec4<f32>((vec4<u32>({{ stream.prefix }}_word{{ chunk }}) >> vec4<u32>(0u, 4u, 8u, 12u)) & vec4<u32>({{ stream.mask }})) - vec4<f32>({{ stream.zero }})) * {{ stream.prefix }}_scale;
98
+ {{ stream.destination }}[{{ chunk }}u * NTILE + slot] = {{ stream.prefix }}_values{{ chunk }};
99
+ {% endfor %}
100
+ {% endfor %}
101
+ {% else %}
102
+ {% for block in range(groupTileK // 8) %}
103
+ {% for stream in packedStreams %}
104
+ let {{ stream.prefix }}_row{{ block }} = expert * {{ stream.rows }} + {{ stream.row }};
105
+ let {{ stream.prefix }}_packed{{ block }} = {{ stream.buffer }}[{{ stream.prefix }}_row{{ block }} * {{ stream.packedCols // 4 }}u + (k_base + {{ block * 8 }}u) / 8u];
106
+ let {{ stream.prefix }}_scale{{ block }} = {{ stream.scales }}[{{ stream.prefix }}_row{{ block }}{% if not stream.columnScales %} * {{ stream.scaleBlocks }}u + (k_base + {{ block * 8 }}u) / {{ stream.quantBlock }}{% endif %}];
107
+ {% for chunk in range(2) %}
108
+ {% if stream.packing == "word8" %}
109
+ let {{ stream.prefix }}_codes{{ block }}_{{ chunk }} = (vec4<u32>({{ stream.prefix }}_packed{{ block }}) >> vec4<u32>({{ chunk * 16 }}u, {{ chunk * 16 + 4 }}u, {{ chunk * 16 + 8 }}u, {{ chunk * 16 + 12 }}u)) & vec4<u32>(15u);
110
+ {% else %}
111
+ let {{ stream.prefix }}_bytes{{ block }}_{{ chunk }} = {{ stream.prefix }}_packed{{ block }}.{{ 'xy' if chunk == 0 else 'zw' }};
112
+ let {{ stream.prefix }}_codes{{ block }}_{{ chunk }} = vec4<u32>({{ stream.prefix }}_bytes{{ block }}_{{ chunk }}.x, {{ stream.prefix }}_bytes{{ block }}_{{ chunk }}.x >> 4u, {{ stream.prefix }}_bytes{{ block }}_{{ chunk }}.y, {{ stream.prefix }}_bytes{{ block }}_{{ chunk }}.y >> 4u) & vec4<u32>(15u);
113
+ {% endif %}
114
+ {{ stream.destination }}[{{ block * 2 + chunk }}u * NTILE + slot] = (vec4<f32>({{ stream.prefix }}_codes{{ block }}_{{ chunk }}) - vec4<f32>({{ stream.zero }})) * {{ stream.prefix }}_scale{{ block }};
115
+ {% endfor %}
116
+ {% endfor %}
117
+ {% endfor %}
118
+ {% endif %}
119
+ }
120
+ {% else %} for (var idx = tid; idx < NTILE * KVEC; idx = idx + THREADS) {
121
+ {% if bStageInterleaved and bKMajor and bLaneInterleaved %}
122
+ let slot = idx % NTILE;
123
+ let n = (slot % THREADS_SIDE) * {{ regN }}u + slot / THREADS_SIDE;
124
+ let kb = (idx / NTILE) * 4u;
125
+ {% else %}
126
  let n = idx / KVEC;
127
  let kb = (idx % KVEC) * 4u;
128
+ {% endif %}
129
  let col = n_base + n;
130
  {% if guarded %}
131
  let col_live = col < {{ nExtent }};
 
153
  }
154
  {% endfor %}
155
  {% endif %}
156
+ b_tile[{{ bStoreIndex }}] = b_vec;
157
  {% if b2Load %}
158
+ b2_tile[{{ bStoreIndex }}] = b2_vec;
159
  {% endif %}
160
+ }{% endif %}{% endmacro %}
161
 
162
+ {% macro group_tile_loop(aLoad, bLoad, b2Load, kExtent, nExtent, regM, regN, bLoadVec4="", b2LoadVec4="", aLoadVec4="", scalarFma=false, bKMajor=false, bLaneInterleaved=false, staticK=false, bStageInterleaved=false, packedStreams=[], foldStage=false, refinePair=false) %}{% set foldStage = foldStage or refinePair %}{% set accum2 = "chunk2" if refinePair else "acc2" %}{% set accum = "chunk" if foldStage else "acc" %}
163
  {% for r in range(regM) %}
164
  {% for c in range(regN) %}
165
  var acc_{{ r }}_{{ c }} = 0.0;
166
  {% if b2Load %}
167
  var acc2_{{ r }}_{{ c }} = 0.0;
168
  {% endif %}
169
+ {% if refinePair %}{% for stream in ["", "2"] %}
170
+ var mass{{ stream }}_{{ r }}_{{ c }} = 0.0;
171
+ {% endfor %}{% endif %}
172
  {% endfor %}
173
  {% endfor %}
174
 
 
185
  break;
186
  }
187
  if (n_full && k_base + KTILE <= {{ kExtent }}) {
188
+ {{ stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, false, bLoadVec4, b2LoadVec4, aLoadVec4, bKMajor, bLaneInterleaved, regN, staticK, bStageInterleaved, packedStreams) }}
189
  } else {
190
+ {{ stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, true, bKMajor=bKMajor, bLaneInterleaved=bLaneInterleaved, regN=regN, staticK=staticK, bStageInterleaved=bStageInterleaved, packedStreams=packedStreams) }}
191
  }
192
  workgroupBarrier();
193
 
194
+ {% if foldStage %}{% for r in range(regM) %}{% for c in range(regN) %}
195
+ var chunk_{{ r }}_{{ c }} = 0.0;
196
+ {% if refinePair %} var chunk2_{{ r }}_{{ c }} = 0.0;
197
+ {% endif %}
198
+ {% endfor %}{% endfor %}{% endif %}{% for staticKv in (range(groupTileKVec) if staticK else [-1]) %}
199
+ {% if staticK %}
200
+ {
201
+ const kv: u32 = {{ staticKv }}u;
202
+ {% else %}
203
  for (var kv = 0u; kv < KVEC; kv = kv + 1u) {
204
+ {% endif %}
205
  {% for r in range(regM) %}
206
  let a{{ r }} = a_tile[(lid.y * {{ regM }}u + {{ r }}u) * KVEC + kv];
207
  {% endfor %}
208
+ {% for c in range(regN) %}{% set bReadIndex =
209
+ ("kv * NTILE + lid.x + " ~ ((c * groupTileN / regN) | int) ~ "u")
210
+ if bKMajor and bLaneInterleaved and regN > 1
211
+ else (("kv * NTILE + lid.x * " ~ regN ~ "u + " ~ c ~ "u") if bKMajor
212
+ else ("(lid.x * " ~ regN ~ "u + " ~ c ~ "u) * KVEC + kv"))
213
+ %}
214
+ let b{{ c }} = b_tile[{{ bReadIndex }}];
215
  {% if b2Load %}
216
+ let s{{ c }} = b2_tile[{{ bReadIndex }}];
217
  {% endif %}
218
  {% endfor %}
219
  {% for r in range(regM) %}
220
  {% for c in range(regN) %}
221
+ {% if scalarFma %}
222
+ {% for component in ['x', 'y', 'z', 'w'] %}
223
+ {{ accum }}_{{ r }}_{{ c }} = fma(a{{ r }}.{{ component }}, b{{ c }}.{{ component }}, {{ accum }}_{{ r }}_{{ c }});
224
+ {% endfor %}
225
+ {% else %}
226
+ {{ accum }}_{{ r }}_{{ c }} = {{ accum }}_{{ r }}_{{ c }} + dot(a{{ r }}, b{{ c }});
227
+ {% endif %}
228
  {% if b2Load %}
229
+ {% if scalarFma %}
230
+ {% for component in ['x', 'y', 'z', 'w'] %}
231
+ {{ accum2 }}_{{ r }}_{{ c }} = fma(a{{ r }}.{{ component }}, s{{ c }}.{{ component }}, {{ accum2 }}_{{ r }}_{{ c }});
232
+ {% endfor %}
233
+ {% else %}
234
+ {{ accum2 }}_{{ r }}_{{ c }} = {{ accum2 }}_{{ r }}_{{ c }} + dot(a{{ r }}, s{{ c }});
235
+ {% endif %}
236
  {% endif %}
237
  {% endfor %}
238
  {% endfor %}
239
  }
240
+ {% endfor %}
241
+ {% if foldStage %}{% for r in range(regM) %}{% for c in range(regN) %}
242
+ {% if refinePair %}{% for stream in ["", "2"] %}{% set id = stream ~ "_" ~ r ~ "_" ~ c %}
243
+ mass{{ id }} += abs(chunk{{ id }});
244
+ acc{{ id }} += chunk{{ id }};
245
+ {% endfor %}{% else %}
246
+ acc_{{ r }}_{{ c }} += chunk_{{ r }}_{{ c }};
247
+ {% endif %}
248
+ {% endfor %}{% endfor %}{% endif %} // Orders this step's tile reads before the next step overwrites them.
249
  workgroupBarrier();
250
  k_base = k_base + KTILE;
251
  }{% endmacro %}
 
285
  "HIDDEN", "INTER", regM, regN,
286
  at4("fc1_experts_weights", bPre ~ "kb") if groupVec4 else "",
287
  (at4(secondBuffer, cPre ~ "kb") if hasSecond else "") if groupVec4 else "",
288
+ at4("input", aPre ~ "kb") if groupVec4 else "", scalarFma=expertF32Schedule, bKMajor=groupWeightKMajor, bLaneInterleaved=groupWeightKMajor, staticK=expertF32Schedule, bStageInterleaved=expertF32Schedule) }}
289
 
290
  {% for r in range(regM) %}
291
  {% for c in range(regN) %}
build/webgpu/moe-ffn-stage.wgsl.jinja CHANGED
@@ -1,8 +1,9 @@
1
- {% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}
2
  {% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
3
  // 2D-folded flat index: gid.y carries the high bits past the dispatch's
4
  // per-axis workgroup fold width.
5
- let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};{% endmacro %}
 
6
  {{ env.wgsl.resourceDeclarations }}
7
 
8
  // FC1 (and, where the schema splits them, FC3) projection plus the activation. One thread owns
@@ -76,7 +77,7 @@ fn swiglu(gate_in: f32, up_in: f32) -> f32 {
76
  let gate = gate_in;
77
  let up = up_in;
78
  {% endif %}
79
- return gate / (1.0 + exp(-{{ matrixActivationAlpha if matrixActivationAlpha is defined else "params.activationAlpha" }} * gate)) * (up + {{ matrixActivationBeta if matrixActivationBeta is defined else "params.activationBeta" }});
80
  }
81
  {% endif %}
82
 
 
1
+ {% macro flat_index_2d(workgroupSize, name="i", bound="params.count") %}
2
  {% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
3
  // 2D-folded flat index: gid.y carries the high bits past the dispatch's
4
  // per-axis workgroup fold width.
5
+ let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};
6
+ {% endmacro %}
7
  {{ env.wgsl.resourceDeclarations }}
8
 
9
  // FC1 (and, where the schema splits them, FC3) projection plus the activation. One thread owns
 
77
  let gate = gate_in;
78
  let up = up_in;
79
  {% endif %}
80
+ return gate / (1.0 + exp(-{{ "params.activationAlpha" }} * gate)) * (up + {{ "params.activationBeta" }});
81
  }
82
  {% endif %}
83
 
build/webgpu/moe-grouped-sgmat.wgsl.jinja CHANGED
@@ -55,7 +55,7 @@ const SUB_COLS: u32 = {{ 16 if second else 32 }}u;
55
  // Alternating 8-wide steps are assigned to the chains at compile time.
56
  // Each subgroup publishes four result banks per chain after A is dead.
57
  // The two row groups reuse those banks with barriers on both sides.
58
- var<workgroup> tile_A: array<f32, {{ (groupedSgmatSharedBytes / 4) | int }}>;
59
 
60
  {% macro weight_offset(c) %}
61
  {% if ffn %}
@@ -69,23 +69,88 @@ var<workgroup> tile_A: array<f32, {{ (groupedSgmatSharedBytes / 4) | int }}>;
69
  (expert * HIDDEN + n_base + subtile_idx * SUB_COLS + {{ c * 8 }}u) * INTER + kidx + step
70
  {% endif %}
71
  {% endmacro %}
72
- @compute @workgroup_size(128){{ " @subgroup_size(32)" if pinSubgroupSize32 else "" }}
73
- fn main(@builtin(workgroup_id) wid: vec3<u32>,
74
- @builtin(local_invocation_index) local_idx: u32,
75
- @builtin(subgroup_invocation_id) lane: u32,
76
- @builtin(subgroup_size) sg_size: u32) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  if (wid.x >= tile_meta[0]) { return; }
78
  let expert = tile_meta[1u + wid.x * 3u];
79
  let slice_base = tile_meta[2u + wid.x * 3u];
80
  let rows = tile_meta[3u + wid.x * 3u];
81
  let a_row = local_idx / 4u;
82
  let a_slot = select(0u, slot_list[slice_base + a_row], a_row < rows);
83
- let subtile_id = local_idx / sg_size;
84
  let subtile_idy = subtile_id % 2u;
85
  let subtile_idx = subtile_id / 2u;
86
  let n_base = wid.y * {{ tileCols }}u;
87
  {% for r in range(2) %}{% for c in range(4) %}{% for chain in range(2) %}
88
- var mat{{ ["C","D","E","F"][chain] }}{{ r }}{{ c }}: subgroup_matrix_result<f32, 8, 8>;
89
  {% endfor %}{% endfor %}{% endfor %}
90
  for (var kidx = 0u; kidx < {{ reduction }}u; kidx += TILE_K) {
91
  for (var i = 0u; i < 8u; i++) {
@@ -106,15 +171,15 @@ fn main(@builtin(workgroup_id) wid: vec3<u32>,
106
  let step = {{ stepIndex * 8 }}u;
107
 
108
  {% for r in range(2) %}
109
- let matA{{ r }} = subgroupMatrixLoad<subgroup_matrix_left<f32, 8, 8>, row_major>(&tile_A, (subtile_idy * SUB_ROWS + {{ r * 8 }}u) * TILE_K + step, TILE_K);
110
  {% endfor %}
111
  {% for c in range(4) %}
112
  {% set source = ("fc3_experts_weights" if second and c >= 2 and hasFc3 else "fc1_experts_weights") if ffn else "fc2_experts_weights" %}
113
  {% set stride = hidden * (2 if second and not hasFc3 and swigluFusion == 1 else 1) if ffn else inter %}
114
- let matB{{ c }} = subgroupMatrixLoad<subgroup_matrix_right<f32, 8, 8>, col_major>(&{{ source }}, {{ weight_offset(c) }}, {{ stride }}u);
115
  {% endfor %}
116
  {% for r in range(2) %}{% for c in range(4) %}
117
- mat{{ ["C","D","E","F"][stepIndex % 2] }}{{ r }}{{ c }} = subgroupMatrixMultiplyAccumulate(matA{{ r }}, matB{{ c }}, mat{{ ["C","D","E","F"][stepIndex % 2] }}{{ r }}{{ c }});
118
  {% endfor %}{% endfor %}
119
 
120
  }
@@ -126,7 +191,7 @@ fn main(@builtin(workgroup_id) wid: vec3<u32>,
126
  {% for r in range(2) %}
127
  {% for c in range(4) %}
128
  {% for chain in range(2) %}
129
- subgroupMatrixStore<row_major>(&tile_A, {{ chain * 1024 }}u + (subtile_id * 4u + {{ c }}u) * 64u, mat{{ ["C","D","E","F"][chain] }}{{ r }}{{ c }}, 8u);
130
  {% endfor %}
131
  {% endfor %}
132
  workgroupBarrier();
@@ -172,4 +237,24 @@ fn main(@builtin(workgroup_id) wid: vec3<u32>,
172
  workgroupBarrier();
173
  {% endif %}
174
  {% endfor %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  }
 
55
  // Alternating 8-wide steps are assigned to the chains at compile time.
56
  // Each subgroup publishes four result banks per chain after A is dead.
57
  // The two row groups reuse those banks with barriers on both sides.
58
+ var<workgroup> tile_A: array<f32, {{ groupedSgmatStageElements }}>;
59
 
60
  {% macro weight_offset(c) %}
61
  {% if ffn %}
 
69
  (expert * HIDDEN + n_base + subtile_idx * SUB_COLS + {{ c * 8 }}u) * INTER + kidx + step
70
  {% endif %}
71
  {% endmacro %}
72
+ // A subgroup owns one matrix tile only after its actual population and the
73
+ // workgroup-wide bank count are proven. The elected subgroup leader claims
74
+ // the role; local invocation IDs only publish the workgroup-wide decision.
75
+ var<workgroup> matrixRoleCount: atomic<u32>;
76
+ var<workgroup> matrixRejected: atomic<u32>;
77
+ var<workgroup> matrixAccepted: u32;
78
+
79
+ fn claimMatrixSubgroupRole(width: u32, requiredWidth: u32) -> u32 {
80
+ // WGSL initializes workgroup variables to zero before this entry point.
81
+ let population = subgroupBallot(true);
82
+ let populationCount = countOneBits(population.x) + countOneBits(population.y)
83
+ + countOneBits(population.z) + countOneBits(population.w);
84
+ var claimed = 0u;
85
+ if (subgroupElect()) {
86
+ claimed = atomicAdd(&matrixRoleCount, 1u);
87
+ if (width != requiredWidth || populationCount != requiredWidth) {
88
+ atomicStore(&matrixRejected, 1u);
89
+ }
90
+ }
91
+ // Reconverge every invocation before broadcasting the elected claim.
92
+ workgroupBarrier();
93
+ return subgroupBroadcastFirst(claimed);
94
+ }
95
+
96
+ fn matrixTopologyAccepted(localIndex: u32, bankCount: u32) -> bool {
97
+ if (localIndex == 0u) {
98
+ matrixAccepted = select(0u, 1u, atomicLoad(&matrixRejected) == 0u
99
+ && atomicLoad(&matrixRoleCount) == bankCount);
100
+ }
101
+ // The uniform load both publishes the leader's decision and gives the
102
+ // compiler a workgroup-uniform condition for the barrier-containing arms.
103
+ return workgroupUniformLoad(&matrixAccepted) != 0u;
104
+ }
105
+ // The portable arm owns two result components per logical 32-invocation group.
106
+ // Staging casts, 8-wide K steps, barriers and the epilogue match the
107
+ // subgroup-matrix arm.
108
+ var<private> matrixSoftwareLane: u32;
109
+
110
+ fn matrixSoftwareMultiplyAccumulate(lhs: array<f32, 8>, rhs: array<vec2<f32>, 8>,
111
+ acc: vec2<f32>) -> vec2<f32> {
112
+ var value = acc;
113
+ for (var k = 0u; k < 8u; k += 1u) {
114
+ value = fma(vec2<f32>(lhs[k]), rhs[k], value);
115
+ }
116
+ return value;
117
+ }
118
+
119
+ {% macro matrixType(software, kind, scalar) %}{% if software %}{% if kind == "left" %}array<f32, 8>{% elif kind == "right" %}array<vec2<f32>, 8>{% else %}vec2<{{ scalar }}>{% endif %}{% else %}subgroup_matrix_{{ kind }}<{{ scalar }}, 8, 8>{% endif %}{% endmacro %}
120
+
121
+ {% macro matrixLoad(software, kind, scalar, major, pointer, offset, stride) %}{% if not software %}subgroupMatrixLoad<{{ matrixType(false, kind, scalar) }}, {{ major }}>({{ pointer }}, {{ offset }}, {{ stride }}){% else %}
122
+ {% if kind == "left" %}array<f32, 8>(
123
+ {% for k in range(8) %}f32((*({{ pointer }}))[({{ offset }}) + {% if major == "row_major" %}(matrixSoftwareLane / 4u) * ({{ stride }}) + {{ k }}u{% else %}{{ k }}u * ({{ stride }}) + matrixSoftwareLane / 4u{% endif %}]){{ "," if k < 7 else "" }}
124
+ {% endfor %})
125
+ {% else %}array<vec2<f32>, 8>(
126
+ {% for k in range(8) %}vec2<f32>(
127
+ {% for h in range(2) %}f32((*({{ pointer }}))[({{ offset }}) + {% if major == "row_major" %}{{ k }}u * ({{ stride }}) + (matrixSoftwareLane % 4u) * 2u + {{ h }}u{% else %}((matrixSoftwareLane % 4u) * 2u + {{ h }}u) * ({{ stride }}) + {{ k }}u{% endif %}]){{ "," if h == 0 else "" }}
128
+ {% endfor %}){{ "," if k < 7 else "" }}
129
+ {% endfor %})
130
+ {% endif %}{% endif %}{% endmacro %}
131
+
132
+ {% macro matrixMma(software, scalar, lhs, rhs, acc) %}{% if not software %}subgroupMatrixMultiplyAccumulate({{ lhs }}, {{ rhs }}, {{ acc }}){% else %}vec2<{{ scalar }}>(matrixSoftwareMultiplyAccumulate({{ lhs }}, {{ rhs }}, vec2<f32>({{ acc }}))){% endif %}{% endmacro %}
133
+
134
+ {% macro matrixStore(software, major, pointer, offset, value, stride) %}{% if not software %}subgroupMatrixStore<{{ major }}>({{ pointer }}, {{ offset }}, {{ value }}, {{ stride }});{% else %}{
135
+ let matrixPointer = {{ pointer }};
136
+ let matrixValue = {{ value }};
137
+ {% for h in range(2) %} (*matrixPointer)[({{ offset }}) + {{ {"row_major": "(matrixSoftwareLane / 4u) * (" ~ stride ~ ") + (matrixSoftwareLane % 4u) * 2u + " ~ h ~ "u", "col_major": "((matrixSoftwareLane % 4u) * 2u + " ~ h ~ "u) * (" ~ stride ~ ") + matrixSoftwareLane / 4u"}[major] }}] = matrixValue.{{ "x" if h == 0 else "y" }};
138
+ {% endfor %}}{% endif %}{% endmacro %}
139
+
140
+ {% macro matrixKernelBody(matrixSoftware) %}
141
+
142
  if (wid.x >= tile_meta[0]) { return; }
143
  let expert = tile_meta[1u + wid.x * 3u];
144
  let slice_base = tile_meta[2u + wid.x * 3u];
145
  let rows = tile_meta[3u + wid.x * 3u];
146
  let a_row = local_idx / 4u;
147
  let a_slot = select(0u, slot_list[slice_base + a_row], a_row < rows);
148
+ let subtile_id = matrixRole;
149
  let subtile_idy = subtile_id % 2u;
150
  let subtile_idx = subtile_id / 2u;
151
  let n_base = wid.y * {{ tileCols }}u;
152
  {% for r in range(2) %}{% for c in range(4) %}{% for chain in range(2) %}
153
+ var mat{{ ["C","D","E","F"][chain] }}{{ r }}{{ c }}: {{ matrixType(matrixSoftware, "result", "f32") }};
154
  {% endfor %}{% endfor %}{% endfor %}
155
  for (var kidx = 0u; kidx < {{ reduction }}u; kidx += TILE_K) {
156
  for (var i = 0u; i < 8u; i++) {
 
171
  let step = {{ stepIndex * 8 }}u;
172
 
173
  {% for r in range(2) %}
174
+ let matA{{ r }} = {{ matrixLoad(matrixSoftware, "left", "f32", "row_major", "&tile_A", "(subtile_idy * SUB_ROWS + " ~ (r * 8) ~ "u) * TILE_K + step", "TILE_K") }};
175
  {% endfor %}
176
  {% for c in range(4) %}
177
  {% set source = ("fc3_experts_weights" if second and c >= 2 and hasFc3 else "fc1_experts_weights") if ffn else "fc2_experts_weights" %}
178
  {% set stride = hidden * (2 if second and not hasFc3 and swigluFusion == 1 else 1) if ffn else inter %}
179
+ let matB{{ c }} = {{ matrixLoad(matrixSoftware, "right", "f32", "col_major", "&" ~ (source), weight_offset(c), (stride) ~ "u") }};
180
  {% endfor %}
181
  {% for r in range(2) %}{% for c in range(4) %}
182
+ mat{{ ["C","D","E","F"][stepIndex % 2] }}{{ r }}{{ c }} = {{ matrixMma(matrixSoftware, "f32", "matA" ~ (r), "matB" ~ (c), "mat" ~ (["C","D","E","F"][stepIndex % 2]) ~ (r) ~ (c)) }};
183
  {% endfor %}{% endfor %}
184
 
185
  }
 
191
  {% for r in range(2) %}
192
  {% for c in range(4) %}
193
  {% for chain in range(2) %}
194
+ {{ matrixStore(matrixSoftware, "row_major", "&tile_A", (chain * 1024) ~ "u + (subtile_id * 4u + " ~ (c) ~ "u) * 64u", "mat" ~ (["C","D","E","F"][chain]) ~ (r) ~ (c), "8u") }}
195
  {% endfor %}
196
  {% endfor %}
197
  workgroupBarrier();
 
237
  workgroupBarrier();
238
  {% endif %}
239
  {% endfor %}
240
+
241
+ {% endmacro %}
242
+ @compute @workgroup_size(128){{ " @subgroup_size(32)" if pinSubgroupSize32 else "" }}
243
+ fn main(
244
+ @builtin(workgroup_id) wid: vec3<u32>,
245
+ @builtin(local_invocation_index) local_idx: u32,
246
+ @builtin(subgroup_size) matrixPhysicalWidth: u32,
247
+ @builtin(subgroup_invocation_id) matrixPhysicalLane: u32
248
+ ) {
249
+ let matrixRole = claimMatrixSubgroupRole(matrixPhysicalWidth, 32u);
250
+ let matrixAcceptedHere = matrixTopologyAccepted(local_idx, 128u / 32u);
251
+ matrixSoftwareLane = local_idx % 32u;
252
+ if (matrixAcceptedHere) {
253
+ let lane = matrixPhysicalLane;
254
+ {{ matrixKernelBody(false) }}
255
+ } else {
256
+ let matrixRole = local_idx / 32u;
257
+ let lane = local_idx % 32u;
258
+ {{ matrixKernelBody(true) }}
259
+ }
260
  }
build/webgpu/moe-output-grouped.wgsl.jinja CHANGED
@@ -16,7 +16,12 @@ const THREADS_SIDE: u32 = {{ groupThreads }}u;
16
  const THREADS: u32 = THREADS_SIDE * THREADS_SIDE;
17
 
18
  const KVEC: u32 = {{ groupTileKVec }}u;
19
- {% macro stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, guarded, bLoadVec4="", b2LoadVec4="", aLoadVec4="") %}
 
 
 
 
 
20
  for (var idx = tid; idx < MTILE * KVEC; idx = idx + THREADS) {
21
  let m = idx / KVEC;
22
  let kb = (idx % KVEC) * 4u;
@@ -33,9 +38,51 @@ const KVEC: u32 = {{ groupTileKVec }}u;
33
  {% endif %}
34
  a_tile[idx] = a_vec;
35
  }
36
- for (var idx = tid; idx < NTILE * KVEC; idx = idx + THREADS) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  let n = idx / KVEC;
38
  let kb = (idx % KVEC) * 4u;
 
39
  let col = n_base + n;
40
  {% if guarded %}
41
  let col_live = col < {{ nExtent }};
@@ -63,16 +110,22 @@ const KVEC: u32 = {{ groupTileKVec }}u;
63
  }
64
  {% endfor %}
65
  {% endif %}
66
- b_tile[idx] = b_vec;
67
  {% if b2Load %}
68
- b2_tile[idx] = b2_vec;
69
  {% endif %}
70
- }{% endmacro %}
71
 
72
- {% macro group_tile_loop(aLoad, bLoad, b2Load, kExtent, nExtent, regM, regN, bLoadVec4="", b2LoadVec4="", aLoadVec4="") %}
73
  {% for r in range(regM) %}
74
  {% for c in range(regN) %}
75
  var acc_{{ r }}_{{ c }} = 0.0;
 
 
 
 
 
 
76
  {% endfor %}
77
  {% endfor %}
78
 
@@ -89,26 +142,67 @@ const KVEC: u32 = {{ groupTileKVec }}u;
89
  break;
90
  }
91
  if (n_full && k_base + KTILE <= {{ kExtent }}) {
92
- {{ stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, false, bLoadVec4, b2LoadVec4, aLoadVec4) }}
93
  } else {
94
- {{ stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, true) }}
95
  }
96
  workgroupBarrier();
97
 
 
 
 
 
 
 
 
 
 
98
  for (var kv = 0u; kv < KVEC; kv = kv + 1u) {
 
99
  {% for r in range(regM) %}
100
  let a{{ r }} = a_tile[(lid.y * {{ regM }}u + {{ r }}u) * KVEC + kv];
101
  {% endfor %}
102
- {% for c in range(regN) %}
103
- let b{{ c }} = b_tile[(lid.x * {{ regN }}u + {{ c }}u) * KVEC + kv];
 
 
 
 
 
 
 
 
104
  {% endfor %}
105
  {% for r in range(regM) %}
106
  {% for c in range(regN) %}
107
- acc_{{ r }}_{{ c }} = acc_{{ r }}_{{ c }} + dot(a{{ r }}, b{{ c }});
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  {% endfor %}
109
  {% endfor %}
110
  }
111
- // Orders this step's tile reads before the next step overwrites them.
 
 
 
 
 
 
 
 
112
  workgroupBarrier();
113
  k_base = k_base + KTILE;
114
  }{% endmacro %}
@@ -143,7 +237,7 @@ fn main(@builtin(workgroup_id) wid: vec3<u32>,
143
  "", "INTER", "HIDDEN", regM, regN,
144
  at4("fc2_experts_weights", bPre ~ "kb") if groupVec4 else "",
145
  "",
146
- at4("hidden_act", aPre ~ "kb") if groupVec4 else "") }}
147
 
148
  {% for r in range(regM) %}
149
  {% for c in range(regN) %}
 
16
  const THREADS: u32 = THREADS_SIDE * THREADS_SIDE;
17
 
18
  const KVEC: u32 = {{ groupTileKVec }}u;
19
+ {% macro stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, guarded, bLoadVec4="", b2LoadVec4="", aLoadVec4="", bKMajor=false, bLaneInterleaved=false, regN=1, staticK=false, bStageInterleaved=false, packedStreams=[]) %}{% set bStoreIndex =
20
+ "idx" if bStageInterleaved and bKMajor and bLaneInterleaved else
21
+ ("(idx % KVEC) * NTILE + n / " ~ regN ~ "u + (n % " ~ regN ~ "u) * " ~ ((groupTileN / regN) | int) ~ "u")
22
+ if bKMajor and bLaneInterleaved and regN > 1
23
+ else ("(idx % KVEC) * NTILE + idx / KVEC" if bKMajor else "idx")
24
+ %}
25
  for (var idx = tid; idx < MTILE * KVEC; idx = idx + THREADS) {
26
  let m = idx / KVEC;
27
  let kb = (idx % KVEC) * 4u;
 
38
  {% endif %}
39
  a_tile[idx] = a_vec;
40
  }
41
+ {% if packedStreams and not guarded and bKMajor and bLaneInterleaved %}
42
+ // Stage columns in shared-memory lane order for contiguous constant-K stores.
43
+ for (var slot = tid; slot < NTILE; slot = slot + THREADS) {
44
+ let local_col = (slot % THREADS_SIDE) * {{ regN }}u + slot / THREADS_SIDE;
45
+ let col = n_base + local_col;
46
+ {% if packedStreams[0].packing == "word32" %}
47
+ {% for stream in packedStreams %}
48
+ let {{ stream.prefix }}_packed = {{ stream.buffer }}[col * ({{ kExtent }} / 32u) + k_base / 32u];
49
+ let {{ stream.prefix }}_scale = f32({{ stream.scales }}[col * {{ stream.scaleBlocks }} + k_base / {{ stream.quantBlock }}]);
50
+ {% endfor %}
51
+ {% for chunk in range(8) %}
52
+ {% for stream in packedStreams %}
53
+ let {{ stream.prefix }}_word{{ chunk }} = {{ stream.prefix }}_packed[{{ (chunk / 2) | int }}] >> {{ (chunk % 2) * 16 }}u;
54
+ let {{ stream.prefix }}_values{{ chunk }} = (vec4<f32>((vec4<u32>({{ stream.prefix }}_word{{ chunk }}) >> vec4<u32>(0u, 4u, 8u, 12u)) & vec4<u32>({{ stream.mask }})) - vec4<f32>({{ stream.zero }})) * {{ stream.prefix }}_scale;
55
+ {{ stream.destination }}[{{ chunk }}u * NTILE + slot] = {{ stream.prefix }}_values{{ chunk }};
56
+ {% endfor %}
57
+ {% endfor %}
58
+ {% else %}
59
+ {% for block in range(groupTileK // 8) %}
60
+ {% for stream in packedStreams %}
61
+ let {{ stream.prefix }}_row{{ block }} = expert * {{ stream.rows }} + {{ stream.row }};
62
+ let {{ stream.prefix }}_packed{{ block }} = {{ stream.buffer }}[{{ stream.prefix }}_row{{ block }} * {{ stream.packedCols // 4 }}u + (k_base + {{ block * 8 }}u) / 8u];
63
+ let {{ stream.prefix }}_scale{{ block }} = {{ stream.scales }}[{{ stream.prefix }}_row{{ block }}{% if not stream.columnScales %} * {{ stream.scaleBlocks }}u + (k_base + {{ block * 8 }}u) / {{ stream.quantBlock }}{% endif %}];
64
+ {% for chunk in range(2) %}
65
+ {% if stream.packing == "word8" %}
66
+ let {{ stream.prefix }}_codes{{ block }}_{{ chunk }} = (vec4<u32>({{ stream.prefix }}_packed{{ block }}) >> vec4<u32>({{ chunk * 16 }}u, {{ chunk * 16 + 4 }}u, {{ chunk * 16 + 8 }}u, {{ chunk * 16 + 12 }}u)) & vec4<u32>(15u);
67
+ {% else %}
68
+ let {{ stream.prefix }}_bytes{{ block }}_{{ chunk }} = {{ stream.prefix }}_packed{{ block }}.{{ 'xy' if chunk == 0 else 'zw' }};
69
+ let {{ stream.prefix }}_codes{{ block }}_{{ chunk }} = vec4<u32>({{ stream.prefix }}_bytes{{ block }}_{{ chunk }}.x, {{ stream.prefix }}_bytes{{ block }}_{{ chunk }}.x >> 4u, {{ stream.prefix }}_bytes{{ block }}_{{ chunk }}.y, {{ stream.prefix }}_bytes{{ block }}_{{ chunk }}.y >> 4u) & vec4<u32>(15u);
70
+ {% endif %}
71
+ {{ stream.destination }}[{{ block * 2 + chunk }}u * NTILE + slot] = (vec4<f32>({{ stream.prefix }}_codes{{ block }}_{{ chunk }}) - vec4<f32>({{ stream.zero }})) * {{ stream.prefix }}_scale{{ block }};
72
+ {% endfor %}
73
+ {% endfor %}
74
+ {% endfor %}
75
+ {% endif %}
76
+ }
77
+ {% else %} for (var idx = tid; idx < NTILE * KVEC; idx = idx + THREADS) {
78
+ {% if bStageInterleaved and bKMajor and bLaneInterleaved %}
79
+ let slot = idx % NTILE;
80
+ let n = (slot % THREADS_SIDE) * {{ regN }}u + slot / THREADS_SIDE;
81
+ let kb = (idx / NTILE) * 4u;
82
+ {% else %}
83
  let n = idx / KVEC;
84
  let kb = (idx % KVEC) * 4u;
85
+ {% endif %}
86
  let col = n_base + n;
87
  {% if guarded %}
88
  let col_live = col < {{ nExtent }};
 
110
  }
111
  {% endfor %}
112
  {% endif %}
113
+ b_tile[{{ bStoreIndex }}] = b_vec;
114
  {% if b2Load %}
115
+ b2_tile[{{ bStoreIndex }}] = b2_vec;
116
  {% endif %}
117
+ }{% endif %}{% endmacro %}
118
 
119
+ {% macro group_tile_loop(aLoad, bLoad, b2Load, kExtent, nExtent, regM, regN, bLoadVec4="", b2LoadVec4="", aLoadVec4="", scalarFma=false, bKMajor=false, bLaneInterleaved=false, staticK=false, bStageInterleaved=false, packedStreams=[], foldStage=false, refinePair=false) %}{% set foldStage = foldStage or refinePair %}{% set accum2 = "chunk2" if refinePair else "acc2" %}{% set accum = "chunk" if foldStage else "acc" %}
120
  {% for r in range(regM) %}
121
  {% for c in range(regN) %}
122
  var acc_{{ r }}_{{ c }} = 0.0;
123
+ {% if b2Load %}
124
+ var acc2_{{ r }}_{{ c }} = 0.0;
125
+ {% endif %}
126
+ {% if refinePair %}{% for stream in ["", "2"] %}
127
+ var mass{{ stream }}_{{ r }}_{{ c }} = 0.0;
128
+ {% endfor %}{% endif %}
129
  {% endfor %}
130
  {% endfor %}
131
 
 
142
  break;
143
  }
144
  if (n_full && k_base + KTILE <= {{ kExtent }}) {
145
+ {{ stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, false, bLoadVec4, b2LoadVec4, aLoadVec4, bKMajor, bLaneInterleaved, regN, staticK, bStageInterleaved, packedStreams) }}
146
  } else {
147
+ {{ stage_group_tiles(aLoad, bLoad, b2Load, kExtent, nExtent, true, bKMajor=bKMajor, bLaneInterleaved=bLaneInterleaved, regN=regN, staticK=staticK, bStageInterleaved=bStageInterleaved, packedStreams=packedStreams) }}
148
  }
149
  workgroupBarrier();
150
 
151
+ {% if foldStage %}{% for r in range(regM) %}{% for c in range(regN) %}
152
+ var chunk_{{ r }}_{{ c }} = 0.0;
153
+ {% if refinePair %} var chunk2_{{ r }}_{{ c }} = 0.0;
154
+ {% endif %}
155
+ {% endfor %}{% endfor %}{% endif %}{% for staticKv in (range(groupTileKVec) if staticK else [-1]) %}
156
+ {% if staticK %}
157
+ {
158
+ const kv: u32 = {{ staticKv }}u;
159
+ {% else %}
160
  for (var kv = 0u; kv < KVEC; kv = kv + 1u) {
161
+ {% endif %}
162
  {% for r in range(regM) %}
163
  let a{{ r }} = a_tile[(lid.y * {{ regM }}u + {{ r }}u) * KVEC + kv];
164
  {% endfor %}
165
+ {% for c in range(regN) %}{% set bReadIndex =
166
+ ("kv * NTILE + lid.x + " ~ ((c * groupTileN / regN) | int) ~ "u")
167
+ if bKMajor and bLaneInterleaved and regN > 1
168
+ else (("kv * NTILE + lid.x * " ~ regN ~ "u + " ~ c ~ "u") if bKMajor
169
+ else ("(lid.x * " ~ regN ~ "u + " ~ c ~ "u) * KVEC + kv"))
170
+ %}
171
+ let b{{ c }} = b_tile[{{ bReadIndex }}];
172
+ {% if b2Load %}
173
+ let s{{ c }} = b2_tile[{{ bReadIndex }}];
174
+ {% endif %}
175
  {% endfor %}
176
  {% for r in range(regM) %}
177
  {% for c in range(regN) %}
178
+ {% if scalarFma %}
179
+ {% for component in ['x', 'y', 'z', 'w'] %}
180
+ {{ accum }}_{{ r }}_{{ c }} = fma(a{{ r }}.{{ component }}, b{{ c }}.{{ component }}, {{ accum }}_{{ r }}_{{ c }});
181
+ {% endfor %}
182
+ {% else %}
183
+ {{ accum }}_{{ r }}_{{ c }} = {{ accum }}_{{ r }}_{{ c }} + dot(a{{ r }}, b{{ c }});
184
+ {% endif %}
185
+ {% if b2Load %}
186
+ {% if scalarFma %}
187
+ {% for component in ['x', 'y', 'z', 'w'] %}
188
+ {{ accum2 }}_{{ r }}_{{ c }} = fma(a{{ r }}.{{ component }}, s{{ c }}.{{ component }}, {{ accum2 }}_{{ r }}_{{ c }});
189
+ {% endfor %}
190
+ {% else %}
191
+ {{ accum2 }}_{{ r }}_{{ c }} = {{ accum2 }}_{{ r }}_{{ c }} + dot(a{{ r }}, s{{ c }});
192
+ {% endif %}
193
+ {% endif %}
194
  {% endfor %}
195
  {% endfor %}
196
  }
197
+ {% endfor %}
198
+ {% if foldStage %}{% for r in range(regM) %}{% for c in range(regN) %}
199
+ {% if refinePair %}{% for stream in ["", "2"] %}{% set id = stream ~ "_" ~ r ~ "_" ~ c %}
200
+ mass{{ id }} += abs(chunk{{ id }});
201
+ acc{{ id }} += chunk{{ id }};
202
+ {% endfor %}{% else %}
203
+ acc_{{ r }}_{{ c }} += chunk_{{ r }}_{{ c }};
204
+ {% endif %}
205
+ {% endfor %}{% endfor %}{% endif %} // Orders this step's tile reads before the next step overwrites them.
206
  workgroupBarrier();
207
  k_base = k_base + KTILE;
208
  }{% endmacro %}
 
237
  "", "INTER", "HIDDEN", regM, regN,
238
  at4("fc2_experts_weights", bPre ~ "kb") if groupVec4 else "",
239
  "",
240
+ at4("hidden_act", aPre ~ "kb") if groupVec4 else "", scalarFma=expertF32Schedule, bKMajor=groupWeightKMajor, bLaneInterleaved=groupWeightKMajor, staticK=expertF32Schedule, bStageInterleaved=expertF32Schedule) }}
241
 
242
  {% for r in range(regM) %}
243
  {% for c in range(regN) %}
build/webgpu/moe-output-stage.wgsl.jinja CHANGED
@@ -1,8 +1,9 @@
1
- {% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}
2
  {% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
3
  // 2D-folded flat index: gid.y carries the high bits past the dispatch's
4
  // per-axis workgroup fold width.
5
- let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};{% endmacro %}
 
6
  {{ env.wgsl.resourceDeclarations }}
7
 
8
  // FC2 projection and the routed sum. One thread owns one output column of one token and walks
 
1
+ {% macro flat_index_2d(workgroupSize, name="i", bound="params.count") %}
2
  {% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
3
  // 2D-folded flat index: gid.y carries the high bits past the dispatch's
4
  // per-axis workgroup fold width.
5
+ let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};
6
+ {% endmacro %}
7
  {{ env.wgsl.resourceDeclarations }}
8
 
9
  // FC2 projection and the routed sum. One thread owns one output column of one token and walks
build/webgpu/moe-route-stage.wgsl.jinja CHANGED
@@ -1,11 +1,12 @@
1
- {% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}
2
  {% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
3
  // 2D-folded flat index: gid.y carries the high bits past the dispatch's
4
  // per-axis workgroup fold width.
5
  let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};
6
  if ({{ name }} >= {{ bound }}) {
7
  return;
8
- }{% endmacro %}
 
9
  {{ env.wgsl.resourceDeclarations }}
10
 
11
  // One thread per token applies softmax to the router logits, selects TOP_K
 
1
+ {% macro flat_index_2d(workgroupSize, name="i", bound="params.count") %}
2
  {% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
3
  // 2D-folded flat index: gid.y carries the high bits past the dispatch's
4
  // per-axis workgroup fold width.
5
  let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};
6
  if ({{ name }} >= {{ bound }}) {
7
  return;
8
+ }
9
+ {% endmacro %}
10
  {{ env.wgsl.resourceDeclarations }}
11
 
12
  // One thread per token applies softmax to the router logits, selects TOP_K
build/webgpu/test.json CHANGED
The diff for this file is too large to render. See raw diff