Xenova HF Staff commited on
Commit
ce19c6b
·
verified ·
1 Parent(s): 18df5b3

sync 6fdf6301e2bb

Browse files
README.md CHANGED
@@ -39,14 +39,14 @@ See the [ONNX `Not` spec](https://onnx.ai/onnx/operators/onnx__Not.html) for the
39
  - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, provenance)
40
  - [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)
41
  - [`test.json`](build/webgpu/test.json) — correctness cases
42
- - [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases
43
  - [`unary-scalar.wgsl.jinja`](build/webgpu/unary-scalar.wgsl.jinja)
44
  - [`unary-vec4.wgsl.jinja`](build/webgpu/unary-vec4.wgsl.jinja)
45
 
46
  ## Use with `@huggingface/kernels`
47
 
48
  ```sh
49
- npm install --save-exact @huggingface/kernels@0.0.1-preview.2
50
  ```
51
 
52
  Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
@@ -56,9 +56,11 @@ It follows the `v1` branch as fixes land. To pin exact artifact bytes, pass a 40
56
 
57
  Replace each `*Data` placeholder with a typed array containing the corresponding input data.
58
 
 
 
59
  ```js
60
  import { getKernel } from "@huggingface/kernels";
61
 
62
  const kernel = await getKernel("webgpu-kernels/ai.onnx.Not", { version: 1 });
63
- const { y } = await kernel({ x: { data: xData, shape: [] } });
64
  ```
 
39
  - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, provenance)
40
  - [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)
41
  - [`test.json`](build/webgpu/test.json) — correctness cases
42
+ - [`bench.json`](build/webgpu/bench.json) — benchmark cases
43
  - [`unary-scalar.wgsl.jinja`](build/webgpu/unary-scalar.wgsl.jinja)
44
  - [`unary-vec4.wgsl.jinja`](build/webgpu/unary-vec4.wgsl.jinja)
45
 
46
  ## Use with `@huggingface/kernels`
47
 
48
  ```sh
49
+ npm install --save-exact @huggingface/kernels@0.0.1-preview.3
50
  ```
51
 
52
  Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
 
56
 
57
  Replace each `*Data` placeholder with a typed array containing the corresponding input data.
58
 
59
+ A `bool` input takes 0/1 values in a `Uint8Array` together with `dtype: "bool"`, since no typed-array class implies it.
60
+
61
  ```js
62
  import { getKernel } from "@huggingface/kernels";
63
 
64
  const kernel = await getKernel("webgpu-kernels/ai.onnx.Not", { version: 1 });
65
+ const { y } = await kernel({ x: { data: xData, shape: [2], dtype: "bool" } });
66
  ```
build/webgpu/manifest.json CHANGED
@@ -15,16 +15,13 @@
15
  "id": "same_layout_vec4",
16
  "priority": 20,
17
  "when": ["numel(shapes.x) > 0", "numel(shapes.x) % 4 == 0", "wideVec4StorageOk"],
