Files
strata/src/core/native_head.cpp
T
Joost R. 1564404f62 WSL2: survive the driver's ~1 GiB pinned host budget with 3 GPUs
Under WSL2 the NVIDIA driver can pin/map only about 1 GiB of host memory in
total (dmesg: "dxgk: create_existing_sysmem: establish_gpadl failed: -122").
With --expert-cache-device1/2 the startup ran out of it twice:

- CUDA2's context is created after CUDA0's weights and MTP, whose mapped host
  buffers have spent the budget: "CUDA2 experts: cudaSetDevice(2) failed: out
  of memory". STRATA_EARLY_REMOTE_CONTEXTS=1 now creates every secondary
  context up front, like CUDA1's (opt-in: the default order is unchanged).
- With three contexts up, the 260 MiB mapped native embedding no longer fits:
  "native embedding: cannot pin 260 MiB". It now falls back to a device copy
  on the current GPU instead of failing the start.

Measured in Docker Desktop (WSL2), RTX 5070 Ti + 2x RTX 5060 Ti, Swift 1.5
IQ3_XXS: starts and serves with both helper caches (5000 experts each).
2026-09-30 14:01:52 +02:00

168 lines
7.0 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 <cuda_runtime.h>
#include <climits>
#include <cstdio>
#include <cstring>
#include <exception>
namespace strata::core {
NativeHead::~NativeHead() {
if (scratch_) cudaFree(scratch_);
if (weights_) cudaFree(weights_);
}
bool NativeHead::load(const std::string& path, 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 {
strata::GgufFile gguf(path);
err = strata::check_architecture(gguf);
if (!err.empty()) return false;
const strata::TensorInfo* tensor = nullptr;
for (const auto& candidate : gguf.tensors()) {
if (candidate.name != "output.weight") continue;
if (tensor) { err = "native head: duplicate output.weight"; return false; }
tensor = &candidate;
}
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));
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;
scratch_ = scratch;
bytes_ = bytes;
n_in_ = (int) n_in;
n_out_ = (int) n_out;
type_ = (int) tensor->type;
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::string& path, int64_t n_embd, int64_t n_vocab, std::string& err) {
try {
strata::GgufFile gguf(path);
const strata::TensorInfo* t = nullptr;
for (const auto& c : gguf.tensors())
if (c.name == "token_embd.weight") t = &c;
if (!t || t->shape.size() != 2 || t->shape[0] != (uint64_t) n_embd || t->shape[1] != (uint64_t) n_vocab ||
!strata::kernels::iq_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;
if (cudaHostAlloc(&host_, bytes_, cudaHostAllocMapped | cudaHostAllocPortable) != 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, 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 (cudaHostGetDevicePointer(&d, host_, 0) != cudaSuccess) {
err = "native embedding: no device alias for the mapped table";
return false;
}
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