Xenova HF Staff commited on
Commit
19fcf4a
·
verified ·
1 Parent(s): 47e90a2

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.Neg
10
+
11
+ `ai.onnx` · standard ONNX operator · ONNX opset ≥ 13
12
+
13
+ ## Description
14
+
15
+ Applies elementwise arithmetic negation to a tensor: `y = -x`. The output has the same shape and type as the input.
16
+
17
+ See the [ONNX `Neg` spec](https://onnx.ai/onnx/operators/onnx__Neg.html) for the reference semantics.
18
+
19
+ ## Inputs
20
+
21
+ | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
22
+ | --- | --- | --- | --- | --- | --- | --- |
23
+ | `X` | `x` | `T` | — | — | Input tensor whose elements are to be negated. | 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 with each element flipped in sign. | required |
30
+
31
+ ## Type constraints
32
+
33
+ | Variable | Allowed dtypes |
34
+ | --- | --- |
35
+ | `T` | `float32`, `float16`, `int32`, `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.Neg", { 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.Neg",
3
+ "tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] },
4
+ "cases": [
5
+ {
6
+ "name": "neg-f32-1m",
7
+ "preset": "smoke",
8
+ "vars": { "dtype": "float32", "count": 1048576 },
9
+ "inputs": { "x": { "shape": [1048576], "dtype": "float32", "dist": "normal", "seed": 920, "scale": 1 } },
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": "neg-f32-8m",
18
+ "preset": "smoke",
19
+ "vars": { "dtype": "float32", "count": 8388608 },
20
+ "inputs": { "x": { "shape": [8388608], "dtype": "float32", "seed": 7017, "dist": "normal", "scale": 1 } },
21
+ "outputs": { "y": { "shape": [8388608], "dtype": "float32" } },
22
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
23
+ },
24
+ {
25
+ "name": "neg-f16-8m",
26
+ "preset": "smoke",
27
+ "vars": { "dtype": "float16", "count": 8388608 },
28
+ "inputs": { "x": { "shape": [8388608], "dtype": "float16", "seed": 7018, "dist": "normal", "scale": 1 } },
29
+ "outputs": { "y": { "shape": [8388608], "dtype": "float16" } },
30
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
31
+ },
32
+ {
33
+ "name": "neg-f32-scalar-fallback-8m",
34
+ "preset": "edge",
35
+ "vars": { "dtype": "float32", "count": 8388607 },
36
+ "inputs": { "x": { "shape": [8388607], "dtype": "float32", "dist": "normal", "seed": 7023, "scale": 1 } },
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,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "domain": "ai.onnx",
3
+ "name": "Neg",
4
+ "sinceVersion": 13,
5
+ "description": "Applies elementwise arithmetic negation to a tensor: `y = -x`. The output has the same shape and type as the input.",
6
+ "inputs": [{ "role": "X", "dtype": "T", "description": "Input tensor whose elements are to be negated." }],
7
+ "outputs": [
8
+ {
9
+ "role": "Y",
10
+ "dtype": "T",
11
+ "rank": "ranks.X",
12
+ "description": "Output tensor with each element flipped in sign.",
13
+ "shape": "shapes.X"
14
+ }
15
+ ],
16
+ "typeConstraints": { "T": ["float32", "float16", "int32", "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
+ "constants": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"", "isInt8": "tensorDtypes.X == \"int8\"" },
23
+ "variants": [
24
+ {
25
+ "id": "same_layout_vec4",
26
+ "when": ["numel(shapes.X) > 0", "numel(shapes.X) % 4 == 0", "numel(shapes.X) == numel(shapes.Y)", "f16Ok(dtypes.T)"],
27
+ "constants": { "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },
28
+ "passes": [
29
+ {
30
+ "id": "main",
31
+ "name": "Neg.vec4",
32
+ "bindings": [
33
+ { "name": "x", "arg": "x", "buffer": { "type": "read-only-storage" }, "elementType": "$vectorScalar" },
34
+ { "name": "y", "arg": "y", "buffer": { "type": "storage" }, "elementType": "$vectorScalar" },
35
+ {
36
+ "name": "params",
37
+ "semantic": "kernel.params",
38
+ "buffer": { "type": "uniform" },
39
+ "struct": {
40
+ "name": "Params",
41
+ "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.y) / 4" }]
42
+ }
43
+ }
44
+ ],
45
+ "dispatch": { "threads": "numel(shapes.Y) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" },
46
+ "source": { "shader": "unary-vec4.wgsl.jinja", "inputs": { "op": "\"neg\"" } }
47
+ }
48
+ ],
49
+ "priority": 20
50
+ },
51
+ {
52
+ "id": "scalar",
53
+ "when": ["numel(shapes.X) == numel(shapes.Y)", "f16Ok(dtypes.T)"],
54
+ "passes": [
55
+ {
56
+ "id": "main",
57
+ "name": "Neg",
58
+ "source": { "shader": "unary-scalar.wgsl.jinja", "inputs": { "op": "\"neg\"", "itemsPerInvocation": 4 } },
59
+ "bindings": [
60
+ { "name": "x", "arg": "x", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" },
61
+ { "name": "y", "arg": "y", "buffer": { "type": "storage" }, "elementType": "$scalar" },
62
+ {
63
+ "name": "params",
64
+ "semantic": "kernel.params",
65
+ "buffer": { "type": "uniform" },
66
+ "struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }] }
67
+ }
68
+ ],
69
+ "dispatch": { "threads": "ceilDiv(numel(shapes.X), 4)", "workgroupSize": "tunables.WORKGROUP_SIZE" }
70
+ }
71
+ ]
72
+ }
73
+ ]
74
+ }
build/webgpu/metadata.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "ai.onnx.Neg",
3
+ "id": "_ai_onnx_neg_webgpu_8236ad6",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "backend": { "type": "webgpu" },
7
+ "digest": {
8
+ "algorithm": "sha256",
9
+ "files": {
10
+ "bench.json": "n5005lamF4m4p0BxJ1kSTJvbtpJuoIEeQ1FPQ0HMuKA=",
11
+ "manifest.json": "PQmzvtWQy5xB+2hy5pYhEUyxUTmHQcfwmJ1WfnkIS5Q=",
12
+ "test.json": "x27LNvCdyCMV4Lg4DSMz7vUiHWGXz7+o9vbFgSRs368=",
13
+ "unary-scalar.wgsl.jinja": "B6v4kydeS0fCwDbfgbbFxI9czECCbxFO++n1qKzvul8=",
14
+ "unary-vec4.wgsl.jinja": "7MxDZBYskcJujwaEid6Vs5Y8f+Wb9aGLE4wjXvJroso="
15
+ }
16
+ },
17
+ "provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } },
18
+ "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/ai.onnx.Neg" }
19
+ }
build/webgpu/test.json ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "op": "ai.onnx.Neg",
3
+ "cases": [
4
+ {
5
+ "name": "vector",
6
+ "inputs": {
7
+ "x": { "dtype": "float32", "shape": [32], "data": { "kind": "fillFloat32", "sinStep": 0.1, "cosStep": 0.2 } }
8
+ },
9
+ "outputs": { "y": { "dtype": "float32", "shape": [32], "tolerance": 0.000001 } }
10
+ },
11
+ {
12
+ "name": "rank0_scalar",
13
+ "inputs": { "x": { "dtype": "float32", "shape": [], "data": { "kind": "values", "values": [-3.5] } } },
14
+ "outputs": { "y": { "dtype": "float32", "shape": [], "tolerance": 0.000001 } }
15
+ },
16
+ {
17
+ "name": "int32_exact_above_float24",
18
+ "inputs": {
19
+ "x": {
20
+ "dtype": "int32",
21
+ "shape": [4],
22
+ "data": { "kind": "values", "values": [16777217, -16777217, 123456789, -123456789] }
23
+ }
24
+ },
25
+ "outputs": { "y": { "dtype": "int32", "shape": [4] } }
26
+ },
27
+ {
28
+ "name": "float_special_values",
29
+ "inputs": {
30
+ "x": {
31
+ "dtype": "float32",
32
+ "shape": [5],
33
+ "data": { "kind": "values", "values": ["-Infinity", 0.0, 0.0, "Infinity", "NaN"] }
34
+ }
35
+ },
36
+ "outputs": { "y": { "dtype": "float32", "shape": [5], "tolerance": 0, "allowNaN": true } }
37
+ },
38
+ {
39
+ "name": "f16_values",
40
+ "inputs": {
41
+ "x": { "dtype": "float16", "shape": [5], "data": { "kind": "values", "values": [-10.0, -1.5, 0.0, 2.0, 8.0] } }
42
+ },
43
+ "outputs": { "y": { "dtype": "float16", "shape": [5], "tolerance": 0 } }
44
+ },
45
+ {
46
+ "name": "ort_float_2x2",
47
+ "provenance": {
48
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
49
+ "test": "MathOpTest.Neg_float"
50
+ },
51
+ "inputs": {
52
+ "x": { "dtype": "float32", "shape": [2, 2], "data": { "kind": "values", "values": [1.0, -2.0, 0.0, -10.0] } }
53
+ },
54
+ "outputs": { "y": { "dtype": "float32", "shape": [2, 2], "tolerance": 0 } }
55
+ },
56
+ {
57
+ "name": "ort_int8_values",
58
+ "provenance": {
59
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
60
+ "test": "MathOpTest.Neg_int8"
61
+ },
62
+ "inputs": { "x": { "dtype": "int8", "shape": [4], "data": { "kind": "values", "values": [1, -2, 0, -10] } } },
63
+ "outputs": { "y": { "dtype": "int8", "shape": [4], "tolerance": 0 } }
64
+ },
65
+ {
66
+ "name": "ort_int8_min_value_overflow_edge",
67
+ "provenance": {
68
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
69
+ "test": "MathOpTest.Neg_int8",
70
+ "notes": "Extends ORT's int8 Neg coverage with INT8_MIN, whose mathematical negation is not representable in int8 storage."
71
+ },
72
+ "inputs": { "x": { "dtype": "int8", "shape": [4], "data": { "kind": "values", "values": [-128, -127, -1, 0] } } },
73
+ "outputs": { "y": { "dtype": "int8", "shape": [4], "tolerance": 0 } }
74
+ },
75
+ {
76
+ "name": "ort_int32_values",
77
+ "provenance": {
78
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
79
+ "test": "MathOpTest.Neg_int32"
80
+ },
81
+ "inputs": { "x": { "dtype": "int32", "shape": [4], "data": { "kind": "values", "values": [1, -2, 0, -10] } } },
82
+ "outputs": { "y": { "dtype": "int32", "shape": [4], "tolerance": 0 } }
83
+ },
84
+ {
85
+ "name": "ort_int16_values_gpu_gap",
86
+ "skipGpu": {
87
+ "category": "todo",
88
+ "reason": "The widened-i32 Neg route is not yet declared and needs an explicit signed 16-bit narrowing step before it can cover the full int16 domain."
89
+ },
90
+ "provenance": {
91
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
92
+ "test": "MathOpTest.Neg_int16",
93
+ "notes": "ORT has signed-integer Neg coverage; this fixture uses the actual ONNX int16 dtype, which is not currently in the WebGPU Neg manifest."
94
+ },
95
+ "inputs": { "x": { "dtype": "int16", "shape": [4], "data": { "kind": "values", "values": [1, -2, 0, -10] } } },
96
+ "outputs": { "y": { "dtype": "int16", "shape": [4], "tolerance": 0 } }
97
+ },
98
+ {
99
+ "name": "ort_int16_min_value_overflow_edge_gpu_gap",
100
+ "skipGpu": {
101
+ "category": "todo",
102
+ "reason": "The current widened-i32 Neg kernel produces 32768 for -(-32768) instead of restoring the required wrapped int16 result; add explicit narrowing before enabling int16."
103
+ },
104
+ "provenance": {
105
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
106
+ "test": "MathOpTest.Neg_int16",
107
+ "notes": "Extends ORT's signed int16 Neg coverage with INT16_MIN, whose mathematical negation is not representable in int16 storage."
108
+ },
109
+ "inputs": {
110
+ "x": { "dtype": "int16", "shape": [4], "data": { "kind": "values", "values": [-32768, -32767, -1, 0] } }
111
+ },
112
+ "outputs": { "y": { "dtype": "int16", "shape": [4], "tolerance": 0 } }
113
+ },
114
+ {
115
+ "name": "f32_subnormal_sign_flip_vec4",
116
+ "provenance": {
117
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
118
+ "test": "MathOpTest.Neg_float",
119
+ "notes": "Finite signed subnormal float32 inputs are valid; Neg should flip their sign without flushing their magnitude to zero."
120
+ },
121
+ "inputs": {
122
+ "x": {
123
+ "dtype": "float32",
124
+ "shape": [4],
125
+ "data": { "kind": "values", "values": [-1e-40, 1e-40, -2e-40, 2e-40] }
126
+ }
127
+ },
128
+ "outputs": { "y": { "dtype": "float32", "shape": [4], "tolerance": 0 } }
129
+ },
130
+ {
131
+ "name": "f32_subnormal_sign_flip_scalar",
132
+ "provenance": {
133
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
134
+ "test": "MathOpTest.Neg_float",
135
+ "notes": "Scalar-path companion for signed subnormal Neg sign flipping."
136
+ },
137
+ "inputs": {
138
+ "x": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [-1e-40, 0.0, 1e-40] } }
139
+ },
140
+ "outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0 } }
141
+ },
142
+ {
143
+ "name": "onnx_backend_example",
144
+ "provenance": {
145
+ "source": "cmake/external/onnx/onnx/backend/test/data/node/test_neg_example",
146
+ "test": "test_neg_example"
147
+ },
148
+ "inputs": { "x": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [-4.0, 2.0] } } },
149
+ "outputs": { "y": { "dtype": "float32", "shape": [2], "tolerance": 0 } }
150
+ },
151
+ {
152
+ "name": "onnx_backend_neg",
153
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_neg" },
154
+ "inputs": {
155
+ "x": {
156
+ "dtype": "float32",
157
+ "shape": [3, 4, 5],
158
+ "data": {
159
+ "kind": "values",
160
+ "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]
161
+ }
162
+ }
163
+ },
164
+ "outputs": { "y": { "dtype": "float32", "shape": [3, 4, 5], "tolerance": 0 } }
165
+ },
166
+ {
167
+ "name": "onnx_backend_neg_example",
168
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_neg_example" },
169
+ "inputs": { "x": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [-4.0, 2.0] } } },
170
+ "outputs": { "y": { "dtype": "float32", "shape": [2], "tolerance": 0.00001 } }
171
+ },
172
+ {
173
+ "name": "vec4_f16_lanes",
174
+ "inputs": {
175
+ "x": {
176
+ "dtype": "float16",
177
+ "shape": [16],
178
+ "data": {
179
+ "kind": "values",
180
+ "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]
181
+ }
182
+ }
183
+ },
184
+ "outputs": { "y": { "dtype": "float16", "shape": [16], "tolerance": 0 } }
185
+ },
186
+ {
187
+ "name": "empty_input_zero_dim",
188
+ "inputs": { "x": { "dtype": "float32", "shape": [0], "data": { "kind": "values", "values": [] } } },
189
+ "outputs": { "y": { "dtype": "float32", "shape": [0], "tolerance": 0 } }
190
+ },
191
+ {
192
+ "name": "int32_min_overflow_wrap",
193
+ "inputs": {
194
+ "x": {
195
+ "dtype": "int32",
196
+ "shape": [4],
197
+ "data": { "kind": "values", "values": [-2147483648, -2147483647, -1, 0] }
198
+ }
199
+ },
200
+ "outputs": {
201
+ "y": {
202
+ "dtype": "int32",
203
+ "shape": [4],
204
+ "tolerance": 0,
205
+ "data": { "kind": "values", "values": [-2147483648, 2147483647, 1, 0] }
206
+ }
207
+ }
208
+ }
209
+ ]
210
+ }
build/webgpu/unary-scalar.wgsl.jinja ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ {% if scalar == "i32" %}
25
+ {% if isInt8 %}
26
+ // int8 is stored in i32 lanes; -(INT8_MIN) wraps to INT8_MIN in the logical
27
+ // dtype. Sign-extend the low byte to reproduce that wraparound.
28
+ y[i] = ((-x[i]) << 24u) >> 24u;
29
+ {% else %}
30
+ y[i] = -x[i];
31
+ {% endif %}
32
+ {% else %}
33
+ y[i] = {{ scalar }}(-f32(x[i]));
34
+ {% endif %}
35
+ {{ flat_tail_close() -}}
36
+ }
build/webgpu/unary-vec4.wgsl.jinja ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ {% if scalar == "i32" %}
19
+ {% if isInt8 %}
20
+ // int8 is stored in i32 lanes; -(INT8_MIN) wraps to INT8_MIN in the logical
21
+ // dtype. Sign-extend the low byte componentwise to reproduce that wraparound.
22
+ y[i] = ((-xv) << vec4<u32>(24u)) >> vec4<u32>(24u);
23
+ {% else %}
24
+ y[i] = -xv;
25
+ {% endif %}
26
+ {% else %}
27
+ y[i] = {{ vectorScalar }}(-vec4<f32>(xv));
28
+ {% endif %}
29
+ }