Xenova HF Staff commited on
Commit
f85db5e
·
verified ·
1 Parent(s): 5d5e27b

sync 2e7068faf55e

Browse files
README.md CHANGED
@@ -1,3 +1,76 @@
1
  ---
 
2
  license: apache-2.0
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ library_name: kernels
3
  license: apache-2.0
4
+ tags:
5
+ - kernel
6
+ - webgpu
7
+ - wgsl
8
  ---
9
+ # ai.onnx.Scan
10
+
11
+ `ai.onnx` · internal tensor lowering (non-standard) · reviewed against ONNX opset 25
12
+
13
+ ## Description
14
+
15
+ Support status: the standard variadic ONNX `Scan` control-flow operator is not implemented because standalone kernel packages cannot carry or execute its body graph. This internal lowering computes a forward or reverse additive prefix scan for one rank-1 state and one rank-2 input and must not be treated as ONNX `Scan`.
16
+
17
+ See the [standard ONNX `Scan` spec](https://onnx.ai/onnx/operators/onnx__Scan.html) for the contract this internal lowering does not implement.
18
+
19
+ ## Inputs
20
+
21
+ | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
22
+ | --- | --- | --- | --- | --- | --- | --- |
23
+ | `initial_state` | `initial_state` | `T` | `1` | — | Initial rank-1 additive state of shape `[dim]`. | required |
24
+ | `scan_input` | `scan_input` | `T` | `2` | — | Rank-2 input of shape `[steps, dim]`. Each row is added to the running state in forward or reverse traversal order. | required |
25
+
26
+ ## Outputs
27
+
28
+ | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
29
+ | --- | --- | --- | --- | --- | --- | --- |
30
+ | `final_state` | `final_state` | `T` | `1` | same as `initial_state` | Final rank-1 state of shape `[dim]` after all input rows have been accumulated. | required |
31
+ | `scan_output` | `scan_output` | `T` | `2` | same as `scan_input` | Inclusive additive prefix results with the same `[steps, dim]` shape as `scan_input`. Reverse traversal still writes each result at its corresponding input row. | required |
32
+
33
+ ## Attributes
34
+
35
+ Default values (overridable per request):
36
+
37
+ | Attribute | Default | Description |
38
+ | --- | --- | --- |
39
+ | `reverse` | `0` | When non-zero, the scan input sequence is traversed in reverse order (equivalent to `scan_input_directions=1`); default `0` scans forward. |
40
+
41
+ ## Type constraints
42
+
43
+ | Variable | Allowed dtypes |
44
+ | --- | --- |
45
+ | `T` | `float32` |
46
+
47
+ ## Files
48
+
49
+ - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, provenance)
50
+ - [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)
51
+ - [`test.json`](build/webgpu/test.json) — correctness cases
52
+ - [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases
53
+ - [`scan-coop-channel.wgsl.jinja`](build/webgpu/scan-coop-channel.wgsl.jinja)
54
+ - [`scan-multichunk-apply.wgsl.jinja`](build/webgpu/scan-multichunk-apply.wgsl.jinja)
55
+ - [`scan-multichunk-carries.wgsl.jinja`](build/webgpu/scan-multichunk-carries.wgsl.jinja)
56
+ - [`scan-multichunk-local.wgsl.jinja`](build/webgpu/scan-multichunk-local.wgsl.jinja)
57
+ - [`scan-prefix-sum.wgsl.jinja`](build/webgpu/scan-prefix-sum.wgsl.jinja)
58
+
59
+ ## Use with `@huggingface/kernels`
60
+
61
+ The loader derives every required output's shape and logical dtype from the manifest contract and this call.
62
+ It then allocates the result tensors automatically.
63
+
64
+ The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.
65
+
66
+ Replace each `*Data` placeholder with a typed array containing the corresponding input data.
67
+
68
+ ```js
69
+ import { getKernel } from "@huggingface/kernels";
70
+
71
+ const kernel = await getKernel("webgpu-kernels/ai.onnx.Scan", { version: 1 });
72
+ const { final_state, scan_output } = await kernel({
73
+ initial_state: { data: initial_stateData, shape: [1] },
74
+ scan_input: { data: scan_inputData, shape: [3, 1] },
75
+ });
76
+ ```
build/webgpu/bench.json ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "op": "ai.onnx.Scan",
3
+ "cases": [
4
+ {
5
+ "name": "lowered_prefix_sum_256x512",
6
+ "preset": "smoke",
7
+ "inputs": {
8
+ "initial_state": { "dtype": "float32", "shape": [512], "dist": "normal", "seed": 741, "scale": 0.1 },
9
+ "scan_input": { "dtype": "float32", "shape": [256, 512], "dist": "normal", "seed": 742, "scale": 0.1 }
10
+ },
11
+ "outputs": {
12
+ "final_state": { "dtype": "float32", "shape": [512] },
13
+ "scan_output": { "dtype": "float32", "shape": [256, 512] }
14
+ },
15
+ "bench": { "primary": true, "metrics": [{ "type": "bandwidth", "value": "256 * 512 * 4 * 2" }] }
16
+ },
17
+ {
18
+ "name": "lowered_prefix_sum_2048x512",
19
+ "inputs": {
20
+ "initial_state": { "dtype": "float32", "shape": [512], "dist": "normal", "seed": 743, "scale": 0.1 },
21
+ "scan_input": { "dtype": "float32", "shape": [2048, 512], "dist": "normal", "seed": 744, "scale": 0.1 }
22
+ },
23
+ "outputs": {
24
+ "final_state": { "dtype": "float32", "shape": [512] },
25
+ "scan_output": { "dtype": "float32", "shape": [2048, 512] }
26
+ },
27
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "2048 * 512 * 4 * 2" }] }
28
+ },
29
+ {
30
+ "name": "lowered_prefix_sum_256x128",
31
+ "inputs": {
32
+ "initial_state": { "dtype": "float32", "shape": [128] },
33
+ "scan_input": { "dtype": "float32", "shape": [256, 128] }
34
+ },
35
+ "outputs": {
36
+ "final_state": { "dtype": "float32", "shape": [128] },
37
+ "scan_output": { "dtype": "float32", "shape": [256, 128] }
38
+ }
39
+ },
40
+ {
41
+ "name": "lowered_fewlane_serial_dim32_steps4096",
42
+ "preset": "smoke",
43
+ "inputs": {
44
+ "initial_state": { "dtype": "float32", "shape": [32], "dist": "normal", "seed": 751, "scale": 0.1 },
45
+ "scan_input": { "dtype": "float32", "shape": [4096, 32], "dist": "normal", "seed": 752, "scale": 0.1 }
46
+ },
47
+ "outputs": {
48
+ "final_state": { "dtype": "float32", "shape": [32] },
49
+ "scan_output": { "dtype": "float32", "shape": [4096, 32] }
50
+ },
51
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "2 * 4 * numel(shapes.scan_input)" }] }
52
+ },
53
+ {
54
+ "name": "coop_healthy_dim512_steps4096",
55
+ "preset": "smoke",
56
+ "inputs": {
57
+ "initial_state": { "dtype": "float32", "shape": [512], "dist": "normal", "seed": 753, "scale": 0.1 },
58
+ "scan_input": { "dtype": "float32", "shape": [4096, 512], "dist": "normal", "seed": 754, "scale": 0.1 }
59
+ },
60
+ "outputs": {
61
+ "final_state": { "dtype": "float32", "shape": [512] },
62
+ "scan_output": { "dtype": "float32", "shape": [4096, 512] }
63
+ },
64
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "2 * 4 * numel(shapes.scan_input)" }] }
65
+ },
66
+ {
67
+ "name": "lowered_largedim_fallback_dim4096_steps1024",
68
+ "preset": "smoke",
69
+ "inputs": {
70
+ "initial_state": { "dtype": "float32", "shape": [4096], "dist": "normal", "seed": 755, "scale": 0.1 },
71
+ "scan_input": { "dtype": "float32", "shape": [1024, 4096], "dist": "normal", "seed": 756, "scale": 0.1 }
72
+ },
73
+ "outputs": {
74
+ "final_state": { "dtype": "float32", "shape": [4096] },
75
+ "scan_output": { "dtype": "float32", "shape": [1024, 4096] }
76
+ },
77
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "2 * 4 * numel(shapes.scan_input)" }] }
78
+ },
79
+ {
80
+ "name": "lowered_smalldim2_singlelane_steps8192",
81
+ "preset": "smoke",
82
+ "inputs": {
83
+ "initial_state": { "dtype": "float32", "shape": [2], "dist": "normal", "seed": 757, "scale": 0.1 },
84
+ "scan_input": { "dtype": "float32", "shape": [8192, 2], "dist": "normal", "seed": 758, "scale": 0.1 }
85
+ },
86
+ "outputs": {
87
+ "final_state": { "dtype": "float32", "shape": [2] },
88
+ "scan_output": { "dtype": "float32", "shape": [8192, 2] }
89
+ },
90
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "2 * 4 * numel(shapes.scan_input)" }] }
91
+ },
92
+ {
93
+ "name": "coop_reverse_partialchunk_dim512_steps300",
94
+ "preset": "smoke",
95
+ "attrs": { "reverse": 1 },
96
+ "inputs": {
97
+ "initial_state": { "dtype": "float32", "shape": [512], "dist": "normal", "seed": 759, "scale": 0.1 },
98
+ "scan_input": { "dtype": "float32", "shape": [300, 512], "dist": "normal", "seed": 760, "scale": 0.1 }
99
+ },
100
+ "outputs": {
101
+ "final_state": { "dtype": "float32", "shape": [512] },
102
+ "scan_output": { "dtype": "float32", "shape": [300, 512] }
103
+ },
104
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "2 * 4 * numel(shapes.scan_input)" }] }
105
+ },
106
+ {
107
+ "name": "lowered_singlelane_dim1_steps65536",
108
+ "preset": "stress",
109
+ "inputs": {
110
+ "initial_state": { "dtype": "float32", "shape": [1], "dist": "normal", "seed": 811, "scale": 0.1 },
111
+ "scan_input": { "dtype": "float32", "shape": [65536, 1], "dist": "normal", "seed": 812, "scale": 0.1 }
112
+ },
113
+ "outputs": {
114
+ "final_state": { "dtype": "float32", "shape": [1] },
115
+ "scan_output": { "dtype": "float32", "shape": [65536, 1] }
116
+ },
117
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "2 * 4 * numel(shapes.scan_input)" }] }
118
+ },
119
+ {
120
+ "name": "lowered_dim63_steps32768_one_workgroup_launchbound",
121
+ "preset": "stress",
122
+ "inputs": {
123
+ "initial_state": { "dtype": "float32", "shape": [63], "dist": "normal", "seed": 813, "scale": 0.1 },
124
+ "scan_input": { "dtype": "float32", "shape": [32768, 63], "dist": "normal", "seed": 814, "scale": 0.1 }
125
+ },
126
+ "outputs": {
127
+ "final_state": { "dtype": "float32", "shape": [63] },
128
+ "scan_output": { "dtype": "float32", "shape": [32768, 63] }
129
+ },
130
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "2 * 4 * numel(shapes.scan_input)" }] }
131
+ },
132
+ {
133
+ "name": "streaming_recurrence_dim8_steps131072_multichunk_pathology",
134
+ "preset": "stress",
135
+ "inputs": {
136
+ "initial_state": { "dtype": "float32", "shape": [8], "dist": "normal", "seed": 815, "scale": 0.1 },
137
+ "scan_input": { "dtype": "float32", "shape": [131072, 8], "dist": "normal", "seed": 816, "scale": 0.01 }
138
+ },
139
+ "outputs": {
140
+ "final_state": { "dtype": "float32", "shape": [8] },
141
+ "scan_output": { "dtype": "float32", "shape": [131072, 8] }
142
+ },
143
+ "bench": { "primary": true, "metrics": [{ "type": "bandwidth", "value": "2 * 4 * numel(shapes.scan_input)" }] }
144
+ }
145
+ ]
146
+ }
build/webgpu/manifest.json ADDED
@@ -0,0 +1,266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "domain": "ai.onnx",
3
+ "name": "Scan",
4
+ "conformance": "internal-lowering",
5
+ "sinceVersion": 25,
6
+ "description": "Support status: the standard variadic ONNX `Scan` control-flow operator is not implemented because standalone kernel packages cannot carry or execute its body graph. This internal lowering computes a forward or reverse additive prefix scan for one rank-1 state and one rank-2 input and must not be treated as ONNX `Scan`.",
7
+ "inputs": [
8
+ {
9
+ "role": "initial_state",
10
+ "dtype": "T",
11
+ "rank": 1,
12
+ "description": "Initial rank-1 additive state of shape `[dim]`."
13
+ },
14
+ {
15
+ "role": "scan_input",
16
+ "dtype": "T",
17
+ "rank": 2,
18
+ "description": "Rank-2 input of shape `[steps, dim]`. Each row is added to the running state in forward or reverse traversal order."
19
+ }
20
+ ],
21
+ "outputs": [
22
+ {
23
+ "role": "final_state",
24
+ "dtype": "T",
25
+ "rank": 1,
26
+ "description": "Final rank-1 state of shape `[dim]` after all input rows have been accumulated.",
27
+ "shape": "shapes.initial_state"
28
+ },
29
+ {
30
+ "role": "scan_output",
31
+ "dtype": "T",
32
+ "rank": 2,
33
+ "description": "Inclusive additive prefix results with the same `[steps, dim]` shape as `scan_input`. Reverse traversal still writes each result at its corresponding input row.",
34
+ "shape": "shapes.scan_input"
35
+ }
36
+ ],
37
+ "attributes": { "reverse": 0 },
38
+ "attributeDescriptions": {
39
+ "reverse": "When non-zero, the scan input sequence is traversed in reverse order (equivalent to `scan_input_directions=1`); default `0` scans forward."
40
+ },
41
+ "attributeConstraints": { "reverse": { "values": [0, 1] } },
42
+ "typeConstraints": { "T": ["float32"] },
43
+ "tunables": { "WORKGROUP_SIZE": 256 },
44
+ "derive": {
45
+ "wave32Adapter": "has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize == 32 and device.adapterInfo.subgroupMaxSize == 32",
46
+ "subgroupsWave32": "device.features.has(\"subgroups\") and wave32Adapter",
47
+ "variableNarrowSubgroups": "device.features.has(\"subgroups\") and has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize < device.adapterInfo.subgroupMaxSize and device.adapterInfo.subgroupMaxSize <= 16",
48
+ "parallelSubgroupScan": "subgroupsWave32 or variableNarrowSubgroups",
49
+ "scanUseSubgroups": "parallelSubgroupScan"
50
+ },
51
+ "args": {
52
+ "initial_state": { "kind": "tensor", "semantic": "initial_state", "role": "input" },
53
+ "scan_input": { "kind": "tensor", "semantic": "scan_input", "role": "input" },
54
+ "final_state": { "kind": "tensor", "semantic": "final_state", "role": "output" },
55
+ "scan_output": { "kind": "tensor", "semantic": "scan_output", "role": "output" }
56
+ },
57
+ "bindingSets": {
58
+ "scanState": [
59
+ {
60
+ "name": "initial_state",
61
+ "arg": "initial_state",
62
+ "semantic": "initial_state",
63
+ "buffer": { "type": "read-only-storage" },
64
+ "elementType": "f32"
65
+ },
66
+ {
67
+ "name": "scan_input",
68
+ "arg": "scan_input",
69
+ "semantic": "scan_input",
70
+ "buffer": { "type": "read-only-storage" },
71
+ "elementType": "f32"
72
+ },
73
+ {
74
+ "name": "final_state",
75
+ "arg": "final_state",
76
+ "semantic": "final_state",
77
+ "buffer": { "type": "storage" },
78
+ "elementType": "f32"
79
+ },
80
+ {
81
+ "name": "scan_output",
82
+ "arg": "scan_output",
83
+ "semantic": "scan_output",
84
+ "buffer": { "type": "storage" },
85
+ "elementType": "f32"
86
+ },
87
+ {
88
+ "name": "params",
89
+ "semantic": "kernel.params",
90
+ "buffer": { "type": "uniform" },
91
+ "struct": {
92
+ "name": "Params",
93
+ "fields": [
94
+ { "name": "steps", "type": "u32", "value": "dim(shapes.scan_input, 0)" },
95
+ { "name": "dim", "type": "u32", "value": "dim(shapes.scan_input, 1)" },
96
+ { "name": "reverse", "type": "u32", "value": "attrs.reverse" }
97
+ ]
98
+ }
99
+ }
100
+ ]
101
+ },
102
+ "variants": [
103
+ {
104
+ "id": "multichunk_small_state",
105
+ "priority": 30,
106
+ "when": ["ranks.initial_state == 1", "ranks.scan_input == 2", "ranks.final_state == 1", "ranks.scan_output == 2", "dim(shapes.initial_state, 0) == dim(shapes.scan_input, 1)", "dim(shapes.final_state, 0) == dim(shapes.initial_state, 0)", "dim(shapes.scan_output, 0) == dim(shapes.scan_input, 0)", "dim(shapes.scan_output, 1) == dim(shapes.scan_input, 1)", "dim(shapes.scan_input, 1) >= 1", "dim(shapes.scan_input, 1) <= 32", "dim(shapes.scan_input, 0) >= 4096", "dim(shapes.scan_input, 1) >= 1"],
107
+ "derive": {
108
+ "chunks": "ceil(dim(shapes.scan_input, 0) / tunables.WORKGROUP_SIZE)",
109
+ "totalChunks": "dim(shapes.scan_input, 1) * chunks"
110
+ },
111
+ "intermediates": [
112
+ { "id": "chunkTotals", "dtype": "float32", "shape": "[totalChunks]" },
113
+ { "id": "chunkCarries", "dtype": "float32", "shape": "[totalChunks]" }
114
+ ],
115
+ "passes": [
116
+ {
117
+ "id": "local",
118
+ "name": "Scan.MultichunkLocal",
119
+ "source": {
120
+ "shader": "scan-multichunk-local.wgsl.jinja",
121
+ "inputs": { "chunks": "chunks", "totalChunks": "totalChunks", "useSubgroups": "scanUseSubgroups" }
122
+ },
123
+ "bindings": [
124
+ {
125
+ "name": "scan_input",
126
+ "arg": "scan_input",
127
+ "semantic": "scan_input",
128
+ "buffer": { "type": "read-only-storage" },
129
+ "elementType": "f32"
130
+ },
131
+ {
132
+ "name": "scan_output",
133
+ "arg": "scan_output",
134
+ "semantic": "scan_output",
135
+ "buffer": { "type": "storage" },
136
+ "elementType": "f32"
137
+ },
138
+ { "name": "chunkTotals", "semantic": "chunkTotals", "buffer": { "type": "storage" }, "elementType": "f32" },
139
+ {
140
+ "name": "params",
141
+ "semantic": "kernel.params",
142
+ "buffer": { "type": "uniform" },
143
+ "struct": {
144
+ "name": "Params",
145
+ "fields": [
146
+ { "name": "steps", "type": "u32", "value": "dim(shapes.scan_input, 0)" },
147
+ { "name": "dim", "type": "u32", "value": "dim(shapes.scan_input, 1)" },
148
+ { "name": "reverse", "type": "u32", "value": "attrs.reverse" }
149
+ ]
150
+ }
151
+ }
152
+ ],
153
+ "dispatch": { "workgroups": "totalChunks" }
154
+ },
155
+ {
156
+ "id": "carries",
157
+ "name": "Scan.MultichunkCarries",
158
+ "source": { "shader": "scan-multichunk-carries.wgsl.jinja", "inputs": { "chunks": "chunks" } },
159
+ "bindings": [
160
+ {
161
+ "name": "initial_state",
162
+ "arg": "initial_state",
163
+ "semantic": "initial_state",
164
+ "buffer": { "type": "read-only-storage" },
165
+ "elementType": "f32"
166
+ },
167
+ {
168
+ "name": "chunkTotals",
169
+ "semantic": "chunkTotals",
170
+ "buffer": { "type": "read-only-storage" },
171
+ "elementType": "f32"
172
+ },
173
+ {
174
+ "name": "chunkCarries",
175
+ "semantic": "chunkCarries",
176
+ "buffer": { "type": "storage" },
177
+ "elementType": "f32"
178
+ },
179
+ {
180
+ "name": "final_state",
181
+ "arg": "final_state",
182
+ "semantic": "final_state",
183
+ "buffer": { "type": "storage" },
184
+ "elementType": "f32"
185
+ },
186
+ {
187
+ "name": "params",
188
+ "semantic": "kernel.params",
189
+ "buffer": { "type": "uniform" },
190
+ "struct": {
191
+ "name": "Params",
192
+ "fields": [{ "name": "dim", "type": "u32", "value": "dim(shapes.scan_input, 1)" }]
193
+ }
194
+ }
195
+ ],
196
+ "dispatch": { "threads": "dim(shapes.scan_input, 1)", "workgroupSize": "tunables.WORKGROUP_SIZE" }
197
+ },
198
+ {
199
+ "id": "apply",
200
+ "name": "Scan.MultichunkApply",
201
+ "source": { "shader": "scan-multichunk-apply.wgsl.jinja", "inputs": { "chunks": "chunks" } },
202
+ "bindings": [
203
+ {
204
+ "name": "chunkCarries",
205
+ "semantic": "chunkCarries",
206
+ "buffer": { "type": "read-only-storage" },
207
+ "elementType": "f32"
208
+ },
209
+ {
210
+ "name": "scan_output",
211
+ "arg": "scan_output",
212
+ "semantic": "scan_output",
213
+ "buffer": { "type": "storage" },
214
+ "elementType": "f32"
215
+ },
216
+ {
217
+ "name": "params",
218
+ "semantic": "kernel.params",
219
+ "buffer": { "type": "uniform" },
220
+ "struct": {
221
+ "name": "Params",
222
+ "fields": [
223
+ { "name": "count", "type": "u32", "value": "numel(shapes.scan_output)" },
224
+ { "name": "steps", "type": "u32", "value": "dim(shapes.scan_input, 0)" },
225
+ { "name": "dim", "type": "u32", "value": "dim(shapes.scan_input, 1)" },
226
+ { "name": "reverse", "type": "u32", "value": "attrs.reverse" }
227
+ ]
228
+ }
229
+ }
230
+ ],
231
+ "dispatch": { "threads": "numel(shapes.scan_output)", "workgroupSize": "tunables.WORKGROUP_SIZE" }
232
+ }
233
+ ]
234
+ },
235
+ {
236
+ "id": "coop_channel_prefix_sum",
237
+ "priority": 10,
238
+ "when": ["ranks.initial_state == 1", "ranks.scan_input == 2", "ranks.final_state == 1", "ranks.scan_output == 2", "dim(shapes.initial_state, 0) == dim(shapes.scan_input, 1)", "dim(shapes.final_state, 0) == dim(shapes.initial_state, 0)", "dim(shapes.scan_output, 0) == dim(shapes.scan_input, 0)", "dim(shapes.scan_output, 1) == dim(shapes.scan_input, 1)", "dim(shapes.scan_input, 1) >= 1", "dim(shapes.scan_input, 1) <= device.limits.maxComputeWorkgroupsPerDimension", "dim(shapes.scan_input, 0) >= 64", "dim(shapes.scan_input, 1) >= 1"],
239
+ "passes": [
240
+ {
241
+ "id": "main",
242
+ "name": "Scan.CoopChannel",
243
+ "source": {
244
+ "shader": "scan-coop-channel.wgsl.jinja",
245
+ "inputs": { "reverse": "attrs.reverse != 0", "useSubgroups": "scanUseSubgroups" }
246
+ },
247
+ "bindings": "scanState",
248
+ "dispatch": { "workgroups": "dim(shapes.scan_input, 1)" }
249
+ }
250
+ ]
251
+ },
252
+ {
253
+ "id": "lowered_prefix_sum",
254
+ "when": ["ranks.initial_state == 1", "ranks.scan_input == 2", "ranks.final_state == 1", "ranks.scan_output == 2", "dim(shapes.initial_state, 0) == dim(shapes.scan_input, 1)", "dim(shapes.final_state, 0) == dim(shapes.initial_state, 0)", "dim(shapes.scan_output, 0) == dim(shapes.scan_input, 0)", "dim(shapes.scan_output, 1) == dim(shapes.scan_input, 1)"],
255
+ "passes": [
256
+ {
257
+ "id": "main",
258
+ "name": "Scan",
259
+ "shader": "scan-prefix-sum.wgsl.jinja",
260
+ "bindings": "scanState",
261
+ "dispatch": { "threads": "dim(shapes.scan_input, 1)", "workgroupSize": "tunables.WORKGROUP_SIZE" }
262
+ }
263
+ ]
264
+ }
265
+ ]
266
+ }
build/webgpu/metadata.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "ai.onnx.Scan",
3
+ "id": "_ai_onnx_scan_webgpu_b9f0a4d",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "backend": { "type": "webgpu" },
7
+ "digest": {
8
+ "algorithm": "sha256",
9
+ "files": {
10
+ "bench.json": "0+C2HUd1flyDUAL11YlBp9o6XTgmDufVT4+as0ZGYZs=",
11
+ "manifest.json": "sOeqaF0C7CiKDFazgO3dRk9xClpZmysMuCawCo1rO7M=",
12
+ "scan-coop-channel.wgsl.jinja": "oxDxT4gqbqgj7CWwFwAm+RSjXJoNzaio+2Kn7ST6qwA=",
13
+ "scan-multichunk-apply.wgsl.jinja": "zrBQlGJuesvSZthECsjSYiSaULCb0AiLsIs1j0qCd0U=",
14
+ "scan-multichunk-carries.wgsl.jinja": "9mRyBR64N4aMcggHqIfg0GdfN7WSe2dxfiqiXb5hmgg=",
15
+ "scan-multichunk-local.wgsl.jinja": "BIVowPnPqSNj7WQwZFBnpmwrwmmCzJsYiuXSUcPt/9I=",
16
+ "scan-prefix-sum.wgsl.jinja": "/gbGyAZ3J8GXqcPXK86J1BKxNHjezV3/nvXsBTi5OHM=",
17
+ "test.json": "DCu1yx2q9qHCxbqB0yyT2mZN656tT+ja1Mrf9knXxKM="
18
+ }
19
+ },
20
+ "provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } },
21
+ "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/ai.onnx.Scan" }
22
+ }
build/webgpu/scan-coop-channel.wgsl.jinja ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Workgroup-per-channel prefix scan for small channel counts. Each workgroup
2
+ // scans the sequential steps axis in workgroup-sized chunks; channel i at step
3
+ // t is stored at t*dim + i. Reverse mode changes traversal direction.
4
+ //
5
+ {% if source.useSubgroups %}
6
+ // Each chunk combines an inclusive subgroup scan with a cross-subgroup carry.
7
+ {% else %}
8
+ // Each chunk uses a workgroup-wide Hillis-Steele scan.
9
+ {% endif %}
10
+ // Chunk-local sums reassociate the additions, while the cross-chunk carry
11
+ // preserves prefix dependencies. Float results therefore depend on this tree.
12
+ {% if source.useSubgroups %}
13
+ enable subgroups;
14
+ {% endif %}
15
+ {{ env.wgsl.resourceDeclarations }}
16
+
17
+ const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;
18
+ const IDENTITY: f32 = 0.0;
19
+
20
+ {% if source.useSubgroups %}
21
+ // One partial per subgroup; subgroup size bounds the count to WG/minSubgroup.
22
+ var<workgroup> sgTotals: array<f32, WG>;
23
+
24
+ @compute @workgroup_size(WG, 1, 1)
25
+ fn main(@builtin(workgroup_id) wg: vec3<u32>,
26
+ @builtin(num_workgroups) nwg: vec3<u32>,
27
+ @builtin(local_invocation_id) lid: vec3<u32>,
28
+ @builtin(subgroup_invocation_id) sgLane: u32,
29
+ @builtin(subgroup_size) sgSize: u32) {
30
+ // wg.y carries channel-index bits past the per-dimension dispatch limit.
31
+ let i = wg.x + wg.y * nwg.x;
32
+ let tid = lid.x;
33
+ if (i >= params.dim) {
34
+ return;
35
+ }
36
+ let safeSg = max(sgSize, 1u);
37
+ let sgId = tid / safeSg;
38
+ let numSub = (WG + safeSg - 1u) / safeSg;
39
+ var carry: f32 = initial_state[i];
40
+ let chunks = (params.steps + WG - 1u) / WG;
41
+ for (var c = 0u; c < chunks; c = c + 1u) {
42
+ let j = c * WG + tid;
43
+ let valid = j < params.steps;
44
+ let t = select(j, params.steps - 1u - j, params.reverse != 0u);
45
+ let addr = t * params.dim + i;
46
+ var v: f32 = IDENTITY;
47
+ if (valid) {
48
+ v = scan_input[addr];
49
+ }
50
+ let incSg = subgroupInclusiveAdd(v);
51
+ let totSg = subgroupAdd(v);
52
+ if (sgLane == 0u) {
53
+ sgTotals[min(sgId, WG - 1u)] = totSg;
54
+ }
55
+ workgroupBarrier();
56
+ var sgOffset: f32 = IDENTITY;
57
+ for (var s = 0u; s < sgId; s = s + 1u) {
58
+ sgOffset = sgOffset + sgTotals[s];
59
+ }
60
+ var chunkTotal: f32 = IDENTITY;
61
+ for (var s = 0u; s < numSub; s = s + 1u) {
62
+ chunkTotal = chunkTotal + sgTotals[s];
63
+ }
64
+ let value = carry + sgOffset + incSg;
65
+ if (valid) {
66
+ scan_output[addr] = value;
67
+ }
68
+ carry = carry + chunkTotal;
69
+ workgroupBarrier();
70
+ }
71
+ if (tid == 0u) {
72
+ final_state[i] = carry;
73
+ }
74
+ }
75
+ {% else %}
76
+ var<workgroup> wgScan: array<f32, WG>;
77
+
78
+ @compute @workgroup_size(WG, 1, 1)
79
+ fn main(@builtin(workgroup_id) wg: vec3<u32>,
80
+ @builtin(num_workgroups) nwg: vec3<u32>,
81
+ @builtin(local_invocation_id) lid: vec3<u32>) {
82
+ let i = wg.x + wg.y * nwg.x;
83
+ let tid = lid.x;
84
+ if (i >= params.dim) {
85
+ return;
86
+ }
87
+ var carry: f32 = initial_state[i];
88
+ let chunks = (params.steps + WG - 1u) / WG;
89
+ for (var c = 0u; c < chunks; c = c + 1u) {
90
+ let j = c * WG + tid;
91
+ let valid = j < params.steps;
92
+ let t = select(j, params.steps - 1u - j, params.reverse != 0u);
93
+ let addr = t * params.dim + i;
94
+ var v: f32 = IDENTITY;
95
+ if (valid) {
96
+ v = scan_input[addr];
97
+ }
98
+ workgroupBarrier();
99
+ wgScan[tid] = v;
100
+ for (var step = 1u; step < WG; step = step << 1u) {
101
+ workgroupBarrier();
102
+ var prev: f32 = IDENTITY;
103
+ if (tid >= step) {
104
+ prev = wgScan[tid - step];
105
+ }
106
+ workgroupBarrier();
107
+ wgScan[tid] = prev + wgScan[tid];
108
+ }
109
+ workgroupBarrier();
110
+ let value = carry + wgScan[tid];
111
+ if (valid) {
112
+ scan_output[addr] = value;
113
+ }
114
+ carry = carry + wgScan[WG - 1u];
115
+ workgroupBarrier();
116
+ }
117
+ if (tid == 0u) {
118
+ final_state[i] = carry;
119
+ }
120
+ }
121
+ {% endif %}
build/webgpu/scan-multichunk-apply.wgsl.jinja ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {{ env.wgsl.resourceDeclarations }}
2
+
3
+ const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;
4
+ const CHUNKS: u32 = {{ source.chunks }}u;
5
+
6
+ @compute @workgroup_size(WG)
7
+ fn main(@builtin(global_invocation_id) gid: vec3<u32>,
8
+ @builtin(num_workgroups) nwg: vec3<u32>) {
9
+ let i = gid.x + gid.y * nwg.x * WG;
10
+ if (i >= params.count) { return; }
11
+ let t = i / params.dim;
12
+ let channel = i % params.dim;
13
+ let iter = select(t, params.steps - 1u - t, params.reverse != 0u);
14
+ let chunk = iter / WG;
15
+ scan_output[i] += chunkCarries[channel * CHUNKS + chunk];
16
+ }
build/webgpu/scan-multichunk-carries.wgsl.jinja ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {{ env.wgsl.resourceDeclarations }}
2
+
3
+ const CHUNKS: u32 = {{ source.chunks }}u;
4
+
5
+ @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
6
+ fn main(@builtin(global_invocation_id) gid: vec3<u32>,
7
+ @builtin(num_workgroups) nwg: vec3<u32>) {
8
+ let channel = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;
9
+ if (channel >= params.dim) { return; }
10
+ var carry = initial_state[channel];
11
+ let base = channel * CHUNKS;
12
+ for (var chunk = 0u; chunk < CHUNKS; chunk += 1u) {
13
+ chunkCarries[base + chunk] = carry;
14
+ carry += chunkTotals[base + chunk];
15
+ }
16
+ final_state[channel] = carry;
17
+ }
build/webgpu/scan-multichunk-local.wgsl.jinja ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Workgroup-parallel local prefix chunks for small-state, long Scan streams.
2
+ {% if source.useSubgroups %}
3
+ enable subgroups;
4
+ {% endif %}
5
+ {{ env.wgsl.resourceDeclarations }}
6
+
7
+ const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;
8
+ const CHUNKS: u32 = {{ source.chunks }}u;
9
+ const TOTAL_CHUNKS: u32 = {{ source.totalChunks }}u;
10
+
11
+ {% if source.useSubgroups %}
12
+ var<workgroup> sgTotals: array<f32, WG>;
13
+
14
+ @compute @workgroup_size(WG)
15
+ fn main(@builtin(workgroup_id) wg: vec3<u32>,
16
+ @builtin(num_workgroups) nwg: vec3<u32>,
17
+ @builtin(local_invocation_id) lid: vec3<u32>,
18
+ @builtin(subgroup_invocation_id) sgLane: u32,
19
+ @builtin(subgroup_size) sgSize: u32) {
20
+ let group = wg.x + wg.y * nwg.x;
21
+ if (group >= TOTAL_CHUNKS) { return; }
22
+ let channel = group / CHUNKS;
23
+ let chunk = group % CHUNKS;
24
+ let j = chunk * WG + lid.x;
25
+ let valid = j < params.steps;
26
+ let t = select(j, params.steps - 1u - j, params.reverse != 0u);
27
+ let addr = t * params.dim + channel;
28
+ var v = 0.0;
29
+ if (valid) { v = scan_input[addr]; }
30
+
31
+ let inclusive = subgroupInclusiveAdd(v);
32
+ let subgroupTotal = subgroupAdd(v);
33
+ let safeSg = max(sgSize, 1u);
34
+ let subgroupId = lid.x / safeSg;
35
+ let subgroupCount = (WG + safeSg - 1u) / safeSg;
36
+ if (sgLane == 0u) { sgTotals[subgroupId] = subgroupTotal; }
37
+ workgroupBarrier();
38
+
39
+ var offset = 0.0;
40
+ for (var s = 0u; s < subgroupId; s += 1u) {
41
+ offset += sgTotals[s];
42
+ }
43
+ if (valid) { scan_output[addr] = offset + inclusive; }
44
+ if (lid.x == 0u) {
45
+ var total = 0.0;
46
+ for (var s = 0u; s < subgroupCount; s += 1u) {
47
+ total += sgTotals[s];
48
+ }
49
+ chunkTotals[group] = total;
50
+ }
51
+ }
52
+ {% else %}
53
+ var<workgroup> values: array<f32, WG>;
54
+
55
+ @compute @workgroup_size(WG)
56
+ fn main(@builtin(workgroup_id) wg: vec3<u32>,
57
+ @builtin(num_workgroups) nwg: vec3<u32>,
58
+ @builtin(local_invocation_id) lid: vec3<u32>) {
59
+ let group = wg.x + wg.y * nwg.x;
60
+ if (group >= TOTAL_CHUNKS) { return; }
61
+ let channel = group / CHUNKS;
62
+ let chunk = group % CHUNKS;
63
+ let j = chunk * WG + lid.x;
64
+ let valid = j < params.steps;
65
+ let t = select(j, params.steps - 1u - j, params.reverse != 0u);
66
+ let addr = t * params.dim + channel;
67
+ var v = 0.0;
68
+ if (valid) { v = scan_input[addr]; }
69
+ values[lid.x] = v;
70
+ for (var step = 1u; step < WG; step <<= 1u) {
71
+ workgroupBarrier();
72
+ var addend = 0.0;
73
+ if (lid.x >= step) { addend = values[lid.x - step]; }
74
+ workgroupBarrier();
75
+ values[lid.x] += addend;
76
+ }
77
+ workgroupBarrier();
78
+ if (valid) { scan_output[addr] = values[lid.x]; }
79
+ if (lid.x == 0u) { chunkTotals[group] = values[WG - 1u]; }
80
+ }
81
+ {% endif %}
build/webgpu/scan-prefix-sum.wgsl.jinja ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {{ env.wgsl.resourceDeclarations }}
2
+
3
+ // One invocation owns each channel and runs its prefix sum serially. Channels
4
+ // run independently, but the cross-step recurrence and per-channel addition
5
+ // order remain sequential.
6
+ @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
7
+ fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {
8
+ // gid.y carries channel-index bits past the per-dimension dispatch limit.
9
+ let i = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;
10
+ if (i >= params.dim) { return; }
11
+ var state = initial_state[i];
12
+ for (var iter = 0u; iter < params.steps; iter = iter + 1u) {
13
+ let t = select(iter, params.steps - 1u - iter, params.reverse != 0u);
14
+ state = state + scan_input[t * params.dim + i];
15
+ scan_output[t * params.dim + i] = state;
16
+ }
17
+ final_state[i] = state;
18
+ }
build/webgpu/test.json ADDED
@@ -0,0 +1,360 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "op": "ai.onnx.Scan",
3
+ "cases": [
4
+ {
5
+ "name": "lowered_prefix_sum_state",
6
+ "inputs": {
7
+ "initial_state": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [1.0, 10.0] } },
8
+ "scan_input": {
9
+ "dtype": "float32",
10
+ "shape": [3, 2],
11
+ "data": { "kind": "values", "values": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] }
12
+ }
13
+ },
14
+ "outputs": {
15
+ "final_state": { "dtype": "float32", "shape": [2], "tolerance": 0.000001 },
16
+ "scan_output": { "dtype": "float32", "shape": [3, 2], "tolerance": 0.000001 }
17
+ }
18
+ },
19
+ {
20
+ "name": "lowered_prefix_sum_reverse",
21
+ "attrs": { "reverse": 1 },
22
+ "inputs": {
23
+ "initial_state": { "dtype": "float32", "shape": [1], "data": { "kind": "values", "values": [0.0] } },
24
+ "scan_input": {
25
+ "dtype": "float32",
26
+ "shape": [4, 1],
27
+ "data": { "kind": "values", "values": [1.0, 2.0, 3.0, 4.0] }
28
+ }
29
+ },
30
+ "outputs": {
31
+ "final_state": { "dtype": "float32", "shape": [1], "tolerance": 0.000001 },
32
+ "scan_output": { "dtype": "float32", "shape": [4, 1], "tolerance": 0.000001 }
33
+ }
34
+ },
35
+ {
36
+ "name": "lowered_ort_projection_short_sequence_two_state_lanes",
37
+ "provenance": {
38
+ "source": "onnxruntime/test/providers/cpu/controlflow/scan_test.cc",
39
+ "test": "Scan8.ShortSequenceTwoInBatchOneLoopStateVar",
40
+ "notes": "Projection onto the framework's lowered prefix-sum variant using two state lanes from ORT's short sequence data."
41
+ },
42
+ "inputs": {
43
+ "initial_state": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [0.0, 10.0] } },
44
+ "scan_input": {
45
+ "dtype": "float32",
46
+ "shape": [2, 2],
47
+ "data": { "kind": "values", "values": [1.0, -1.0, 4.0, -4.0] }
48
+ }
49
+ },
50
+ "outputs": {
51
+ "final_state": { "dtype": "float32", "shape": [2], "tolerance": 0.000001 },
52
+ "scan_output": { "dtype": "float32", "shape": [2, 2], "tolerance": 0.000001 }
53
+ }
54
+ },
55
+ {
56
+ "name": "lowered_ort_projection_reverse_two_state_lanes",
57
+ "attrs": { "reverse": 1 },
58
+ "provenance": {
59
+ "source": "onnxruntime/test/providers/cpu/controlflow/scan_test.cc",
60
+ "test": "Scan8.MixedSequenceLensReverse",
61
+ "notes": "Projection onto the framework's lowered reverse prefix-sum variant using reverse-direction values from ORT's Scan coverage."
62
+ },
63
+ "inputs": {
64
+ "initial_state": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [0.0, 10.0] } },
65
+ "scan_input": {
66
+ "dtype": "float32",
67
+ "shape": [2, 2],
68
+ "data": { "kind": "values", "values": [1.0, -1.0, 4.0, -4.0] }
69
+ }
70
+ },
71
+ "outputs": {
72
+ "final_state": { "dtype": "float32", "shape": [2], "tolerance": 0.000001 },
73
+ "scan_output": { "dtype": "float32", "shape": [2, 2], "tolerance": 0.000001 }
74
+ }
75
+ },
76
+ {
77
+ "name": "lowered_ort_projection_scalar_loop_state",
78
+ "provenance": {
79
+ "source": "onnxruntime/test/providers/cpu/controlflow/scan_test.cc",
80
+ "test": "Scan8.OnnxScalarLoopState",
81
+ "notes": "Projection onto the framework's lowered prefix-sum variant using a scalar carried state."
82
+ },
83
+ "inputs": {
84
+ "initial_state": { "dtype": "float32", "shape": [1], "data": { "kind": "values", "values": [0.0] } },
85
+ "scan_input": { "dtype": "float32", "shape": [3, 1], "data": { "kind": "values", "values": [1.0, 2.0, 3.0] } }
86
+ },
87
+ "outputs": {
88
+ "final_state": { "dtype": "float32", "shape": [1], "tolerance": 0.000001 },
89
+ "scan_output": { "dtype": "float32", "shape": [3, 1], "tolerance": 0.000001 }
90
+ }
91
+ },
92
+ {
93
+ "name": "lowered_prefix_sum_zero_steps",
94
+ "inputs": {
95
+ "initial_state": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [2.0, -3.0] } },
96
+ "scan_input": { "dtype": "float32", "shape": [0, 2], "data": { "kind": "values", "values": [] } }
97
+ },
98
+ "outputs": {
99
+ "final_state": { "dtype": "float32", "shape": [2], "tolerance": 0.000001 },
100
+ "scan_output": { "dtype": "float32", "shape": [0, 2], "tolerance": 0.000001 }
101
+ }
102
+ },
103
+ {
104
+ "name": "lowered_prefix_sum_reverse_rank2_signed",
105
+ "attrs": { "reverse": 1 },
106
+ "inputs": {
107
+ "initial_state": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [10.0, -10.0] } },
108
+ "scan_input": {
109
+ "dtype": "float32",
110
+ "shape": [3, 2],
111
+ "data": { "kind": "values", "values": [1.0, -1.0, 2.0, -2.0, 3.0, -3.0] }
112
+ }
113
+ },
114
+ "outputs": {
115
+ "final_state": { "dtype": "float32", "shape": [2], "tolerance": 0.000001 },
116
+ "scan_output": { "dtype": "float32", "shape": [3, 2], "tolerance": 0.000001 }
117
+ }
118
+ },
119
+ {
120
+ "name": "lowered_prefix_sum_reverse_zero_steps",
121
+ "attrs": { "reverse": 1 },
122
+ "inputs": {
123
+ "initial_state": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [7.0, -8.0] } },
124
+ "scan_input": { "dtype": "float32", "shape": [0, 2], "data": { "kind": "values", "values": [] } }
125
+ },
126
+ "outputs": {
127
+ "final_state": { "dtype": "float32", "shape": [2], "tolerance": 0.000001 },
128
+ "scan_output": { "dtype": "float32", "shape": [0, 2], "tolerance": 0.000001 }
129
+ }
130
+ },
131
+ {
132
+ "name": "lowered_prefix_sum_zero_delta_lane",
133
+ "inputs": {
134
+ "initial_state": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [1.0, 10.0, -5.0] } },
135
+ "scan_input": {
136
+ "dtype": "float32",
137
+ "shape": [3, 3],
138
+ "data": { "kind": "values", "values": [0.0, 2.0, -1.0, 0.0, -3.0, 4.0, 0.0, 1.0, -2.0] }
139
+ }
140
+ },
141
+ "outputs": {
142
+ "final_state": { "dtype": "float32", "shape": [3], "tolerance": 0.000001 },
143
+ "scan_output": { "dtype": "float32", "shape": [3, 3], "tolerance": 0.000001 }
144
+ }
145
+ },
146
+ {
147
+ "name": "coop_channel_prefix_sum_128x64",
148
+ "provenance": {
149
+ "notes": "Cross-gate case for the coop_channel_prefix_sum variant (dim>=64 and steps>=64). dim=64 -> 64 channel workgroups; steps=128 spans multiple WG-sized chunks with a cross-chunk carry. Tolerance covers the within-chunk subgroup/Hillis-Steele reassociation vs the strict serial loop."
150
+ },
151
+ "inputs": {
152
+ "initial_state": { "dtype": "float32", "shape": [64] },
153
+ "scan_input": { "dtype": "float32", "shape": [128, 64] }
154
+ },
155
+ "outputs": {
156
+ "final_state": { "dtype": "float32", "shape": [64], "tolerance": 0.0001 },
157
+ "scan_output": { "dtype": "float32", "shape": [128, 64], "tolerance": 0.0001 }
158
+ }
159
+ },
160
+ {
161
+ "name": "coop_channel_prefix_sum_reverse_300x96",
162
+ "attrs": { "reverse": 1 },
163
+ "provenance": {
164
+ "notes": "Reverse-direction cross-gate case for coop_channel_prefix_sum. steps=300 is NOT a multiple of the 256 workgroup size, exercising the partial-chunk identity padding; dim=96 channel workgroups."
165
+ },
166
+ "inputs": {
167
+ "initial_state": { "dtype": "float32", "shape": [96] },
168
+ "scan_input": { "dtype": "float32", "shape": [300, 96] }
169
+ },
170
+ "outputs": {
171
+ "final_state": { "dtype": "float32", "shape": [96], "tolerance": 0.0001 },
172
+ "scan_output": { "dtype": "float32", "shape": [300, 96], "tolerance": 0.0001 }
173
+ }
174
+ },
175
+ {
176
+ "name": "coop_channel_prefix_sum_dim512_steps256_perf_compact",
177
+ "provenance": {
178
+ "notes": "Compact correctness sibling for the Scan no-MMA prefix-sum cliff: dim=512 and steps=256 select coop_channel_prefix_sum with one full workgroup-sized scan chunk."
179
+ },
180
+ "inputs": {
181
+ "initial_state": { "dtype": "float32", "shape": [512] },
182
+ "scan_input": { "dtype": "float32", "shape": [256, 512] }
183
+ },
184
+ "outputs": {
185
+ "final_state": { "dtype": "float32", "shape": [512], "tolerance": 0.0001 },
186
+ "scan_output": { "dtype": "float32", "shape": [256, 512], "tolerance": 0.0001 }
187
+ }
188
+ },
189
+ {
190
+ "name": "coop_channel_prefix_sum_dim1024_steps256",
191
+ "provenance": {
192
+ "notes": "Exercises a wide cooperative dispatch (1024 channel workgroups) and one full workgroup-sized scan chunk. Tolerance covers within-chunk subgroup/Hillis-Steele reassociation."
193
+ },
194
+ "inputs": {
195
+ "initial_state": { "dtype": "float32", "shape": [1024] },
196
+ "scan_input": { "dtype": "float32", "shape": [256, 1024] }
197
+ },
198
+ "outputs": {
199
+ "final_state": { "dtype": "float32", "shape": [1024], "tolerance": 0.0001 },
200
+ "scan_output": { "dtype": "float32", "shape": [256, 1024], "tolerance": 0.0001 }
201
+ }
202
+ },
203
+ {
204
+ "name": "coop_dim1025_above_old_fixed_gate",
205
+ "provenance": {
206
+ "notes": "Regression for the former hard-coded dim<=1024 ceiling. Device-limit-derived cooperative coverage keeps this realistic 1025-channel scan off the serial fallback."
207
+ },
208
+ "inputs": {
209
+ "initial_state": { "dtype": "float32", "shape": [1025] },
210
+ "scan_input": { "dtype": "float32", "shape": [256, 1025] }
211
+ },
212
+ "outputs": {
213
+ "final_state": { "dtype": "float32", "shape": [1025], "tolerance": 0.0001 },
214
+ "scan_output": { "dtype": "float32", "shape": [256, 1025], "tolerance": 0.0001 }
215
+ }
216
+ },
217
+ {
218
+ "name": "coop_channel_prefix_sum_dim64_steps64_min_gate",
219
+ "provenance": {
220
+ "notes": "Both lower coop gates at their exact inclusive thresholds: dim=64 (dim>=64) and steps=64 (steps>=64). This is the smallest shape that still selects coop_channel_prefix_sum. An off-by-one tightening of either >=64 gate (to >64) would demote this corner to the lowered fallback. Verifies coop at the minimum supported width and exactly one partial-free chunk boundary (steps==WG/4)."
221
+ },
222
+ "inputs": {
223
+ "initial_state": { "dtype": "float32", "shape": [64] },
224
+ "scan_input": { "dtype": "float32", "shape": [64, 64] }
225
+ },
226
+ "outputs": {
227
+ "final_state": { "dtype": "float32", "shape": [64], "tolerance": 0.0001 },
228
+ "scan_output": { "dtype": "float32", "shape": [64, 64], "tolerance": 0.0001 }
229
+ }
230
+ },
231
+ {
232
+ "name": "lowered_dim64_steps63_below_steps_gate",
233
+ "provenance": {
234
+ "notes": "Just below the coop steps gate: dim=64 passes dim>=64 but steps=63 fails steps>=64, so this MUST fall to lowered_prefix_sum. Pins the steps off-by-one: an erroneous steps>=63 (or >63 vs >=64) gate would mis-route this short sequence. Both variants are spec-correct; this checks the lowered path at the exact step below the coop threshold."
235
+ },
236
+ "inputs": {
237
+ "initial_state": { "dtype": "float32", "shape": [64] },
238
+ "scan_input": { "dtype": "float32", "shape": [63, 64] }
239
+ },
240
+ "outputs": {
241
+ "final_state": { "dtype": "float32", "shape": [64], "tolerance": 0.000001 },
242
+ "scan_output": { "dtype": "float32", "shape": [63, 64], "tolerance": 0.000001 }
243
+ }
244
+ },
245
+ {
246
+ "name": "lowered_prefix_sum_subnormal_residuals_gpu_gap",
247
+ "skipGpu": {
248
+ "category": "permanent",
249
+ "reason": "Portable WGSL floating-point semantics do not guarantee preservation of the subnormal values required by this fixture. Backend evidence: Metal flushes denormals to zero in the ALU; this lowered prefix-sum accumulates subnormal residuals that flush on GPU. Permanent FTZ limitation."
250
+ },
251
+ "provenance": {
252
+ "source": "onnxruntime/test/providers/cpu/controlflow/scan_test.cc",
253
+ "test": "Scan8.ShortSequenceTwoInBatchOneLoopStateVar",
254
+ "notes": "Projection onto the lowered prefix-sum variant: finite subnormal loop-state increments are valid residuals and must not flush to zero."
255
+ },
256
+ "inputs": {
257
+ "initial_state": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [0.0, 0.0] } },
258
+ "scan_input": {
259
+ "dtype": "float32",
260
+ "shape": [4, 2],
261
+ "data": { "kind": "values", "values": [1e-40, -1e-40, 1e-40, -1e-40, -1e-40, 1e-40, 2e-40, -2e-40] }
262
+ }
263
+ },
264
+ "outputs": {
265
+ "final_state": { "dtype": "float32", "shape": [2], "tolerance": 0 },
266
+ "scan_output": { "dtype": "float32", "shape": [4, 2], "tolerance": 0 }
267
+ }
268
+ },
269
+ {
270
+ "name": "lowered_prefix_sum_reverse_subnormal_residuals_gpu_gap",
271
+ "skipGpu": {
272
+ "category": "permanent",
273
+ "reason": "Portable WGSL floating-point semantics do not guarantee preservation of the subnormal values required by this fixture. Backend evidence: Metal flushes denormals to zero in the ALU; this lowered prefix-sum accumulates subnormal residuals that flush on GPU. Permanent FTZ limitation."
274
+ },
275
+ "attrs": { "reverse": 1 },
276
+ "provenance": {
277
+ "source": "onnxruntime/test/providers/cpu/controlflow/scan_test.cc",
278
+ "test": "Scan8.MixedSequenceLensReverse",
279
+ "notes": "Reverse companion for subnormal prefix residuals; the scan-output slots are written in reverse traversal order but still contain finite float32 subnormal sums."
280
+ },
281
+ "inputs": {
282
+ "initial_state": { "dtype": "float32", "shape": [1], "data": { "kind": "values", "values": [0.0] } },
283
+ "scan_input": {
284
+ "dtype": "float32",
285
+ "shape": [3, 1],
286
+ "data": { "kind": "values", "values": [1e-40, 2e-40, 3e-40] }
287
+ }
288
+ },
289
+ "outputs": {
290
+ "final_state": { "dtype": "float32", "shape": [1], "tolerance": 0 },
291
+ "scan_output": { "dtype": "float32", "shape": [3, 1], "tolerance": 0 }
292
+ }
293
+ },
294
+ {
295
+ "name": "coop_channel_prefix_sum_dim64_steps257_chunk_carry",
296
+ "provenance": {
297
+ "notes": "Routes to coop_channel_prefix_sum (dim=64>=64, dim<=1024, steps=257>=64). chunks=ceil(257/256)=2: chunk 0 = steps 0..255, chunk 1 = ONLY step 256 (one valid lane, 255 identity-padded). Isolates cross-chunk carry propagation into a near-empty second chunk and the final_state = carry-after-both-chunks path. An off-by-one chunk bound, stale carry, or padded-lane double-count would corrupt scan_output[256*64+i] and final_state. Verifies BOTH outputs against the TS reference; tolerance 1e-4 covers within-chunk subgroup/Hillis-Steele reassociation."
298
+ },
299
+ "inputs": {
300
+ "initial_state": { "dtype": "float32", "shape": [64] },
301
+ "scan_input": { "dtype": "float32", "shape": [257, 64] }
302
+ },
303
+ "outputs": {
304
+ "final_state": { "dtype": "float32", "shape": [64], "tolerance": 0.0001 },
305
+ "scan_output": { "dtype": "float32", "shape": [257, 64], "tolerance": 0.0001 }
306
+ }
307
+ },
308
+ {
309
+ "name": "coop_channel_prefix_sum_reverse_dim64_steps257_chunk_carry",
310
+ "attrs": { "reverse": 1 },
311
+ "provenance": {
312
+ "notes": "Reverse coop_channel_prefix_sum (dim=64>=64, dim<=1024, steps=257>=64, reverse=1). chunks=2: chunk 0 (j=0..255) -> original steps 256..1, chunk 1 (j=256) -> original step 0. Pins the minimal 1-step residual chunk under reverse: verifies the reversed read/write addresses at the chunk boundary and the scan-order cross-chunk carry land at the correct reversed scan_output slots and final_state. Both outputs checked vs the TS reference; tolerance 1e-4 covers within-chunk reassociation."
313
+ },
314
+ "inputs": {
315
+ "initial_state": { "dtype": "float32", "shape": [64] },
316
+ "scan_input": { "dtype": "float32", "shape": [257, 64] }
317
+ },
318
+ "outputs": {
319
+ "final_state": { "dtype": "float32", "shape": [64], "tolerance": 0.0001 },
320
+ "scan_output": { "dtype": "float32", "shape": [257, 64], "tolerance": 0.0001 }
321
+ }
322
+ },
323
+ {
324
+ "name": "lowered_dim1_steps2000_singlechannel_serial",
325
+ "provenance": {
326
+ "notes": "dim=1 fails coop 'dim>=64' -> lowered_prefix_sum, one active lane (i=0) running the serial 2000-step recurrence with addressing t*1+0=t. Covers the single-channel serial scan over a long sequence (existing dim=1 cases stop at steps<=4). Serial per-channel accumulation is bit-identical to the reference, so tolerance stays at 1e-6; both outputs verified vs the TS reference."
327
+ },
328
+ "inputs": {
329
+ "initial_state": { "dtype": "float32", "shape": [1], "data": { "kind": "values", "values": [3.5] } },
330
+ "scan_input": {
331
+ "dtype": "float32",
332
+ "shape": [2000, 1],
333
+ "data": { "kind": "fillFloat32", "sinStep": 0.017, "scale": 0.25, "offset": 0.01 }
334
+ }
335
+ },
336
+ "outputs": {
337
+ "final_state": { "dtype": "float32", "shape": [1], "tolerance": 0.000001 },
338
+ "scan_output": { "dtype": "float32", "shape": [2000, 1], "tolerance": 0.000001 }
339
+ }
340
+ },
341
+ {
342
+ "name": "multichunk_small_state_dim2_steps4096",
343
+ "provenance": {
344
+ "notes": "Compact correctness lock for a realistic long-stream recurrence with only two independent state lanes. The optimized path partitions the sequential axis into workgroup-sized chunks, scans chunk totals, then applies carries."
345
+ },
346
+ "inputs": {
347
+ "initial_state": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [1.5, -2.0] } },
348
+ "scan_input": {
349
+ "dtype": "float32",
350
+ "shape": [4096, 2],
351
+ "data": { "kind": "fillFloat32", "sinStep": 0.017, "cosStep": 0.013, "scale": 0.01, "offset": 0.0001 }
352
+ }
353
+ },
354
+ "outputs": {
355
+ "final_state": { "dtype": "float32", "shape": [2], "tolerance": 0.001, "relTolerance": 0.0001 },
356
+ "scan_output": { "dtype": "float32", "shape": [4096, 2], "tolerance": 0.001, "relTolerance": 0.0001 }
357
+ }
358
+ }
359
+ ]
360
+ }