Xenova HF Staff commited on
Commit
504ada0
·
verified ·
1 Parent(s): a44211c

sync c928d21e6cc1

Browse files
build/webgpu/bias-add.wgsl.jinja CHANGED
@@ -1,3 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  {% macro flat_index_2d(name="i", bound="params.count", guardInline=false, note="dispatch-limit") %}
2
  {% if note == "dispatch-limit" %}
3
  // 2D-folded flat index: gid.y carries the high bits past the
@@ -48,10 +73,10 @@ enable f16;
48
  // for one buffer would invalidate the command buffer.
49
  const HIDDEN: u32 = {{ hidden }}u;
50
 
 
51
  @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
52
  fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {
53
  {{ flat_index_2d() }}
54
- {% if vec4 %}
55
  let base = i * 4u;
56
  let xv = vec4<f32>(f32(x[base]), f32(x[base + 1u]), f32(x[base + 2u]), f32(x[base + 3u]));
57
  let sv = vec4<f32>(f32(skip[base]), f32(skip[base + 1u]), f32(skip[base + 2u]), f32(skip[base + 3u]));
@@ -59,10 +84,13 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups)
59
  let bv = vec4<f32>(f32(bias[bcol]), f32(bias[bcol + 1u]), f32(bias[bcol + 2u]), f32(bias[bcol + 3u]));
60
  let v = xv + bv + sv;
61
  y[i] = vec4<{{ scalar }}>(v);
 
62
  {% else %}
 
63
  let xv = f32(x[i]);
64
  let sv = f32(skip[i]);
65
  let v = xv + f32(bias[i % HIDDEN]) + sv;
66
  y[i] = {{ scalar }}(v);
67
- {% endif %}
68
  }
 
 
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
+ {% if source.itemsPerInvocation is defined %}
8
+ // Tail-safe scalar x4 keeps vector-like dispatch density without requiring
9
+ // the logical tensor length (or its storage binding) to be vec4 aligned.
10
+ let begin = invocation * {{ source.itemsPerInvocation }}u;
11
+ let end = min(begin + {{ source.itemsPerInvocation }}u, params.count);
12
+ for (var i = begin; i < end; i = i + 1u) {
13
+ {%- else %}
14
+ let i = invocation;
15
+ if (i >= params.count) {
16
+ return;
17
+ }
18
+ {%- endif %}
19
+ {% endmacro %}
20
+ {% macro flat_tail_close() %}
21
+ {% if source.itemsPerInvocation is defined %}
22
+ }
23
+ {% endif %}
24
+ {% endmacro %}
25
+
26
  {% macro flat_index_2d(name="i", bound="params.count", guardInline=false, note="dispatch-limit") %}
27
  {% if note == "dispatch-limit" %}
28
  // 2D-folded flat index: gid.y carries the high bits past the
 
73
  // for one buffer would invalidate the command buffer.
74
  const HIDDEN: u32 = {{ hidden }}u;
75
 
76
+ {% if vec4 %}
77
  @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
78
  fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {
79
  {{ flat_index_2d() }}
 
80
  let base = i * 4u;
81
  let xv = vec4<f32>(f32(x[base]), f32(x[base + 1u]), f32(x[base + 2u]), f32(x[base + 3u]));
82
  let sv = vec4<f32>(f32(skip[base]), f32(skip[base + 1u]), f32(skip[base + 2u]), f32(skip[base + 3u]));
 
84
  let bv = vec4<f32>(f32(bias[bcol]), f32(bias[bcol + 1u]), f32(bias[bcol + 2u]), f32(bias[bcol + 3u]));
85
  let v = xv + bv + sv;
86
  y[i] = vec4<{{ scalar }}>(v);
87
+ }
88
  {% else %}
89
+ {{ flat_tail_open() }}
90
  let xv = f32(x[i]);
91
  let sv = f32(skip[i]);
92
  let v = xv + f32(bias[i % HIDDEN]) + sv;
93
  y[i] = {{ scalar }}(v);
94
+ {{ flat_tail_close() -}}
95
  }
96
+ {% endif %}
build/webgpu/manifest.json CHANGED
@@ -110,6 +110,7 @@
110
  "id": "main",
111
  "name": "BiasAdd.scalar",
112
  "shader": "bias-add.wgsl.jinja",
 
113
  "bindings": [
114
  {
115
  "name": "x",
@@ -140,7 +141,7 @@
140
  "struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.X)" }] }
141
  }
142
  ],
143
- "dispatch": { "threads": "numel(shapes.X)", "workgroupSize": "tunables.WORKGROUP_SIZE" }
144
  }
145
  ]
146
  }
 
110
  "id": "main",
111
  "name": "BiasAdd.scalar",
112
  "shader": "bias-add.wgsl.jinja",
113
+ "source": { "inputs": { "itemsPerInvocation": 4 } },
114
  "bindings": [
115
  {
116
  "name": "x",
 
141
  "struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.X)" }] }
142
  }
143
  ],
144
+ "dispatch": { "threads": "ceilDiv(numel(shapes.X), 4)", "workgroupSize": "tunables.WORKGROUP_SIZE" }
145
  }
146
  ]
147
  }
build/webgpu/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "com.microsoft.BiasAdd",
3
- "id": "_com_microsoft_biasadd_webgpu_e97b808",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
@@ -8,11 +8,11 @@
8
  "algorithm": "sha256",
9
  "files": {
10
  "bench.json": "W2cQSIIm7hI8MbHD6Cro9DGB/ByB035ELwNigWwGHYw=",
11
- "bias-add.wgsl.jinja": "VJnmyJ3GrZrzADYN5uP73XMs4i+YXvC39ECRQ9j40Ow=",
12
- "manifest.json": "pAy9IP3Uvl8QAonrSEw9nzg8kJxnbXeRZXoQql+sxe4=",
13
  "test.json": "PLZQbRu0fhmrRSakHOzPjTiZL0LYBLFxmBdw7gd31m4="
14
  }
15
  },
16
- "provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } },
17
  "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/com.microsoft.BiasAdd" }
18
  }
 
1
  {
2
  "name": "com.microsoft.BiasAdd",
3
+ "id": "_com_microsoft_biasadd_webgpu_40b1ba1",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
 
8
  "algorithm": "sha256",
9
  "files": {
10
  "bench.json": "W2cQSIIm7hI8MbHD6Cro9DGB/ByB035ELwNigWwGHYw=",
11
+ "bias-add.wgsl.jinja": "bZPMYyEnjQ+7kuft3Vjj/ZfXZq2hV5QcMREEyyiGt8w=",
12
+ "manifest.json": "0KqWQgIXxxbypIfz2fQy91LPsxiEoov4j8V52QlixsE=",
13
  "test.json": "PLZQbRu0fhmrRSakHOzPjTiZL0LYBLFxmBdw7gd31m4="
14
  }
15
  },
16
+ "provenance": { "kernel": { "sha": "c928d21e6cc1310861cba3bafb75f5f679ecf5f3", "dirty": false } },
17
  "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/com.microsoft.BiasAdd" }
18
  }