Xenova HF Staff commited on
Commit
b372242
·
verified ·
1 Parent(s): a6387bd

sync 2e7068faf55e

Browse files
README.md CHANGED
@@ -1,3 +1,60 @@
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.Relu
10
+
11
+ `ai.onnx` · standard ONNX operator · ONNX opset ≥ 14
12
+
13
+ ## Description
14
+
15
+ Applies the rectified linear unit function elementwise: `y = max(0, x)`. The output has the same shape and type as the input.
16
+
17
+ See the [ONNX `Relu` spec](https://onnx.ai/onnx/operators/onnx__Relu.html) for the reference semantics.
18
+
19
+ ## Inputs
20
+
21
+ | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
22
+ | --- | --- | --- | --- | --- | --- | --- |
23
+ | `X` | `x` | `T` | — | — | Values clamped elementwise to a minimum of zero. | required |
24
+
25
+ ## Outputs
26
+
27
+ | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
28
+ | --- | --- | --- | --- | --- | --- | --- |
29
+ | `Y` | `y` | `T` | same as `X` | same as `X` | Output tensor; same shape as the input. | required |
30
+
31
+ ## Type constraints
32
+
33
+ | Variable | Allowed dtypes |
34
+ | --- | --- |
35
+ | `T` | `float32`, `float16`, `int32`, `int16`, `int8` |
36
+
37
+ ## Files
38
+
39
+ - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, 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
+ The loader derives every required output's shape and logical dtype from the manifest contract and this call.
49
+ It then allocates the result tensors automatically.
50
+
51
+ The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.
52
+
53
+ Replace each `*Data` placeholder with a typed array containing the corresponding input data.
54
+
55
+ ```js
56
+ import { getKernel } from "@huggingface/kernels";
57
+
58
+ const kernel = await getKernel("webgpu-kernels/ai.onnx.Relu", { version: 1 });
59
+ const { y } = await kernel({ x: { data: xData, shape: [] } });
60
+ ```
build/webgpu/bench.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "op": "ai.onnx.Relu",
3
+ "tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] },
4
+ "cases": [
5
+ {
6
+ "name": "relu-f32-1m",
7
+ "preset": "smoke",
8
+ "vars": { "dtype": "float32", "count": 1048576 },
9
+ "inputs": { "x": { "shape": [1048576], "dtype": "float32", "dist": "normal", "seed": 101, "scale": 2 } },
10
+ "outputs": { "y": { "shape": [1048576], "dtype": "float32" } },
11
+ "bench": {
12
+ "primary": true,
13
+ "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }]
14
+ }
15
+ },
16
+ {
17
+ "name": "relu-f32-8m",
18
+ "preset": "smoke",
19
+ "vars": { "dtype": "float32", "count": 8388608 },
20
+ "inputs": { "x": { "shape": [8388608], "dtype": "float32", "seed": 7015, "dist": "normal", "scale": 2 } },
21
+ "outputs": { "y": { "shape": [8388608], "dtype": "float32" } },
22
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
23
+ },
24
+ {
25
+ "name": "relu-f16-8m",
26
+ "preset": "smoke",
27
+ "vars": { "dtype": "float16", "count": 8388608 },
28
+ "inputs": { "x": { "shape": [8388608], "dtype": "float16", "seed": 7016, "dist": "normal", "scale": 2 } },
29
+ "outputs": { "y": { "shape": [8388608], "dtype": "float16" } },
30
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
31
+ },
32
+ {
33
+ "name": "relu-f32-scalar-fallback-8m",
34
+ "preset": "edge",
35
+ "vars": { "dtype": "float32", "count": 8388607 },
36
+ "inputs": { "x": { "shape": [8388607], "dtype": "float32", "dist": "normal", "seed": 7025, "scale": 2 } },
37
+ "outputs": { "y": { "shape": [8388607], "dtype": "float32", "dist": "empty" } },
38
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
39
+ }
40
+ ]
41
+ }
build/webgpu/manifest.json ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "domain": "ai.onnx",
3
+ "name": "Relu",
4
+ "sinceVersion": 14,
5
+ "description": "Applies the rectified linear unit function elementwise: `y = max(0, x)`. The output has the same shape and type as the input.",
6
+ "inputs": [{ "role": "X", "dtype": "T", "description": "Values clamped elementwise to a minimum of zero." }],
7
+ "outputs": [
8
+ {
9
+ "role": "Y",
10
+ "dtype": "T",
11
+ "rank": "ranks.x",
12
+ "shape": "shapes.x",
13
+ "description": "Output tensor; same shape as the input."
14
+ }
15
+ ],
16
+ "typeConstraints": { "T": ["float32", "float16", "int32", "int16", "int8"] },
17
+ "args": {
18
+ "x": { "kind": "tensor", "semantic": "X", "role": "input" },
19
+ "y": { "kind": "tensor", "semantic": "Y", "role": "output" }
20
+ },
21
+ "tunables": { "WORKGROUP_SIZE": 256 },
22
+ "derive": {
23
+ "wideVec4StorageOk": "dtypes.T != \"f32\" or device.features.has(\"subgroups\") or not (has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize == 8 and device.adapterInfo.subgroupMaxSize <= 32)"
24
+ },
25
+ "variants": [
26
+ {
27
+ "id": "same_layout_vec4",
28
+ "when": ["numel(shapes.X) > 0", "numel(shapes.X) % 4 == 0", "numel(shapes.X) == numel(shapes.Y)", "f16Ok(dtypes.T)", "wideVec4StorageOk"],
29
+ "passes": [
30
+ {
31
+ "id": "main",
32
+ "name": "Relu.vec4",
33
+ "bindings": [
34
+ { "name": "x", "arg": "x", "buffer": { "type": "read-only-storage" }, "elementType": "$vectorScalar" },
35
+ { "name": "y", "arg": "y", "buffer": { "type": "storage" }, "elementType": "$vectorScalar" },
36
+ {
37
+ "name": "params",
38
+ "semantic": "kernel.params",
39
+ "buffer": { "type": "uniform" },
40
+ "struct": {
41
+ "name": "Params",
42
+ "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.y) / 4" }]
43
+ }
44
+ }
45
+ ],
46
+ "dispatch": { "threads": "numel(shapes.Y) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" },
47
+ "source": { "shader": "unary-vec4.wgsl.jinja", "inputs": { "op": "\"relu\"" } }
48
+ }
49
+ ],
50
+ "priority": 20,
51
+ "constants": {
52
+ "scalar": "dtypes.T",
53
+ "usesF16": "dtypes.T == \"f16\"",
54
+ "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\""
55
+ }
56
+ },
57
+ {
58
+ "id": "scalar",
59
+ "when": "f16Ok(dtypes.T)",
60
+ "passes": [
61
+ {
62
+ "id": "main",
63
+ "name": "scalar",
64
+ "source": { "shader": "unary-scalar.wgsl.jinja", "inputs": { "op": "\"relu\"", "itemsPerInvocation": 4 } },
65
+ "bindings": [
66
+ { "name": "x", "arg": "x", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$T" },
67
+ { "name": "y", "arg": "y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$T" },
68
+ {
69
+ "name": "params",
70
+ "semantic": "kernel.params",
71
+ "buffer": { "type": "uniform" },
72
+ "struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.X)" }] }
73
+ }
74
+ ],
75
+ "dispatch": { "threads": "ceilDiv(numel(shapes.X), 4)", "workgroupSize": "tunables.WORKGROUP_SIZE" }
76
+ }
77
+ ]
78
+ }
79
+ ]
80
+ }
build/webgpu/metadata.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "ai.onnx.Relu",
3
+ "id": "_ai_onnx_relu_webgpu_38d226d",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "backend": { "type": "webgpu" },
7
+ "digest": {
8
+ "algorithm": "sha256",
9
+ "files": {
10
+ "bench.json": "pCIgxzDmyCADelXHsyZ3NqiYlep/O2kEemkYPRpHi4U=",
11
+ "manifest.json": "J17/rYpIwl28VO9DLnSBUimzXY15xRye4otbVNfgPOg=",
12
+ "test.json": "h0j4IDFTdi+LEdq6CjTJnSNQ1PTE9tpICFf8PVVFEKs=",
13
+ "unary-scalar.wgsl.jinja": "Vgm24ufHWZEC2cUeI0dA2snfsng2zIRpM1wC8URlqKA=",
14
+ "unary-vec4.wgsl.jinja": "xSW7Bah2pFe8MxojMV3phM8GEeicxBAk++vGxLURc0Q="
15
+ }
16
+ },
17
+ "provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } },
18
+ "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/ai.onnx.Relu" }
19
+ }
build/webgpu/test.json ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "op": "ai.onnx.Relu",
3
+ "fixtureArrays": {
4
+ "onnx_backend_input_x": [1.764052391052246, 0.40015721321105957, 0.978738009929657, 2.2408931255340576, 1.8675580024719238, -0.9772778749465942, 0.9500884413719177, -0.15135720372200012, -0.10321885347366333, 0.4105985164642334, 0.14404356479644775, 1.4542734622955322, 0.7610377073287964, 0.12167501449584961, 0.44386324286460876, 0.3336743414402008, 1.4940791130065918, -0.2051582634449005, 0.3130677044391632, -0.8540957570075989, -2.5529897212982178, 0.653618574142456, 0.8644362092018127, -0.7421650290489197, 2.269754648208618, -1.4543657302856445, 0.04575851559638977, -0.18718385696411133, 1.5327792167663574, 1.4693588018417358, 0.154947429895401, 0.37816253304481506, -0.8877857327461243, -1.980796456336975, -0.34791216254234314, 0.15634897351264954, 1.2302906513214111, 1.202379822731018, -0.38732680678367615, -0.302302747964859, -1.0485529899597168, -1.420017957687378, -1.7062702178955078, 1.950775384902954, -0.5096521973609924, -0.4380742907524109, -1.2527953386306763, 0.7774903774261475, -1.6138978004455566, -0.21274028718471527, -0.8954665660858154, 0.38690251111984253, -0.5108051300048828, -1.18063223361969, -0.02818222902715206, 0.4283318817615509, 0.06651721894741058, 0.30247190594673157, -0.6343221068382263, -0.3627411723136902]
5
+ },
6
+ "cases": [
7
+ {
8
+ "name": "int16_scalar_boundaries",
9
+ "inputs": {
10
+ "x": { "dtype": "int16", "shape": [5], "data": { "kind": "values", "values": [-32768, -1, 0, 1, 32767] } }
11
+ },
12
+ "outputs": {
13
+ "y": {
14
+ "dtype": "int16",
15
+ "shape": [5],
16
+ "tolerance": 0,
17
+ "data": { "kind": "values", "values": [0, 0, 0, 1, 32767] }
18
+ }
19
+ }
20
+ },
21
+ {
22
+ "name": "vector_17",
23
+ "inputs": {
24
+ "x": { "dtype": "float32", "shape": [17], "data": { "kind": "fillFloat32", "sinStep": 0.37, "cosStep": 0.19 } }
25
+ },
26
+ "outputs": { "y": { "dtype": "float32", "shape": [17], "tolerance": 0.000001 } }
27
+ },
28
+ {
29
+ "name": "rank0_negative_scalar",
30
+ "inputs": { "x": { "dtype": "float32", "shape": [], "data": { "kind": "values", "values": [-0.125] } } },
31
+ "outputs": { "y": { "dtype": "float32", "shape": [], "tolerance": 0.000001 } }
32
+ },
33
+ {
34
+ "name": "nan_input_propagates",
35
+ "inputs": {
36
+ "x": { "dtype": "float32", "shape": [4], "data": { "kind": "values", "values": ["NaN", -1.0, 0.0, 2.0] } }
37
+ },
38
+ "outputs": { "y": { "dtype": "float32", "shape": [4], "tolerance": 0.000001, "allowNaN": true } }
39
+ },
40
+ {
41
+ "name": "f32_positive_subnormal_preserved_gpu_gap",
42
+ "skipGpu": {
43
+ "category": "permanent",
44
+ "reason": "Portable WGSL floating-point semantics do not guarantee preservation of the subnormal values required by this fixture. Backend evidence: WebGPU/Metal flushes subnormals to zero (f32 and f16); the kernel cannot preserve denormal inputs/outputs bit-exactly."
45
+ },
46
+ "provenance": {
47
+ "source": "onnxruntime/test/providers/cpu/activation/activation_op_test.cc",
48
+ "test": "ActivationOpTest.Relu",
49
+ "notes": "Positive subnormal activations are valid Relu outputs; zero-flushing drops them while negative subnormals still clamp to zero."
50
+ },
51
+ "inputs": {
52
+ "x": { "dtype": "float32", "shape": [4], "data": { "kind": "values", "values": [-1e-40, 0.0, 1e-40, 1e-39] } }
53
+ },
54
+ "outputs": { "y": { "dtype": "float32", "shape": [4], "tolerance": 0 } }
55
+ },
56
+ {
57
+ "name": "f32_positive_subnormal_preserved_scalar_gpu_gap",
58
+ "skipGpu": {
59
+ "category": "permanent",
60
+ "reason": "Portable WGSL floating-point semantics do not guarantee preservation of the subnormal values required by this fixture. Backend evidence: WebGPU/Metal flushes subnormals to zero (f32 and f16); the kernel cannot preserve denormal inputs/outputs bit-exactly."
61
+ },
62
+ "provenance": {
63
+ "source": "onnxruntime/test/providers/cpu/activation/activation_op_test.cc",
64
+ "test": "ActivationOpTest.Relu",
65
+ "notes": "Scalar-path companion: positive subnormal activations must pass through Relu unchanged."
66
+ },
67
+ "inputs": {
68
+ "x": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [-1e-40, 0.0, 1e-40] } }
69
+ },
70
+ "outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0 } }
71
+ },
72
+ {
73
+ "name": "int32_exact_above_float24",
74
+ "inputs": {
75
+ "x": {
76
+ "dtype": "int32",
77
+ "shape": [5],
78
+ "data": { "kind": "values", "values": [16777217, -16777217, 0, 123456789, -123456789] }
79
+ }
80
+ },
81
+ "outputs": { "y": { "dtype": "int32", "shape": [5] } }
82
+ },
83
+ {
84
+ "name": "ort_f32_activation_extremes",
85
+ "provenance": {
86
+ "source": "onnxruntime/test/providers/cpu/activation/activation_op_test.cc",
87
+ "test": "ActivationOpTest.Relu",
88
+ "notes": "Float32 subset of ORT's shared activation vector."
89
+ },
90
+ "inputs": {
91
+ "x": {
92
+ "dtype": "float32",
93
+ "shape": [13],
94
+ "data": {
95
+ "kind": "values",
96
+ "values": [-1.0, 0.0, 1.0, 100.0, -100.0, 1000.0, -1000.0, 1.1754943508222875e-38, 1.1754943508222876e-39, -1.1754943508222876e-39, 3.4028234663852886e+38, -3.4028234663852886e+38, "Infinity"]
97
+ }
98
+ }
99
+ },
100
+ "outputs": { "y": { "dtype": "float32", "shape": [13], "tolerance": 0.000001 } }
101
+ },
102
+ {
103
+ "name": "ort_int8_activation_values",
104
+ "provenance": {
105
+ "source": "onnxruntime/test/providers/cpu/activation/activation_op_test.cc",
106
+ "test": "ActivationOpTest.Relu",
107
+ "notes": "Int8 values from ORT's Relu coverage."
108
+ },
109
+ "inputs": {
110
+ "x": {
111
+ "dtype": "int8",
112
+ "shape": [9],
113
+ "data": { "kind": "values", "values": [-1, -5, 0, 1, 5, 100, -100, -128, 127] }
114
+ }
115
+ },
116
+ "outputs": { "y": { "dtype": "int8", "shape": [9], "tolerance": 0 } }
117
+ },
118
+ {
119
+ "name": "ort_fp16_activation_extremes",
120
+ "provenance": {
121
+ "source": "onnxruntime/test/providers/cpu/activation/activation_op_test.cc",
122
+ "test": "ActivationOpTest.Relu_fp16"
123
+ },
124
+ "inputs": {
125
+ "x": {
126
+ "dtype": "float16",
127
+ "shape": [13],
128
+ "data": {
129
+ "kind": "values",
130
+ "values": [-1.0, 0.0, 1.0, 100.0, -100.0, 1000.0, -1000.0, 1.1754943508222875e-38, 1.1754943508222876e-39, -1.1754943508222876e-39, 3.4028234663852886e+38, -3.4028234663852886e+38, "Infinity"]
131
+ }
132
+ }
133
+ },
134
+ "outputs": { "y": { "dtype": "float16", "shape": [13], "tolerance": 0.001 } }
135
+ },
136
+ {
137
+ "name": "onnx_backend_rank3_float32",
138
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_relu" },
139
+ "inputs": {
140
+ "x": {
141
+ "dtype": "float32",
142
+ "shape": [3, 4, 5],
143
+ "data": { "kind": "values", "values": { "$ref": "#/fixtureArrays/onnx_backend_input_x" } }
144
+ }
145
+ },
146
+ "outputs": { "y": { "dtype": "float32", "shape": [3, 4, 5], "tolerance": 0 } }
147
+ },
148
+ {
149
+ "name": "onnx_backend_relu",
150
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_relu" },
151
+ "inputs": {
152
+ "x": {
153
+ "dtype": "float32",
154
+ "shape": [3, 4, 5],
155
+ "data": { "kind": "values", "values": { "$ref": "#/fixtureArrays/onnx_backend_input_x" } }
156
+ }
157
+ },
158
+ "outputs": { "y": { "dtype": "float32", "shape": [3, 4, 5], "tolerance": 0.00001 } }
159
+ },
160
+ {
161
+ "name": "vec4_f16_lanes",
162
+ "inputs": {
163
+ "x": {
164
+ "dtype": "float16",
165
+ "shape": [16],
166
+ "data": {
167
+ "kind": "values",
168
+ "values": [-6.0, -4.0, -3.0, -2.0, -1.5, -1.0, -0.5, -0.25, 0.0, 0.25, 0.5, 1.0, 1.5, 2.0, 4.0, 6.0]
169
+ }
170
+ }
171
+ },
172
+ "outputs": { "y": { "dtype": "float16", "shape": [16], "tolerance": 0 } }
173
+ },
174
+ {
175
+ "name": "vec4_i32_lanes",
176
+ "inputs": {
177
+ "x": { "dtype": "int32", "shape": [8], "data": { "kind": "values", "values": [-5, -1, 0, 3, 7, -100, 100, 2] } }
178
+ },
179
+ "outputs": { "y": { "dtype": "int32", "shape": [8] } }
180
+ },
181
+ {
182
+ "name": "vec4_i8_lanes",
183
+ "inputs": {
184
+ "x": {
185
+ "dtype": "int8",
186
+ "shape": [8],
187
+ "data": { "kind": "values", "values": [-128, -5, -1, 0, 1, 5, 100, 127] }
188
+ }
189
+ },
190
+ "outputs": { "y": { "dtype": "int8", "shape": [8] } }
191
+ },
192
+ {
193
+ "name": "empty_input_zero_dim",
194
+ "inputs": { "x": { "dtype": "float32", "shape": [0], "data": { "kind": "values", "values": [] } } },
195
+ "outputs": { "y": { "dtype": "float32", "shape": [0], "tolerance": 0 } }
196
+ },
197
+ {
198
+ "name": "f16_nan_propagates_vec4",
199
+ "inputs": {
200
+ "x": {
201
+ "dtype": "float16",
202
+ "shape": [8],
203
+ "data": { "kind": "values", "values": ["NaN", -2.0, -1.0, 0.0, 1.0, 2.0, -0.5, 0.5] }
204
+ }
205
+ },
206
+ "outputs": { "y": { "dtype": "float16", "shape": [8], "tolerance": 0, "allowNaN": true } },
207
+ "requires": { "features": ["shader-f16"] }
208
+ }
209
+ ]
210
+ }
build/webgpu/unary-scalar.wgsl.jinja ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% macro flat_tail_open() %}
2
+ @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
3
+ fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {
4
+ // 2D-folded flat index: gid.y carries the high bits when the element count exceeds the
5
+ // maxComputeWorkgroupsPerDimension limit (the dispatch caps x and spills the rest into y).
6
+ let invocation = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;
7
+ // Tail-safe scalar x4 keeps vector-like dispatch density without requiring
8
+ // the logical tensor length (or its storage binding) to be vec4 aligned.
9
+ let begin = invocation * {{ source.itemsPerInvocation }}u;
10
+ let end = min(begin + {{ source.itemsPerInvocation }}u, params.count);
11
+ for (var i = begin; i < end; i = i + 1u) {
12
+ {%- endmacro %}
13
+ {% macro flat_tail_close() %}
14
+ }
15
+ {% endmacro %}
16
+
17
+ // Scalar unary fallback. Each branch retains the operation's numeric hardening,
18
+ // including Payne-Hanek trigonometric range reduction and NaN/overflow guards.
19
+ {% if T == "f16" %}
20
+ enable f16;
21
+ {% endif %}
22
+ {{ env.wgsl.resourceDeclarations }}
23
+
24
+ {% if T != "i32" %}
25
+ fn is_nan_f32(value: f32) -> bool {
26
+ let bits = bitcast<u32>(value);
27
+ return (bits & 0x7f800000u) == 0x7f800000u && (bits & 0x007fffffu) != 0u;
28
+ }
29
+
30
+ {% endif %}
31
+ {{ flat_tail_open() }}
32
+ {% if T == "i32" %}
33
+ let value = x[i];
34
+ y[i] = select(value, 0i, value < 0i);
35
+ {% else %}
36
+ let value = f32(x[i]);
37
+ var out = max(value, 0.0);
38
+ if (is_nan_f32(value)) {
39
+ out = value;
40
+ }
41
+ y[i] = {{ T }}(out);
42
+ {% endif %}
43
+ {{ flat_tail_close() -}}
44
+ }
build/webgpu/unary-vec4.wgsl.jinja ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Loads and stores vec4<T> (128 bits) while retaining scalar per-component
2
+ // arithmetic, including per-component helper calls for guard-heavy operations.
3
+ {% if usesF16 %}
4
+ enable f16;
5
+ {% endif %}
6
+ {{ env.wgsl.resourceDeclarations }}
7
+
8
+ {% macro emit_is_nan_f32() %}
9
+ fn is_nan_f32(value: f32) -> bool {
10
+ let bits = bitcast<u32>(value);
11
+ return (bits & 0x7f800000u) == 0x7f800000u && (bits & 0x007fffffu) != 0u;
12
+ }{% endmacro %}
13
+ {% if (source.op == "clip" and scalar != "i32" and scalar != "u32") or (source.op == "relu" and scalar != "i32") or source.op == "sin" or source.op == "tan" %}
14
+ {{ emit_is_nan_f32() }}
15
+ {% endif %}
16
+ {% if source.op == "relu" and scalar != "i32" %}
17
+ fn relu_value(value: f32) -> f32 {
18
+ // Some shader compilers apply no-NaN fast-math to vector select/compare
19
+ // expressions. Inspecting the IEEE payload explicitly keeps Relu's required
20
+ // NaN propagation deterministic across backends.
21
+ if (is_nan_f32(value)) {
22
+ return value;
23
+ }
24
+ return max(value, 0.0);
25
+ }
26
+ {% endif %}
27
+
28
+ @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
29
+ fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {
30
+ // 2D-folded flat index: gid.y carries the high bits when the element count exceeds the
31
+ // maxComputeWorkgroupsPerDimension limit (the dispatch caps x and spills the rest into y).
32
+ let i = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;
33
+ if (i >= params.count) {
34
+ return;
35
+ }
36
+ let xv = x[i];
37
+ {% if scalar == "i32" %}
38
+ y[i] = select(xv, vec4<i32>(0i), xv < vec4<i32>(0i));
39
+ {% else %}
40
+ // Apply the IEEE NaN guard per lane because no-NaN fast math can rewrite
41
+ // vector compare/select.
42
+ let fv = vec4<f32>(xv);
43
+ y[i] = {{ vectorScalar }}(vec4<f32>(
44
+ relu_value(fv.x), relu_value(fv.y), relu_value(fv.z), relu_value(fv.w)));
45
+ {% endif %}
46
+ }