Xenova HF Staff commited on
Commit
7cd7c7c
·
verified ·
1 Parent(s): a08c66f

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.Sqrt
10
+
11
+ `ai.onnx` · standard ONNX operator · ONNX opset ≥ 13
12
+
13
+ ## Description
14
+
15
+ Computes the elementwise square root of the input tensor: `Y = X^0.5`. Returns `NaN` for negative input values.
16
+
17
+ See the [ONNX `Sqrt` spec](https://onnx.ai/onnx/operators/onnx__Sqrt.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 whose principal square roots are computed elementwise. | 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 containing the elementwise square roots of `X`. | required |
30
+
31
+ ## Type constraints
32
+
33
+ | Variable | Allowed dtypes |
34
+ | --- | --- |
35
+ | `T` | `float32`, `float16` |
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.Sqrt", { version: 1 });
59
+ const { y } = await kernel({ x: { data: xData, shape: [] } });
60
+ ```
build/webgpu/bench.json ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "op": "ai.onnx.Sqrt",
3
+ "tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] },
4
+ "cases": [
5
+ {
6
+ "name": "sqrt-f32-1m",
7
+ "preset": "smoke",
8
+ "vars": { "dtype": "float32", "count": 1048576 },
9
+ "inputs": {
10
+ "x": { "shape": [1048576], "dtype": "float32", "dist": "uniform", "seed": 610, "scale": 1, "offset": 1 }
11
+ },
12
+ "outputs": { "y": { "shape": [1048576], "dtype": "float32" } },
13
+ "bench": {
14
+ "primary": true,
15
+ "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }]
16
+ }
17
+ },
18
+ {
19
+ "name": "sqrt-f32-8m",
20
+ "preset": "smoke",
21
+ "vars": { "dtype": "float32", "count": 8388608 },
22
+ "inputs": {
23
+ "x": { "shape": [8388608], "dtype": "float32", "seed": 7013, "dist": "uniform", "scale": 1, "offset": 1 }
24
+ },
25
+ "outputs": { "y": { "shape": [8388608], "dtype": "float32" } },
26
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
27
+ },
28
+ {
29
+ "name": "sqrt-f16-8m",
30
+ "preset": "smoke",
31
+ "vars": { "dtype": "float16", "count": 8388608 },
32
+ "inputs": {
33
+ "x": { "shape": [8388608], "dtype": "float16", "seed": 7014, "dist": "uniform", "scale": 1, "offset": 1 }
34
+ },
35
+ "outputs": { "y": { "shape": [8388608], "dtype": "float16" } },
36
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
37
+ },
38
+ {
39
+ "name": "sqrt-f32-8m-scalar-fallback",
40
+ "preset": "edge",
41
+ "vars": { "dtype": "float32", "count": 8388609 },
42
+ "inputs": {
43
+ "x": { "shape": [8388609], "dtype": "float32", "dist": "uniform", "seed": 7211, "scale": 1, "offset": 1 }
44
+ },
45
+ "outputs": { "y": { "shape": [8388609], "dtype": "float32", "dist": "empty" } },
46
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
47
+ }
48
+ ]
49
+ }
build/webgpu/manifest.json ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "domain": "ai.onnx",
3
+ "name": "Sqrt",
4
+ "sinceVersion": 13,
5
+ "description": "Computes the elementwise square root of the input tensor: `Y = X^0.5`. Returns `NaN` for negative input values.",
6
+ "inputs": [
7
+ { "role": "X", "dtype": "T", "description": "Values whose principal square roots are computed elementwise." }
8
+ ],
9
+ "outputs": [
10
+ {
11
+ "role": "Y",
12
+ "dtype": "T",
13
+ "rank": "ranks.X",
14
+ "description": "Output tensor containing the elementwise square roots of `X`.",
15
+ "shape": "shapes.X"
16
+ }
17
+ ],
18
+ "typeConstraints": { "T": ["float32", "float16"] },
19
+ "args": {
20
+ "x": { "kind": "tensor", "semantic": "X", "role": "input" },
21
+ "y": { "kind": "tensor", "semantic": "Y", "role": "output" }
22
+ },
23
+ "tunables": { "WORKGROUP_SIZE": 256 },
24
+ "variants": [
25
+ {
26
+ "id": "same_layout_vec4",
27
+ "when": ["numel(shapes.x) > 0", "numel(shapes.x) % 4 == 0", "numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
28
+ "constants": {
29
+ "scalar": "dtypes.T",
30
+ "usesF16": "dtypes.T == \"f16\"",
31
+ "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\""
32
+ },
33
+ "passes": [
34
+ {
35
+ "id": "main",
36
+ "name": "Sqrt.vec4",
37
+ "source": { "shader": "unary-vec4.wgsl.jinja", "inputs": { "op": "\"sqrt\"" } },
38
+ "bindings": [
39
+ { "name": "x", "arg": "x", "buffer": { "type": "read-only-storage" }, "elementType": "$vectorScalar" },
40
+ { "name": "y", "arg": "y", "buffer": { "type": "storage" }, "elementType": "$vectorScalar" },
41
+ {
42
+ "name": "params",
43
+ "semantic": "kernel.params",
44
+ "buffer": { "type": "uniform" },
45
+ "struct": {
46
+ "name": "Params",
47
+ "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.y) / 4" }]
48
+ }
49
+ }
50
+ ],
51
+ "dispatch": { "threads": "numel(shapes.y) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" }
52
+ }
53
+ ],
54
+ "priority": 20
55
+ },
56
+ {
57
+ "id": "scalar",
58
+ "when": ["numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
59
+ "constants": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"" },
60
+ "passes": [
61
+ {
62
+ "id": "main",
63
+ "name": "Sqrt",
64
+ "source": { "shader": "unary-scalar.wgsl.jinja", "inputs": { "op": "\"sqrt\"", "itemsPerInvocation": 4 } },
65
+ "bindings": [
66
+ { "name": "x", "arg": "x", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" },
67
+ { "name": "y", "arg": "y", "buffer": { "type": "storage" }, "elementType": "$scalar" },
68
+ {
69
+ "name": "params",
70
+ "semantic": "kernel.params",
71
+ "buffer": { "type": "uniform" },
72
+ "struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }] }
73
+ }
74
+ ],
75
+ "dispatch": { "threads": "ceilDiv(numel(shapes.y), 4)", "workgroupSize": "tunables.WORKGROUP_SIZE" }
76
+ }
77
+ ]
78
+ }
79
+ ]
80
+ }
build/webgpu/metadata.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "ai.onnx.Sqrt",
3
+ "id": "_ai_onnx_sqrt_webgpu_a12c6e5",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "backend": { "type": "webgpu" },
7
+ "digest": {
8
+ "algorithm": "sha256",
9
+ "files": {
10
+ "bench.json": "zZsOr8xlZCQSGFaKHtZrqo5rA+Cm+Wm57it9ocFzj+o=",
11
+ "manifest.json": "n37vXFoJbr2LZzaamwpaAPZqtoeGiEYLn52ql30g+xo=",
12
+ "test.json": "RSo58XZfuOywGddYHTpyTvrpBXY9gOZL6LWTwjwpgtg=",
13
+ "unary-scalar.wgsl.jinja": "xsLYVBQLpl1Xq5FyPATjZem+6Vqb1o3ef/RsvBuKwAw=",
14
+ "unary-vec4.wgsl.jinja": "8lb1ey0+QO0x7qrQ5uqlrgEeJJtWToXkKBbKSo9EyHc="
15
+ }
16
+ },
17
+ "provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } },
18
+ "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/ai.onnx.Sqrt" }
19
+ }
build/webgpu/test.json ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "op": "ai.onnx.Sqrt",
3
+ "cases": [
4
+ {
5
+ "name": "vector_positive",
6
+ "inputs": { "x": { "dtype": "float32", "shape": [32], "data": { "kind": "constant", "value": 4.0 } } },
7
+ "outputs": { "y": { "dtype": "float32", "shape": [32], "tolerance": 0.000001 } }
8
+ },
9
+ {
10
+ "name": "rank0_scalar_positive",
11
+ "inputs": { "x": { "dtype": "float32", "shape": [], "data": { "kind": "values", "values": [9.0] } } },
12
+ "outputs": { "y": { "dtype": "float32", "shape": [], "tolerance": 0.000001 } }
13
+ },
14
+ {
15
+ "name": "special_values_neg_inf_nan",
16
+ "inputs": {
17
+ "x": {
18
+ "dtype": "float32",
19
+ "shape": [6],
20
+ "data": { "kind": "values", "values": [-1.0, -4.0, 0.0, 4.0, "Infinity", "NaN"] }
21
+ }
22
+ },
23
+ "outputs": { "y": { "dtype": "float32", "shape": [6], "tolerance": 0.000001, "allowNaN": true } }
24
+ },
25
+ {
26
+ "name": "positive_subnormal_values_gpu_gap",
27
+ "skipGpu": {
28
+ "category": "permanent",
29
+ "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."
30
+ },
31
+ "provenance": {
32
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
33
+ "test": "MathOpTest.Sqrt_Float",
34
+ "notes": "Positive subnormal inputs should produce tiny nonzero square roots; zero-flushing loses the signal."
35
+ },
36
+ "inputs": {
37
+ "x": { "dtype": "float32", "shape": [4], "data": { "kind": "values", "values": [1e-45, 1e-40, 1e-39, 1e-38] } }
38
+ },
39
+ "outputs": { "y": { "dtype": "float32", "shape": [4], "tolerance": 1e-27 } }
40
+ },
41
+ {
42
+ "name": "positive_subnormal_values_scalar_gpu_gap",
43
+ "skipGpu": {
44
+ "category": "permanent",
45
+ "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."
46
+ },
47
+ "provenance": {
48
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
49
+ "test": "MathOpTest.Sqrt_Float",
50
+ "notes": "Scalar-path companion: positive subnormal inputs should produce tiny nonzero square roots."
51
+ },
52
+ "inputs": {
53
+ "x": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [1e-45, 1e-40, 1e-39] } }
54
+ },
55
+ "outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 1e-27 } }
56
+ },
57
+ {
58
+ "name": "ort_float_2x2",
59
+ "provenance": {
60
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
61
+ "test": "MathOpTest.Sqrt_Float"
62
+ },
63
+ "inputs": {
64
+ "x": { "dtype": "float32", "shape": [2, 2], "data": { "kind": "values", "values": [1.0, 4.0, 0.0, 9.0] } }
65
+ },
66
+ "outputs": { "y": { "dtype": "float32", "shape": [2, 2], "tolerance": 0.000001 } }
67
+ },
68
+ {
69
+ "name": "ort_float16_2x2",
70
+ "provenance": {
71
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
72
+ "test": "MathOpTest.Sqrt_Float",
73
+ "notes": "ORT TestUnaryFloat16 coverage paired with the float Sqrt fixture."
74
+ },
75
+ "inputs": {
76
+ "x": { "dtype": "float16", "shape": [2, 2], "data": { "kind": "values", "values": [1.0, 4.0, 0.0, 9.0] } }
77
+ },
78
+ "outputs": { "y": { "dtype": "float16", "shape": [2, 2], "tolerance": 0.00098 } }
79
+ },
80
+ {
81
+ "name": "negative_domain_nonfinite_and_signed_zero",
82
+ "provenance": {
83
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
84
+ "test": "MathOpTest.Sqrt_Float",
85
+ "notes": "Additional node-level domain edge: negative finite inputs produce NaN while zeros and infinities stay on the boundary."
86
+ },
87
+ "inputs": {
88
+ "x": {
89
+ "dtype": "float32",
90
+ "shape": [6],
91
+ "data": { "kind": "values", "values": [-1.0, 0.0, 0.0, 4.0, "Infinity", "NaN"] }
92
+ }
93
+ },
94
+ "outputs": {
95
+ "y": {
96
+ "dtype": "float32",
97
+ "shape": [6],
98
+ "tolerance": 0,
99
+ "allowNaN": true,
100
+ "data": { "kind": "values", "values": ["NaN", 0.0, 0.0, 2.0, "Infinity", "NaN"] }
101
+ }
102
+ }
103
+ },
104
+ {
105
+ "name": "large_exact_square_values",
106
+ "provenance": {
107
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
108
+ "test": "MathOpTest.Sqrt_Float",
109
+ "notes": "Additional node-level edge with exactly representable large square inputs and exact integer roots."
110
+ },
111
+ "inputs": {
112
+ "x": {
113
+ "dtype": "float32",
114
+ "shape": [5],
115
+ "data": { "kind": "values", "values": [65536.0, 1048576.0, 16777216.0, 67108864.0, 268435456.0] }
116
+ }
117
+ },
118
+ "outputs": {
119
+ "y": {
120
+ "dtype": "float32",
121
+ "shape": [5],
122
+ "tolerance": 0,
123
+ "data": { "kind": "values", "values": [256.0, 1024.0, 4096.0, 8192.0, 16384.0] }
124
+ }
125
+ }
126
+ },
127
+ {
128
+ "name": "onnx_backend_example",
129
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_sqrt_example" },
130
+ "inputs": { "x": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [1.0, 4.0, 9.0] } } },
131
+ "outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0.000001 } }
132
+ },
133
+ {
134
+ "name": "onnx_backend_sqrt",
135
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_sqrt" },
136
+ "inputs": {
137
+ "x": {
138
+ "dtype": "float32",
139
+ "shape": [3, 4, 5],
140
+ "data": {
141
+ "kind": "values",
142
+ "values": [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]
143
+ }
144
+ }
145
+ },
146
+ "outputs": { "y": { "dtype": "float32", "shape": [3, 4, 5], "tolerance": 0.00001, "allowNaN": true } }
147
+ },
148
+ {
149
+ "name": "onnx_backend_sqrt_example",
150
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_sqrt_example" },
151
+ "inputs": { "x": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [1.0, 4.0, 9.0] } } },
152
+ "outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0.00001 } }
153
+ },
154
+ {
155
+ "name": "empty_input_zero_dim",
156
+ "inputs": { "x": { "dtype": "float32", "shape": [0], "data": { "kind": "values", "values": [] } } },
157
+ "outputs": { "y": { "dtype": "float32", "shape": [0], "tolerance": 0 } }
158
+ },
159
+ {
160
+ "name": "f16_scalar_fallback_negative_and_nan",
161
+ "inputs": {
162
+ "x": { "dtype": "float16", "shape": [5], "data": { "kind": "values", "values": [-4.0, -1.0, 0.0, 4.0, "NaN"] } }
163
+ },
164
+ "outputs": { "y": { "dtype": "float16", "shape": [5], "tolerance": 0.001, "allowNaN": true } }
165
+ },
166
+ {
167
+ "name": "f16_scalar_fallback_large_and_infinity",
168
+ "inputs": {
169
+ "x": {
170
+ "dtype": "float16",
171
+ "shape": [7],
172
+ "data": { "kind": "values", "values": [65504.0, 100.0, 1.0, 0.0, 0.0, "Infinity", "NaN"] }
173
+ }
174
+ },
175
+ "outputs": { "y": { "dtype": "float16", "shape": [7], "tolerance": 1, "allowNaN": true } }
176
+ }
177
+ ]
178
+ }
build/webgpu/unary-scalar.wgsl.jinja ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 usesF16 %}
20
+ enable f16;
21
+ {% endif %}
22
+ {{ env.wgsl.resourceDeclarations }}
23
+ {{ flat_tail_open() }}
24
+ y[i] = {{ scalar }}(sqrt(f32(x[i])));
25
+ {{ flat_tail_close() -}}
26
+ }
build/webgpu/unary-vec4.wgsl.jinja ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
9
+ @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
10
+ fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {
11
+ // 2D-folded flat index: gid.y carries the high bits when the element count exceeds the
12
+ // maxComputeWorkgroupsPerDimension limit (the dispatch caps x and spills the rest into y).
13
+ let i = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;
14
+ if (i >= params.count) {
15
+ return;
16
+ }
17
+ let xv = x[i];
18
+ y[i] = {{ vectorScalar }}(sqrt(vec4<f32>(xv)));
19
+ }