sync 6fdf6301e2bb
Browse filesFiles changed (7)
- README.md+2-2
- build/webgpu/group-normalization-splitk-apply.wgsl.jinja+2-1
- build/webgpu/group-normalization-splitk-partials.wgsl.jinja+2-1
- build/webgpu/group-normalization-stash-f16-serial.wgsl.jinja-3
- build/webgpu/manifest.json+13-26
- build/webgpu/metadata.json+8-8
- build/webgpu/norm-row-stats.wgsl.jinja+6-115
50 unmodified lines51525354545556572 unmodified lines606162636364656650 unmodified lines- [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, provenance)- [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)- [`test.json`](build/webgpu/test.json) — correctness cases- [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases- [`bench.json`](build/webgpu/bench.json) — benchmark cases- [`group-normalization-splitk-apply.wgsl.jinja`](build/webgpu/group-normalization-splitk-apply.wgsl.jinja)- [`group-normalization-splitk-partials.wgsl.jinja`](build/webgpu/group-normalization-splitk-partials.wgsl.jinja)- [`group-normalization-stash-f16-serial.wgsl.jinja`](build/webgpu/group-normalization-stash-f16-serial.wgsl.jinja)2 unmodified lines## Use with `@huggingface/kernels`
```shnpm install --save-exact @huggingface/kernels@0.0.1-preview.2npm install --save-exact @huggingface/kernels@0.0.1-preview.3```
Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.9 unmodified lines1011121313141516179 unmodified lines
@compute @workgroup_size(WG, 1, 1)fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) { let row = wg.x; // wg.y carries the row index past the dispatch fold width; wg.z is the part. let row = wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u; let part = wg.z; if (row >= params.rows) { return; } var pair = vec2<f32>(0.0);6 unmodified lines7891010111213146 unmodified lines
@compute @workgroup_size(WG, 1, 1)fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) { let row = wg.x; // wg.y carries the row index past the dispatch fold width; wg.z is the part. let row = wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u; let part = wg.z; if (row >= params.rows) { return; } let tid = lid.x;1212355 unmodified lines59606164626364{% if usesF16 %}enable f16;{% endif %}{{ env.wgsl.resourceDeclarations }}
const HIDDEN: u32 = {{ hiddenSize }}u;55 unmodified lines return unpack2x16float(value & 0xffffu).x;}
// ONNX GroupNormalization-21 expresses the stash_type=FLOAT16 stage as a// graph of f16 tensor operators. One invocation owns a complete group so each// intermediate addition and arithmetic stage remains f16.39 unmodified lines404142434445464344454647484849503 unmodified lines54555658596061626357585960613 unmodified lines6566677368697016 unmodified lines878889969091928 unmodified lines1011021031111041051061075 unmodified lines11311411512311611711811912012812112212312411 unmodified lines1361371381461391401411491421431521441451465 unmodified lines1521531541641651551561571585 unmodified lines1641651661771671681691801701711831721731743 unmodified lines17817918019319418118218318439 unmodified lines "groupSplitCovered": "groupRowCovered and groupRows <= tunables.SPLIT_STATS_MAX_ROWS and groupRows <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and groupHidden >= tunables.SPLIT_STATS_MIN_HIDDEN and groupPartialBytes <= device.limits.maxStorageBufferBindingSize and groupPartialBytes <= device.limits.maxBufferSize" }, "bindings": { "x": { "buffer": "read-only-storage", "elementType": "$ioElement" }, "scale": { "buffer": "read-only-storage", "elementType": "$scalar" }, "bias": { "buffer": "read-only-storage", "elementType": "$scalar" }, "y": { "buffer": "storage", "elementType": "$ioElement" }, "x": { "elementType": "$ioElement" }, "scale": { "elementType": "$scalar" }, "bias": { "elementType": "$scalar" }, "y": { "elementType": "$ioElement" }, "params": { "buffer": "uniform", "struct": [ { "name": "rows", "type": "u32", "value": "groupRows" }, {3 unmodified lines } ] }, "x_2": { "name": "x", "buffer": "read-only-storage", "elementType": "$scalar" }, "params_2": { "name": "params", "buffer": "uniform", "struct": [{ "name": "rows", "type": "u32", "value": "groupRows" }] } "x_apply": { "name": "x", "elementType": "$scalar" }, "params_rows": { "name": "params", "struct": [{ "name": "rows", "type": "u32", "value": "groupRows" }] } }, "variants": [ {3 unmodified lines "derive": { "scalar": "dtypes.T", "ioElement": "dtypes.T", "usesF16": "dtypes.T == \"f16\"", "hiddenSize": "groupHidden", "spatial": "groupSpatial", "channelsPerGroup": "groupChannelsPerGroup",16 unmodified lines "when": ["groupSplitCovered"], "derive": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"", "hiddenSize": "groupHidden", "spatial": "groupSpatial", "channelsPerGroup": "groupChannelsPerGroup",8 unmodified lines "id": "partials", "name": "GroupNormalization.SplitKPartials", "shader": "group-normalization-splitk-partials.wgsl.jinja", "bindings": ["x_2", { "name": "partials", "buffer": "storage", "elementType": "vec2<f32>" }, "params_2"], "bindings": ["x_apply", { "name": "partials", "elementType": "vec2<f32>" }, "params_rows"], "dispatch": { "x": "min(groupRows, DISPATCH_FOLD_WIDTH)", "y": "ceilDiv(groupRows, DISPATCH_FOLD_WIDTH)",5 unmodified lines "name": "GroupNormalization.SplitKApply", "shader": "group-normalization-splitk-apply.wgsl.jinja", "bindings": [ "x_2", "x_apply", "scale", "bias", { "name": "partials", "buffer": "read-only-storage", "elementType": "vec2<f32>" }, { "arg": "y", "elementType": "$scalar" }, "params_2" "params_rows" ], "dispatch": { "x": "min(groupRows, DISPATCH_FOLD_WIDTH)",11 unmodified lines "passes": [ { "id": "main", "name": "GroupNormalization.group_subgroup_vec4", "name": "GroupNormalization.GroupSubgroupVec4", "shader": "norm-row-stats.wgsl.jinja", "derive": { "modeSpec": "\"group\"", "vec4": true, "scalar": "dtypes.T", "usesF16Spec": "dtypes.T == \"f16\"", "hidden": "groupHidden", "wg": "groupVec4Workgroup", "epsilon": "attrs.epsilon",5 unmodified lines "combineSubgroups": "hasSubgroupId" }, "bindings": ["x", "scale", "bias", "y", "params"], "dispatch": { "x": "min(groupRows, 65535)", "y": "ceilDiv(groupRows, 65535)", "z": 1 }, "subgroupCollectivesWidth": "portable" "dispatch": { "x": "min(groupRows, 65535)", "y": "ceilDiv(groupRows, 65535)", "z": 1 } } ] },5 unmodified lines "passes": [ { "id": "main", "name": "GroupNormalization.group_subgroup", "name": "GroupNormalization.GroupSubgroup", "shader": "norm-row-stats.wgsl.jinja", "derive": { "modeSpec": "\"group\"", "vec4": false, "scalar": "dtypes.T", "usesF16Spec": "dtypes.T == \"f16\"", "hidden": "groupHidden", "wg": "groupScalarWorkgroup", "epsilon": "attrs.epsilon",3 unmodified lines "combineSubgroups": "hasSubgroupId" }, "bindings": ["x", "scale", "bias", "y", "params"], "dispatch": { "x": "min(groupRows, 65535)", "y": "ceilDiv(groupRows, 65535)", "z": 1 }, "subgroupCollectivesWidth": "portable" "dispatch": { "x": "min(groupRows, 65535)", "y": "ceilDiv(groupRows, 65535)", "z": 1 } } ] }12334561 unmodified line8910111213141511121314151617181919202121222324{ "name": "ai.onnx.GroupNormalization", "id": "_ai_onnx_groupnormalization_webgpu_a85b638", "id": "_ai_onnx_groupnormalization_webgpu_c63203e", "version": 1, "license": "Apache-2.0", "backend": { "type": "webgpu" },1 unmodified line "algorithm": "sha256", "files": { "bench.json": "gttctaT32ACO8eeDLTBubmz2KmY+JbRp40eLp4BLAiw=", "group-normalization-splitk-apply.wgsl.jinja": "bJQ3aD6iCak7YjlH5yZZGPo8FIGY8CuCz3BwVnTnUPE=", "group-normalization-splitk-partials.wgsl.jinja": "DVgljhomjpQ4XizLxIEh8NckAby7lIb6+plUaGh812A=", "group-normalization-stash-f16-serial.wgsl.jinja": "Wez9kqS+lzZASbm2BpWZSsHuNTT4rqsus0NiyhmGmvY=", "manifest.json": "A56uOwydeaOGyMd1ulwyeOrEUd7G8uLRbxougyW6s8g=", "norm-row-stats.wgsl.jinja": "CyRuHHc7bYmXEhtvfCxLRvjhidAMvicRA5nuJJESwxg=", "group-normalization-splitk-apply.wgsl.jinja": "teD7D7rpdNBVIoXSGMhWtjYhB5HegbAuP9Uq7u1socc=", "group-normalization-splitk-partials.wgsl.jinja": "Tif7IYCZjN0rU9RwOjuDJRuL+nWRA+fP9NurRkyxfGA=", "group-normalization-stash-f16-serial.wgsl.jinja": "R+PB+omhL6ghmEal1QbolJcaSJtiayYgBs0lPi3WcQA=", "manifest.json": "QmDFj0Qi2AT20Sw051jLbEE+/46hPAiZMuFkp6ydMfE=", "norm-row-stats.wgsl.jinja": "5fSl/yJ/eXZXSkHd/WTM3uDq8Wj6W/0geOtl9i36uXM=", "test.json": "jfHQT3aDoszhxWXbi2YtXb9bqMfNRQyaM3HFdPdOWA0=" } }, "provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } }, "provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } }, "webgpu": { "manifestSpec": "2.0", "manifestSpec": "2.1", "variants": { "group_stash_f16_serial": ["group-normalization-stash-f16-serial.wgsl.jinja"], "group_splitk": ["group-normalization-splitk-apply.wgsl.jinja", "group-normalization-splitk-partials.wgsl.jinja"],123456789101112131234519 unmodified lines2526273940414228294546473031322 unmodified lines353637565758596061626364656667686970717273747576777879808182838485868788899091929338394051 unmodified lines92939415115215315415515615715895969798991001011651661671021691031041051 unmodified line1071081091771781791801811821831841851101111121131141151161911921931941171961181191208 unmodified lines129130131211212213132133216134218219220221222223224225135136137138139230231232233234235236237238239240241242243244140141142248249250143252144145146{% if usesF16Spec %}enable f16;{% endif %}{% set combineSubgroups = combineSubgroups %}{% set scalarIo = scalarIo if scalarIo is defined else false %}{% set packedBf16Embedding = packedBf16Embedding if packedBf16Embedding is defined else false %}{% set rmsChainNorm = rmsChainNorm if rmsChainNorm is defined else false %}{% set hiddenPairs = hiddenPairs | default(0) %}{% set numRows = numRows | default(0) %}{% set epsilon = epsilon | default("0.0") %}{% set epsilon2 = epsilon2 | default("0.0") %}{% set numGroupsSpec = numGroupsSpec | default(0) %}{% set cpg = cpg | default(0) %}{% set scalarIo = false %}{% set packedF32 = "vec4<f32>" %}{% set spatialVec = spatialVec | default(0) %}{% set spatial = spatial | default(0) %}{% set reduceThreadParameters = ", sg_lane: u32, sg_id: u32, num_sg: u32"19 unmodified lines{% if vec4 %}const HIDDEN_V: u32 = {{ hiddenVec }}u;{% endif %}{% if packedBf16Embedding %}const HIDDEN_PAIRS: u32 = {{ hiddenPairs }}u;const NUM_ROWS: u32 = {{ numRows }}u;{% endif %}const WG: u32 = {{ wg }}u;const EPSILON: f32 = {{ epsilon }};{% if rmsChainNorm %}const EPSILON2: f32 = {{ epsilon2 }};{% endif %}const NUM_GROUPS: u32 = {{ numGroupsSpec }}u;const CPG: u32 = {{ cpg }}u;{% if vec4 %}2 unmodified linesconst SPATIAL: u32 = {{ spatial }}u;{% endif %}
{% if packedBf16Embedding %}{% if vec4 %}fn unpack_bf16_pair(word: u32) -> vec2<f32> { let bits = vec2<u32>(word & 0xffffu, word >> 16u); return bitcast<vec2<f32>>(bits << vec2<u32>(16u));}{% endif %}
{% if not vec4 %}fn embedding_scalar(source_row: u32, hidden: u32) -> f32 { if (source_row >= NUM_ROWS) { return 0.0; } let word = x[source_row * HIDDEN_PAIRS + (hidden >> 1u)]; let bits = select(word & 0xffffu, word >> 16u, (hidden & 1u) != 0u); return bitcast<f32>(bits << 16u);}{% endif %}
{% if vec4 %}fn embedding_vec4(source_row: u32, hidden_vec: u32) -> vec4<f32> { if (source_row >= NUM_ROWS) { return vec4<f32>(0.0); } let base = source_row * HIDDEN_PAIRS + hidden_vec * 2u; let low = unpack_bf16_pair(x[base]); let high = unpack_bf16_pair(x[base + 1u]); return vec4<f32>(low, high);}{% endif %}{% endif %}
{% if vec4 and scalarIo %}fn load_vec4(index: u32) -> vec4<f32> { return vec4<f32>(x[index], x[index + 1u], x[index + 2u], x[index + 3u]);}{% endif %}
{% if combineSubgroups %}var<workgroup> sg_partials: array<vec2<f32>, WG>;
51 unmodified lines return; } let tid = lid.x;{% if packedBf16Embedding %} let source_row = indices[row];{% if vec4 %} let base = row * HIDDEN_V;{% else %} let base = row * HIDDEN;{% endif %}{% elif vec4 and not scalarIo %}{% if vec4 and not scalarIo %} let base = row * HIDDEN_V;{% else %} let base = row * HIDDEN;{% endif %}
{% if vec4 %}{% if scalarIo %} let shift = f32(x[base]);{% else %} let shift = f32(x[base].x);{% endif %}{% else %} let shift = f32(x[base]);{% endif %}1 unmodified line var acc = vec2<f32>(0.0, 0.0);{% if vec4 %} for (var i = tid; i < HIDDEN_V; i = i + WG) {{% if packedBf16Embedding %} let v = embedding_vec4(source_row, i); embedding_out[base + i] = v;{% elif scalarIo %} let v = load_vec4(base + i * 4u);{% else %} let v = vec4<f32>(x[base + i]);{% endif %} let d = v - vec4<f32>(shift); let v = {{ packedF32 }}(x[base + i]); let d = v - {{ packedF32 }}(shift); acc.x = acc.x + d.x + d.y + d.z + d.w; acc.y = acc.y + dot(d, d); }{% else %} for (var i = tid; i < HIDDEN; i = i + WG) {{% if packedBf16Embedding %} let v = embedding_scalar(source_row, i); embedding_out[base + i] = v;{% else %} let v = f32(x[base + i]);{% endif %} let d = v - shift; acc.x = acc.x + d; acc.y = acc.y + d * d;8 unmodified lines let row_mean = shift + mean_d; let g_ch_base = (row % NUM_GROUPS) * CPG;
{% if rmsChainNorm %} var acc2 = 0.0;{% endif %}{% if vec4 %} for (var i = tid; i < HIDDEN_V; i = i + WG) {{% if packedBf16Embedding %} let idx = base + i; let v = embedding_vec4(source_row, i);{% elif scalarIo %} let idx = base + i * 4u; let v = load_vec4(idx);{% else %} let idx = base + i; let v = vec4<f32>(x[idx]);{% endif %} let v = {{ packedF32 }}(x[idx]); let ch = g_ch_base + i / SPATIAL_V; let normed = (v - vec4<f32>(row_mean)) / vec4<f32>(denom); y[idx] = {{ vecType }}(normed * vec4<f32>(f32(scale[ch])) + vec4<f32>(f32(bias[ch]))); }{% if rmsChainNorm %}
// The chained second norm reads the residual row this loop just stored. This // barrier completes those stores and any preceding shared-scratch use before // the next reduction reuses its scratch; each lane then re-reads only the // elements it wrote itself. workgroupBarrier(); let total2 = reduce_scalar(acc2{{ reduceThreadArguments }}); let inv2 = inverseSqrt(total2 / f32(HIDDEN) + EPSILON2); for (var i = tid; i < HIDDEN_V; i = i + WG) { let idx = base + i; let hv = vec4<f32>(y[idx]); normed2[idx] = {{ vecType }}(hv * inv2 * vec4<f32>(scale2[i])); }{% endif %}{% else %} for (var i = tid; i < HIDDEN; i = i + WG) { let idx = base + i;{% if packedBf16Embedding %} let v = embedding_scalar(source_row, i);{% else %} let v = f32(x[idx]);{% endif %} let ch = g_ch_base + i / SPATIAL; let normed = (v - row_mean) / denom; y[idx] = {{ scalar }}(normed * f32(scale[ch]) + f32(bias[ch]));README.md+2-2
build/webgpu/group-normalization-splitk-apply.wgsl.jinja+2-1
build/webgpu/group-normalization-splitk-partials.wgsl.jinja+2-1
build/webgpu/group-normalization-stash-f16-serial.wgsl.jinja-3
build/webgpu/manifest.json+13-26
build/webgpu/metadata.json+8-8
build/webgpu/norm-row-stats.wgsl.jinja+6-115