mmq_original_prefill_misc.cu and device_poison.cu are verbatim kernel bodies (UNSEALED PTX stubs); the pure Rust halves carry launch geometry, the gather_native copy-width decision, a literal strata_q2 blob conversion model, the DeviceArena bump allocator and the compute-capability policy. oracle/drivers/misc_kernels.cpp executes the kernels through the host shim and writes the golden hashes the Rust models are tested against. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
107 lines
6.5 KiB
C++
107 lines
6.5 KiB
C++
// Host-shim execution of the small Original kernels: moe_mmq.cu copy1 / strata_q2 / iota, the already ported copy16 and
|
|
// swiglu (mmq_original_group_swiglu.cu) and device.cu's poison_kernel. Prints the golden fixture JSON consumed by
|
|
// mmq_original_prefill_misc.rs and device_arena.rs tests, and fails (non-zero exit) when a kernel disagrees with its
|
|
// own plain-C++ expectation.
|
|
#include SHIM_KERNEL_0
|
|
#include SHIM_KERNEL_1
|
|
#include SHIM_KERNEL_2
|
|
|
|
static uint64_t fnv(const void* p, size_t n, uint64_t h = 1469598103934665603ull) {
|
|
const uint8_t* b = (const uint8_t*) p;
|
|
for (size_t i = 0; i < n; ++i) { h ^= b[i]; h *= 1099511628211ull; }
|
|
return h;
|
|
}
|
|
static std::vector<uint8_t> lcg(uint64_t seed, size_t n) {
|
|
std::vector<uint8_t> v(n);
|
|
uint64_t x = seed;
|
|
for (auto& b : v) { x = x * 6364136223846793005ull + 1442695040888963407ull; b = (uint8_t) (x >> 56); }
|
|
return v;
|
|
}
|
|
static unsigned blocks(int64_t n) { return (unsigned) ((n + 255) / 256); }
|
|
static int bad = 0;
|
|
static void expect(bool ok, const char* what) { if (!ok) { std::fprintf(stderr, "FAILED: %s\n", what); bad = 1; } }
|
|
|
|
int main() {
|
|
std::printf("{\n");
|
|
// ---- strata_q2
|
|
{
|
|
const uint64_t seed = 0x5eed01;
|
|
auto blob = lcg(seed, 1382400);
|
|
std::vector<uint16_t> gu(51200 * 9, 0xffff), dn(25600 * 9, 0xffff);
|
|
shim_launch(dim3(blocks(1280LL * 40 + 2560LL * 10)), dim3(256), 0, [&] { leleka_original_mmq_strata_q2(blob.data(), gu.data(), dn.data()); });
|
|
bool all = true; // independent expectation: every output word was written, scale first
|
|
for (size_t b = 0; b < 51200; ++b) { size_t row = b / 40, k = b % 40; uint16_t sc; std::memcpy(&sc, &blob[1228800 + (row * 40 + k) * 2], 2); all &= gu[b * 9] == sc; }
|
|
for (size_t b = 0; b < 25600; ++b) { size_t row = b / 10, k = b % 10; uint16_t sc; std::memcpy(&sc, &blob[1331200 + (row * 10 + k) * 2], 2); all &= dn[b * 9] == sc; }
|
|
expect(all, "strata_q2 scales");
|
|
std::printf("\"strata_q2\":{\"seed\":\"%llx\",\"blob_hash\":\"%llx\",\"gu_hash\":\"%llx\",\"dn_hash\":\"%llx\"},\n", (unsigned long long) seed,
|
|
(unsigned long long) fnv(blob.data(), blob.size()), (unsigned long long) fnv(gu.data(), gu.size() * 2), (unsigned long long) fnv(dn.data(), dn.size() * 2));
|
|
}
|
|
// ---- copy16 (uint4) and copy1 (bytes)
|
|
for (int wide = 1; wide >= 0; --wide) {
|
|
const uint64_t seed = wide ? 0xc0b16 : 0xc0b01;
|
|
const size_t half = wide ? 4096 : 1001, down = wide ? 2048 : 77;
|
|
auto gate = lcg(seed, half), up = lcg(seed ^ 1, half), dw = lcg(seed ^ 2, down);
|
|
std::vector<uint8_t> ab(2 * half + 16, 0xcd), c(down + 16, 0xcd);
|
|
if (wide) {
|
|
const int64_t na = half / 16, nc = down / 16;
|
|
shim_launch(dim3(blocks(2 * na + nc)), dim3(256), 0, [&] {
|
|
leleka_original_mmq_gather((const uint4*) gate.data(), na, (const uint4*) up.data(), na, (uint4*) ab.data(), (const uint4*) dw.data(), nc, (uint4*) c.data()); });
|
|
} else {
|
|
const int64_t na = half, nc = down;
|
|
shim_launch(dim3(blocks(2 * na + nc)), dim3(256), 0, [&] {
|
|
leleka_original_mmq_copy1(gate.data(), na, up.data(), na, ab.data(), dw.data(), nc, c.data()); });
|
|
}
|
|
expect(std::memcmp(ab.data(), gate.data(), half) == 0 && std::memcmp(ab.data() + half, up.data(), half) == 0 && std::memcmp(c.data(), dw.data(), down) == 0, "copy content");
|
|
expect(ab[2 * half] == 0xcd && c[down] == 0xcd, "copy overrun");
|
|
std::printf("\"%s\":{\"seed\":\"%llx\",\"half_bytes\":%zu,\"down_bytes\":%zu,\"ab_hash\":\"%llx\",\"c_hash\":\"%llx\"},\n", wide ? "copy16" : "copy1", (unsigned long long) seed, half, down,
|
|
(unsigned long long) fnv(ab.data(), 2 * half), (unsigned long long) fnv(c.data(), down));
|
|
}
|
|
// ---- iota (grid larger than n: the tail must stay untouched)
|
|
{
|
|
const int64_t n = 1000;
|
|
std::vector<int32_t> v(1100, -7);
|
|
shim_launch(dim3(blocks(n)), dim3(256), 0, [&] { leleka_original_mmq_iota(v.data(), n); });
|
|
bool ok = true;
|
|
for (int i = 0; i < 1100; ++i) ok &= v[i] == (i < n ? i : -7);
|
|
expect(ok, "iota");
|
|
bool tail = true;
|
|
for (int i = n; i < 1100; ++i) tail &= v[i] == -7;
|
|
std::printf("\"iota\":{\"n\":%lld,\"hash\":\"%llx\",\"tail_untouched\":%s},\n", (long long) n, (unsigned long long) fnv(v.data(), n * 4), tail ? "true" : "false");
|
|
}
|
|
// ---- swiglu (interleaved and split): checked against a double reference
|
|
{
|
|
const int64_t rows = 5, n_ff = 640;
|
|
std::vector<float> gu((size_t) rows * 2 * n_ff), h((size_t) rows * n_ff);
|
|
auto raw = lcg(0x5179, gu.size());
|
|
for (size_t i = 0; i < gu.size(); ++i) gu[i] = ((int) raw[i] - 128) / 16.0f;
|
|
double worst = 0;
|
|
for (int inter = 0; inter < 2; ++inter) {
|
|
shim_launch(dim3(blocks(rows * n_ff)), dim3(256), 0, [&] { leleka_original_mmq_swiglu(gu.data(), h.data(), rows, n_ff, inter != 0); });
|
|
for (int64_t r = 0; r < rows; ++r) for (int64_t k = 0; k < n_ff; ++k) {
|
|
const float* row = &gu[(size_t) r * 2 * n_ff];
|
|
double g = inter ? row[2 * k] : row[k], u = inter ? row[2 * k + 1] : row[n_ff + k];
|
|
double ref = g / (1.0 + std::exp(-g)) * u;
|
|
worst = std::max(worst, std::fabs(ref - h[(size_t) r * n_ff + k]) / (std::fabs(ref) + 1e-3));
|
|
}
|
|
}
|
|
expect(worst < 1e-5, "swiglu");
|
|
std::printf("\"swiglu\":{\"worst_relative_error\":%.3e},\n", worst);
|
|
}
|
|
// ---- poison with the Original's slicing loop at a small max_blocks
|
|
{
|
|
const uint64_t bytes = 4000, max_blocks = 3, threads = 256;
|
|
std::vector<uint32_t> mem(1100, 0xdeadbeefu);
|
|
const uint64_t n = bytes / 4, nblocks = (n + threads - 1) / threads;
|
|
for (uint64_t b = 0; b < nblocks; b += max_blocks) {
|
|
const uint64_t chunk = (nblocks - b < max_blocks) ? (nblocks - b) : max_blocks;
|
|
shim_launch(dim3((unsigned) chunk), dim3(256), 0, [&] { leleka_poison_f32((float*) mem.data() + b * threads, n - b * threads); });
|
|
}
|
|
bool ok = true;
|
|
for (int i = 0; i < 1100; ++i) ok &= mem[i] == (i < 1000 ? 0x7fc00000u : 0xdeadbeefu);
|
|
expect(ok, "poison");
|
|
std::printf("\"poison\":{\"bytes\":%llu,\"words\":1100,\"max_blocks\":%llu,\"hash\":\"%llx\"}\n", (unsigned long long) bytes, (unsigned long long) max_blocks, (unsigned long long) fnv(mem.data(), mem.size() * 4));
|
|
}
|
|
std::printf("}\n");
|
|
return bad;
|
|
}
|