Xenova HF Staff commited on
Commit
a764dd9
·
verified ·
1 Parent(s): 20f512c

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.Floor
10
+
11
+ `ai.onnx` · standard ONNX operator · ONNX opset ≥ 13
12
+
13
+ ## Description
14
+
15
+ Applies `floor(x)` elementwise to a floating-point tensor, rounding each value down to the nearest integer. Integral values, ±0, NaN, and infinities are returned unchanged.
16
+
17
+ See the [ONNX `Floor` spec](https://onnx.ai/onnx/operators/onnx__Floor.html) for the reference semantics.
18
+
19
+ ## Inputs
20
+
21
+ | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
22
+ | --- | --- | --- | --- | --- | --- | --- |
23
+ | `X` | `x` | `T` | — | — | Floating-point values to round elementwise toward negative infinity. | 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 floored. | 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.Floor", { version: 1 });
59
+ const { y } = await kernel({ x: { data: xData, shape: [] } });
60
+ ```
build/webgpu/bench.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "op": "ai.onnx.Floor",
3
+ "cases": [
4
+ {
5
+ "name": "1m_f32",
6
+ "preset": "smoke",
7
+ "inputs": { "x": { "dtype": "float32", "shape": [1048576] } },
8
+ "outputs": { "y": { "dtype": "float32", "shape": [1048576] } },
9
+ "bench": { "metrics": [{ "type": "bandwidth", "value": "(numel(shapes.x) + numel(shapes.y)) * 4" }] }
10
+ },
11
+ {
12
+ "name": "floor-f32-1m",
13
+ "preset": "edge",
14
+ "vars": { "dtype": "float32", "count": 1048575 },
15
+ "inputs": {
16
+ "x": { "shape": [1048575], "dtype": "float32", "dist": "uniform", "seed": 5201, "scale": 4096, "offset": -2048 }
17
+ },
18
+ "outputs": { "y": { "shape": [1048575], "dtype": "float32" } },
19
+ "bench": {
20
+ "primary": true,
21
+ "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }]
22
+ }
23
+ }
24
+ ]
25
+ }
build/webgpu/manifest.json ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "domain": "ai.onnx",
3
+ "name": "Floor",
4
+ "sinceVersion": 13,
5
+ "description": "Applies `floor(x)` elementwise to a floating-point tensor, rounding each value down to the nearest integer. Integral values, ±0, NaN, and infinities are returned unchanged.",
6
+ "inputs": [
7
+ { "role": "X", "dtype": "T", "description": "Floating-point values to round elementwise toward negative infinity." }
8
+ ],
9
+ "outputs": [
10
+ {
11
+ "role": "Y",
12
+ "dtype": "T",
13
+ "rank": "ranks.X",
14
+ "description": "Output tensor with each element floored.",
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": "Floor.vec4",
37
+ "source": { "shader": "unary-vec4.wgsl.jinja", "inputs": { "op": "\"floor\"" } },
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": "elementwise",
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": "Floor",
64
+ "source": { "shader": "unary-scalar.wgsl.jinja", "inputs": { "op": "\"floor\"", "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.Floor",
3
+ "id": "_ai_onnx_floor_webgpu_5559c7d",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "backend": { "type": "webgpu" },
7
+ "digest": {
8
+ "algorithm": "sha256",
9
+ "files": {
10
+ "bench.json": "u3q/2NJLInnGS2x7xOIXR6JG/htYvBe8EsnyHr3va30=",
11
+ "manifest.json": "HXDn5fMrDUtCxOGwRmHRKRiLnCcUHv0Y84m8eT+PGW0=",
12
+ "test.json": "e2EcsnnmGNk9FYgGwYpFKQPpJysnXTzZw5sWJp6v2eE=",
13
+ "unary-scalar.wgsl.jinja": "FVxPc7LDZo8Ar/ld3AL66EhVBYk2fcfxnUlEt6wvVrE=",
14
+ "unary-vec4.wgsl.jinja": "ACvVE8Y+AUwmOcbVE0Qke6o0xPNsDpu5u57RAGgDQzg="
15
+ }
16
+ },
17
+ "provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } },
18
+ "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/ai.onnx.Floor" }
19
+ }
build/webgpu/test.json ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "op": "ai.onnx.Floor",
3
+ "cases": [
4
+ {
5
+ "name": "f32_values",
6
+ "inputs": {
7
+ "x": {
8
+ "dtype": "float32",
9
+ "shape": [6],
10
+ "data": { "kind": "values", "values": [-2.7, -1.2, -0.1, 0.0, 1.2, 2.9] }
11
+ }
12
+ },
13
+ "outputs": { "y": { "dtype": "float32", "shape": [6] } }
14
+ },
15
+ {
16
+ "name": "rank0_positive_fraction",
17
+ "inputs": { "x": { "dtype": "float32", "shape": [], "data": { "kind": "values", "values": [1.75] } } },
18
+ "outputs": { "y": { "dtype": "float32", "shape": [] } }
19
+ },
20
+ {
21
+ "name": "nonfinite_and_integer_values",
22
+ "inputs": {
23
+ "x": {
24
+ "dtype": "float32",
25
+ "shape": [6],
26
+ "data": { "kind": "values", "values": ["-Infinity", -2.0, 0.0, 0.9999999, "Infinity", "NaN"] }
27
+ }
28
+ },
29
+ "outputs": { "y": { "dtype": "float32", "shape": [6], "tolerance": 0, "allowNaN": true } }
30
+ },
31
+ {
32
+ "name": "f32_subnormal_boundary_vec4_gpu_gap",
33
+ "skipGpu": {
34
+ "category": "permanent",
35
+ "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."
36
+ },
37
+ "provenance": {
38
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
39
+ "test": "MathOpTest.Floor",
40
+ "notes": "Negative subnormal inputs are strictly less than zero, so Floor must round them down to negative one; flushing them to zero changes the boundary result."
41
+ },
42
+ "inputs": {
43
+ "x": {
44
+ "dtype": "float32",
45
+ "shape": [4],
46
+ "data": { "kind": "values", "values": [-1e-40, 0.0, 1e-40, -1.0000001] }
47
+ }
48
+ },
49
+ "outputs": { "y": { "dtype": "float32", "shape": [4], "tolerance": 0 } }
50
+ },
51
+ {
52
+ "name": "f32_subnormal_boundary_scalar_gpu_gap",
53
+ "skipGpu": {
54
+ "category": "permanent",
55
+ "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."
56
+ },
57
+ "provenance": {
58
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
59
+ "test": "MathOpTest.Floor",
60
+ "notes": "Scalar-path companion for negative subnormal Floor boundary behavior."
61
+ },
62
+ "inputs": {
63
+ "x": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [-1e-40, 0.0, 1e-40] } }
64
+ },
65
+ "outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0 } }
66
+ },
67
+ {
68
+ "name": "zero_size_noop",
69
+ "inputs": { "x": { "dtype": "float32", "shape": [0, 2], "data": { "kind": "values", "values": [] } } },
70
+ "outputs": { "y": { "dtype": "float32", "shape": [0, 2], "tolerance": 0 } }
71
+ },
72
+ {
73
+ "name": "ort_dim_zero_unary_noop",
74
+ "provenance": {
75
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
76
+ "test": "MathOpTest.DimWithZeroHandling",
77
+ "notes": "ORT unary zero-dimension Floor case."
78
+ },
79
+ "inputs": { "x": { "dtype": "float32", "shape": [0, 3], "data": { "kind": "values", "values": [] } } },
80
+ "outputs": { "y": { "dtype": "float32", "shape": [0, 3], "tolerance": 0 } }
81
+ },
82
+ {
83
+ "name": "f16_fractional_nonfinite_values",
84
+ "inputs": {
85
+ "x": {
86
+ "dtype": "float16",
87
+ "shape": [8],
88
+ "data": { "kind": "values", "values": ["-Infinity", -1.5, -0.5, -0.0001, 0.0001, 1.5, "Infinity", "NaN"] }
89
+ }
90
+ },
91
+ "outputs": { "y": { "dtype": "float16", "shape": [8], "tolerance": 0, "allowNaN": true } }
92
+ },
93
+ {
94
+ "name": "ort_float_2x2",
95
+ "provenance": {
96
+ "source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
97
+ "test": "MathOpTest.Floor"
98
+ },
99
+ "inputs": {
100
+ "x": { "dtype": "float32", "shape": [2, 2], "data": { "kind": "values", "values": [-1.5, 0.2, -0.5, 10.3] } }
101
+ },
102
+ "outputs": {
103
+ "y": {
104
+ "dtype": "float32",
105
+ "shape": [2, 2],
106
+ "tolerance": 0,
107
+ "data": { "kind": "values", "values": [-2.0, 0.0, -1.0, 10.0] }
108
+ }
109
+ }
110
+ },
111
+ {
112
+ "name": "onnx_backend_example",
113
+ "provenance": {
114
+ "source": "cmake/external/onnx/onnx/backend/test/data/node/test_floor_example",
115
+ "test": "test_floor_example"
116
+ },
117
+ "inputs": {
118
+ "x": {
119
+ "dtype": "float32",
120
+ "shape": [3],
121
+ "data": { "kind": "values", "values": [-1.5, 1.2000000476837158, 2.0] }
122
+ }
123
+ },
124
+ "outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0 } }
125
+ },
126
+ {
127
+ "name": "onnx_backend_floor",
128
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_floor" },
129
+ "inputs": {
130
+ "x": {
131
+ "dtype": "float32",
132
+ "shape": [3, 4, 5],
133
+ "data": {
134
+ "kind": "values",
135
+ "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]
136
+ }
137
+ }
138
+ },
139
+ "outputs": { "y": { "dtype": "float32", "shape": [3, 4, 5], "tolerance": 0 } }
140
+ },
141
+ {
142
+ "name": "onnx_backend_floor_example",
143
+ "provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_floor_example" },
144
+ "inputs": {
145
+ "x": {
146
+ "dtype": "float32",
147
+ "shape": [3],
148
+ "data": { "kind": "values", "values": [-1.5, 1.2000000476837158, 2.0] }
149
+ }
150
+ },
151
+ "outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0.00001 } }
152
+ },
153
+ {
154
+ "name": "f32_large_half_integer_precision_boundary",
155
+ "inputs": {
156
+ "x": {
157
+ "dtype": "float32",
158
+ "shape": [6],
159
+ "data": { "kind": "values", "values": [8388607.5, 8388608.0, -8388607.5, -8388608.0, 8388609.0, -8388609.0] }
160
+ }
161
+ },
162
+ "outputs": {
163
+ "y": {
164
+ "dtype": "float32",
165
+ "shape": [6],
166
+ "tolerance": 0,
167
+ "data": { "kind": "values", "values": [8388607.0, 8388608.0, -8388608.0, -8388608.0, 8388610.0, -8388610.0] }
168
+ }
169
+ }
170
+ },
171
+ {
172
+ "name": "f16_scalar_path_fractional",
173
+ "inputs": {
174
+ "x": { "dtype": "float16", "shape": [5], "data": { "kind": "values", "values": [-1.5, -0.5, 0.0, 0.5, 1.5] } }
175
+ },
176
+ "outputs": { "y": { "dtype": "float16", "shape": [5], "tolerance": 0 } }
177
+ }
178
+ ]
179
+ }
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 }}(floor(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 }}(floor(vec4<f32>(xv)));
19
+ }