keyless.rs: SKLROUT1 artifact admission (layout cross-read against prepare-keyless-routing.py), fmaf-order query routing, positional key reconstruction. cvec.rs: GGUF v3 parsing and the load_control_vectors table builder (19 golden cases incl. the Original's error texts, bit-exact, from the Original function extracted verbatim) and a reduction-tree twin of cvec_kernel. attn.rs: scratch arithmetic and an f64 GQA reference. The emulated kernel text reproduces routing and cvec bit for bit and attention modes 2/3/4 (incl. M-RoPE) to 1.6e-7 relative L2. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
494 lines
26 KiB
C++
494 lines
26 KiB
C++
// Executes the kernel TEXT of crates/cuda-driver/src/experimental_*.cu on the CPU (thread-per-CUDA-thread
|
|
// emulation, oracle/emu/cuda_emu.h) over deterministic scenarios and writes the results to
|
|
// tests/fixtures/emulated_kernels.json. tests/emulated_kernels.rs regenerates the same inputs with the Rust
|
|
// references (tests/common/mod.rs mirrors the generators below) and requires the kernel outputs to match.
|
|
// No GPU, no nvcc: this validates control flow, indexing and arithmetic of the literal kernel text; it does not
|
|
// validate PTX code generation, nvcc's FMA contraction, device libm or performance.
|
|
#include "cuda_runtime.h" // the emulation header (oracle/emu), NOT the CUDA toolkit
|
|
#include "emulate_common.h"
|
|
|
|
#include <set>
|
|
|
|
namespace {
|
|
constexpr int HEADS = 2, HD = 256, PAGE = 4, ROW_Q4 = 144;
|
|
|
|
std::vector<float> rotate(const std::vector<float>& x) { // leleka_fwht256_f32 over rows of 256
|
|
std::vector<float> out(x.size());
|
|
const long long rows = (long long) x.size() / 256;
|
|
emu_launch(dim3((unsigned) ((rows + 3) / 4)), dim3(32, 4), [&] { leleka_fwht256_f32(x.data(), out.data(), rows); });
|
|
return out;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------- fwht
|
|
Json scenario_fwht() {
|
|
Lcg g(11);
|
|
const int rows = 9;
|
|
const float scale[3] = {1.0f, 20.0f, 0.02f};
|
|
std::vector<float> in((size_t) rows * 256);
|
|
for (int r = 0; r < rows; ++r)
|
|
for (int i = 0; i < 256; ++i) in[(size_t) r * 256 + i] = g.soft() * scale[r % 3];
|
|
const std::vector<float> out = rotate(in);
|
|
Json j;
|
|
j.num("rows", rows);
|
|
j.num("input_fnv", (long long) fnv_vec(in));
|
|
j.hex("out", out);
|
|
return j;
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------- Q4_0 / Q8
|
|
struct Pools {
|
|
std::vector<uint8_t> k_q4, v_q4, ks, vs, kq, vq; // bytes
|
|
};
|
|
|
|
Json scenario_q4() {
|
|
const int pages = 3, cells = 12;
|
|
const std::vector<int32_t> table = {2, -1, 0};
|
|
const size_t pool_bytes = (size_t) pages * HEADS * PAGE * ROW_Q4;
|
|
std::vector<uint8_t> k(pool_bytes, 0), v(pool_bytes, 0), hk(pool_bytes, 0), hv(pool_bytes, 0);
|
|
Lcg g(21);
|
|
std::vector<float> all_k, all_v;
|
|
std::vector<int32_t> step(4);
|
|
for (int pos = 0; pos < cells; ++pos) {
|
|
std::vector<float> kraw(512), vraw(512);
|
|
for (auto& x : kraw) x = g.soft() * 3.0f;
|
|
for (auto& x : vraw) x = g.soft() * 3.0f;
|
|
all_k.insert(all_k.end(), kraw.begin(), kraw.end());
|
|
all_v.insert(all_v.end(), vraw.begin(), vraw.end());
|
|
const auto kc = rotate(kraw), vc = rotate(vraw);
|
|
step = {pos, pos + 1, (pos + 1) / 4, 0};
|
|
emu_launch(dim3(HEADS, HD / 32, 2), dim3(32), [&] {
|
|
leleka_kv_append_q4_step(k.data(), v.data(), table.data(), step.data(), kc.data(), vc.data(), HEADS, HD, PAGE,
|
|
hk.data(), hv.data());
|
|
});
|
|
}
|
|
Json j;
|
|
j.num("input_fnv", (long long) (fnv_vec(all_k) ^ (fnv_vec(all_v) * 31)));
|
|
j.hex("k", k); j.hex("v", v); j.hex("host_k", hk); j.hex("host_v", hv);
|
|
|
|
// prompt path: T cells from pos0 with a host copy and a staging pool, K then V grids
|
|
{
|
|
const int T = 6;
|
|
const long long pos0 = 3;
|
|
std::vector<uint8_t> bk(pool_bytes, 0), bv(pool_bytes, 0), bhk(pool_bytes, 0), bhv(pool_bytes, 0), bsk(pool_bytes, 0), bsv(pool_bytes, 0);
|
|
Lcg g2(31);
|
|
std::vector<float> K((size_t) T * 512), V((size_t) T * 512);
|
|
for (auto& x : K) x = g2.soft() * 3.0f;
|
|
for (auto& x : V) x = g2.soft() * 3.0f;
|
|
const auto Kr = rotate(K), Vr = rotate(V);
|
|
for (int is_v = 0; is_v < 2; ++is_v)
|
|
emu_launch(dim3(T, HEADS, HD / 32), dim3(32), [&] {
|
|
leleka_kv_append_q4_batch(bk.data(), bv.data(), table.data(), pos0, Kr.data(), Vr.data(), HEADS, HD, PAGE,
|
|
is_v, bhk.data(), bhv.data(), bsk.data(), bsv.data());
|
|
});
|
|
j.num("batch_input_fnv", (long long) (fnv_vec(K) ^ (fnv_vec(V) * 31)));
|
|
j.hex("batch_k", bk); j.hex("batch_v", bv); j.hex("batch_host_k", bhk); j.hex("batch_host_v", bhv);
|
|
j.hex("batch_stage_k", bsk); j.hex("batch_stage_v", bsv);
|
|
}
|
|
|
|
// gather the resident cells 0 1 3 8 10 11 from the sequential pools
|
|
const std::vector<int32_t> ids = {0, 1, 3, 8, 10, 11};
|
|
const std::vector<int32_t> gstep = {0, 0, 0, (int32_t) ids.size()};
|
|
std::vector<uint16_t> ks(ids.size() * HEADS * HD), vs(ids.size() * HEADS * HD);
|
|
const int total_blocks = (int) ids.size() * HEADS * (HD / 32);
|
|
emu_launch(dim3((total_blocks + 3) / 4), dim3(32, 4), [&] {
|
|
leleka_kv_gather_q4_step(k.data(), v.data(), table.data(), ids.data(), gstep.data(), HEADS, HD, PAGE, ks.data(), vs.data());
|
|
});
|
|
j.hex("gather_k", ks); j.hex("gather_v", vs);
|
|
return j;
|
|
}
|
|
|
|
Json scenario_q8() {
|
|
const int pages = 3, cells = 12;
|
|
const size_t rows = (size_t) pages * HEADS * PAGE;
|
|
const std::vector<int32_t> stream_table = {2, -1, 0}, table = {1, 2, 0};
|
|
Json j;
|
|
Lcg g(41);
|
|
std::vector<float> all_k, all_v;
|
|
std::vector<std::vector<float>> kc, vc;
|
|
for (int pos = 0; pos < cells; ++pos) {
|
|
std::vector<float> kraw(512), vraw(512);
|
|
for (auto& x : kraw) x = g.soft() * 2.0f;
|
|
for (auto& x : vraw) x = g.soft() * 2.0f;
|
|
all_k.insert(all_k.end(), kraw.begin(), kraw.end());
|
|
all_v.insert(all_v.end(), vraw.begin(), vraw.end());
|
|
kc.push_back(kraw);
|
|
vc.push_back(vraw);
|
|
}
|
|
j.num("input_fnv", (long long) (fnv_vec(all_k) ^ (fnv_vec(all_v) * 31)));
|
|
auto append = [&](int8_t* kq, int8_t* vq, uint16_t* ks, uint16_t* vs, const std::vector<int32_t>& tab, int pos,
|
|
const std::vector<float>& kk, const std::vector<float>& vv, int8_t* hkq, int8_t* hvq, uint16_t* hks, uint16_t* hvs) {
|
|
const std::vector<int32_t> step = {pos, pos + 1, (pos + 1) / 4, 0};
|
|
emu_launch(dim3(HEADS, HD / 64, kq ? 2 : 1), dim3(64), [&] {
|
|
leleka_kv_append_q8_step(kq, vq, ks, vs, tab.data(), step.data(), kk.data(), vv.data(), HEADS, HD, PAGE, hkq, hvq, hks, hvs);
|
|
});
|
|
};
|
|
// 1. INT8 K+V, resident (permuted table)
|
|
{
|
|
std::vector<int8_t> kq(rows * HD), vq(rows * HD);
|
|
std::vector<uint16_t> ks(rows * 4), vs(rows * 4);
|
|
for (int pos = 0; pos < cells; ++pos) append(kq.data(), vq.data(), ks.data(), vs.data(), table, pos, kc[pos], vc[pos], nullptr, nullptr, nullptr, nullptr);
|
|
j.hex("int8_kq", kq); j.hex("int8_vq", vq); j.hex("int8_ks", ks); j.hex("int8_vs", vs);
|
|
const std::vector<int32_t> ids = {0, 1, 3, 8, 10, 11}, gstep = {0, 0, 0, 6};
|
|
std::vector<uint16_t> ksc(6 * HEADS * HD), vsc(6 * HEADS * HD);
|
|
emu_launch(dim3((unsigned) ((6 * HEADS * (HD / 4) + 255) / 256)), dim3(256), [&] {
|
|
leleka_kv_gather_q8_step(kq.data(), vq.data(), ks.data(), vs.data(), table.data(), ids.data(), gstep.data(), HEADS, HD, PAGE, ksc.data(), vsc.data());
|
|
});
|
|
j.hex("int8_gather_k", ksc); j.hex("int8_gather_v", vsc);
|
|
}
|
|
// 2. INT8 with the streaming host copy (blocks 1 not resident)
|
|
{
|
|
std::vector<int8_t> kq(rows * HD), vq(rows * HD), hkq(rows * HD), hvq(rows * HD);
|
|
std::vector<uint16_t> ks(rows * 4), vs(rows * 4), hks(rows * 4), hvs(rows * 4);
|
|
for (int pos = 0; pos < cells; ++pos) append(kq.data(), vq.data(), ks.data(), vs.data(), stream_table, pos, kc[pos], vc[pos], hkq.data(), hvq.data(), hks.data(), hvs.data());
|
|
j.hex("stream_kq", kq); j.hex("stream_vq", vq); j.hex("stream_ks", ks); j.hex("stream_vs", vs);
|
|
j.hex("stream_host_kq", hkq); j.hex("stream_host_vq", hvq); j.hex("stream_host_ks", hks); j.hex("stream_host_vs", hvs);
|
|
}
|
|
// 3. V-only (keyless): k_q null, z extent 1
|
|
{
|
|
std::vector<int8_t> vq(rows * HD);
|
|
std::vector<uint16_t> vs(rows * 4);
|
|
for (int pos = 0; pos < cells; ++pos) append(nullptr, vq.data(), nullptr, vs.data(), table, pos, kc[pos], vc[pos], nullptr, nullptr, nullptr, nullptr);
|
|
j.hex("vonly_vq", vq); j.hex("vonly_vs", vs);
|
|
}
|
|
// 4. K8V4: rotate V; INT8 K-only with the pointers folded onto the K pool; Q4_0 V-only folded onto the V pool
|
|
{
|
|
std::vector<int8_t> kq(rows * HD);
|
|
std::vector<uint16_t> ks(rows * 4);
|
|
std::vector<uint8_t> v4(rows * ROW_Q4);
|
|
for (int pos = 0; pos < cells; ++pos) {
|
|
const auto vrot = rotate(vc[pos]);
|
|
const std::vector<int32_t> step = {pos, pos + 1, (pos + 1) / 4, 0};
|
|
append(kq.data(), kq.data(), ks.data(), ks.data(), table, pos, kc[pos], kc[pos], nullptr, nullptr, nullptr, nullptr);
|
|
emu_launch(dim3(HEADS, HD / 32, 2), dim3(32), [&] {
|
|
leleka_kv_append_q4_step(v4.data(), v4.data(), table.data(), step.data(), vrot.data(), vrot.data(), HEADS, HD, PAGE, nullptr, nullptr);
|
|
});
|
|
}
|
|
j.hex("k8v4_kq", kq); j.hex("k8v4_ks", ks); j.hex("k8v4_v4", v4);
|
|
const std::vector<int32_t> ids = {0, 1, 3, 8, 10, 11}, gstep = {0, 0, 0, 6};
|
|
std::vector<uint16_t> ksc(6 * HEADS * HD), vsc(6 * HEADS * HD);
|
|
emu_launch(dim3((unsigned) ((6 * HEADS * (HD / 4) + 255) / 256)), dim3(256), [&] {
|
|
leleka_kv_gather_q8_step(kq.data(), kq.data(), ks.data(), ks.data(), table.data(), ids.data(), gstep.data(), HEADS, HD, PAGE, ksc.data(), ksc.data());
|
|
});
|
|
const int total_blocks = 6 * HEADS * (HD / 32);
|
|
emu_launch(dim3((total_blocks + 3) / 4), dim3(32, 4), [&] {
|
|
leleka_kv_gather_q4_step(v4.data(), v4.data(), table.data(), ids.data(), gstep.data(), HEADS, HD, PAGE, vsc.data(), vsc.data());
|
|
});
|
|
j.hex("k8v4_gather_k", ksc); j.hex("k8v4_gather_v", vsc);
|
|
}
|
|
return j;
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------- KV streaming
|
|
struct StreamState {
|
|
std::vector<int32_t> page_table, slot_block, slot_stamp, slot_ref, ctl, miss_block, miss_slot;
|
|
};
|
|
uint64_t state_hash(const StreamState& m, int placed) {
|
|
uint64_t h = 0xcbf29ce484222325ull;
|
|
auto add = [&](const std::vector<int32_t>& v, size_t n) {
|
|
for (size_t i = 0; i < n; ++i)
|
|
for (int b = 0; b < 4; ++b) h = (h ^ (uint8_t) ((uint32_t) v[i] >> (8 * b))) * 0x100000001b3ull;
|
|
};
|
|
add(m.page_table, m.page_table.size());
|
|
add(m.slot_block, m.slot_block.size());
|
|
add(m.slot_stamp, m.slot_stamp.size());
|
|
add(m.slot_ref, m.slot_ref.size());
|
|
add(m.miss_slot, (size_t) placed);
|
|
add(m.ctl, m.ctl.size());
|
|
return h;
|
|
}
|
|
// Host copy bytes: a pure function of (array, index), mirrored in tests/emulated_kernels.rs.
|
|
uint8_t host_byte(int array, size_t i) { return (uint8_t) (((uint32_t) (i + (size_t) array * 7919u) * 2654435761u) >> 24); }
|
|
|
|
Json scenario_stream() {
|
|
const int n_blocks = 1600, n_slots = 1030, cap = 76, n_q = 3, calls = 45;
|
|
const int runs_len[4] = {2048, 2048, 64, 64}; // Int8 runs of one block: k_q, v_q, k_scale, v_scale
|
|
StreamState m;
|
|
m.page_table.assign(n_blocks, 0); m.slot_block.assign(n_slots, 0); m.slot_stamp.assign(n_slots, 0);
|
|
m.slot_ref.assign(n_slots, 7); m.ctl.assign(16, 5); m.miss_block.assign(n_slots, 0); m.miss_slot.assign(n_slots, 0);
|
|
std::vector<std::vector<uint8_t>> host(4), slots(4);
|
|
for (int a = 0; a < 4; ++a) {
|
|
host[a].resize((size_t) n_blocks * runs_len[a]);
|
|
for (size_t i = 0; i < host[a].size(); ++i) host[a][i] = host_byte(a, i);
|
|
slots[a].assign((size_t) n_slots * runs_len[a], 0);
|
|
}
|
|
emu_launch(dim3(128), dim3(256), [&] {
|
|
leleka_kv_stream_reset(m.page_table.data(), m.slot_block.data(), m.slot_stamp.data(), m.slot_ref.data(), m.ctl.data(), n_blocks, n_slots);
|
|
});
|
|
Json j;
|
|
j.num("reset_state_fnv", (long long) state_hash(m, 0));
|
|
Lcg g(51);
|
|
std::string call_list = "[";
|
|
for (int call = 0; call < calls; ++call) {
|
|
std::vector<int32_t> ids((size_t) n_q * cap, 0), steps((size_t) n_q * 4, 0);
|
|
for (int q = 0; q < n_q; ++q) {
|
|
std::set<int> blocks;
|
|
while ((int) blocks.size() < cap / 4)
|
|
blocks.insert(g.below(2) ? (int) g.below(n_blocks) : (int) ((call * 7 + g.below(120)) % n_blocks));
|
|
int w = 0;
|
|
for (int b : blocks) for (int i = 0; i < 4; ++i) ids[(size_t) q * cap + w++] = b * 4 + i;
|
|
steps[q * 4 + 3] = w;
|
|
}
|
|
emu_launch(dim3(1), dim3(1024), [&] {
|
|
leleka_kv_stream_resolve(m.page_table.data(), m.slot_block.data(), m.slot_stamp.data(), m.slot_ref.data(), m.ctl.data(),
|
|
m.miss_block.data(), m.miss_slot.data(), n_slots, ids.data(), steps.data(), n_q, cap, PAGE);
|
|
});
|
|
emu_launch(dim3(96), dim3(128), [&] {
|
|
leleka_kv_stream_copy(m.ctl.data(), m.miss_block.data(), m.miss_slot.data(), host[0].data(), host[1].data(), host[2].data(), host[3].data(),
|
|
slots[0].data(), slots[1].data(), slots[2].data(), slots[3].data(), runs_len[0], runs_len[1], runs_len[2], runs_len[3], 4);
|
|
});
|
|
const int placed = m.ctl[2];
|
|
std::string order = "[";
|
|
for (int k = 0; k < placed; ++k) order += (k ? "," : "") + std::to_string(m.miss_block[k]);
|
|
std::string ctl = "[";
|
|
for (int k = 0; k < 16; ++k) ctl += (k ? "," : "") + std::to_string(m.ctl[k]);
|
|
call_list += std::string(call ? ",\n" : "") + "{\"order\":" + order + "],\"ctl\":" + ctl + "],\"state_fnv\":" +
|
|
std::to_string((long long) state_hash(m, placed)) + "}";
|
|
}
|
|
j.raw("calls", call_list + "]");
|
|
j.num("slots_fnv_k_q", (long long) fnv_vec(slots[0])); j.num("slots_fnv_v_q", (long long) fnv_vec(slots[1]));
|
|
j.num("slots_fnv_k_scale", (long long) fnv_vec(slots[2])); j.num("slots_fnv_v_scale", (long long) fnv_vec(slots[3]));
|
|
|
|
// the static ring of the MTP drafter and the plain u4 copy that stands in for cudaMemcpyAsync
|
|
std::vector<int32_t> ring(37, 0);
|
|
emu_launch(dim3(64), dim3(256), [&] { leleka_kv_ring_table(ring.data(), 37, 8); });
|
|
j.ints("ring_table", ring);
|
|
std::vector<uint8_t> src(100 * 16), dst(100 * 16, 0);
|
|
for (size_t i = 0; i < src.size(); ++i) src[i] = host_byte(5, i);
|
|
emu_launch(dim3(2), dim3(128), [&] { leleka_kv_copy_u4(src.data(), dst.data(), 100); });
|
|
j.num("copy_u4_equal", src == dst);
|
|
|
|
// reset again: everything evicted, counters zero
|
|
emu_launch(dim3(128), dim3(256), [&] {
|
|
leleka_kv_stream_reset(m.page_table.data(), m.slot_block.data(), m.slot_stamp.data(), m.slot_ref.data(), m.ctl.data(), n_blocks, n_slots);
|
|
});
|
|
j.num("final_reset_state_fnv", (long long) state_hash(m, 0));
|
|
return j;
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------ keyless routing
|
|
// matrices[(h*256+d)*256+j] = (d==j ? .8f : 0.f) + (float)(((h*19+d*3+j*11)%17)-8) * .001f (mirrored in Rust)
|
|
std::vector<float> routing_matrices(int heads) {
|
|
std::vector<float> m((size_t) heads * 256 * 256);
|
|
for (int h = 0; h < heads; ++h)
|
|
for (int d = 0; d < 256; ++d)
|
|
for (int j = 0; j < 256; ++j)
|
|
m[((size_t) h * 256 + d) * 256 + j] = (d == j ? .8f : 0.f) + (float) (((h * 19 + d * 3 + j * 11) % 17) - 8) * .001f;
|
|
return m;
|
|
}
|
|
|
|
Json scenario_route() {
|
|
const int tokens = 3, heads = 3;
|
|
const auto m = routing_matrices(heads);
|
|
Lcg g(61);
|
|
std::vector<float> q((size_t) tokens * heads * 256);
|
|
for (auto& x : q) x = g.soft();
|
|
std::vector<float> out = q;
|
|
emu_launch(dim3(tokens * heads), dim3(256), [&] { leleka_keyless_route_query(out.data(), m.data(), heads); });
|
|
Json j;
|
|
j.num("tokens", tokens); j.num("heads", heads);
|
|
j.hex("routed", out);
|
|
return j;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------- control vector
|
|
Json scenario_cvec() {
|
|
const int n = 300, hc = 3, T = 2, L = 4, layer = 2;
|
|
Lcg g(71);
|
|
std::vector<float> v(n);
|
|
for (auto& x : v) x = g.soft();
|
|
double nrm = 0;
|
|
for (float x : v) nrm += (double) x * x;
|
|
nrm = std::sqrt(nrm);
|
|
std::vector<float> unit(n);
|
|
for (int j = 0; j < n; ++j) unit[j] = (float) (v[j] / nrm);
|
|
const size_t r_ld = (size_t) hc * n;
|
|
std::vector<float> R0((size_t) T * r_ld), bo((size_t) T * n), inj((size_t) T * hc);
|
|
for (auto& x : R0) x = g.soft() * 3.0f;
|
|
for (auto& x : bo) x = g.soft();
|
|
for (auto& x : inj) x = g.soft();
|
|
Json j;
|
|
j.num("n", n); j.num("hc", hc); j.num("T", T); j.num("layer", layer); j.num("L", L);
|
|
j.hex("unit", unit); j.hex("add_dir", v); j.hex("r0", R0); j.hex("bo", bo); j.hex("inj", inj);
|
|
auto run = [&](const char* name, int mode, const std::vector<float>& dirv, float s_val, int layer_used, bool on, bool write) {
|
|
std::vector<float> dir((size_t) L * n, 0.0f), s(L, 0.0f);
|
|
std::copy(dirv.begin(), dirv.end(), dir.begin() + (size_t) layer * n);
|
|
s[layer] = s_val;
|
|
int on_flag = on ? 1 : 0;
|
|
std::vector<float> R = R0;
|
|
emu_launch(dim3(hc, T), dim3(256), [&] {
|
|
leleka_cvec_apply(R.data(), dir.data(), s.data(), &on_flag, mode, layer_used, n, hc, (long long) r_ld,
|
|
write ? bo.data() : nullptr, n, write ? inj.data() : nullptr, hc, write ? 1 : 0);
|
|
});
|
|
j.hex(name, R);
|
|
};
|
|
run("project_s1", 0, unit, 1.0f, layer, true, false);
|
|
run("project_s2", 0, unit, 2.0f, layer, true, false);
|
|
run("add", 1, v, 1.0f, layer, true, false);
|
|
run("off", 0, unit, 1.0f, layer, false, false);
|
|
run("off_write", 0, unit, 1.0f, layer, false, true);
|
|
run("project_write", 0, unit, 1.0f, layer, true, true);
|
|
run("add_write", 1, v, 1.0f, layer, true, true);
|
|
run("no_direction_layer", 0, unit, 1.0f, 1, true, false);
|
|
return j;
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------ attention
|
|
constexpr int ATT_CELLS = 70, ATT_CAP = 70, ATT_NCHUNKS = 2, ATT_NQ = 2, NH = 24;
|
|
uint64_t scratch_floats() { return (uint64_t) ATT_NCHUNKS * NH * (HD + 2) + 64; }
|
|
|
|
struct AttnRun { std::vector<float> out; };
|
|
// chunk kernel then merge, for ATT_NQ queries with per-query selections
|
|
template <class ChunkFn>
|
|
std::vector<float> run_attention(const std::vector<int32_t>& ids, const std::vector<int32_t>& steps, int n_q, ChunkFn chunk) {
|
|
const long long stride = (long long) scratch_floats();
|
|
std::vector<float> scratch((size_t) stride * n_q, 0.0f), out((size_t) n_q * NH * HD, 0.0f);
|
|
float* part_acc = scratch.data();
|
|
float* part_m = part_acc + (size_t) ATT_NCHUNKS * NH * HD;
|
|
float* part_l = part_m + (size_t) ATT_NCHUNKS * NH;
|
|
emu_launch(dim3(ATT_NCHUNKS, HEADS, n_q), dim3(256), [&] { chunk(part_acc, part_m, part_l, stride); });
|
|
emu_launch(dim3(NH, n_q), dim3(256), [&] { leleka_qsa_attn_merge(part_acc, part_m, part_l, ATT_NCHUNKS, out.data(), stride); });
|
|
return out;
|
|
}
|
|
|
|
std::vector<int32_t> att_table() { // permutation of the 18 pages
|
|
std::vector<int32_t> t(18);
|
|
for (int i = 0; i < 18; ++i) t[i] = (i * 5 + 3) % 18;
|
|
return t;
|
|
}
|
|
|
|
Json scenario_attention() {
|
|
Json root;
|
|
const auto table = att_table();
|
|
const int pages = 18;
|
|
const size_t rows = (size_t) pages * HEADS * PAGE;
|
|
Lcg g(81);
|
|
std::vector<std::vector<float>> kc, vc;
|
|
for (int c = 0; c < ATT_CELLS; ++c) {
|
|
std::vector<float> k(512), v(512);
|
|
for (auto& x : k) x = g.soft() * 1.5f;
|
|
for (auto& x : v) x = g.soft() * 1.5f;
|
|
kc.push_back(k); vc.push_back(v);
|
|
}
|
|
std::vector<float> q((size_t) ATT_NQ * NH * HD);
|
|
for (auto& x : q) x = g.soft();
|
|
root.num("input_fnv", (long long) (fnv_vec(q) ^ (fnv_vec(kc[0]) * 3) ^ (fnv_vec(vc[ATT_CELLS - 1]) * 5)));
|
|
// selections: query 0 attends cells 0..69, query 1 cells 0..65
|
|
std::vector<int32_t> ids((size_t) ATT_NQ * ATT_CAP, 0), steps((size_t) ATT_NQ * 4, 0);
|
|
for (int i = 0; i < ATT_CELLS; ++i) ids[i] = i;
|
|
for (int i = 0; i < 66; ++i) ids[ATT_CAP + i] = i;
|
|
steps[3] = ATT_CELLS; steps[4 + 3] = 66;
|
|
|
|
auto append_q4 = [&](std::vector<uint8_t>& kp, std::vector<uint8_t>& vp, const std::vector<float>& k, const std::vector<float>& v, int pos) {
|
|
const std::vector<int32_t> step = {pos, pos + 1, (pos + 1) / 4, 0};
|
|
emu_launch(dim3(HEADS, HD / 32, 2), dim3(32), [&] {
|
|
leleka_kv_append_q4_step(kp.data(), vp.data(), table.data(), step.data(), k.data(), v.data(), HEADS, HD, PAGE, nullptr, nullptr);
|
|
});
|
|
};
|
|
|
|
// ---- mode 2: Q4_0 K and V (rotated), rotated query, output in the rotated domain
|
|
{
|
|
std::vector<uint8_t> kp(rows * ROW_Q4), vp(rows * ROW_Q4);
|
|
for (int c = 0; c < ATT_CELLS; ++c) append_q4(kp, vp, rotate(kc[c]), rotate(vc[c]), c);
|
|
const auto qr = rotate(q);
|
|
const auto out = run_attention(ids, steps, ATT_NQ, [&](float* pa, float* pm, float* pl, long long stride) {
|
|
leleka_qsa_attn_chunk_m2(qr.data(), kp.data(), vp.data(), table.data(), ids.data(), steps.data(), HEADS, PAGE, pa, pm, pl, ATT_NCHUNKS, ATT_CAP, stride);
|
|
});
|
|
Json j; j.hex("out", out);
|
|
root.obj("m2", j);
|
|
}
|
|
// ---- mode 3: INT8 K (unrotated), Q4_0 V (rotated), unrotated query, rotated output
|
|
{
|
|
std::vector<int8_t> kq(rows * HD);
|
|
std::vector<uint16_t> ks(rows * 4);
|
|
std::vector<uint8_t> v4(rows * ROW_Q4);
|
|
for (int c = 0; c < ATT_CELLS; ++c) {
|
|
const std::vector<int32_t> step = {c, c + 1, (c + 1) / 4, 0};
|
|
emu_launch(dim3(HEADS, HD / 64, 2), dim3(64), [&] {
|
|
leleka_kv_append_q8_step(kq.data(), kq.data(), ks.data(), ks.data(), table.data(), step.data(), kc[c].data(), kc[c].data(), HEADS, HD, PAGE, nullptr, nullptr, nullptr, nullptr);
|
|
});
|
|
const auto vr = rotate(vc[c]);
|
|
emu_launch(dim3(HEADS, HD / 32, 2), dim3(32), [&] {
|
|
leleka_kv_append_q4_step(v4.data(), v4.data(), table.data(), step.data(), vr.data(), vr.data(), HEADS, HD, PAGE, nullptr, nullptr);
|
|
});
|
|
}
|
|
const auto out = run_attention(ids, steps, ATT_NQ, [&](float* pa, float* pm, float* pl, long long stride) {
|
|
leleka_qsa_attn_chunk_m3(q.data(), kq.data(), ks.data(), v4.data(), table.data(), ids.data(), steps.data(), HEADS, PAGE, pa, pm, pl, ATT_NCHUNKS, ATT_CAP, stride);
|
|
});
|
|
Json j; j.hex("out", out);
|
|
root.obj("m3", j);
|
|
}
|
|
// ---- mode 4: V-only INT8, routed + position-encoded queries, keys rebuilt from V by position
|
|
{
|
|
std::vector<int8_t> vq(rows * HD);
|
|
std::vector<uint16_t> vs(rows * 4);
|
|
for (int c = 0; c < ATT_CELLS; ++c) {
|
|
const std::vector<int32_t> step = {c, c + 1, (c + 1) / 4, 0};
|
|
emu_launch(dim3(HEADS, HD / 64, 1), dim3(64), [&] {
|
|
leleka_kv_append_q8_step(nullptr, vq.data(), nullptr, vs.data(), table.data(), step.data(), kc[c].data(), vc[c].data(), HEADS, HD, PAGE, nullptr, nullptr, nullptr, nullptr);
|
|
});
|
|
}
|
|
// rope tables [position][32] from double math
|
|
std::vector<float> co((size_t) ATT_CELLS * 32), si((size_t) ATT_CELLS * 32);
|
|
for (int p = 0; p < ATT_CELLS; ++p)
|
|
for (int d = 0; d < 32; ++d) {
|
|
const double angle = p * std::pow(1e7, -2.0 * d / 64);
|
|
co[(size_t) p * 32 + d] = (float) std::cos(angle);
|
|
si[(size_t) p * 32 + d] = (float) std::sin(angle);
|
|
}
|
|
std::vector<int32_t> mpos((size_t) ATT_CELLS * 3);
|
|
for (int cell = 0; cell < ATT_CELLS; ++cell) for (int sector = 0; sector < 3; ++sector) mpos[cell * 3 + sector] = cell / (sector + 2);
|
|
root.hex("cos", co); root.hex("sin", si);
|
|
const auto matrices = routing_matrices(NH);
|
|
// per-query positions and selections (cells with cell % 3 != 1 up to the position)
|
|
const int qpos[ATT_NQ] = {ATT_CELLS - 1, 51};
|
|
std::vector<int32_t> kids((size_t) ATT_NQ * ATT_CAP, 0), ksteps((size_t) ATT_NQ * 4, 0);
|
|
for (int b = 0; b < ATT_NQ; ++b) {
|
|
int w = 0;
|
|
for (int cell = 0; cell <= qpos[b]; ++cell) if (cell % 3 != 1) kids[(size_t) b * ATT_CAP + w++] = cell;
|
|
ksteps[b * 4 + 3] = w;
|
|
}
|
|
for (int vision = 0; vision < 2; ++vision) {
|
|
std::vector<float> routed = q;
|
|
emu_launch(dim3(ATT_NQ * NH), dim3(256), [&] { leleka_keyless_route_query(routed.data(), matrices.data(), NH); });
|
|
std::vector<float> roped = routed;
|
|
for (int b = 0; b < ATT_NQ; ++b) for (int h = 0; h < NH; ++h) for (int d = 0; d < 32; ++d) {
|
|
const int i = (b * NH + h) * HD + d;
|
|
const int rp = vision ? mpos[qpos[b] * 3 + d % 3] : qpos[b];
|
|
const float a = roped[i], vv = roped[i + 32], c = co[(size_t) rp * 32 + d], s2 = si[(size_t) rp * 32 + d];
|
|
roped[i] = a * c - vv * s2; roped[i + 32] = a * s2 + vv * c;
|
|
}
|
|
const auto out = run_attention(kids, ksteps, ATT_NQ, [&](float* pa, float* pm, float* pl, long long stride) {
|
|
leleka_qsa_attn_chunk_m4(roped.data(), vq.data(), vs.data(), table.data(), co.data(), si.data(), vision ? mpos.data() : nullptr, 64,
|
|
kids.data(), ksteps.data(), HEADS, PAGE, pa, pm, pl, ATT_NCHUNKS, ATT_CAP, stride);
|
|
});
|
|
Json j; j.hex("out", out);
|
|
root.obj(vision ? "m4_mrope" : "m4", j);
|
|
}
|
|
}
|
|
return root;
|
|
}
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
Json root;
|
|
root.obj("fwht", scenario_fwht());
|
|
root.obj("q4", scenario_q4());
|
|
root.obj("q8", scenario_q8());
|
|
root.obj("stream", scenario_stream());
|
|
root.obj("route", scenario_route());
|
|
root.obj("cvec", scenario_cvec());
|
|
root.obj("attn", scenario_attention());
|
|
#ifdef HAVE_SCENARIOS_2
|
|
scenarios_2(root);
|
|
#endif
|
|
const std::string text = root.finish() + "\n";
|
|
if (argc > 1) {
|
|
FILE* f = std::fopen(argv[1], "w");
|
|
std::fwrite(text.data(), 1, text.size(), f);
|
|
std::fclose(f);
|
|
} else {
|
|
std::fwrite(text.data(), 1, text.size(), stdout);
|
|
}
|
|
return 0;
|
|
}
|