Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree (570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base = the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main). Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details, conflict table and left-out files are in docs/upstream.md. Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61), docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out. scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run"). Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR macros on. No GPU run, no quality gate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
217 lines
10 KiB
C++
217 lines
10 KiB
C++
#include "strata/core/native_head.hpp"
|
|
#include "strata/artifact/gguf_reader.hpp"
|
|
#include "strata/kernels/iq_kernels.hpp"
|
|
#include "strata/kernels/native_mmvq.hpp"
|
|
#include "strata/platform/memory.hpp"
|
|
|
|
#include <cuda_runtime.h>
|
|
#include <climits>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <exception>
|
|
|
|
namespace strata::core {
|
|
|
|
NativeHead::~NativeHead() {
|
|
unload();
|
|
}
|
|
|
|
void NativeHead::unload() {
|
|
if (weights_) strata::kernels::native_q6_k_unpack(weights_);
|
|
if (weights_) strata::kernels::native_q8_0_sm60_release(weights_);
|
|
if (scratch_) cudaFree(scratch_);
|
|
if (weights_) cudaFree(weights_);
|
|
scratch_ = nullptr;
|
|
weights_ = nullptr;
|
|
}
|
|
|
|
bool NativeHead::load(const std::vector<std::string>& shards, int64_t n_in, int64_t n_out, std::string& err) {
|
|
if (loaded()) { err = "native head is already loaded"; return false; }
|
|
if (n_in <= 0 || n_out <= 0 || n_in > INT_MAX || n_out > INT_MAX || n_in % 256) {
|
|
err = "native head requires positive int32 dimensions and whole 256-value rows";
|
|
return false;
|
|
}
|
|
try {
|
|
// The architecture is the metadata shard's; output.weight comes from whichever shard holds it (shard 2
|
|
// of Unsloth's UD-Q4_K_XL, whose shard 1 holds no tensor). GgufModel refuses a duplicate across shards.
|
|
const strata::GgufModel model(shards);
|
|
err = strata::check_engine_architecture(model.meta());
|
|
if (!err.empty()) return false;
|
|
size_t at = 0;
|
|
const strata::TensorInfo* tensor = model.find("output.weight", &at);
|
|
const strata::GgufFile& gguf = model.shard(at);
|
|
if (!tensor || !strata::kernels::native_mmvq_supported((int) tensor->type) || tensor->shape.size() != 2 ||
|
|
tensor->shape[0] != (uint64_t) n_in || tensor->shape[1] != (uint64_t) n_out) {
|
|
err = "native head: expected a natively supported output.weight with the canonical head dimensions";
|
|
return false;
|
|
}
|
|
const uint64_t bytes = strata::kernels::native_mmvq_weight_bytes((int) tensor->type, (int) n_in, (int) n_out);
|
|
const uint64_t payload = gguf.file_size() - gguf.data_start();
|
|
if (tensor->offset > payload || bytes > payload - tensor->offset) {
|
|
err = "native head: truncated output.weight payload";
|
|
return false;
|
|
}
|
|
void* weights = nullptr;
|
|
void* scratch = nullptr;
|
|
cudaError_t status = cudaMalloc(&weights, bytes);
|
|
if (status == cudaSuccess)
|
|
status = cudaMalloc(&scratch, strata::kernels::native_q8_1_bytes((int) n_in, 1));
|
|
strata::platform::advise_willneed(gguf.tensor_data(*tensor), bytes);
|
|
if (status == cudaSuccess)
|
|
status = cudaMemcpy(weights, gguf.tensor_data(*tensor), bytes, cudaMemcpyHostToDevice);
|
|
if (status != cudaSuccess) {
|
|
if (scratch) cudaFree(scratch);
|
|
if (weights) cudaFree(weights);
|
|
err = std::string("native head upload: ") + cudaGetErrorString(status);
|
|
return false;
|
|
}
|
|
weights_ = weights;
|
|
{ int d = -1; if (cudaGetDevice(&d) == cudaSuccess) device_ = d; }
|
|
scratch_ = scratch;
|
|
bytes_ = bytes;
|
|
n_in_ = (int) n_in;
|
|
n_out_ = (int) n_out;
|
|
type_ = (int) tensor->type;
|
|
if (type_ == 14 && strata::kernels::native_q6_k_packed_enabled()) // STRATA_Q6_PACKED=1
|
|
strata::kernels::native_q6_k_pack(weights_, n_in_, n_out_, "output head");
|
|
if (type_ == 8) // STRATA_Q8_SM60=1 (a no-op otherwise); the MTP draft head reads the same matrix
|
|
strata::kernels::native_q8_0_sm60_pack(weights_, n_in_, n_out_, "output head");
|
|
return true;
|
|
} catch (const std::exception& error) {
|
|
err = std::string("native head: ") + error.what();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool NativeHead::run(const float* mixed, float* logits, void* stream, std::string& err) const {
|
|
if (!loaded() || !mixed || !logits || !stream) {
|
|
err = "native head requires loaded weights, device buffers and an explicit stream";
|
|
return false;
|
|
}
|
|
try {
|
|
if (type_ == 13) {
|
|
strata::kernels::native_q5_k_f32(weights_, mixed, scratch_, logits, n_in_, n_out_, 1, stream);
|
|
} else {
|
|
strata::kernels::native_quantize_q8_1(mixed, scratch_, n_in_, 1, stream);
|
|
strata::kernels::native_mmvq(type_, weights_, scratch_, logits, n_in_, n_out_, 1, stream);
|
|
}
|
|
} catch (const std::exception& error) {
|
|
err = std::string("native head launch: ") + error.what();
|
|
return false;
|
|
}
|
|
const cudaError_t status = cudaPeekAtLastError();
|
|
if (status != cudaSuccess) {
|
|
err = std::string("native head launch: ") + cudaGetErrorString(status);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// ================================ plan v0.3 P6: THE NATIVE EMBEDDING ================================
|
|
|
|
namespace {
|
|
const NativeEmbed* g_embed = nullptr;
|
|
}
|
|
void set_native_embed(const NativeEmbed* e) { g_embed = e; }
|
|
const NativeEmbed* native_embed() { return g_embed; }
|
|
|
|
NativeEmbed::~NativeEmbed() {
|
|
if (host_) cudaFreeHost(host_);
|
|
else if (dev_) cudaFree(const_cast<void*>(dev_)); // the VRAM fallback below
|
|
}
|
|
|
|
bool NativeEmbed::load(const std::vector<std::string>& shards, int64_t n_embd, int64_t n_vocab, std::string& err) {
|
|
try {
|
|
const strata::GgufModel model(shards);
|
|
// --embd-gguf's one-tensor file (tools/embd_bf16_pack.py) says "strata-embd": only its tensor is checked
|
|
const strata::MetaValue* arch = model.meta().get("general.architecture");
|
|
err = arch != nullptr && arch->s == "strata-embd" ? std::string() : strata::check_engine_architecture(model.meta());
|
|
if (!err.empty()) { err = "native embedding: " + err; return false; }
|
|
size_t at = 0;
|
|
const strata::TensorInfo* t = model.find("token_embd.weight", &at);
|
|
const strata::GgufFile& gguf = model.shard(at);
|
|
if (!t || t->shape.size() != 2 || t->shape[0] != (uint64_t) n_embd || t->shape[1] != (uint64_t) n_vocab ||
|
|
!strata::kernels::embed_type_supported((int) t->type) || n_embd % 256) {
|
|
err = "native embedding: token_embd.weight is absent, of another shape, or of a type without a GPU "
|
|
"dequantizer";
|
|
return false;
|
|
}
|
|
row_ = strata::kernels::iq_row_bytes((int) t->type, n_embd);
|
|
bytes_ = (uint64_t) row_ * (uint64_t) n_vocab;
|
|
// the table is copied out of the mapping below: a truncated shard must be an error, not a read past EOF
|
|
if (!model.in_bounds(*t, at) || strata::tensor_payload_bytes(*t) != bytes_) {
|
|
err = "native embedding: token_embd.weight's payload is truncated or not " + std::to_string(bytes_) +
|
|
" B (" + gguf.path() + ")";
|
|
bytes_ = 0;
|
|
return false;
|
|
}
|
|
strata::platform::advise_willneed(gguf.tensor_data(*t), bytes_); // copied out of the mapping below
|
|
cudaError_t host_err = cudaHostAlloc(&host_, bytes_, cudaHostAllocMapped | cudaHostAllocPortable);
|
|
if (host_err != cudaSuccess) {
|
|
// Under WSL2 the driver's pinned/mapped host budget (~1 GiB) can be spent by the GPU contexts
|
|
// themselves (three cards). The table is only gathered from, so keep it in the current device's VRAM
|
|
// instead: it costs its size there and reads faster than over PCIe.
|
|
cudaGetLastError();
|
|
host_ = nullptr;
|
|
void* d = nullptr;
|
|
if (cudaMalloc(&d, bytes_) != cudaSuccess ||
|
|
cudaMemcpy(d, gguf.tensor_data(*t), bytes_, cudaMemcpyHostToDevice) != cudaSuccess) {
|
|
if (d) cudaFree(d);
|
|
cudaGetLastError();
|
|
err = "native embedding: cannot pin " + std::to_string(bytes_ >> 20) + " MiB: " +
|
|
cudaGetErrorString(host_err) + ", nor place it in VRAM";
|
|
return false;
|
|
}
|
|
std::fprintf(stderr, "strata: native embedding: cannot pin %llu MiB, kept in VRAM instead\n",
|
|
(unsigned long long) (bytes_ >> 20));
|
|
dev_ = d;
|
|
} else {
|
|
std::memcpy(host_, gguf.tensor_data(*t), bytes_);
|
|
void* d = nullptr;
|
|
#if defined(STRATA_USE_HIP)
|
|
// #325: the Windows HIP stack can refuse the device alias of a mapped allocation (and, when it gives
|
|
// one, it is the host address itself - unified addressing; kernels read it correctly there, a
|
|
// device-to-device copy into it does not land: tests/hip/mapped_alias). The table is only gathered from,
|
|
// so without an alias it goes into VRAM like the unpinnable case above, instead of failing the start.
|
|
if (cudaHostGetDevicePointer(&d, host_, 0) != cudaSuccess || d == nullptr) {
|
|
cudaGetLastError();
|
|
d = nullptr;
|
|
if (cudaMalloc(&d, bytes_) != cudaSuccess ||
|
|
cudaMemcpy(d, gguf.tensor_data(*t), bytes_, cudaMemcpyHostToDevice) != cudaSuccess) {
|
|
if (d) cudaFree(d);
|
|
cudaGetLastError();
|
|
err = "native embedding: no device alias for the mapped table, and no VRAM to copy it into";
|
|
return false;
|
|
}
|
|
cudaFreeHost(host_); // the destructor frees dev_ when host_ is null
|
|
host_ = nullptr;
|
|
std::fprintf(stderr, "strata: native embedding: no device alias for the mapped table, kept in VRAM\n");
|
|
}
|
|
#else
|
|
if (cudaHostGetDevicePointer(&d, host_, 0) != cudaSuccess) {
|
|
err = "native embedding: no device alias for the mapped table";
|
|
return false;
|
|
}
|
|
#endif
|
|
dev_ = d;
|
|
}
|
|
type_ = (int) t->type;
|
|
n_embd_ = n_embd;
|
|
n_vocab_ = n_vocab;
|
|
return true;
|
|
} catch (const std::exception& e) {
|
|
err = std::string("native embedding: ") + e.what();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void NativeEmbed::gather_dev(const int32_t* tokens, int64_t n_tok, float* out, void* stream) const {
|
|
strata::kernels::iq_embed_rows(type_, dev_, row_, tokens, n_tok, n_embd_, out, stream);
|
|
}
|
|
|
|
void NativeEmbed::gather_one(int64_t token, float* out, void* stream) const {
|
|
strata::kernels::iq_dequant_f32(type_, (const uint8_t*) dev_ + (size_t) token * row_, n_embd_, out, stream);
|
|
}
|
|
|
|
} // namespace strata::core
|