Source: https://github.com/Niko1221/Strata, commit 6f32ec070f23ced9f50e704d854d775da52591ab (engine 0.1.39, MIT). Every file is as upstream published it; later commits show exactly what this fork changes. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
116 lines
5.9 KiB
C++
116 lines
5.9 KiB
C++
#pragma once
|
|
|
|
#include <cstddef>
|
|
|
|
namespace strata::kernels {
|
|
|
|
// Native GGUF Q2_0, Q4_0, Q5_0, Q8_0, Q3_K, Q4_K, Q5_K, Q6_K, IQ4_NL
|
|
// and IQ4_XS / CUDA Q8_1 adapters, pinned to llama.cpp
|
|
// 3cf03257f219afbe7334045ff7c6a06ac68c627d, sm_120 generic MMVQ.
|
|
// All pointers are device pointers, at least 4-byte aligned, with no overlap.
|
|
// All calls enqueue on the explicit non-null CUDA stream; no allocation or wait.
|
|
// The translation unit must use --use_fast_math, as the pinned CUDA oracle does.
|
|
//
|
|
// Shapes use GGUF order: n_in is the contiguous reduction dimension, n_out is
|
|
// the weight row count, and ncols is the activation column/token count, 1..8
|
|
// (plan v0.3 P3). Columns are contiguous: activation column j is x + j * n_in
|
|
// (its Q8_1 blocks at j * n_in / 32) and output column j is y + j * n_out. See
|
|
// native_mmvq_set_multi_exact for how ncols > 1 relates to ncols == 1.
|
|
// n_in must be a positive multiple of 32 for quantization/Q4_0/Q5_0/Q8_0/IQ4_NL,
|
|
// 64 for Q2_0, or 256 for the other formats. n_out must be positive. Weights remain
|
|
// unmodified row-major GGUF blocks: Q3_K=110, Q4_K=144, Q5_K=176, Q6_K=210 and
|
|
// IQ4_XS=136 bytes per 256 elements; Q2_0 is 18 bytes per 64 elements.
|
|
// Q4_0/IQ4_NL=18, Q5_0=22, Q8_0=34 bytes per 32 elements.
|
|
// Q8_1 scratch has 36 bytes per 32 elements, with no extra row padding here.
|
|
// Input floats must be finite, and their block scales/sums representable in FP16.
|
|
std::size_t native_q8_1_bytes(int n_in, int ncols = 1);
|
|
|
|
// Layout for ncols > 1. false: llama.cpp's generic multi-column table (upstream), equal to ncols == 1 to
|
|
// float rounding, speed not yet measured. true (default): the ncols == 1 layout, every column bitwise equal to a
|
|
// single-column call. Set before
|
|
// graph capture; captured graphs keep the kernels they captured.
|
|
void native_mmvq_set_multi_exact(bool exact);
|
|
bool native_mmvq_multi_exact();
|
|
|
|
// One quantization may serve multiple weight matrices sharing the same input.
|
|
// Q8_1 stores FP16 scale and FP16 warp sum of the ORIGINAL float inputs; it does
|
|
// not reconstruct that sum from the quantized integers.
|
|
void native_quantize_q8_1(const float* x, void* x_q8_1, int n_in, int ncols,
|
|
void* stream);
|
|
|
|
void native_swiglu_quantize_q8_1(const float* gate, const float* up, void* x_q8_1,
|
|
int n_in, int ncols, void* stream);
|
|
|
|
void native_q5_k_mmvq(const void* weights, const void* x_q8_1, float* y,
|
|
int n_in, int n_out, int ncols, void* stream);
|
|
|
|
// Convenience composition: caller owns scratch sized by native_q8_1_bytes.
|
|
void native_q5_k_f32(const void* weights, const float* x, void* scratch_q8_1,
|
|
float* y, int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q2_0_mmvq(const void* weights, const void* x_q8_1, float* y,
|
|
int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q2_0_f32(const void* weights, const float* x, void* scratch_q8_1,
|
|
float* y, int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q3_k_mmvq(const void* weights, const void* x_q8_1, float* y,
|
|
int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q3_k_f32(const void* weights, const float* x, void* scratch_q8_1,
|
|
float* y, int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_iq4_xs_mmvq(const void* weights, const void* x_q8_1, float* y,
|
|
int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_iq4_xs_f32(const void* weights, const float* x, void* scratch_q8_1,
|
|
float* y, int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q4_k_mmvq(const void* weights, const void* x_q8_1, float* y,
|
|
int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q4_k_f32(const void* weights, const float* x, void* scratch_q8_1,
|
|
float* y, int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q6_k_mmvq(const void* weights, const void* x_q8_1, float* y,
|
|
int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q6_k_f32(const void* weights, const float* x, void* scratch_q8_1,
|
|
float* y, int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q4_0_mmvq(const void* weights, const void* x_q8_1, float* y,
|
|
int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q4_0_f32(const void* weights, const float* x, void* scratch_q8_1,
|
|
float* y, int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q5_0_mmvq(const void* weights, const void* x_q8_1, float* y,
|
|
int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q5_0_f32(const void* weights, const float* x, void* scratch_q8_1,
|
|
float* y, int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q8_0_mmvq(const void* weights, const void* x_q8_1, float* y,
|
|
int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_q8_0_f32(const void* weights, const float* x, void* scratch_q8_1,
|
|
float* y, int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_iq4_nl_mmvq(const void* weights, const void* x_q8_1, float* y,
|
|
int n_in, int n_out, int ncols, void* stream);
|
|
|
|
void native_iq4_nl_f32(const void* weights, const float* x, void* scratch_q8_1,
|
|
float* y, int n_in, int n_out, int ncols, void* stream);
|
|
|
|
// Storage/dispatch helpers take stable GGML type IDs, avoiding a ggml runtime
|
|
// dependency in the engine: Q4_0=2, Q5_0=6, Q8_0=8, Q3_K=11, Q4_K=12, Q5_K=13,
|
|
// Q6_K=14, IQ4_NL=20, IQ4_XS=23, Q2_0=42. Unsupported IDs throw in the byte-count
|
|
// and launch helpers; only the
|
|
// capability query returns false.
|
|
bool native_mmvq_supported(int ggml_type) noexcept;
|
|
std::size_t native_mmvq_weight_bytes(int ggml_type, int n_in, int n_out);
|
|
void native_mmvq(int ggml_type, const void* weights, const void* x_q8_1, float* y,
|
|
int n_in, int n_out, int ncols, void* stream);
|
|
|
|
} // namespace strata::kernels
|