sync 6fdf6301e2bb
Browse filesFiles changed (8)
67 unmodified lines68697071717273743 unmodified lines787980818182838467 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- [`cast-scalar-x4.wgsl.jinja`](build/webgpu/cast-scalar-x4.wgsl.jinja)- [`matmul-bnb4-gemv.wgsl.jinja`](build/webgpu/matmul-bnb4-gemv.wgsl.jinja)- [`matmul-bnb4-sgmat.wgsl.jinja`](build/webgpu/matmul-bnb4-sgmat.wgsl.jinja)3 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.121 unmodified lines122123124125125126127128121 unmodified lines { "name": "bnb4-nf4-speculative-m15-k4096-n4096-b64-band", "provenance": { "notes": "Fifteen rows represent a speculative decoder's verification width and exercise the tiled route immediately below M=16. `nf4-tiled-speculative-band-m16-control` brackets that boundary." "notes": "Measures a 15-row NF4 multiply (K=4096, N=4096, block size 64), the verification width of a speculative decoder. `bnb4-nf4-speculative-m16-k4096-n4096-b64-tile-floor-control` is the same multiply with 16 rows." }, "preset": "model", "vars": { "M": 15, "K": 4096, "N": 4096, "blockSize": 64 },71 unmodified lines72737475767778798081828384858687888990919220 unmodified lines11311411510110210310410510610710810911611711811912011112112212313 unmodified lines13713813913113213314013513614113813914214114214314414514614714814314414518 unmodified lines16416516617317417516717717816818018116918318418518618717018919017117219319417317417558 unmodified lines23423523625926026123726326423823926724024127024224327327427527624424524610 unmodified lines25725825929329429529629729826026126226326 unmodified lines29029129233133233329333533629433833934029534134234334434534634734829629729814 unmodified lines31331431536937037131637337431737637731837938038138238331938538632038838939032132232332449 unmodified lines3743753764464474484494504514523773783793807 unmodified lines38838939046646746846947047139139239339471 unmodified lines "sgmatWidenOutBytes": "dim(shapes.aT, 0) * attrs.N * 4", "sgmatWidenFits": "sgmatWidenBytes <= device.limits.maxStorageBufferBindingSize and sgmatWidenBytes <= device.limits.maxBufferSize and sgmatWidenOutBytes <= device.limits.maxStorageBufferBindingSize and sgmatWidenOutBytes <= device.limits.maxBufferSize", "quantType": "attrs.quant_type", "K": "attrs.K", "N": "attrs.N", "blockSize": "attrs.block_size", "tileCols": "tunables.SGMAT_TILE_COLS", "tileK": "tunables.SGMAT_TILE_K", "matrixSize": "sgmatMatrixSize", "subCols": "sgmatSubCols", "colMatrices": "sgmatSubCols / sgmatMatrixSize", "loadWidth": "sgmatLoadWidth", "rowSubtiles": "sgmatRowSubtiles", "workgroupSize": "sgmatWorkgroupSize", "bLoadsPerRow": "sgmatBLoadsPerRow", "aScalar": "\"f16\" if tensorDtypes.aT == \"float16\" else \"f32\"", "usesF16": "tensorDtypes.aT == \"float16\"", "absmaxScalar": "\"f16\" if tensorDtypes.absmaxT == \"float16\" else \"f32\"", "ABSMAX_LEN": "ceilDiv(attrs.N * attrs.K, attrs.block_size)", "stagedGeometryValid": "tunables.SGMAT_TILE_COLS >= sgmatSubCols and tunables.SGMAT_TILE_COLS % sgmatSubCols == 0 and tunables.SGMAT_TILE_K >= sgmatMatrixSize and tunables.SGMAT_TILE_K % sgmatMatrixSize == 0", "stagedRowSubtiles": "max(1, min(sgmatRowSubtiles, pow2ceil(ceilDiv(dim(shapes.aT, 0), sgmatMatrixSize)), floor(deviceWorkgroupCap / (32 * max(1, sgmatColSubtiles))), floor(tunables.SGMAT_TILE_K / sgmatMatrixSize), floor((device.limits.maxComputeWorkgroupStorageSize - sgmatWorkgroupStorageBytes) / (4 * sgmatMatrixSize * max(1, tunables.SGMAT_TILE_K)))))",20 unmodified lines "hybridResourcesFit": "prefixResourcesFit and hybridStorageBytes <= device.limits.maxComputeWorkgroupStorageSize and hybridDispatchRows <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)" }, "bindings": { "a": { "arg": "aT", "buffer": "read-only-storage", "elementType": "$aScalar" }, "b": { "arg": "bT", "buffer": "read-only-storage", "elementType": "u32" }, "absmax": { "arg": "absmaxT", "buffer": "read-only-storage", "elementType": "$absmaxScalar", "length": "$ABSMAX_LEN" }, "y": { "arg": "yT", "buffer": "storage", "elementType": "$aScalar" }, "a": { "arg": "aT", "elementType": "$aScalar" }, "b": { "arg": "bT", "elementType": "u32" }, "absmax": { "arg": "absmaxT", "elementType": "$absmaxScalar", "length": "$ABSMAX_LEN" }, "y": { "arg": "yT", "elementType": "$aScalar" }, "params": { "buffer": "uniform", "struct": [ { "name": "rows", "type": "u32", "value": "dim(shapes.aT, 0)" }, { "name": "K", "type": "u32", "value": "attrs.K" },13 unmodified lines "subgroupMatrixConfigs": [{ "componentType": "f32", "resultComponentType": "f32", "M": 8, "N": 8, "K": 8 }] }, "derive": { "K": "attrs.K", "N": "attrs.N", "blockSize": "attrs.block_size", "tileRows": "prefixTileRows", "tileCols": "tunables.SGMAT_TILE_COLS", "tileK": "tunables.SGMAT_TILE_K", "subRows": "prefixTileRows / sgmatRowSubtiles", "subCols": "sgmatSubCols", "matrixSize": "sgmatMatrixSize", "rowMatrices": "prefixTileRows / (sgmatRowSubtiles * sgmatMatrixSize)", "colMatrices": "sgmatSubCols / sgmatMatrixSize", "loadWidth": "sgmatLoadWidth", "rowSubtiles": "sgmatRowSubtiles", "workgroupSize": "sgmatWorkgroupSize", "bLoadsPerRow": "sgmatBLoadsPerRow", "aScalar": "\"f32\"", "usesF16": false, "absmaxScalar": "\"f16\" if tensorDtypes.absmaxT == \"float16\" else \"f32\"", "hybridRows": true, "rowCount": "dim(shapes.aT, 0)", "fullDispatchRows": "sgmatFullRows / prefixTileRows",18 unmodified lines "subgroupMatrixConfigs": [{ "componentType": "f32", "resultComponentType": "f32", "M": 8, "N": 8, "K": 8 }] }, "derive": { "K": "attrs.K", "N": "attrs.N", "blockSize": "attrs.block_size", "tileRows": "prefixTileRows", "tileCols": "tunables.SGMAT_TILE_COLS", "tileK": "tunables.SGMAT_TILE_K", "subRows": "prefixTileRows / sgmatRowSubtiles", "subCols": "sgmatSubCols", "matrixSize": "sgmatMatrixSize", "rowMatrices": "prefixTileRows / (sgmatRowSubtiles * sgmatMatrixSize)", "colMatrices": "sgmatSubCols / sgmatMatrixSize", "loadWidth": "sgmatLoadWidth", "rowSubtiles": "sgmatRowSubtiles", "workgroupSize": "sgmatWorkgroupSize", "bLoadsPerRow": "sgmatBLoadsPerRow", "aScalar": "\"f32\"", "usesF16": true, "absmaxScalar": "\"f16\" if tensorDtypes.absmaxT == \"float16\" else \"f32\"", "srcScalar": "\"f16\"", "outScalar": "\"f32\"", "wrapNarrowInt": false, "wrapSigned": false, "hybridRows": true, "rowCount": "dim(shapes.aT, 0)", "fullDispatchRows": "sgmatFullRows / prefixTileRows",58 unmodified lines "subgroupMatrixConfigs": [{ "componentType": "f32", "resultComponentType": "f32", "M": 8, "N": 8, "K": 8 }] }, "derive": { "K": "attrs.K", "N": "attrs.N", "blockSize": "attrs.block_size", "tileRows": "stagedTileRows", "tileCols": "tunables.SGMAT_TILE_COLS", "tileK": "tunables.SGMAT_TILE_K", "subRows": "sgmatMatrixSize", "subCols": "stagedSubCols", "matrixSize": "sgmatMatrixSize", "rowMatrices": 1, "colMatrices": "stagedColumnMatrices", "loadWidth": "sgmatLoadWidth", "rowSubtiles": "stagedRowSubtiles", "workgroupSize": "stagedWorkgroupSize", "bLoadsPerRow": "sgmatBLoadsPerRow", "aScalar": "\"f16\" if tensorDtypes.aT == \"float16\" else \"f32\"", "usesF16": "tensorDtypes.aT == \"float16\"", "absmaxScalar": "\"f16\" if tensorDtypes.absmaxT == \"float16\" else \"f32\"", "stageActivations": true, "rowCount": "dim(shapes.aT, 0)" },10 unmodified lines "id": "gemv", "priority": 20, "when": ["gemvShapeValid", "portableWorkgroupFits"], "derive": { "workgroupSize": "portableWorkgroupSize", "aScalar": "\"f16\" if tensorDtypes.aT == \"float16\" else \"f32\"", "usesF16": "tensorDtypes.aT == \"float16\"", "absmaxScalar": "\"f16\" if tensorDtypes.absmaxT == \"float16\" else \"f32\"" }, "derive": { "workgroupSize": "portableWorkgroupSize" }, "passes": [ { "id": "main",26 unmodified lines "subgroupMatrixConfigs": [{ "componentType": "f32", "resultComponentType": "f32", "M": 8, "N": 8, "K": 8 }] }, "derive": { "K": "attrs.K", "N": "attrs.N", "blockSize": "attrs.block_size", "tileRows": "sgmatTileRows", "tileCols": "tunables.SGMAT_TILE_COLS", "tileK": "tunables.SGMAT_TILE_K", "subRows": "sgmatSubRows", "subCols": "sgmatSubCols", "matrixSize": "sgmatMatrixSize", "rowMatrices": "sgmatSubRows / sgmatMatrixSize", "rowMatrices": "sgmatSubRows / sgmatMatrixSize" "colMatrices": "sgmatSubCols / sgmatMatrixSize", "loadWidth": "sgmatLoadWidth", "rowSubtiles": "sgmatRowSubtiles", "workgroupSize": "sgmatWorkgroupSize", "bLoadsPerRow": "sgmatBLoadsPerRow", "aScalar": "\"f32\"", "usesF16": false, "absmaxScalar": "\"f16\" if tensorDtypes.absmaxT == \"float16\" else \"f32\"" }, "passes": [ {14 unmodified lines "subgroupMatrixConfigs": [{ "componentType": "f32", "resultComponentType": "f32", "M": 8, "N": 8, "K": 8 }] }, "derive": { "K": "attrs.K", "N": "attrs.N", "blockSize": "attrs.block_size", "tileRows": "sgmatTileRows", "tileCols": "tunables.SGMAT_TILE_COLS", "tileK": "tunables.SGMAT_TILE_K", "subRows": "sgmatSubRows", "subCols": "sgmatSubCols", "matrixSize": "sgmatMatrixSize", "rowMatrices": "sgmatSubRows / sgmatMatrixSize", "colMatrices": "sgmatSubCols / sgmatMatrixSize", "loadWidth": "sgmatLoadWidth", "rowSubtiles": "sgmatRowSubtiles", "workgroupSize": "sgmatWorkgroupSize", "bLoadsPerRow": "sgmatBLoadsPerRow", "aScalar": "\"f32\"", "usesF16": true, "absmaxScalar": "\"f16\" if tensorDtypes.absmaxT == \"float16\" else \"f32\"", "srcScalar": "\"f16\"", "outScalar": "\"f32\"", "wrapNarrowInt": false, "wrapSigned": false "outScalar": "\"f32\"" }, "intermediates": [ { "id": "aF32", "dtype": "float32", "shape": "[numel(shapes.aT)]" },49 unmodified lines "id": "tiled", "priority": 10, "when": ["tileEligible", "tileWorkgroupFits"], "derive": { "tileK": "tunables.PORTABLE_TILE_K", "aScalar": "\"f16\" if tensorDtypes.aT == \"float16\" else \"f32\"", "usesF16": "tensorDtypes.aT == \"float16\"", "absmaxScalar": "\"f16\" if tensorDtypes.absmaxT == \"float16\" else \"f32\"", "tileRows": "portableTileRows" }, "derive": { "tileK": "tunables.PORTABLE_TILE_K", "tileRows": "portableTileRows" }, "passes": [ { "id": "main",7 unmodified lines "id": "scalar", "priority": 0, "when": ["commonShapeValid", "portableWorkgroupSize > 0"], "derive": { "workgroupSize": "portableWorkgroupSize", "aScalar": "\"f16\" if tensorDtypes.aT == \"float16\" else \"f32\"", "usesF16": "tensorDtypes.aT == \"float16\"", "absmaxScalar": "\"f16\" if tensorDtypes.absmaxT == \"float16\" else \"f32\"" }, "derive": { "workgroupSize": "portableWorkgroupSize" }, "passes": [ { "id": "main",20 unmodified lines212223242425262728293031313233342 unmodified lines373839404140414220 unmodified lines -0.0, -5.208333333e-03, -0.66666667, -1.0, -0.33333333, -0.5, -0.16666667, -0.25);{%- else %}{% else %}var<private> quant_map: array<f32, 16> = array<f32, 16>( -1.0, -0.6961928009986877, -0.5250730514526367, -0.39491748809814453, -0.28444138169288635, -0.18477343022823334, -0.09105003625154495, 0.0, 0.07958029955625534, 0.16093020141124725, 0.24611230194568634, 0.33791524171829224, 0.44070982933044434, 0.5626170039176941, 0.7229568362236023, 1.0);{%- endif %}{% endif %}
// One dequantized weight at flat index `flat` into the [N, K] weight.fn dequant_weight(flat: u32) -> f32 {2 unmodified lines return quant_map[code] * f32(absmax[flat / params.blockSize]);}
// Decode (M == 1): one workgroup owns each output column, with lanes striding K.// Each lane keeps one accumulator and the workgroup closes with a tree reduction.@compute @workgroup_size(WG, 1, 1)8 unmodified lines91011121213142 unmodified lines1718192120212234 unmodified lines575859626061626364656669707172676869708 unmodified lines{% if usesF16 %}// The widened path keeps the activations and the output in f32 for the matrix// units, but the quantization scales stay in the request's own dtype.enable f16;{% endif %}enable subgroups;{% if pinSubgroupSize32 %}2 unmodified linesenable chromium_experimental_subgroup_matrix;diagnostic(off, chromium.subgroup_matrix_uniformity);
{{ env.wgsl.resourceDeclarations }}
const K: u32 = {{ K }}u;34 unmodified lines -0.0, -5.208333333e-03, -0.66666667, -1.0, -0.33333333, -0.5, -0.16666667, -0.25);{%- else %}{% else %}var<private> quant_map: array<f32, 16> = array<f32, 16>( -1.0, -0.6961928009986877, -0.5250730514526367, -0.39491748809814453, -0.28444138169288635, -0.18477343022823334, -0.09105003625154495, 0.0, 0.07958029955625534, 0.16093020141124725, 0.24611230194568634, 0.33791524171829224, 0.44070982933044434, 0.5626170039176941, 0.7229568362236023, 1.0);{%- endif %}
{% endif %}
fn load_b(tile_base: u32, k_base: u32, row: u32, col_group: u32) { let col = col_group * LOAD_WIDTH;40 unmodified lines414243444445464748495051525152535426 unmodified lines8182838584858640 unmodified lines -0.0, -5.208333333e-03, -0.66666667, -1.0, -0.33333333, -0.5, -0.16666667, -0.25);{%- else %}{% else %}var<private> quant_map: array<f32, 16> = array<f32, 16>( -1.0, -0.6961928009986877, -0.5250730514526367, -0.39491748809814453, -0.28444138169288635, -0.18477343022823334, -0.09105003625154495, 0.0, 0.07958029955625534, 0.16093020141124725, 0.24611230194568634, 0.33791524171829224, 0.44070982933044434, 0.5626170039176941, 0.7229568362236023, 1.0);{%- endif %}
{% endif %}
// Four consecutive dequantized weights starting at flat index `flat` into the// [N, K] weight. Consecutive indices share a stored byte, so the run costs two26 unmodified lines return vec4<f32>(quant_map[codes.x], quant_map[codes.y], quant_map[codes.z], quant_map[codes.w]) * scales;}
@compute @workgroup_size(16, 8, 1)fn main( @builtin(workgroup_id) wg: vec3<u32>,1234567815 unmodified lines242526222728293031323329343536372 unmodified lines404142383943444243444545464748{% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}{% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %} // 2D-folded flat index: gid.y carries the high bits past the dispatch's // per-axis workgroup fold width. let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};{% endmacro %}{{ env.wgsl.resourceDeclarations }}
const WG: u32 = {{ workgroupSize }}u;15 unmodified lines -0.0, -5.208333333e-03, -0.66666667, -1.0, -0.33333333, -0.5, -0.16666667, -0.25);{%- else %}{% else %}var<private> quant_map: array<f32, 16> = array<f32, 16>( -1.0, -0.6961928009986877, -0.5250730514526367, -0.39491748809814453, -0.28444138169288635, -0.18477343022823334, -0.09105003625154495, 0.0, 0.07958029955625534, 0.16093020141124725, 0.24611230194568634, 0.33791524171829224, 0.44070982933044434, 0.5626170039176941, 0.7229568362236023, 1.0);{%- endif %}{% endif %}
// One dequantized weight at flat index `flat` into the [N, K] weight.fn dequant_weight(flat: u32) -> f32 {2 unmodified lines return quant_map[code] * f32(absmax[flat / params.blockSize]);}
@compute @workgroup_size(WG, 1, 1)fn main(@builtin(global_invocation_id) gid: vec3<u32>) { // 2D-folded flat output-element index: gid.y carries the high bits past the // per-axis dispatch fold width. With no fold this // reduces to gid.x; the index >= total guard drops the tail. let index = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * WG;{{ flat_index_2d("WG", "index", "") }} let total = params.rows * params.N;
if (index >= total) {1233456789101011121314151612131415161718192020212222232425{ "name": "com.microsoft.MatMulBnb4", "id": "_com_microsoft_matmulbnb4_webgpu_237048a", "id": "_com_microsoft_matmulbnb4_webgpu_2efe1ae", "version": 1, "license": "Apache-2.0", "backend": { "type": "webgpu" }, "digest": { "algorithm": "sha256", "files": { "bench.json": "/Z1zFaT4D+0TEFMxG/zqun5CjI9irI93GNGwLZdLXHQ=", "bench.json": "Ixtu6FBToeEWEbpbM/+lGVS3jt0IPtrmZrxRfxo7Uj8=", "cast-scalar-x4.wgsl.jinja": "s8Y9E2+D7cFgYTUEJhiuPsUzvHjv2mXVoiRmv0A0vTU=", "manifest.json": "TEJfUwu6Isit+t0fBCRp0Ny+uC8hUjSe2jgG5kCFcEg=", "matmul-bnb4-gemv.wgsl.jinja": "7W3L5RH+5dk5/dKNnjwVFtBt+xgCxorJJ04/8zHrySw=", "matmul-bnb4-sgmat.wgsl.jinja": "9WS5F6zd/KFztTpbZjBgJe7WIl0l7uiRelcDv/9YbBc=", "matmul-bnb4-tiled.wgsl.jinja": "bADcd5OFuhrZ33Qntqh1PXREZolJZ+RKS+VlweAjPo0=", "matmul-bnb4.wgsl.jinja": "6rtf2K0op1IJv2+l66HJjq8XnBATGyefxit0Hj5QREI=", "manifest.json": "TTpD+bhv4VB93QlNQ4Z2xGHvG4ClFxnokPMIbKMH2Bc=", "matmul-bnb4-gemv.wgsl.jinja": "5mV78S/5vyeTFu8anlXAsKA+OVz7iy3fmo990lgf/yU=", "matmul-bnb4-sgmat.wgsl.jinja": "A0jgHcIQrjdodubOI3bgHSYU9SsGew+/3IJwkMMr6eo=", "matmul-bnb4-tiled.wgsl.jinja": "auoAXKsuYOXVbPmzjXMm6IQ5L1HxzjDWaPTSxD+Wd/s=", "matmul-bnb4.wgsl.jinja": "81lKMMKBPnPeNbcFXiZZe4KBGefTeBfNaHH1LSgP/6U=", "test.json": "KPAf0hjucty64auyD+xOS5RAGDEPYeAlUSJPhbig7CA=" } }, "provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } }, "provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } }, "webgpu": { "manifestSpec": "2.0", "manifestSpec": "2.1", "variants": { "sgmat_hybrid_rows": ["matmul-bnb4-sgmat.wgsl.jinja"], "sgmat_hybrid_rows_widened": ["cast-scalar-x4.wgsl.jinja", "matmul-bnb4-sgmat.wgsl.jinja"],README.md+2-2
build/webgpu/bench.json+1-1
build/webgpu/manifest.json+24-104
build/webgpu/matmul-bnb4-gemv.wgsl.jinja+2-4
build/webgpu/matmul-bnb4-sgmat.wgsl.jinja+2-7
build/webgpu/matmul-bnb4-tiled.wgsl.jinja+2-4
build/webgpu/matmul-bnb4.wgsl.jinja+8-8
build/webgpu/metadata.json+9-9