sync 6fdf6301e2bb
Browse files
README.md
CHANGED
|
@@ -49,7 +49,7 @@ Default values (overridable per request):
|
|
| 49 |
- [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, provenance)
|
| 50 |
- [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)
|
| 51 |
- [`test.json`](build/webgpu/test.json) — correctness cases
|
| 52 |
-
- [`bench.json`](build/webgpu/bench.json) — benchmark
|
| 53 |
- [`resize-coord-transform.wgsl.jinja`](build/webgpu/resize-coord-transform.wgsl.jinja)
|
| 54 |
- [`resize-generic.wgsl.jinja`](build/webgpu/resize-generic.wgsl.jinja)
|
| 55 |
- [`resize-linear-2x-stencil.wgsl.jinja`](build/webgpu/resize-linear-2x-stencil.wgsl.jinja)
|
|
@@ -58,7 +58,7 @@ Default values (overridable per request):
|
|
| 58 |
## Use with `@huggingface/kernels`
|
| 59 |
|
| 60 |
```sh
|
| 61 |
-
npm install --save-exact @huggingface/kernels@0.0.1-preview.
|
| 62 |
```
|
| 63 |
|
| 64 |
Outputs with inferable metadata are allocated automatically. Explicit `outputs` entries request optional results or provide metadata that cannot be inferred from the supplied inputs and attributes.
|
|
|
|
| 49 |
- [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, provenance)
|
| 50 |
- [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)
|
| 51 |
- [`test.json`](build/webgpu/test.json) — correctness cases
|
| 52 |
+
- [`bench.json`](build/webgpu/bench.json) — benchmark cases
|
| 53 |
- [`resize-coord-transform.wgsl.jinja`](build/webgpu/resize-coord-transform.wgsl.jinja)
|
| 54 |
- [`resize-generic.wgsl.jinja`](build/webgpu/resize-generic.wgsl.jinja)
|
| 55 |
- [`resize-linear-2x-stencil.wgsl.jinja`](build/webgpu/resize-linear-2x-stencil.wgsl.jinja)
|
|
|
|
| 58 |
## Use with `@huggingface/kernels`
|
| 59 |
|
| 60 |
```sh
|
| 61 |
+
npm install --save-exact @huggingface/kernels@0.0.1-preview.3
|
| 62 |
```
|
| 63 |
|
| 64 |
Outputs with inferable metadata are allocated automatically. Explicit `outputs` entries request optional results or provide metadata that cannot be inferred from the supplied inputs and attributes.
|
build/webgpu/manifest.json
CHANGED
|
@@ -8,13 +8,19 @@
|
|
| 8 |
"attributeConstraints": { "mode": { "values": ["nearest", "linear"] } },
|
| 9 |
"typeConstraints": { "T": ["float32", "float16", "int32", "int8", "uint8"], "S": ["float32"] },
|
| 10 |
"tunables": { "WORKGROUP_SIZE": { "default": 256 } },
|
| 11 |
-
"derive": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"when": ["f16Ok(dtypes.T)"],
|
| 13 |
"bindings": {
|
| 14 |
-
"y": { "
|
| 15 |
-
"scales": { "
|
| 16 |
"params": {
|
| 17 |
-
"buffer": "uniform",
|
| 18 |
"struct": [
|
| 19 |
{ "name": "count", "type": "u32", "value": "numel(shapes.y)" },
|
| 20 |
{ "name": "C", "type": "u32", "value": "dim(shapes.x, 1)" },
|
|
@@ -30,13 +36,14 @@
|
|
| 30 |
"id": "nearest_integer_scale_vec4",
|
| 31 |
"priority": 21,
|
| 32 |
"when": ["ranks.x == 4", "ranks.y == 4", "dim(shapes.x, 0) == dim(shapes.y, 0)", "dim(shapes.x, 1) == dim(shapes.y, 1)", "numel(shapes.y) > 0", "attrs.mode == \"nearest\"", "(dim(shapes.y, 2) == dim(shapes.x, 2) * 2 or dim(shapes.y, 2) == dim(shapes.x, 2) * 3 or dim(shapes.y, 2) == dim(shapes.x, 2) * 4)", "(dim(shapes.y, 3) == dim(shapes.x, 3) * 2 or dim(shapes.y, 3) == dim(shapes.x, 3) * 3 or dim(shapes.y, 3) == dim(shapes.x, 3) * 4)", "dim(shapes.y, 3) % 4 == 0"],
|
|
|
|
| 33 |
"derive": { "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"", "outputElement": "\"vec4<\" ~ dtypes.T ~ \">\"" },
|
| 34 |
"passes": [
|
| 35 |
{
|
| 36 |
"id": "main",
|
| 37 |
-
"name": "Upsample.
|
| 38 |
"shader": "resize-nearest-integer-scale.wgsl.jinja",
|
| 39 |
-
"derive": { "
|
| 40 |
"bindings": ["x", "y"],
|
| 41 |
"dispatch": {
|
| 42 |
"x": "min(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
|
|
@@ -54,9 +61,9 @@
|
|
| 54 |
"passes": [
|
| 55 |
{
|
| 56 |
"id": "main",
|
| 57 |
-
"name": "Upsample.
|
| 58 |
"shader": "resize-nearest-integer-scale.wgsl.jinja",
|
| 59 |
-
"derive": { "
|
| 60 |
"bindings": ["x", "y"],
|
| 61 |
"dispatch": {
|
| 62 |
"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
|
|
@@ -70,19 +77,13 @@
|
|
| 70 |
"id": "linear_2x_stencil_vec4",
|
| 71 |
"priority": 20,
|
| 72 |
"when": ["ranks.x == 4", "ranks.y == 4", "dim(shapes.x, 0) == dim(shapes.y, 0)", "dim(shapes.x, 1) == dim(shapes.y, 1)", "numel(shapes.y) > 0", "attrs.mode == \"linear\"", "dim(shapes.y, 2) == dim(shapes.x, 2) * 2", "dim(shapes.y, 3) == dim(shapes.x, 3) * 2", "dim(shapes.y, 3) % 4 == 0"],
|
| 73 |
-
"derive": { "
|
| 74 |
"passes": [
|
| 75 |
{
|
| 76 |
"id": "main",
|
| 77 |
-
"name": "Upsample.
|
| 78 |
"shader": "resize-linear-2x-stencil.wgsl.jinja",
|
| 79 |
-
"derive": {
|
| 80 |
-
"xShape": "shapes.x",
|
| 81 |
-
"yShape": "shapes.y",
|
| 82 |
-
"coordinateMode": "\"asymmetric\"",
|
| 83 |
-
"vec4": true,
|
| 84 |
-
"scalesBuffer": true
|
| 85 |
-
},
|
| 86 |
"bindings": ["x", "scales", "y"],
|
| 87 |
"dispatch": {
|
| 88 |
"x": "min(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
|
|
@@ -100,14 +101,8 @@
|
|
| 100 |
"passes": [
|
| 101 |
{
|
| 102 |
"id": "main",
|
| 103 |
-
"name": "Upsample.
|
| 104 |
"shader": "resize-linear-2x-stencil.wgsl.jinja",
|
| 105 |
-
"derive": {
|
| 106 |
-
"xShape": "shapes.x",
|
| 107 |
-
"yShape": "shapes.y",
|
| 108 |
-
"coordinateMode": "\"asymmetric\"",
|
| 109 |
-
"scalesBuffer": true
|
| 110 |
-
},
|
| 111 |
"bindings": ["x", "scales", "y"],
|
| 112 |
"dispatch": {
|
| 113 |
"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
|
|
@@ -121,20 +116,13 @@
|
|
| 121 |
"id": "nchw_rank4_vec4",
|
| 122 |
"priority": 1,
|
| 123 |
"when": ["ranks.x == 4", "ranks.y == 4", "dim(shapes.x, 0) == dim(shapes.y, 0)", "dim(shapes.x, 1) == dim(shapes.y, 1)", "dim(shapes.y, 3) % 4 == 0"],
|
| 124 |
-
"derive": { "
|
| 125 |
"passes": [
|
| 126 |
{
|
| 127 |
"id": "main",
|
| 128 |
"name": "Upsample.Vec4",
|
| 129 |
"shader": "resize-coord-transform.wgsl.jinja",
|
| 130 |
-
"derive": {
|
| 131 |
-
"modeSpec": "attrs.mode",
|
| 132 |
-
"coordinateMode": "\"asymmetric\"",
|
| 133 |
-
"nearestMode": "\"floor\"",
|
| 134 |
-
"vec4": true,
|
| 135 |
-
"vecType": "\"vec4<\" ~ dtypes.T ~ \">\"",
|
| 136 |
-
"scalesBuffer": true
|
| 137 |
-
},
|
| 138 |
"bindings": ["x", "scales", "y", "params"],
|
| 139 |
"dispatch": {
|
| 140 |
"x": "min(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), 65535)",
|
|
@@ -153,12 +141,6 @@
|
|
| 153 |
"id": "main",
|
| 154 |
"name": "Upsample",
|
| 155 |
"shader": "resize-coord-transform.wgsl.jinja",
|
| 156 |
-
"derive": {
|
| 157 |
-
"modeSpec": "attrs.mode",
|
| 158 |
-
"coordinateMode": "\"asymmetric\"",
|
| 159 |
-
"nearestMode": "\"floor\"",
|
| 160 |
-
"scalesBuffer": true
|
| 161 |
-
},
|
| 162 |
"bindings": ["x", "scales", "y", "params"],
|
| 163 |
"dispatch": {
|
| 164 |
"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)",
|
|
@@ -177,14 +159,6 @@
|
|
| 177 |
"id": "main",
|
| 178 |
"name": "Upsample.Generic",
|
| 179 |
"shader": "resize-generic.wgsl.jinja",
|
| 180 |
-
"derive": {
|
| 181 |
-
"xShape": "shapes.x",
|
| 182 |
-
"yShape": "shapes.y",
|
| 183 |
-
"modeSpec": "attrs.mode",
|
| 184 |
-
"coordinateMode": "\"asymmetric\"",
|
| 185 |
-
"nearestMode": "\"floor\"",
|
| 186 |
-
"scalesBuffer": true
|
| 187 |
-
},
|
| 188 |
"bindings": ["x", "scales", { "arg": "y" }],
|
| 189 |
"dispatch": {
|
| 190 |
"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
|
|
|
|
| 8 |
"attributeConstraints": { "mode": { "values": ["nearest", "linear"] } },
|
| 9 |
"typeConstraints": { "T": ["float32", "float16", "int32", "int8", "uint8"], "S": ["float32"] },
|
| 10 |
"tunables": { "WORKGROUP_SIZE": { "default": 256 } },
|
| 11 |
+
"derive": {
|
| 12 |
+
"variableSubgroup16To32": "device.features.has(\"subgroups\") and has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize == 16 and device.adapterInfo.subgroupMaxSize == 32",
|
| 13 |
+
"scalar": "dtypes.T",
|
| 14 |
+
"coordinateMode": "\"asymmetric\"",
|
| 15 |
+
"modeSpec": "attrs.mode",
|
| 16 |
+
"xShape": "shapes.x",
|
| 17 |
+
"yShape": "shapes.y"
|
| 18 |
+
},
|
| 19 |
"when": ["f16Ok(dtypes.T)"],
|
| 20 |
"bindings": {
|
| 21 |
+
"y": { "elementType": "$outputElement" },
|
| 22 |
+
"scales": { "elementType": "f32" },
|
| 23 |
"params": {
|
|
|
|
| 24 |
"struct": [
|
| 25 |
{ "name": "count", "type": "u32", "value": "numel(shapes.y)" },
|
| 26 |
{ "name": "C", "type": "u32", "value": "dim(shapes.x, 1)" },
|
|
|
|
| 36 |
"id": "nearest_integer_scale_vec4",
|
| 37 |
"priority": 21,
|
| 38 |
"when": ["ranks.x == 4", "ranks.y == 4", "dim(shapes.x, 0) == dim(shapes.y, 0)", "dim(shapes.x, 1) == dim(shapes.y, 1)", "numel(shapes.y) > 0", "attrs.mode == \"nearest\"", "(dim(shapes.y, 2) == dim(shapes.x, 2) * 2 or dim(shapes.y, 2) == dim(shapes.x, 2) * 3 or dim(shapes.y, 2) == dim(shapes.x, 2) * 4)", "(dim(shapes.y, 3) == dim(shapes.x, 3) * 2 or dim(shapes.y, 3) == dim(shapes.x, 3) * 3 or dim(shapes.y, 3) == dim(shapes.x, 3) * 4)", "dim(shapes.y, 3) % 4 == 0"],
|
| 39 |
+
"demoteWhen": ["variableSubgroup16To32 and dtypes.T == \"f16\""],
|
| 40 |
"derive": { "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"", "outputElement": "\"vec4<\" ~ dtypes.T ~ \">\"" },
|
| 41 |
"passes": [
|
| 42 |
{
|
| 43 |
"id": "main",
|
| 44 |
+
"name": "Upsample.NearestIntegerScaleVec4",
|
| 45 |
"shader": "resize-nearest-integer-scale.wgsl.jinja",
|
| 46 |
+
"derive": { "vec4": true },
|
| 47 |
"bindings": ["x", "y"],
|
| 48 |
"dispatch": {
|
| 49 |
"x": "min(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
|
|
|
|
| 61 |
"passes": [
|
| 62 |
{
|
| 63 |
"id": "main",
|
| 64 |
+
"name": "Upsample.NearestIntegerScale",
|
| 65 |
"shader": "resize-nearest-integer-scale.wgsl.jinja",
|
| 66 |
+
"derive": { "vec4": false },
|
| 67 |
"bindings": ["x", "y"],
|
| 68 |
"dispatch": {
|
| 69 |
"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
|
|
|
|
| 77 |
"id": "linear_2x_stencil_vec4",
|
| 78 |
"priority": 20,
|
| 79 |
"when": ["ranks.x == 4", "ranks.y == 4", "dim(shapes.x, 0) == dim(shapes.y, 0)", "dim(shapes.x, 1) == dim(shapes.y, 1)", "numel(shapes.y) > 0", "attrs.mode == \"linear\"", "dim(shapes.y, 2) == dim(shapes.x, 2) * 2", "dim(shapes.y, 3) == dim(shapes.x, 3) * 2", "dim(shapes.y, 3) % 4 == 0"],
|
| 80 |
+
"derive": { "outputElement": "\"vec4<\" ~ dtypes.T ~ \">\"" },
|
| 81 |
"passes": [
|
| 82 |
{
|
| 83 |
"id": "main",
|
| 84 |
+
"name": "Upsample.Linear2xStencilVec4",
|
| 85 |
"shader": "resize-linear-2x-stencil.wgsl.jinja",
|
| 86 |
+
"derive": { "vec4": true },
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
"bindings": ["x", "scales", "y"],
|
| 88 |
"dispatch": {
|
| 89 |
"x": "min(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
|
|
|
|
| 101 |
"passes": [
|
| 102 |
{
|
| 103 |
"id": "main",
|
| 104 |
+
"name": "Upsample.Linear2xStencil",
|
| 105 |
"shader": "resize-linear-2x-stencil.wgsl.jinja",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
"bindings": ["x", "scales", "y"],
|
| 107 |
"dispatch": {
|
| 108 |
"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
|
|
|
|
| 116 |
"id": "nchw_rank4_vec4",
|
| 117 |
"priority": 1,
|
| 118 |
"when": ["ranks.x == 4", "ranks.y == 4", "dim(shapes.x, 0) == dim(shapes.y, 0)", "dim(shapes.x, 1) == dim(shapes.y, 1)", "dim(shapes.y, 3) % 4 == 0"],
|
| 119 |
+
"derive": { "outputElement": "\"vec4<\" ~ dtypes.T ~ \">\"" },
|
| 120 |
"passes": [
|
| 121 |
{
|
| 122 |
"id": "main",
|
| 123 |
"name": "Upsample.Vec4",
|
| 124 |
"shader": "resize-coord-transform.wgsl.jinja",
|
| 125 |
+
"derive": { "vec4": true, "vecType": "\"vec4<\" ~ dtypes.T ~ \">\"" },
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
"bindings": ["x", "scales", "y", "params"],
|
| 127 |
"dispatch": {
|
| 128 |
"x": "min(ceilDiv((numel(shapes.y) / 4), (tunables.WORKGROUP_SIZE)), 65535)",
|
|
|
|
| 141 |
"id": "main",
|
| 142 |
"name": "Upsample",
|
| 143 |
"shader": "resize-coord-transform.wgsl.jinja",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
"bindings": ["x", "scales", "y", "params"],
|
| 145 |
"dispatch": {
|
| 146 |
"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), 65535)",
|
|
|
|
| 159 |
"id": "main",
|
| 160 |
"name": "Upsample.Generic",
|
| 161 |
"shader": "resize-generic.wgsl.jinja",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
"bindings": ["x", "scales", { "arg": "y" }],
|
| 163 |
"dispatch": {
|
| 164 |
"x": "min(ceilDiv((numel(shapes.y)), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
|
build/webgpu/metadata.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
{
|
| 2 |
"name": "ai.onnx.Upsample",
|
| 3 |
-
"id": "
|
| 4 |
"version": 1,
|
| 5 |
"license": "Apache-2.0",
|
| 6 |
"backend": { "type": "webgpu" },
|
|
@@ -8,17 +8,17 @@
|
|
| 8 |
"algorithm": "sha256",
|
| 9 |
"files": {
|
| 10 |
"bench.json": "F6Vym+nah84vMiKJZvh+qKarMEzp84955NrGH18TWBs=",
|
| 11 |
-
"manifest.json": "
|
| 12 |
-
"resize-coord-transform.wgsl.jinja": "
|
| 13 |
-
"resize-generic.wgsl.jinja": "
|
| 14 |
-
"resize-linear-2x-stencil.wgsl.jinja": "
|
| 15 |
-
"resize-nearest-integer-scale.wgsl.jinja": "
|
| 16 |
"test.json": "Cmo87nuUyMyjpettO/Nks1mu7Z00DCEnYyaHf5fQNsI="
|
| 17 |
}
|
| 18 |
},
|
| 19 |
-
"provenance": { "kernel": { "sha": "
|
| 20 |
"webgpu": {
|
| 21 |
-
"manifestSpec": "2.
|
| 22 |
"variants": {
|
| 23 |
"nearest_integer_scale_vec4": ["resize-nearest-integer-scale.wgsl.jinja"],
|
| 24 |
"nearest_integer_scale": ["resize-nearest-integer-scale.wgsl.jinja"],
|
|
|
|
| 1 |
{
|
| 2 |
"name": "ai.onnx.Upsample",
|
| 3 |
+
"id": "_ai_onnx_upsample_webgpu_cb71822",
|
| 4 |
"version": 1,
|
| 5 |
"license": "Apache-2.0",
|
| 6 |
"backend": { "type": "webgpu" },
|
|
|
|
| 8 |
"algorithm": "sha256",
|
| 9 |
"files": {
|
| 10 |
"bench.json": "F6Vym+nah84vMiKJZvh+qKarMEzp84955NrGH18TWBs=",
|
| 11 |
+
"manifest.json": "2uEwD7jDi9nHofX7ePgu09pGy/AIHaIFE/lLqA5BNKc=",
|
| 12 |
+
"resize-coord-transform.wgsl.jinja": "aRm+usKi/SLxH3VPgozMY05TxLa6Hpk5KOA5OD/gMYM=",
|
| 13 |
+
"resize-generic.wgsl.jinja": "UXVb1OZE8NjCU3LL4cBRNvLmePLnodiLlo0lCasjvWE=",
|
| 14 |
+
"resize-linear-2x-stencil.wgsl.jinja": "YY/uYm9GjvF2Qsxi6GmZB7Ga6Ejv14tQdlFchKjAIh0=",
|
| 15 |
+
"resize-nearest-integer-scale.wgsl.jinja": "828yL0GocXjpF1axsC40EKvwjyH1ZB2e15gCNEJh1m4=",
|
| 16 |
"test.json": "Cmo87nuUyMyjpettO/Nks1mu7Z00DCEnYyaHf5fQNsI="
|
| 17 |
}
|
| 18 |
},
|
| 19 |
+
"provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } },
|
| 20 |
"webgpu": {
|
| 21 |
+
"manifestSpec": "2.1",
|
| 22 |
"variants": {
|
| 23 |
"nearest_integer_scale_vec4": ["resize-nearest-integer-scale.wgsl.jinja"],
|
| 24 |
"nearest_integer_scale": ["resize-nearest-integer-scale.wgsl.jinja"],
|
build/webgpu/resize-coord-transform.wgsl.jinja
CHANGED
|
@@ -3,8 +3,11 @@
|
|
| 3 |
// Nearest-index rounding is load-bearing for ONNX compatibility:
|
| 4 |
// round_prefer_ceil ties toward positive infinity, while round_prefer_floor
|
| 5 |
// uses its specified epsilon.
|
| 6 |
-
{%
|
| 7 |
-
{% set
|
|
|
|
|
|
|
|
|
|
| 8 |
{{ env.wgsl.resourceDeclarations }}
|
| 9 |
{% set wg = workgroupSize if workgroupSize is defined else tunables.WORKGROUP_SIZE %}
|
| 10 |
|
|
@@ -15,10 +18,12 @@ fn source_coord(out_coord: u32, in_size: u32, out_size: u32, explicit_scale: f32
|
|
| 15 |
// 3.4999998 after double rounding. An explicit scale uses coord/scale.
|
| 16 |
return select(f32(out_coord) * f32(in_size) / f32(out_size), f32(out_coord) / explicit_scale, explicit_scale > 0.0);
|
| 17 |
}
|
|
|
|
|
|
|
| 18 |
/* Backend-stable nearest-neighbor coordinate selection. Inferred align-corners,
|
| 19 |
* asymmetric, and half-pixel transforms use exact integer ratios. This matters
|
| 20 |
* at nearest-mode ties: reassociating an f32 division into a reciprocal
|
| 21 |
-
* multiply can move an exact 0.5 to either side of the boundary. */
|
| 22 |
fn nearest_index(v: f32) -> i32 {
|
| 23 |
return i32(floor(v));
|
| 24 |
}
|
|
@@ -55,18 +60,15 @@ fn nearest_coord_index(
|
|
| 55 |
}
|
| 56 |
return nearest_index(source_coord(out_coord, in_size, out_size, explicit_scale));
|
| 57 |
}
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
{% else %}
|
| 61 |
-
|
| 62 |
-
{% endif -%}
|
| 63 |
fn input_at(base: u32, h: i32, w: i32) -> f32 {
|
| 64 |
let hh = u32(clamp(h, 0, i32(params.inH) - 1));
|
| 65 |
let ww = u32(clamp(w, 0, i32(params.inW) - 1));
|
| 66 |
return f32(x[base + hh * params.inW + ww]);
|
| 67 |
}
|
| 68 |
-
|
| 69 |
{% if modeSpec == "linear" %}
|
|
|
|
| 70 |
// Clamp before choosing neighbors. At an edge both neighbors coincide and
|
| 71 |
// receive half weight; the four-term order is observable for integer outputs.
|
| 72 |
fn bilinear_sample(base: u32, source_y: f32, source_x: f32) -> f32 {
|
|
@@ -92,19 +94,12 @@ fn bilinear_sample(base: u32, source_y: f32, source_x: f32) -> f32 {
|
|
| 92 |
|
| 93 |
@compute @workgroup_size({{ wg }})
|
| 94 |
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
| 95 |
-
|
| 96 |
-
// per-axis dispatch fold width.
|
| 97 |
-
let flat = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wg }}u;
|
| 98 |
-
{% if scalesBuffer %}
|
| 99 |
// Read spatial scales from NCHW axes 2 and 3. Inferring a scale from the
|
| 100 |
// rounded output extent would discard its fractional part; for example,
|
| 101 |
// input 2 scaled by 2.333 has output extent 4, whose extent ratio is 2.0.
|
| 102 |
let scaleH_val = f32(scales[2]);
|
| 103 |
let scaleW_val = f32(scales[3]);
|
| 104 |
-
{% else %}
|
| 105 |
-
let scaleH_val = params.scaleH;
|
| 106 |
-
let scaleW_val = params.scaleW;
|
| 107 |
-
{% endif %}
|
| 108 |
{% if vec4 %}
|
| 109 |
// One invocation owns four contiguous output columns. Because outW is
|
| 110 |
// divisible by four, the group never crosses a row; all lanes share n, c,
|
|
@@ -140,45 +135,6 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
| 140 |
bilinear_sample(base, sy, sx3));
|
| 141 |
{% endif %}
|
| 142 |
y[flat] = {{ vecType }}(r);
|
| 143 |
-
{% elif scalarX4 %}
|
| 144 |
-
// Each invocation computes four row-local columns without a vec4 storage
|
| 145 |
-
// binding. The final one to three columns use guarded scalar stores.
|
| 146 |
-
let groups_per_row = (params.outW + 3u) / 4u;
|
| 147 |
-
let group_count = (params.count / params.outW) * groups_per_row;
|
| 148 |
-
if (flat >= group_count) { return; }
|
| 149 |
-
let group_in_row = flat % groups_per_row;
|
| 150 |
-
let row = flat / groups_per_row;
|
| 151 |
-
let ow0 = group_in_row * 4u;
|
| 152 |
-
let oh = row % params.outH;
|
| 153 |
-
let c = (row / params.outH) % params.C;
|
| 154 |
-
let n = row / (params.outH * params.C);
|
| 155 |
-
let base = (n * params.C + c) * params.inH * params.inW;
|
| 156 |
-
{% if modeSpec != "nearest" %}
|
| 157 |
-
let sy = source_coord(oh, params.inH, params.outH, scaleH_val);
|
| 158 |
-
let sx0 = source_coord(ow0, params.inW, params.outW, scaleW_val);
|
| 159 |
-
let sx1 = source_coord(ow0 + 1u, params.inW, params.outW, scaleW_val);
|
| 160 |
-
let sx2 = source_coord(ow0 + 2u, params.inW, params.outW, scaleW_val);
|
| 161 |
-
let sx3 = source_coord(ow0 + 3u, params.inW, params.outW, scaleW_val);
|
| 162 |
-
{% endif %}
|
| 163 |
-
{% if modeSpec == "nearest" %}
|
| 164 |
-
let ih = nearest_coord_index(oh, params.inH, params.outH, scaleH_val);
|
| 165 |
-
let r = vec4<f32>(
|
| 166 |
-
input_at(base, ih, nearest_coord_index(ow0, params.inW, params.outW, scaleW_val)),
|
| 167 |
-
input_at(base, ih, nearest_coord_index(ow0 + 1u, params.inW, params.outW, scaleW_val)),
|
| 168 |
-
input_at(base, ih, nearest_coord_index(ow0 + 2u, params.inW, params.outW, scaleW_val)),
|
| 169 |
-
input_at(base, ih, nearest_coord_index(ow0 + 3u, params.inW, params.outW, scaleW_val)));
|
| 170 |
-
{% else %}
|
| 171 |
-
let r = vec4<f32>(
|
| 172 |
-
bilinear_sample(base, sy, sx0),
|
| 173 |
-
bilinear_sample(base, sy, sx1),
|
| 174 |
-
bilinear_sample(base, sy, sx2),
|
| 175 |
-
bilinear_sample(base, sy, sx3));
|
| 176 |
-
{% endif %}
|
| 177 |
-
let i = row * params.outW + ow0;
|
| 178 |
-
y[i] = {{ scalar }}(r.x);
|
| 179 |
-
if (ow0 + 1u < params.outW) { y[i + 1u] = {{ scalar }}(r.y); }
|
| 180 |
-
if (ow0 + 2u < params.outW) { y[i + 2u] = {{ scalar }}(r.z); }
|
| 181 |
-
if (ow0 + 3u < params.outW) { y[i + 3u] = {{ scalar }}(r.w); }
|
| 182 |
{% else %}
|
| 183 |
let i = flat;
|
| 184 |
if (i >= params.count) { return; }
|
|
|
|
| 3 |
// Nearest-index rounding is load-bearing for ONNX compatibility:
|
| 4 |
// round_prefer_ceil ties toward positive infinity, while round_prefer_floor
|
| 5 |
// uses its specified epsilon.
|
| 6 |
+
{% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}
|
| 7 |
+
{% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
|
| 8 |
+
// 2D-folded flat index: gid.y carries the high bits past the dispatch's
|
| 9 |
+
// per-axis workgroup fold width.
|
| 10 |
+
let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};{% endmacro %}
|
| 11 |
{{ env.wgsl.resourceDeclarations }}
|
| 12 |
{% set wg = workgroupSize if workgroupSize is defined else tunables.WORKGROUP_SIZE %}
|
| 13 |
|
|
|
|
| 18 |
// 3.4999998 after double rounding. An explicit scale uses coord/scale.
|
| 19 |
return select(f32(out_coord) * f32(in_size) / f32(out_size), f32(out_coord) / explicit_scale, explicit_scale > 0.0);
|
| 20 |
}
|
| 21 |
+
{% if modeSpec == "nearest" %}
|
| 22 |
+
|
| 23 |
/* Backend-stable nearest-neighbor coordinate selection. Inferred align-corners,
|
| 24 |
* asymmetric, and half-pixel transforms use exact integer ratios. This matters
|
| 25 |
* at nearest-mode ties: reassociating an f32 division into a reciprocal
|
| 26 |
+
* multiply can move an exact 0.5 to either side of the boundary. */
|
| 27 |
fn nearest_index(v: f32) -> i32 {
|
| 28 |
return i32(floor(v));
|
| 29 |
}
|
|
|
|
| 60 |
}
|
| 61 |
return nearest_index(source_coord(out_coord, in_size, out_size, explicit_scale));
|
| 62 |
}
|
| 63 |
+
{% endif %}
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
fn input_at(base: u32, h: i32, w: i32) -> f32 {
|
| 66 |
let hh = u32(clamp(h, 0, i32(params.inH) - 1));
|
| 67 |
let ww = u32(clamp(w, 0, i32(params.inW) - 1));
|
| 68 |
return f32(x[base + hh * params.inW + ww]);
|
| 69 |
}
|
|
|
|
| 70 |
{% if modeSpec == "linear" %}
|
| 71 |
+
|
| 72 |
// Clamp before choosing neighbors. At an edge both neighbors coincide and
|
| 73 |
// receive half weight; the four-term order is observable for integer outputs.
|
| 74 |
fn bilinear_sample(base: u32, source_y: f32, source_x: f32) -> f32 {
|
|
|
|
| 94 |
|
| 95 |
@compute @workgroup_size({{ wg }})
|
| 96 |
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
| 97 |
+
{{ flat_index_2d(wg, "flat", "") }}
|
|
|
|
|
|
|
|
|
|
| 98 |
// Read spatial scales from NCHW axes 2 and 3. Inferring a scale from the
|
| 99 |
// rounded output extent would discard its fractional part; for example,
|
| 100 |
// input 2 scaled by 2.333 has output extent 4, whose extent ratio is 2.0.
|
| 101 |
let scaleH_val = f32(scales[2]);
|
| 102 |
let scaleW_val = f32(scales[3]);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
{% if vec4 %}
|
| 104 |
// One invocation owns four contiguous output columns. Because outW is
|
| 105 |
// divisible by four, the group never crosses a row; all lanes share n, c,
|
|
|
|
| 135 |
bilinear_sample(base, sy, sx3));
|
| 136 |
{% endif %}
|
| 137 |
y[flat] = {{ vecType }}(r);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
{% else %}
|
| 139 |
let i = flat;
|
| 140 |
if (i >= params.count) { return; }
|
build/webgpu/resize-generic.wgsl.jinja
CHANGED
|
@@ -1,20 +1,17 @@
|
|
| 1 |
-
{% set coeffA =
|
| 2 |
// Rank-generic interpolation for arbitrary ranks and resized axes. Scales are
|
| 3 |
// read from the input buffer, and linear interpolation runs only over resized
|
| 4 |
// axes; passthrough axes contribute one tap.
|
| 5 |
{{ env.wgsl.resourceDeclarations }}
|
| 6 |
{% set wg = workgroupSize if workgroupSize is defined else tunables.WORKGROUP_SIZE %}
|
| 7 |
-
{%
|
| 8 |
{% set ys = yShape %}
|
| 9 |
-
{% set r = xs | length
|
| 10 |
-
{% set usesExplicitScale = coordinateMode not in ["align_corners", "tf_crop_and_resize"] %}
|
| 11 |
-
|
| 12 |
{% set resolvedScales = namespace(values = []) %}
|
| 13 |
{% for a in range(r) %}
|
| 14 |
{% set axisScale = namespace(value = 0) %}
|
| 15 |
{% set resolvedScales.values = resolvedScales.values + [axisScale.value] %}
|
| 16 |
{% endfor %}
|
| 17 |
-
|
| 18 |
{% set roiStarts = namespace(values = []) %}
|
| 19 |
{% set roiEnds = namespace(values = []) %}
|
| 20 |
{% for a in range(r) %}
|
|
@@ -23,35 +20,32 @@
|
|
| 23 |
{% set roiEnds.values = roiEnds.values + [bounds.end] %}
|
| 24 |
{% endfor %}
|
| 25 |
{% set ocount = namespace(v = 1) %}
|
| 26 |
-
{% for a in range(r) %}{% set ocount.v = ocount.v * ys[a] %}{% endfor %}
|
|
|
|
| 27 |
{% for a in range(r) %}
|
| 28 |
{% set resized.list = resized.list + [a] %}
|
| 29 |
{% endfor %}
|
| 30 |
{% set k = resized.list | length %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
const WG: u32 = {{ wg }}u;
|
|
|
|
| 33 |
|
| 34 |
// Runtime coordinate form for nearest interpolation when an exact
|
| 35 |
// inferred-ratio expression is unavailable.
|
| 36 |
-
{% if modeSpec == "nearest" and k > 0 and (roi is not defined or (roi | length) == 0) and coordinateMode != "align_corners" %}
|
| 37 |
-
|
| 38 |
fn source_coord(out_coord: u32, in_size: u32, out_size: u32, explicit_scale: f32) -> f32 {
|
| 39 |
return select(f32(out_coord) * f32(in_size) / f32(out_size), f32(out_coord) / explicit_scale, explicit_scale > 0.0);
|
| 40 |
}
|
| 41 |
-
{%
|
| 42 |
-
{%
|
| 43 |
-
{% set s = namespace(v = 1) %}
|
| 44 |
-
{% for j in range(a + 1, r) %}{% set s.v = s.v * xs[j] %}{% endfor %}
|
| 45 |
-
{{ s.v }}
|
| 46 |
-
{%- endmacro %}
|
| 47 |
-
|
| 48 |
-
{% macro out_coord(a) -%}
|
| 49 |
-
{% if ys[a] == 1 -%}0u{% else -%}oc{{ a }}{% endif -%}
|
| 50 |
-
{% endmacro %}{% macro src_coord(a, oc) %}
|
| 51 |
-
(f32({{ oc }}) / scales[{{ a }}])
|
| 52 |
-
{% endmacro %}
|
| 53 |
|
| 54 |
-
{% if modeSpec == "cubic" %}
|
| 55 |
// Keys cubic convolution kernel with coefficient coeffA.
|
| 56 |
fn cubic_weight(s_in: f32) -> f32 {
|
| 57 |
let a = f32({{ coeffA }});
|
|
@@ -63,21 +57,17 @@ fn cubic_weight(s_in: f32) -> f32 {
|
|
| 63 |
return 0.0;
|
| 64 |
}
|
| 65 |
{% endif %}
|
| 66 |
-
|
| 67 |
{% if modeSpec == "nearest" and k > 0 %}
|
|
|
|
|
|
|
| 68 |
/* Backend-stable nearest-neighbor coordinate selection. Inferred align-corners,
|
| 69 |
* asymmetric, and half-pixel transforms use exact integer ratios. This matters
|
| 70 |
* at nearest-mode ties: reassociating an f32 division into a reciprocal
|
| 71 |
-
* multiply can move an exact 0.5 to either side of the boundary. */
|
| 72 |
-
{% set inferredHalfPixelMode = coordinateMode in ["pytorch_half_pixel", "half_pixel", "half_pixel_symmetric"] %}
|
| 73 |
-
{% set noRoi = roi is not defined or (roi | length) == 0 %}
|
| 74 |
-
{% set usesExplicitScale = coordinateMode not in ["align_corners", "tf_crop_and_resize"] %}
|
| 75 |
-
{% if modeSpec == "nearest" %}
|
| 76 |
fn nearest_index(v: f32) -> i32 {
|
| 77 |
return i32(floor(v));
|
| 78 |
}
|
| 79 |
|
| 80 |
-
{% if noRoi and (ratioMode or inferredHalfPixelMode) %}
|
| 81 |
fn rational_value_nearest_index(numerator: u32, denominator: u32) -> i32 {
|
| 82 |
if (numerator == 0u || denominator == 0u) {
|
| 83 |
return 0;
|
|
@@ -86,8 +76,6 @@ fn rational_value_nearest_index(numerator: u32, denominator: u32) -> i32 {
|
|
| 86 |
return i32(lower);
|
| 87 |
}
|
| 88 |
|
| 89 |
-
{% endif %}
|
| 90 |
-
{% if noRoi and ratioMode %}
|
| 91 |
fn rational_nearest_index(coord: u32, numerator: u32, denominator: u32) -> i32 {
|
| 92 |
if (coord == 0u || numerator == 0u || denominator == 0u) {
|
| 93 |
return 0;
|
|
@@ -101,56 +89,18 @@ fn rational_nearest_index(coord: u32, numerator: u32, denominator: u32) -> i32 {
|
|
| 101 |
return rational_value_nearest_index(coord * numerator, denominator);
|
| 102 |
}
|
| 103 |
|
| 104 |
-
{% endif %}
|
| 105 |
-
{% if noRoi and inferredHalfPixelMode %}
|
| 106 |
-
fn inferred_half_pixel_nearest_index(
|
| 107 |
-
coord: u32,
|
| 108 |
-
in_size: u32,
|
| 109 |
-
out_size: u32,
|
| 110 |
-
subtract_half: bool
|
| 111 |
-
) -> i32 {
|
| 112 |
-
// (coord + 1/2) * in/out [- 1/2]
|
| 113 |
-
// = ((2*coord + 1)*in [- out]) / (2*out)
|
| 114 |
-
// Use the f32 definition only when the u32 intermediates would overflow.
|
| 115 |
-
if (in_size == 0u || out_size == 0u || out_size > 0x7fffffffu ||
|
| 116 |
-
coord > (0xffffffffu - 1u) / 2u) {
|
| 117 |
-
return nearest_index(source_coord(coord, in_size, out_size, 0.0));
|
| 118 |
-
}
|
| 119 |
-
let odd_coord = coord * 2u + 1u;
|
| 120 |
-
// 0xfffffffe, not 0xffffffff — see rational_nearest_index (NVVM crash).
|
| 121 |
-
if (odd_coord > 0xfffffffeu / in_size) {
|
| 122 |
-
return nearest_index(source_coord(coord, in_size, out_size, 0.0));
|
| 123 |
-
}
|
| 124 |
-
let scaled = odd_coord * in_size;
|
| 125 |
-
let shift = select(0u, out_size, subtract_half);
|
| 126 |
-
// A negative half-pixel coordinate is in [-0.5, 0). Every nearest mode may
|
| 127 |
-
// choose -1 or 0 there, but Resize clamps both to source index zero.
|
| 128 |
-
if (scaled <= shift) {
|
| 129 |
-
return 0;
|
| 130 |
-
}
|
| 131 |
-
return rational_value_nearest_index(scaled - shift, out_size * 2u);
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
{% endif %}
|
| 135 |
-
{% if noRoi %}
|
| 136 |
fn nearest_coord_index(
|
| 137 |
out_coord: u32,
|
| 138 |
in_size: u32,
|
| 139 |
-
out_size: u32
|
| 140 |
explicit_scale: f32
|
| 141 |
-
{% endif %}
|
| 142 |
) -> i32 {
|
| 143 |
if (explicit_scale <= 0.0) {
|
| 144 |
return rational_nearest_index(out_coord, in_size, out_size);
|
| 145 |
}
|
| 146 |
-
return nearest_index(source_coord(out_coord, in_size, out_size
|
| 147 |
}
|
| 148 |
-
|
| 149 |
{% endif %}
|
| 150 |
-
|
| 151 |
-
{% else %}
|
| 152 |
-
|
| 153 |
-
{% endif -%}
|
| 154 |
{% endif %}
|
| 155 |
|
| 156 |
@compute @workgroup_size({{ wg }})
|
|
@@ -172,14 +122,14 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups)
|
|
| 172 |
// Per-axis source coordinates.
|
| 173 |
{% for a in range(r) %}
|
| 174 |
{% if a in resized.list and (modeSpec != "nearest" or (roi is defined and roi)) %}
|
| 175 |
-
let s{{ a }} = {{ src_coord(a, out_coord(a)) }};
|
| 176 |
{% endif %}
|
| 177 |
{% endfor %}
|
| 178 |
{% if modeSpec == "nearest" %}
|
| 179 |
{% for a in range(r) %}
|
| 180 |
{% if a in resized.list %}
|
| 181 |
{% set explicitScale = "f32(scales[" ~ a ~ "])" %}
|
| 182 |
-
let idx{{ a }} = u32(clamp(nearest_coord_index({{ out_coord(a) }}, {{ xs[a] }}u, {{ ys[a] }}u
|
| 183 |
{% else %}
|
| 184 |
let idx{{ a }} = {{ out_coord(a) }};
|
| 185 |
{% endif %}
|
|
|
|
| 1 |
+
{% set coeffA = 0 %}
|
| 2 |
// Rank-generic interpolation for arbitrary ranks and resized axes. Scales are
|
| 3 |
// read from the input buffer, and linear interpolation runs only over resized
|
| 4 |
// axes; passthrough axes contribute one tap.
|
| 5 |
{{ env.wgsl.resourceDeclarations }}
|
| 6 |
{% set wg = workgroupSize if workgroupSize is defined else tunables.WORKGROUP_SIZE %}
|
| 7 |
+
{% set xs = xShape %}
|
| 8 |
{% set ys = yShape %}
|
| 9 |
+
{% set r = xs | length %}
|
|
|
|
|
|
|
| 10 |
{% set resolvedScales = namespace(values = []) %}
|
| 11 |
{% for a in range(r) %}
|
| 12 |
{% set axisScale = namespace(value = 0) %}
|
| 13 |
{% set resolvedScales.values = resolvedScales.values + [axisScale.value] %}
|
| 14 |
{% endfor %}
|
|
|
|
| 15 |
{% set roiStarts = namespace(values = []) %}
|
| 16 |
{% set roiEnds = namespace(values = []) %}
|
| 17 |
{% for a in range(r) %}
|
|
|
|
| 20 |
{% set roiEnds.values = roiEnds.values + [bounds.end] %}
|
| 21 |
{% endfor %}
|
| 22 |
{% set ocount = namespace(v = 1) %}
|
| 23 |
+
{% for a in range(r) %}{% set ocount.v = ocount.v * ys[a] %}{% endfor %}
|
| 24 |
+
{% set resized = namespace(list = []) %}
|
| 25 |
{% for a in range(r) %}
|
| 26 |
{% set resized.list = resized.list + [a] %}
|
| 27 |
{% endfor %}
|
| 28 |
{% set k = resized.list | length %}
|
| 29 |
+
{% macro istride(a) %}
|
| 30 |
+
{% set s = namespace(v = 1) %}
|
| 31 |
+
{% for j in range(a + 1, r) %}{% set s.v = s.v * xs[j] %}{% endfor %}
|
| 32 |
+
{{ s.v }}{% endmacro %}
|
| 33 |
+
{% macro out_coord(a) %}{% if ys[a] == 1 %}0u{% else %}oc{{ a }}{% endif %}{% endmacro %}
|
| 34 |
+
{% macro src_coord(a, oc) %}
|
| 35 |
+
(f32({{ oc }}) / scales[{{ a }}])
|
| 36 |
+
{% endmacro %}
|
| 37 |
|
| 38 |
const WG: u32 = {{ wg }}u;
|
| 39 |
+
{% if modeSpec == "nearest" and k > 0 and (roi is not defined or (roi | length) == 0) and coordinateMode != "align_corners" %}
|
| 40 |
|
| 41 |
// Runtime coordinate form for nearest interpolation when an exact
|
| 42 |
// inferred-ratio expression is unavailable.
|
|
|
|
|
|
|
| 43 |
fn source_coord(out_coord: u32, in_size: u32, out_size: u32, explicit_scale: f32) -> f32 {
|
| 44 |
return select(f32(out_coord) * f32(in_size) / f32(out_size), f32(out_coord) / explicit_scale, explicit_scale > 0.0);
|
| 45 |
}
|
| 46 |
+
{% endif %}
|
| 47 |
+
{% if modeSpec == "cubic" and k > 0 %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
| 49 |
// Keys cubic convolution kernel with coefficient coeffA.
|
| 50 |
fn cubic_weight(s_in: f32) -> f32 {
|
| 51 |
let a = f32({{ coeffA }});
|
|
|
|
| 57 |
return 0.0;
|
| 58 |
}
|
| 59 |
{% endif %}
|
|
|
|
| 60 |
{% if modeSpec == "nearest" and k > 0 %}
|
| 61 |
+
{% if modeSpec == "nearest" %}
|
| 62 |
+
|
| 63 |
/* Backend-stable nearest-neighbor coordinate selection. Inferred align-corners,
|
| 64 |
* asymmetric, and half-pixel transforms use exact integer ratios. This matters
|
| 65 |
* at nearest-mode ties: reassociating an f32 division into a reciprocal
|
| 66 |
+
* multiply can move an exact 0.5 to either side of the boundary. */
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
fn nearest_index(v: f32) -> i32 {
|
| 68 |
return i32(floor(v));
|
| 69 |
}
|
| 70 |
|
|
|
|
| 71 |
fn rational_value_nearest_index(numerator: u32, denominator: u32) -> i32 {
|
| 72 |
if (numerator == 0u || denominator == 0u) {
|
| 73 |
return 0;
|
|
|
|
| 76 |
return i32(lower);
|
| 77 |
}
|
| 78 |
|
|
|
|
|
|
|
| 79 |
fn rational_nearest_index(coord: u32, numerator: u32, denominator: u32) -> i32 {
|
| 80 |
if (coord == 0u || numerator == 0u || denominator == 0u) {
|
| 81 |
return 0;
|
|
|
|
| 89 |
return rational_value_nearest_index(coord * numerator, denominator);
|
| 90 |
}
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
fn nearest_coord_index(
|
| 93 |
out_coord: u32,
|
| 94 |
in_size: u32,
|
| 95 |
+
out_size: u32,
|
| 96 |
explicit_scale: f32
|
|
|
|
| 97 |
) -> i32 {
|
| 98 |
if (explicit_scale <= 0.0) {
|
| 99 |
return rational_nearest_index(out_coord, in_size, out_size);
|
| 100 |
}
|
| 101 |
+
return nearest_index(source_coord(out_coord, in_size, out_size, explicit_scale));
|
| 102 |
}
|
|
|
|
| 103 |
{% endif %}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
{% endif %}
|
| 105 |
|
| 106 |
@compute @workgroup_size({{ wg }})
|
|
|
|
| 122 |
// Per-axis source coordinates.
|
| 123 |
{% for a in range(r) %}
|
| 124 |
{% if a in resized.list and (modeSpec != "nearest" or (roi is defined and roi)) %}
|
| 125 |
+
let s{{ a }} = {{ src_coord(a, out_coord(a)) | trim }};
|
| 126 |
{% endif %}
|
| 127 |
{% endfor %}
|
| 128 |
{% if modeSpec == "nearest" %}
|
| 129 |
{% for a in range(r) %}
|
| 130 |
{% if a in resized.list %}
|
| 131 |
{% set explicitScale = "f32(scales[" ~ a ~ "])" %}
|
| 132 |
+
let idx{{ a }} = u32(clamp(nearest_coord_index({{ out_coord(a) }}, {{ xs[a] }}u, {{ ys[a] }}u, {{ explicitScale }}), 0, {{ xs[a] - 1 }}));
|
| 133 |
{% else %}
|
| 134 |
let idx{{ a }} = {{ out_coord(a) }};
|
| 135 |
{% endif %}
|
build/webgpu/resize-linear-2x-stencil.wgsl.jinja
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
{% set scalesBuffer = scalesBuffer if scalesBuffer is defined else false %}
|
| 2 |
// NCHW bilinear 2x upscale on both spatial axes. The
|
| 3 |
// interpolation weights collapse to fixed per-parity stencils, replacing
|
| 4 |
// per-element floating-point coordinate divisions with integer parity tests
|
|
@@ -25,7 +24,6 @@ fn input_at(base: u32, h: i32, w: i32) -> f32 {
|
|
| 25 |
let ww = u32(clamp(w, 0, i32(IN_W) - 1));
|
| 26 |
return f32(x[base + hh * IN_W + ww]);
|
| 27 |
}
|
| 28 |
-
{% if scalesBuffer %}
|
| 29 |
|
| 30 |
// The output shape condition floor(in * scale) == 2 * in also admits scales
|
| 31 |
// slightly above 2. Read the spatial scales: exactly 2 uses the stencils;
|
|
@@ -49,7 +47,6 @@ fn general_sample(base: u32, oh: u32, ow: u32) -> f32 {
|
|
| 49 |
let v11 = input_at(base, y1, x1);
|
| 50 |
return wx0 * wy0 * v00 + wx1 * wy0 * v01 + wx0 * wy1 * v10 + wx1 * wy1 * v11;
|
| 51 |
}
|
| 52 |
-
{% endif %}
|
| 53 |
|
| 54 |
@compute @workgroup_size({{ wg }})
|
| 55 |
fn main(
|
|
@@ -57,9 +54,7 @@ fn main(
|
|
| 57 |
@builtin(num_workgroups) nwg: vec3<u32>
|
| 58 |
) {
|
| 59 |
let stride = nwg.x * WG;
|
| 60 |
-
{% if scalesBuffer %}
|
| 61 |
let exact2x = f32(scales[2]) == 2.0 && f32(scales[3]) == 2.0;
|
| 62 |
-
{% endif %}
|
| 63 |
{% if vec4 %}
|
| 64 |
// Vec4 path: one thread owns 4 contiguous output columns (OUT_W % 4 == 0, so
|
| 65 |
// a 4-group never crosses a row). All 4 lanes share the same oh -> same source
|
|
@@ -76,7 +71,6 @@ fn main(
|
|
| 76 |
let nc = tt / OUT_H;
|
| 77 |
let base = nc * IN_HW;
|
| 78 |
let ow0 = ow4 * 4u;
|
| 79 |
-
{% if scalesBuffer %}
|
| 80 |
if (!exact2x) {
|
| 81 |
y[v] = vec4<{{ scalar }}>(vec4<f32>(
|
| 82 |
general_sample(base, oh, ow0),
|
|
@@ -85,7 +79,6 @@ fn main(
|
|
| 85 |
general_sample(base, oh, ow0 + 3u)));
|
| 86 |
continue;
|
| 87 |
}
|
| 88 |
-
{% endif %}
|
| 89 |
let qx0 = i32(ow0 / 2u);
|
| 90 |
let y0 = i32(oh / 2u);
|
| 91 |
let wy = select(0.0, 0.5, (oh & 1u) == 1u);
|
|
@@ -110,12 +103,10 @@ fn main(
|
|
| 110 |
let oh = t % OUT_H;
|
| 111 |
let nc = t / OUT_H;
|
| 112 |
let base = nc * IN_HW;
|
| 113 |
-
{% if scalesBuffer %}
|
| 114 |
if (!exact2x) {
|
| 115 |
y[i] = {{ scalar }}(general_sample(base, oh, ow));
|
| 116 |
continue;
|
| 117 |
}
|
| 118 |
-
{% endif %}
|
| 119 |
let qy = i32(oh / 2u);
|
| 120 |
let qx = i32(ow / 2u);
|
| 121 |
let oddY = (oh & 1u) == 1u;
|
|
|
|
|
|
|
| 1 |
// NCHW bilinear 2x upscale on both spatial axes. The
|
| 2 |
// interpolation weights collapse to fixed per-parity stencils, replacing
|
| 3 |
// per-element floating-point coordinate divisions with integer parity tests
|
|
|
|
| 24 |
let ww = u32(clamp(w, 0, i32(IN_W) - 1));
|
| 25 |
return f32(x[base + hh * IN_W + ww]);
|
| 26 |
}
|
|
|
|
| 27 |
|
| 28 |
// The output shape condition floor(in * scale) == 2 * in also admits scales
|
| 29 |
// slightly above 2. Read the spatial scales: exactly 2 uses the stencils;
|
|
|
|
| 47 |
let v11 = input_at(base, y1, x1);
|
| 48 |
return wx0 * wy0 * v00 + wx1 * wy0 * v01 + wx0 * wy1 * v10 + wx1 * wy1 * v11;
|
| 49 |
}
|
|
|
|
| 50 |
|
| 51 |
@compute @workgroup_size({{ wg }})
|
| 52 |
fn main(
|
|
|
|
| 54 |
@builtin(num_workgroups) nwg: vec3<u32>
|
| 55 |
) {
|
| 56 |
let stride = nwg.x * WG;
|
|
|
|
| 57 |
let exact2x = f32(scales[2]) == 2.0 && f32(scales[3]) == 2.0;
|
|
|
|
| 58 |
{% if vec4 %}
|
| 59 |
// Vec4 path: one thread owns 4 contiguous output columns (OUT_W % 4 == 0, so
|
| 60 |
// a 4-group never crosses a row). All 4 lanes share the same oh -> same source
|
|
|
|
| 71 |
let nc = tt / OUT_H;
|
| 72 |
let base = nc * IN_HW;
|
| 73 |
let ow0 = ow4 * 4u;
|
|
|
|
| 74 |
if (!exact2x) {
|
| 75 |
y[v] = vec4<{{ scalar }}>(vec4<f32>(
|
| 76 |
general_sample(base, oh, ow0),
|
|
|
|
| 79 |
general_sample(base, oh, ow0 + 3u)));
|
| 80 |
continue;
|
| 81 |
}
|
|
|
|
| 82 |
let qx0 = i32(ow0 / 2u);
|
| 83 |
let y0 = i32(oh / 2u);
|
| 84 |
let wy = select(0.0, 0.5, (oh & 1u) == 1u);
|
|
|
|
| 103 |
let oh = t % OUT_H;
|
| 104 |
let nc = t / OUT_H;
|
| 105 |
let base = nc * IN_HW;
|
|
|
|
| 106 |
if (!exact2x) {
|
| 107 |
y[i] = {{ scalar }}(general_sample(base, oh, ow));
|
| 108 |
continue;
|
| 109 |
}
|
|
|
|
| 110 |
let qy = i32(oh / 2u);
|
| 111 |
let qx = i32(ow / 2u);
|
| 112 |
let oddY = (oh & 1u) == 1u;
|
build/webgpu/resize-nearest-integer-scale.wgsl.jinja
CHANGED
|
@@ -9,7 +9,7 @@
|
|
| 9 |
// other coordinate-mode combinations require the generic coordinate transform.
|
| 10 |
{{ env.wgsl.resourceDeclarations }}
|
| 11 |
{% set wg = workgroupSize if workgroupSize is defined else tunables.WORKGROUP_SIZE %}
|
| 12 |
-
{%
|
| 13 |
{% set inW = xShape[3] %}
|
| 14 |
{% set outH = yShape[2] %}
|
| 15 |
{% set outW = yShape[3] %}
|
|
@@ -20,7 +20,8 @@ const IN_W: u32 = {{ inW }}u;
|
|
| 20 |
const IN_HW: u32 = {{ inH * inW }}u;
|
| 21 |
const OUT_H: u32 = {{ outH }}u;
|
| 22 |
const SCALE_H: u32 = {{ ((outH / inH) | int) }}u;
|
| 23 |
-
{% if not vec4 or scaleW == 3 %}
|
|
|
|
| 24 |
{% endif %}
|
| 25 |
{% if vec4 %}
|
| 26 |
const OUT_W_VECS: u32 = {{ ((outW / 4) | int) }}u;
|
|
|
|
| 9 |
// other coordinate-mode combinations require the generic coordinate transform.
|
| 10 |
{{ env.wgsl.resourceDeclarations }}
|
| 11 |
{% set wg = workgroupSize if workgroupSize is defined else tunables.WORKGROUP_SIZE %}
|
| 12 |
+
{% set inH = xShape[2] %}
|
| 13 |
{% set inW = xShape[3] %}
|
| 14 |
{% set outH = yShape[2] %}
|
| 15 |
{% set outW = yShape[3] %}
|
|
|
|
| 20 |
const IN_HW: u32 = {{ inH * inW }}u;
|
| 21 |
const OUT_H: u32 = {{ outH }}u;
|
| 22 |
const SCALE_H: u32 = {{ ((outH / inH) | int) }}u;
|
| 23 |
+
{% if not vec4 or scaleW == 3 %}
|
| 24 |
+
const SCALE_W: u32 = {{ scaleW }}u;
|
| 25 |
{% endif %}
|
| 26 |
{% if vec4 %}
|
| 27 |
const OUT_W_VECS: u32 = {{ ((outW / 4) | int) }}u;
|