sync 2e7068faf55e
Browse files- README.md +57 -0
- build/webgpu/bench.json +98 -0
- build/webgpu/manifest.json +78 -0
- build/webgpu/metadata.json +19 -0
- build/webgpu/test.json +257 -0
- build/webgpu/unary-scalar.wgsl.jinja +226 -0
- build/webgpu/unary-vec4.wgsl.jinja +219 -0
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.Sin
|
| 10 |
+
|
| 11 |
+
`ai.onnx` · standard ONNX operator · ONNX opset ≥ 7
|
| 12 |
+
|
| 13 |
+
## Description
|
| 14 |
+
|
| 15 |
+
Computes the sine of each element of the input tensor, elementwise. The output has the same shape and type as the input.
|
| 16 |
+
|
| 17 |
+
See the [ONNX `Sin` spec](https://onnx.ai/onnx/operators/onnx__Sin.html) for the reference semantics.
|
| 18 |
+
|
| 19 |
+
## Inputs
|
| 20 |
+
|
| 21 |
+
| Name | Bind key | Logical dtype | Rank | Shape | Description | Presence |
|
| 22 |
+
| --- | --- | --- | --- | --- | --- | --- |
|
| 23 |
+
| `input` | `x` | `T` | — | — | Angles in radians whose sine is 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 sine 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.Sin", { version: 1 });
|
| 59 |
+
const { y } = await kernel({ x: { data: xData, shape: [3] } });
|
| 60 |
+
```
|
build/webgpu/bench.json
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"op": "ai.onnx.Sin",
|
| 3 |
+
"cases": [
|
| 4 |
+
{
|
| 5 |
+
"name": "1m_f32",
|
| 6 |
+
"inputs": { "x": { "dtype": "float32", "shape": [1048576] } },
|
| 7 |
+
"outputs": { "y": { "dtype": "float32", "shape": [1048576] } }
|
| 8 |
+
},
|
| 9 |
+
{
|
| 10 |
+
"name": "sin-f32-1m-vec4",
|
| 11 |
+
"preset": "smoke",
|
| 12 |
+
"vars": { "dtype": "float32", "count": 1048576 },
|
| 13 |
+
"inputs": {
|
| 14 |
+
"x": {
|
| 15 |
+
"shape": [1048576],
|
| 16 |
+
"dtype": "float32",
|
| 17 |
+
"dist": "uniform",
|
| 18 |
+
"seed": 631,
|
| 19 |
+
"scale": 6.283185307179586,
|
| 20 |
+
"offset": -3.141592653589793
|
| 21 |
+
}
|
| 22 |
+
},
|
| 23 |
+
"outputs": { "y": { "shape": [1048576], "dtype": "float32" } },
|
| 24 |
+
"bench": {
|
| 25 |
+
"primary": true,
|
| 26 |
+
"metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }]
|
| 27 |
+
}
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"name": "sin-f32-1m-plus-1-scalar-fallback",
|
| 31 |
+
"preset": "smoke",
|
| 32 |
+
"vars": { "dtype": "float32", "count": 1048577 },
|
| 33 |
+
"inputs": {
|
| 34 |
+
"x": {
|
| 35 |
+
"shape": [1048577],
|
| 36 |
+
"dtype": "float32",
|
| 37 |
+
"dist": "uniform",
|
| 38 |
+
"seed": 632,
|
| 39 |
+
"scale": 6.283185307179586,
|
| 40 |
+
"offset": -3.141592653589793
|
| 41 |
+
}
|
| 42 |
+
},
|
| 43 |
+
"outputs": { "y": { "shape": [1048577], "dtype": "float32" } },
|
| 44 |
+
"bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"name": "sin-f16-1m-vec4",
|
| 48 |
+
"preset": "smoke",
|
| 49 |
+
"vars": { "dtype": "float16", "count": 1048576 },
|
| 50 |
+
"inputs": {
|
| 51 |
+
"x": {
|
| 52 |
+
"shape": [1048576],
|
| 53 |
+
"dtype": "float16",
|
| 54 |
+
"dist": "uniform",
|
| 55 |
+
"seed": 633,
|
| 56 |
+
"scale": 6.283185307179586,
|
| 57 |
+
"offset": -3.141592653589793
|
| 58 |
+
}
|
| 59 |
+
},
|
| 60 |
+
"outputs": { "y": { "shape": [1048576], "dtype": "float16" } },
|
| 61 |
+
"bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"name": "sin-f16-1m-plus-1-scalar-fallback",
|
| 65 |
+
"preset": "smoke",
|
| 66 |
+
"vars": { "dtype": "float16", "count": 1048577 },
|
| 67 |
+
"inputs": {
|
| 68 |
+
"x": {
|
| 69 |
+
"shape": [1048577],
|
| 70 |
+
"dtype": "float16",
|
| 71 |
+
"dist": "uniform",
|
| 72 |
+
"seed": 634,
|
| 73 |
+
"scale": 6.283185307179586,
|
| 74 |
+
"offset": -3.141592653589793
|
| 75 |
+
}
|
| 76 |
+
},
|
| 77 |
+
"outputs": { "y": { "shape": [1048577], "dtype": "float16" } },
|
| 78 |
+
"bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
"name": "sin-f32-8m-vec4-sustained",
|
| 82 |
+
"preset": "smoke",
|
| 83 |
+
"vars": { "dtype": "float32", "count": 8388608 },
|
| 84 |
+
"inputs": {
|
| 85 |
+
"x": {
|
| 86 |
+
"shape": [8388608],
|
| 87 |
+
"dtype": "float32",
|
| 88 |
+
"dist": "uniform",
|
| 89 |
+
"seed": 635,
|
| 90 |
+
"scale": 6.283185307179586,
|
| 91 |
+
"offset": -3.141592653589793
|
| 92 |
+
}
|
| 93 |
+
},
|
| 94 |
+
"outputs": { "y": { "shape": [8388608], "dtype": "float32" } },
|
| 95 |
+
"bench": { "metrics": [{ "type": "bandwidth", "value": "args.count * dtypeBytes(args.dtype) * 2" }] }
|
| 96 |
+
}
|
| 97 |
+
]
|
| 98 |
+
}
|
build/webgpu/manifest.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"domain": "ai.onnx",
|
| 3 |
+
"name": "Sin",
|
| 4 |
+
"sinceVersion": 7,
|
| 5 |
+
"description": "Computes the sine of each element of the input tensor, elementwise. The output has the same shape and type as the input.",
|
| 6 |
+
"inputs": [{ "role": "input", "dtype": "T", "description": "Angles in radians whose sine is computed elementwise." }],
|
| 7 |
+
"outputs": [
|
| 8 |
+
{
|
| 9 |
+
"role": "output",
|
| 10 |
+
"dtype": "T",
|
| 11 |
+
"rank": "ranks.input",
|
| 12 |
+
"description": "The sine of the input tensor computed elementwise.",
|
| 13 |
+
"shape": "shapes.input"
|
| 14 |
+
}
|
| 15 |
+
],
|
| 16 |
+
"typeConstraints": { "T": ["float32", "float16"] },
|
| 17 |
+
"args": {
|
| 18 |
+
"x": { "kind": "tensor", "semantic": "input", "role": "input" },
|
| 19 |
+
"y": { "kind": "tensor", "semantic": "output", "role": "output" }
|
| 20 |
+
},
|
| 21 |
+
"tunables": { "WORKGROUP_SIZE": 256 },
|
| 22 |
+
"variants": [
|
| 23 |
+
{
|
| 24 |
+
"id": "same_layout_vec4",
|
| 25 |
+
"when": ["numel(shapes.x) > 0", "numel(shapes.x) % 4 == 0", "numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
|
| 26 |
+
"constants": {
|
| 27 |
+
"scalar": "dtypes.T",
|
| 28 |
+
"usesF16": "dtypes.T == \"f16\"",
|
| 29 |
+
"vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\""
|
| 30 |
+
},
|
| 31 |
+
"passes": [
|
| 32 |
+
{
|
| 33 |
+
"id": "main",
|
| 34 |
+
"name": "Sin.vec4",
|
| 35 |
+
"source": { "shader": "unary-vec4.wgsl.jinja", "inputs": { "op": "\"sin\"" } },
|
| 36 |
+
"bindings": [
|
| 37 |
+
{ "name": "x", "arg": "x", "buffer": { "type": "read-only-storage" }, "elementType": "$vectorScalar" },
|
| 38 |
+
{ "name": "y", "arg": "y", "buffer": { "type": "storage" }, "elementType": "$vectorScalar" },
|
| 39 |
+
{
|
| 40 |
+
"name": "params",
|
| 41 |
+
"semantic": "kernel.params",
|
| 42 |
+
"buffer": { "type": "uniform" },
|
| 43 |
+
"struct": {
|
| 44 |
+
"name": "Params",
|
| 45 |
+
"fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.y) / 4" }]
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
],
|
| 49 |
+
"dispatch": { "threads": "numel(shapes.y) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" }
|
| 50 |
+
}
|
| 51 |
+
],
|
| 52 |
+
"priority": 20
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"id": "elementwise",
|
| 56 |
+
"when": ["numel(shapes.x) == numel(shapes.y)", "f16Ok(dtypes.T)"],
|
| 57 |
+
"constants": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"" },
|
| 58 |
+
"passes": [
|
| 59 |
+
{
|
| 60 |
+
"id": "main",
|
| 61 |
+
"name": "Sin",
|
| 62 |
+
"source": { "shader": "unary-scalar.wgsl.jinja", "inputs": { "op": "\"sin\"", "itemsPerInvocation": 4 } },
|
| 63 |
+
"bindings": [
|
| 64 |
+
{ "name": "x", "arg": "x", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" },
|
| 65 |
+
{ "name": "y", "arg": "y", "buffer": { "type": "storage" }, "elementType": "$scalar" },
|
| 66 |
+
{
|
| 67 |
+
"name": "params",
|
| 68 |
+
"semantic": "kernel.params",
|
| 69 |
+
"buffer": { "type": "uniform" },
|
| 70 |
+
"struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.y)" }] }
|
| 71 |
+
}
|
| 72 |
+
],
|
| 73 |
+
"dispatch": { "threads": "ceilDiv(numel(shapes.y), 4)", "workgroupSize": "tunables.WORKGROUP_SIZE" }
|
| 74 |
+
}
|
| 75 |
+
]
|
| 76 |
+
}
|
| 77 |
+
]
|
| 78 |
+
}
|
build/webgpu/metadata.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "ai.onnx.Sin",
|
| 3 |
+
"id": "_ai_onnx_sin_webgpu_c7e0592",
|
| 4 |
+
"version": 1,
|
| 5 |
+
"license": "Apache-2.0",
|
| 6 |
+
"backend": { "type": "webgpu" },
|
| 7 |
+
"digest": {
|
| 8 |
+
"algorithm": "sha256",
|
| 9 |
+
"files": {
|
| 10 |
+
"bench.json": "UOV9gwFw9jnme1cEK/nz1bt9ipju32MueHfRdIRt6es=",
|
| 11 |
+
"manifest.json": "lvniX/6EzV7L5v6AW9Hj79TOZHWdAqO28c95M26jdTE=",
|
| 12 |
+
"test.json": "YWbR8aUbOj0MA7AZt4qhnRqG8tegECpDx1fA4HA2ae0=",
|
| 13 |
+
"unary-scalar.wgsl.jinja": "7jBrpk29+fb0h+uKME4UDlhIALpJxVg/4GCuxsQEEyE=",
|
| 14 |
+
"unary-vec4.wgsl.jinja": "rNqiHPIA51+d8IiR66QdEzBZZvNtoBvCKZoQeyNCLX8="
|
| 15 |
+
}
|
| 16 |
+
},
|
| 17 |
+
"provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } },
|
| 18 |
+
"webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/ai.onnx.Sin" }
|
| 19 |
+
}
|
build/webgpu/test.json
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"op": "ai.onnx.Sin",
|
| 3 |
+
"cases": [
|
| 4 |
+
{
|
| 5 |
+
"name": "f32_values",
|
| 6 |
+
"inputs": {
|
| 7 |
+
"x": {
|
| 8 |
+
"dtype": "float32",
|
| 9 |
+
"shape": [6],
|
| 10 |
+
"data": { "kind": "values", "values": [-3.0, -1.0, 0.0, 0.5, 1.0, 3.0] }
|
| 11 |
+
}
|
| 12 |
+
},
|
| 13 |
+
"outputs": { "y": { "dtype": "float32", "shape": [6], "tolerance": 0.000001 } }
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"name": "f32_subnormal_identity_tail_gpu_gap",
|
| 17 |
+
"skipGpu": {
|
| 18 |
+
"category": "permanent",
|
| 19 |
+
"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."
|
| 20 |
+
},
|
| 21 |
+
"provenance": {
|
| 22 |
+
"source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
|
| 23 |
+
"test": "MathOpTest.SinFloat",
|
| 24 |
+
"notes": "For tiny finite inputs sin(x) rounds back to x in float32; zero-flushing erases the signed tail."
|
| 25 |
+
},
|
| 26 |
+
"inputs": {
|
| 27 |
+
"x": {
|
| 28 |
+
"dtype": "float32",
|
| 29 |
+
"shape": [4],
|
| 30 |
+
"data": { "kind": "values", "values": [-1e-39, -1e-40, 1e-40, 1e-39] }
|
| 31 |
+
}
|
| 32 |
+
},
|
| 33 |
+
"outputs": { "y": { "dtype": "float32", "shape": [4], "tolerance": 0 } }
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"name": "f32_subnormal_identity_tail_scalar_gpu_gap",
|
| 37 |
+
"skipGpu": {
|
| 38 |
+
"category": "permanent",
|
| 39 |
+
"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."
|
| 40 |
+
},
|
| 41 |
+
"provenance": {
|
| 42 |
+
"source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
|
| 43 |
+
"test": "MathOpTest.SinFloat",
|
| 44 |
+
"notes": "Scalar-path companion: subnormal inputs are valid finite Sin outputs."
|
| 45 |
+
},
|
| 46 |
+
"inputs": {
|
| 47 |
+
"x": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [-1e-40, 0.0, 1e-40] } }
|
| 48 |
+
},
|
| 49 |
+
"outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0 } }
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"name": "f16_values",
|
| 53 |
+
"inputs": {
|
| 54 |
+
"x": {
|
| 55 |
+
"dtype": "float16",
|
| 56 |
+
"shape": [2, 3],
|
| 57 |
+
"data": { "kind": "values", "values": [-2.0, -0.5, 0.0, 0.5, 1.0, 2.0] }
|
| 58 |
+
}
|
| 59 |
+
},
|
| 60 |
+
"outputs": { "y": { "dtype": "float16", "shape": [2, 3] } },
|
| 61 |
+
"tolerance": 0.001
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"name": "large_argument_range_reduction",
|
| 65 |
+
"inputs": {
|
| 66 |
+
"x": {
|
| 67 |
+
"dtype": "float32",
|
| 68 |
+
"shape": [8],
|
| 69 |
+
"data": {
|
| 70 |
+
"kind": "values",
|
| 71 |
+
"values": [1000000.0, 10000000.0, 10000000000000.0, 100000000000000000000.0, -1000000000000000.0, -123456.78, 314159.265, 2500000000.0]
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
},
|
| 75 |
+
"outputs": { "y": { "dtype": "float32", "shape": [8], "tolerance": 0.0001 } }
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
"name": "ort_float_opset22",
|
| 79 |
+
"provenance": {
|
| 80 |
+
"source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
|
| 81 |
+
"test": "MathOpTest.Sin_Opset22"
|
| 82 |
+
},
|
| 83 |
+
"inputs": {
|
| 84 |
+
"x": { "dtype": "float32", "shape": [4], "data": { "kind": "values", "values": [1.1, -1.1, 2.2, -2.2] } }
|
| 85 |
+
},
|
| 86 |
+
"outputs": { "y": { "dtype": "float32", "shape": [4], "tolerance": 0.000001 } }
|
| 87 |
+
},
|
| 88 |
+
{
|
| 89 |
+
"name": "ort_nonfinite_values",
|
| 90 |
+
"provenance": {
|
| 91 |
+
"source": "onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc",
|
| 92 |
+
"test": "MathOpTest.Sin_Opset22",
|
| 93 |
+
"notes": "Extends ORT's Sin opset-22 case with signed infinities, signed zero, and NaN."
|
| 94 |
+
},
|
| 95 |
+
"inputs": {
|
| 96 |
+
"x": {
|
| 97 |
+
"dtype": "float32",
|
| 98 |
+
"shape": [7],
|
| 99 |
+
"data": { "kind": "values", "values": ["-Infinity", -1.0, 0.0, 0.0, 1.0, "Infinity", "NaN"] }
|
| 100 |
+
}
|
| 101 |
+
},
|
| 102 |
+
"outputs": { "y": { "dtype": "float32", "shape": [7], "tolerance": 0.000001, "allowNaN": true } }
|
| 103 |
+
},
|
| 104 |
+
{
|
| 105 |
+
"name": "onnx_backend_example",
|
| 106 |
+
"provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_sin_example" },
|
| 107 |
+
"inputs": { "x": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [-1.0, 0.0, 1.0] } } },
|
| 108 |
+
"outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0.000001 } }
|
| 109 |
+
},
|
| 110 |
+
{
|
| 111 |
+
"name": "onnx_backend_sin",
|
| 112 |
+
"provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_sin" },
|
| 113 |
+
"inputs": {
|
| 114 |
+
"x": {
|
| 115 |
+
"dtype": "float32",
|
| 116 |
+
"shape": [3, 4, 5],
|
| 117 |
+
"data": {
|
| 118 |
+
"kind": "values",
|
| 119 |
+
"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]
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
},
|
| 123 |
+
"outputs": { "y": { "dtype": "float32", "shape": [3, 4, 5], "tolerance": 0.00001 } }
|
| 124 |
+
},
|
| 125 |
+
{
|
| 126 |
+
"name": "onnx_backend_sin_example",
|
| 127 |
+
"provenance": { "source": "cmake/external/onnx/onnx/backend/test/data/node/test_sin_example" },
|
| 128 |
+
"inputs": { "x": { "dtype": "float32", "shape": [3], "data": { "kind": "values", "values": [-1.0, 0.0, 1.0] } } },
|
| 129 |
+
"outputs": { "y": { "dtype": "float32", "shape": [3], "tolerance": 0.00001 } }
|
| 130 |
+
},
|
| 131 |
+
{
|
| 132 |
+
"name": "vec4_f16_lanes",
|
| 133 |
+
"inputs": {
|
| 134 |
+
"x": {
|
| 135 |
+
"dtype": "float16",
|
| 136 |
+
"shape": [16],
|
| 137 |
+
"data": {
|
| 138 |
+
"kind": "values",
|
| 139 |
+
"values": [-100.0, -50.0, -20.0, -10.0, -6.0, -3.0, -1.5, -0.5, 0.0, 0.5, 1.5, 3.0, 6.0, 10.0, 50.0, 100.0]
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
},
|
| 143 |
+
"outputs": { "y": { "dtype": "float16", "shape": [16], "tolerance": 0.001, "relTolerance": 0.002 } }
|
| 144 |
+
},
|
| 145 |
+
{
|
| 146 |
+
"name": "vec4_f32_nonfinite",
|
| 147 |
+
"inputs": {
|
| 148 |
+
"x": {
|
| 149 |
+
"dtype": "float32",
|
| 150 |
+
"shape": [8],
|
| 151 |
+
"data": { "kind": "values", "values": ["-Infinity", -1.0, 0.0, 0.5, 1.0, "Infinity", "NaN", 2.0] }
|
| 152 |
+
}
|
| 153 |
+
},
|
| 154 |
+
"outputs": { "y": { "dtype": "float32", "shape": [8], "tolerance": 0.000001, "allowNaN": true } }
|
| 155 |
+
},
|
| 156 |
+
{
|
| 157 |
+
"name": "empty_input_zero_dim",
|
| 158 |
+
"inputs": { "x": { "dtype": "float32", "shape": [0], "data": { "kind": "values", "values": [] } } },
|
| 159 |
+
"outputs": { "y": { "dtype": "float32", "shape": [0], "tolerance": 0 } }
|
| 160 |
+
},
|
| 161 |
+
{
|
| 162 |
+
"name": "f16_finite_scalar_fallback_non_mult4",
|
| 163 |
+
"inputs": {
|
| 164 |
+
"x": {
|
| 165 |
+
"dtype": "float16",
|
| 166 |
+
"shape": [10],
|
| 167 |
+
"data": { "kind": "values", "values": [-3.0, -2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 3.0] }
|
| 168 |
+
}
|
| 169 |
+
},
|
| 170 |
+
"outputs": { "y": { "dtype": "float16", "shape": [10], "tolerance": 0.002, "relTolerance": 0.002 } }
|
| 171 |
+
},
|
| 172 |
+
{
|
| 173 |
+
"name": "f16_finite_vec4_realistic_range",
|
| 174 |
+
"inputs": {
|
| 175 |
+
"x": {
|
| 176 |
+
"dtype": "float16",
|
| 177 |
+
"shape": [4, 8],
|
| 178 |
+
"data": {
|
| 179 |
+
"kind": "values",
|
| 180 |
+
"values": [-3.140625, -2.75, -2.359375, -1.96875, -1.5703125, -1.179688, -0.785156, -0.392578, 0.0, 0.392578, 0.785156, 1.179688, 1.5703125, 1.96875, 2.359375, 2.75, 3.140625, -3.0, -2.5, -2.0, -1.5, -1.0, -0.5, 0.25, 0.75, 1.25, 1.75, 2.25, 2.5, 2.75, 3.0, 3.125]
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
+
},
|
| 184 |
+
"outputs": { "y": { "dtype": "float16", "shape": [4, 8], "tolerance": 0.002, "relTolerance": 0.002 } }
|
| 185 |
+
},
|
| 186 |
+
{
|
| 187 |
+
"name": "f32_finite_scalar_fallback_non_mult4",
|
| 188 |
+
"inputs": {
|
| 189 |
+
"x": {
|
| 190 |
+
"dtype": "float32",
|
| 191 |
+
"shape": [7],
|
| 192 |
+
"data": {
|
| 193 |
+
"kind": "values",
|
| 194 |
+
"values": [-3.141592653589793, -1.5707963267948966, -0.7853981633974483, 0.0, 0.7853981633974483, 1.5707963267948966, 3.141592653589793]
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
},
|
| 198 |
+
"outputs": { "y": { "dtype": "float32", "shape": [7], "tolerance": 0.000001 } }
|
| 199 |
+
},
|
| 200 |
+
{
|
| 201 |
+
"name": "f16_large_argument_range_reduction",
|
| 202 |
+
"inputs": {
|
| 203 |
+
"x": {
|
| 204 |
+
"dtype": "float16",
|
| 205 |
+
"shape": [8],
|
| 206 |
+
"data": {
|
| 207 |
+
"kind": "values",
|
| 208 |
+
"values": [10000.0, 20000.0, 30000.0, 40000.0, -10000.0, -20000.0, 50000.0, 65504.0]
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
},
|
| 212 |
+
"outputs": { "y": { "dtype": "float16", "shape": [8], "tolerance": 0.01, "relTolerance": 0.01 } }
|
| 213 |
+
},
|
| 214 |
+
{
|
| 215 |
+
"name": "f32_reduce_threshold_boundary",
|
| 216 |
+
"inputs": {
|
| 217 |
+
"x": {
|
| 218 |
+
"dtype": "float32",
|
| 219 |
+
"shape": [8],
|
| 220 |
+
"data": {
|
| 221 |
+
"kind": "values",
|
| 222 |
+
"values": [9999.0, 9999.5, 10000.0, 10000.5, 10001.0, -9999.0, -10000.0, -10001.0]
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
},
|
| 226 |
+
"outputs": { "y": { "dtype": "float32", "shape": [8], "tolerance": 0.001 } }
|
| 227 |
+
},
|
| 228 |
+
{
|
| 229 |
+
"name": "f32_vec4_sustained_4096",
|
| 230 |
+
"provenance": {
|
| 231 |
+
"notes": "Compact sibling for sustained f32 Sin vec4 benchmarks; keeps generated angles in a modest range while exercising packed throughput."
|
| 232 |
+
},
|
| 233 |
+
"inputs": {
|
| 234 |
+
"x": {
|
| 235 |
+
"dtype": "float32",
|
| 236 |
+
"shape": [4096],
|
| 237 |
+
"data": { "kind": "fillFloat32", "sinStep": 0.017, "cosStep": 0.011, "scale": 1.5 }
|
| 238 |
+
}
|
| 239 |
+
},
|
| 240 |
+
"outputs": { "y": { "dtype": "float32", "shape": [4096], "tolerance": 0.000001 } }
|
| 241 |
+
},
|
| 242 |
+
{
|
| 243 |
+
"name": "f32_scalar_tail_4097",
|
| 244 |
+
"provenance": {
|
| 245 |
+
"notes": "Compact sibling for the f32 Sin scalar-tail benchmark; odd numel forces the elementwise fallback path."
|
| 246 |
+
},
|
| 247 |
+
"inputs": {
|
| 248 |
+
"x": {
|
| 249 |
+
"dtype": "float32",
|
| 250 |
+
"shape": [4097],
|
| 251 |
+
"data": { "kind": "fillFloat32", "sinStep": 0.019, "cosStep": 0.007, "scale": 1.5 }
|
| 252 |
+
}
|
| 253 |
+
},
|
| 254 |
+
"outputs": { "y": { "dtype": "float32", "shape": [4097], "tolerance": 0.000001 } }
|
| 255 |
+
}
|
| 256 |
+
]
|
| 257 |
+
}
|
build/webgpu/unary-scalar.wgsl.jinja
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
{% macro emit_is_nan_f32() %}
|
| 24 |
+
fn is_nan_f32(value: f32) -> bool {
|
| 25 |
+
let bits = bitcast<u32>(value);
|
| 26 |
+
return (bits & 0x7f800000u) == 0x7f800000u && (bits & 0x007fffffu) != 0u;
|
| 27 |
+
}{% endmacro %}
|
| 28 |
+
{% macro emit_is_inf_f32() %}
|
| 29 |
+
fn is_inf_f32(value: f32) -> bool {
|
| 30 |
+
return (bitcast<u32>(value) & 0x7fffffffu) == 0x7f800000u;
|
| 31 |
+
}{% endmacro %}
|
| 32 |
+
{% macro emit_nan_from_inf_f32() %}
|
| 33 |
+
fn nan_from_inf_f32(value: f32) -> f32 {
|
| 34 |
+
return bitcast<f32>(bitcast<u32>(value) | 0x00400000u);
|
| 35 |
+
}{% endmacro %}
|
| 36 |
+
{% macro emit_reduce_pio2() %}
|
| 37 |
+
fn reduce_pio2(ax: f32) -> Pio2 {
|
| 38 |
+
let ix = bitcast<u32>(ax);
|
| 39 |
+
let e = (ix >> 23u) & 0xffu;
|
| 40 |
+
let mant = (ix & 0x7fffffu) | 0x800000u; // 24-bit mantissa
|
| 41 |
+
let p = i32(e) - 150; // ax = mant * 2^p
|
| 42 |
+
// 96-bit accumulator a2:a1:a0 holding (ax * 2/pi mod 4) * 2^94.
|
| 43 |
+
var a0 = 0u; var a1 = 0u; var a2 = 0u;
|
| 44 |
+
for (var k = 0u; k < 14u; k = k + 1u) {
|
| 45 |
+
let shift = p - 24 * (i32(k) + 1) + 94;
|
| 46 |
+
if (shift <= -48 || shift >= 96) { continue; }
|
| 47 |
+
// prod = mant * TWO_OVER_PI[k] (<= 48 bits) -> phi32:plo
|
| 48 |
+
let t = TWO_OVER_PI[k];
|
| 49 |
+
let mlo = mant & 0xffffu; let mhi = mant >> 16u;
|
| 50 |
+
let tlo = t & 0xffffu; let thi = t >> 16u;
|
| 51 |
+
let ll = mlo * tlo;
|
| 52 |
+
let mid = mlo * thi + mhi * tlo; // <= 25 bits, no overflow
|
| 53 |
+
let hh = mhi * thi;
|
| 54 |
+
let plo = ll + ((mid & 0xffffu) << 16u);
|
| 55 |
+
let carry = select(0u, 1u, plo < ll);
|
| 56 |
+
let phi32 = hh + (mid >> 16u) + carry; // <= 16 bits
|
| 57 |
+
// Place (phi32:plo) << shift as a 64-bit value (v1:v0) at offset off >= 0.
|
| 58 |
+
var v0 = 0u; var v1 = 0u; var off = 0;
|
| 59 |
+
if (shift < 0) {
|
| 60 |
+
let s = u32(-shift); // 1..47
|
| 61 |
+
if (s < 32u) {
|
| 62 |
+
v0 = (plo >> s) | (phi32 << (32u - s));
|
| 63 |
+
v1 = phi32 >> s;
|
| 64 |
+
} else {
|
| 65 |
+
v0 = phi32 >> (s - 32u);
|
| 66 |
+
v1 = 0u;
|
| 67 |
+
}
|
| 68 |
+
off = 0;
|
| 69 |
+
} else {
|
| 70 |
+
v0 = plo; v1 = phi32; off = shift; // 0..95
|
| 71 |
+
}
|
| 72 |
+
// Spread (v1:v0) across 96-bit limbs starting at bit `off`.
|
| 73 |
+
let li = off / 32; let b = u32(off % 32);
|
| 74 |
+
var w0 = 0u; var w1 = 0u; var w2 = 0u;
|
| 75 |
+
if (b == 0u) { w0 = v0; w1 = v1; w2 = 0u; }
|
| 76 |
+
else {
|
| 77 |
+
w0 = v0 << b;
|
| 78 |
+
w1 = (v1 << b) | (v0 >> (32u - b));
|
| 79 |
+
w2 = v1 >> (32u - b);
|
| 80 |
+
}
|
| 81 |
+
var t0 = 0u; var t1 = 0u; var t2 = 0u;
|
| 82 |
+
if (li == 0) { t0 = w0; t1 = w1; t2 = w2; }
|
| 83 |
+
else if (li == 1) { t1 = w0; t2 = w1; }
|
| 84 |
+
else { t2 = w0; }
|
| 85 |
+
// 96-bit add acc += t (bits above 2^96 are multiples of 4, so discarded).
|
| 86 |
+
let n0 = a0 + t0; let c0 = select(0u, 1u, n0 < a0);
|
| 87 |
+
let n1a = a1 + t1; let c1a = select(0u, 1u, n1a < a1);
|
| 88 |
+
let n1 = n1a + c0; let c1b = select(0u, 1u, n1 < n1a);
|
| 89 |
+
let n2 = a2 + t2 + c1a + c1b;
|
| 90 |
+
a0 = n0; a1 = n1; a2 = n2;
|
| 91 |
+
}
|
| 92 |
+
var n = a2 >> 30u; // integer part mod 4
|
| 93 |
+
var phi = f32(a2 & 0x3fffffffu) * (1.0 / 1073741824.0)
|
| 94 |
+
+ f32(a1) * (1.0 / 4611686018427387904.0); // fraction in [0,1)
|
| 95 |
+
if (phi >= 0.5) { phi = phi - 1.0; n = n + 1u; }
|
| 96 |
+
var out: Pio2;
|
| 97 |
+
out.octant = n & 3u;
|
| 98 |
+
out.r = phi * PIO2_F;
|
| 99 |
+
return out;
|
| 100 |
+
}{% endmacro %}
|
| 101 |
+
{% macro emit_reduce_pio2_fast() %}
|
| 102 |
+
fn reduce_pio2_fast(ax: f32) -> Pio2 {
|
| 103 |
+
// Cody-Waite reduction for ordinary magnitudes. Splitting pi/2 keeps the
|
| 104 |
+
// residual accurate even when n is thousands; larger inputs use Payne-Hanek.
|
| 105 |
+
let nFloat = floor(fma(ax, INV_PIO2_F, 0.5));
|
| 106 |
+
var residual = fma(-nFloat, PIO2_HI_F, ax);
|
| 107 |
+
residual = fma(-nFloat, PIO2_LO_F, residual);
|
| 108 |
+
var out: Pio2;
|
| 109 |
+
out.octant = u32(nFloat) & 3u;
|
| 110 |
+
out.r = residual;
|
| 111 |
+
return out;
|
| 112 |
+
}{% endmacro %}
|
| 113 |
+
{% macro emit_trig_reduction_support() %}
|
| 114 |
+
{% set needPreciseTrigCentered = false %}
|
| 115 |
+
{% set needPreciseTrigTwoPi = false %}
|
| 116 |
+
// Backend-stable f32 sine/cosine core.
|
| 117 |
+
//
|
| 118 |
+
// Shader transcendental accuracy is implementation-defined, and some portable
|
| 119 |
+
// backends are only accurate to roughly 1e-4. Each path retains the most accurate
|
| 120 |
+
// available phase representation, reduces it to [-pi, pi], then uses these
|
| 121 |
+
// polynomials. The half-pi core is degree 13 for sine and degree 12 for cosine;
|
| 122 |
+
// truncation error is well below one f32 ULP over its documented interval.
|
| 123 |
+
{% if needPreciseTrigCentered %}
|
| 124 |
+
const PRECISE_TRIG_PI: f32 = 3.141592653589793;
|
| 125 |
+
{% endif %}
|
| 126 |
+
{% if needPreciseTrigTwoPi %}
|
| 127 |
+
const PRECISE_TRIG_TWO_PI: f32 = 6.283185307179586;
|
| 128 |
+
{% endif %}
|
| 129 |
+
{% if needPreciseTrigCentered %}
|
| 130 |
+
const PRECISE_TRIG_HALF_PI: f32 = 1.5707963267948966;
|
| 131 |
+
{% endif %}
|
| 132 |
+
|
| 133 |
+
// Input must be in [-pi/2, pi/2]. Returns (cos(x), sin(x)).
|
| 134 |
+
fn precise_sincos_half_pi(x: f32) -> vec2<f32> {
|
| 135 |
+
let x2 = x * x;
|
| 136 |
+
|
| 137 |
+
var sinPolynomial = 1.6059043836821613e-10;
|
| 138 |
+
sinPolynomial = fma(sinPolynomial, x2, -2.505210838544172e-8);
|
| 139 |
+
sinPolynomial = fma(sinPolynomial, x2, 2.7557319223985893e-6);
|
| 140 |
+
sinPolynomial = fma(sinPolynomial, x2, -1.984126984126984e-4);
|
| 141 |
+
sinPolynomial = fma(sinPolynomial, x2, 8.333333333333333e-3);
|
| 142 |
+
sinPolynomial = fma(sinPolynomial, x2, -1.6666666666666666e-1);
|
| 143 |
+
let sine = x * fma(sinPolynomial, x2, 1.0);
|
| 144 |
+
|
| 145 |
+
var cosPolynomial = 2.08767569878681e-9;
|
| 146 |
+
cosPolynomial = fma(cosPolynomial, x2, -2.755731922398589e-7);
|
| 147 |
+
cosPolynomial = fma(cosPolynomial, x2, 2.48015873015873e-5);
|
| 148 |
+
cosPolynomial = fma(cosPolynomial, x2, -1.388888888888889e-3);
|
| 149 |
+
cosPolynomial = fma(cosPolynomial, x2, 4.1666666666666664e-2);
|
| 150 |
+
cosPolynomial = fma(cosPolynomial, x2, -5.0e-1);
|
| 151 |
+
let cosine = fma(cosPolynomial, x2, 1.0);
|
| 152 |
+
|
| 153 |
+
return vec2<f32>(cosine, sine);
|
| 154 |
+
}
|
| 155 |
+
{% if needPreciseTrigCentered %}
|
| 156 |
+
|
| 157 |
+
// Input must be in [-pi, pi]. Returns (cos(x), sin(x)).
|
| 158 |
+
fn precise_sincos_centered(x: f32) -> vec2<f32> {
|
| 159 |
+
var folded = x;
|
| 160 |
+
var cosineSign = 1.0;
|
| 161 |
+
if (folded > PRECISE_TRIG_HALF_PI) {
|
| 162 |
+
folded = PRECISE_TRIG_PI - folded;
|
| 163 |
+
cosineSign = -1.0;
|
| 164 |
+
} else if (folded < -PRECISE_TRIG_HALF_PI) {
|
| 165 |
+
folded = -PRECISE_TRIG_PI - folded;
|
| 166 |
+
cosineSign = -1.0;
|
| 167 |
+
}
|
| 168 |
+
let value = precise_sincos_half_pi(folded);
|
| 169 |
+
return vec2<f32>(cosineSign * value.x, value.y);
|
| 170 |
+
}
|
| 171 |
+
{% endif %}
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
// 24-bit words of 2/pi used by the exact Payne-Hanek large-input reducer.
|
| 175 |
+
const TWO_OVER_PI: array<u32, 14> = array<u32, 14>(
|
| 176 |
+
0xa2f983u, 0x6e4e44u, 0x1529fcu, 0x2757d1u, 0xf534ddu, 0xc0db62u,
|
| 177 |
+
0x95993cu, 0x439041u, 0xfe5163u, 0xabdebbu, 0xc561b7u, 0x246e3au,
|
| 178 |
+
0x424dd2u, 0xe00649u
|
| 179 |
+
);
|
| 180 |
+
const PIO2_F: f32 = 1.5707963267948966;
|
| 181 |
+
const INV_PIO2_F: f32 = 0.6366197723675814;
|
| 182 |
+
const PIO2_HI_F: f32 = 1.570796251296997;
|
| 183 |
+
const PIO2_LO_F: f32 = 7.549789415861596e-8;
|
| 184 |
+
const REDUCE_THRESHOLD: f32 = 1.0e4;
|
| 185 |
+
|
| 186 |
+
struct Pio2 { octant: u32, r: f32 };
|
| 187 |
+
|
| 188 |
+
{{ emit_reduce_pio2_fast() }}
|
| 189 |
+
|
| 190 |
+
{{ emit_reduce_pio2() }}
|
| 191 |
+
{%- endmacro %}
|
| 192 |
+
{% macro emit_sin_accurate() %}
|
| 193 |
+
fn sin_accurate(x: f32) -> f32 {
|
| 194 |
+
if (is_nan_f32(x)) {
|
| 195 |
+
return x;
|
| 196 |
+
}
|
| 197 |
+
if (is_inf_f32(x)) {
|
| 198 |
+
return nan_from_inf_f32(x);
|
| 199 |
+
}
|
| 200 |
+
let ax = abs(x);
|
| 201 |
+
var red: Pio2;
|
| 202 |
+
if (ax < REDUCE_THRESHOLD) { red = reduce_pio2_fast(ax); }
|
| 203 |
+
else { red = reduce_pio2(ax); }
|
| 204 |
+
let reduced = precise_sincos_half_pi(red.r);
|
| 205 |
+
var s: f32;
|
| 206 |
+
switch (red.octant) {
|
| 207 |
+
case 0u: { s = reduced.y; }
|
| 208 |
+
case 1u: { s = reduced.x; }
|
| 209 |
+
case 2u: { s = -reduced.y; }
|
| 210 |
+
default: { s = -reduced.x; }
|
| 211 |
+
}
|
| 212 |
+
return select(s, -s, x < 0.0); // sin is odd
|
| 213 |
+
}{% endmacro %}
|
| 214 |
+
{{ emit_trig_reduction_support() }}
|
| 215 |
+
|
| 216 |
+
{{ emit_is_nan_f32() }}
|
| 217 |
+
|
| 218 |
+
{{ emit_is_inf_f32() }}
|
| 219 |
+
|
| 220 |
+
{{ emit_nan_from_inf_f32() }}
|
| 221 |
+
|
| 222 |
+
{{ emit_sin_accurate() }}
|
| 223 |
+
{{ flat_tail_open() }}
|
| 224 |
+
y[i] = {{ scalar }}(sin_accurate(f32(x[i])));
|
| 225 |
+
{{ flat_tail_close() -}}
|
| 226 |
+
}
|
build/webgpu/unary-vec4.wgsl.jinja
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
{% macro emit_is_inf_f32() %}
|
| 14 |
+
fn is_inf_f32(value: f32) -> bool {
|
| 15 |
+
return (bitcast<u32>(value) & 0x7fffffffu) == 0x7f800000u;
|
| 16 |
+
}{% endmacro %}
|
| 17 |
+
{% macro emit_nan_from_inf_f32() %}
|
| 18 |
+
fn nan_from_inf_f32(value: f32) -> f32 {
|
| 19 |
+
return bitcast<f32>(bitcast<u32>(value) | 0x00400000u);
|
| 20 |
+
}{% endmacro %}
|
| 21 |
+
{% macro emit_reduce_pio2() %}
|
| 22 |
+
fn reduce_pio2(ax: f32) -> Pio2 {
|
| 23 |
+
let ix = bitcast<u32>(ax);
|
| 24 |
+
let e = (ix >> 23u) & 0xffu;
|
| 25 |
+
let mant = (ix & 0x7fffffu) | 0x800000u; // 24-bit mantissa
|
| 26 |
+
let p = i32(e) - 150; // ax = mant * 2^p
|
| 27 |
+
// 96-bit accumulator a2:a1:a0 holding (ax * 2/pi mod 4) * 2^94.
|
| 28 |
+
var a0 = 0u; var a1 = 0u; var a2 = 0u;
|
| 29 |
+
for (var k = 0u; k < 14u; k = k + 1u) {
|
| 30 |
+
let shift = p - 24 * (i32(k) + 1) + 94;
|
| 31 |
+
if (shift <= -48 || shift >= 96) { continue; }
|
| 32 |
+
// prod = mant * TWO_OVER_PI[k] (<= 48 bits) -> phi32:plo
|
| 33 |
+
let t = TWO_OVER_PI[k];
|
| 34 |
+
let mlo = mant & 0xffffu; let mhi = mant >> 16u;
|
| 35 |
+
let tlo = t & 0xffffu; let thi = t >> 16u;
|
| 36 |
+
let ll = mlo * tlo;
|
| 37 |
+
let mid = mlo * thi + mhi * tlo; // <= 25 bits, no overflow
|
| 38 |
+
let hh = mhi * thi;
|
| 39 |
+
let plo = ll + ((mid & 0xffffu) << 16u);
|
| 40 |
+
let carry = select(0u, 1u, plo < ll);
|
| 41 |
+
let phi32 = hh + (mid >> 16u) + carry; // <= 16 bits
|
| 42 |
+
// Place (phi32:plo) << shift as a 64-bit value (v1:v0) at offset off >= 0.
|
| 43 |
+
var v0 = 0u; var v1 = 0u; var off = 0;
|
| 44 |
+
if (shift < 0) {
|
| 45 |
+
let s = u32(-shift); // 1..47
|
| 46 |
+
if (s < 32u) {
|
| 47 |
+
v0 = (plo >> s) | (phi32 << (32u - s));
|
| 48 |
+
v1 = phi32 >> s;
|
| 49 |
+
} else {
|
| 50 |
+
v0 = phi32 >> (s - 32u);
|
| 51 |
+
v1 = 0u;
|
| 52 |
+
}
|
| 53 |
+
off = 0;
|
| 54 |
+
} else {
|
| 55 |
+
v0 = plo; v1 = phi32; off = shift; // 0..95
|
| 56 |
+
}
|
| 57 |
+
// Spread (v1:v0) across 96-bit limbs starting at bit `off`.
|
| 58 |
+
let li = off / 32; let b = u32(off % 32);
|
| 59 |
+
var w0 = 0u; var w1 = 0u; var w2 = 0u;
|
| 60 |
+
if (b == 0u) { w0 = v0; w1 = v1; w2 = 0u; }
|
| 61 |
+
else {
|
| 62 |
+
w0 = v0 << b;
|
| 63 |
+
w1 = (v1 << b) | (v0 >> (32u - b));
|
| 64 |
+
w2 = v1 >> (32u - b);
|
| 65 |
+
}
|
| 66 |
+
var t0 = 0u; var t1 = 0u; var t2 = 0u;
|
| 67 |
+
if (li == 0) { t0 = w0; t1 = w1; t2 = w2; }
|
| 68 |
+
else if (li == 1) { t1 = w0; t2 = w1; }
|
| 69 |
+
else { t2 = w0; }
|
| 70 |
+
// 96-bit add acc += t (bits above 2^96 are multiples of 4, so discarded).
|
| 71 |
+
let n0 = a0 + t0; let c0 = select(0u, 1u, n0 < a0);
|
| 72 |
+
let n1a = a1 + t1; let c1a = select(0u, 1u, n1a < a1);
|
| 73 |
+
let n1 = n1a + c0; let c1b = select(0u, 1u, n1 < n1a);
|
| 74 |
+
let n2 = a2 + t2 + c1a + c1b;
|
| 75 |
+
a0 = n0; a1 = n1; a2 = n2;
|
| 76 |
+
}
|
| 77 |
+
var n = a2 >> 30u; // integer part mod 4
|
| 78 |
+
var phi = f32(a2 & 0x3fffffffu) * (1.0 / 1073741824.0)
|
| 79 |
+
+ f32(a1) * (1.0 / 4611686018427387904.0); // fraction in [0,1)
|
| 80 |
+
if (phi >= 0.5) { phi = phi - 1.0; n = n + 1u; }
|
| 81 |
+
var out: Pio2;
|
| 82 |
+
out.octant = n & 3u;
|
| 83 |
+
out.r = phi * PIO2_F;
|
| 84 |
+
return out;
|
| 85 |
+
}{% endmacro %}
|
| 86 |
+
{% macro emit_reduce_pio2_fast() %}
|
| 87 |
+
fn reduce_pio2_fast(ax: f32) -> Pio2 {
|
| 88 |
+
// Cody-Waite reduction for ordinary magnitudes. Splitting pi/2 keeps the
|
| 89 |
+
// residual accurate even when n is thousands; larger inputs use Payne-Hanek.
|
| 90 |
+
let nFloat = floor(fma(ax, INV_PIO2_F, 0.5));
|
| 91 |
+
var residual = fma(-nFloat, PIO2_HI_F, ax);
|
| 92 |
+
residual = fma(-nFloat, PIO2_LO_F, residual);
|
| 93 |
+
var out: Pio2;
|
| 94 |
+
out.octant = u32(nFloat) & 3u;
|
| 95 |
+
out.r = residual;
|
| 96 |
+
return out;
|
| 97 |
+
}{% endmacro %}
|
| 98 |
+
{% macro emit_trig_reduction_support() %}
|
| 99 |
+
{% set needPreciseTrigCentered = false %}
|
| 100 |
+
{% set needPreciseTrigTwoPi = false %}
|
| 101 |
+
// Backend-stable f32 sine/cosine core.
|
| 102 |
+
//
|
| 103 |
+
// Shader transcendental accuracy is implementation-defined, and some portable
|
| 104 |
+
// backends are only accurate to roughly 1e-4. Each path retains the most accurate
|
| 105 |
+
// available phase representation, reduces it to [-pi, pi], then uses these
|
| 106 |
+
// polynomials. The half-pi core is degree 13 for sine and degree 12 for cosine;
|
| 107 |
+
// truncation error is well below one f32 ULP over its documented interval.
|
| 108 |
+
{% if needPreciseTrigCentered %}
|
| 109 |
+
const PRECISE_TRIG_PI: f32 = 3.141592653589793;
|
| 110 |
+
{% endif %}
|
| 111 |
+
{% if needPreciseTrigTwoPi %}
|
| 112 |
+
const PRECISE_TRIG_TWO_PI: f32 = 6.283185307179586;
|
| 113 |
+
{% endif %}
|
| 114 |
+
{% if needPreciseTrigCentered %}
|
| 115 |
+
const PRECISE_TRIG_HALF_PI: f32 = 1.5707963267948966;
|
| 116 |
+
{% endif %}
|
| 117 |
+
|
| 118 |
+
// Input must be in [-pi/2, pi/2]. Returns (cos(x), sin(x)).
|
| 119 |
+
fn precise_sincos_half_pi(x: f32) -> vec2<f32> {
|
| 120 |
+
let x2 = x * x;
|
| 121 |
+
|
| 122 |
+
var sinPolynomial = 1.6059043836821613e-10;
|
| 123 |
+
sinPolynomial = fma(sinPolynomial, x2, -2.505210838544172e-8);
|
| 124 |
+
sinPolynomial = fma(sinPolynomial, x2, 2.7557319223985893e-6);
|
| 125 |
+
sinPolynomial = fma(sinPolynomial, x2, -1.984126984126984e-4);
|
| 126 |
+
sinPolynomial = fma(sinPolynomial, x2, 8.333333333333333e-3);
|
| 127 |
+
sinPolynomial = fma(sinPolynomial, x2, -1.6666666666666666e-1);
|
| 128 |
+
let sine = x * fma(sinPolynomial, x2, 1.0);
|
| 129 |
+
|
| 130 |
+
var cosPolynomial = 2.08767569878681e-9;
|
| 131 |
+
cosPolynomial = fma(cosPolynomial, x2, -2.755731922398589e-7);
|
| 132 |
+
cosPolynomial = fma(cosPolynomial, x2, 2.48015873015873e-5);
|
| 133 |
+
cosPolynomial = fma(cosPolynomial, x2, -1.388888888888889e-3);
|
| 134 |
+
cosPolynomial = fma(cosPolynomial, x2, 4.1666666666666664e-2);
|
| 135 |
+
cosPolynomial = fma(cosPolynomial, x2, -5.0e-1);
|
| 136 |
+
let cosine = fma(cosPolynomial, x2, 1.0);
|
| 137 |
+
|
| 138 |
+
return vec2<f32>(cosine, sine);
|
| 139 |
+
}
|
| 140 |
+
{% if needPreciseTrigCentered %}
|
| 141 |
+
|
| 142 |
+
// Input must be in [-pi, pi]. Returns (cos(x), sin(x)).
|
| 143 |
+
fn precise_sincos_centered(x: f32) -> vec2<f32> {
|
| 144 |
+
var folded = x;
|
| 145 |
+
var cosineSign = 1.0;
|
| 146 |
+
if (folded > PRECISE_TRIG_HALF_PI) {
|
| 147 |
+
folded = PRECISE_TRIG_PI - folded;
|
| 148 |
+
cosineSign = -1.0;
|
| 149 |
+
} else if (folded < -PRECISE_TRIG_HALF_PI) {
|
| 150 |
+
folded = -PRECISE_TRIG_PI - folded;
|
| 151 |
+
cosineSign = -1.0;
|
| 152 |
+
}
|
| 153 |
+
let value = precise_sincos_half_pi(folded);
|
| 154 |
+
return vec2<f32>(cosineSign * value.x, value.y);
|
| 155 |
+
}
|
| 156 |
+
{% endif %}
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
// 24-bit words of 2/pi used by the exact Payne-Hanek large-input reducer.
|
| 160 |
+
const TWO_OVER_PI: array<u32, 14> = array<u32, 14>(
|
| 161 |
+
0xa2f983u, 0x6e4e44u, 0x1529fcu, 0x2757d1u, 0xf534ddu, 0xc0db62u,
|
| 162 |
+
0x95993cu, 0x439041u, 0xfe5163u, 0xabdebbu, 0xc561b7u, 0x246e3au,
|
| 163 |
+
0x424dd2u, 0xe00649u
|
| 164 |
+
);
|
| 165 |
+
const PIO2_F: f32 = 1.5707963267948966;
|
| 166 |
+
const INV_PIO2_F: f32 = 0.6366197723675814;
|
| 167 |
+
const PIO2_HI_F: f32 = 1.570796251296997;
|
| 168 |
+
const PIO2_LO_F: f32 = 7.549789415861596e-8;
|
| 169 |
+
const REDUCE_THRESHOLD: f32 = 1.0e4;
|
| 170 |
+
|
| 171 |
+
struct Pio2 { octant: u32, r: f32 };
|
| 172 |
+
|
| 173 |
+
{{ emit_reduce_pio2_fast() }}
|
| 174 |
+
|
| 175 |
+
{{ emit_reduce_pio2() }}
|
| 176 |
+
{%- endmacro %}
|
| 177 |
+
{% macro emit_sin_accurate() %}
|
| 178 |
+
fn sin_accurate(x: f32) -> f32 {
|
| 179 |
+
if (is_nan_f32(x)) {
|
| 180 |
+
return x;
|
| 181 |
+
}
|
| 182 |
+
if (is_inf_f32(x)) {
|
| 183 |
+
return nan_from_inf_f32(x);
|
| 184 |
+
}
|
| 185 |
+
let ax = abs(x);
|
| 186 |
+
var red: Pio2;
|
| 187 |
+
if (ax < REDUCE_THRESHOLD) { red = reduce_pio2_fast(ax); }
|
| 188 |
+
else { red = reduce_pio2(ax); }
|
| 189 |
+
let reduced = precise_sincos_half_pi(red.r);
|
| 190 |
+
var s: f32;
|
| 191 |
+
switch (red.octant) {
|
| 192 |
+
case 0u: { s = reduced.y; }
|
| 193 |
+
case 1u: { s = reduced.x; }
|
| 194 |
+
case 2u: { s = -reduced.y; }
|
| 195 |
+
default: { s = -reduced.x; }
|
| 196 |
+
}
|
| 197 |
+
return select(s, -s, x < 0.0); // sin is odd
|
| 198 |
+
}{% endmacro %}
|
| 199 |
+
{{ emit_is_nan_f32() }}
|
| 200 |
+
{{ emit_trig_reduction_support() }}
|
| 201 |
+
|
| 202 |
+
{{ emit_is_inf_f32() }}
|
| 203 |
+
|
| 204 |
+
{{ emit_nan_from_inf_f32() }}
|
| 205 |
+
|
| 206 |
+
{{ emit_sin_accurate() }}
|
| 207 |
+
|
| 208 |
+
@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
|
| 209 |
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {
|
| 210 |
+
// 2D-folded flat index: gid.y carries the high bits when the element count exceeds the
|
| 211 |
+
// maxComputeWorkgroupsPerDimension limit (the dispatch caps x and spills the rest into y).
|
| 212 |
+
let i = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;
|
| 213 |
+
if (i >= params.count) {
|
| 214 |
+
return;
|
| 215 |
+
}
|
| 216 |
+
let xv = x[i];
|
| 217 |
+
let fv = vec4<f32>(xv);
|
| 218 |
+
y[i] = {{ vectorScalar }}(vec4<f32>(sin_accurate(fv.x), sin_accurate(fv.y), sin_accurate(fv.z), sin_accurate(fv.w)));
|
| 219 |
+
}
|