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++
feat: support OrcaRouter Q4_K_S
Suoriks75a39eef3a68
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 4d26b3831..1b3319e3f 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -35,6 +35,7 @@ endif()option(STRATA_ENABLE_CUDA "Build the CUDA targets (Phase 2+)" OFF)option(STRATA_ENABLE_HIP "Build the HIP targets for wave32 AMD GPUs (gfx1100, gfx1201)" OFF)option(STRATA_PREFILL_MMQ "Build the optional GGML MMQ prefill path for HIP" OFF)+option(STRATA_ORCA_Q4KS_MMQ "Build extra GGML MMQ instances 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")endif()@@ -758,6 +759,9 @@ set(STRATA_GGML_DIR "" CACHE PATH "llama.cpp checkout to take ggml from (empty:if(STRATA_PREFILL_MMQ AND NOT STRATA_NATIVE_EXPERTS)message(FATAL_ERROR "STRATA_PREFILL_MMQ requires STRATA_NATIVE_EXPERTS=ON for GGML sources")endif()+if(STRATA_ORCA_Q4KS_MMQ AND NOT STRATA_NATIVE_EXPERTS)+ message(FATAL_ERROR "STRATA_ORCA_Q4KS_MMQ requires STRATA_NATIVE_EXPERTS=ON for GGML sources")+endif()if(STRATA_NATIVE_EXPERTS)enable_language(C)set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)@@ -806,6 +810,10 @@ if(STRATA_NATIVE_EXPERTS)endif()target_link_libraries(strata_kernels_cpu PUBLIC ggml-cpu ggml-base)target_compile_definitions(strata_kernels_cpu PUBLIC STRATA_NATIVE_EXPERTS=1)+ set(_strata_mmq_types q2_0 iq2_xxs iq2_xs iq2_s iq3_xxs iq3_s iq4_nl iq4_xs q8_0)+ if(STRATA_ORCA_Q4KS_MMQ)+ list(APPEND _strata_mmq_types q4_k q5_0 q5_1)+ endif()if(STRATA_ENABLE_CUDA)# prompt-speed plan step 2b: llama.cpp's MMQ kernels (ggml-cuda, MIT) for the prompt path's experts, compiled# from the same checkout as ggml above - only mmq.cuh's instances for the packs' types and the quantizer, with@@ -817,7 +825,7 @@ if(STRATA_NATIVE_EXPERTS)endif()set(_strata_mmq ${_strata_gg}/src/ggml-cuda)add_library(strata_mmq STATIC src/prefill/moe_mmq.cu src/prefill/ggml_cuda_host.cu ${_strata_mmq}/quantize.cu)- foreach(_t q2_0 iq2_xxs iq2_xs iq2_s iq3_xxs iq3_s iq4_nl iq4_xs q8_0) # q8_0: the drafter's matrices (E-9)+ foreach(_t IN LISTS _strata_mmq_types) # q8_0: the drafter's matrices (E-9)target_sources(strata_mmq PRIVATE ${_strata_mmq}/template-instances/mmq-instance-${_t}.cu)endforeach()target_include_directories(strata_mmq PRIVATE ${_strata_gg}/include ${_strata_gg}/src ${_strata_mmq}@@ -827,6 +835,9 @@ if(STRATA_NATIVE_EXPERTS)target_compile_options(strata_mmq PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=/bigobj>)endif()target_link_libraries(strata_mmq PUBLIC ggml-base ${_strata_gpu_runtime_target})+ if(STRATA_ORCA_Q4KS_MMQ)+ target_compile_definitions(strata_mmq PRIVATE STRATA_ORCA_Q4KS_MMQ=1)+ endif()target_link_libraries(strata_prefill PUBLIC strata_mmq)target_compile_definitions(strata_prefill PUBLIC STRATA_PREFILL_MMQ=1)elseif(STRATA_PREFILL_MMQ)@@ -845,7 +856,7 @@ if(STRATA_NATIVE_EXPERTS)src/prefill/moe_mmq.cusrc/prefill/ggml_cuda_host.cu"${_strata_mmq}/quantize.cu")- foreach(_t q2_0 iq2_xxs iq2_xs iq2_s iq3_xxs iq3_s iq4_nl iq4_xs q8_0)+ foreach(_t IN LISTS _strata_mmq_types)list(APPEND _strata_mmq_sources "${_strata_mmq}/template-instances/mmq-instance-${_t}.cu")endforeach()set_source_files_properties(${_strata_mmq_sources} PROPERTIES LANGUAGE HIP)@@ -854,6 +865,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_ORCA_Q4KS_MMQ)+ target_compile_definitions(strata_mmq PRIVATE STRATA_ORCA_Q4KS_MMQ=1)+ endif()target_link_libraries(strata_mmq PUBLIC ggml-base hip::host roc::hipblas)target_link_libraries(strata_prefill PUBLIC strata_mmq)target_compile_definitions(strata_prefill PUBLIC STRATA_PREFILL_MMQ=1)@@ -891,6 +905,13 @@ if(STRATA_NATIVE_EXPERTS)set_tests_properties(native_expert_parity_refuses_q6_K PROPERTIES WILL_FAIL TRUE)endif()endif()+ if(STRATA_ENABLE_CUDA)+ add_executable(ple_q5_parity src/kernels/ple_q5_parity.cpp)+ target_link_libraries(ple_q5_parity PRIVATE strata_kernels strata_artifact ggml-base ${_strata_gpu_runtime_target})+ add_executable(q5_projection_parity src/kernels/q5_projection_parity.cpp)+ target_include_directories(q5_projection_parity PRIVATE ${_strata_gpu_include_directories})+ target_link_libraries(q5_projection_parity PRIVATE strata_kernels strata_artifact ggml-base ${_strata_gpu_runtime_target})+ endif()endif()if(STRATA_ENABLE_CUDA AND STRATA_BUILD_TESTS AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/bench/micro/hit_cpu_order_parity.cu")diff --git a/docs/ORCA_Q4_K_S.md b/docs/ORCA_Q4_K_S.mdnew file mode 100644index 000000000..6a1800e36--- /dev/null+++ b/docs/ORCA_Q4_K_S.md@@ -0,0 +1,36 @@+# OrcaRouter Uncensored Q4_K_S (manual setup)++This path uses the three original `orcarouter/Qwen3.8-Flash-Next-Uncensored-GGUF` Q4_K_S shards without rewriting their expert, embedding or PLE table bytes. It is separate from the installer model menu. Keep all three shard filenames together.++The ordinary GGUF quantizes some small projections which Strata reads as BF16. `--compat-bf16` dequantizes and rounds those projections to BF16 in the separate pack; it does not restore their original precision. The Q5_0 PLE table remains in the source GGUF and currently uses the mapped reader (`--ple-io mmap`). The Q4_K gate/up experts and Q5_0/Q5_1 down experts remain in GGUF form, including layers whose three tensors cross shard boundaries.++## Build and prepare++Build Strata with CUDA, native experts and `STRATA_ORCA_Q4KS_MMQ=ON`. This opt-in compiles the Q4_K, Q5_0 and Q5_1 GGML MMQ instances for CUDA or HIP; the usual Q8_0 draft-layer instance remains in both builds. HIP also needs `STRATA_PREFILL_MMQ=ON`. Select your card's compute capability, for example `89` on an L40S. On Windows with Ninja and CUDA 12.6, use the Visual Studio developer environment and `-DCMAKE_CUDA_RUNTIME_LIBRARY=Shared` to keep the CUDA runtime linkage consistent. The default pinned ggml checkout is supported by the project build.++```sh+cmake -S . -B build -G Ninja -DSTRATA_ENABLE_CUDA=ON -DSTRATA_NATIVE_EXPERTS=ON -DSTRATA_ORCA_Q4KS_MMQ=ON -DCMAKE_CUDA_ARCHITECTURES=89 -DCMAKE_CUDA_RUNTIME_LIBRARY=Shared -DCMAKE_BUILD_TYPE=Release+cmake --build build --target strata native_expert_parity ple_q5_parity+python tools/iq_pack.py --gguf /models/Qwen3.8-Flash-Next-Uncensored-Q4_K_S-00001-of-00003.gguf --out packs/orca-q4_k_s --compat-bf16+```++`iq_pack.py` needs NumPy and the `gguf-py` module from the pinned llama.cpp dependency (`STRATA_GGUF_PY` can point to that directory). It writes a roughly 1.43 GiB dense pack and a native-expert manifest. The original 112 GB of model shards are read in place; `experts.bin` is optional and is not written by this command.++Check the changed kernels against the real files before starting the service:++```sh+build/native_expert_parity /models/Qwen3.8-Flash-Next-Uncensored-Q4_K_S-00001-of-00003.gguf /models/Qwen3.8-Flash-Next-Uncensored-Q4_K_S-00002-of-00003.gguf /models/Qwen3.8-Flash-Next-Uncensored-Q4_K_S-00003-of-00003.gguf 0 5 6 20 47+build/ple_q5_parity /models/Qwen3.8-Flash-Next-Uncensored-Q4_K_S-00001-of-00003.gguf+```++## Engine flags++Pass the same first shard to `--native` and `--ple-gguf`; Strata discovers the other two shards by name. For a first text-only run, use 32K context and a modest cache. Once that passes, increase the context and cache with memory measurements on the target machine.++```sh+--pack packs/orca-q4_k_s --native /models/Qwen3.8-Flash-Next-Uncensored-Q4_K_S-00001-of-00003.gguf --ple-gguf /models/Qwen3.8-Flash-Next-Uncensored-Q4_K_S-00001-of-00003.gguf --ple-io mmap --expert-profile data/expert-profile.bin --expert-cache auto --prefill 512 --spec 4 --mtp /path/to/mtp/rt --max-context 32768 --kv int8+```++For two GPUs in persistent server mode, add `--layer-split auto --split-device 1` (with CUDA0 and CUDA1 visible). The usual Strata MTP runtime is built from the original model's draft layer; draft acceptance on the Uncensored fine-tune must be measured. Keep this model's own tokenizer from the pack. Do not combine `--expert-cache-per-layer` with a variable-size native cache: that policy's slot ordering is independent of the pack's per-layer blob sizes.++This port does not claim that Q4_K_S beats llama.cpp on a given computer. Compare the same model bytes, prompt, context, output length and GPU load; report prefill and decode separately.diff --git a/src/kernels/cuda/dequant_bf16.cu b/src/kernels/cuda/dequant_bf16.cuindex faac2ab4b..32601d606 100644--- a/src/kernels/cuda/dequant_bf16.cu+++ b/src/kernels/cuda/dequant_bf16.cu@@ -67,6 +67,17 @@ __device__ __forceinline__ void group32(const uint8_t* row_blocks, int gi_in_rowput(out, j, (float) (((b[6 + j] & 0x0F) | xh0) - 16) * d);put(out, j + 16, (float) (((b[6 + j] >> 4) | xh1) - 16) * d);}+ } else if constexpr (TYPE == 7) { // Q5_1+ const uint8_t* b = row_blocks + (size_t) gi_in_row * 24;+ const float d = h2f(b), m = h2f(b + 2);+ const uint32_t qh = (uint32_t) b[4] | ((uint32_t) b[5] << 8) |+ ((uint32_t) b[6] << 16) | ((uint32_t) b[7] << 24);+ for (int j = 0; j < 16; ++j) {+ const int xh0 = ((qh >> j) & 1) << 4;+ const int xh1 = ((qh >> (j + 16)) & 1) << 4;+ put(out, j, (float) ((b[8 + j] & 15) | xh0) * d + m);+ put(out, j + 16, (float) ((b[8 + j] >> 4) | xh1) * d + m);+ }} else if constexpr (TYPE == 8) { // Q8_0const uint8_t* b = row_blocks + (size_t) gi_in_row * 34;const float d = h2f(b);@@ -167,6 +178,7 @@ bool geometry(int type, int& block_elems, int& block_bytes) {switch (type) {case 2: block_elems = 32; block_bytes = 18; return true;case 6: block_elems = 32; block_bytes = 22; return true;+ case 7: block_elems = 32; block_bytes = 24; return true;case 8: block_elems = 32; block_bytes = 34; return true;case 20: block_elems = 32; block_bytes = 18; return true;case 11: block_elems = 256; block_bytes = 110; return true;@@ -195,6 +207,7 @@ void launch(int type, const void* blocks, int64_t row0, int64_t rows, int64_t coswitch (type) {case 2: STRATA_DQ(2);case 6: STRATA_DQ(6);+ case 7: STRATA_DQ(7);case 8: STRATA_DQ(8);case 11: STRATA_DQ(11);case 12: STRATA_DQ(12);diff --git a/src/kernels/cuda/native_mmvq.cu b/src/kernels/cuda/native_mmvq.cuindex 219576f79..e0a4c1bdb 100644--- a/src/kernels/cuda/native_mmvq.cu+++ b/src/kernels/cuda/native_mmvq.cu@@ -1439,7 +1439,7 @@ void native_iq4_nl_f32(const void* weights, const float* x, void* scratch_q8_1,}bool native_mmvq_supported(int ggml_type) noexcept {- return ggml_type == 2 || ggml_type == 6 || ggml_type == 8 || ggml_type == 11 ||+ return ggml_type == 2 || ggml_type == 6 || ggml_type == 7 || ggml_type == 8 || ggml_type == 11 ||ggml_type == 12 || ggml_type == 13 || ggml_type == 14 || ggml_type == 20 ||ggml_type == 23 || ggml_type == 42 || ggml_type == 16 || ggml_type == 17 || ggml_type == 18 ||ggml_type == 21 || ggml_type == 22 || ggml_type == 29;@@ -1450,6 +1450,7 @@ std::size_t native_mmvq_weight_bytes(int ggml_type, int n_in, int n_out) {switch (ggml_type) {case 2: block_elems = 32; block_bytes = 18; break;case 6: block_elems = 32; block_bytes = 22; break;+ case 7: block_elems = 32; block_bytes = 24; break;case 8: block_elems = 32; block_bytes = 34; break;case 20: block_elems = 32; block_bytes = 18; break;case 11: block_elems = 256; block_bytes = 110; break;@@ -1476,6 +1477,7 @@ void native_mmvq(int ggml_type, const void* weights, const void* x_q8_1, float*switch (ggml_type) {case 2: native_q4_0_mmvq(weights, x_q8_1, y, n_in, n_out, ncols, stream); break;case 6: native_q5_0_mmvq(weights, x_q8_1, y, n_in, n_out, ncols, stream); break;+ case 7: iq_mmvq(ggml_type, weights, x_q8_1, y, n_in, n_out, ncols, stream); break;case 8: native_q8_0_mmvq(weights, x_q8_1, y, n_in, n_out, ncols, stream); break;case 20: native_iq4_nl_mmvq(weights, x_q8_1, y, n_in, n_out, ncols, stream); break;case 11: native_q3_k_mmvq(weights, x_q8_1, y, n_in, n_out, ncols, stream); break;diff --git a/src/kernels/ngram.cpp b/src/kernels/ngram.cppindex 4831f63d8..bd366c50a 100644--- a/src/kernels/ngram.cpp+++ b/src/kernels/ngram.cpp@@ -3,6 +3,7 @@// See include/strata/kernels/ngram.hpp for the semantics, the rival readings, and the note on MADV_RANDOM.#include "strata/kernels/ngram.hpp"#include "strata/artifact/gguf_reader.hpp"+#include "strata/artifact/dequant.hpp"#include "strata/kernels/f16_bits.hpp"#include "strata/ngram/ple_reader.hpp"@@ -116,6 +117,8 @@ struct PleTable::Impl {GgufFile* file = nullptr;const uint8_t* data = nullptr;uint64_t n_rows = 0;+ uint32_t row_bytes = PLE_ROW_BYTES;+ bool q5_0 = false;mutable uint64_t bytes_read = 0;// Direct mode (plan v0.3 P2): the mapping above is released after the header parse and every row comes// from an unbuffered SSD read into `raw`.@@ -155,11 +158,20 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoOclose();return false;}- if (std::strcmp(t->type_name(), "IQ4_NL") != 0) {- err = std::string("per_layer_token_embd.weight is ") + t->type_name() + ", not IQ4_NL";+ impl_->q5_0 = std::strcmp(t->type_name(), "Q5_0") == 0;+ if (!impl_->q5_0 && std::strcmp(t->type_name(), "IQ4_NL") != 0) {+ err = std::string("per_layer_token_embd.weight is ") + t->type_name() + ", expected IQ4_NL or Q5_0";close();return false;}+ // The direct reader has fixed 90-byte rows. Keep it for IQ4_NL; Q5_0's+ // 110-byte rows use the mapped path until that reader supports variable rows.+ if (impl_->q5_0 && io.mode == PleIo::Direct) {+ err = "Q5_0 PLE requires --ple-io mmap";+ close();+ return false;+ }+ impl_->row_bytes = impl_->q5_0 ? (PLE_HEAD_DIM / 32) * 22 : PLE_ROW_BYTES;impl_->n_rows = t->shape[1];impl_->data = impl_->file->tensor_data(*t);@@ -171,7 +183,7 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoO// assumed. A wrong data offset would leave a different remainder.// A shard may hold other tensors too (Swift 1.5's shard 1 holds layers 0-12 and the table): the table must// then fit inside the file at its own offset; alone in its shard (the original's shard 2) it fills it exactly.- const uint64_t need = impl_->n_rows * (uint64_t) PLE_ROW_BYTES;+ const uint64_t need = impl_->n_rows * (uint64_t) impl_->row_bytes;const uint64_t have = impl_->file->file_size() - impl_->file->data_start();const bool alone = impl_->file->tensors().size() == 1;if (alone ? need != have : t->offset + need > have) {@@ -179,7 +191,7 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoOstd::snprintf(buf, sizeof buf,"PLE table size mismatch: %llu rows x %d B = %llu at offset %llu, but the file holds %llu from ""data_start %llu",- (unsigned long long) impl_->n_rows, PLE_ROW_BYTES, (unsigned long long) need,+ (unsigned long long) impl_->n_rows, (int) impl_->row_bytes, (unsigned long long) need,(unsigned long long) t->offset, (unsigned long long) have,(unsigned long long) impl_->file->data_start());err = buf;@@ -231,6 +243,8 @@ void PleTable::close() {impl_->file = nullptr;impl_->data = nullptr;impl_->n_rows = 0;+ impl_->row_bytes = PLE_ROW_BYTES;+ impl_->q5_0 = false;}bool PleTable::is_open() const { return impl_->data != nullptr || impl_->reader.is_open(); }@@ -256,8 +270,14 @@ void PleTable::read_row(uint32_t row, float* out160) const {std::memset(out160, 0, (size_t) PLE_HEAD_DIM * sizeof(float));return;}- iq4nl_dequant_row(impl_->data + (size_t) row * PLE_ROW_BYTES, out160);- impl_->bytes_read += PLE_ROW_BYTES;+ const uint8_t* raw = impl_->data + (size_t) row * impl_->row_bytes;+ if (impl_->q5_0) {+ for (int b = 0; b < PLE_HEAD_DIM / 32; ++b)+ strata::dequantize_q5_0(raw + (size_t) b * 22, out160 + b * 32);+ } else {+ iq4nl_dequant_row(raw, out160);+ }+ impl_->bytes_read += impl_->row_bytes;}bool PleTable::issue(const uint32_t* rows16) {@@ -274,8 +294,8 @@ bool PleTable::issue(const uint32_t* rows16) {ULONG_PTR n = 0;for (int h = 0; h < PLE_N_HEADS; ++h) {if (rows16[h] >= impl_->n_rows) continue;- ranges[n].VirtualAddress = (PVOID) (impl_->data + (size_t) rows16[h] * PLE_ROW_BYTES);- ranges[n].NumberOfBytes = PLE_ROW_BYTES;+ ranges[n].VirtualAddress = (PVOID) (impl_->data + (size_t) rows16[h] * impl_->row_bytes);+ ranges[n].NumberOfBytes = impl_->row_bytes;++n;}if (n > 0) (void) PrefetchVirtualMemory(GetCurrentProcess(), n, ranges, 0);@@ -364,8 +384,8 @@ void PleTable::gather(const uint32_t* rows16, float* out2560) const {for (int h = 0; h < PLE_N_HEADS; ++h) {// Out-of-range rows are handled by `read_row` as zeros and have no address to prefetch.if (rows16[h] >= impl_->n_rows) continue;- ranges[n].VirtualAddress = (PVOID) (impl_->data + (size_t) rows16[h] * PLE_ROW_BYTES);- ranges[n].NumberOfBytes = PLE_ROW_BYTES;+ ranges[n].VirtualAddress = (PVOID) (impl_->data + (size_t) rows16[h] * impl_->row_bytes);+ ranges[n].NumberOfBytes = impl_->row_bytes;++n;}if (n > 0) (void) PrefetchVirtualMemory(GetCurrentProcess(), n, ranges, 0);diff --git a/src/kernels/ple_q5_parity.cpp b/src/kernels/ple_q5_parity.cppnew file mode 100644index 000000000..cd387a8e4--- /dev/null+++ b/src/kernels/ple_q5_parity.cpp@@ -0,0 +1,59 @@+// Real-artifact Q5_0 PLE rows against ggml's reference dequantizer.+#define NOMINMAX+#include "strata/artifact/gguf_reader.hpp"+#include "strata/kernels/ngram.hpp"+#include "ggml.h"++#include <algorithm>+#include <cmath>+#include <cstdio>+#include <string>+#include <vector>++int main(int argc, char** argv) {+ if (argc != 2) {+ std::fprintf(stderr, "usage: ple_q5_parity <gguf-containing-ple>\n");+ return 2;+ }+ strata::GgufFile gguf(argv[1]);+ const auto* tensor = gguf.find("per_layer_token_embd.weight");+ if (!tensor || tensor->type != 6 || tensor->shape.size() != 2 || tensor->shape[0] != 160) {+ std::fprintf(stderr, "expected Q5_0 PLE [160, N]\n");+ return 2;+ }+ strata::kernels::PleIoOptions options;+ options.mode = strata::kernels::PleIo::Mmap;+ strata::kernels::PleTable table;+ std::string err;+ if (!table.open(argv[1], err, options)) {+ std::fprintf(stderr, "%s\n", err.c_str());+ return 1;+ }+ const auto* traits = ggml_get_type_traits(GGML_TYPE_Q5_0);+ const uint8_t* bytes = gguf.tensor_data(*tensor);+ const uint32_t probes[] = {0, 1, 12345, 20000003, (uint32_t) (table.rows() - 1)};+ double max_abs = 0.0;+ for (uint32_t row : probes) {+ float got[160], want[160];+ table.read_row(row, got);+ traits->to_float(bytes + (size_t) row * 110, want, 160);+ for (int i = 0; i < 160; ++i)+ max_abs = std::max(max_abs, (double) std::fabs(got[i] - want[i]));+ }+ uint32_t rows[16];+ for (int i = 0; i < 16; ++i) rows[i] = probes[i % 5];+ float batch[16 * 160];+ if (!table.issue(rows) || !table.collect(batch, err)) {+ std::fprintf(stderr, "collect: %s\n", err.c_str());+ return 1;+ }+ for (int h = 0; h < 16; ++h) {+ float want[160];+ traits->to_float(bytes + (size_t) rows[h] * 110, want, 160);+ for (int i = 0; i < 160; ++i)+ max_abs = std::max(max_abs, (double) std::fabs(batch[h * 160 + i] - want[i]));+ }+ std::printf("Q5_0 PLE: %zu rows, max_abs %.3e %s\n", (size_t) table.rows(), max_abs,+ max_abs <= 1e-6 ? "PASS" : "FAIL");+ return max_abs <= 1e-6 ? 0 : 1;+}diff --git a/src/kernels/q5_projection_parity.cpp b/src/kernels/q5_projection_parity.cppnew file mode 100644index 000000000..20faf9954--- /dev/null+++ b/src/kernels/q5_projection_parity.cpp@@ -0,0 +1,62 @@+// Q5_1 shared-expert projection: Strata CUDA dequantization vs ggml.+#define NOMINMAX+#include "strata/artifact/gguf_reader.hpp"+#include "strata/kernels/dequant_bf16.hpp"+#include "ggml.h"++#include <cuda_runtime.h>+#include <algorithm>+#include <cmath>+#include <cstdio>+#include <cstdint>+#include <cstring>+#include <vector>++int main(int argc, char** argv) {+ if (argc != 2) {+ std::fprintf(stderr, "usage: q5_projection_parity <shard1.gguf>\n");+ return 2;+ }+ strata::GgufFile gguf(argv[1]);+ const auto* t = gguf.find("blk.0.ffn_down_shexp.weight");+ if (!t || t->type != 7 || t->shape.size() != 2) {+ std::fprintf(stderr, "expected blk.0.ffn_down_shexp.weight Q5_1\n");+ return 2;+ }+ const int64_t cols = (int64_t) t->shape[0], rows = std::min<int64_t>(4, (int64_t) t->shape[1]);+ const size_t weight_bytes = (size_t) rows * (size_t) (cols / 32) * 24;+ const size_t elements = (size_t) rows * (size_t) cols;+ const uint8_t* source = gguf.tensor_data(*t);+ void *dweight = nullptr, *dfloat = nullptr, *dbf16 = nullptr;+ if (cudaMalloc(&dweight, weight_bytes) != cudaSuccess ||+ cudaMalloc(&dfloat, elements * sizeof(float)) != cudaSuccess ||+ cudaMalloc(&dbf16, elements * sizeof(uint16_t)) != cudaSuccess) {+ std::fprintf(stderr, "CUDA allocation failed\n");+ return 1;+ }+ cudaMemcpy(dweight, source, weight_bytes, cudaMemcpyHostToDevice);+ strata::kernels::dequant_f32(7, dweight, 0, rows, cols, (float*) dfloat, nullptr);+ strata::kernels::dequant_bf16(7, dweight, 0, rows, cols, (uint16_t*) dbf16, nullptr);+ std::vector<float> got(elements), ref(elements);+ std::vector<uint16_t> got_bf16(elements);+ cudaMemcpy(got.data(), dfloat, elements * sizeof(float), cudaMemcpyDeviceToHost);+ cudaMemcpy(got_bf16.data(), dbf16, elements * sizeof(uint16_t), cudaMemcpyDeviceToHost);+ cudaFree(dweight); cudaFree(dfloat); cudaFree(dbf16);+ const auto* traits = ggml_get_type_traits(GGML_TYPE_Q5_1);+ for (int64_t r = 0; r < rows; ++r)+ traits->to_float(source + (size_t) r * (size_t) (cols / 32) * 24,+ ref.data() + (size_t) r * (size_t) cols, cols);+ double max_abs = 0;+ size_t bf16_bad = 0;+ for (size_t i = 0; i < elements; ++i) {+ max_abs = std::max(max_abs, (double) std::fabs(got[i] - ref[i]));+ uint32_t bits;+ std::memcpy(&bits, &ref[i], sizeof(bits));+ const uint16_t expected = (uint16_t) ((bits + 0x7fffu + ((bits >> 16) & 1u)) >> 16);+ if (got_bf16[i] != expected) ++bf16_bad;+ }+ const bool ok = max_abs <= 1e-6 && bf16_bad == 0;+ std::printf("Q5_1 projection: %zu elements, max_abs %.3e, bf16 differences %zu %s\n",+ elements, max_abs, bf16_bad, ok ? "PASS" : "FAIL");+ return ok ? 0 : 1;+}diff --git a/src/prefill/moe_mmq.cu b/src/prefill/moe_mmq.cuindex ac2daee4f..35aa53548 100644--- a/src/prefill/moe_mmq.cu+++ b/src/prefill/moe_mmq.cu@@ -91,7 +91,11 @@ bool built() { return true; }bool supported(int t) {switch ((ggml_type) t) {- case GGML_TYPE_Q2_0: case GGML_TYPE_IQ2_XXS: case GGML_TYPE_IQ2_XS: case GGML_TYPE_IQ2_S:+ case GGML_TYPE_Q2_0:+#ifdef STRATA_ORCA_Q4KS_MMQ+ case GGML_TYPE_Q4_K: case GGML_TYPE_Q5_0: case GGML_TYPE_Q5_1:+#endif+ case GGML_TYPE_IQ2_XXS: case GGML_TYPE_IQ2_XS: case GGML_TYPE_IQ2_S: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)return true;@@ -134,6 +138,11 @@ void Context::run(const Product& p, void* stream) {auto& ctx = *(ggml_backend_cuda_context*) ctx_;const cudaStream_t s = (cudaStream_t) stream;switch (t) {+#ifdef STRATA_ORCA_Q4KS_MMQ+ case GGML_TYPE_Q4_K: mul_mat_q_case<GGML_TYPE_Q4_K>(ctx, a, s); break;+ case GGML_TYPE_Q5_0: mul_mat_q_case<GGML_TYPE_Q5_0>(ctx, a, s); break;+ case GGML_TYPE_Q5_1: mul_mat_q_case<GGML_TYPE_Q5_1>(ctx, a, s); break;+#endifcase GGML_TYPE_Q2_0: mul_mat_q_case<GGML_TYPE_Q2_0>(ctx, a, s); break;case GGML_TYPE_IQ2_XXS: mul_mat_q_case<GGML_TYPE_IQ2_XXS>(ctx, a, s); break;case GGML_TYPE_IQ2_XS: mul_mat_q_case<GGML_TYPE_IQ2_XS>(ctx, a, s); break;