Xenova HF Staff commited on
Commit
ebc1b30
·
verified ·
1 Parent(s): 495ad1c

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.Log
10
+
11
+ `ai.onnx` · standard ONNX operator · ONNX opset ≥ 13
12
+
13
+ ## Description
14
+
15
+ Computes the natural logarithm of each element of the input tensor, producing an output of the same shape.
16
+
17
+ See the [ONNX `Log` spec](https://onnx.ai/onnx/operators/onnx__Log.html) for the reference semantics.
18
+
19
+ ## Inputs
20
+
21
+ | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
22
+ | --- | --- | --- | --- | --- | --- | --- |
23
+ | `input` | `x` | `T` | — | — | Values whose natural logarithms are computed elementwise. | required |
24
+
25
+ ## Outputs
26
+
27
+ | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
28
+ | --- | --- | --- | --- | --- | --- | --- |
29
+ | `output` | `y` | `T` | same as `input` | same as `input` | The natural log of the input tensor, computed elementwise. | 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.Log", { 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.Log",
3
+ "tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] },
4
+ "cases": [
5
+ {
6
+ "name": "log-f32-1m",
7
+ "preset": "smoke",
8
+ "vars": { "dtype": "float32", "count": 1048576 },
9
+ "inputs": {
10
+ "x": { "shape": [1048576], "dtype": "float32", "dist": "uniform", "seed": 910, "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": "log-f32-8m",
20
+ "preset": "smoke",
21
+ "vars": { "dtype": "float32", "count": 8388608 },
22
+ "inputs": {
23
+ "x": { "shape": [8388608], "dtype": "float32", "seed": 7011, "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": "log-f16-8m",
30
+ "preset": "smoke",
31
+ "vars": { "dtype": "float16", "count": 8388608 },
32
+ "inputs": {
33
+ "x": { "shape": [8388608], "dtype": "float16", "seed": 7012, "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": "log-f32-odd-scalar-fallback",
40
+ "preset": "edge",
41
+ "vars": { "dtype": "float32", "count": 1048577 },
42
+ "inputs": {
43
+ "x": { "shape": [1048577], "dtype": "float32", "dist": "uniform", "seed": 7031, "scale": 1, "offset": 1 }
44
+ },
45
+ "outputs": { "y": { "shape": [1048577], "dtype": "float32" } },
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": "Log",
4
+ "sinceVersion": 13,
5
+ "description": "Computes the natural logarithm of each element of the input tensor, producing an output of the same shape.",
6
+ "inputs": [
7
+ { "role": "input", "dtype": "T", "description": "Values whose natural logarithms are computed elementwise." }
8
+ ],
9
+ "outputs": [
10
+ {
11
+ "role": "output",
12
+ "dtype": "T",
13
+ "rank": "ranks.input",
14
+ "description": "The natural log of the input tensor, computed elementwise.",
15
+ "shape": "shapes.input"
16
+ }
17
+ ],
18
+ "typeConstraints": { "T": ["float32", "float16"] },
19
+ "args": {
20
+ "x": { "kind": "tensor", "semantic": "input", "role": "input" },
21
+ "y": { "kind": "tensor", "semantic": "output", "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": "Log.vec4",
37
+ "source": { "shader": "unary-vec4.wgsl.jinja", "inputs": { "op": "\"log\"" } },
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": "Log",
64
+ "source": { "shader": "unary-scalar.wgsl.jinja", "inputs": { "op": "\"log\"", "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.Log",
3
+ "id": "_ai_onnx_log_webgpu_2cf8591",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "backend": { "type": "webgpu" },
7
+ "digest": {
8
+ "algorithm": "sha256",
9
+ "files": {
10
+ "bench.json": "r7qXaSJXGGynT+ARGoJs6iYyKU+pTj7T7TbCcS1LRLs=",
11
+ "manifest.json": "8qaXwo8hQlTvqDgxd6nCxHGAf8k11eDjoUNfKRvG164=",
12
+ "test.json": "B1RS6+XfT6gcxEtTg2ZGEnEkyMawcYaeCBTIuntIGtc=",
13
+ "unary-scalar.wgsl.jinja": "mfdeF8kKBQgPX6wT3gRXd/fnkMK6WBCOHOO7Y+rNFVY=",
14
+ "unary-vec4.wgsl.jinja": "1Kt6VZ/JRGUyA4Im6S/4ND8TGN3YtreG3gTtpiyTKEg="
15
+ }
16
+ },
17
+ "provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } },
18
+ "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/ai.onnx.Log" }
19
+ }
build/webgpu/test.json ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "op": "ai.onnx.Log",
3
+ "cases": [
4
+ {
5
+ "name": "vector_positive",
6
+ "inputs": { "x": { "dtype": "float32", "shape": [32], "data": { "kind": "constant", "value": 2.5 } } },
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": [4.0] } } },
12
+ "outputs": { "y": { "dtype": "float32", "shape": [], "tolerance": 0.000001 } }
13
+ },
14
+ {
15
+ "name": "domain_edges_zero_negative_inf",
16
+ "inputs": {
17
+ "x": {
18
+ "dtype": "float32",
19
+ "shape": [6],
20
+ "data": { "kind": "values", "values": [0.0, -1.0, -5.0, "Infinity", "NaN", 1.0] }
21
+ }
22
+ },
23
+ "outputs": { "y": { "dtype": "float32", "shape": [6], "tolerance": 0.000001, "allowNaN": true } }
24
+ },
25
+ {
26
+ "name": "f32_positive_subnormal_inputs_finite_logs_vec4_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.Log",
34
+ "notes": "Positive subnormal inputs are inside Log's domain and should produce finite large negative values; flushing them to zero produces -Infinity."
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": 0.000001 } }
40
+ },
41
+ {
42
+ "name": "f32_positive_subnormal_inputs_finite_logs_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.Log",
50
+ "notes": "Scalar-path companion for positive subnormal Log inputs."
51
+ },
52
+ "inputs": {
53
+ "x": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [1e-45, 1e-40, 1e-38] } }
54
+ },
55
+ "outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0.000001 } }
56
+ },
57
+ {
58
+ "name": "f16_positive_values",
59
+ "inputs": {
60
+ "x": { "dtype": "float16", "shape": [5], "data": { "kind": "values", "values": [0.25, 0.5, 1.0, 2.0, 8.0] } }
61
+ },
62
+ "outputs": { "y": { "dtype": "float16", "shape": [5], "tolerance": 0.002 } }
63
+ },
64
+ {
65
+ "name": "subnormal_positive_f32",
66
+ "inputs": {
67
+ "x": {
68
+ "dtype": "float32",
69
+ "shape": [4],
70
+ "data": { "kind": "values", "values": [1.17549435e-38, 1e-37, 1e-20, 1e-10] }
71
+ }
72
+ },
73
+ "outputs": { "y": { "dtype": "float32", "shape": [4], "tolerance": 0.00001 } }
74
+ },
75
+ {
76
+ "name": "positive_subnormal_values_gpu_gap",
77
+ "skipGpu": {
78
+ "category": "permanent",
79
+ "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."
80
+ },
81
+ "provenance": {
82
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
83
+ "test": "MathOpTest.Log",
84
+ "notes": "Positive subnormal inputs are valid for Log; flushing them to zero turns finite logs into -Infinity."
85
+ },
86
+ "inputs": {
87
+ "x": { "dtype": "float32", "shape": [4], "data": { "kind": "values", "values": [1e-45, 1e-40, 1e-39, 1e-38] } }
88
+ },
89
+ "outputs": { "y": { "dtype": "float32", "shape": [4], "tolerance": 0.00001 } }
90
+ },
91
+ {
92
+ "name": "positive_subnormal_values_scalar_gpu_gap",
93
+ "skipGpu": {
94
+ "category": "permanent",
95
+ "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."
96
+ },
97
+ "provenance": {
98
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
99
+ "test": "MathOpTest.Log",
100
+ "notes": "Scalar-path companion: positive subnormal inputs are valid and should produce finite logs."
101
+ },
102
+ "inputs": {
103
+ "x": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [1e-45, 1e-40, 1e-39] } }
104
+ },
105
+ "outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0.00001 } }
106
+ },
107
+ {
108
+ "name": "ort_float_2x2",
109
+ "provenance": {
110
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
111
+ "test": "MathOpTest.Log"
112
+ },
113
+ "inputs": {
114
+ "x": { "dtype": "float32", "shape": [2, 2], "data": { "kind": "values", "values": [1.0, 2.0, 5.0, 10.0] } }
115
+ },
116
+ "outputs": { "y": { "dtype": "float32", "shape": [2, 2], "tolerance": 0.000001 } }
117
+ },
118
+ {
119
+ "name": "ort_float16_2x2_projection",
120
+ "provenance": {
121
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
122
+ "test": "MathOpTest.Log",
123
+ "notes": "Float16 projection of ORT's float Log node values."
124
+ },
125
+ "inputs": {
126
+ "x": { "dtype": "float16", "shape": [2, 2], "data": { "kind": "values", "values": [1.0, 2.0, 5.0, 10.0] } }
127
+ },
128
+ "outputs": { "y": { "dtype": "float16", "shape": [2, 2], "tolerance": 0.002 } }
129
+ },
130
+ {
131
+ "name": "signed_zero_negative_infinity_domain",
132
+ "provenance": {
133
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
134
+ "test": "MathOpTest.Log",
135
+ "notes": "Additional node-level domain boundary: log(+/-0) is -Infinity, negative finite and -Infinity inputs produce NaN, and log(+Infinity) is +Infinity."
136
+ },
137
+ "inputs": {
138
+ "x": {
139
+ "dtype": "float32",
140
+ "shape": [7],
141
+ "data": { "kind": "values", "values": [0.0, 0.0, -1.0, "-Infinity", 1.0, "Infinity", "NaN"] }
142
+ }
143
+ },
144
+ "outputs": {
145
+ "y": {
146
+ "dtype": "float32",
147
+ "shape": [7],
148
+ "tolerance": 0,
149
+ "allowNaN": true,
150
+ "data": { "kind": "values", "values": ["-Infinity", "-Infinity", "NaN", "NaN", 0.0, "Infinity", "NaN"] }
151
+ }
152
+ }
153
+ },
154
+ {
155
+ "name": "onnx_backend_example",
156
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_log_example" },
157
+ "inputs": { "x": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [1.0, 10.0] } } },
158
+ "outputs": { "y": { "dtype": "float32", "shape": [2], "tolerance": 0.000001 } }
159
+ },
160
+ {
161
+ "name": "onnx_backend_log",
162
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_log" },
163
+ "inputs": {
164
+ "x": {
165
+ "dtype": "float32",
166
+ "shape": [3, 4, 5],
167
+ "data": {
168
+ "kind": "values",
169
+ "values": [5.8360395431518555, 1.4920592308044434, 2.6610958576202393, 9.401724815368652, 6.472471237182617, 0.37633413076400757, 2.5859384536743164, 0.859540581703186, 0.901929497718811, 1.507719874382019, 1.1549344062805176, 4.281371593475342, 2.1404964923858643, 1.1293870210647583, 1.5587172508239746, 1.3960884809494019, 4.4552321434021, 0.8145183324813843, 1.3676141500473022, 0.4256679117679596, 0.07784857600927353, 1.9224849939346313, 2.3736672401428223, 0.47608205676078796, 9.677026748657227, 0.23354846239089966, 1.0468215942382812, 0.8292912244796753, 4.6310296058654785, 4.346446990966797, 1.1675965785980225, 1.4596000909805298, 0.4115660488605499, 0.1379593163728714, 0.706160843372345, 1.1692341566085815, 3.4222238063812256, 3.3280277252197266, 0.6788691878318787, 0.7391142249107361, 0.3504444658756256, 0.24170967936515808, 0.18154163658618927, 7.034140110015869, 0.6007044911384583, 0.6452777981758118, 0.285705029964447, 2.176004409790039, 0.1991100162267685, 0.8083661198616028, 0.4084170162677765, 1.4724129438400269, 0.6000123023986816, 0.3070845305919647, 0.9722112417221069, 1.5346952676773071, 1.0687793493270874, 1.3531996011734009, 0.5302948355674744, 0.6957665085792542]
170
+ }
171
+ }
172
+ },
173
+ "outputs": { "y": { "dtype": "float32", "shape": [3, 4, 5], "tolerance": 0.00001, "allowNaN": true } }
174
+ },
175
+ {
176
+ "name": "onnx_backend_log_example",
177
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_log_example" },
178
+ "inputs": { "x": { "dtype": "float32", "shape": [2], "data": { "kind": "values", "values": [1.0, 10.0] } } },
179
+ "outputs": { "y": { "dtype": "float32", "shape": [2], "tolerance": 0.00001 } }
180
+ },
181
+ {
182
+ "name": "empty_input_zero_dim",
183
+ "inputs": { "x": { "dtype": "float32", "shape": [0], "data": { "kind": "values", "values": [] } } },
184
+ "outputs": { "y": { "dtype": "float32", "shape": [0], "tolerance": 0 } }
185
+ },
186
+ {
187
+ "name": "f16_domain_edges_scalar_path",
188
+ "inputs": {
189
+ "x": {
190
+ "dtype": "float16",
191
+ "shape": [5],
192
+ "data": { "kind": "values", "values": [0.0, -1.0, "-Infinity", "Infinity", 1.0] }
193
+ }
194
+ },
195
+ "outputs": { "y": { "dtype": "float16", "shape": [5], "allowNaN": true } },
196
+ "tolerance": 0.001
197
+ },
198
+ {
199
+ "name": "f16_domain_edges_vec4_path",
200
+ "inputs": {
201
+ "x": { "dtype": "float16", "shape": [4], "data": { "kind": "values", "values": [0.0, -2.0, "Infinity", 1.0] } }
202
+ },
203
+ "outputs": { "y": { "dtype": "float16", "shape": [4], "allowNaN": true } },
204
+ "tolerance": 0.001
205
+ }
206
+ ]
207
+ }
build/webgpu/unary-scalar.wgsl.jinja ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ {% set unaryDomainGuard = device.adapterInfo.architecture == "" or device.adapterInfo.architecture == "apple" %}
24
+ {% macro emit_log_safe(guarded) %}
25
+ fn log_safe(x: f32) -> f32 {
26
+ {%- if guarded %}
27
+ // A no-NaN compilation path can return a finite value for out-of-domain
28
+ // operands. Preserve IEEE domain behavior from the loaded bits: both
29
+ // signed zeros map to -Inf; negative finite values, -Inf, and input NaNs map
30
+ // to a quiet NaN.
31
+ let bits = bitcast<u32>(x);
32
+ let magnitude = bits & 0x7fffffffu;
33
+ if (magnitude == 0u) {
34
+ return negative_infinity();
35
+ }
36
+ let exponent_all = (magnitude & 0x7f800000u) == 0x7f800000u;
37
+ let input_nan = exponent_all && (magnitude & 0x007fffffu) != 0u;
38
+ if ((bits & 0x80000000u) != 0u || input_nan) {
39
+ return bitcast<f32>(bits | 0x7fc00000u);
40
+ }
41
+ {%- endif %}
42
+ return log(x);
43
+ }{% endmacro %}
44
+ {% if unaryDomainGuard %}fn negative_infinity() -> f32 {
45
+ var bits = 0xff800000u;
46
+ return bitcast<f32>(bits);
47
+ }
48
+ {% endif %}
49
+ {{ emit_log_safe(unaryDomainGuard) }}
50
+ {{ flat_tail_open() }}
51
+ y[i] = {{ scalar }}(log_safe(f32(x[i])));
52
+ {{ flat_tail_close() -}}
53
+ }
build/webgpu/unary-vec4.wgsl.jinja ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ {% set unaryDomainGuard = device.adapterInfo.architecture == "" or device.adapterInfo.architecture == "apple" %}
9
+ {% macro emit_log_safe(guarded) %}
10
+ fn log_safe(x: f32) -> f32 {
11
+ {%- if guarded %}
12
+ // A no-NaN compilation path can return a finite value for out-of-domain
13
+ // operands. Preserve IEEE domain behavior from the loaded bits: both
14
+ // signed zeros map to -Inf; negative finite values, -Inf, and input NaNs map
15
+ // to a quiet NaN.
16
+ let bits = bitcast<u32>(x);
17
+ let magnitude = bits & 0x7fffffffu;
18
+ if (magnitude == 0u) {
19
+ return negative_infinity();
20
+ }
21
+ let exponent_all = (magnitude & 0x7f800000u) == 0x7f800000u;
22
+ let input_nan = exponent_all && (magnitude & 0x007fffffu) != 0u;
23
+ if ((bits & 0x80000000u) != 0u || input_nan) {
24
+ return bitcast<f32>(bits | 0x7fc00000u);
25
+ }
26
+ {%- endif %}
27
+ return log(x);
28
+ }{% endmacro %}
29
+ {% if unaryDomainGuard %}fn negative_infinity() -> f32 {
30
+ var bits = 0xff800000u;
31
+ return bitcast<f32>(bits);
32
+ }
33
+ {% endif %}
34
+ {{ emit_log_safe(unaryDomainGuard) }}
35
+
36
+ @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
37
+ fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {
38
+ // 2D-folded flat index: gid.y carries the high bits when the element count exceeds the
39
+ // maxComputeWorkgroupsPerDimension limit (the dispatch caps x and spills the rest into y).
40
+ let i = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;
41
+ if (i >= params.count) {
42
+ return;
43
+ }
44
+ let xv = x[i];
45
+ let fv = vec4<f32>(xv);
46
+ y[i] = {{ vectorScalar }}(vec4<f32>(
47
+ log_safe(fv.x), log_safe(fv.y), log_safe(fv.z), log_safe(fv.w)));
48
+ }