18
- "derive": { "scalar": "dtypes.B", "usesF16": false, "vectorScalar": "\"vec4<\" ~ dtypes.B ~ \">\"" },
19
  "passes": [
20
  {
21
  "id": "main",
22
- "name": "Not.vec4",
23
  "shader": "unary-vec4.wgsl.jinja",
24
- "derive": {
25
- "op": "\"not\"",
26
- "vec4PerThread": "4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1"
27
- },
28
  "bindings": [
29
  { "arg": "x", "elementType": "vec4<u32>" },
30
  { "arg": "y", "elementType": "vec4<u32>" },
@@ -45,7 +42,6 @@
45
  "id": "main",
46
  "name": "Not",
47
  "shader": "unary-scalar.wgsl.jinja",
48
- "derive": { "op": "\"not\"" },
49
  "bindings": [
50
  { "arg": "x", "elementType": "u32" },
51
  { "arg": "y", "elementType": "u32" },
 
15
  "id": "same_layout_vec4",
16
  "priority": 20,
17
  "when": ["numel(shapes.x) > 0", "numel(shapes.x) % 4 == 0", "wideVec4StorageOk"],
18
+ "derive": { "vectorScalar": "\"vec4<\" ~ dtypes.B ~ \">\"", "scalar": "dtypes.B" },
19
  "passes": [
20
  {
21
  "id": "main",
22
+ "name": "Not.Vec4",
23
  "shader": "unary-vec4.wgsl.jinja",
24
+ "derive": { "vec4PerThread": "4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1" },
 
 
 
25
  "bindings": [
26
  { "arg": "x", "elementType": "vec4<u32>" },
27
  { "arg": "y", "elementType": "vec4<u32>" },
 
42
  "id": "main",
43
  "name": "Not",
44
  "shader": "unary-scalar.wgsl.jinja",
 
45
  "bindings": [
46
  { "arg": "x", "elementType": "u32" },
47
  { "arg": "y", "elementType": "u32" },
build/webgpu/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "ai.onnx.Not",
3
- "id": "_ai_onnx_not_webgpu_36db185",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
@@ -8,15 +8,15 @@
8
  "algorithm": "sha256",
9
  "files": {
10
  "bench.json": "L8DnqPVQmtoUbVzOJf4ZOBc4B5szJWInfBVo8dLAsjI=",
11
- "manifest.json": "XcMLy6Mv7EKh+Xx5cHodb3BrUbRuFMx5wwBDmoC34qQ=",
12
  "test.json": "C0OWAWpvBPaJ3TPJV+Q7uEeKYDziZXz17iNTn7Bl8+s=",
13
- "unary-scalar.wgsl.jinja": "aF5jl3TxM+kRr5JdAJgNXYpsijeufs+DRUN95lLAl50=",
14
- "unary-vec4.wgsl.jinja": "UQ/vPY94Jhd4/nX5WCB8jGObg2b9oNcqq4hpdqDXUVM="
15
  }
16
  },
17
- "provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } },
18
  "webgpu": {
19
- "manifestSpec": "2.0",
20
  "variants": { "same_layout_vec4": ["unary-vec4.wgsl.jinja"], "elementwise": ["unary-scalar.wgsl.jinja"] }
21
  }
22
  }
 
1
  {
2
  "name": "ai.onnx.Not",
3
+ "id": "_ai_onnx_not_webgpu_6e19d7e",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
 
8
  "algorithm": "sha256",
9
  "files": {
10
  "bench.json": "L8DnqPVQmtoUbVzOJf4ZOBc4B5szJWInfBVo8dLAsjI=",
11
+ "manifest.json": "BCYOzxneNalRr+4OjUZTMTByUEqUz/iK1HylpIyPjmE=",
12
  "test.json": "C0OWAWpvBPaJ3TPJV+Q7uEeKYDziZXz17iNTn7Bl8+s=",
13
+ "unary-scalar.wgsl.jinja": "ZdY8qjNQvzHtQc/mLDR5TsKg/J35QwBfwnkZoOLJDwc=",
14
+ "unary-vec4.wgsl.jinja": "ajrPgCRHsVACLo9RyHJHftcz65FhdgUch0tjkcQwA9c="
15
  }
16
  },
17
+ "provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } },
18
  "webgpu": {
19
+ "manifestSpec": "2.1",
20
  "variants": { "same_layout_vec4": ["unary-vec4.wgsl.jinja"], "elementwise": ["unary-scalar.wgsl.jinja"] }
21
  }
22
  }
build/webgpu/unary-scalar.wgsl.jinja CHANGED
@@ -12,14 +12,12 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
12
  for (var i = begin; i < end; i = i + 1u) {
13
  {%- endmacro %}
14
  {% macro flat_tail_close() %}
15
- }
16
- {% endmacro %}
17
-
18
- // Scalar unary elementwise implementation. Specialization emits only the
19
- // selected operation and any numerical helper it requires.
20
  {{ env.wgsl.resourceDeclarations }}
21
 
22
  {{ flat_tail_open() }}
23
  y[i] = select(0u, 1u, x[i] == 0u);
24
- {{ flat_tail_close() -}}
25
  }
 
12
  for (var i = begin; i < end; i = i + 1u) {
13
  {%- endmacro %}
14
  {% macro flat_tail_close() %}
15
+ }{% endmacro %}
16
+ // Scalar unary elementwise kernel: applies the selected operation, with the
17
+ // numerical helpers it uses, to each element.
 
 
18
  {{ env.wgsl.resourceDeclarations }}
19
 
20
  {{ flat_tail_open() }}
21
  y[i] = select(0u, 1u, x[i] == 0u);
22
+ {{ flat_tail_close() }}
23
  }
build/webgpu/unary-vec4.wgsl.jinja CHANGED
@@ -2,34 +2,42 @@
2
  // component.
3
  {{ env.wgsl.resourceDeclarations }}
4
 
5
-
6
- {% set vec4PerThread = vec4PerThread %}
 
 
 
 
 
 
 
 
7
  {% if vec4PerThread > 1 %}
8
  const ITEMS: u32 = {{ vec4PerThread }}u;
9
- {% endif %}
10
 
 
11
  @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
12
  fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
13
- // 2D-folded flat index: gid.y carries the high bits when the element count exceeds the
14
- // per-axis dispatch fold width (the dispatch caps x and spills the rest into y).
15
  {% if vec4PerThread > 1 %}
16
  // Each invocation walks ITEMS vec4 groups a span apart. Consecutive lanes
17
  // access consecutive words on every step, while each lane can keep several
18
  // independent loads in flight.
19
- let tid = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;
20
  let span = (params.count + ITEMS - 1u) / ITEMS;
 
 
 
 
 
 
21
  for (var j = 0u; j < ITEMS; j = j + 1u) {
22
  let i = tid + j * span;
23
  if (i >= params.count) {
24
  break;
25
  }
26
  {% else %}
27
- let i = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;
28
- if (i >= params.count) {
29
- return;
30
- }
31
  {% endif %}
32
-
33
  let xv = x[i];
34
  y[i] = select(vec4<u32>(0u), vec4<u32>(1u), xv == vec4<u32>(0u));
35
  {% if vec4PerThread > 1 %}
 
2
  // component.
3
  {{ env.wgsl.resourceDeclarations }}
4
 
5
+ {% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}
6
+ {% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
7
+ // 2D-folded flat index: gid.y carries the high bits past the dispatch's
8
+ // per-axis workgroup fold width.
9
+ {% if bound == "" %}
10
+ let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};{% else %}
11
+ let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};
12
+ if ({{ name }} >= {{ bound }}) {
13
+ return;
14
+ }{% endif %}{% endmacro %}
15
  {% if vec4PerThread > 1 %}
16
  const ITEMS: u32 = {{ vec4PerThread }}u;
 
17
 
18
+ {% endif %}
19
  @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
20
  fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
 
 
21
  {% if vec4PerThread > 1 %}
22
  // Each invocation walks ITEMS vec4 groups a span apart. Consecutive lanes
23
  // access consecutive words on every step, while each lane can keep several
24
  // independent loads in flight.
25
+ {{ flat_index_2d(tunables.WORKGROUP_SIZE, "tid", "") }}
26
  let span = (params.count + ITEMS - 1u) / ITEMS;
27
+ // Lanes at or past span exist only because the dispatch rounds up to whole
28
+ // workgroups. Lane span + k would start on lane k's second group and rewrite
29
+ // up to ITEMS - 1 groups another lane already stored.
30
+ if (tid >= span) {
31
+ return;
32
+ }
33
  for (var j = 0u; j < ITEMS; j = j + 1u) {
34
  let i = tid + j * span;
35
  if (i >= params.count) {
36
  break;
37
  }
38
  {% else %}
39
+ {{ flat_index_2d(tunables.WORKGROUP_SIZE) }}
 
 
 
40
  {% endif %}
 
41
  let xv = x[i];
42
  y[i] = select(vec4<u32>(0u), vec4<u32>(1u), xv == vec4<u32>(0u));
43
  {% if vec4PerThread > 1 %}