Xenova HF Staff commited on
Commit
852bc32
·
verified ·
1 Parent(s): 45af05c

sync 91d990483a17

Browse files
README.md CHANGED
@@ -18,15 +18,15 @@ See the [ONNX `Flatten` spec](https://onnx.ai/onnx/operators/onnx__Flatten.html)
18
 
19
  ## Inputs
20
 
21
- | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
22
- | --- | --- | --- | --- | --- | --- | --- |
23
- | `input` | `input` | `T` | — | — | Input tensor to flatten into a matrix. | required |
24
 
25
  ## Outputs
26
 
27
- | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
28
- | --- | --- | --- | --- | --- | --- | --- |
29
- | `output` | `output` | `T` | `2` | derived; see description | A 2-D tensor whose outer dimension combines axes before `axis` and whose inner dimension combines the remaining axes. | required |
30
 
31
  ## Attributes
32
 
@@ -44,7 +44,7 @@ Default values (overridable per request):
44
 
45
  ## Files
46
 
47
- - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, provenance)
48
  - [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)
49
  - [`test.json`](build/webgpu/test.json) — correctness cases
50
  - [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases
@@ -53,10 +53,14 @@ Default values (overridable per request):
53
 
54
  ## Use with `@huggingface/kernels`
55
 
56
- The loader derives every required output's shape and logical dtype from the manifest contract and this call.
57
- It then allocates the result tensors automatically.
 
 
 
58
 
59
  The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.
 
60
 
61
  Replace each `*Data` placeholder with a typed array containing the corresponding input data.
62
 
 
18
 
19
  ## Inputs
20
 
21
+ | Name | Logical dtype | Rank | Shape | Description | Presence |
22
+ | --- | --- | --- | --- | --- | --- |
23
+ | `input` | `T` | — | — | Input tensor to flatten into a matrix. | required |
24
 
25
  ## Outputs
26
 
27
+ | Name | Logical dtype | Rank | Shape | Description | Presence |
28
+ | --- | --- | --- | --- | --- | --- |
29
+ | `output` | `T` | `2` | derived | A 2-D tensor whose outer dimension combines axes before `axis` and whose inner dimension combines the remaining axes. | required |
30
 
31
  ## Attributes
32
 
 
44
 
45
  ## Files
46
 
47
+ - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, provenance)
48
  - [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)
49
  - [`test.json`](build/webgpu/test.json) — correctness cases
