From f1d2467b5b7d56a6e9211eaa8b2add51262bedbd Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 14 Nov 2026 01:17:31 +0100 Subject: [PATCH] prefill: size expert matrices from Strata's own type table The prompt helpers (src/prefill/moe_helpers.cu, mmq::matrix_bytes) asked ggml for the block size and the block bytes of a type id. Those two calls were the only references from the engine's own objects into ggml-base in a build without MMQ, and they made the linker take ggml.c and five more ggml-base members into the engine. matrix_bytes now reads strata::quant::type_info (src/quant/types.cpp), the table the CPU expert pool already uses: the GGUF ids with ggml's sizes, and the trellis types 144-149 that moe_helpers.cu had as constants. strata_pf_own links strata_quant. Same numbers: tests/quant/type_geometry_oracle.cpp compares the table with ggml's own, id by id (built with STRATA_BUILD_TESTS, ggml-base as the oracle): 28 ids equal, 0 differ. Ids only ggml knows (I8, I16, I32, I64, F64, TQ1_0, TQ2_0) and unknown ids now give 0 bytes instead of ggml's value or an out-of-range read. The GGUF reader knows only I8 of those (the FP8 PLE table), and no prompt path takes a matrix of any of them. The build with MMQ keeps its own ggml calls in moe_mmq.cu. --- cmake/backends/cuda.cmake | 4 +-- cmake/backends/hip.cmake | 4 +-- cmake/backends/quant.cmake | 4 +++ src/prefill/moe_helpers.cu | 18 ++++------ tests/quant/type_geometry_oracle.cpp | 53 ++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 tests/quant/type_geometry_oracle.cpp diff --git a/cmake/backends/cuda.cmake b/cmake/backends/cuda.cmake index a2978b07f6..311fe61323 100644 --- a/cmake/backends/cuda.cmake +++ b/cmake/backends/cuda.cmake @@ -264,7 +264,7 @@ macro(strata_cuda_mmq) target_compile_definitions(strata_mmq PRIVATE STRATA_MMQ_TQP=1) endif() strata_pf_own_mirror() - target_link_libraries(strata_pf_own PUBLIC ggml-base ${_strata_gpu_runtime_target}) + target_link_libraries(strata_pf_own PUBLIC strata_quant ggml-base ${_strata_gpu_runtime_target}) if(TARGET CUDA::cuda_driver) target_link_libraries(strata_pf_own PUBLIC CUDA::cuda_driver) endif() @@ -289,7 +289,7 @@ macro(strata_cuda_pf_own) if(MSVC) target_compile_options(strata_pf_own PRIVATE $<$:-Xcompiler=/bigobj>) endif() - target_link_libraries(strata_pf_own PUBLIC ggml-base ${_strata_gpu_runtime_target}) + target_link_libraries(strata_pf_own PUBLIC strata_quant ggml-base ${_strata_gpu_runtime_target}) if(TARGET CUDA::cuda_driver) target_link_libraries(strata_pf_own PUBLIC CUDA::cuda_driver) endif() diff --git a/cmake/backends/hip.cmake b/cmake/backends/hip.cmake index 6744cdeca1..3798b53445 100644 --- a/cmake/backends/hip.cmake +++ b/cmake/backends/hip.cmake @@ -91,7 +91,7 @@ macro(strata_hip_mmq) endif() target_link_libraries(strata_mmq PUBLIC ggml-base hip::host roc::hipblas) strata_pf_own_mirror() - target_link_libraries(strata_pf_own PUBLIC ggml-base hip::host roc::hipblas) + target_link_libraries(strata_pf_own PUBLIC strata_quant ggml-base hip::host roc::hipblas) target_link_libraries(strata_mmq PUBLIC strata_pf_own) target_link_libraries(strata_prefill PUBLIC strata_mmq) target_compile_definitions(strata_prefill PUBLIC STRATA_PREFILL_MMQ=1 STRATA_PREFILL_FUSED=1) @@ -112,7 +112,7 @@ macro(strata_hip_pf_own) add_library(strata_pf_own STATIC ${_strata_pf_own_sources} src/prefill/mmq_absent.cpp) target_include_directories(strata_pf_own PRIVATE "${_strata_gg}/include" "${_strata_gg}/src" PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) target_compile_definitions(strata_pf_own PRIVATE GGML_USE_HIP=1) - target_link_libraries(strata_pf_own PUBLIC ggml-base hip::host roc::hipblas) + target_link_libraries(strata_pf_own PUBLIC strata_quant ggml-base hip::host roc::hipblas) target_link_libraries(strata_prefill PUBLIC strata_pf_own) target_compile_definitions(strata_prefill PUBLIC STRATA_PREFILL_FUSED=1) set(_strata_pf_tests_lib strata_pf_own) diff --git a/cmake/backends/quant.cmake b/cmake/backends/quant.cmake index d6e3dbb886..987c549e91 100644 --- a/cmake/backends/quant.cmake +++ b/cmake/backends/quant.cmake @@ -81,6 +81,10 @@ macro(strata_quant_layer) add_executable(quant_selftest ${_sq}/tests/quant/quant_selftest.cpp) target_link_libraries(quant_selftest PRIVATE strata_quant) add_test(NAME quant_selftest COMMAND quant_selftest) + # the table's block geometry against ggml's own table, id by id (ggml-base as the oracle; the engine sizes its prompt expert buffers from the table) + add_executable(type_geometry_oracle ${_sq}/tests/quant/type_geometry_oracle.cpp) + target_link_libraries(type_geometry_oracle PRIVATE strata_quant ggml-base) + add_test(NAME type_geometry_oracle COMMAND type_geometry_oracle) # the oracle: ggml-cpu's own dots and quantizers (the flavour of the ggml-cpu this build links decides the contract checked) add_executable(quant_parity ${_sq}/tests/quant/quant_parity.cpp) target_link_libraries(quant_parity PRIVATE strata_quant ggml-cpu ggml-base) diff --git a/src/prefill/moe_helpers.cu b/src/prefill/moe_helpers.cu index 7fd9471f2d..2931717ed9 100644 --- a/src/prefill/moe_helpers.cu +++ b/src/prefill/moe_helpers.cu @@ -5,7 +5,7 @@ #include "strata/prefill/moe_fused.hpp" // kMoeEmbd, kMoeFF: the compiled geometry's widths -#include "ggml.h" // ggml_blck_size, ggml_type_size (ggml-base, the format definitions) +#include "strata/quant/types.hpp" // the block geometry of a type id (Strata's own type table, no ggml) #include @@ -15,15 +15,6 @@ namespace strata::prefill::mmq { namespace { -// (the same as moe_mmq.cu's) the Gyro trellis types (ids 144-146, 147-149) are outside the type table of an upstream ggml: their block -// geometry is known here (128 weights; 34 / 26 / 30 / 42 / 50 / 58 bytes), everything else asks ggml -inline bool is_tq(int t) { return t >= 144 && t <= 149; } -inline int64_t type_blck(int t) { return is_tq(t) ? 128 : (int64_t) ggml_blck_size((ggml_type) t); } -inline size_t type_bytes(int t) { - return t == 144 ? 34 : t == 145 ? 26 : t == 146 ? 30 : t == 147 ? 42 : t == 148 ? 50 : t == 149 ? 58 - : ggml_type_size((ggml_type) t); -} - void ck(cudaError_t e, const char* what) { if (e != cudaSuccess) { std::fprintf(stderr, "prefill mmq: %s: %s\n", what, cudaGetErrorString(e)); @@ -158,8 +149,13 @@ unsigned blocks(int64_t n) { return (unsigned) ((n + 255) / 256); } } // namespace +// The block geometry comes from Strata's type table (src/quant/types.cpp): the GGUF format's ids with ggml's sizes, the trellis types +// 144-149 included (128 weights; 34 / 26 / 30 / 42 / 50 / 58 bytes); tests/quant/type_geometry_oracle.cpp checks it against ggml's +// own table. An id the table does not know has no size here (0): no prompt path reads such a matrix. size_t matrix_bytes(int t, int64_t rows, int64_t cols) { - return (size_t) rows * (size_t) (cols / type_blck((int) t)) * type_bytes((int) t); + const ::strata::quant::TypeInfo* ti = ::strata::quant::type_info(t); + if (ti == nullptr) return 0; + return (size_t) rows * (size_t) (cols / ti->blck) * ti->bytes; } size_t q8_bytes(int64_t rows, int64_t cols) { diff --git a/tests/quant/type_geometry_oracle.cpp b/tests/quant/type_geometry_oracle.cpp new file mode 100644 index 0000000000..571083adf7 --- /dev/null +++ b/tests/quant/type_geometry_oracle.cpp @@ -0,0 +1,53 @@ +// tests/quant/type_geometry_oracle.cpp - Strata's type table (strata::quant::type_info) against ggml's own table, id by id. +// +// The engine's prompt buffers size an expert matrix from this table (src/prefill/moe_helpers.cu, mmq::matrix_bytes), which used to ask +// ggml (ggml_blck_size / ggml_type_size). ggml is the oracle here only: the test is built where ggml-base is (STRATA_BUILD_TESTS). +#include + +#include "ggml.h" +#include "strata/quant/types.hpp" + +namespace sq = strata::quant; + +static int g_fail = 0; +#define CHECK(c, ...) \ + do { \ + if (!(c)) { \ + ++g_fail; \ + std::printf("FAIL line %d: ", __LINE__); \ + std::printf(__VA_ARGS__); \ + std::printf("\n"); \ + } \ + } while (0) + +int main() { + int same = 0, ggml_only = 0; + // every id of ggml's table: where Strata's table knows the id, the block geometry is ggml's + for (int t = 0; t < GGML_TYPE_COUNT; ++t) { + const int64_t gb = ggml_blck_size((ggml_type) t); + const size_t gs = ggml_type_size((ggml_type) t); + const sq::TypeInfo* ti = sq::type_info(t); + if (ti == nullptr) { + if (gb > 0) { + ++ggml_only; + std::printf(" id %d (%s): in ggml's table only (%lld values, %zu bytes)\n", t, ggml_type_name((ggml_type) t), (long long) gb, gs); + } + continue; + } + CHECK(gb == ti->blck && gs == ti->bytes, "id %d (%s): Strata %d values / %zu bytes, ggml %lld / %zu", t, ti->name, ti->blck, ti->bytes, + (long long) gb, gs); + ++same; + } + // the trellis types, which ggml's table does not have: the geometry moe_helpers.cu had as constants + const int tq_ids[] = {144, 145, 146, 147, 148, 149}; + const size_t tq_bytes[] = {34, 26, 30, 42, 50, 58}; + for (int i = 0; i < 6; ++i) { + const sq::TypeInfo* ti = sq::type_info(tq_ids[i]); + CHECK(ti != nullptr && ti->blck == 128 && ti->bytes == tq_bytes[i], "trellis id %d: 128 values / %zu bytes", tq_ids[i], tq_bytes[i]); + } + // ids in neither table are unknown, never read out of range + const int unknown[] = {-1, 4, 5, (int) GGML_TYPE_COUNT, 143, 150, 1000}; + for (int t : unknown) CHECK(sq::type_info(t) == nullptr, "id %d unknown", t); + std::printf("type_geometry_oracle: %d ids equal to ggml's, %d in ggml's table only, %d failures\n", same, ggml_only, g_fail); + return g_fail == 0 ? 0 : 1; +}