From 76aa1a7cb4f84e4e3537e7c1bb537fa779ca6846 Mon Sep 17 00:00:00 2001 From: MiniX16 Date: Tue, 10 Nov 2026 14:17:00 +0100 Subject: [PATCH 1/2] fix(core): split flat indices with std.intdiv instead of f32 division Inside 'use gpu', d.u32(a / b) compiles to u32(f32(a) / f32(b)). Past 2^24, or on drivers whose f32 division is not correctly rounded, the quotient lands on the wrong row. Every kernel now uses std.intdiv, and the 2^24 cases that were it.fails pass. --- .../src/core/kernels/attention/attn.ts | 22 ++-- .../src/core/kernels/attention/attnDecode.ts | 10 +- .../src/core/kernels/attention/rope.ts | 6 +- .../runntime/src/core/kernels/conv/conv1d.ts | 2 +- .../src/core/kernels/conv/convTranspose2d.ts | 12 +- .../runntime/src/core/kernels/conv/hwc4.ts | 120 +++++++++--------- .../core/kernels/elementwise/swigluChunk.ts | 2 +- .../src/core/kernels/image/imageToChw.ts | 2 +- .../src/core/kernels/matmul/argmaxDot.ts | 2 +- .../src/core/kernels/matmul/matmul.ts | 10 +- .../src/core/kernels/matmul/matmulGemv.ts | 4 +- .../core/kernels/matmul/matmulTiledVec4.ts | 2 +- .../src/core/kernels/matmul/moeQuantW.ts | 12 +- .../src/core/kernels/shape/columns.ts | 6 +- packages/runntime/src/core/kernels/ssm.ts | 16 +-- .../core/kernels/attention/attn.gpu.test.ts | 17 ++- .../core/kernels/attention/rope.gpu.test.ts | 10 +- .../kernels/conv/convTranspose2d.gpu.test.ts | 8 +- .../elementwise/swigluChunk.gpu.test.ts | 10 +- .../core/kernels/shape/columns.gpu.test.ts | 8 +- .../test/core/kernels/ssm.gpu.test.ts | 8 +- 21 files changed, 141 insertions(+), 148 deletions(-) diff --git a/packages/runntime/src/core/kernels/attention/attn.ts b/packages/runntime/src/core/kernels/attention/attn.ts index fd8a5e46..df1091e5 100644 --- a/packages/runntime/src/core/kernels/attention/attn.ts +++ b/packages/runntime/src/core/kernels/attention/attn.ts @@ -117,8 +117,8 @@ function makeAttnKernel(elem: Elem = F32_ELEM) { return; } const h = idx % C.qHeads; - const row = d.u32(idx / C.qHeads); // explicit integer division - const kvh = d.u32(h / d.u32(C.qHeads / C.kvHeads)); + const row = std.intdiv(idx, C.qHeads); + const kvh = std.intdiv(h, std.intdiv(C.qHeads, C.kvHeads)); const hd = C.headDim; const qWidth = C.qHeads * hd; const kvWidth = C.kvHeads * hd; @@ -346,8 +346,8 @@ function makeAttnRowsKernel(cfg: AttnGeoCfg, elem: Elem = F32_ELEM) { return; } const h = gid.x % qHeads; - const row = d.u32(gid.x / qHeads); // explicit integer division - const kvh = d.u32(h / group); + const row = std.intdiv(gid.x, qHeads); + const kvh = std.intdiv(h, group); const qBase = row * qStride + h * hd; const qPos = D.qPosOffset + row; @@ -441,8 +441,8 @@ function makeAttnRowsSplitKernel(cfg: AttnGeoCfg, elem: Elem = F32_ELEM) { return; } const h = gid.x % qHeads; - const row = d.u32(gid.x / qHeads); // explicit integer division - const kvh = d.u32(h / group); + const row = std.intdiv(gid.x, qHeads); + const kvh = std.intdiv(h, group); const qBase = row * qStride + h * hd; const qPos = D.qPosOffset + row; @@ -484,7 +484,7 @@ function makeAttnRowsSplitKernel(cfg: AttnGeoCfg, elem: Elem = F32_ELEM) { // The max guard keeps chunkLen ≥ 1, so the arithmetic cannot wrap. const range = end - lo + 1; - const chunkLen = std.max(d.u32((range + D.chunks - 1) / D.chunks), 1); + const chunkLen = std.max(std.intdiv(range + D.chunks - 1, D.chunks), 1); const sliceLo = lo + gid.y * chunkLen; const sliceEnd = std.min(sliceLo + chunkLen - 1, end); @@ -547,8 +547,8 @@ function makeAttnRowsSubgroupSplitKernel(cfg: AttnGeoCfg, elem: Elem = F32_ELEM) const D = S.$.dims; const lane = lid.x; const h = wid.x % qHeads; - const row = d.u32(wid.x / qHeads); // explicit integer division - const kvh = d.u32(h / group); + const row = std.intdiv(wid.x, qHeads); + const kvh = std.intdiv(h, group); const qBase = row * qStride + h * hd; const qPos = D.qPosOffset + row; @@ -598,7 +598,7 @@ function makeAttnRowsSubgroupSplitKernel(cfg: AttnGeoCfg, elem: Elem = F32_ELEM) } const range = end - lo + 1; - const chunkLen = std.max(d.u32((range + D.chunks - 1) / D.chunks), 1); + const chunkLen = std.max(std.intdiv(range + D.chunks - 1, D.chunks), 1); const sliceLo = lo + wid.y * chunkLen; const sliceEnd = std.min(sliceLo + chunkLen - 1, end); @@ -683,7 +683,7 @@ function makeAttnMergeKernel(cfg: AttnGeoCfg, elem: Elem = F32_ELEM) { return; } const h = gid.x % qHeads; - const row = d.u32(gid.x / qHeads); // explicit integer division + const row = std.intdiv(gid.x, qHeads); const stride = 2 + hd; const rowBase = (row * qHeads + h) * D.chunks; diff --git a/packages/runntime/src/core/kernels/attention/attnDecode.ts b/packages/runntime/src/core/kernels/attention/attnDecode.ts index e349837a..9179bb6f 100644 --- a/packages/runntime/src/core/kernels/attention/attnDecode.ts +++ b/packages/runntime/src/core/kernels/attention/attnDecode.ts @@ -100,8 +100,8 @@ function makeAttnDecodeKernel(cfg: AttnDecodeCfg, elem: Elem) { const tid = lid.x; const slot = flatWorkgroupId(wid); const h = slot % qHeads; - const row = d.u32(slot / qHeads); - const kvh = d.u32(h / group); + const row = std.intdiv(slot, qHeads); + const kvh = std.intdiv(h, group); const qBase = row * qWidth + h * hd; const qPos = D.qPosOffset + row; @@ -280,8 +280,8 @@ function makeAttnDecodeSplitKernel(cfg: AttnDecodeCfg, elem: Elem) { const tid = lid.x; const chunkSlot = flatWorkgroupId(wid); const chunk = chunkSlot % D.chunks; - const h = d.u32(chunkSlot / D.chunks); - const kvh = d.u32(h / group); + const h = std.intdiv(chunkSlot, D.chunks); + const kvh = std.intdiv(h, group); const qBase = h * hd; // qLen = 1 → row 0 const qPos = D.qPosOffset; @@ -306,7 +306,7 @@ function makeAttnDecodeSplitKernel(cfg: AttnDecodeCfg, elem: Elem) { const end = std.min(qPos + windowRight, D.kvLen - 1); const range = end + 1 - std.min(lo, end + 1); // 0 when the band is empty // A floor of 1 keeps `myLo + chunkLen − 1` from wrapping. - const chunkLen = std.max(d.u32((range + D.chunks - 1) / D.chunks), 1); + const chunkLen = std.max(std.intdiv(range + D.chunks - 1, D.chunks), 1); const myLo = lo + chunk * chunkLen; const myEnd = std.min(myLo + chunkLen - 1, end); for (let j = myLo + tid; j <= myEnd; j += WORKGROUP_SIZE) { diff --git a/packages/runntime/src/core/kernels/attention/rope.ts b/packages/runntime/src/core/kernels/attention/rope.ts index 2a8cd340..48db7006 100644 --- a/packages/runntime/src/core/kernels/attention/rope.ts +++ b/packages/runntime/src/core/kernels/attention/rope.ts @@ -1,4 +1,4 @@ -import tgpu, { d } from 'typegpu'; +import tgpu, { d, std } from 'typegpu'; import type { TgpuRoot } from 'typegpu'; import { type F32Buffer, @@ -49,12 +49,12 @@ function makeRopeVariant(elem: Elem) { 'use gpu'; const D = layout.$.dims; const C = config.$; - const pairs = d.u32(C.cols / 2); + const pairs = std.intdiv(C.cols, 2); const idx = flatIndex(gid); if (idx >= D.rows * pairs) { return; } - const row = d.u32(idx / pairs); + const row = std.intdiv(idx, pairs); const col0 = (idx % pairs) * 2; const dcol = col0 % C.headDim; // table column (pair-duplicated: d == d+1) const c = layout.$.cos[row * C.headDim + dcol]!; diff --git a/packages/runntime/src/core/kernels/conv/conv1d.ts b/packages/runntime/src/core/kernels/conv/conv1d.ts index 26139e6e..368af21c 100644 --- a/packages/runntime/src/core/kernels/conv/conv1d.ts +++ b/packages/runntime/src/core/kernels/conv/conv1d.ts @@ -73,7 +73,7 @@ function makeConv1dVariant(elem: Elem) { if (idx >= tOut * C.c4c) { return; } - const frame = d.u32(idx / C.c4c); + const frame = std.intdiv(idx, C.c4c); const ch0 = (idx % C.c4c) * 4; const windowStart = frame * C.stride; // in padded coordinates diff --git a/packages/runntime/src/core/kernels/conv/convTranspose2d.ts b/packages/runntime/src/core/kernels/conv/convTranspose2d.ts index f8bbe045..abe887a5 100644 --- a/packages/runntime/src/core/kernels/conv/convTranspose2d.ts +++ b/packages/runntime/src/core/kernels/conv/convTranspose2d.ts @@ -58,10 +58,10 @@ function makeConvTranspose2dKernel(cfg: ConvTranspose2dCfg, elem: Elem) { return; } const wo = idx % wOut; - const ho = d.u32(idx / wOut) % hOut; - const co = d.u32(idx / (wOut * hOut)); - const hi = d.u32(ho / k); // explicit integer division (codebase convention) - const wi = d.u32(wo / k); + const ho = std.intdiv(idx, wOut) % hOut; + const co = std.intdiv(idx, wOut * hOut); + const hi = std.intdiv(ho, k); + const wi = std.intdiv(wo, k); const di = ho % k; const dj = wo % k; const rowBase = (co * k * k + di * k + dj) * cIn; @@ -69,7 +69,7 @@ function makeConvTranspose2dKernel(cfg: ConvTranspose2dCfg, elem: Elem) { const plane = dims.h * dims.w; let acc = d.f32(0); if (paired) { - const wordBase = d.u32(rowBase / 2); // rowBase is even when cIn is + const wordBase = std.intdiv(rowBase, 2); // rowBase is even when cIn is for (const c2 of std.range(cIn / 2)) { const pair = std.unpack2x16float(layout.$.w[wordBase + c2]!); const xb = 2 * c2 * plane + xPix; @@ -78,7 +78,7 @@ function makeConvTranspose2dKernel(cfg: ConvTranspose2dCfg, elem: Elem) { } else { for (const ci of std.range(cIn)) { const flat = rowBase + ci; - const pair = std.unpack2x16float(layout.$.w[d.u32(flat / 2)]!); + const pair = std.unpack2x16float(layout.$.w[std.intdiv(flat, 2)]!); const wv = std.select(pair.x, pair.y, flat % 2 === 1); acc += wv * d.f32(layout.$.x[ci * plane + xPix]!); } diff --git a/packages/runntime/src/core/kernels/conv/hwc4.ts b/packages/runntime/src/core/kernels/conv/hwc4.ts index b5753d3a..fdaebc85 100644 --- a/packages/runntime/src/core/kernels/conv/hwc4.ts +++ b/packages/runntime/src/core/kernels/conv/hwc4.ts @@ -140,9 +140,9 @@ export const makeConvGenericKernel = (cfg: Conv2dHwc4Cfg) => { })(({ gid }) => { 'use gpu'; const dims = convLayout.$.dims; - const hOut = d.u32((dims.h + 2 * padding - kH) / stride) + 1; - const wOut = d.u32((dims.w + 2 * padding - kW) / stride) + 1; - const wRuns = d.u32((wOut + cols - 1) / cols); + const hOut = std.intdiv(dims.h + 2 * padding - kH, stride) + 1; + const wOut = std.intdiv(dims.w + 2 * padding - kW, stride) + 1; + const wRuns = std.intdiv(wOut + cols - 1, cols); const idx = flatIndex(gid); if (idx >= co4 * hOut * wRuns) { return; @@ -150,9 +150,9 @@ export const makeConvGenericKernel = (cfg: Conv2dHwc4Cfg) => { // Narrow outputs put the channel block fastest: lanes share input loads // and store whole pixels. Wide ones put the column run fastest, so lanes // share one weight row. - const oB = narrowOut ? idx % co4 : d.u32(idx / (wRuns * hOut)); - const ox0 = (narrowOut ? d.u32(idx / co4) % wRuns : idx % wRuns) * cols; - const oy = narrowOut ? d.u32(idx / (co4 * wRuns)) : d.u32(idx / wRuns) % hOut; + const oB = narrowOut ? idx % co4 : std.intdiv(idx, wRuns * hOut); + const ox0 = (narrowOut ? std.intdiv(idx, co4) % wRuns : idx % wRuns) * cols; + const oy = narrowOut ? std.intdiv(idx, co4 * wRuns) : std.intdiv(idx, wRuns) % hOut; const accs = d.arrayOf(d.vec4h, cols)(); for (const ky of tgpu.unroll(std.range(kH))) { @@ -255,11 +255,11 @@ const makeConvTiledKernel = (cfg: Conv2dHwc4Cfg) => { })(({ lid, wid }) => { 'use gpu'; const dims = convLayout.$.dims; - const hOut = d.u32((dims.h + 2 * padding - kH) / stride) + 1; - const wOut = d.u32((dims.w + 2 * padding - kW) / stride) + 1; + const hOut = std.intdiv(dims.h + 2 * padding - kH, stride) + 1; + const wOut = std.intdiv(dims.w + 2 * padding - kW, stride) + 1; const nPix = hOut * wOut; const tx = lid % TILED_WG_X; - const ty = d.u32(lid / TILED_WG_X); + const ty = std.intdiv(lid, TILED_WG_X); const nBase = wid.x * TILED_BN; const oBase = wid.y * TILED_BMB; @@ -271,11 +271,11 @@ const makeConvTiledKernel = (cfg: Conv2dHwc4Cfg) => { for (const i of tgpu.unroll(std.range(TILED_A_LOADS))) { const e = lid + i * TILED_THREADS; const iL = e % 4; - const oBl = d.u32(e / 4) % TILED_BMB; - const kgl = d.u32(e / (4 * TILED_BMB)); + const oBl = std.intdiv(e, 4) % TILED_BMB; + const kgl = std.intdiv(e, 4 * TILED_BMB); const kg = kg0 + kgl; const oB = oBase + oBl; - const tap = d.u32(kg / ci4); + const tap = std.intdiv(kg, ci4); const iB = kg % ci4; let v = d.vec4h(); if (kg < kGroups && oB < co4) { @@ -287,14 +287,14 @@ const makeConvTiledKernel = (cfg: Conv2dHwc4Cfg) => { for (const i of tgpu.unroll(std.range(TILED_B_LOADS))) { const e = lid + i * TILED_THREADS; const kgl = e % TILED_BKG; - const px = d.u32(e / TILED_BKG); + const px = std.intdiv(e, TILED_BKG); const kg = kg0 + kgl; const n = nBase + px; - const tap = d.u32(kg / ci4); + const tap = std.intdiv(kg, ci4); const iB = kg % ci4; - const ky = d.u32(tap / kW); + const ky = std.intdiv(tap, kW); const kx = tap % kW; - const oy = d.u32(n / wOut); + const oy = std.intdiv(n, wOut); const ox = n % wOut; const iyP = oy * stride + ky; const ixP = ox * stride + kx; @@ -378,12 +378,12 @@ export const makeConv1x1Kernel = (cfg: Conv2dHwc4Cfg) => { })(({ gid }) => { 'use gpu'; const dims = convLayout.$.dims; - const pT = d.u32((dims.h * dims.w) / pxT); + const pT = std.intdiv(dims.h * dims.w, pxT); const idx = flatIndex(gid); if (idx >= pT * co4) { return; } - const p0 = d.u32(idx / co4) * pxT; + const p0 = std.intdiv(idx, co4) * pxT; const oB = idx % co4; const accs = d.arrayOf(d.vec4f, pxT)(); @@ -456,9 +456,9 @@ export const makeConv1x1SmallKernel = (cfg: Conv2dHwc4Cfg) => { const dims = convLayout.$.dims; const hw = dims.h * dims.w; const kLane = lid % SMALL_1X1_KS; - const oLane = d.u32(lid / SMALL_1X1_KS); + const oLane = std.intdiv(lid, SMALL_1X1_KS); const oB = (wid.x % oGroups) * SMALL_1X1_OL + oLane; - const p0 = d.u32(wid.x / oGroups) * SMALL_1X1_PXT; + const p0 = std.intdiv(wid.x, oGroups) * SMALL_1X1_PXT; const accs = d.arrayOf(d.vec4h, SMALL_1X1_PXT)(); if (oB < co4) { @@ -539,16 +539,16 @@ const makeConvDwKernel = (cfg: Conv2dHwc4Cfg) => { })(({ gid }) => { 'use gpu'; const dims = convLayout.$.dims; - const hOut = d.u32((dims.h + 2 * padding - kH) / stride) + 1; - const wOut = d.u32((dims.w + 2 * padding - kW) / stride) + 1; - const wRuns = d.u32((wOut + cols - 1) / cols); + const hOut = std.intdiv(dims.h + 2 * padding - kH, stride) + 1; + const wOut = std.intdiv(dims.w + 2 * padding - kW, stride) + 1; + const wRuns = std.intdiv(wOut + cols - 1, cols); const idx = flatIndex(gid); if (idx >= c4n * hOut * wRuns) { return; } const cB = idx % c4n; - const run = d.u32(idx / c4n) % wRuns; - const oy = d.u32(idx / (c4n * wRuns)); + const run = std.intdiv(idx, c4n) % wRuns; + const oy = std.intdiv(idx, c4n * wRuns); const ox0 = run * cols; const accs = d.arrayOf(d.vec4f, cols)(); @@ -637,16 +637,16 @@ const makeWinogradInKernel = (cfg: Conv2dHwc4Cfg) => { })(({ lid, wid }) => { 'use gpu'; const dims = convLayout.$.dims; - const tilesX = d.u32((dims.w + 3) / 4); - const tiles = tilesX * d.u32((dims.h + 3) / 4); + const tilesX = std.intdiv(dims.w + 3, 4); + const tiles = tilesX * std.intdiv(dims.h + 3, 4); const p = lid % WINOGRAD_PAIRS; - const lane = d.u32(lid / WINOGRAD_PAIRS); + const lane = std.intdiv(lid, WINOGRAD_PAIRS); const pair = flatWorkgroupId(wid) * WINOGRAD_PAIRS + p; const live = pair < tiles * ci4; const iB = pair % ci4; - const t = d.u32(pair / ci4); + const t = std.intdiv(pair, ci4); // Patch row `lane` in padded coordinates: input (y, x) is (yP - 1, xP - 1). - const yP = d.u32(t / tilesX) * 4 + lane; + const yP = std.intdiv(t, tilesX) * 4 + lane; const x0 = (t % tilesX) * 4; const xs = d.arrayOf(d.vec4f, 6)(); @@ -691,15 +691,15 @@ const makeWinogradMulKernel = (cfg: Conv2dHwc4Cfg) => { })(({ gid }) => { 'use gpu'; const dims = convLayout.$.dims; - const tiles = d.u32((dims.w + 3) / 4) * d.u32((dims.h + 3) / 4); - const groups = d.u32((tiles + WINOGRAD_MUL_TILES - 1) / WINOGRAD_MUL_TILES); + const tiles = std.intdiv(dims.w + 3, 4) * std.intdiv(dims.h + 3, 4); + const groups = std.intdiv(tiles + WINOGRAD_MUL_TILES - 1, WINOGRAD_MUL_TILES); const idx = flatIndex(gid); if (idx >= WINOGRAD_PLANES * groups * co4) { return; } const oB = idx % co4; - const t0 = (d.u32(idx / co4) % groups) * WINOGRAD_MUL_TILES; - const k = d.u32(idx / (co4 * groups)); + const t0 = (std.intdiv(idx, co4) % groups) * WINOGRAD_MUL_TILES; + const k = std.intdiv(idx, co4 * groups); const plane = k * tiles; const accs = d.arrayOf(d.vec4f, WINOGRAD_MUL_TILES)(); @@ -733,14 +733,14 @@ const makeWinogradOutKernel = (cfg: Conv2dHwc4Cfg) => { })(({ lid, wid }) => { 'use gpu'; const dims = convLayout.$.dims; - const tilesX = d.u32((dims.w + 3) / 4); - const tiles = tilesX * d.u32((dims.h + 3) / 4); + const tilesX = std.intdiv(dims.w + 3, 4); + const tiles = tilesX * std.intdiv(dims.h + 3, 4); const p = lid % WINOGRAD_PAIRS; - const lane = d.u32(lid / WINOGRAD_PAIRS); + const lane = std.intdiv(lid, WINOGRAD_PAIRS); const pair = flatWorkgroupId(wid) * WINOGRAD_PAIRS + p; const live = pair < tiles * co4; const oB = pair % co4; - const t = d.u32(pair / co4); + const t = std.intdiv(pair, co4); const ms = d.arrayOf(d.vec4f, 6)(); if (live) { @@ -764,7 +764,7 @@ const makeWinogradOutKernel = (cfg: Conv2dHwc4Cfg) => { winogradRows.$[sb + 16 + lane]!, winogradRows.$[sb + 20 + lane]!, ); - const y0 = d.u32(t / tilesX) * 4; + const y0 = std.intdiv(t, tilesX) * 4; for (const r of tgpu.unroll(std.range(4))) { const y = y0 + r; if (y < dims.h) { @@ -976,15 +976,15 @@ const makeMaxPoolHwc4Kernel = (cfg: MaxPool2dHwc4Cfg) => { })(({ gid }) => { 'use gpu'; const dims = moveLayout.$.dims; - const hOut = d.u32((dims.h + 2 * padding - k) / stride) + 1; - const wOut = d.u32((dims.w + 2 * padding - k) / stride) + 1; + const hOut = std.intdiv(dims.h + 2 * padding - k, stride) + 1; + const wOut = std.intdiv(dims.w + 2 * padding - k, stride) + 1; const idx = flatIndex(gid); if (idx >= dims.c4 * hOut * wOut) { return; } const cB = idx % dims.c4; - const ox = d.u32(idx / dims.c4) % wOut; - const oy = d.u32(idx / (dims.c4 * wOut)); + const ox = std.intdiv(idx, dims.c4) % wOut; + const oy = std.intdiv(idx, dims.c4 * wOut); let best = d.vec4h(-65504.0); // f16 lowest; every window has >= 1 valid tap for (let ky = d.u32(0); ky < k; ky++) { @@ -1017,10 +1017,10 @@ const makeUpsampleHwc4Kernel = (scale: number) => return; } const cB = idx % dims.c4; - const ox = d.u32(idx / dims.c4) % wOut; - const oy = d.u32(idx / (dims.c4 * wOut)); + const ox = std.intdiv(idx, dims.c4) % wOut; + const oy = std.intdiv(idx, dims.c4 * wOut); moveLayout.$.out[(oy * wOut + ox) * dims.outC4 + dims.outOffB + cB] = d.vec4h( - moveLayout.$.x[(d.u32(oy / scale) * dims.w + d.u32(ox / scale)) * dims.c4 + cB]!, + moveLayout.$.x[(std.intdiv(oy, scale) * dims.w + std.intdiv(ox, scale)) * dims.c4 + cB]!, ); }); @@ -1033,15 +1033,15 @@ const makeAvgPoolHwc4Kernel = (cfg: AvgPool2dHwc4Cfg) => { })(({ gid }) => { 'use gpu'; const dims = moveLayout.$.dims; - const hOut = d.u32((dims.h - k) / stride) + 1; - const wOut = d.u32((dims.w - k) / stride) + 1; + const hOut = std.intdiv(dims.h - k, stride) + 1; + const wOut = std.intdiv(dims.w - k, stride) + 1; const idx = flatIndex(gid); if (idx >= dims.c4 * hOut * wOut) { return; } const cB = idx % dims.c4; - const ox = d.u32(idx / dims.c4) % wOut; - const oy = d.u32(idx / (dims.c4 * wOut)); + const ox = std.intdiv(idx, dims.c4) % wOut; + const oy = std.intdiv(idx, dims.c4 * wOut); // f32 accumulation: a k² sum of f16 values drifts too far at k up to 8. let acc = d.vec4f(); @@ -1070,8 +1070,8 @@ const makePad2dHwc4Kernel = (padH: number, padW: number) => return; } const cB = idx % dims.c4; - const ox = d.u32(idx / dims.c4) % wOut; - const oy = d.u32(idx / (dims.c4 * wOut)); + const ox = std.intdiv(idx, dims.c4) % wOut; + const oy = std.intdiv(idx, dims.c4 * wOut); let v = d.vec4h(); if (ox >= padW && ox < dims.w + padW && oy >= padH && oy < dims.h + padH) { v = d.vec4h(moveLayout.$.x[((oy - padH) * dims.w + (ox - padW)) * dims.c4 + cB]!); @@ -1107,8 +1107,8 @@ const makeResizeBilinearHwc4Kernel = (alignCorners: boolean) => return; } const cB = idx % dims.c4; - const ox = d.u32(idx / dims.c4) % dims.outW; - const oy = d.u32(idx / (dims.c4 * dims.outW)); + const ox = std.intdiv(idx, dims.c4) % dims.outW; + const oy = std.intdiv(idx, dims.c4 * dims.outW); let sy = d.f32(0); let sx = d.f32(0); @@ -1153,7 +1153,7 @@ const makeChannelAffineHwc4Kernel = (hasShift: boolean) => })(({ gid }) => { 'use gpu'; const dims = affineLayout.$.dims; - const c4 = d.u32((dims.c + 3) / 4); + const c4 = std.intdiv(dims.c + 3, 4); const idx = flatIndex(gid); if (idx >= dims.h * dims.w * c4) { return; @@ -1297,7 +1297,7 @@ const copyChHwc4Kernel = tgpu.computeFn({ if (idx >= dims.pElems * dims.nB) { return; } - const px = d.u32(idx / dims.nB); + const px = std.intdiv(idx, dims.nB); const b = idx % dims.nB; copyLayout.$.out[px * dims.dstC4 + dims.dstOffB + b] = d.vec4h( copyLayout.$.x[px * dims.srcC4 + dims.srcOffB + b]!, @@ -1352,12 +1352,12 @@ const makeToHwc4Kernel = (srcKey: 'f32' | 'f16') => { 'use gpu'; const dims = layout.$.dims; const p = dims.h * dims.w; - const c4 = d.u32((dims.c + 3) / 4); + const c4 = std.intdiv(dims.c + 3, 4); const idx = flatIndex(gid); if (idx >= p * c4) { return; } - const px = d.u32(idx / c4); + const px = std.intdiv(idx, c4); const cB = idx % c4; const c0 = cB * 4; const zero = d.f16(0); @@ -1390,12 +1390,12 @@ const makeToChwKernel = (dstKey: 'f32' | 'f16') => { 'use gpu'; const dims = layout.$.dims; const p = dims.h * dims.w; - const c4 = d.u32((dims.c + 3) / 4); + const c4 = std.intdiv(dims.c + 3, 4); const idx = flatIndex(gid); if (idx >= p * c4) { return; } - const px = d.u32(idx / c4); + const px = std.intdiv(idx, c4); const cB = idx % c4; const v = layout.$.x[px * c4 + cB]!; const c0 = cB * 4; diff --git a/packages/runntime/src/core/kernels/elementwise/swigluChunk.ts b/packages/runntime/src/core/kernels/elementwise/swigluChunk.ts index 03fcdbad..f0b417ba 100644 --- a/packages/runntime/src/core/kernels/elementwise/swigluChunk.ts +++ b/packages/runntime/src/core/kernels/elementwise/swigluChunk.ts @@ -36,7 +36,7 @@ function makeSwigluChunkVariant(elem: Elem) { const half = layout.$.dims.half; const i = flatIndex(gid); if (i >= total) return; - const row = d.u32(i / half); + const row = std.intdiv(i, half); const c = i % half; const base = row * 2 * half; const hidden = d.f32(layout.$.x[base + c]!); diff --git a/packages/runntime/src/core/kernels/image/imageToChw.ts b/packages/runntime/src/core/kernels/image/imageToChw.ts index a2ef143f..3c103045 100644 --- a/packages/runntime/src/core/kernels/image/imageToChw.ts +++ b/packages/runntime/src/core/kernels/image/imageToChw.ts @@ -103,7 +103,7 @@ const makeImageToChwKernel = (source: typeof textureSource) => offset = ((side - img) / 2) * scale; } - const o = d.vec2f(d.f32(idx % P.outW), d.f32(d.u32(idx / P.outW))); + const o = d.vec2f(d.f32(idx % P.outW), d.f32(std.intdiv(idx, P.outW))); // Image coordinate of this input pixel's center, in image pixels. const s = (o + 0.5 - offset) / scale; let v = d.vec3f(P.padR, P.padG, P.padB); diff --git a/packages/runntime/src/core/kernels/matmul/argmaxDot.ts b/packages/runntime/src/core/kernels/matmul/argmaxDot.ts index dae80a33..62115c91 100644 --- a/packages/runntime/src/core/kernels/matmul/argmaxDot.ts +++ b/packages/runntime/src/core/kernels/matmul/argmaxDot.ts @@ -46,7 +46,7 @@ function makeArgmaxDotVariant(elem: Elem) { const cols = config.$.cols; const tid = lid.x; const kl = tid % KL; - const rl = d.u32(tid / KL); + const rl = std.intdiv(tid, KL); // Rows scan in increasing order, so a strict > keeps the lowest winning // index. The step condition is workgroup-uniform, so every thread reaches diff --git a/packages/runntime/src/core/kernels/matmul/matmul.ts b/packages/runntime/src/core/kernels/matmul/matmul.ts index 25255caf..74665633 100644 --- a/packages/runntime/src/core/kernels/matmul/matmul.ts +++ b/packages/runntime/src/core/kernels/matmul/matmul.ts @@ -88,12 +88,12 @@ export function makeMatmulKernel(cfg: MatmulCfg, elem: Elem = F32_ELEM) { })(({ gid }) => { 'use gpu'; const m = L.$.dims.m; - const mQuads = d.u32((m + 3) / 4); // explicit integer division (codebase convention) + const mQuads = std.intdiv(m + 3, 4); const idx = flatIndex(gid); if (idx >= mQuads * n4c) { return; } - const r0 = d.u32(idx / n4c) * 4; + const r0 = std.intdiv(idx, n4c) * 4; const col0 = (idx % n4c) * 4; // Clamped so loads stay in bounds; the writeback discards duplicates. const r1 = std.min(r0 + 1, m - 1); @@ -324,7 +324,7 @@ function makeMatmulTiledKernel(cfg: MatmulCfg) { 'use gpu'; const m = matmulLayout.$.dims.m; const tid = lid.x; - const tr = d.u32(tid / 16); // explicit integer division (codebase convention) + const tr = std.intdiv(tid, 16); const tc = tid % 16; const rowBase = wid.y * TILE; const colBase = wid.x * TILE; @@ -342,7 +342,7 @@ function makeMatmulTiledKernel(cfg: MatmulCfg) { // become 0, so the inner loop needs no guards. for (const i of tgpu.unroll(std.range(A_LOADS))) { const e = tid + i * TILED_THREADS; - const er = d.u32(e / TILE_K); + const er = std.intdiv(e, TILE_K); const ec = e % TILE_K; const ar = rowBase + er; const ak = k0 + ec; @@ -351,7 +351,7 @@ function makeMatmulTiledKernel(cfg: MatmulCfg) { } for (const iv of tgpu.unroll(std.range(B_LOADS))) { const e = tid + iv * TILED_THREADS; // vec4 slot: row-major [TILE_K, TILE/4] - const er = d.u32(e / B_TILE_ROW_VEC4S); + const er = std.intdiv(e, B_TILE_ROW_VEC4S); const ec = (e % B_TILE_ROW_VEC4S) * 4; const bk = k0 + er; const bc = colBase + ec; diff --git a/packages/runntime/src/core/kernels/matmul/matmulGemv.ts b/packages/runntime/src/core/kernels/matmul/matmulGemv.ts index 8d312fe1..a39d8095 100644 --- a/packages/runntime/src/core/kernels/matmul/matmulGemv.ts +++ b/packages/runntime/src/core/kernels/matmul/matmulGemv.ts @@ -51,7 +51,7 @@ function makeGemvVariant(elem: Elem) { const n = config.$.n; const tid = lid.x; const colLane = tid % TN; - const kLane = d.u32(tid / TN); // explicit integer division (codebase convention) + const kLane = std.intdiv(tid, TN); const col = flatWorkgroupId(wid) * TN + colLane; let acc = d.f32(0); @@ -191,7 +191,7 @@ function makeGemvVec4Kernel(cfg: GemvVec4Cfg, elem: Elem) { })(({ wid, lid }) => { 'use gpu'; const lane = lid % VEC_LANES; - const slice = d.u32(lid / VEC_LANES); + const slice = std.intdiv(lid, VEC_LANES); const cg = wid.x * VEC_LANES + lane; let acc = d.vec4f(); diff --git a/packages/runntime/src/core/kernels/matmul/matmulTiledVec4.ts b/packages/runntime/src/core/kernels/matmul/matmulTiledVec4.ts index a27cdf8b..2f4dd46b 100644 --- a/packages/runntime/src/core/kernels/matmul/matmulTiledVec4.ts +++ b/packages/runntime/src/core/kernels/matmul/matmulTiledVec4.ts @@ -120,7 +120,7 @@ function makeMatmulTiledVec4Kernel(cfg: MatmulTiledVec4Cfg, elem: Elem) { 'use gpu'; const m = layout.$.dims.m; const tx = lid % WGC; - const ty = d.u32(lid / WGC); + const ty = std.intdiv(lid, WGC); const mBase = wid.y * BM; const nBase4 = wid.x * bn4; diff --git a/packages/runntime/src/core/kernels/matmul/moeQuantW.ts b/packages/runntime/src/core/kernels/matmul/moeQuantW.ts index 4f80a3f5..a96cc15e 100644 --- a/packages/runntime/src/core/kernels/matmul/moeQuantW.ts +++ b/packages/runntime/src/core/kernels/matmul/moeQuantW.ts @@ -85,7 +85,7 @@ const tileDots = (w0: number, w1: number, w2: number, w3: number, x: d.v4f) => { /** Word offset of column c inside a weight tile. */ const colWord = (c: number) => { 'use gpu'; - return d.u32((c * config.$.bits * 4) / 32); + return std.intdiv(c * config.$.bits * 4, 32); }; /** Adds partialA (and partialB) over the K lanes of each unit, so lane 0 @@ -126,11 +126,11 @@ const moeUpKernel = tgpu.computeFn({ 'use gpu'; const C = config.$; const lane = lid % K_LANES; - const unit = wid.x * UNITS + d.u32(lid / K_LANES); + const unit = wid.x * UNITS + std.intdiv(lid, K_LANES); // The grid is at most 65535 rows high; longer inputs loop. const rows = moeUpLayout.$.dims.tokens * MOE_SLOTS; for (let row = wid.y; row < rows; row += MAX_WORKGROUPS_PER_DIM) { - const t = d.u32(row / MOE_SLOTS); + const t = std.intdiv(row, MOE_SLOTS); const expert = d.u32(moeUpLayout.$.route[t * 2 * MOE_SLOTS + (row % MOE_SLOTS)]!); let glu = d.vec4f(); @@ -140,7 +140,7 @@ const moeUpKernel = tgpu.computeFn({ const sRow = expert * C.kg; for (let k4 = lane; k4 < C.k4count; k4 += K_LANES) { const x = moeUpLayout.$.x[t * C.k4count + k4]!; - const s = (sRow + d.u32(k4 / C.g4)) * C.n4 + unit; + const s = (sRow + std.intdiv(k4, C.g4)) * C.n4 + unit; const tb = wBase + k4 * C.tileWords; const g = tileDots( moeUpLayout.$.gluW[tb]!, @@ -192,7 +192,7 @@ const moeDownKernel = tgpu.computeFn({ 'use gpu'; const C = config.$; const lane = lid % K_LANES; - const unit = wid.x * UNITS + d.u32(lid / K_LANES); + const unit = wid.x * UNITS + std.intdiv(lid, K_LANES); // The grid is at most 65535 rows high; longer inputs loop. for (let t = wid.y; t < moeDownLayout.$.dims.tokens; t += MAX_WORKGROUPS_PER_DIM) { const r = t * 2 * MOE_SLOTS; @@ -208,7 +208,7 @@ const moeDownKernel = tgpu.computeFn({ let dot = d.vec4f(); for (let k4 = lane; k4 < C.k4count; k4 += K_LANES) { const tb = wBase + k4 * C.tileWords; - const sc = (sRow + d.u32(k4 / C.g4)) * C.n4 + unit; + const sc = (sRow + std.intdiv(k4, C.g4)) * C.n4 + unit; const v = tileDots( moeDownLayout.$.w[tb]!, moeDownLayout.$.w[tb + colWord(1)]!, diff --git a/packages/runntime/src/core/kernels/shape/columns.ts b/packages/runntime/src/core/kernels/shape/columns.ts index 17f7bd1e..47317616 100644 --- a/packages/runntime/src/core/kernels/shape/columns.ts +++ b/packages/runntime/src/core/kernels/shape/columns.ts @@ -1,4 +1,4 @@ -import tgpu, { d } from 'typegpu'; +import tgpu, { d, std } from 'typegpu'; import type { TgpuRoot } from 'typegpu'; import { type FloatBuffer, @@ -31,7 +31,7 @@ function makeVariant(elem: Elem) { if (i >= meta.total) { return; } - const r = d.u32(i / meta.outCols); // explicit integer division (codebase convention) + const r = std.intdiv(i, meta.outCols); const c = i % meta.outCols; sliceLayout.$.out[i] = sliceLayout.$.x[r * meta.srcCols + meta.start + c]!; }); @@ -54,7 +54,7 @@ function makeVariant(elem: Elem) { return; } const outCols = meta.aCols + meta.bCols; - const r = d.u32(i / outCols); // explicit integer division (codebase convention) + const r = std.intdiv(i, outCols); const c = i % outCols; if (c < meta.aCols) { concatLayout.$.out[i] = concatLayout.$.a[r * meta.aCols + c]!; diff --git a/packages/runntime/src/core/kernels/ssm.ts b/packages/runntime/src/core/kernels/ssm.ts index 6f95af43..9d9cb086 100644 --- a/packages/runntime/src/core/kernels/ssm.ts +++ b/packages/runntime/src/core/kernels/ssm.ts @@ -20,7 +20,7 @@ const sourcePixel = (direction: number, position: number, width: number, height: t = width * height - 1 - t; } if (direction === 1 || direction === 3) { - return (t % height) * width + d.u32(t / height); + return (t % height) * width + std.intdiv(t, height); } return t; }; @@ -49,8 +49,8 @@ export const ssmScanProjectXKernel = tgpu.computeFn({ return; } const pos = thread % P; - const row = d.u32(thread / P) % rows; - const dir = d.u32(thread / (P * rows)); + const row = std.intdiv(thread, P) % rows; + const dir = std.intdiv(thread, P * rows); const pixel = sourcePixel(dir, pos, D.w, D.h); let acc = d.f32(0); @@ -84,8 +84,8 @@ export const ssmScanProjectDtKernel = tgpu.computeFn({ return; } const pos = thread % P; - const co = d.u32(thread / P) % D.c; - const dir = d.u32(thread / (P * D.c)); + const co = std.intdiv(thread, P) % D.c; + const dir = std.intdiv(thread, P * D.c); const dtBase = SSM_DIRS * D.c + 2 * SSM_DIRS * SSM_STATE + dir * D.rank; let acc = d.f32(0); @@ -133,7 +133,7 @@ export const ssmSelectiveScanKernel = tgpu.computeFn({ if (seq >= SSM_DIRS * D.c) { return; } - const dir = d.u32(seq / D.c); + const dir = std.intdiv(seq, D.c); const ch = seq % D.c; const bBase = SSM_DIRS * D.c + dir * SSM_STATE; const cBase = bBase + SSM_DIRS * SSM_STATE; @@ -186,7 +186,7 @@ export const ssmScanMergeKernel = tgpu.computeFn({ if (thread >= D.c * P) { return; } - const ch = d.u32(thread / P); + const ch = std.intdiv(thread, P); const pixel = thread % P; // Unrolled, so `dir` is comptime and each direction keeps only its own // address arithmetic. @@ -194,7 +194,7 @@ export const ssmScanMergeKernel = tgpu.computeFn({ for (const dir of tgpu.unroll(std.range(SSM_DIRS))) { let position = pixel; if (dir === 1 || dir === 3) { - position = (pixel % D.w) * D.h + d.u32(pixel / D.w); + position = (pixel % D.w) * D.h + std.intdiv(pixel, D.w); } if (dir >= 2) { position = P - 1 - position; diff --git a/packages/runntime/test/core/kernels/attention/attn.gpu.test.ts b/packages/runntime/test/core/kernels/attention/attn.gpu.test.ts index b11b4597..16602d68 100644 --- a/packages/runntime/test/core/kernels/attention/attn.gpu.test.ts +++ b/packages/runntime/test/core/kernels/attention/attn.gpu.test.ts @@ -253,14 +253,17 @@ describe('attn kernels', () => { await checkSdpa(deviceFor(ctx, device, dtype, subgroups), c, dtype); }); - // Known bug: the batch kernel splits its flat index into row and head with - // d.u32(idx / qHeads), which TypeGPU compiles to u32(f32(idx) / - // f32(qHeads)). f32 counts exactly only up to 2^24, so past 2^24 row-heads - // rows come out wrong. Turn into a plain `it` with the fix. + // f32 counts exactly only up to 2^24, so the flat row-head index past it + // must be split with integer division or rows come out wrong. // batch: past GRID_Y_STRIDE row-heads - it.fails('past 2^24 row-heads (batch)', () => - checkSdpa(withoutSubgroups, { qLen: 2 ** 24 + 3, kvLen: 2, qHeads: 1, kvHeads: 1, headDim: 1 }), - ); + it('past 2^24 row-heads (batch)', () => + checkSdpa(withoutSubgroups, { + qLen: 2 ** 24 + 3, + kvLen: 2, + qHeads: 1, + kvHeads: 1, + headDim: 1, + })); it('rejects qLen = 0 with an INVALID_ARGUMENT', () => { expect(() => upload(root, new Float32Array(0), [0, 8])).toThrow( diff --git a/packages/runntime/test/core/kernels/attention/rope.gpu.test.ts b/packages/runntime/test/core/kernels/attention/rope.gpu.test.ts index 5a741999..46efe0f2 100644 --- a/packages/runntime/test/core/kernels/attention/rope.gpu.test.ts +++ b/packages/runntime/test/core/kernels/attention/rope.gpu.test.ts @@ -124,12 +124,10 @@ describe('rope kernel', () => { await checkRope({ rows, cols, headDim: 64 }); }); - // Known bug: the kernel splits the flat pair index into row and pair with - // d.u32(idx / pairs), which TypeGPU compiles to u32(f32(idx) / f32(pairs)). - // f32 counts exactly only up to 2^24, so past 2^24 pairs rows come out - // wrong. f16 is the dtype whose 2^25 elements still fit one binding. Turn - // into a plain `it` with the fix. - it.fails('f16 past 2^24 pairs', async (ctx) => { + // f32 counts exactly only up to 2^24, so the flat pair index past it must be + // split with integer division or rows come out wrong. f16 is the dtype + // whose 2^25 elements still fit one binding. + it('f16 past 2^24 pairs', async (ctx) => { requireFeature(ctx, root, 'shader-f16'); const rows = 2 ** 14 + 1; const cols = 2048; diff --git a/packages/runntime/test/core/kernels/conv/convTranspose2d.gpu.test.ts b/packages/runntime/test/core/kernels/conv/convTranspose2d.gpu.test.ts index a9845ec3..1828db54 100644 --- a/packages/runntime/test/core/kernels/conv/convTranspose2d.gpu.test.ts +++ b/packages/runntime/test/core/kernels/conv/convTranspose2d.gpu.test.ts @@ -87,11 +87,9 @@ describe('convTranspose2d kernel', () => { it('runs past GRID_Y_STRIDE outputs, in the second grid dimension', () => checkConvTranspose({ cIn: 2, cOut: 1, h: 1024, w: 1025, k: 2 }, 'f32')); - // Known bug: the kernel splits the flat output index into channel, row and - // column with d.u32(idx / wOut), which TypeGPU compiles to u32(f32(idx) / - // f32(wOut)). f32 counts exactly only up to 2^24, so past it outputs come - // from the wrong pixel. Turn into a plain `it` with the fix. - it.fails('runs past 2^24 outputs', async () => { + // f32 counts exactly only up to 2^24, so the flat output index past it must + // be split with integer division or outputs come from the wrong pixel. + it('runs past 2^24 outputs', async () => { expect(4098 * 4096).toBeGreaterThan(2 ** 24); await checkConvTranspose({ cIn: 2, cOut: 1, h: 2049, w: 2048, k: 2 }, 'f32'); }); diff --git a/packages/runntime/test/core/kernels/elementwise/swigluChunk.gpu.test.ts b/packages/runntime/test/core/kernels/elementwise/swigluChunk.gpu.test.ts index dc7da866..39a8fd29 100644 --- a/packages/runntime/test/core/kernels/elementwise/swigluChunk.gpu.test.ts +++ b/packages/runntime/test/core/kernels/elementwise/swigluChunk.gpu.test.ts @@ -60,12 +60,10 @@ describe('swigluChunk kernel', () => { await checkSwigluChunk(2048, 2049, 'f32'); }); - // Known bug: the kernel splits the flat output index into row and column - // with d.u32(i / half), which TypeGPU compiles to u32(f32(i) / f32(half)). - // f32 counts exactly only up to 2^24, so past 2^24 outputs it reads the - // wrong row. f16 is the dtype whose 2^25 inputs still fit one binding. - // Turn into a plain `it` with the fix. - it.fails('f16 past 2^24 outputs', async (ctx) => { + // f32 counts exactly only up to 2^24, so the flat output index past it must + // be split with integer division or it reads the wrong row. f16 is the + // dtype whose 2^25 inputs still fit one binding. + it('f16 past 2^24 outputs', async (ctx) => { requireFeature(ctx, root, 'shader-f16'); expect(4100 * 4097).toBeGreaterThan(2 ** 24); await checkSwigluChunk(4100, 4097, 'f16'); diff --git a/packages/runntime/test/core/kernels/shape/columns.gpu.test.ts b/packages/runntime/test/core/kernels/shape/columns.gpu.test.ts index 4495d023..2a057a9b 100644 --- a/packages/runntime/test/core/kernels/shape/columns.gpu.test.ts +++ b/packages/runntime/test/core/kernels/shape/columns.gpu.test.ts @@ -112,11 +112,9 @@ describe('columns kernels', () => { await sliceAndCat(2048, 4097); }); - // Known bug: the kernels split the flat index into row and column with - // d.u32(i / cols), which TypeGPU compiles to u32(f32(i) / f32(cols)). f32 - // counts exactly only up to 2^24, so past it rows and columns come out - // wrong. Turn into a plain `it` with the fix. - it.fails('runs past 2^24 elements', async () => { + // f32 counts exactly only up to 2^24, so the flat index past it must be + // split with integer division or rows and columns come out wrong. + it('runs past 2^24 elements', async () => { expect(4100 * 4096).toBeGreaterThan(2 ** 24); await sliceAndCat(4100, 4097); }); diff --git a/packages/runntime/test/core/kernels/ssm.gpu.test.ts b/packages/runntime/test/core/kernels/ssm.gpu.test.ts index 36f5d3d6..3fac215b 100644 --- a/packages/runntime/test/core/kernels/ssm.gpu.test.ts +++ b/packages/runntime/test/core/kernels/ssm.gpu.test.ts @@ -245,11 +245,9 @@ describe('ssm kernels', () => { expectClose(got, mergeRef(directional, s), { atol: 0, rtol: 0 }); }); - // Known bug: the kernels split the flat thread index with d.u32(thread / P), - // which TypeGPU compiles to u32(f32(thread) / f32(P)). f32 counts exactly - // only up to 2^24, so past 2^24 delta threads (4·C·P) some land on the - // wrong channel. Turn into a plain `it` with the fix. - it.fails('ssmScanProject past 2^24 delta threads', async () => { + // f32 counts exactly only up to 2^24, so the flat thread index past it must + // be split with integer division or threads land on the wrong channel. + it('ssmScanProject past 2^24 delta threads', async () => { const s = { c: 64, h: 200, w: 330, rank: 1 }; const p = s.h * s.w; expect(SSM_DIRS * s.c * p).toBeGreaterThan(2 ** 24); From 8bdbb2eab7ab258e39bd81a9db6a95f516221c24 Mon Sep 17 00:00:00 2001 From: MiniX16 Date: Sat, 14 Nov 2026 14:51:43 +0100 Subject: [PATCH 2/2] fix(core): keep colWord u32 and test hwc4 layouts with four-channel block counts that are not a power of two colWord is called with literals, so std.intdiv on its i32 argument returned i32 where d.u32 returned u32. The toHwc4/toChw round-trip and channelAffine cases use c = 10 (c4 = 3), which fails on main on GPUs whose f32 division is not exact for such divisors. --- packages/runntime/src/core/kernels/matmul/moeQuantW.ts | 2 +- packages/runntime/test/core/kernels/conv/hwc4.gpu.test.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/runntime/src/core/kernels/matmul/moeQuantW.ts b/packages/runntime/src/core/kernels/matmul/moeQuantW.ts index a96cc15e..f18f5904 100644 --- a/packages/runntime/src/core/kernels/matmul/moeQuantW.ts +++ b/packages/runntime/src/core/kernels/matmul/moeQuantW.ts @@ -85,7 +85,7 @@ const tileDots = (w0: number, w1: number, w2: number, w3: number, x: d.v4f) => { /** Word offset of column c inside a weight tile. */ const colWord = (c: number) => { 'use gpu'; - return std.intdiv(c * config.$.bits * 4, 32); + return std.intdiv(d.u32(c) * config.$.bits * 4, 32); }; /** Adds partialA (and partialB) over the K lanes of each unit, so lane 0 diff --git a/packages/runntime/test/core/kernels/conv/hwc4.gpu.test.ts b/packages/runntime/test/core/kernels/conv/hwc4.gpu.test.ts index aa48654a..421bfb2a 100644 --- a/packages/runntime/test/core/kernels/conv/hwc4.gpu.test.ts +++ b/packages/runntime/test/core/kernels/conv/hwc4.gpu.test.ts @@ -787,6 +787,8 @@ describe('hwc4 kernels', () => { { c: 1, h: 1, w: 1 }, { c: 5, h: 3, w: 7 }, { c: 8, h: 4, w: 4 }, + // c4 = 3: a block count that f32 division can split wrong. + { c: 10, h: 3, w: 5 }, ].map((s) => ({ from, to, ...s })), ), ), @@ -803,7 +805,9 @@ describe('hwc4 kernels', () => { it.for([false, true])('channelAffine, shift %s', async (withShift, ctx) => { needF16(ctx); - const [c, h, w] = [6, 3, 5]; + const [c, h, w] = [10, 3, 5]; + // c4 = 3: a block count that f32 division can split wrong, and a partial + // last block. const x = image(c, h, w, 1); const scale = randomF32(c, 2); const shift = randomF32(c, 3);