Files
leleka/tests/kar-prefill-contract.cpp
T
Pavlo Ratushnyi 33f075fb7c
KAR CUDA compile (GPU hidden) / kar-cuda-compile (pull_request) Successful in 18m46s
feat(kar): add explicit bounded batched prefill backend
2026-10-07 18:20:58 +02:00

93 lines
4.2 KiB
C++

#include "strata/prefill/experimental_experts.hpp"
#include <cassert>
#include <iostream>
#include <numeric>
using namespace strata::prefill;
struct Backend {
int calls = 0, mode = 0;
std::vector<int64_t> batches;
};
bool backend(void* user, int64_t layer, const float* x, const int32_t* ids,
int64_t tokens, int64_t k, int32_t* kind, float* y, std::string& error) {
auto& b = *static_cast<Backend*>(user);
++b.calls;
b.batches.push_back(tokens);
assert(layer == 7 && tokens >= 1 && tokens <= 8 && k == 1);
if (b.mode == 1 || (b.mode == 5 && b.calls == 2)) { error = "synthetic backend fault"; return false; }
if (b.mode == 2) return true; // unhandled rows may not fall back
for (int64_t t = 0; t < tokens; ++t) {
assert(kind[t] == -1 && ids[t] == ids[0]);
kind[t] = 3;
for (int h = 0; h < 3; ++h) y[t * 3 + h] = x[t * 3 + h] + (float) ids[t] * 100;
}
if (b.mode == 3) y[0] = std::numeric_limits<float>::infinity();
if (b.mode == 4) y[1] = std::numeric_limits<float>::quiet_NaN();
return true;
}
int main() {
std::string error;
bool enabled = true;
assert(parse_experimental_prefill_flag(nullptr, enabled, error) && !enabled);
assert(parse_experimental_prefill_flag("0", enabled, error) && !enabled);
assert(parse_experimental_prefill_flag("1", enabled, error) && enabled);
for (auto bad : {"", "true", "01", "2", " 1", "1 "})
assert(!parse_experimental_prefill_flag(bad, enabled, error));
// 19 tokens, 2 routes: expert 0 is split 8/8/3, expert 1 empty,
// expert 2 split 8/8/3; reversed second group verifies grouped scatter.
std::array<int32_t, 4> offsets{0, 19, 19, 38};
std::array<int32_t, 38> source{};
for (int t = 0; t < 19; ++t) { source[t] = t; source[19 + t] = 18 - t; }
std::array<float, 38 * 3> output{};
int reads = 0, writes = 0;
bool read_fail = false, write_fail = false;
auto read = [&](const int32_t* rows, int64_t n, float* x, std::string& e) {
++reads;
if (read_fail) { e = "synthetic transfer fault"; return false; }
for (int64_t t = 0; t < n; ++t)
for (int h = 0; h < 3; ++h) x[t * 3 + h] = (float) (rows[t] * 10 + h);
return true;
};
auto write = [&](int64_t begin, int64_t n, const float* y, std::string& e) {
++writes;
if (write_fail) { e = "synthetic output fault"; return false; }
std::copy(y, y + n * 3, output.begin() + begin * 3);
return true;
};
Backend b;
uint64_t completed = 0;
auto run = [&](int64_t hidden = 3, void* owner = nullptr) {
return dispatch_grouped_experts(7, hidden, 3, 19, 2, offsets.data(), source.data(),
backend, owner ? owner : &b, read, write, completed, error);
};
assert(run() && completed == 38 && reads == 6 && writes == 6);
assert((b.batches == std::vector<int64_t>{8, 8, 3, 8, 8, 3}));
for (int row = 0; row < 38; ++row)
for (int h = 0; h < 3; ++h)
assert(output[row * 3 + h] == source[row] * 10 + h + (row < 19 ? 0 : 200));
for (int mode = 1; mode <= 4; ++mode) {
b = Backend{}; b.mode = mode; completed = 0; reads = writes = 0;
assert(!run() && completed == 0 && b.calls == 1 && writes == 0 && !error.empty());
}
b = Backend{}; b.mode = 5; completed = 0; reads = writes = 0;
assert(!run() && completed == 8 && b.calls == 2 && writes == 1);
b = Backend{}; completed = 0; reads = writes = 0;
assert(!dispatch_grouped_experts(7, 3, 3, 19, 2, offsets.data(), source.data(),
backend, nullptr, read, write, completed, error) && b.calls == 0 && reads == 0);
source.back() = 19;
assert(!run() && reads == 0 && b.calls == 0 && writes == 0);
source.back() = 0;
offsets[1] = 39;
assert(!run() && reads == 0 && b.calls == 0);
offsets[1] = 19;
assert(!run(0) && reads == 0 && b.calls == 0);
assert(!run(16385) && reads == 0 && b.calls == 0);
read_fail = true;
assert(!run() && b.calls == 0 && writes == 0);
read_fail = false; write_fail = true;
assert(!run() && b.calls == 1 && completed == 0);
std::cout << "PASS: bounded KAR prefill grouping, scatter, strict opt-in and fail-closed ownership/transfer contract\n";
}