Xenova HF Staff commited on
Commit
b08ac42
·
verified ·
1 Parent(s): fca5e7d

sync 6fdf6301e2bb

Browse files
README.md CHANGED
@@ -51,14 +51,14 @@ Default values (overridable per request):
51
  - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, provenance)
52
  - [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)
53
  - [`test.json`](build/webgpu/test.json) — correctness cases
54
- - [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases
55
  - [`scatter-flat-copy.wgsl.jinja`](build/webgpu/scatter-flat-copy.wgsl.jinja)
56
  - [`tensor-scatter.wgsl.jinja`](build/webgpu/tensor-scatter.wgsl.jinja)
57
 
58
  ## Use with `@huggingface/kernels`
59
 
60
  ```sh
61
- npm install --save-exact @huggingface/kernels@0.0.1-preview.2
62
  ```
63
 
64
  Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
 
51
  - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, provenance)
52
  - [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)
53
  - [`test.json`](build/webgpu/test.json) — correctness cases
54
+ - [`bench.json`](build/webgpu/bench.json) — benchmark cases
55
  - [`scatter-flat-copy.wgsl.jinja`](build/webgpu/scatter-flat-copy.wgsl.jinja)
56
  - [`tensor-scatter.wgsl.jinja`](build/webgpu/tensor-scatter.wgsl.jinja)
57
 
58
  ## Use with `@huggingface/kernels`
59
 
60
  ```sh
61
+ npm install --save-exact @huggingface/kernels@0.0.1-preview.3
62
  ```
63
 
64
  Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
build/webgpu/manifest.json CHANGED
@@ -24,10 +24,10 @@
24
  "tunables": { "WORKGROUP_SIZE": { "default": 256 } },
25
  "when": ["(attrs.axis if attrs.axis >= 0 else attrs.axis + ranks.past) < ranks.past", "(attrs.axis if attrs.axis >= 0 else attrs.axis + ranks.past) >= 1", "f16Ok(dtypes.T)", "numel(shapes.past) == numel(shapes.present)", "ranks.past == ranks.present", "ranks.past == ranks.update", "ranks.past >= 2"],
26
  "bindings": {
27
- "update": { "buffer": "read-only-storage", "elementType": "$scalar" },
28
- "present": { "buffer": "storage", "elementType": "$scalar" },
29
- "params": { "buffer": "uniform", "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.update)" }] },
30
- "writeIndices": { "buffer": "read-only-storage", "elementType": "u32" }
31
  },
