Xenova HF Staff commited on
Commit
78aad0a
·
verified ·
1 Parent(s): 6c10a5f

sync 6fdf6301e2bb

Browse files
README.md CHANGED
@@ -39,14 +39,14 @@ See the [ONNX `Exp` spec](https://onnx.ai/onnx/operators/onnx__Exp.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.
@@ -60,5 +60,5 @@ Replace each `*Data` placeholder with a typed array containing the corresponding
60
  import { getKernel } from "@huggingface/kernels";
61
 
62
  const kernel = await getKernel("webgpu-kernels/ai.onnx.Exp", { 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.
 
60
  import { getKernel } from "@huggingface/kernels";
61
 
62
  const kernel = await getKernel("webgpu-kernels/ai.onnx.Exp", { version: 1 });
63
+ const { y } = await kernel({ x: { data: xData, shape: [3] } });
64
  ```
build/webgpu/manifest.json CHANGED
@@ -11,20 +11,13 @@
11
  "id": "same_layout_vec4",
12
  "priority": 20,
13
  "when": ["numel(shapes.x) > 0", "numel(shapes.x) % 4 == 0", "numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
14
- "derive": {
15
- "scalar": "dtypes.T",
16
- "usesF16": "dtypes.T == \"f16\"",
17
- "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\""
18
- },
19
  "passes": [
20
  {
21
  "id": "main",
22
- "name": "Exp.vec4",
23
  "shader": "unary-vec4.wgsl.jinja",
24
- "derive": {
25
- "op": "\"exp\"",
26
- "vec4PerThread": "4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1"
27
- },
28
  "bindings": ["x", "y", "params_unary"],
29
  "dispatch": {
30
  "x": "min(ceilDiv((ceilDiv(numel(shapes.y) / 4, 4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1)), (tunables.WORKGROUP_SIZE)), 65535)",
@@ -37,14 +30,14 @@
37
  {
38
  "id": "scalar",
39
  "when": ["numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
40
- "derive": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"" },
41
  "passes": [
42
  {
43
  "id": "main",
44
  "name": "Exp",
45
  "shader": "unary-scalar.wgsl.jinja",
46
- "derive": { "op": "\"exp\"", "itemsPerInvocation": 4 },
47
- "bindings": ["x_2", "y_2", "params_2_unary"],
48
  "dispatch": {
49
  "x": "min(ceilDiv((ceilDiv(numel(shapes.y), 4)), (tunables.WORKGROUP_SIZE)), 65535)",
50
  "y": "ceilDiv(ceilDiv((ceilDiv(numel(shapes.y), 4)), (tunables.WORKGROUP_SIZE)), 65535)",
@@ -56,34 +49,29 @@
56
  {
57
  "id": "same_layout_vec4_tail",
58
  "priority": 19,
59
- "when": ["numel(shapes.x) > 4", "numel(shapes.x) % 4 != 0", "numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
60
- "derive": {
61
- "scalar": "dtypes.T",
62
- "usesF16": "dtypes.T == \"f16\"",
63
- "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\""
64
- },
65
  "passes": [
66
  {
67
  "id": "bulk",
68
- "name": "Exp.vec4Bulk",
69
  "shader": "unary-vec4.wgsl.jinja",
70
  "derive": {
71
- "op": "\"exp\"",
72
- "vec4PerThread": "4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1"
73
  },
74
  "bindings": ["x", "y", "params_unary_tail"],
75
  "dispatch": {
76
- "x": "min(ceilDiv((ceilDiv(floor(numel(shapes.y) / 4), 4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1)), (tunables.WORKGROUP_SIZE)), 65535)",
77
- "y": "ceilDiv(ceilDiv((ceilDiv(floor(numel(shapes.y) / 4), 4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1)), (tunables.WORKGROUP_SIZE)), 65535)",
78
  "z": 1
79
  }
80
  },
81
  {
82
  "id": "tail",
83
- "name": "Exp.tail",
84
  "shader": "unary-scalar.wgsl.jinja",
85
- "derive": { "op": "\"exp\"", "itemsPerInvocation": 4, "tailOnly": true },
86
- "bindings": ["x_2", "y_2", "params_2_unary_tail"],
87
  "dispatch": {
88
  "x": "min(ceilDiv((1), (tunables.WORKGROUP_SIZE)), 65535)",
89
  "y": "ceilDiv(ceilDiv((1), (tunables.WORKGROUP_SIZE)), 65535)",
@@ -96,25 +84,15 @@
96
  "bindings": {
97
  "x": { "buffer": "read-only-storage", "elementType": "$vectorScalar" },
98
  "y": { "buffer": "storage", "elementType": "$vectorScalar" },
99
- "params_unary": {
100
- "buffer": "uniform",
101
- "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.y) / 4" }],
102
- "name": "params"
103
- },
104
- "x_2": { "buffer": "read-only-storage", "name": "x", "elementType": "$scalar" },
105
- "y_2": { "buffer": "storage", "name": "y", "elementType": "$scalar" },
106
- "params_2_unary": {
107
- "buffer": "uniform",
108
- "name": "params",
109
- "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }]
110
- },
111
  "params_unary_tail": {
112
- "buffer": "uniform",
113
  "struct": [{ "name": "count", "type": "u32", "value": "floor(numel(shapes.y) / 4)" }],
114
  "name": "params"
115
  },
116
  "params_2_unary_tail": {
117
- "buffer": "uniform",
118
  "name": "params",
119
  "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }]
120
  }
 
11
  "id": "same_layout_vec4",
12
  "priority": 20,
13
  "when": ["numel(shapes.x) > 0", "numel(shapes.x) % 4 == 0", "numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
14
+ "derive": { "scalar": "dtypes.T", "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },
 
 
 
 
15
  "passes": [
16
  {
17
  "id": "main",
18
+ "name": "Exp.Vec4",
19
  "shader": "unary-vec4.wgsl.jinja",
20
+ "derive": { "vec4PerThread": "4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1" },
 
 
 
21
  "bindings": ["x", "y", "params_unary"],
22
  "dispatch": {
23
  "x": "min(ceilDiv((ceilDiv(numel(shapes.y) / 4, 4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1)), (tunables.WORKGROUP_SIZE)), 65535)",
 
30
  {
31
  "id": "scalar",
32
  "when": ["numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
33
+ "derive": { "scalar": "dtypes.T" },
34
  "passes": [
35
  {
36
  "id": "main",
37
  "name": "Exp",
38
  "shader": "unary-scalar.wgsl.jinja",
39
+ "derive": { "itemsPerInvocation": 4 },
40
+ "bindings": ["x_scalar", "y_scalar", "params_2_unary"],
41
  "dispatch": {
42
  "x": "min(ceilDiv((ceilDiv(numel(shapes.y), 4)), (tunables.WORKGROUP_SIZE)), 65535)",
43
  "y": "ceilDiv(ceilDiv((ceilDiv(numel(shapes.y), 4)), (tunables.WORKGROUP_SIZE)), 65535)",
 
49
  {
50
  "id": "same_layout_vec4_tail",
51
  "priority": 19,
52
+ "when": ["numel(shapes.x) > 4", "numel(shapes.x) % 4 != 0", "numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)", "ceilDiv(numel(shapes.y), 4) >= ceilDiv(tunables.MIN_TAIL_ELEMENTS | default(0), 4)"],
53
+ "derive": { "scalar": "dtypes.T", "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },
 
 
 
 
54
  "passes": [
55
  {
56
  "id": "bulk",
57
+ "name": "Exp.Vec4Bulk",
58
  "shader": "unary-vec4.wgsl.jinja",
59
  "derive": {
60
+ "vec4PerThread": "tunables.TAIL_VEC4_PER_THREAD | default(4 if numel(shapes.y) * dtypeBytes(tensorDtypes.y) <= 16777216 else 1)"
 
61
  },
62
  "bindings": ["x", "y", "params_unary_tail"],
63
  "dispatch": {
64
+ "x": "min(ceilDiv((ceilDiv(floor(numel(shapes.y) / 4), vec4PerThread)), (tunables.WORKGROUP_SIZE)), 65535)",
65
+ "y": "ceilDiv(ceilDiv((ceilDiv(floor(numel(shapes.y) / 4), vec4PerThread)), (tunables.WORKGROUP_SIZE)), 65535)",
66
  "z": 1
67
  }
68
  },
69
  {
70
  "id": "tail",
71
+ "name": "Exp.Tail",
72
  "shader": "unary-scalar.wgsl.jinja",
73
+ "derive": { "itemsPerInvocation": 4, "tailOnly": true },
74
+ "bindings": ["x_scalar", "y_scalar", "params_2_unary_tail"],
75
  "dispatch": {
76
  "x": "min(ceilDiv((1), (tunables.WORKGROUP_SIZE)), 65535)",
77
  "y": "ceilDiv(ceilDiv((1), (tunables.WORKGROUP_SIZE)), 65535)",
 
84
  "bindings": {
85
  "x": { "buffer": "read-only-storage", "elementType": "$vectorScalar" },
86
  "y": { "buffer": "storage", "elementType": "$vectorScalar" },
87
+ "params_unary": { "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.y) / 4" }], "name": "params" },
88
+ "x_scalar": { "buffer": "read-only-storage", "name": "x", "elementType": "$scalar" },
89
+ "y_scalar": { "buffer": "storage", "name": "y", "elementType": "$scalar" },
90
+ "params_2_unary": { "name": "params", "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }] },
 
 
 
 
 
 
 
 
91
  "params_unary_tail": {
 
92
  "struct": [{ "name": "count", "type": "u32", "value": "floor(numel(shapes.y) / 4)" }],
93
  "name": "params"
94
  },
95
  "params_2_unary_tail": {
 
96
  "name": "params",
97
  "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }]
98
  }
build/webgpu/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "ai.onnx.Exp",
3
- "id": "_ai_onnx_exp_webgpu_54efd57",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
@@ -8,15 +8,15 @@
8
  "algorithm": "sha256",
9
  "files": {
10
  "bench.json": "hXnZmhQq25pSDnNsS9yW3nvS9SsBTTorqFMG3w/mvBE=",
11
- "manifest.json": "Nti89fFoOaDa1+mJ5t9hkSbrnAtbpm0d9typIbHMLkE=",
12
  "test.json": "zkyVhMvJxO8EKNfsAtpXtON8b0ebybHIWCACwCUAzrY=",
13
- "unary-scalar.wgsl.jinja": "SqWzlNZemU567siNk8zaR5u8fDpU7mxHCVMkdKkjT8Q=",
14
- "unary-vec4.wgsl.jinja": "Lr5YK27PDhw5pI17ThbHevm72DJotaTK6fNxoE57PQs="
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
  "scalar": ["unary-scalar.wgsl.jinja"],
 
1
  {
2
  "name": "ai.onnx.Exp",
3
+ "id": "_ai_onnx_exp_webgpu_6bb59b0",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
 
8
  "algorithm": "sha256",
9
  "files": {
10
  "bench.json": "hXnZmhQq25pSDnNsS9yW3nvS9SsBTTorqFMG3w/mvBE=",
11
+ "manifest.json": "ZDJfQavPbpmH2k13HYmNw60SWCVN9wp2cUtuQcL+kJk=",
12
  "test.json": "zkyVhMvJxO8EKNfsAtpXtON8b0ebybHIWCACwCUAzrY=",
13
+ "unary-scalar.wgsl.jinja": "MDwmgm4/F0+YYT/4GjPobaaEL0QJQ23Yn3SxMU7ed64=",
14
+ "unary-vec4.wgsl.jinja": "kXedltivihOenB34znu+SIxCnHrmOU6B/oIh+/us8KY="
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
  "scalar": ["unary-scalar.wgsl.jinja"],
build/webgpu/unary-scalar.wgsl.jinja CHANGED
@@ -22,16 +22,12 @@ 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
  {{ flat_tail_open() }}
35
  y[i] = {{ scalar }}(exp(f32(x[i])));
36
- {{ flat_tail_close() -}}
37
  }
 
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
  y[i] = {{ scalar }}(exp(f32(x[i])));
32
+ {{ flat_tail_close() }}
33
  }
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] = {{ vectorScalar }}(exp(vec4<f32>(xv)));
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] = {{ vectorScalar }}(exp(vec4<f32>(xv)));
43
  {% if vec4PerThread > 1 %}