Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
prefill: the dense GGUF projections through MMQ on HIP (opt-in STRATA_DENSE_MMQ=1), the K-quant instances on the HIP build
xendak43881f14a30a
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 4a6dd9da2..1deb7d76c 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -73,7 +73,7 @@ option(STRATA_PREFILL_MMQ "Build the optional GGML MMQ prefill path for HIP" OFF# CUDA: MMQ prompt kernels for Q4_K / Q5_K / Q5_1 (Unsloth's UD-Q4_K_XL experts). Off by default: the engine loads# every kernel when it starts (CUDA_MODULE_LOADING=EAGER), so these took ~5 MB of VRAM - 1-3 expert slots - from every# model, and the UD-Q4_K_XL prompt is SSD-bound with or without them (docs/UNSLOTH_Q4.md)-option(STRATA_MMQ_KQUANTS "Build the CUDA MMQ prompt kernels for Q4_K / Q5_K / Q5_1 experts" OFF)+option(STRATA_MMQ_KQUANTS "Build the MMQ prompt kernels for Q4_K / Q5_K / Q5_1 / Q6_K: the CUDA experts of Unsloth's UD-Q4_K_XL, and on HIP the dense GGUF projections (STRATA_DENSE_MMQ)" OFF)option(STRATA_ORCA_Q4KS_MMQ "Build the GGML Q5_0 MMQ instance for OrcaRouter Q4_K_S" OFF)if(STRATA_PREFILL_MMQ AND NOT STRATA_ENABLE_HIP)message(FATAL_ERROR "STRATA_PREFILL_MMQ requires STRATA_ENABLE_HIP=ON")@@ -1097,11 +1097,11 @@ if(STRATA_NATIVE_EXPERTS)add_library(strata_mmq STATIC src/prefill/moe_mmq.cu src/prefill/moe_fused.cu src/prefill/moe_fused_iq.cusrc/prefill/ggml_cuda_host.cu ${_strata_mmq}/quantize.cu)# q8_0: the drafter's matrices (E-9); STRATA_ORCA_Q4KS_MMQ adds q5_0 (#296, in _strata_mmq_types), and- # STRATA_MMQ_KQUANTS q4_k q5_k q5_1, the experts of Unsloth's UD-Q4_K_XL (docs/UNSLOTH_Q4.md; the dispatch- # follows eddoursul/Strata 7706c1b)+ # STRATA_MMQ_KQUANTS q4_k q5_k q5_1 q6_k: the experts of Unsloth's UD-Q4_K_XL (docs/UNSLOTH_Q4.md; the dispatch+ # follows eddoursul/Strata 7706c1b), q6_k also the usual attention weight of the plain i-quant GGUFsset(_strata_mmq_cuda_types ${_strata_mmq_types})if(STRATA_MMQ_KQUANTS)- list(APPEND _strata_mmq_cuda_types q4_k q5_k q5_1)+ list(APPEND _strata_mmq_cuda_types q4_k q5_k q5_1 q6_k)endif()foreach(_t IN LISTS _strata_mmq_cuda_types)target_sources(strata_mmq PRIVATE ${_strata_mmq}/template-instances/mmq-instance-${_t}.cu)@@ -1159,6 +1159,11 @@ if(STRATA_NATIVE_EXPERTS)src/prefill/moe_fused_iq.cusrc/prefill/ggml_cuda_host.cu"${_strata_mmq}/quantize.cu")+ if(STRATA_MMQ_KQUANTS)+ # the same K-quant instances as the CUDA branch: on HIP they serve the dense GGUF projections+ # of Gemm::native (STRATA_DENSE_MMQ=1), the mixed-quant packs' attention/PLE tensors+ list(APPEND _strata_mmq_types q4_k q5_k q5_1 q6_k)+ endif()foreach(_t IN LISTS _strata_mmq_types)list(APPEND _strata_mmq_sources "${_strata_mmq}/template-instances/mmq-instance-${_t}.cu")endforeach()@@ -1171,6 +1176,9 @@ if(STRATA_NATIVE_EXPERTS)PRIVATE "${_strata_gg}/include" "${_strata_gg}/src" "${_strata_mmq}"PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)target_compile_definitions(strata_mmq PRIVATE GGML_USE_HIP=1)+ if(STRATA_MMQ_KQUANTS)+ target_compile_definitions(strata_mmq PRIVATE STRATA_MMQ_KQUANTS=1)+ endif()if(STRATA_ORCA_Q4KS_MMQ)target_compile_definitions(strata_mmq PRIVATE STRATA_ORCA_Q4KS_MMQ=1)endif()diff --git a/include/strata/prefill/gemm.hpp b/include/strata/prefill/gemm.hppindex ab148380b..d2960a2f4 100644--- a/include/strata/prefill/gemm.hpp+++ b/include/strata/prefill/gemm.hpp@@ -42,6 +42,8 @@ public:/// W given as native GGUF blocks of `ggml_type`, dequantized to FP16 in the scratch, X in FP16. `ldx` (> K) is/// X's padded row stride, taken only by STRATA_PF_PAD's path (0 = K).+ /// On an MMQ build a beta = 0 product whose type is covered, whose K is a multiple of 256 values and whose matrix fits+ /// the card's shared memory runs through llama.cpp's int8 MMQ instead (opt-in: STRATA_DENSE_MMQ=1).void native(const uint16_t* X, int ggml_type, const void* W_blocks, float* Y, int64_t T, int64_t N, int64_t K,int64_t ldy = 0, float beta = 0.0f, int64_t ldx = 0);@@ -65,6 +67,11 @@ private:int64_t tc_w_elems_ = 0;uint16_t* tc_x_ = nullptr;int64_t tc_x_elems_ = 0;+ bool native_mmq(const uint16_t* X, int ggml_type, const void* W_blocks, float* Y, int64_t T, int64_t N, int64_t K,+ int64_t ldy);+ void* mmq_ctx_ = nullptr;+ void* mmq_buf_ = nullptr;+ bool mmq_failed_ = false;};diff --git a/include/strata/prefill/moe_mmq.hpp b/include/strata/prefill/moe_mmq.hppindex 0859dca35..5c5dd7122 100644--- a/include/strata/prefill/moe_mmq.hpp+++ b/include/strata/prefill/moe_mmq.hpp@@ -12,8 +12,9 @@ namespace strata::prefill::mmq {/// This build has the MMQ path (the ggml sources were available to the build).bool built();-/// MMQ covers this ggml type (the i-quants and Q2_0 the packs use, Q8_0, and in a CUDA build with STRATA_MMQ_KQUANTS-/// the Q4_K / Q5_K / Q5_1 of Unsloth's UD-Q4_K_XL; IQ1_M is not covered).+/// MMQ covers this ggml type (the i-quants and Q2_0 the packs use, Q8_0, and with STRATA_MMQ_KQUANTS the+/// K-quants Q4_K / Q5_K / Q5_1 / Q6_K: Unsloth's UD-Q4_K_XL experts (CUDA), and the dense GGUF projections of+/// the mixed-quant packs through Gemm::native's STRATA_DENSE_MMQ path (HIP); IQ1_M is not covered).bool supported(int ggml_type);/// #420: `supported`, and on every visible GPU llama.cpp's MMQ has a tile for this type and a weight matrix of/// `w_rows` rows that fits the card's shared memory - the same test its tile choice makes, which aborts the process@@ -85,4 +86,9 @@ void swiglu(const float* gu, float* h, int64_t rows, int64_t n_ff, bool interlea/// dst[i] = i for i < n (the identity row map MMQ's MoE mode writes through).void iota(int32_t* dst, int64_t n, void* stream);+/// y[i] = float(x[i]) for FP16 bits x.+void f16_to_f32(const uint16_t* x, float* y, int64_t n, void* stream);+/// dst = {0, rows} (the bounds of one matrix; dst on the device).+void set_bounds(int32_t* dst, int32_t rows, void* stream);+} // namespace strata::prefill::mmqdiff --git a/src/prefill/gemm.cu b/src/prefill/gemm.cuindex 131322290..ff1ba8cc1 100644--- a/src/prefill/gemm.cu+++ b/src/prefill/gemm.cu@@ -1,6 +1,9 @@// src/prefill/gemm.cu - see include/strata/prefill/gemm.hpp.#include "strata/prefill/gemm.hpp"#include "strata/kernels/dequant_bf16.hpp"+#ifdef STRATA_PREFILL_MMQ+#include "strata/prefill/moe_mmq.hpp"+#endif#ifdef STRATA_USE_HIP#include "wmma_gemm.h"@@ -313,6 +316,10 @@ bool try_hipblaslt(void* opaque_state, strata::prefill::hipblaslt::InputType typ} // namespaceGemm::~Gemm() {+#ifdef STRATA_PREFILL_MMQ+ delete static_cast<strata::prefill::mmq::Context*>(mmq_ctx_);+ if (mmq_buf_) cudaFree(mmq_buf_);+#endif#if defined(__HIPCC__) && defined(STRATA_HIPBLASLT_AVAILABLE)delete static_cast<HipLtState*>(hipblaslt_state_);#endif@@ -598,6 +605,73 @@ void Gemm::f16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_STRATA_ABSORB_HIPBLAS_STICKY("cublasGemmEx f16");}+#ifdef STRATA_PREFILL_MMQ+bool Gemm::native_mmq(const uint16_t* X, int type, const void* W, float* Y, int64_t T, int64_t N, int64_t K,+ int64_t ldy) {+ namespace mmq = strata::prefill::mmq;+ constexpr int64_t kRows = 1024, kMaxK = 8192;+ static const bool enabled = [] {+ const char* e = std::getenv("STRATA_DENSE_MMQ");+ return e && e[0] == '1';+ }();+ if (!enabled || mmq_failed_ || !mmq::built() || !mmq::fits(type, N)) return false;+ // K must be a multiple of 256: llama.cpp's MMQ loads the weights in 256-value K chunks, and the+ // chunk past a partial row reads past the row (the next row's bytes - or, for the last weight+ // row, bytes past the tensor, which no caller guarantees to be zeros). The MoE path is safe+ // because its gather buffers carry a zeroed tail; a dense tensor is passed as-is, so a K that+ // is not a full chunk multiple (e.g. the 640-value shared-expert down: 20 blocks of 32) stays on+ // the dequantize + cuBLAS path.+ if (K > kMaxK || K % 256 != 0) return false;+ int64_t R = scratch_elems_ / (2 * K); // FP32 activations for a chunk fit in the FP16 dequant scratch+ if (R > kRows) R = kRows;+ if (R > T) R = T;+ if (R < 1) return false;+ if (ldy <= 0) ldy = N;++ const auto up = [](size_t v) { return (v + 255) & ~(size_t) 255; };+ const size_t ident_off = up(mmq::q8_bytes(kRows, kMaxK));+ const size_t bounds_off = ident_off + up((size_t) kRows * 4);+ if (!mmq_buf_) {+ if (cudaMalloc(&mmq_buf_, bounds_off + 256) != cudaSuccess) {+ cudaGetLastError();+ mmq_failed_ = true;+ return false;+ }+ mmq_ctx_ = new mmq::Context();+ mmq::iota((int32_t*) ((uint8_t*) mmq_buf_ + ident_off), kRows, stream_);+ std::fprintf(stderr, "prefill gemm: dense MMQ on (STRATA_DENSE_MMQ=1)\n");+ }+ void* xq = mmq_buf_;+ const int32_t* ident = (const int32_t*) ((uint8_t*) mmq_buf_ + ident_off);+ int32_t* bounds = (int32_t*) ((uint8_t*) mmq_buf_ + bounds_off);+ float* xf = (float*) scratch_;+ auto* ctx = static_cast<mmq::Context*>(mmq_ctx_);++ for (int64_t r0 = 0; r0 < T; r0 += R) {+ const int64_t rows = (T - r0 < R) ? T - r0 : R;+ mmq::f16_to_f32(X + r0 * K, xf, rows * K, stream_);+ mmq::quantize(xf, nullptr, xq, type, K, K, rows, stream_);+ mmq::set_bounds(bounds, (int32_t) rows, stream_);+ mmq::Product p;+ p.w = W;+ p.type = type;+ p.w_rows = N;+ p.w_cols = K;+ p.expert_bytes = mmq::matrix_bytes(type, N, K);+ p.n = 1;+ p.xq = xq;+ p.bounds = bounds;+ p.ids = ident;+ p.total_rows = rows;+ p.max_rows = rows;+ p.dst = Y + r0 * ldy;+ p.ld_dst = ldy;+ ctx->run(p, stream_);+ }+ return true;+}+#endif+void Gemm::native(const uint16_t* X, int ggml_type, const void* W_blocks, float* Y, int64_t T, int64_t N, int64_t K,int64_t ldy, float beta, int64_t ldx) {#ifdef STRATA_USE_HIP@@ -633,6 +707,9 @@ void Gemm::native(const uint16_t* X, int ggml_type, const void* W_blocks, float*(long long) ldx, (long long) K);std::exit(1);}+#ifdef STRATA_PREFILL_MMQ+ if (beta == 0.0f && (ldx == 0 || ldx == K) && native_mmq(X, ggml_type, W_blocks, Y, T, N, K, ldy)) return;+#endifif (N * K > scratch_elems_) {// Too large for the scratch at once: in row slices.const int64_t rows = scratch_elems_ / K;diff --git a/src/prefill/moe_mmq.cu b/src/prefill/moe_mmq.cuindex c61e707a5..fb351ed80 100644--- a/src/prefill/moe_mmq.cu+++ b/src/prefill/moe_mmq.cu@@ -99,6 +99,16 @@ __global__ void iota_kernel(int32_t* dst, int64_t n) {if (i < n) dst[i] = (int32_t) i;}+__global__ void f16_to_f32_kernel(const uint16_t* __restrict__ x, float* __restrict__ y, int64_t n) {+ const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;+ if (i < n) y[i] = __half2float(__ushort_as_half(x[i]));+}++__global__ void set_bounds_kernel(int32_t* d, int32_t rows) {+ d[0] = 0;+ d[1] = rows;+}+unsigned blocks(int64_t n) { return (unsigned) ((n + 255) / 256); }} // namespace@@ -115,7 +125,7 @@ bool supported(int t) {case GGML_TYPE_IQ3_XXS: case GGML_TYPE_IQ3_S: case GGML_TYPE_IQ4_NL: case GGML_TYPE_IQ4_XS:case GGML_TYPE_Q8_0: // the draft layer's dense matrices (E-9)#ifdef STRATA_MMQ_KQUANTS- case GGML_TYPE_Q4_K: case GGML_TYPE_Q5_K: case GGML_TYPE_Q5_1: // Unsloth's UD-Q4_K_XL experts (CUDA)+ case GGML_TYPE_Q4_K: case GGML_TYPE_Q5_K: case GGML_TYPE_Q6_K: case GGML_TYPE_Q5_1: // the K-quants: Unsloth's UD-Q4_K_XL experts (CUDA), the dense GGUF projections (STRATA_DENSE_MMQ)#endifreturn true;default:@@ -200,6 +210,7 @@ void Context::run(const Product& p, void* stream) {#ifdef STRATA_MMQ_KQUANTScase GGML_TYPE_Q4_K: mul_mat_q_case<GGML_TYPE_Q4_K>(ctx, a, s); break;case GGML_TYPE_Q5_K: mul_mat_q_case<GGML_TYPE_Q5_K>(ctx, a, s); break;+ case GGML_TYPE_Q6_K: mul_mat_q_case<GGML_TYPE_Q6_K>(ctx, a, s); break;case GGML_TYPE_Q5_1: mul_mat_q_case<GGML_TYPE_Q5_1>(ctx, a, s); break;#endifdefault:@@ -263,4 +274,15 @@ void iota(int32_t* dst, int64_t n, void* stream) {ck(cudaGetLastError(), "iota");}+void f16_to_f32(const uint16_t* x, float* y, int64_t n, void* stream) {+ if (n <= 0) return;+ f16_to_f32_kernel<<<blocks(n), 256, 0, (cudaStream_t) stream>>>(x, y, n);+ ck(cudaGetLastError(), "f16_to_f32");+}++void set_bounds(int32_t* dst, int32_t rows, void* stream) {+ set_bounds_kernel<<<1, 1, 0, (cudaStream_t) stream>>>(dst, rows);+ ck(cudaGetLastError(), "set_bounds");+}+} // namespace strata::prefill::mmqdiff --git a/tests/hip/prefill_mmq_parity.cpp b/tests/hip/prefill_mmq_parity.cppindex 2fd7a0d7a..00946e88d 100644--- a/tests/hip/prefill_mmq_parity.cpp+++ b/tests/hip/prefill_mmq_parity.cpp@@ -202,6 +202,30 @@ std::vector<std::vector<uint8_t>> synthetic_q2(int experts, int64_t out_rows, inreturn w;}+// A synthetic matrix of any ggml type with a host quantizer (the K-quants: the dense GGUF tensors the+// STRATA_DENSE_MMQ path multiplies on HIP). K stays a 256-value multiple: the dense path only takes+// such K, and a partial 256-value chunk would read past the synthetic tensor's end.+std::vector<std::vector<uint8_t>> synthetic_of_type(ggml_type type, int experts, int64_t out_rows, int64_t cols,+ int trial) {+ const auto * tr = ggml_get_type_traits(type);+ if (!tr || !tr->from_float_ref) throw std::runtime_error("no host quantizer for the type");+ std::vector<std::vector<uint8_t>> w;+ for (int e = 0; e < experts; ++e) {+ std::vector<uint8_t> blocks((size_t) out_rows * row_bytes(type, cols));+ std::vector<float> row((size_t) cols);+ for (int64_t r = 0; r < out_rows; ++r) {+ for (int64_t k = 0; k < cols; ++k) {+ const int code = (int) ((k * 29 + r * 17 + e * 31 + trial * 13) % 97) - 48;+ row[(size_t) k] = 0.02f * std::sin(0.011f * (float) (k + 2) + 0.29f * (float) r + 0.37f * (float) e) ++ 0.0009f * (float) code;+ }+ tr->from_float_ref(row.data(), blocks.data() + (size_t) r * row_bytes(type, cols), cols);+ }+ w.push_back(std::move(blocks));+ }+ return w;+}+bool run_real_iq3_first_expert(Context & ctx, hipStream_t stream, const std::string & pack) {std::ifstream meta(pack + "/native_experts.txt");if (!meta) throw std::runtime_error("cannot open native_experts.txt: " + pack);@@ -268,6 +292,22 @@ int main(int argc, char ** argv) {make_activations(rows, 640, trial + 3), src, dst);++trial;}+ // The K-quant instances a STRATA_MMQ_KQUANTS build adds (the dense GGUF projections of the+ // mixed-quant packs through Gemm::native). Skipped when the build does not have them.+ for (const auto & [name, type] : std::initializer_list<std::pair<const char *, ggml_type>>{+ {"Q4_K", GGML_TYPE_Q4_K}, {"Q5_K", GGML_TYPE_Q5_K}, {"Q6_K", GGML_TYPE_Q6_K},+ {"Q5_1", GGML_TYPE_Q5_1}}) {+ if (!supported((int) type)) {+ std::cout << "synthetic-" << name << " skipped: not built (STRATA_MMQ_KQUANTS off?)\n";+ continue;+ }+ const int rows = 5;+ const std::vector<int> counts{rows};+ const auto src = permutation(rows, 3, false), dst = permutation(rows, 2, true);+ run_product(ctx, stream, std::string("synthetic-") + name, type,+ 1280, 2560, synthetic_of_type(type, 1, 1280, 2560, 5), counts,+ make_activations(rows, 2560, 77), src, dst);+ }if (argc == 2) run_real_iq3_first_expert(ctx, stream, argv[1]);}hip_check(hipStreamSynchronize(stream), "final synchronize");