Files
leleka/crates/cuda-driver/oracle/drivers/flash_attn.cpp
T

75 lines
4.0 KiB
C++

// Executes native_flash_attn.cu's kernel (one query, Q24x256 / KV2x256, F16 K/V) on the host shim and compares it with an
// FP64 softmax attention over the same F16 K/V values, for several widths, with and without the additive mask, plus the
// invalid-step refusal (NaN output, status 1). Tolerance: relative to the largest output magnitude of the head.
#include SHIM_KERNEL_0
#include <random>
static uint16_t to_h(float f) { return __float2half_rn(f).bits; }
static float from_h(uint16_t b) { return __half2float(__ushort_as_half(b)); }
static int run(int width, bool with_mask, unsigned seed, double& worst) {
const int capacity = 256, max_context = 256;
std::mt19937 rng(seed);
std::normal_distribution<float> nd(0.0f, 1.0f);
std::vector<float> q(24 * 256), out(24 * 256, -1.0f);
for (auto& x : q) x = 0.8f * nd(rng);
std::vector<uint16_t> k((size_t) capacity * 2 * 256), v((size_t) capacity * 2 * 256), mask(256);
for (auto& x : k) x = to_h(nd(rng));
for (auto& x : v) x = to_h(nd(rng));
// padding the kernel must never read: arbitrary bytes (NaN patterns)
for (int c = width; c < capacity; ++c) for (int i = 0; i < 2 * 256; ++i) { k[(size_t) c * 512 + i] = 0x7e00; v[(size_t) c * 512 + i] = 0x7e00; }
for (int c = 0; c < 256; ++c) mask[c] = c < width ? to_h(with_mask ? (float) ((c * 7) % 5) * -0.5f : 0.0f) : 0x7e00;
int32_t step[4] = {width - 1, width, width / 4, width};
int32_t status = -1;
shim_launch(dim3(24), dim3(32, 4), 0, [&] {
leleka_native_flash_attn_short(q.data(), (const half*) k.data(), (const half*) v.data(), step, max_context, 256, 0.0625f, out.data(), &status,
with_mask ? (const half*) mask.data() : nullptr); });
if (status != 0) { std::fprintf(stderr, "width %d: status %d\n", width, status); return 1; }
for (int h = 0; h < 24; ++h) {
const int kv = h / 12;
std::vector<double> lg(width);
double m = -1e300;
for (int c = 0; c < width; ++c) {
double d = 0;
for (int j = 0; j < 256; ++j) d += (double) q[h * 256 + j] * from_h(k[((size_t) c * 2 + kv) * 256 + j]);
lg[c] = d * 0.0625 + (with_mask ? from_h(mask[c]) : 0.0);
m = std::max(m, lg[c]);
}
double l = 0;
for (auto& x : lg) { x = std::exp(x - m); l += x; }
double mag = 0;
std::vector<double> ref(256, 0.0);
for (int c = 0; c < width; ++c) for (int j = 0; j < 256; ++j) ref[j] += lg[c] / l * from_h(v[((size_t) c * 2 + kv) * 256 + j]);
for (double r : ref) mag = std::max(mag, std::fabs(r));
for (int j = 0; j < 256; ++j) worst = std::max(worst, std::fabs(out[h * 256 + j] - ref[j]) / std::max(mag, 1e-6));
}
return 0;
}
int main() {
int bad = 0;
double worst = 0;
for (int width : {1, 2, 5, 31, 32, 33, 100, 128, 129, 255, 256}) {
bad += run(width, false, 100 + width, worst);
bad += run(width, true, 200 + width, worst);
}
std::printf("native flash attention: worst relative error %.3e\n", worst);
// invalid steps: nothing is read, the output is NaN and the status says why
{
std::vector<float> q(24 * 256, 0.0f), out(24 * 256, 0.0f);
std::vector<uint16_t> kv(256 * 512, 0);
int32_t status = 0;
for (auto bads : std::vector<std::vector<int32_t>>{{0, 0, 0, 0}, {5, 7, 1, 7}, {6, 7, 2, 7}, {6, 7, 1, 300}, {6, 6, 1, 7}}) {
int32_t step[4] = {bads[0], bads[1], bads[2], bads[3]};
shim_launch(dim3(24), dim3(32, 4), 0, [&] {
leleka_native_flash_attn_short(q.data(), (const half*) kv.data(), (const half*) kv.data(), step, 256, 256, 0.0625f, out.data(), &status, nullptr); });
bool nan = true;
for (float x : out) nan &= std::isnan(x);
if (status != 1 || !nan) { std::fprintf(stderr, "invalid step not refused\n"); bad++; }
}
}
const bool pass = bad == 0 && worst < 5e-5;
std::puts(pass ? "PASS" : "FAIL");
return pass ? 0 : 1;
}