Inference engine, OpenAI/Anthropic API on localhost, optional image input, one-file setup (START-HERE.bat / setup.sh). Model files are downloaded from Hugging Face by the setup.
152 lines
6.1 KiB
C++
152 lines
6.1 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 <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_);
|
|
}
|
|
|
|
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) {
|
|
host_ = nullptr;
|
|
err = "native embedding: cannot pin " + std::to_string(bytes_ >> 20) + " MiB";
|
|
return false;
|
|
}
|
|
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
|