From 50fa397db7f2447680ee9aea6e5adddd0b750936 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:44:44 +0900 Subject: [PATCH] poly: Inkling 2's prompt attention on Turing in attn_mma_kernel's shape ink_flash75_kernel (PR 209) takes 2.8 ms for one 1,024-row call of a sliding-window layer on an RTX 2070 Super, where attn_mma_kernel of our earlier Inkling engine (PR 200) takes about 1 ms over the same keys. The tensor-core steps are the same in both; the time goes to the work around them: - the relative-position bias: ink_flash75_kernel makes it a 16-term float dot product per score, read from shared memory as 4 float4s (16 FMAs and 4 LDS.128 for every score, every tile of a window layer, since its extent is the window); - the softmax in natural units (expf) and int64 masks on every score; - 213 registers and 6 warps a block: 1 block, 6 warps an SM, so the plain tile copies and the two barriers a tile are not hidden. ink_mma75_kernel is attn_mma_kernel's shape (7f3f4574f, 16f3585bd): 4 warps of 16 rows a block, rows interleaved token-major over the key head's G query heads, 32-key tiles counted from position 0. The bias is a product on the tensor cores: the warp's R (log2 units) times the table rows of the tile's distances, both split into a half and the half of what is left (hi*hi + lo*hi + hi*lo, about float rounding), read once a score from a per-warp float tile. The softmax runs in log2 units, and a tile that every row of a warp attends whole skips its masks. 219 registers, 27 KB shared memory: 2 blocks, 8 warps an SM. The query is rounded to halves unscaled, the sink is subtracted from each score, the weights are rounded to halves and summed as halves, the softmax has the term of score 0 and the gate comes last, as in ink_flash75_kernel. poly_ink_flash_test --bench, RTX 2070 Super, one 1,024-row call: window layer 2.84 -> 1.01 ms, full layer at 2K keys 2.95 -> 1.20 ms, 8K 9.95 -> 4.62 ms, 32K 38.9 -> 18.1 ms. Prompt tok/s (3 interleaved runs, IQ4_NL): +2.3% at 2K, +5.6% at 8K, +9.9% at 32K. Class: rounding (fp16 MMA, fp32 accumulate), the same class as 209. Upstream's accuracy check (1636925ea: 3 prompts of 302 to 8,192 tokens, 64 tokens, IQ4_NL, UD-Q2_K_XL loop and an IQ3_S; RTX 2070 8 GB, experts frozen) on main 5b080f37b: the most likely token is 209's greedy answer at 566 of 576 steps (98.3%) and attend_batch()'s (STRATA_INK_FLASH=0) at 567 (98.4%), where 209's own kernel gives 569 (98.8%); two runs gave the same bits. It is the default on Turing; STRATA_INK_MMA75=0 goes back to ink_flash75_kernel (main's bits, checked on all 9 cases). sm_80+ and HIP are unchanged. poly_ink_flash_test gains a split-call test: the same rows in one call and in several (run last call first), every output bit equal; the splits move rows to other block, warp and row slots for G = 1 to 4, full and window layers, a window below the tile (11 cases). Window cases for G = 2 and 4 and a 20-position window are added to the float64 checks. Run it with and without STRATA_INK_MMA75=1 on Turing. Credits: the kernel shape and the bias on the tensor cores are attn_mma_kernel from our PR 200 (7f3f4574f, 16f3585bd, dbd42271f, c69df6a42); the arithmetic and the Turing kernel it replaces are VecSzn's ink_flash_kernel and PR 209's ink_flash75_kernel. Co-Authored-By: Claude Opus 5.5 --- docs/SETTINGS.md | 3 +- docs/models/inkling.md | 6 + engine/src/poly/ink_flash_test.cu | 82 +++++++++- engine/src/poly/inkling2.cu | 220 ++++++++++++++++++++++++++- terminal/data/settings-register.json | 35 +++++ 5 files changed, 343 insertions(+), 3 deletions(-) diff --git a/docs/SETTINGS.md b/docs/SETTINGS.md index 9e36ee8400..8605ae1e1b 100644 --- a/docs/SETTINGS.md +++ b/docs/SETTINGS.md @@ -365,7 +365,7 @@ Off unless you set them. The last column says where each one is described. | `STRATA_WDDM_BUDGET` | number | AMD, Intel Arc | [AMD_HIP.md](AMD_HIP.md) | | `STRATA_WDDM_MARGIN_MIB` | number | AMD | [AMD_HIP.md](AMD_HIP.md) | -## Switches that go back to earlier behaviour (119) +## Switches that go back to earlier behaviour (120) These behaviours are on by default. Setting the switch returns the earlier behaviour; the notes in the register say which version made it the default. @@ -405,6 +405,7 @@ version made it the default. | `STRATA_IL_PAD` | number | NVIDIA and AMD | v0.2.16 | - | | `STRATA_INK_FLASH` | on / off | NVIDIA and AMD | - | - | | `STRATA_INK_KERNEL_TABLES` | on / off | NVIDIA | - | [inkling.md](models/inkling.md) | +| `STRATA_INK_MMA75` | on / off | NVIDIA and AMD | - | [inkling.md](models/inkling.md) | | `STRATA_INK_PROJECT_MIXED` | on / off | NVIDIA | - | - | | `STRATA_IQ3_LANES` | on / off | NVIDIA and AMD | - | - | | `STRATA_IQ_MT_MIN` | on / off | NVIDIA and AMD, CPU | v0.2.16 | [BATCHING.md](BATCHING.md), [DETAILS.md](DETAILS.md), [OLDER_GPUS.md](OLDER_GPUS.md) | diff --git a/docs/models/inkling.md b/docs/models/inkling.md index 979761787a..58411b8c01 100644 --- a/docs/models/inkling.md +++ b/docs/models/inkling.md @@ -79,6 +79,12 @@ whatever its launch, so the answers are the same to the bit. It was measured +1. Ti (22 GB, every expert in VRAM) and no change on an RTX 2070 (8 GB). `STRATA_POLY_EXPERT_TILES=0` goes back to a launch for each run. +On an RTX 20 card a prompt's attention runs in a tensor-core kernel shaped for Turing: one layer's call of 1,024 rows +takes 1.0 ms in a sliding-window layer and 18 ms in a full layer at 32K keys (RTX 2070 Super). It rounds the +attention's sums in another order than the kernel before it, which `STRATA_INK_MMA75=0` goes back to (2.8 ms and +39 ms; a 32K prompt reads about 9% slower). Against that kernel's greedy answers to the three prompts above (64 tokens +each, the three files) it gives the same most likely token at 566 of the 576 steps (98.3%). + On an RTX 30 card or newer, most of a prompt's products run on the int8 tensor cores, with the prompt's numbers rounded to 8 bits. UD-Q2_K_XL's IQ1_S experts (the gate and up of 6 layers) are the exception: they stay on halves unless `STRATA_PREFILL_IQ1S=int8` is set. On an RTX 3090 that setting reads a 2,048-token prompt 7.7% faster (2,159 diff --git a/engine/src/poly/ink_flash_test.cu b/engine/src/poly/ink_flash_test.cu index 27d6a3c699..91ed2f64ec 100644 --- a/engine/src/poly/ink_flash_test.cu +++ b/engine/src/poly/ink_flash_test.cu @@ -1,8 +1,14 @@ -// src/poly/ink_flash_test.cu - ink_attend_batch() (ink_flash_kernel, from Ampere on) against a float64 reference of the same attention, beside the two paths it stands in +// src/poly/ink_flash_test.cu - ink_attend_batch() (the flash kernels: ink_flash_kernel from Ampere on, +// ink_flash75_kernel or ink_mma75_kernel on Turing) against a float64 reference of the same attention, beside the two +// paths it stands in // for: ink_attend() (a token's scores in floats) and attend_batch() with an AttnBias (cuBLAS products, the path // Inkling2 takes when ink_attend_batch() does not). Inkling 2 Mini's heads: 128 values, 16 relative values; groups // of 1 to 4 query heads, full and window layers, ring caches, a gate, calls that start past position 0. // +// Then the same rows in one call and split over several: every output the same bits (a token's result does not depend +// on the call it is in). On Turing it tests ink_mma75_kernel, and ink_flash75_kernel with +// STRATA_INK_MMA75=0: run it both ways there. +// // usage: poly_ink_flash_test (exit 0: every case within the bounds below; 77: no flash kernel here) // each case also prints a hash of the flash kernel's output bits, which two // builds of the same arithmetic must share @@ -18,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -214,6 +221,75 @@ size_t work_floats(int rows, int n_head) { // inkling2.cpp's awork_ return heads + heads / 2 + 1 + 3 * (size_t) rows * n_head; } +// A token's result must not depend on the call it is in: the case's rows in one call, then the same rows (same caches, +// same inputs) in calls of the given sizes, one after another; every output float the same bits. +int run_split_tests(cudaStream_t st) { + struct Split { + Case c; + std::vector sizes; + }; + const Split splits[] = { + // name per n_kv pos rows window ring extent gate call sizes + {{"split window ring g3", 3, 8, 1900, 333, 1024, 1357, 1024, true}, {1, 100, 232}}, + {{"split window 1K g3", 3, 8, 3072, 1024, 1024, 2048, 1024, true}, {512, 512}}, + {{"split window 1K g3 odd", 3, 8, 3072, 1024, 1024, 2048, 1024, true}, {333, 21, 1, 669}}, + {{"split full later g3", 3, 8, 1500, 200, 0, 0, 1024, false}, {21, 64, 115}}, + {{"split full pos0 g3", 3, 8, 0, 700, 0, 0, 1024, true}, {300, 17, 383}}, + {{"split window g1", 1, 8, 999, 64, 128, 192, 128, true}, {5, 59}}, + {{"split full g2", 2, 4, 40, 130, 0, 0, 64, false}, {17, 113}}, + {{"split full g4", 4, 2, 0, 77, 0, 0, 1024, false}, {5, 72}}, + {{"split window g2", 2, 4, 300, 150, 128, 278, 128, true}, {7, 30, 113}}, + {{"split window g4", 4, 2, 500, 90, 100, 190, 100, false}, {3, 87}}, + {{"split window 20 g3", 3, 8, 64, 70, 20, 90, 20, true}, {11, 59}}, + }; + int fails = 0; + for (unsigned si = 0; si < sizeof(splits) / sizeof(splits[0]); ++si) { + const Split& sp = splits[si]; + Call x(sp.c, 4321u + si); + const size_t per_row = (size_t) x.n_head * kDim, n_out = (size_t) sp.c.rows * per_row; + float* out = zeros(n_out); + InkAttn a = x.attn(out, nullptr, 0); + if (!ink_attend_batch(a, st)) { + std::printf("%-24s flash off\n", sp.c.name); + cudaFree(out); + continue; + } + ck(cudaStreamSynchronize(st), "ink_attend_batch"); + const std::vector whole = download(out, n_out); + ck(cudaMemset(out, 0, n_out * sizeof(float)), "cudaMemset"); + // the calls last first, so a call that wrote past its rows would leave that behind + std::vector first; + int at = 0; + for (const int m : sp.sizes) first.push_back(at), at += m; + bool took = true; + for (size_t k = sp.sizes.size(); k-- > 0;) { + const int m = sp.sizes[k]; + at = first[k]; + InkAttn b = a; + b.q = x.dq + (size_t) at * per_row; + b.r = x.dr + (size_t) at * x.n_head * kRel; + b.sink = x.dsink + (size_t) at * x.n_head; + if (x.dgate) b.gate = x.dgate + (size_t) at * per_row; + b.out = out + (size_t) at * per_row; + b.pos = sp.c.pos + at; + b.rows = m; + took = ink_attend_batch(b, st) && took; + } + at = first.back() + sp.sizes.back(); + ck(cudaStreamSynchronize(st), "ink_attend_batch (split)"); + const std::vector parts = download(out, n_out); + size_t differ = 0; + for (size_t i = 0; i < n_out; ++i) differ += std::memcmp(&whole[i], &parts[i], 4) != 0; + const bool ok = took && at == sp.c.rows && differ == 0; + std::printf("%-24s one call fnv %016llx | %zu calls fnv %016llx | %zu floats differ %s\n", sp.c.name, + (unsigned long long) fnv(whole), sp.sizes.size(), (unsigned long long) fnv(parts), differ, + ok ? "ok" : "FAIL"); + fails += !ok; + cudaFree(out); + } + return fails; +} + int run_tests() { const Case cases[] = { // name per n_kv pos rows window ring extent gate @@ -228,6 +304,9 @@ int run_tests() { {"window ring odd g3", 3, 4, 2049, 45, 100, 145, 100, false}, {"window ring g1", 1, 8, 999, 64, 128, 192, 128, true}, {"few rows g3", 3, 8, 33, 32, 0, 0, 1024, false}, + {"window ring g2", 2, 4, 700, 150, 128, 278, 128, true}, + {"window ring g4", 4, 2, 333, 90, 100, 190, 100, false}, + {"window 20 g3", 3, 8, 64, 70, 20, 90, 20, true}, }; cudaStream_t st; ck(cudaStreamCreate(&st), "stream"); @@ -272,6 +351,7 @@ int run_tests() { fails += !ok; for (void* p : {(void*) out, (void*) score, (void*) work, (void*) zero}) cudaFree(p); } + fails += run_split_tests(st); blas_destroy(blas); cudaStreamDestroy(st); std::printf("%d failed\n", fails); diff --git a/engine/src/poly/inkling2.cu b/engine/src/poly/inkling2.cu index 78b288d2ac..c61230736e 100644 --- a/engine/src/poly/inkling2.cu +++ b/engine/src/poly/inkling2.cu @@ -2305,6 +2305,205 @@ __global__ void __launch_bounds__(kFastThreads, 2) ink_fast_kernel(InkAttn a) { #endif } +// The same attention on Turing in the shape of attn_mma_kernel from our earlier Inkling engine (#200's 7f3f4574f, +// 16f3585bd): a launch block is 4 warps of 16 query rows, row r of the block token r / G and query head r % G of the +// key head (16 * 4 / G tokens, a row unused for G = 3), keys go 32 at a time through one buffer. It differs from +// ink_flash75_kernel in what costs it time: the relative-position bias is a product on the tensor cores too, the +// warp's R (in log2 units) times the table's rows of the distances the tile spans, each split into a half and the +// half of what that leaves (hi*hi + lo*hi + hi*lo, summed in floats: about a float product's rounding), a warp's 16 +// rows by mma75_bias(G) distances in shared memory as floats, each score reading its one float; the softmax runs in +// log2 units (exp2f); and a tile that every row of a warp attends whole skips the masks. A score is the query rounded +// to halves times the key summed in floats, times the scale, plus the bias, less the sink; a weight is rounded to a +// half for the values' product and summed as that half; the softmax has one more term of score 0, then the gate. The +// tiles are counted from position 0 and a row's sums read nothing of the block's other rows, so a token's result does +// not depend on the call it is in (ink_flash_test's split calls). +constexpr int kMma75Warps = 4, kMma75Keys = 32; + +__host__ __device__ constexpr int mma75_tokens(int g) { return 16 * kMma75Warps / g; } +// a warp's distances a tile: its rows' tokens (at most 15 / G + 2) and the keys, in blocks of 8 +__host__ __device__ constexpr int mma75_bias(int g) { return (15 / g + 2 + kMma75Keys - 1 + 7) / 8 * 8; } + +template +__global__ void __launch_bounds__(kMma75Warps * 32) ink_mma75_kernel(InkAttn a) { +#if INK_MMA && !INK_FLASH + constexpr int DIM = 128, KEYS = kMma75Keys, ROW = kFlashRow, NR = kFlashRel, W = kMma75Warps, THREADS = W * 32; + constexpr int TOKENS = mma75_tokens(G), BIAS = mma75_bias(G); + constexpr float LOG2E = 1.44269504f; + static_assert(NR == 16, "the bias product is one k = 16 step"); + __shared__ __align__(16) __half ks[KEYS][ROW], vs[KEYS][ROW]; + __shared__ __align__(16) float bias[W][16][BIAS]; + const int lane = threadIdx.x % 32, w = threadIdx.x / 32, gid = lane / 4, tig = lane % 4; + const int g = blockIdx.y, t0 = (gridDim.x - 1 - blockIdx.x) * TOKENS; // the longest blocks first + const int nt = min(TOKENS, a.rows - t0), wt0 = 16 * w / G, wt1 = min((16 * w + 15) / G, TOKENS - 1); + const long long p0 = a.pos + t0; + int tok[2]; + bool ok[2]; + size_t th[2]; // the thread's rows gid and gid + 8 of the warp's: token, and its head's place in q, r, out +#pragma unroll + for (int i = 0; i < 2; ++i) { + const int r = 16 * w + gid + 8 * i; + tok[i] = r / G; + ok[i] = tok[i] < nt; + th[i] = ok[i] ? size_t(t0 + tok[i]) * a.n_head + g * G + r % G : 0; + } + uint32_t qa[8][4], ra[4], rl[4]; // a block of 16 values a step; R in log2 units, and what its halves leave +#pragma unroll + for (int i = 0; i < 2; ++i) +#pragma unroll + for (int u = 0; u < 2; ++u) { +#pragma unroll + for (int k = 0; k < 8; ++k) { + float2 x = make_float2(0.0f, 0.0f); + if (ok[i]) x = *reinterpret_cast(a.q + th[i] * DIM + 16 * k + 8 * u + 2 * tig); + qa[k][i + 2 * u] = ink_h2(x.x, x.y); + } + float2 x = make_float2(0.0f, 0.0f); + if (ok[i]) x = *reinterpret_cast(a.r + th[i] * NR + 8 * u + 2 * tig); + const float2 y = make_float2(x.x * LOG2E, x.y * LOG2E); + const __half2 h = __floats2half2_rn(y.x, y.y); + ra[i + 2 * u] = *reinterpret_cast(&h); + rl[i + 2 * u] = ink_h2(y.x - __low2float(h), y.y - __high2float(h)); + } + float sink[2]; +#pragma unroll + for (int i = 0; i < 2; ++i) sink[i] = ok[i] ? a.sink[th[i]] * LOG2E : 0.0f; + const float sl = a.scale * LOG2E; + float o[16][4], m[2] = {-INFINITY, -INFINITY}, l[2] = {0.0f, 0.0f}; +#pragma unroll + for (int n = 0; n < 16; ++n) o[n][0] = o[n][1] = o[n][2] = o[n][3] = 0.0f; + // the block's tokens attend positions [lo, hi): from the first one's window to the last one + const long long hi = p0 + nt, lo = a.window > 0 ? max(0LL, p0 - a.window + 1) : 0; + const bool wfull = wt1 < nt; // every row of the warp is a token of the call + const __half* kc = reinterpret_cast(a.keys); + const __half* vc = reinterpret_cast(a.vals); + for (long long k0 = lo / KEYS * KEYS; k0 < hi; k0 += KEYS) { + __syncthreads(); // the last tile's keys, values and table rows are read + for (int i = threadIdx.x; i < KEYS * DIM / 8; i += THREADS) { + const int r = i / (DIM / 8), c = 8 * (i % (DIM / 8)); + const long long key = k0 + r; + uint4 kx = make_uint4(0, 0, 0, 0), vx = kx; // zeros outside [lo, hi), which no weight reads + if (key >= lo && key < hi) { + const size_t at = (size_t(a.ring > 0 ? key % a.ring : key) * a.n_kv + g) * DIM + c; + kx = *reinterpret_cast(kc + at); + vx = *reinterpret_cast(vc + at); + } + *reinterpret_cast(&ks[r][c]) = kx; + *reinterpret_cast(&vs[r][c]) = vx; + } + // the block's distances own - key are d0 .. ; a tile with one below the extent has a bias + const long long d0 = p0 - (k0 + KEYS - 1); + const bool near = d0 < a.extent; + __syncthreads(); + float s[KEYS / 8][4]; +#pragma unroll + for (int n = 0; n < KEYS / 8; ++n) { + s[n][0] = s[n][1] = s[n][2] = s[n][3] = 0.0f; +#pragma unroll + for (int k = 0; k < 8; k += 2) { + uint32_t b[4]; // keys 8 n.., values 16 k.. and 16 (k + 1).. + ink_ldsm4(b, &ks[8 * n + lane % 8][16 * k + 8 * (lane / 8)], false); + ink_mma(s[n], qa[k], b); + ink_mma(s[n], qa[k + 1], b + 2); + } + } + if (near) { // the warp's rows times its distances: column e is distance d0 + wt0 + e +#pragma unroll + for (int n = 0; n < BIAS / 8; ++n) { + // the B fragment: the table's row of distance d0 + wt0 + 8 n + gid, values 2 tig.. and 8 + 2 tig.., + // zeros before 0 and from the extent on; as halves, and the halves of what they leave + const long long d = d0 + wt0 + 8 * n + gid; + float2 x0 = make_float2(0.0f, 0.0f), x1 = x0; + if (d >= 0 && d < a.extent) { + x0 = __ldg(reinterpret_cast(a.rel_bias + size_t(d) * NR + 2 * tig)); + x1 = __ldg(reinterpret_cast(a.rel_bias + size_t(d) * NR + 8 + 2 * tig)); + } + const __half2 h0 = __floats2half2_rn(x0.x, x0.y), h1 = __floats2half2_rn(x1.x, x1.y); + const uint32_t b[2] = {*reinterpret_cast(&h0), *reinterpret_cast(&h1)}; + float c[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + const uint32_t bl[2] = {ink_h2(x0.x - __low2float(h0), x0.y - __high2float(h0)), + ink_h2(x1.x - __low2float(h1), x1.y - __high2float(h1))}; + ink_mma(c, ra, b); // hi * hi + ink_mma(c, rl, b); // + lo * hi + ink_mma(c, ra, bl); // + hi * lo: about what the products in floats give + *reinterpret_cast(&bias[w][gid][8 * n + 2 * tig]) = make_float2(c[0], c[1]); + *reinterpret_cast(&bias[w][gid + 8][8 * n + 2 * tig]) = make_float2(c[2], c[3]); + } + __syncwarp(); + } + // whether every row of the warp attends every key of the tile: then no score needs its mask + const bool whole = wfull && k0 + KEYS - 1 <= p0 + wt0 && (a.window <= 0 || k0 > p0 + wt1 - a.window); +#pragma unroll + for (int i = 0; i < 2; ++i) { + // the row's token attends positions (own - window, own]: columns c of the tile with lo_c < c <= hi_c + const long long own = p0 + tok[i]; + const int hi_c = ok[i] ? (int) min(own - k0, (long long) KEYS) : -1; + const int lo_c = a.window > 0 ? (int) max(own - a.window - k0, -1LL) : -1; + const int e0 = tok[i] - wt0 + KEYS - 1; + float mx = -INFINITY; +#pragma unroll + for (int n = 0; n < KEYS / 8; ++n) +#pragma unroll + for (int j = 2 * i; j < 2 * i + 2; ++j) { + const int c = 8 * n + 2 * tig + j % 2; + const float b = near ? bias[w][gid + 8 * i][e0 - c] : 0.0f; + float v = fmaf(s[n][j], sl, b) - sink[i]; + if (!whole && (c > hi_c || c <= lo_c)) v = -INFINITY; + s[n][j] = v; + mx = fmaxf(mx, v); + } + mx = fmaxf(mx, __shfl_xor_sync(0xffffffffu, mx, 1)); + mx = fmaxf(mx, __shfl_xor_sync(0xffffffffu, mx, 2)); + const float now = fmaxf(m[i], mx), base = now == -INFINITY ? 0.0f : now; + const float f = exp2f(m[i] - base); + m[i] = now; + l[i] *= f; +#pragma unroll + for (int n = 0; n < 16; ++n) { + o[n][2 * i] *= f; + o[n][2 * i + 1] *= f; + } +#pragma unroll + for (int n = 0; n < KEYS / 8; ++n) +#pragma unroll + for (int j = 2 * i; j < 2 * i + 2; ++j) { + s[n][j] = __half2float(__float2half(exp2f(s[n][j] - base))); // the weight the values get + l[i] += s[n][j]; + } + } +#pragma unroll + for (int k = 0; k < KEYS / 16; ++k) { // keys 16 k..: the weights as an A fragment + const uint32_t pa[4] = {ink_h2(s[2 * k][0], s[2 * k][1]), ink_h2(s[2 * k][2], s[2 * k][3]), + ink_h2(s[2 * k + 1][0], s[2 * k + 1][1]), ink_h2(s[2 * k + 1][2], s[2 * k + 1][3])}; +#pragma unroll + for (int n = 0; n < 16; n += 2) { + uint32_t b[4]; // keys 16 k.., values 8 n.. and 8 (n + 1).. + ink_ldsm4(b, &vs[16 * k + lane % 8 + 8 * (lane / 8 % 2)][8 * n + 8 * (lane / 16)], true); + ink_mma(o[n], pa, b); + ink_mma(o[n + 1], pa, b + 2); + } + } + } +#pragma unroll + for (int i = 0; i < 2; ++i) { + l[i] += __shfl_xor_sync(0xffffffffu, l[i], 1); + l[i] += __shfl_xor_sync(0xffffffffu, l[i], 2); + if (!ok[i]) continue; + const float inv = 1.0f / (l[i] + exp2f(0.0f - m[i])); // the term of score 0, against the row's top + const size_t at = th[i] * DIM + 2 * tig; +#pragma unroll + for (int n = 0; n < 16; ++n) +#pragma unroll + for (int e = 0; e < 2; ++e) { + float v = o[n][2 * i + e] * inv; + if (a.gate) v *= 1.0f / (1.0f + expf(-a.gate[at + 8 * n + e])); // ink_mul_sigmoid_kernel's + a.out[at + 8 * n + e] = v; + } + } +#else + (void) a; // ink_attend_batch() launches it only where the PTX has the steps +#endif +} + __device__ __forceinline__ float2 situ_scales(const ExpertScales& s, int row) { if (!s.gate && !s.up) return make_float2(1.0f, 1.0f); const int e = s.ids ? s.ids[row] : s.e[row]; @@ -2850,8 +3049,27 @@ void ink_attend(const InkAttn& a, void* stream) { } #if !defined(__HIPCC__) -// ink_attend_batch() on Turing: ink_flash75_kernel, fp16 caches +// ink_attend_batch() on Turing, fp16 caches: ink_mma75_kernel, or ink_flash75_kernel with STRATA_INK_MMA75=0. On +// upstream's accuracy check (3 prompts of 302 to 8,192 tokens, 64 tokens, 3 files) ink_mma75_kernel's most likely +// token is ink_flash75_kernel's greedy answer at 98.3% of the steps and attend_batch()'s at 98.4%. static bool flash75(const InkAttn& a, int per, void* stream) { + static const bool mma = [] { + const char* v = std::getenv("STRATA_INK_MMA75"); + return !(v && v[0] == '0'); + }(); + if (mma) { + const auto launch = [&](auto kernel, int g) { + kernel<<>>(a); + }; + switch (per) { + case 1: launch(ink_mma75_kernel<1>, 1); break; + case 2: launch(ink_mma75_kernel<2>, 2); break; + case 3: launch(ink_mma75_kernel<3>, 3); break; + default: launch(ink_mma75_kernel<4>, 4); break; + } + launched("ink_mma75"); + return true; + } const auto launch = [&](auto kernel, size_t smem, int g) { const cudaError_t e = cudaFuncSetAttribute(kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) smem); if (e != cudaSuccess) throw std::runtime_error(std::string("ink_attend_batch: shared memory: ") + cudaGetErrorString(e)); diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 122d8a4ec3..6fe99d5ab4 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -16648,6 +16648,41 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_INK_MMA75", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/poly/inkling2.cu", + "line": 3057, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": "on", + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [ + "docs/models/inkling.md:84" + ], + "first_commit": null, + "first_date": null, + "since": null, + "last_commit": null, + "last_date": null, + "status": "rollback", + "owner": "", + "note": "0: Inkling 2's prompt attention on Turing (sm_75) goes back to ink_flash75_kernel (PR 209) instead of ink_mma75_kernel (attn_mma_kernel's shape, the position bias on the tensor cores as hi+lo halves; 98.3% top-1 against ink_flash75_kernel's greedy answers)", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_INK_PROJECT_MIXED", "backends": [