Six offline-compilation sources spliced verbatim from the Original (kv_q4.cu, kv_q8.cu, kv_stream.cu, keyless.cu, cvec.cu, qsa_decode_attn.cu modes 2/3/4) with flat extern C entries; kernel bodies are byte-identical, only signatures and launch geometry moved. A thread-per-CUDA-thread emulation header runs the kernel text on the host; the FWHT, Q4_0, INT8, V-only, K8V4 and streaming-append paths match the Rust references bit for bit (new cache module = CPU twin of the pool data movement). No GPU, no nvcc. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
121 lines
7.1 KiB
C++
121 lines
7.1 KiB
C++
// Shared pieces of the kernel-emulation harness: the deterministic integer generator (mirrored exactly by
|
|
// tests/common/mod.rs), JSON/hex writers, "device" buffers, and the C prototypes of the kernel entries.
|
|
#pragma once
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// ---- the entries of crates/cuda-driver/src/experimental_*.cu (each compiled into its own object)
|
|
extern "C" {
|
|
void leleka_fwht256_f32(const float* src, float* dst, long long n_rows);
|
|
void leleka_kv_append_q4_step(uint8_t* k_q4, uint8_t* v_q4, const int32_t* table, const int32_t* step,
|
|
const float* kcur, const float* vcur, int kv_heads, int head_dim, int page_size,
|
|
uint8_t* host_k_q4, uint8_t* host_v_q4);
|
|
void leleka_kv_append_q4_batch(uint8_t* k_q4, uint8_t* v_q4, const int32_t* table, long long pos0, const float* K,
|
|
const float* V, int kv_heads, int head_dim, int page_size, int is_v_grid,
|
|
uint8_t* host_k_q4, uint8_t* host_v_q4, uint8_t* stage_k_q4, uint8_t* stage_v_q4);
|
|
void leleka_kv_gather_q4_step(const uint8_t* k_q4, const uint8_t* v_q4, const int32_t* table, const int32_t* ids,
|
|
const int32_t* step, int kv_heads, int head_dim, int page_size, uint16_t* k_scratch,
|
|
uint16_t* v_scratch);
|
|
void leleka_kv_append_q8_step(int8_t* k_q, int8_t* v_q, uint16_t* k_scale, uint16_t* v_scale, const int32_t* table,
|
|
const int32_t* step, const float* kcur, const float* vcur, int kv_heads, int head_dim,
|
|
int page_size, int8_t* host_k_q, int8_t* host_v_q, uint16_t* host_k_scale,
|
|
uint16_t* host_v_scale);
|
|
void leleka_kv_gather_q8_step(const int8_t* k_q, const int8_t* v_q, const uint16_t* k_scale, const uint16_t* v_scale,
|
|
const int32_t* table, const int32_t* ids, const int32_t* step, int kv_heads,
|
|
int head_dim, int page_size, uint16_t* k_scratch, uint16_t* v_scratch);
|
|
void leleka_keyless_route_query(float* q, const float* matrices, int heads);
|
|
void leleka_cvec_apply(float* R, const float* dir, const float* s_l, const int* on, int mode, long long layer, int n,
|
|
int hc, long long r_ld, const float* bo, long long bo_ld, const float* inj, long long inj_ld,
|
|
int write);
|
|
void leleka_qsa_attn_chunk_m2(const float* q, const uint8_t* k_q4, const uint8_t* v_q4, const int32_t* page_table,
|
|
const int32_t* ids, const int32_t* step, int n_kv_heads, int page_size, float* part_acc,
|
|
float* part_m, float* part_l, int n_chunks, int cap, long long scratch_stride);
|
|
void leleka_qsa_attn_chunk_m3(const float* q, const int8_t* k_q, const uint16_t* k_scale, const uint8_t* v_q4,
|
|
const int32_t* page_table, const int32_t* ids, const int32_t* step, int n_kv_heads,
|
|
int page_size, float* part_acc, float* part_m, float* part_l, int n_chunks, int cap,
|
|
long long scratch_stride);
|
|
void leleka_qsa_attn_chunk_m4(const float* q, const int8_t* v_q, const uint16_t* v_scale, const int32_t* page_table,
|
|
const float* keyless_cos, const float* keyless_sin, const int32_t* keyless_mrope,
|
|
int keyless_n_rot, const int32_t* ids, const int32_t* step, int n_kv_heads,
|
|
int page_size, float* part_acc, float* part_m, float* part_l, int n_chunks, int cap,
|
|
long long scratch_stride);
|
|
void leleka_qsa_attn_merge(const float* part_acc, const float* part_m, const float* part_l, int n_chunks,
|
|
float* attn, long long scratch_stride);
|
|
void leleka_kv_stream_reset(int32_t* page_table, int32_t* slot_block, int32_t* slot_stamp, int32_t* slot_ref,
|
|
int32_t* ctl, long long n_blocks, long long n_slots);
|
|
void leleka_kv_stream_resolve(int32_t* page_table, int32_t* slot_block, int32_t* slot_stamp, int32_t* slot_ref,
|
|
int32_t* ctl, int32_t* miss_block, int32_t* miss_slot, long long n_slots,
|
|
const int32_t* ids, const int32_t* steps, int n_q, int cap, int page_size);
|
|
void leleka_kv_stream_copy(int32_t* ctl, int32_t* miss_block, int32_t* miss_slot, const uint8_t* src0,
|
|
const uint8_t* src1, const uint8_t* src2, const uint8_t* src3, uint8_t* dst0, uint8_t* dst1,
|
|
uint8_t* dst2, uint8_t* dst3, int len0, int len1, int len2, int len3, int n_runs);
|
|
void leleka_kv_ring_table(int32_t* table, long long n_blocks, long long n_slots);
|
|
void leleka_kv_copy_u4(const void* src, void* dst, long long n_u4);
|
|
}
|
|
|
|
// ---- deterministic data: identical in tests/common/mod.rs
|
|
struct Lcg {
|
|
uint64_t s;
|
|
explicit Lcg(uint64_t seed) : s(seed) {}
|
|
uint32_t next() {
|
|
s = s * 6364136223846793005ull + 1442695040888963407ull;
|
|
return (uint32_t) (s >> 32);
|
|
}
|
|
// uniform in [-1, 1) on a 2^-23 grid: integer -> float and the scale are exact
|
|
float unit() { return (float) ((int32_t) (next() >> 8) - (1 << 23)) * (1.0f / 8388608.0f); }
|
|
// approximately normal: the sum of four units / 2 (variance 1/3 * 4 / 4)
|
|
float soft() { return (unit() + unit() + unit() + unit()) * 0.5f; }
|
|
uint32_t below(uint32_t n) { return next() % n; }
|
|
};
|
|
|
|
inline std::string hex_bytes(const void* p, size_t n) {
|
|
static const char* d = "0123456789abcdef";
|
|
std::string out;
|
|
out.reserve(n * 2);
|
|
const uint8_t* b = (const uint8_t*) p;
|
|
for (size_t i = 0; i < n; ++i) {
|
|
out += d[b[i] >> 4];
|
|
out += d[b[i] & 15];
|
|
}
|
|
return out;
|
|
}
|
|
template <class T>
|
|
std::string hex_vec(const std::vector<T>& v) {
|
|
return hex_bytes(v.data(), v.size() * sizeof(T));
|
|
}
|
|
inline uint64_t fnv1a(const void* p, size_t n) {
|
|
uint64_t h = 0xcbf29ce484222325ull;
|
|
for (size_t i = 0; i < n; ++i) h = (h ^ ((const uint8_t*) p)[i]) * 0x100000001b3ull;
|
|
return h;
|
|
}
|
|
template <class T>
|
|
uint64_t fnv_vec(const std::vector<T>& v) {
|
|
return fnv1a(v.data(), v.size() * sizeof(T));
|
|
}
|
|
|
|
// minimal JSON object writer (flat keys, values pre-rendered)
|
|
struct Json {
|
|
std::string out = "{";
|
|
bool first = true;
|
|
void raw(const std::string& key, const std::string& value) {
|
|
out += (first ? "\n\"" : ",\n\"") + key + "\":" + value;
|
|
first = false;
|
|
}
|
|
void str(const std::string& key, const std::string& s) { raw(key, "\"" + s + "\""); }
|
|
void num(const std::string& key, long long v) { raw(key, std::to_string(v)); }
|
|
template <class T>
|
|
void hex(const std::string& key, const std::vector<T>& v) { str(key, hex_vec(v)); }
|
|
template <class T>
|
|
void ints(const std::string& key, const std::vector<T>& v) {
|
|
std::string s = "[";
|
|
for (size_t i = 0; i < v.size(); ++i) s += (i ? "," : "") + std::to_string((long long) v[i]);
|
|
raw(key, s + "]");
|
|
}
|
|
void obj(const std::string& key, const Json& j) { raw(key, j.finish()); }
|
|
std::string finish() const { return out + "}"; }
|
|
};
|