32
  "variants": [
33
  {
@@ -36,7 +36,6 @@
36
  "when": ["not present.writeIndices"],
37
  "derive": {
38
  "scalar": "dtypes.T",
39
- "usesF16": "dtypes.T == \"f16\"",
40
  "copyElementType": "(\"vec4<\" ~ dtypes.T ~ \">\") if numel(shapes.present) % 4 == 0 else dtypes.T"
41
  },
42
  "passes": [
@@ -88,7 +87,6 @@
88
  "when": ["present.writeIndices", "dtypes.I == \"u32\""],
89
  "derive": {
90
  "scalar": "dtypes.T",
91
- "usesF16": "dtypes.T == \"f16\"",
92
  "copyElementType": "(\"vec4<\" ~ dtypes.T ~ \">\") if numel(shapes.present) % 4 == 0 else dtypes.T"
93
  },
94
  "passes": [
 
24
  "tunables": { "WORKGROUP_SIZE": { "default": 256 } },
25
  "when": ["(attrs.axis if attrs.axis >= 0 else attrs.axis + ranks.past) < ranks.past", "(attrs.axis if attrs.axis >= 0 else attrs.axis + ranks.past) >= 1", "f16Ok(dtypes.T)", "numel(shapes.past) == numel(shapes.present)", "ranks.past == ranks.present", "ranks.past == ranks.update", "ranks.past >= 2"],
26
  "bindings": {
27
+ "update": { "elementType": "$scalar" },
28
+ "present": { "elementType": "$scalar" },
29
+ "params": { "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.update)" }] },
30
+ "writeIndices": { "elementType": "u32" }
31
  },
32
  "variants": [
33
  {
 
36
  "when": ["not present.writeIndices"],
37
  "derive": {
38
  "scalar": "dtypes.T",
 
39
  "copyElementType": "(\"vec4<\" ~ dtypes.T ~ \">\") if numel(shapes.present) % 4 == 0 else dtypes.T"
40
  },
41
  "passes": [
 
87
  "when": ["present.writeIndices", "dtypes.I == \"u32\""],
88
  "derive": {
89
  "scalar": "dtypes.T",
 
90
  "copyElementType": "(\"vec4<\" ~ dtypes.T ~ \">\") if numel(shapes.present) % 4 == 0 else dtypes.T"
91
  },
92
  "passes": [
build/webgpu/metadata.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "ai.onnx.TensorScatter",
3
- "id": "_ai_onnx_tensorscatter_webgpu_fae7a8d",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
@@ -8,15 +8,15 @@
8
  "algorithm": "sha256",
9
  "files": {
10
  "bench.json": "8pIi4/PPMqyk12pePykM6uwawjM0N6tsexFwiHFqySw=",
11
- "manifest.json": "RY7RXbSuGDdCHSr8e4qqS5wG4oCULwzNV5C9/ToI6Ak=",
12
- "scatter-flat-copy.wgsl.jinja": "mXcfAXV6Nxe+Y+cvem8Wwwt0BeqRQXaiCNhQoiynX0M=",
13
- "tensor-scatter.wgsl.jinja": "cDtR3E7EOm41/YKXMPKfPP3YCakYj/pbU85wt2obnY0=",
14
- "test.json": "H/VHqdsOkO3A3dM1LSKrAmk5XO2vlrDyry0EXKm/2X4="
15
  }
16
  },
17
- "provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } },
18
  "webgpu": {
19
- "manifestSpec": "2.0",
20
  "variants": {
21
  "no_write_indices": ["scatter-flat-copy.wgsl.jinja", "tensor-scatter.wgsl.jinja"],
22
  "with_write_indices": ["scatter-flat-copy.wgsl.jinja", "tensor-scatter.wgsl.jinja"]
 
1
  {
2
  "name": "ai.onnx.TensorScatter",
3
+ "id": "_ai_onnx_tensorscatter_webgpu_8788770",
4
  "version": 1,
5
  "license": "Apache-2.0",
6
  "backend": { "type": "webgpu" },
 
8
  "algorithm": "sha256",
9
  "files": {
10
  "bench.json": "8pIi4/PPMqyk12pePykM6uwawjM0N6tsexFwiHFqySw=",
11
+ "manifest.json": "7bDm3ClInzrG71KDz1vyQyLoZ3QlD1ZT6U/id4/12yY=",
12
+ "scatter-flat-copy.wgsl.jinja": "z+Iqbgw81tlT7Mlb03i42dmQbUZbLck2EyxqCec5cOY=",
13
+ "tensor-scatter.wgsl.jinja": "Sg5KztaxvYkEg6Qc475ganSCgW8lWwqR6lDUHaM4d/k=",
14
+ "test.json": "TaHjTyeBD13qqJayE8remS/d1YpS4f+bIPPR+YTk6C0="
15
  }
16
  },
17
+ "provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } },
18
  "webgpu": {
19
+ "manifestSpec": "2.1",
20
  "variants": {
21
  "no_write_indices": ["scatter-flat-copy.wgsl.jinja", "tensor-scatter.wgsl.jinja"],
22
  "with_write_indices": ["scatter-flat-copy.wgsl.jinja", "tensor-scatter.wgsl.jinja"]
build/webgpu/scatter-flat-copy.wgsl.jinja CHANGED
@@ -3,12 +3,14 @@
3
  // numel(output) % 4 == 0 the bindings rebind as vec4<T> (128-bit loads).
4
  // Tail-safe modes keep scalar bindings but copy a compile-time group of adjacent
5
  // elements per invocation, guarding only the final partial group.
6
- {% set scalarTailSafe = scalarTailSafe is defined and scalarTailSafe %}
7
- {% set copyWidth = copyWidth | default(0) %}
8
- {% set elementCount = elementCount | default(0) %}
9
- {% if usesF16 %}
10
- enable f16;
11
- {% endif %}
 
 
12
  {{ env.wgsl.resourceDeclarations }}
13
  {% set wg = workgroupSize if workgroupSize is defined else tunables.WORKGROUP_SIZE %}
14
 
@@ -17,10 +19,7 @@ const WG: u32 = {{ wg }}u;
17
 
18
  @compute @workgroup_size({{ wg }})
19
  fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
20
- let i = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * WG;
21
- if (i >= COUNT) {
22
- return;
23
- }
24
  {% if scalarTailSafe %}
25
  let base = i * {{ copyWidth }}u;
26
  dst[base] = src[base];
 
3
  // numel(output) % 4 == 0 the bindings rebind as vec4<T> (128-bit loads).
4
  // Tail-safe modes keep scalar bindings but copy a compile-time group of adjacent
5
  // elements per invocation, guarding only the final partial group.
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 }};
11
+ if ({{ name }} >= {{ bound }}) {
12
+ return;
13
+ }{% endmacro %}
14
  {{ env.wgsl.resourceDeclarations }}
15
  {% set wg = workgroupSize if workgroupSize is defined else tunables.WORKGROUP_SIZE %}
16
 
 
19
 
20
  @compute @workgroup_size({{ wg }})
21
  fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
22
+ {{ flat_index_2d("WG", bound="COUNT") }}
 
 
 
23
  {% if scalarTailSafe %}
24
  let base = i * {{ copyWidth }}u;
25
  dst[base] = src[base];
build/webgpu/tensor-scatter.wgsl.jinja CHANGED
@@ -1,38 +1,12 @@
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 dispatch's
4
- // per-axis workgroup fold width (outputs > 16.7M elements).
5
- {% elif note == "limit" %}
6
- // 2D-folded flat index: gid.y carries the high bits past the dispatch's
7
- // per-axis workgroup fold width.
8
- {% elif note == "device-axis" %}
9
- // The flat dispatch is folded across x/y at a fixed per-axis workgroup
10
- // width; gid.y carries the high portion of the output index.
11
- {% elif note == "vec4-limit" %}
12
- // 2D-folded flat vec4 index: gid.y carries the high bits past the dispatch's
13
- // per-axis workgroup fold width (the dispatch caps x and spills into y).
14
- {% elif note == "element-limit" %}
15
- // 2D-folded flat element index: gid.y carries the high bits past the
16
- // dispatch's per-axis workgroup fold width.
17
- {% elif note == "dispatch" %}
18
  // 2D-folded flat index: gid.y carries the high bits past the dispatch's
19
  // per-axis workgroup fold width.
20
- {% endif %}
21
- {% if bound == "" %}
22
- let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;
23
- {%- elif guardInline %}
24
- let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;
25
- if ({{ name }} >= {{ bound }}) { return; }
26
- {%- else %}
27
- let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;
28
  if ({{ name }} >= {{ bound }}) {
29
  return;
30
- }
31
- {%- endif %}
32
- {% endmacro %}
33
-
34
  {{ env.wgsl.resourceDeclarations }}
35
-
36
  {% set max_seq = pastShape[axisSpec] %}
37
  {% set seq_len = updateShape[axisSpec] %}
38
  {% set dim0_stride = namespace(value=1) %}
@@ -65,7 +39,7 @@ fn present_offset(update_index: u32, cache_axis: u32) -> u32 {
65
 
66
  @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
67
  fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
68
- {{ flat_index_2d("j") }}
69
  // batch_idx is the leading (dim-0) coordinate; s is the coordinate along the
70
  // sequence axis. write_indices is indexed per batch sample.
71
  {% if hasIndices %}
@@ -90,7 +64,11 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
90
  // Reduce both terms before adding them: a write index near the u32 ceiling
91
  // makes the sum wrap, and a wrapped sum lands on a different slot than the
92
  // one the operator names unless the capacity happens to divide 2^32.
93
- let cache_axis = (w % {{ max_seq }}u + s % {{ max_seq }}u) % {{ max_seq }}u;
 
 
 
 
94
  {% else %}
95
  let cache_axis = w + s;
96
  {% endif %}
 
1
+ {% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}
2
+ {% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  // 2D-folded flat index: gid.y carries the high bits past the dispatch's
4
  // per-axis workgroup fold width.
5
+ let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};
 
 
 
 
 
 
 
6
  if ({{ name }} >= {{ bound }}) {
7
  return;
8
+ }{% endmacro %}
 
 
 
9
  {{ env.wgsl.resourceDeclarations }}
 
10
  {% set max_seq = pastShape[axisSpec] %}
11
  {% set seq_len = updateShape[axisSpec] %}
12
  {% set dim0_stride = namespace(value=1) %}
 
39
 
40
  @compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})
41
  fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
42
+ {{ flat_index_2d(tunables.WORKGROUP_SIZE, "j") }}
43
  // batch_idx is the leading (dim-0) coordinate; s is the coordinate along the
44
  // sequence axis. write_indices is indexed per batch sample.
45
  {% if hasIndices %}
 
64
  // Reduce both terms before adding them: a write index near the u32 ceiling
65
  // makes the sum wrap, and a wrapped sum lands on a different slot than the
66
  // one the operator names unless the capacity happens to divide 2^32.
67
+ // A zero-capacity cache has no slot to name: nothing below can store (the
68
+ // bound is 0), but the literal divisor must stay nonzero for the shader to
69
+ // compile (the upstream circular zero-sequence-length test).
70
+ {% set mod_seq = max_seq if max_seq > 0 else 1 %}
71
+ let cache_axis = (w % {{ mod_seq }}u + s % {{ mod_seq }}u) % {{ mod_seq }}u;
72
  {% else %}
73
  let cache_axis = w + s;
74
  {% endif %}
build/webgpu/test.json CHANGED
@@ -406,7 +406,7 @@
406
  "name": "ort_standard_int32_linear_extremes",
407
  "provenance": {
408
  "source": "onnxruntime/core/providers/cpu/llm/tensorscatter.cc",
409
- "notes": "The opset-24 CPU kernel copies fixed-size payload bytes; int32 extrema and values beyond f32 exactness lock that same storage-preserving contract."
410
  },
411
  "attrs": { "axis": 2, "mode": "linear" },
412
  "inputs": {
@@ -467,7 +467,7 @@
467
  "name": "ort_standard_uint32_linear_extremes",
468
  "provenance": {
469
  "source": "onnxruntime/core/providers/cpu/llm/tensorscatter.cc",
470
- "notes": "ONNX TensorScatter-24 permits uint32; UINT32_MAX and values across the signed boundary lock exact u32 payload movement."
471
  },
472
  "attrs": { "axis": 2, "mode": "linear" },
473
  "inputs": {
@@ -567,6 +567,70 @@
567
  "tolerance": 0
568
  }
569
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570
  }
571
  ]
572
  }
 
406
  "name": "ort_standard_int32_linear_extremes",
407
  "provenance": {
408
  "source": "onnxruntime/core/providers/cpu/llm/tensorscatter.cc",
409
+ "notes": "Int32 extrema and values beyond float32 exactness check that scatter preserves payload values without numeric conversion."
410
  },
411
  "attrs": { "axis": 2, "mode": "linear" },
412
  "inputs": {
 
467
  "name": "ort_standard_uint32_linear_extremes",
468
  "provenance": {
469
  "source": "onnxruntime/core/providers/cpu/llm/tensorscatter.cc",
470
+ "notes": "TensorScatter-24 with uint32: update values include 16,777,217 (one past float32's exact-integer limit) and 4,000,000,000 (above int32 range); linear mode writes them at indices 1-2, leaving 0 and 2,147,483,648 (the int32/uint32 sign boundary) untouched."
471
  },
472
  "attrs": { "axis": 2, "mode": "linear" },
473
  "inputs": {
 
567
  "tolerance": 0
568
  }
569
  }
570
+ },
571
+ {
572
+ "name": "ort_circular_batch_larger_than_max_sequence_length",
573
+ "provenance": {
574
+ "source": "onnxruntime/test/providers/cpu/llm/tensorscatter_op_test.cc",
575
+ "test": "TensorScatterTest.Circular_BatchLargerThanMaxSequenceLength",
576
+ "notes": "batch_size 5 exceeds max_sequence_length 4, so a modulo applied to the whole index tuple (instead of only the sequence coordinate) would fold batch 4 into batch 0. Sequence positions wrap 3 -> 0 within every batch."
577
+ },
578
+ "attrs": { "mode": "circular" },
579
+ "inputs": {
580
+ "past": { "dtype": "float32", "shape": [5, 4, 1], "data": { "kind": "constant", "value": -1.0 } },
581
+ "update": {
582
+ "dtype": "float32",
583
+ "shape": [5, 2, 1],
584
+ "data": { "kind": "values", "values": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] }
585
+ },
586
+ "writeIndices": { "dtype": "uint32", "shape": [5], "data": { "kind": "values", "values": [3, 3, 3, 3, 3] } }
587
+ },
588
+ "outputs": { "present": { "dtype": "float32", "shape": [5, 4, 1], "tolerance": 0 } }
589
+ },
590
+ {
591
+ "name": "ort_circular_zero_sequence_length_is_no_op",
592
+ "provenance": {
593
+ "source": "onnxruntime/test/providers/cpu/llm/tensorscatter_op_test.cc",
594
+ "test": "TensorScatterTest.Circular_ZeroSequenceLengthIsNoOp",
595
+ "notes": "A zero-length cache axis in circular mode: the operator must be a no-op. write_indices is INT64_MAX upstream; writeIndices storage here is uint32, so the u32 ceiling stands in for it."
596
+ },
597
+ "attrs": { "mode": "circular" },
598
+ "inputs": {
599
+ "past": { "dtype": "float32", "shape": [1, 0, 1], "data": { "kind": "values", "values": [] } },
600
+ "update": { "dtype": "float32", "shape": [1, 0, 1], "data": { "kind": "values", "values": [] } },
601
+ "writeIndices": { "dtype": "uint32", "shape": [1], "data": { "kind": "values", "values": [4294967295] } }
602
+ },
603
+ "outputs": { "present": { "dtype": "float32", "shape": [1, 0, 1], "tolerance": 0 } }
604
+ },
605
+ {
606
+ "name": "ort_circular_zero_sequence_length_preserves_cache",
607
+ "provenance": {
608
+ "source": "onnxruntime/test/providers/cpu/llm/tensorscatter_op_test.cc",
609
+ "test": "TensorScatterTest.Circular_ZeroSequenceLengthPreservesCache",
610
+ "notes": "Zero-length update in circular mode with a huge write index must leave the cache untouched. Upstream uses INT64_MAX; writeIndices storage here is uint32, so the u32 ceiling stands in."
611
+ },
612
+ "attrs": { "mode": "circular" },
613
+ "inputs": {
614
+ "past": { "dtype": "float32", "shape": [1, 4, 1], "data": { "kind": "values", "values": [1.0, 2.0, 3.0, 4.0] } },
615
+ "update": { "dtype": "float32", "shape": [1, 0, 1], "data": { "kind": "values", "values": [] } },
616
+ "writeIndices": { "dtype": "uint32", "shape": [1], "data": { "kind": "values", "values": [4294967295] } }
617
+ },
618
+ "outputs": { "present": { "dtype": "float32", "shape": [1, 4, 1], "tolerance": 0 } }
619
+ },
620
+ {
621
+ "name": "ort_linear_zero_sequence_length_write_index_at_capacity",
622
+ "provenance": {
623
+ "source": "onnxruntime/test/providers/cpu/llm/tensorscatter_op_test.cc",
624
+ "test": "TensorScatterTest.Linear_ZeroSequenceLengthPreservesCache",
625
+ "notes": "write_indices == max_sequence_length is legal in linear mode only because sequence_length is 0; the result must be an exact copy of past_cache."
626
+ },
627
+ "attrs": { "mode": "linear" },
628
+ "inputs": {
629
+ "past": { "dtype": "float32", "shape": [1, 4, 1], "data": { "kind": "values", "values": [1.0, 2.0, 3.0, 4.0] } },
630
+ "update": { "dtype": "float32", "shape": [1, 0, 1], "data": { "kind": "values", "values": [] } },
631
+ "writeIndices": { "dtype": "uint32", "shape": [1], "data": { "kind": "values", "values": [4] } }
632
+ },
633
+ "outputs": { "present": { "dtype": "float32", "shape": [1, 4, 1], "tolerance": 0 } }
634
  }
635
  ]
636
  }