50
  - [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases
 
53
 
54
  ## Use with `@huggingface/kernels`
55
 
56
+ ```sh
57
+ npm install --save-exact @huggingface/kernels@0.0.1-preview.2
58
+ ```
59
+
60
+ Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
61
 
62
  The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.
63
+ It follows the `v1` branch as fixes land. To pin exact artifact bytes, pass a 40-character commit `revision` instead of `version`.
64
 
65
  Replace each `*Data` placeholder with a typed array containing the corresponding input data.
66
 
build/webgpu/bench.json CHANGED
@@ -1,5 +1,4 @@
1
  {
2
- "op": "ai.onnx.Flatten",
3
  "tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] },
4
  "cases": [
5
  {
 
1
  {
 
2
  "tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] },
3
  "cases": [
4
  {
build/webgpu/datamove-elementwise-copy.wgsl.jinja CHANGED
@@ -1,6 +1,3 @@
1
- {% if usesF16 %}
2
- enable f16;
3
- {% endif %}
4
  {{ env.wgsl.resourceDeclarations }}
5
 
6
  const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;
 
 
 
 
1
  {{ env.wgsl.resourceDeclarations }}
2
 
3
  const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;
build/webgpu/datamove-flat-copy.wgsl.jinja CHANGED
@@ -1,9 +1,6 @@
1
- {% if usesF16 %}
2
- enable f16;
3
- {% endif %}
4
  {{ env.wgsl.resourceDeclarations }}
5
 
6
- const COUNT: u32 = {{ source.count }}u;
7
  const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;
8
 
9
  @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
 
 
 
 
1
  {{ env.wgsl.resourceDeclarations }}
2
 
3
+ const COUNT: u32 = {{ count }}u;
4
  const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;
5
 
6
  @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
build/webgpu/manifest.json CHANGED
@@ -2,101 +2,67 @@
2
  "domain": "ai.onnx",
3
  "name": "Flatten",
4
  "sinceVersion": 13,
5
- "description": "Flattens the input tensor into a 2-D matrix by splitting its dimensions at `axis`: dimensions before `axis` are collapsed into the first output dimension, and dimensions from `axis` onward into the second. For input shape `(d_0, ..., d_n)`, the output shape is `(d_0 * ... * d_{axis-1}, d_axis * ... * d_n)`.",
6
- "inputs": [{ "role": "input", "dtype": "T", "description": "Input tensor to flatten into a matrix." }],
7
- "outputs": [
8
- {
9
- "role": "output",
10
  "dtype": "T",
11
  "rank": 2,
12
- "description": "A 2-D tensor whose outer dimension combines axes before `axis` and whose inner dimension combines the remaining axes.",
13
  "shape": ["numel(prefix(shapes.input, axisNorm))", "numel(suffix(shapes.input, axisNorm))"]
14
  }
15
- ],
16
- "attributes": { "axis": 1 },
17
- "attributeDescriptions": {
18
- "axis": "The axis at which to split the input dimensions (exclusive upper bound for the outer dimension). Must be in `[-r, r]` where `r` is the input rank; negative values count from the back. When `axis = 0` the output shape is `(1, total_elements)`."
19
  },
 
20
  "typeConstraints": { "T": ["float32", "float16", "int32", "uint32", "int16", "int8", "uint8", "bool"] },
21
- "args": {
22
- "input": { "kind": "tensor", "semantic": "input", "role": "input" },
23
- "output": { "kind": "tensor", "semantic": "output", "role": "output" }
24
- },
25
- "tunables": { "WORKGROUP_SIZE": 256 },
26
  "derive": {
27
  "axisInRange": "attrs.axis == floor(attrs.axis) and attrs.axis >= 0 - ranks.input and attrs.axis <= ranks.input",
28
  "axisNorm": "attrs.axis if attrs.axis >= 0 else attrs.axis + ranks.input",
29
  "flattenShapeOk": "axisInRange and ranks.output == 2 and sameShape(shapes.output, [numel(prefix(shapes.input, axisNorm)), numel(suffix(shapes.input, axisNorm))])",
30
  "baseContract": "flattenShapeOk and f16Ok(dtypes.T)"
31
  },
32
- "constants": { "usesF16": "dtypes.T == \"f16\"" },
33
  "variants": [
34
  {
35
  "id": "copy_vec4",
36
  "priority": 20,
37
- "when": ["baseContract", "numel(shapes.input) > 0", "numel(shapes.input) % 4 == 0"],
38
- "constants": { "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },
39
  "passes": [
40
  {
41
  "id": "main",
42
  "name": "Flatten.copyVec4",
43
- "source": { "shader": "datamove-flat-copy.wgsl.jinja", "inputs": { "count": "numel(shapes.output) / 4" } },
 
44
  "bindings": [
45
- {
46
- "name": "src",
47
- "arg": "input",
48
- "semantic": "input",
49
- "buffer": { "type": "read-only-storage" },
50
- "elementType": "$vectorScalar"
51
- },
52
- {
53
- "name": "dst",
54
- "arg": "output",
55
- "semantic": "output",
56
- "buffer": { "type": "storage" },
57
- "elementType": "$vectorScalar"
58
- }
59
  ],
60
- "dispatch": { "gridStride": "numel(shapes.output) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" },
 
 
 
 
61
  "viewAlias": [{ "input": "src", "output": "dst" }]
62
  }
63
  ]
64
  },
65
  {
66
  "id": "copy",
67
- "when": "baseContract",
68
- "constants": { "scalar": "dtypes.T" },
69
  "passes": [
70
  {
71
  "id": "main",
72
  "name": "Flatten",
73
  "shader": "datamove-elementwise-copy.wgsl.jinja",
74
  "bindings": [
75
- {
76
- "name": "x",
77
- "arg": "input",
78
- "semantic": "input",
79
- "buffer": { "type": "read-only-storage" },
80
- "elementType": "$scalar"
81
- },
82
- {
83
- "name": "y",
84
- "arg": "output",
85
- "semantic": "output",
86
- "buffer": { "type": "storage" },
87
- "elementType": "$scalar"
88
- },
89
- {
90
- "name": "params",
91
- "semantic": "kernel.params",
92
- "buffer": { "type": "uniform" },
93
- "struct": {
94
- "name": "Params",
95
- "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.output)" }]
96
- }
97
- }
98
  ],
99
- "dispatch": { "gridStride": "numel(shapes.output)", "workgroupSize": "tunables.WORKGROUP_SIZE" },
 
 
 
 
100
  "viewAlias": [{ "input": "x", "output": "y" }]
101
  }
102
  ]
 
2
  "domain": "ai.onnx",
3
  "name": "Flatten",
4
  "sinceVersion": 13,
5
+ "inputs": { "input": { "dtype": "T" } },
6
+ "outputs": {
7
+ "output": {
 
 
8
  "dtype": "T",
9
  "rank": 2,
 
10
  "shape": ["numel(prefix(shapes.input, axisNorm))", "numel(suffix(shapes.input, axisNorm))"]
11
  }
 
 
 
 
12
  },
13
+ "attributes": { "axis": { "default": 1 } },
14
  "typeConstraints": { "T": ["float32", "float16", "int32", "uint32", "int16", "int8", "uint8", "bool"] },
15
+ "tunables": { "WORKGROUP_SIZE": { "default": 256 } },
 
 
 
 
16
  "derive": {
17
  "axisInRange": "attrs.axis == floor(attrs.axis) and attrs.axis >= 0 - ranks.input and attrs.axis <= ranks.input",
18
  "axisNorm": "attrs.axis if attrs.axis >= 0 else attrs.axis + ranks.input",
19
  "flattenShapeOk": "axisInRange and ranks.output == 2 and sameShape(shapes.output, [numel(prefix(shapes.input, axisNorm)), numel(suffix(shapes.input, axisNorm))])",
20
  "baseContract": "flattenShapeOk and f16Ok(dtypes.T)"
21
  },
22
+ "when": ["baseContract"],
23
  "variants": [
24
  {
25
  "id": "copy_vec4",
26
  "priority": 20,
27
+ "when": ["numel(shapes.input) > 0", "numel(shapes.input) % 4 == 0"],
28
+ "derive": { "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },
29
  "passes": [
30
  {
31
  "id": "main",
32
  "name": "Flatten.copyVec4",
33
+ "shader": "datamove-flat-copy.wgsl.jinja",
34
+ "derive": { "count": "numel(shapes.output) / 4" },
35
  "bindings": [
36
+ { "arg": "input", "name": "src", "elementType": "$vectorScalar" },
37
+ { "arg": "output", "name": "dst", "elementType": "$vectorScalar" }
 
 
 
 
 
 
 
 
 
 
 
 
38
  ],
39
+ "dispatch": {
40
+ "x": "min(ceilDiv((numel(shapes.output) / 4), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
41
+ "y": 1,
42
+ "z": 1
43
+ },
44
  "viewAlias": [{ "input": "src", "output": "dst" }]
45
  }
46
  ]
47
  },
48
  {
49
  "id": "copy",
50
+ "derive": { "scalar": "dtypes.T" },
 
51
  "passes": [
52
  {
53
  "id": "main",
54
  "name": "Flatten",
55
  "shader": "datamove-elementwise-copy.wgsl.jinja",
56
  "bindings": [
57
+ { "arg": "input", "name": "x", "elementType": "$scalar" },
58
+ { "arg": "output", "name": "y", "elementType": "$scalar" },
59
+ { "name": "params", "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.output)" }] }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  ],
61
+ "dispatch": {
62
+ "x": "min(ceilDiv((numel(shapes.output)), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
63
+ "y": 1,
64
+ "z": 1
65
+ },
66
  "viewAlias": [{ "input": "x", "output": "y" }]
67
  }
68
  ]
build/webgpu/metadata.json CHANGED
@@ -1,19 +1,22 @@
1
  {
2
  "name": "ai.onnx.Flatten",
3
- "id": "_ai_onnx_flatten_webgpu_fc79cfe",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
7
  "digest": {
8
  "algorithm": "sha256",
9
  "files": {
10
- "bench.json": "BQ94yR8AoRBXTibbssosAWUd+g36SutpEg2z706kO08=",
11
- "datamove-elementwise-copy.wgsl.jinja": "J5yC2bAddPiP+odXLgVGS3TJ9jeNsfRTvedKrj/fhZg=",
12
- "datamove-flat-copy.wgsl.jinja": "rEpeYMvIZQuQnB2f3lt51jrrklKICsqUTzCJPRTknNk=",
13
- "manifest.json": "CDoYf0fbzbaehSbZyJtHaB+i7Brv4L4zWhgbxHRKR78=",
14
- "test.json": "twoee1e3lNhBcaIKA8KHZATJ+qrjMQew63aqtGJLI5s="
15
  }
16
  },
17
- "provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } },
18
- "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/ai.onnx.Flatten" }
 
 
 
19
  }
 
1
  {
2
  "name": "ai.onnx.Flatten",
3
+ "id": "_ai_onnx_flatten_webgpu_96e061d",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
7
  "digest": {
8
  "algorithm": "sha256",
9
  "files": {
10
+ "bench.json": "FS0xOpUC7d41FBW9+EvJwtD83YRoeO1BAurg5W301vk=",
11
+ "datamove-elementwise-copy.wgsl.jinja": "Q1WuZCqcDf9b6cbT4rLilpeWj6327t/bSFmKr6IVBoQ=",
12
+ "datamove-flat-copy.wgsl.jinja": "g9d62mer2bmHfbScqX5CIT5Zq/Pdinac0W2dH0CD+5s=",
13
+ "manifest.json": "AfSmBgJc/qrYWFaC8xY7jewzS41YztUERtAgmWwW9Go=",
14
+ "test.json": "IA5HhouFO++AQutVUkOgRYSipQ5h/e1SOb49EK5xm4A="
15
  }
16
  },
17
+ "provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } },
18
+ "webgpu": {
19
+ "manifestSpec": "2.0",
20
+ "variants": { "copy_vec4": ["datamove-flat-copy.wgsl.jinja"], "copy": ["datamove-elementwise-copy.wgsl.jinja"] }
21
+ }
22
  }
build/webgpu/test.json CHANGED
@@ -1,5 +1,4 @@
1
  {
2
- "op": "ai.onnx.Flatten",
3
  "fixtureArrays": {
4
  "onnx_backend_flatten_input_input": [0.54881352186203, 0.7151893377304077, 0.6027633547782898, 0.5448831915855408, 0.42365479469299316, 0.6458941102027893, 0.4375872015953064, 0.891772985458374, 0.9636627435684204, 0.3834415078163147, 0.7917250394821167, 0.5288949012756348, 0.5680445432662964, 0.9255966544151306, 0.07103605568408966, 0.08712930232286453, 0.020218396559357643, 0.832619845867157, 0.7781567573547363, 0.8700121641159058, 0.978618323802948, 0.7991585731506348, 0.4614793658256531, 0.7805292010307312, 0.11827442795038223, 0.6399210095405579, 0.14335328340530396, 0.9446688890457153, 0.5218483209609985, 0.4146619439125061, 0.26455560326576233, 0.7742336988449097, 0.4561503231525421, 0.568433940410614, 0.018789799883961678, 0.6176354885101318, 0.6120957136154175, 0.6169340014457703, 0.9437480568885803, 0.681820273399353, 0.35950788855552673, 0.43703195452690125, 0.6976311802864075, 0.0602254718542099, 0.6667667031288147, 0.670637845993042, 0.21038256585597992, 0.12892629206180573, 0.31542834639549255, 0.36371076107025146, 0.5701967477798462, 0.4386015236377716, 0.9883738160133362, 0.10204481333494186, 0.20887675881385803, 0.16130951046943665, 0.6531082987785339, 0.25329160690307617, 0.4663107693195343, 0.24442559480667114, 0.15896958112716675, 0.11037514358758926, 0.6563295722007751, 0.13818295300006866, 0.1965823620557785, 0.3687251806259155, 0.8209932446479797, 0.09710127860307693, 0.8379449248313904, 0.0960984081029892, 0.9764594435691833, 0.4686512053012848, 0.9767611026763916, 0.6048455238342285, 0.7392635941505432, 0.03918779268860817, 0.28280696272850037, 0.12019655853509903, 0.296140193939209, 0.11872772127389908, 0.3179831802845001, 0.414262980222702, 0.06414749473333359, 0.6924721002578735, 0.5666014552116394, 0.26538950204849243, 0.5232480764389038, 0.09394051134586334, 0.5759465098381042, 0.9292961955070496, 0.3185689449310303, 0.6674103736877441, 0.13179786503314972, 0.7163271903991699, 0.28940609097480774, 0.18319135904312134, 0.5865129232406616, 0.02010754682123661, 0.8289400339126587, 0.004695476032793522, 0.6778165102005005, 0.2700079679489136, 0.7351940274238586, 0.9621885418891907, 0.2487531453371048, 0.5761573314666748, 0.5920419096946716, 0.5722519159317017, 0.22308163344860077, 0.9527490139007568, 0.4471253752708435, 0.8464086651802063, 0.6994792819023132, 0.2974369525909424, 0.8137978315353394, 0.396505743265152, 0.8811032176017761, 0.5812729001045227, 0.8817353844642639, 0.6925315856933594],
5
  "ort_uint32_bool_like_axis2_input_input": [0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1]
@@ -169,7 +168,7 @@
169
  "provenance": {
170
  "source": "onnxruntime/test/providers/cpu/nn/flatten_op_test.cc",
171
  "test": "FlattenOpTest.Flatten_axis2_bool",
172
- "notes": "Adapts ORT's bool fixture to logical uint32 values because this framework stores bool-like tensors as u32 slots."
173
  },
174
  "attrs": { "axis": 2 },
175
  "inputs": {
 
1
  {
 
2
  "fixtureArrays": {
3
  "onnx_backend_flatten_input_input": [0.54881352186203, 0.7151893377304077, 0.6027633547782898, 0.5448831915855408, 0.42365479469299316, 0.6458941102027893, 0.4375872015953064, 0.891772985458374, 0.9636627435684204, 0.3834415078163147, 0.7917250394821167, 0.5288949012756348, 0.5680445432662964, 0.9255966544151306, 0.07103605568408966, 0.08712930232286453, 0.020218396559357643, 0.832619845867157, 0.7781567573547363, 0.8700121641159058, 0.978618323802948, 0.7991585731506348, 0.4614793658256531, 0.7805292010307312, 0.11827442795038223, 0.6399210095405579, 0.14335328340530396, 0.9446688890457153, 0.5218483209609985, 0.4146619439125061, 0.26455560326576233, 0.7742336988449097, 0.4561503231525421, 0.568433940410614, 0.018789799883961678, 0.6176354885101318, 0.6120957136154175, 0.6169340014457703, 0.9437480568885803, 0.681820273399353, 0.35950788855552673, 0.43703195452690125, 0.6976311802864075, 0.0602254718542099, 0.6667667031288147, 0.670637845993042, 0.21038256585597992, 0.12892629206180573, 0.31542834639549255, 0.36371076107025146, 0.5701967477798462, 0.4386015236377716, 0.9883738160133362, 0.10204481333494186, 0.20887675881385803, 0.16130951046943665, 0.6531082987785339, 0.25329160690307617, 0.4663107693195343, 0.24442559480667114, 0.15896958112716675, 0.11037514358758926, 0.6563295722007751, 0.13818295300006866, 0.1965823620557785, 0.3687251806259155, 0.8209932446479797, 0.09710127860307693, 0.8379449248313904, 0.0960984081029892, 0.9764594435691833, 0.4686512053012848, 0.9767611026763916, 0.6048455238342285, 0.7392635941505432, 0.03918779268860817, 0.28280696272850037, 0.12019655853509903, 0.296140193939209, 0.11872772127389908, 0.3179831802845001, 0.414262980222702, 0.06414749473333359, 0.6924721002578735, 0.5666014552116394, 0.26538950204849243, 0.5232480764389038, 0.09394051134586334, 0.5759465098381042, 0.9292961955070496, 0.3185689449310303, 0.6674103736877441, 0.13179786503314972, 0.7163271903991699, 0.28940609097480774, 0.18319135904312134, 0.5865129232406616, 0.02010754682123661, 0.8289400339126587, 0.004695476032793522, 0.6778165102005005, 0.2700079679489136, 0.7351940274238586, 0.9621885418891907, 0.2487531453371048, 0.5761573314666748, 0.5920419096946716, 0.5722519159317017, 0.22308163344860077, 0.9527490139007568, 0.4471253752708435, 0.8464086651802063, 0.6994792819023132, 0.2974369525909424, 0.8137978315353394, 0.396505743265152, 0.8811032176017761, 0.5812729001045227, 0.8817353844642639, 0.6925315856933594],
4
  "ort_uint32_bool_like_axis2_input_input": [0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1]
 
168
  "provenance": {
169
  "source": "onnxruntime/test/providers/cpu/nn/flatten_op_test.cc",
170
  "test": "FlattenOpTest.Flatten_axis2_bool",
171
+ "notes": "Projects boolean source values to logical booleans stored in uint32 slots."
172
  },
173
  "attrs": { "axis": 2 },
174
  "inputs": {