Xenova HF Staff commited on
Commit
ba2ec1d
·
verified ·
1 Parent(s): 9565b8c

sync 6fdf6301e2bb

Browse files
README.md CHANGED
@@ -47,14 +47,14 @@ Default values (overridable per request):
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
51
  - [`unary-scalar.wgsl.jinja`](build/webgpu/unary-scalar.wgsl.jinja)
52
  - [`unary-vec4.wgsl.jinja`](build/webgpu/unary-vec4.wgsl.jinja)
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.
 
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 cases
51
  - [`unary-scalar.wgsl.jinja`](build/webgpu/unary-scalar.wgsl.jinja)
52
  - [`unary-vec4.wgsl.jinja`](build/webgpu/unary-vec4.wgsl.jinja)
53
 
54
  ## Use with `@huggingface/kernels`
55
 
56
  ```sh
57
+ npm install --save-exact @huggingface/kernels@0.0.1-preview.3
58
  ```
59
 
60
  Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
build/webgpu/manifest.json CHANGED
@@ -12,18 +12,13 @@
12
  "id": "same_layout_vec4",
13
  "priority": 20,
14
  "when": ["numel(shapes.x) > 0", "numel(shapes.x) % 4 == 0", "numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
15
- "derive": {
16
- "scalar": "dtypes.T",
17
- "usesF16": "dtypes.T == \"f16\"",
18
- "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\""
19
- },
20
  "passes": [
21
  {
22
  "id": "main",
23
- "name": "ThresholdedRelu.vec4",
24
  "shader": "unary-vec4.wgsl.jinja",
25
  "derive": {
26
- "op": "\"thresholded_relu\"",
27
  "alpha": "attrs.alpha",
28
  "vec4PerThread": "4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1"
29
  },
@@ -39,14 +34,14 @@
39
  {
40
  "id": "elementwise",
41
  "when": ["numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
42
- "derive": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"" },
43
  "passes": [
44
  {
45
  "id": "main",
46
  "name": "ThresholdedRelu",
47
  "shader": "unary-scalar.wgsl.jinja",
48
- "derive": { "op": "\"thresholded_relu\"", "itemsPerInvocation": 4 },
49
- "bindings": ["x_2", "y_2", "params_2_unary_alpha"],
50
  "dispatch": {
51
  "x": "min(ceilDiv((ceilDiv(numel(shapes.y), 4)), (tunables.WORKGROUP_SIZE)), 65535)",
52
  "y": "ceilDiv(ceilDiv((ceilDiv(numel(shapes.y), 4)), (tunables.WORKGROUP_SIZE)), 65535)",
@@ -59,18 +54,13 @@
59
  "id": "same_layout_vec4_tail",
60
  "priority": 19,
61
  "when": ["numel(shapes.x) > 4", "numel(shapes.x) % 4 != 0", "numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
62
- "derive": {
63
- "scalar": "dtypes.T",
64
- "usesF16": "dtypes.T == \"f16\"",
65
- "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\""
66
- },
67
  "passes": [
68
  {
69
  "id": "bulk",
70
- "name": "ThresholdedRelu.vec4Bulk",
71
  "shader": "unary-vec4.wgsl.jinja",
72
  "derive": {
73
- "op": "\"thresholded_relu\"",
74
  "alpha": "attrs.alpha",
75
  "vec4PerThread": "4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1"
76
  },
@@ -83,10 +73,10 @@
83
  },
84
  {
85
  "id": "tail",
86
- "name": "ThresholdedRelu.tail",
87
  "shader": "unary-scalar.wgsl.jinja",
88
- "derive": { "op": "\"thresholded_relu\"", "itemsPerInvocation": 4, "tailOnly": true },
89
- "bindings": ["x_2", "y_2", "params_2_unary_alpha_tail"],
90
  "dispatch": {
91
  "x": "min(ceilDiv((1), (tunables.WORKGROUP_SIZE)), 65535)",
92
  "y": "ceilDiv(ceilDiv((1), (tunables.WORKGROUP_SIZE)), 65535)",
@@ -100,14 +90,12 @@
100
  "x": { "buffer": "read-only-storage", "elementType": "$vectorScalar" },
101
  "y": { "buffer": "storage", "elementType": "$vectorScalar" },
102
  "params_unary_alpha": {
103
- "buffer": "uniform",
104
  "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.y) / 4" }],
105
  "name": "params"
106
  },
107
- "x_2": { "buffer": "read-only-storage", "name": "x", "elementType": "$scalar" },
108
- "y_2": { "buffer": "storage", "name": "y", "elementType": "$scalar" },
109
  "params_2_unary_alpha": {
110
- "buffer": "uniform",
111
  "name": "params",
112
  "struct": [
113
  { "name": "count", "type": "u32", "value": "numel(shapes.y)" },
@@ -115,12 +103,10 @@
115
  ]
116
  },
117
  "params_unary_alpha_tail": {
118
- "buffer": "uniform",
119
  "struct": [{ "name": "count", "type": "u32", "value": "floor(numel(shapes.y) / 4)" }],
120
  "name": "params"
121
  },
122
  "params_2_unary_alpha_tail": {
123
- "buffer": "uniform",
124
  "name": "params",
125
  "struct": [
126
  { "name": "count", "type": "u32", "value": "numel(shapes.y)" },
 
12
  "id": "same_layout_vec4",
13
  "priority": 20,
14
  "when": ["numel(shapes.x) > 0", "numel(shapes.x) % 4 == 0", "numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
15
+ "derive": { "scalar": "dtypes.T", "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },
 
 
 
 
16
  "passes": [
17
  {
18
  "id": "main",
19
+ "name": "ThresholdedRelu.Vec4",
20
  "shader": "unary-vec4.wgsl.jinja",
21
  "derive": {
 
22
  "alpha": "attrs.alpha",
23
  "vec4PerThread": "4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1"
24
  },
 
34
  {
35
  "id": "elementwise",
36
  "when": ["numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
37
+ "derive": { "scalar": "dtypes.T" },
38
  "passes": [
39
  {
40
  "id": "main",
41
  "name": "ThresholdedRelu",
42
  "shader": "unary-scalar.wgsl.jinja",
43
+ "derive": { "itemsPerInvocation": 4 },
44
+ "bindings": ["x_scalar", "y_scalar", "params_2_unary_alpha"],
45
  "dispatch": {
46
  "x": "min(ceilDiv((ceilDiv(numel(shapes.y), 4)), (tunables.WORKGROUP_SIZE)), 65535)",
47
  "y": "ceilDiv(ceilDiv((ceilDiv(numel(shapes.y), 4)), (tunables.WORKGROUP_SIZE)), 65535)",
 
54
  "id": "same_layout_vec4_tail",
55
  "priority": 19,
56
  "when": ["numel(shapes.x) > 4", "numel(shapes.x) % 4 != 0", "numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
57
+ "derive": { "scalar": "dtypes.T", "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },
 
 
 
 
58
  "passes": [
59
  {
60
  "id": "bulk",
61
+ "name": "ThresholdedRelu.Vec4Bulk",
62
  "shader": "unary-vec4.wgsl.jinja",
63
  "derive": {
 
64
  "alpha": "attrs.alpha",
65
  "vec4PerThread": "4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1"
66
  },
 
73
  },
74
  {
75
  "id": "tail",
76
+ "name": "ThresholdedRelu.Tail",
77
  "shader": "unary-scalar.wgsl.jinja",
78
+ "derive": { "itemsPerInvocation": 4, "tailOnly": true },
79
+ "bindings": ["x_scalar", "y_scalar", "params_2_unary_alpha_tail"],
80
  "dispatch": {
81
  "x": "min(ceilDiv((1), (tunables.WORKGROUP_SIZE)), 65535)",
82
  "y": "ceilDiv(ceilDiv((1), (tunables.WORKGROUP_SIZE)), 65535)",
 
90
  "x": { "buffer": "read-only-storage", "elementType": "$vectorScalar" },
91
  "y": { "buffer": "storage", "elementType": "$vectorScalar" },
92
  "params_unary_alpha": {
 
93
  "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.y) / 4" }],
94
  "name": "params"
95
  },
96
+ "x_scalar": { "buffer": "read-only-storage", "name": "x", "elementType": "$scalar" },
97
+ "y_scalar": { "buffer": "storage", "name": "y", "elementType": "$scalar" },
98
  "params_2_unary_alpha": {
 
99
  "name": "params",
100
  "struct": [
101
  { "name": "count", "type": "u32", "value": "numel(shapes.y)" },
 
103
  ]
104
  },
105
  "params_unary_alpha_tail": {
 
106
  "struct": [{ "name": "count", "type": "u32", "value": "floor(numel(shapes.y) / 4)" }],
107
  "name": "params"
108
  },
109
  "params_2_unary_alpha_tail": {
 
110
  "name": "params",
111
  "struct": [
112
  { "name": "count", "type": "u32", "value": "numel(shapes.y)" },
build/webgpu/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "ai.onnx.ThresholdedRelu",
3
- "id": "_ai_onnx_thresholdedrelu_webgpu_298d44c",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
@@ -8,15 +8,15 @@
8
  "algorithm": "sha256",
9
  "files": {
10
  "bench.json": "Q/QvZ3Z+1f6CMnjA2NsvhHn0+OUUzgy3MTEnfD4OGUg=",
11
- "manifest.json": "PMjpTYsFou999h37B5iXWYzNMgs8ieGwbauqt5AGWcU=",
12
  "test.json": "zo+aTrKshpWFzBmHjaSQmDsS+B5IGLluPx5zuxS+N0w=",
13
- "unary-scalar.wgsl.jinja": "NeQsRK1jLjG0Tw50+OEIUP9RrCg97Ed1rkMlBT/RHoc=",
14
- "unary-vec4.wgsl.jinja": "VZh6jrl9IPkBRvHO1GJ9+zEJY0tJKVh7CEouna+MykQ="
15
  }
16
  },
17
- "provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } },
18
  "webgpu": {
19
- "manifestSpec": "2.0",
20
  "variants": {
21
  "same_layout_vec4": ["unary-vec4.wgsl.jinja"],
22
  "elementwise": ["unary-scalar.wgsl.jinja"],
 
1
  {
2
  "name": "ai.onnx.ThresholdedRelu",
3
+ "id": "_ai_onnx_thresholdedrelu_webgpu_64d7e11",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
 
8
  "algorithm": "sha256",
9
  "files": {
10
  "bench.json": "Q/QvZ3Z+1f6CMnjA2NsvhHn0+OUUzgy3MTEnfD4OGUg=",
11
+ "manifest.json": "Lhg3TJheG8T03L3JQM/ZOBd52HPJKVRZbmVGO++vgkU=",
12
  "test.json": "zo+aTrKshpWFzBmHjaSQmDsS+B5IGLluPx5zuxS+N0w=",
13
+ "unary-scalar.wgsl.jinja": "JKQp405BSFFGByRGJu5V6jbar/aJn/EbuEK0Li8toBs=",
14
+ "unary-vec4.wgsl.jinja": "7a5q6Clfe7fa9XJyNj7oaXFcaFcztBUE3+W90+mLP+w="
15
  }
16
  },
17
+ "provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } },
18
  "webgpu": {
19
+ "manifestSpec": "2.1",
20
  "variants": {
21
  "same_layout_vec4": ["unary-vec4.wgsl.jinja"],
22
  "elementwise": ["unary-scalar.wgsl.jinja"],
build/webgpu/unary-scalar.wgsl.jinja CHANGED
@@ -22,18 +22,13 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
22
  for (var i = begin; i < end; i = i + 1u) {
23
  {%- endmacro %}
24
  {% macro flat_tail_close() %}
25
- }
26
- {% endmacro %}
27
-
28
- // Scalar unary elementwise implementation. Specialization emits only the
29
- // selected operation and any numerical helper it requires.
30
- {% if usesF16 %}
31
- enable f16;
32
- {% endif %}
33
  {{ env.wgsl.resourceDeclarations }}
34
- {% set alpha = alpha | default(0) %}
35
  {{ flat_tail_open() }}
36
  let v = f32(x[i]);
37
  y[i] = {{ scalar }}(select(0.0, v, v > params.alpha));
38
- {{ flat_tail_close() -}}
39
  }
 
22
  for (var i = begin; i < end; i = i + 1u) {
23
  {%- endmacro %}
24
  {% macro flat_tail_close() %}
25
+ }{% endmacro %}
26
+ // Scalar unary elementwise kernel: applies the selected operation, with the
27
+ // numerical helpers it uses, to each element.
 
 
 
 
 
28
  {{ env.wgsl.resourceDeclarations }}
29
+
30
  {{ flat_tail_open() }}
31
  let v = f32(x[i]);
32
  y[i] = {{ scalar }}(select(0.0, v, v > params.alpha));
33
+ {{ flat_tail_close() }}
34
  }
build/webgpu/unary-vec4.wgsl.jinja CHANGED
@@ -2,36 +2,44 @@
2
  // component.
3
  {{ env.wgsl.resourceDeclarations }}
4
 
5
- {% set alpha = alpha | default(0) %}
6
  const THRESHOLDED_RELU_ALPHA: f32 = f32({{ alpha }});
7
 
8
- {% set vec4PerThread = vec4PerThread %}
 
 
 
 
 
 
 
 
 
9
  {% if vec4PerThread > 1 %}
10
  const ITEMS: u32 = {{ vec4PerThread }}u;
11
- {% endif %}
12
 
 
13
  @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
14
  fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
15
- // 2D-folded flat index: gid.y carries the high bits when the element count exceeds the
16
- // per-axis dispatch fold width (the dispatch caps x and spills the rest into y).
17
  {% if vec4PerThread > 1 %}
18
  // Each invocation walks ITEMS vec4 groups a span apart. Consecutive lanes
19
  // access consecutive words on every step, while each lane can keep several
20
  // independent loads in flight.
21
- let tid = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;
22
  let span = (params.count + ITEMS - 1u) / ITEMS;
 
 
 
 
 
 
23
  for (var j = 0u; j < ITEMS; j = j + 1u) {
24
  let i = tid + j * span;
25
  if (i >= params.count) {
26
  break;
27
  }
28
  {% else %}
29
- let i = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;
30
- if (i >= params.count) {
31
- return;
32
- }
33
  {% endif %}
34
-
35
  let xv = x[i];
36
  let fv = vec4<f32>(xv);
37
  y[i] = {{ vectorScalar }}(select(vec4<f32>(0.0), fv, fv > vec4<f32>(THRESHOLDED_RELU_ALPHA)));
 
2
  // component.
3
  {{ env.wgsl.resourceDeclarations }}
4
 
 
5
  const THRESHOLDED_RELU_ALPHA: f32 = f32({{ alpha }});
6
 
7
+ {% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}
8
+ {% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
9
+ // 2D-folded flat index: gid.y carries the high bits past the dispatch's
10
+ // per-axis workgroup fold width.
11
+ {% if bound == "" %}
12
+ let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};{% else %}
13
+ let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};
14
+ if ({{ name }} >= {{ bound }}) {
15
+ return;
16
+ }{% endif %}{% endmacro %}
17
  {% if vec4PerThread > 1 %}
18
  const ITEMS: u32 = {{ vec4PerThread }}u;
 
19
 
20
+ {% endif %}
21
  @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
22
  fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
 
 
23
  {% if vec4PerThread > 1 %}
24
  // Each invocation walks ITEMS vec4 groups a span apart. Consecutive lanes
25
  // access consecutive words on every step, while each lane can keep several
26
  // independent loads in flight.
27
+ {{ flat_index_2d(tunables.WORKGROUP_SIZE, "tid", "") }}
28
  let span = (params.count + ITEMS - 1u) / ITEMS;
29
+ // Lanes at or past span exist only because the dispatch rounds up to whole
30
+ // workgroups. Lane span + k would start on lane k's second group and rewrite
31
+ // up to ITEMS - 1 groups another lane already stored.
32
+ if (tid >= span) {
33
+ return;
34
+ }
35
  for (var j = 0u; j < ITEMS; j = j + 1u) {
36
  let i = tid + j * span;
37
  if (i >= params.count) {
38
  break;
39
  }
40
  {% else %}
41
+ {{ flat_index_2d(tunables.WORKGROUP_SIZE) }}
 
 
 
42
  {% endif %}
 
43
  let xv = x[i];
44
  let fv = vec4<f32>(xv);
45
  y[i] = {{ vectorScalar }}(select(vec4<f32>(0.0), fv, fv > vec4<f32>(THRESHOLDED_RELU_ALPHA)));