sync c928d21e6cc1
Browse filesFiles changed (5)
54 unmodified lines555657585859606154 unmodified lines- [`softmax-online.wgsl.jinja`](build/webgpu/softmax-online.wgsl.jinja)- [`softmax-row-stage-strided-vec4.wgsl.jinja`](build/webgpu/softmax-row-stage-strided-vec4.wgsl.jinja)- [`softmax-row-stage.wgsl.jinja`](build/webgpu/softmax-row-stage.wgsl.jinja)- [`softmax-strided-online-lane.wgsl.jinja`](build/webgpu/softmax-strided-online-lane.wgsl.jinja)- [`softmax-strided-online-coop.wgsl.jinja`](build/webgpu/softmax-strided-online-coop.wgsl.jinja)- [`softmax-strided-packed4-tail.wgsl.jinja`](build/webgpu/softmax-strided-packed4-tail.wgsl.jinja)
## Use with `@huggingface/kernels`23 unmodified lines242526272829303114 unmodified lines46474847495051525354220 unmodified lines27527627727427827928028128212 unmodified lines29529629729329829930030130242 unmodified lines34534634734234834935035135210 unmodified lines363364365359366367368369370371372373374115 unmodified lines49049149249349449549649749849950050150250350450550650750850951051151251351451551651765 unmodified lines58358458555255355455555655755855956056156256356456556656756858658758823 unmodified lines }, "tunables": { "WORKGROUP_SIZE": 256, "ROW_MIN_WORKGROUP": 64, "ROW_COLS_PER_LANE": 8, "BLOCK_COLS": 1024, "LONGROW_SPLIT_MIN_COLS": 65536, "STRIDED_CAPACITY_MIN_ROWS": 1228814 unmodified lines "axisDispatchFits": "ceilDiv(axisRows, device.limits.maxComputeWorkgroupsPerDimension) <= device.limits.maxComputeWorkgroupsPerDimension", "axisThreadDispatchFits": "ceilDiv(ceilDiv(axisRows, tunables.WORKGROUP_SIZE), device.limits.maxComputeWorkgroupsPerDimension) <= device.limits.maxComputeWorkgroupsPerDimension", "lastAxisOk": "numel(shapes.x) == numel(shapes.y) and ranks.x >= 1 and (attrs.axis == -1 or attrs.axis == ranks.x - 1) and f16Ok(dtypes.T)", "stridedAxisOk": "numel(shapes.x) == numel(shapes.y) and ranks.x >= 2 and attrs.axis + ranks.x >= 0 and attrs.axis < ranks.x and not (attrs.axis == -1 or attrs.axis == ranks.x - 1)" "stridedAxisOk": "numel(shapes.x) == numel(shapes.y) and ranks.x >= 2 and attrs.axis + ranks.x >= 0 and attrs.axis < ranks.x and not (attrs.axis == -1 or attrs.axis == ranks.x - 1)", "stridedCoopTileCols": "min(128, max(8, pow2ceil(ceilDiv(axisOuter * axisInner, 256))))", "rowWorkgroupSize": "min(tunables.WORKGROUP_SIZE, max(tunables.ROW_MIN_WORKGROUP, pow2ceil(ceilDiv(lastCols, tunables.ROW_COLS_PER_LANE))))" }, "bindingSets": { "longrowBlockStats": [220 unmodified lines "combineSubgroups": "device.features.has(\"subgroups\") and dim(shapes.x, ranks.x - 1) >= 1024", "scalar": "dtypes.T", "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"", "usesF16": "dtypes.T == \"f16\"" "usesF16": "dtypes.T == \"f16\"", "workgroupSize": "rowWorkgroupSize" }, "passes": [ {12 unmodified lines "constants": { "combineSubgroups": "device.features.has(\"subgroups\")", "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"" "usesF16": "dtypes.T == \"f16\"", "workgroupSize": "rowWorkgroupSize" }, "passes": [ {42 unmodified lines "combineSubgroups": false, "scalar": "dtypes.T", "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"", "usesF16": "dtypes.T == \"f16\"" "usesF16": "dtypes.T == \"f16\"", "workgroupSize": "tunables.WORKGROUP_SIZE" }, "passes": [ {10 unmodified lines "priority": 14, "tunables": { "WORKGROUP_SIZE": 32 }, "when": ["dim(shapes.x, ranks.x - 1) > 0", "dim(shapes.x, ranks.x - 1) <= 32", "ceilDiv(rows(shapes.x, ranks.x - 1), device.limits.maxComputeWorkgroupsPerDimension) <= device.limits.maxComputeWorkgroupsPerDimension", "lastAxisOk"], "constants": { "combineSubgroups": false, "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"" }, "constants": { "combineSubgroups": false, "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"", "workgroupSize": "tunables.WORKGROUP_SIZE" }, "passes": [ { "id": "main",115 unmodified lines } ] }, { "id": "strided_online_coop", "priority": 19, "when": ["stridedAxisOk", "f16Ok(dtypes.T)", "axisThreadDispatchFits"], "passes": [ { "id": "main", "name": "LogSoftmax.StridedOnlineCoop", "source": { "shader": "softmax-strided-online-coop.wgsl.jinja", "inputs": { "xShape": "shapes.x", "axis": "axis", "op": "\"logsoftmax\"", "tileCols": "stridedCoopTileCols" } }, "bindings": "stridedOnline", "dispatch": { "workgroups": "ceilDiv(axisOuter * axisInner, stridedCoopTileCols)" } } ] }, { "id": "strided_online_packed4_tail", "priority": 18,65 unmodified lines "dispatch": { "threads": "numel(shapes.x)", "workgroupSize": "tunables.WORKGROUP_SIZE" } } ] }, { "id": "strided_online_capacity_fallback", "priority": -1, "when": ["stridedAxisOk", "f16Ok(dtypes.T)", "axisThreadDispatchFits"], "passes": [ { "id": "main", "name": "LogSoftmax.StridedOnlineCapacityFallback", "source": { "shader": "softmax-strided-online-lane.wgsl.jinja", "inputs": { "xShape": "shapes.x", "axis": "axis", "op": "\"logsoftmax\"" } }, "bindings": "stridedOnline", "dispatch": { "threads": "axisRows", "workgroupSize": "tunables.WORKGROUP_SIZE" } } ] } ]}12334561 unmodified line89101111121314151616171819192021222324242526{ "name": "ai.onnx.LogSoftmax", "id": "_ai_onnx_logsoftmax_webgpu_0804fe4", "id": "_ai_onnx_logsoftmax_webgpu_00d56e1", "version": 1, "license": "Apache-2.0", "backend": { "type": "webgpu" },1 unmodified line "algorithm": "sha256", "files": { "bench.json": "rqFaX5XmJwQz0QQ1ET5FRMCuYEMotdcOJR+z6Y57a/0=", "manifest.json": "QDbHBdv1drmn3+Mqxdqx24xatM7c38mQxNZrDzDPmM8=", "manifest.json": "m0YO7FppNrFhQRavPhUB1ruckiRUpkx9OaKbZWrZpdc=", "softmax-longrow-normalize.wgsl.jinja": "AamVdoPDaWPQ3sk5loxtcuNu7oftC5J7hsPwLRDA7M0=", "softmax-longrow-stats.wgsl.jinja": "K0PI+9cmRgjJsOOcBRyugN0Dm7yxBy0xvyPamEpCgxo=", "softmax-normalize.wgsl.jinja": "eekHyttLyBk2X/mG54dUS2kIc5KycR3grH6000pqh1A=", "softmax-online-packed-rows.wgsl.jinja": "94Xmz8Mtj/ji7XpIQUpsDdO6uShKJtWknzEI0Er+sT8=", "softmax-online.wgsl.jinja": "qL/179d83HTFuyCUkfHqE0MBbtFMOEl8lzAH7aWmUXA=", "softmax-online.wgsl.jinja": "s3GyunMaKcOIzUKhI2TUkb650oKqApFW3EASngfE/Ro=", "softmax-row-stage-strided-vec4.wgsl.jinja": "l1byT6xoLl6Q7kSPd/uws6X5NsS2PwlSv2TBMCioVZA=", "softmax-row-stage.wgsl.jinja": "dxlM6y02DwXeLeavHDiq70h0NCRZgz+bniCltyEgRX4=", "softmax-strided-online-lane.wgsl.jinja": "BTLdB6LamPLNJsNmsllObl/9Y/Cx1EbuwdqeYpcppXg=", "softmax-strided-online-coop.wgsl.jinja": "bZkpOM+peRv2E0z0iT4V6bJTEsCHBpiF1ScPwENDFlU=", "softmax-strided-packed4-tail.wgsl.jinja": "z7+TYeybaE/yXds/ODe4wMyosZeIrCtTfUmZsA2eIXo=", "test.json": "uvZ0xakkYLGTptboBDjo8khdaQQ2h+QWiu4Z/wLWmfQ=" } }, "provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } }, "provenance": { "kernel": { "sha": "c928d21e6cc1310861cba3bafb75f5f679ecf5f3", "dirty": false } }, "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/ai.onnx.LogSoftmax" }}11 unmodified lines121314151516171819202122155 unmodified lines17817918017718118218318411 unmodified lines// the shared online-softmax combiner) and a fused second sweep writes// the result. The epilogue is either softmax or log-softmax, and vec4 row access// is used when rows are vectorized.const WG: u32 = {{ tunables.WORKGROUP_SIZE }}u;//// `workgroupSize` is a variant constant, not the op-wide tunable: the merge tree// costs a barrier per level whatever the row length, so a short row wants fewer,// busier lanes and a long row wants the widest workgroup the device allows.const WG: u32 = {{ workgroupSize }}u;// FLT_MAX, not -inf, as the online (m, d) accumulator init: merges must keep// `m - m` finite so an empty lane / all--inf row contributes the exact// accumulator identity (m, d) = (-FLT_MAX, 0). Operator epilogues interpret155 unmodified lines{% endif %}
@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})@compute @workgroup_size({{ workgroupSize }})fn main(@builtin(workgroup_id) wid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>{% if combineSubgroups %},12341234567891011121375 unmodified lines8990918788899091929293949594959697989910010110296979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150108109110111112151152153154155156157158114159160// Scratch-free online softmax/log-softmax for a non-last reduction axis. One// invocation owns one (outer, inner) row, so neighboring lanes make coalesced// reads at every reduction step. This is both the large-inner fast path and the// capacity-safe fallback when row-stat scratch cannot be allocated.// Cooperative online softmax/log-softmax for a non-last reduction axis. A// workgroup owns TILE_COLS consecutive (outer, inner) rows and splits the// reduction walk across AXIS_LANES invocations per row, so short-row shapes// still fill the machine and every load stays coalesced across the column// lanes regardless of whether the inner extent divides four. Per-lane online// partials merge in workgroup memory with the same update form as the serial// walk, so NaN and +Inf rows reproduce the single-lane kernel's outputs.{% set operation = source.op if source.op is defined else "softmax" %}{% set tileCols = source.tileCols %}{% set axisLanes = (256 / tileCols) | int %}{% if T == "f16" %}enable f16;{% endif %}75 unmodified lines return select(value - maxValue, 0.0, equalFiniteMax);}
const WGS: u32 = {{ tunables.WORKGROUP_SIZE }}u;@compute @workgroup_size(WGS)fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) { let row = gid.x + gid.y * nwg.x * WGS; if (row >= STRIDED_ROWS) { return; }const TILE_COLS: u32 = {{ tileCols }}u;const AXIS_LANES: u32 = {{ axisLanes }}u;{% set negInit = "-FLT_MAX" if operation == "logsoftmax" else "-F32_MAX" %}
var row_max = -{{ "FLT_MAX" if operation == "logsoftmax" else "F32_MAX" }}; var row_sum = 0.0; for (var k = 0u; k < REDUCE_SIZE; k += 1u) { let value = f32(x[element_offset(row, k)]); if (value > row_max) { row_sum = row_sum * exp(row_max - value) + 1.0; row_max = value; } else { row_sum += exp(value - row_max);var<workgroup> part_max: array<f32, TILE_COLS * AXIS_LANES>;var<workgroup> part_sum: array<f32, TILE_COLS * AXIS_LANES>;
@compute @workgroup_size(TILE_COLS, AXIS_LANES, 1)fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) { let tile = wg.x + wg.y * nwg.x; let row = tile * TILE_COLS + lid.x; let row_live = row < STRIDED_ROWS;
var lane_max = {{ negInit }}; var lane_sum = 0.0; if (row_live) { for (var k = lid.y; k < REDUCE_SIZE; k += AXIS_LANES) { let value = f32(x[element_offset(row, k)]); if (value > lane_max) { lane_sum = lane_sum * exp(lane_max - value) + 1.0; lane_max = value; } else { lane_sum += exp(value - lane_max); } } } part_max[lid.y * TILE_COLS + lid.x] = lane_max; part_sum[lid.y * TILE_COLS + lid.x] = lane_sum; workgroupBarrier();
// One lane per column merges the axis partials serially, in the same // (max, rescale, add) form as the walk above: an empty partial // ({{ negInit }}, 0) is the identity, a NaN partial poisons the sum the // way a NaN element does, and +Inf carries into the merged max. if (lid.y == 0u) { var merged_max = part_max[lid.x]; var merged_sum = part_sum[lid.x]; for (var t = 1u; t < AXIS_LANES; t += 1u) { let m2 = part_max[t * TILE_COLS + lid.x]; let s2 = part_sum[t * TILE_COLS + lid.x]; if (m2 > merged_max) { merged_sum = merged_sum * exp(merged_max - m2) + s2; merged_max = m2; } else { merged_sum += s2 * exp(m2 - merged_max); } } part_max[lid.x] = merged_max; part_sum[lid.x] = merged_sum; } workgroupBarrier();
let row_max = part_max[lid.x]; let row_sum = part_sum[lid.x]; let sum_bits = bitcast<u32>(row_sum); let sum_is_nan = (sum_bits & 0x7f800000u) == 0x7f800000u && (sum_bits & 0x007fffffu) != 0u; if (row_live) { for (var k = 0u; k < REDUCE_SIZE; k += 1u) { let offset = element_offset(row, k); var out = shifted_value(f32(x[offset]), row_max) - log(row_sum); if (row_max > FLT_MAX || sum_is_nan) { out = bitcast<f32>(bitcast<u32>(row_sum) | 0x7fc00000u); for (var k = lid.y; k < REDUCE_SIZE; k += AXIS_LANES) { let offset = element_offset(row, k); var out = shifted_value(f32(x[offset]), row_max) - log(row_sum); if (row_max > FLT_MAX || sum_is_nan) { out = bitcast<f32>(bitcast<u32>(row_sum) | 0x7fc00000u); } y[offset] = {{ T }}(out); } y[offset] = {{ T }}(out); }}README.md+1-1
build/webgpu/manifest.json+39-22
build/webgpu/metadata.json+5-5
build/webgpu/softmax-online.wgsl.jinja+6-2
build/webgpu/softmax-strided-online-lane.wgsl.jinja → build/webgpu/softmax-strided-online-coop.wgsl.jinjaRenamed+69-25