diff --git a/engine/src/poly/ink_flash_test.cu b/engine/src/poly/ink_flash_test.cu index 91ed2f64e..183bfbd39 100644 --- a/engine/src/poly/ink_flash_test.cu +++ b/engine/src/poly/ink_flash_test.cu @@ -11,10 +11,13 @@ // // 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 +// builds of the same arithmetic must share, and fails unless the blocks of +// 64, 48 and 32 rows (ink_flash_tokens()'s sizes) give the same bits // STRATA_FAST_ATTN=1 poly_ink_flash_test (full layers on ink_fast_kernel: twice the flash bound) -// poly_ink_flash_test --bench [keys...] (ms of one call of 1,024 rows, Inkling 2 Mini's shape, full and -// window layers, at 2K/8K/32K keys or the ones given) +// poly_ink_flash_test --bench [--rows N] [keys...] +// (ms of one call of 1,024 rows or N, Inkling 2 Mini's shape, full and window +// layers, at 2K/8K/32K keys or the ones given: the size ink_flash_tokens() +// picks, then each of 64, 48 and 32 rows a block) #include "inkling2_kernels.hpp" #include @@ -307,11 +310,13 @@ int run_tests() { {"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}, + {"prompt chunk full g3", 3, 8, 1024, 1024, 0, 0, 1024, true}, + {"prompt chunk window g3", 3, 8, 1024, 1024, 1024, 2048, 1024, true}, }; cudaStream_t st; ck(cudaStreamCreate(&st), "stream"); void* blas = blas_create(st); - int fails = 0; + int fails = 0, size_calls = 0, size_same = 0; bool ran_flash = false; for (unsigned ci = 0; ci < sizeof(cases) / sizeof(cases[0]); ++ci) { const Case& cs = cases[ci]; @@ -330,6 +335,21 @@ int run_tests() { ck(cudaStreamSynchronize(st), "ink_attend_batch"); const std::vector got_flash = download(out, n_out); ck(cudaMemset(out, 0, n_out * sizeof(float)), "cudaMemset"); + // every block size the kernel takes: the same bits as the size it picked + bool same = true; + std::string sizes; + if (flash && ink_flash_tokens(a) > 0) // ink_flash_kernel took it (not Turing's kernel or STRATA_FAST_ATTN's) + for (const int t : {64, 48, 32}) { + ck(cudaMemset(out, 0xff, n_out * sizeof(float)), "cudaMemset"); // NaNs where nothing is written + ink_attend_batch_rows(a, st, t); + ck(cudaStreamSynchronize(st), "ink_attend_batch"); + const uint64_t h = fnv(download(out, n_out)); + const bool eq = h == fnv(got_flash); + same = same && eq; + sizes += " " + std::to_string(t) + (eq ? "=" : "!"); + ++size_calls, size_same += eq; + } + ck(cudaMemset(out, 0, n_out * sizeof(float)), "cudaMemset"); ink_attend(a, st); ck(cudaStreamSynchronize(st), "ink_attend"); const std::vector got_token = download(out, n_out); @@ -342,18 +362,78 @@ int run_tests() { eb = max_err(got_batch, want); const char* fv = std::getenv("STRATA_FAST_ATTN"); const double flash_err = fv && fv[0] == '1' && cs.window <= 0 ? 2 * kFlashErr : kFlashErr; - const bool ok = (!flash || ef <= flash_err) && et <= kTokenErr && eb <= kBatchErr; + const bool ok = (!flash || ef <= flash_err) && et <= kTokenErr && eb <= kBatchErr && same; ran_flash = ran_flash || flash; std::printf("%-22s flash %s err %.3e | ink_attend err %.3e | attend_batch err %.3e | flash-batch %.3e | " - "bits %016llx %s\n", + "bits %016llx picked %d, sizes%s %s\n", cs.name, flash ? "ran" : "off", ef, et, eb, flash ? max_diff(got_flash, got_batch) : 0.0, - (unsigned long long) fnv(got_flash), ok ? "ok" : "FAIL"); + (unsigned long long) fnv(got_flash), ink_flash_tokens(a), sizes.c_str(), + ok ? "ok" : "FAIL"); fails += !ok; for (void* p : {(void*) out, (void*) score, (void*) work, (void*) zero}) cudaFree(p); } fails += run_split_tests(st); + // FP8 caches (codes and a scale a head, ink_kv8_row() bytes a row) and Q8_0 caches (int8 codes and a half scale + // a block of 32, ink_kvq8_row() bytes a row): no reference, the block sizes' bits only, where ink_flash_kernel + // takes the call (ink_flash_tokens() > 0) + for (const bool q8 : {false, true}) + for (unsigned ci = 0; ran_flash && ci < sizeof(cases) / sizeof(cases[0]); ++ci) { + const Case& cs = cases[ci]; + Call x(cs, 4321u + ci); + std::mt19937 rng(99u + ci); + const int codes = cs.n_kv * kDim; + const size_t row = q8 ? ink_kvq8_row(cs.n_kv, kDim) : ink_kv8_row(cs.n_kv, kDim); + std::vector k8((size_t) x.slots * row), v8(k8.size()); + for (std::vector* c : {&k8, &v8}) + for (long long t = 0; t < x.slots; ++t) { + uint8_t* r = c->data() + (size_t) t * row; + for (int i = 0; i < codes; ++i) // FP8: no NaN codes (exponent never 1111); Q8_0: -127..127 + r[i] = q8 ? (uint8_t) (int8_t) ((int) (rng() % 255) - 127) : (uint8_t) ((rng() % 255) & 0xf7); + for (int h = 0; h < (q8 ? codes / 32 : cs.n_kv); ++h) { + const float sc = 0.002f + 0.004f * (float) (rng() % 1000) / 1000.0f; + if (q8) { + const uint16_t hb = half_bits(sc * 64.0f / 127.0f); + std::memcpy(r + codes + h * sizeof(uint16_t), &hb, sizeof(hb)); + } else { + std::memcpy(r + codes + h * sizeof(float), &sc, sizeof(float)); + } + } + } + uint8_t* dk8 = upload(k8); + uint8_t* dv8 = upload(v8); + const size_t n_out = (size_t) cs.rows * x.n_head * kDim; + float* out = zeros(n_out); + InkAttn a = x.attn(out, nullptr, 0); + a.keys = reinterpret_cast(dk8), a.vals = reinterpret_cast(dv8); + (q8 ? a.kvq8 : a.kv8) = true; + const int picked = ink_flash_tokens(a); + if (picked > 0) { + ink_attend_batch(a, st); + ck(cudaStreamSynchronize(st), "ink_attend_batch"); + const std::vector got = download(out, n_out); + bool same = true, finite = true; + for (const float f : got) finite = finite && std::isfinite(f); + std::string sizes; + for (const int t : {64, 48, 32}) { + ck(cudaMemset(out, 0xff, n_out * sizeof(float)), "cudaMemset"); + ink_attend_batch_rows(a, st, t); + ck(cudaStreamSynchronize(st), "ink_attend_batch"); + const bool eq = fnv(download(out, n_out)) == fnv(got); + same = same && eq; + sizes += " " + std::to_string(t) + (eq ? "=" : "!"); + ++size_calls, size_same += eq; + } + const bool ok = same && finite; + std::printf("%-22s %s caches: bits %016llx picked %d, sizes%s%s %s\n", cs.name, q8 ? "q8_0" : "fp8 ", + (unsigned long long) fnv(got), picked, sizes.c_str(), finite ? "" : " NON-FINITE", + ok ? "ok" : "FAIL"); + fails += !ok; + } + for (void* p : {(void*) out, (void*) dk8, (void*) dv8}) cudaFree(p); + } blas_destroy(blas); cudaStreamDestroy(st); + std::printf("block sizes: %d / %d calls give the picked size's bits\n", size_same, size_calls); std::printf("%d failed\n", fails); if (!ran_flash) { std::printf("ink_attend_batch() takes no call on this card\n"); @@ -363,14 +443,13 @@ int run_tests() { } // ms of one call of `rows` rows whose last position is keys - 1, Inkling 2 Mini's 24 heads on 8, window layers' 1,024 -int run_bench(std::vector all_keys) { +int run_bench(std::vector all_keys, int rows) { cudaStream_t st; ck(cudaStreamCreate(&st), "stream"); void* blas = blas_create(st); cudaEvent_t e0, e1; cudaEventCreate(&e0); cudaEventCreate(&e1); - const int rows = 1024; if (all_keys.empty()) all_keys = {2048, 8192, 32768}; for (const long long keys : all_keys) for (const bool swa : {false, true}) { @@ -401,8 +480,13 @@ int run_bench(std::vector all_keys) { bool flash = true; const float tf = time([&] { flash = ink_attend_batch(a, st); }); const float tb = time([&] { attend_batch_path(blas, x, out, score, work, zero, st); }); - std::printf("%-6s keys %6lld rows %d: flash %s %8.3f ms | attend_batch %8.3f ms\n", cs.name, keys, rows, - flash ? " " : "off", flash ? tf : 0.0f, tb); + std::printf("%-6s keys %6lld rows %d: flash %s %8.3f ms (picked %d) | attend_batch %8.3f ms\n", cs.name, + keys, rows, flash ? " " : "off", flash ? tf : 0.0f, ink_flash_tokens(a), tb); + if (flash && ink_flash_tokens(a) > 0) + for (const int t : {64, 48, 32}) { + const float ms = time([&] { ink_attend_batch_rows(a, st, t); }); + std::printf("%-6s keys %6lld rows %d: flash tokens %d %8.3f ms\n", cs.name, keys, rows, t, ms); + } for (void* p : {(void*) out, (void*) score, (void*) work, (void*) zero}) cudaFree(p); } blas_destroy(blas); @@ -422,8 +506,11 @@ int main(int argc, char** argv) { std::printf("%s (sm_%d%d)\n", p.name, p.major, p.minor); if (argc > 1 && std::string(argv[1]) == "--bench") { std::vector keys; - for (int i = 2; i < argc; ++i) keys.push_back(std::atoll(argv[i])); - return run_bench(keys); + int rows = 1024; + for (int i = 2; i < argc; ++i) + if (std::string(argv[i]) == "--rows" && i + 1 < argc) rows = std::atoi(argv[++i]); + else keys.push_back(std::atoll(argv[i])); + return run_bench(keys, rows); } return run_tests(); } diff --git a/engine/src/poly/inkling2.cu b/engine/src/poly/inkling2.cu index cfe6c3d5f..c5d726639 100644 --- a/engine/src/poly/inkling2.cu +++ b/engine/src/poly/inkling2.cu @@ -1528,7 +1528,7 @@ template __device__ __forceinline__ void ink_cp_wait() { #endif // ink_attend() of a prompt batch on the tensor cores (ink_attend_batch()), FlashAttention-2's way, as kernels.cu's -// attn_flash128_kernel goes: a launch block takes 64 tokens of a key head (32 when LEAN), a warp 16 tokens of one of the +// attn_flash128_kernel goes: a launch block takes TOKENS tokens of a key head (32 when LEAN), a warp 16 tokens of one of the // head's G query heads, and the keys go kFlashKeys at a time, two buffers of keys and values in turn, each row's // softmax kept as it goes, so no score leaves the registers. A score is what attend_batch() with an AttnBias makes // of it: the query rounded to halves, its product with the key summed in floats and times the scale, plus the bias @@ -1545,21 +1545,25 @@ template __device__ __forceinline__ void ink_cp_wait() { // would add weights of 0 and scale by 1, every tile still starts at a multiple of kFlashKeys, and a row's tiles and // the order of its sums are the same. On the other cards (sm_86, sm_89, sm_120) it was slower per layer, so they // keep the kernel without LEAN. +// TOKENS without LEAN (64, 48 or 32; ink_flash_tokens() picks it by how the blocks fill the card's last wave): a +// block's rows. A row's scores, weights and sums are the same whatever block it is in: its tiles of keys still start +// at multiples of kFlashKeys and go in the same order, and a tile none of whose keys the row sees (the block's wider +// span) adds weights of 0 and scales by 1, or keeps the zeros before the row's first key, so TOKENS changes no bit. constexpr int kFlashKeys = 64, kFlashRow = 128 + 8, kFlashRel = 16; -constexpr int flash_tokens(bool lean) { return lean ? 32 : 64; } // the kernel's TOKENS +constexpr int flash_tokens(bool lean) { return lean ? 32 : 64; } // the kernel's default TOKENS constexpr size_t kFlashKv = size_t(4) * kFlashKeys * kFlashRow * sizeof(__half); -// the shared memory of ink_flash_kernel: keys and values, the table's columns (LEAN, G < 4), r -constexpr size_t flash_smem(int g, bool lean) { - return kFlashKv + - (lean && g < 4 ? size_t(2) * (flash_tokens(lean) + kFlashKeys) * kFlashRel * sizeof(float) : 0) + - size_t(flash_tokens(lean)) * g * kFlashRel * sizeof(float); +// the shared memory of ink_flash_kernel: keys and values, the table's columns (LEAN, G < 4), r +constexpr size_t flash_smem(int g, bool lean, int tokens) { + return kFlashKv + (lean && g < 4 ? size_t(2) * (tokens + kFlashKeys) * kFlashRel * sizeof(float) : 0) + + size_t(tokens) * g * kFlashRel * sizeof(float); } -template -__global__ void __launch_bounds__((LEAN ? 32 : 64) / 16 * G * 32, 1) ink_flash_kernel(InkAttn a) { +template +__global__ void __launch_bounds__(TOKENS / 16 * G * 32, 1) ink_flash_kernel(InkAttn a) { + static_assert(!LEAN || TOKENS == 32, "LEAN takes 32 tokens a block"); + static_assert(TOKENS % 16 == 0, "a warp takes 16 tokens"); #if INK_FLASH - constexpr int TOKENS = LEAN ? 32 : 64, DIM = 128, KEYS = kFlashKeys, ROW = kFlashRow, NR = kFlashRel, - THREADS = TOKENS / 16 * G * 32; + constexpr int DIM = 128, KEYS = kFlashKeys, ROW = kFlashRow, NR = kFlashRel, THREADS = TOKENS / 16 * G * 32; constexpr int SPAN = TOKENS + KEYS; // a tile's distances (one more than it spans, for 16 bytes) extern __shared__ __align__(16) __half ink_flash_sm[]; __half(*ks)[KEYS][ROW] = reinterpret_cast<__half(*)[KEYS][ROW]>(ink_flash_sm); @@ -3327,41 +3331,161 @@ bool ink_attend_batch(const InkAttn& a, void* stream) { launched("ink_wmma"); return true; #else - if (a.rows <= 0) return true; - static const bool off = [] { // STRATA_INK_FLASH=0: attend_batch()'s path - const char* v = std::getenv("STRATA_INK_FLASH"); - return v && v[0] == '0'; - }(); - const int per = a.n_kv > 0 && a.n_head % a.n_kv == 0 ? a.n_head / a.n_kv : 0; - if (off) return false; - if (a.dim != 128 || a.n_rel != kFlashRel || per < 1 || per > kMaxPer || a.at || a.q8) return false; + return ink_attend_batch_rows(a, stream, 0); +#endif +} + +#if !defined(__HIPCC__) +namespace { + +int flash_ptx() { static const int ptx = [] { cudaFuncAttributes f{}; return cudaFuncGetAttributes(&f, ink_flash_kernel<1, false>) == cudaSuccess ? f.ptxVersion : 0; }(); - // Turing: ink_flash75_kernel reads fp16 caches only (FP8 and Q8_0 rows go to attend_batch()) - if (ptx < 80) return ptx >= 75 && !a.kv8 && !a.kvq8 && flash75(a, per, stream); - static const bool lean = [] { // ink_flash_kernel's LEAN on sm_80 (A100 class) only: the card it was faster on + return ptx; +} + +bool flash_lean() { // ink_flash_kernel's LEAN on sm_80 (A100 class) only: the card it was faster on + static const bool lean = [] { int dev = 0, major = 0, minor = 0; return cudaGetDevice(&dev) == cudaSuccess && cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev) == cudaSuccess && cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, dev) == cudaSuccess && major == 8 && minor == 0; }(); - const auto launch = [&](auto kernel, int g, bool ln) { - const size_t smem = flash_smem(g, ln); - const int tokens = flash_tokens(ln); - 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)); - kernel<<>>(a); - }; - static const bool fast = [] { // STRATA_FAST_ATTN=1: ink_fast_kernel for full layers (opt-in, other bits) + return lean; +} + +bool flash_fast() { // STRATA_FAST_ATTN=1: ink_fast_kernel for full layers (opt-in, other bits) + static const bool fast = [] { const char* v = std::getenv("STRATA_FAST_ATTN"); const bool on = v && v[0] == '1'; if (on) std::fprintf(stderr, "strata: STRATA_FAST_ATTN=1: Inkling 2's full layers read prompts on ink_fast_kernel (other bits)\n"); return on; }(); - if (fast && a.window <= 0 && !a.kvq8) { // ink_fast_kernel reads fp16 and FP8 caches + return fast; +} + +// the query heads on a key head when ink_attend_batch() takes the call at all, else 0 +int flash_per(const InkAttn& a) { + static const bool off = [] { // STRATA_INK_FLASH=0: attend_batch()'s path + const char* v = std::getenv("STRATA_INK_FLASH"); + return v && v[0] == '0'; + }(); + const int per = a.n_kv > 0 && a.n_head % a.n_kv == 0 ? a.n_head / a.n_kv : 0; + if (off || a.dim != 128 || a.n_rel != kFlashRel || per < 1 || per > kMaxPer || a.at || a.q8) return 0; + return per; +} + +constexpr int kFlashSizes[] = {64, 48, 32}; // ink_flash_kernel's TOKENS without LEAN, main's first + +int flash_size_index(int tokens) { return tokens == 64 ? 0 : tokens == 48 ? 1 : 2; } + +template void* flash_fn() { return reinterpret_cast(ink_flash_kernel); } + +void* flash_kernel(int g, int tokens) { // ink_flash_kernel + static void* const fns[4][3] = {{flash_fn<1, 64>(), flash_fn<1, 48>(), flash_fn<1, 32>()}, + {flash_fn<2, 64>(), flash_fn<2, 48>(), flash_fn<2, 32>()}, + {flash_fn<3, 64>(), flash_fn<3, 48>(), flash_fn<3, 32>()}, + {flash_fn<4, 64>(), flash_fn<4, 48>(), flash_fn<4, 32>()}}; + return fns[g - 1][flash_size_index(tokens)]; +} + +// The kernel's shared memory set (once a kernel and device), and its blocks a multiprocessor +int flash_resident(int g, int tokens) { + constexpr int kDevs = 16; + static std::atomic cache[kDevs][4][3] = {}; // 0: not asked yet; else 1 + blocks + int dev = 0; + if (cudaGetDevice(&dev) != cudaSuccess) dev = 0; + std::atomic* c = dev >= 0 && dev < kDevs ? &cache[dev][g - 1][flash_size_index(tokens)] : nullptr; + if (c && c->load(std::memory_order_relaxed) > 0) return c->load(std::memory_order_relaxed) - 1; + void* fn = flash_kernel(g, tokens); + const size_t smem = flash_smem(g, false, tokens); + const cudaError_t e = cudaFuncSetAttribute(fn, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) smem); + if (e != cudaSuccess) throw std::runtime_error(std::string("ink_attend_batch: shared memory: ") + cudaGetErrorString(e)); + int n = 0; + if (cudaOccupancyMaxActiveBlocksPerMultiprocessor(&n, fn, tokens / 16 * g * 32, smem) != cudaSuccess) n = 0; + if (c) c->store(1 + n, std::memory_order_relaxed); + return n; +} + +int flash_sms() { + int dev = 0, n = 0; + if (cudaGetDevice(&dev) != cudaSuccess) return 0; + return cudaDeviceGetAttribute(&n, cudaDevAttrMultiProcessorCount, dev) == cudaSuccess ? n : 0; +} + +// The rows a block for a call ink_flash_kernel without LEAN takes: STRATA_INK_FLASH_TOKENS=64/48/32 fixes them (64: +// main's shape); else the size whose launch ends soonest by its waves: blocks over the multiprocessors' resident +// slots, rounded up, times a block's time against one of 64 rows (kFlashCost, measured on an RTX 5080 Laptop at 2K to +// 32K keys: a block of 48 takes 0.9 of one of 64, as nvcc gives both 168 registers, and one of 32 0.65). Another size +// than 64 is taken only when that is at least kFlashGain less, so a card whose last wave is full or nearly so keeps +// 64. Inkling 2 Mini's 1,024-row calls (128 blocks of 64, one a multiprocessor) move to 48 on 59 to 63 +// multiprocessors (a third wave of 64 nearly empty), 88 to 127 and 176 to 199, and keep 64 elsewhere. +int flash_pick(const InkAttn& a, int per) { + static const int fixed = [] { + const char* v = std::getenv("STRATA_INK_FLASH_TOKENS"); + const int t = v ? std::atoi(v) : 0; + return t == 64 || t == 48 || t == 32 ? t : 0; + }(); + if (fixed) return fixed; + constexpr double kFlashCost[] = {1.0, 0.9, 0.65}, kFlashGain = 0.05; // kFlashSizes' blocks + const int sms = flash_sms(); + if (sms < 1 || a.rows <= 0) return 64; + double best = 0.0, main_cost = 0.0; + int pick = 64; + for (int i = 0; i < 3; ++i) { + const int t = kFlashSizes[i], res = flash_resident(per, t); + if (res < 1) continue; + const long long blocks = (long long) grid((size_t) a.rows, t) * a.n_kv, slots = (long long) sms * res; + const double cost = (double) ((blocks + slots - 1) / slots) * res * kFlashCost[i]; + if (t == 64) { + main_cost = best = cost; + } else if (main_cost > 0.0 && cost < best && cost <= main_cost * (1.0 - kFlashGain)) { + best = cost; + pick = t; + } + } + return pick; +} + +// whether the call goes to ink_flash_kernel without LEAN (not Turing's kernel, LEAN's or ink_fast_kernel) +bool flash_sized(const InkAttn& a, int per) { + return per > 0 && flash_ptx() >= 80 && !flash_lean() && !(flash_fast() && a.window <= 0 && !a.kvq8); +} + +} // namespace + +int ink_flash_tokens(const InkAttn& a) { + if (a.rows <= 0) return 0; + const int per = flash_per(a); + return flash_sized(a, per) ? flash_pick(a, per) : 0; +} +#else +int ink_flash_tokens(const InkAttn&) { return 0; } +#endif + +bool ink_attend_batch_rows(const InkAttn& a, void* stream, int tokens) { +#if defined(__HIPCC__) + if (tokens == 64 || tokens == 48 || tokens == 32) return false; // no ink_flash_kernel on HIP + return ink_attend_batch(a, stream); +#else + if (a.rows <= 0) return true; + const int per = flash_per(a); + if (per == 0) return false; + const int ptx = flash_ptx(); + // Turing: ink_flash75_kernel reads fp16 caches only (FP8 and Q8_0 rows go to attend_batch()) + if (ptx < 80) return ptx >= 75 && !a.kv8 && !a.kvq8 && flash75(a, per, stream); + const bool lean = flash_lean(); + const auto launch = [&](auto kernel, int g, bool ln) { + const int t = flash_tokens(ln); + const size_t smem = flash_smem(g, ln, t); + 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)); + kernel<<>>(a); + }; + if (flash_fast() && a.window <= 0 && !a.kvq8) { // ink_fast_kernel reads fp16 and FP8 caches // HACC where the tensor cores sum fp16 products into floats at half rate: all but sm_80, sm_90 and sm_100, // whose whitepapers give the same dense FP16 TFLOPS with FP32 accumulation as with FP16 static const bool hacc = [] { @@ -3388,20 +3512,24 @@ bool ink_attend_batch(const InkAttn& a, void* stream) { launched("ink_fast"); return true; } - if (lean) + if (lean) { switch (per) { case 1: launch(ink_flash_kernel<1, true>, 1, true); break; case 2: launch(ink_flash_kernel<2, true>, 2, true); break; case 3: launch(ink_flash_kernel<3, true>, 3, true); break; default: launch(ink_flash_kernel<4, true>, 4, true); break; } - else - switch (per) { - case 1: launch(ink_flash_kernel<1, false>, 1, false); break; - case 2: launch(ink_flash_kernel<2, false>, 2, false); break; - case 3: launch(ink_flash_kernel<3, false>, 3, false); break; - default: launch(ink_flash_kernel<4, false>, 4, false); break; - } + launched("ink_flash"); + return true; + } + // without LEAN: the rows a block by the card's last wave (flash_pick()), or `tokens`; the same bits each way + if (tokens != 64 && tokens != 48 && tokens != 32) tokens = flash_pick(a, per); + flash_resident(per, tokens); // sets the kernel's shared memory + InkAttn arg = a; + void* args[] = {&arg}; + (void) cudaLaunchKernel(flash_kernel(per, tokens), dim3(grid((size_t) a.rows, tokens), a.n_kv), + dim3(tokens / 16 * per * 32), args, flash_smem(per, false, tokens), + cs(stream)); // as a <<<>>> launch: an error shows at the next check launched("ink_flash"); return true; #endif diff --git a/engine/src/poly/inkling2_kernels.hpp b/engine/src/poly/inkling2_kernels.hpp index cccf9ca71..0026015ba 100644 --- a/engine/src/poly/inkling2_kernels.hpp +++ b/engine/src/poly/inkling2_kernels.hpp @@ -257,6 +257,14 @@ int ink_attend_parts(int n_head, int n_kv, int dim, int window, int64_t last); /// and fp16 caches, their products split in halves (hi + lo, about float-exact), the weights too. False, with nothing /// launched, where it does not take them. bool ink_attend_batch(const InkAttn& a, void* stream); +/// ink_attend_batch() with the rows a block of ink_flash_kernel (without LEAN) fixed: `tokens` 64, 48 or 32, which +/// change no bit (poly_ink_flash_test compares them); anything else, the size ink_flash_tokens() picks. The other +/// kernels ink_attend_batch() may take ignore it. CUDA and HIP only (Metal has its own ink_attend_batch()). +bool ink_attend_batch_rows(const InkAttn& a, void* stream, int tokens); +/// The rows a block ink_flash_kernel takes for this call on the current card, by how its blocks fill the +/// multiprocessors' last wave: 64, 48 or 32 (STRATA_INK_FLASH_TOKENS=64/48/32 fixes it). 0 where the call goes +/// elsewhere: attend_batch(), Turing's kernel, LEAN (sm_80), STRATA_FAST_ATTN's kernel, HIP. +int ink_flash_tokens(const InkAttn& a); /// An FP8 cache row's bytes. inline size_t ink_kv8_row(int n_kv, int dim) { return (size_t) n_kv * dim + (size_t) n_kv * sizeof(float); } diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 632476c14..1b115a170 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -16613,6 +16613,39 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_INK_FLASH_TOKENS", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/poly/inkling2.cu", + "line": 3428, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "int", + "default_state": null, + "code_default": "0", + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": "37670fc1b9", + "first_date": "2026-11-23", + "since": null, + "last_commit": "37670fc1b9", + "last_date": "2026-11-23", + "status": "rollback", + "owner": "poly", + "note": "64, 48 or 32: the query rows a block of Inkling 2's prompt flash attention (ink_flash_kernel without LEAN) takes, instead of the choice by the card's last-wave fill; 64 is main's shape (the same bits either way)", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_INK_KERNEL_TABLES", "backends": [