Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 100 additions & 13 deletions engine/src/poly/ink_flash_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cuda_fp16.h>
Expand Down Expand Up @@ -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];
Expand All @@ -330,6 +335,21 @@ int run_tests() {
ck(cudaStreamSynchronize(st), "ink_attend_batch");
const std::vector<float> 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<float> got_token = download(out, n_out);
Expand All @@ -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<uint8_t> k8((size_t) x.slots * row), v8(k8.size());
for (std::vector<uint8_t>* 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<const uint16_t*>(dk8), a.vals = reinterpret_cast<const uint16_t*>(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<float> 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");
Expand All @@ -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<long long> all_keys) {
int run_bench(std::vector<long long> 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}) {
Expand Down Expand Up @@ -401,8 +480,13 @@ int run_bench(std::vector<long long> 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);
Expand All @@ -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<long long> 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();
}
Loading
Loading