okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bc09a17e5
main
1# cmake/backends/quant.cmake - the Strata quant layer (own-stack phase 2): strata_quant, its self-test and the ggml-cpu oracle test. 2# 3# strata_quant has no ggml dependency (the block layouts and codebooks come from third_party/strata-ggml/strata-quant-common.h, a plain header). 4# Only quant_parity links ggml-cpu, as the oracle. Called from CMakeLists.txt after the ggml subproject is configured, and (OWN-2N) from metal/CMakeLists.txt 5# behind STRATA_OWN_CPU_QUANT, which sets STRATA_QUANT_ROOT to the repository root first (the root project's own call leaves it unset: its source dir is the root). 6macro ( strata_quant_layer ) 7if ( NOT STRATA_QUANT_ROOT) 8set ( STRATA_QUANT_ROOT ${ CMAKE_CURRENT_SOURCE_DIR }) 9endif () 10set ( _sq ${ STRATA_QUANT_ROOT }) 11set ( _strata_quant_srcs ${ _sq } /src/quant/types.cpp ${ _sq } /src/quant/isa.cpp ${ _sq } /src/quant/dispatch.cpp ${ _sq } /src/quant/selfcheck.cpp 12${ _sq } /src/quant/act_ref.cpp ${ _sq } /src/quant/dot_generic.cpp ${ _sq } /src/quant/dequant.cpp ) 13add_library ( strata_quant STATIC ${ _strata_quant_srcs }) 14target_include_directories ( strata_quant PUBLIC ${ _sq } /include PRIVATE ${ _sq } /src/quant ${ _sq } /third_party/strata-ggml ) 15if ( NOT MSVC) 16# the Generic contract is ggml's _generic C code, summed in order: no contraction into FMA whatever the target's baseline 17set_source_files_properties ( $ { _sq } /src/quant/dot_generic.cpp ${ _sq } /src/quant/act_ref.cpp PROPERTIES COMPILE_OPTIONS "-ffp-contract=off" ) 18endif () 19if ( STRATA_CPU_X86) 20# the x86 tiers: ggml's own x86 code ported, each file compiled with its flavour's instruction-set flags so the #if __AVX2__ / __AVX__ branches 21# select what ggml-cpu selects for that flavour (the release builds are AVX2; STRATA_ISA_FLOOR=avx is the AVX-only one) 22target_sources ( strata_quant PRIVATE ${ _sq } /src/quant/x86/dot_avx2.cpp ${ _sq } /src/quant/x86/act_avx2.cpp ${ _sq } /src/quant/x86/dot_avx.cpp 23${ _sq } /src/quant/x86/act_avx.cpp ${ _sq } /src/quant/x86/dot_avx512.cpp ) 24target_compile_definitions ( strata_quant PRIVATE STRATA_QUANT_X86_TIERS=1 ) 25if ( MSVC) 26# MSVC contracts neither ggml-cpu's C nor these files' intrinsics (measured: 0 differences from its own ggml in all three flavours); the Windows bits 27# differ from GCC's in the Q5_K and IQ1_S dots, see below 28set_source_files_properties ( $ { _sq } /src/quant/x86/dot_avx2.cpp ${ _sq } /src/quant/x86/act_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2" ) 29set_source_files_properties ( $ { _sq } /src/quant/x86/dot_avx.cpp ${ _sq } /src/quant/x86/act_avx.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX" ) 30set_source_files_properties ( $ { _sq } /src/quant/x86/dot_avx512.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX512" ) 31else () 32# What is pinned, and against what: the reference is ggml-cpu as GCC 13.3 builds it (C, gnu11, where GCC contracts a multiply and an add into an 33# FMA wherever it can, the intrinsics' `_mm256_add_ps(_mm256_mul_ps(a, b), c)` patterns included). -ffp-contract=fast keeps this C++ (-std=c++20, 34# an ISO mode that contracts less) summing as that ggml does; the one place it does NOT fuse, the IQ4_NL scalar tail, is pinned unfused in 35# x86_dots.inc (STRATA_NOFUSE), and dot_generic.cpp (Q2_0 under every contract) is -ffp-contract=off above. 36# Measured 11-10 on one Broadwell box, all three flavours: our results are the same bytes under GCC 13.3, 14.3 and 15.2; ggml-cpu's are not 37# (GCC 14.3 also fuses the IQ4_NL tail: 55 rows of the oracle; GCC 15.2 that and the generic Q2_0 dot: 238 rows; 1-4 ulp). So the oracle's gate 38# (tests/quant/quant_parity.cpp, leg 1) compares with the GCC 13.3 reference stored in tests/quant/golden_x86.txt, under any compiler; the ggml 39# built here is compared for information. MSVC (branch above, no pin): ours equals MSVC's ggml, which does not contract the intrinsics, so 40# Q5_K and IQ1_S dots differ from GCC's by 1-512 ulp (the golden file's msvc lines). 41set ( STRATA_QUANT_AVX2_EXTRA "" CACHE STRING "extra compile options for the AVX2 tier files (experiments), semicolon separated" ) 42set_source_files_properties ( $ { _sq } /src/quant/x86/dot_avx2.cpp ${ _sq } /src/quant/x86/act_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c;-ffp-contract=fast; ${ STRATA_QUANT_AVX2_EXTRA } " ) 43# AVX2, FMA and F16C explicitly off: a -march=native build host must not leak wider code into the AVX-only tier 44set_source_files_properties ( $ { _sq } /src/quant/x86/dot_avx.cpp ${ _sq } /src/quant/x86/act_avx.cpp PROPERTIES COMPILE_OPTIONS "-mavx;-mno-avx2;-mno-fma;-mno-f16c;-ffp-contract=fast" ) 45# the 512-bit kernels of the Avx2 contract: F/DQ/BW/VL + VNNI (what Isa::Avx512 means), called only behind caps() 46set_source_files_properties ( $ { _sq } /src/quant/x86/dot_avx512.cpp PROPERTIES COMPILE_OPTIONS 47"-mavx512f;-mavx512bw;-mavx512vl;-mavx512dq;-mavx512vnni;-mavx2;-mfma;-mf16c;-ffp-contract=fast" ) 48endif () 49endif () 50if ( NOT STRATA_CPU_X86AND CMAKE_SYSTEM_PROCESSORMATCHES "^(aarch64|arm64|ARM64) $ " ) 51# the Arm tier (OWN-2N): ggml's NEON dots and Q8_0 / Q8_1 quantizers ported, compiled as ggml-cpu's arm64 flavour (armv8.2-a + dotprod + fp16 + i8mm, no SVE; 52# the Spark's STRATA_PORTABLE build and the Mac's native build have those features and no SVE). The i8mm code of ggml's dots is the two-row form, which the 53# tier does not call; it is compiled out by constant propagation. GCC contracts a multiply and an add into an FMA wherever it can in ggml's gnu11 C 54# (-ffp-contract=fast); Clang's default (contract within one expression) is what ggml-cpu gets there, so only GCC is given the flag. 55# The same goes for ggml-base's scalar references, which the pool's Q8_K quantizer (ggml's Arm Q8_K is `quantize_row_q8_K_ref`), the Q4_0 requantizer and 56# the dequantizers are: on arm64 ggml-base is built with FMA and GCC fuses `iscale * x + 2^23 + 2^22` (nearest_int) and `x * id + 8.5` there, which it does not 57# on x86 (no FMA in the baseline). So on arm64 these two files are compiled with ggml-base's contraction, not the Generic contract's "off". 58set ( STRATA_QUANT_REF_CONTRACT "" CACHE STRING "arm64: -ffp-contract mode of act_ref.cpp and dequant.cpp (off|on|fast; empty: ggml-base's, fast under GCC and on under Clang)" ) 59if ( NOT " ${ STRATA_QUANT_REF_CONTRACT } " STREQUAL "" ) 60set ( _sq_ref_contract ${ STRATA_QUANT_REF_CONTRACT }) 61elseif ( CMAKE_CXX_COMPILER_IDSTREQUAL "GNU" ) 62set ( _sq_ref_contract fast ) 63else () 64set ( _sq_ref_contract on ) 65endif () 66set_source_files_properties ( $ { _sq } /src/quant/act_ref.cpp ${ _sq } /src/quant/dequant.cpp PROPERTIES COMPILE_OPTIONS "-ffp-contract= ${ _sq_ref_contract } " ) 67target_sources ( strata_quant PRIVATE ${ _sq } /src/quant/arm/dot_neon.cpp ${ _sq } /src/quant/arm/act_neon.cpp ) 68target_compile_definitions ( strata_quant PRIVATE STRATA_QUANT_ARM_TIERS=1 ) 69set ( STRATA_QUANT_NEON_EXTRA "" CACHE STRING "extra compile options for the Arm tier files (experiments), semicolon separated" ) 70if ( CMAKE_CXX_COMPILER_IDSTREQUAL "GNU" ) 71set_source_files_properties ( $ { _sq } /src/quant/arm/dot_neon.cpp ${ _sq } /src/quant/arm/act_neon.cpp PROPERTIES COMPILE_OPTIONS 72"-march=armv8.2-a+dotprod+fp16+i8mm;-ffp-contract=fast; ${ STRATA_QUANT_NEON_EXTRA } " ) 73else () 74set_source_files_properties ( $ { _sq } /src/quant/arm/dot_neon.cpp ${ _sq } /src/quant/arm/act_neon.cpp PROPERTIES COMPILE_OPTIONS 75"-march=armv8.2-a+dotprod+fp16+i8mm; ${ STRATA_QUANT_NEON_EXTRA } " ) 76endif () 77endif () 78add_executable ( quant_bench ${ _sq } /tests/quant/quant_bench.cpp ) # one-thread GB/s of every kernel (no ggml); not a test 79target_link_libraries ( quant_bench PRIVATE strata_quant ) 80if ( STRATA_BUILD_TESTS) 81add_executable ( quant_selftest ${ _sq } /tests/quant/quant_selftest.cpp ) 82target_link_libraries ( quant_selftest PRIVATE strata_quant ) 83add_test ( NAME quant_selftest COMMAND quant_selftest ) 84# the CPU model of the fused prompt kernels' NVFP4 weight decode against the layer's dequantizer (no GPU; tests/cuda/prefill_decode_golden.cu is the GPU side) 85add_executable ( nvfp4_decode_model_test ${ _sq } /tests/quant/nvfp4_decode_model_test.cpp ) 86target_link_libraries ( nvfp4_decode_model_test PRIVATE strata_quant ) 87add_test ( NAME nvfp4_decode_model_test COMMAND nvfp4_decode_model_test ) 88if ( NOT STRATA_QUANT_NO_GGML) # (the CPU check without ggml builds the tests above only; everything below links ggml) 89# 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) 90add_executable ( type_geometry_oracle ${ _sq } /tests/quant/type_geometry_oracle.cpp ) 91target_link_libraries ( type_geometry_oracle PRIVATE strata_quant ggml-base ) 92add_test ( NAME type_geometry_oracle COMMAND type_geometry_oracle ) 93# the oracle: ggml-cpu's own dots and quantizers (the flavour of the ggml-cpu this build links decides the contract checked) 94add_executable ( quant_parity ${ _sq } /tests/quant/quant_parity.cpp ) 95target_link_libraries ( quant_parity PRIVATE strata_quant ggml-cpu ggml-base ) 96target_include_directories ( quant_parity PRIVATE ${ _sq } /src/quant ${ _sq } /third_party/strata-ggml ) 97# leg 1 of the oracle: the golden reference digests (tests/quant/quant_parity.cpp says how they are made and checked) 98target_compile_definitions ( quant_parity PRIVATE STRATA_QUANT_GOLDEN_FILE= " ${ _sq } /tests/quant/golden_x86.txt" ) 99add_test ( NAME quant_parity COMMAND quant_parity ) 100set_tests_properties ( quant_parity PROPERTIES TIMEOUT 600 ) 101if ( STRATA_CPU_X86AND EXISTS ${ _sq } /tests/quant/golden_x86.txt) 102# the golden lines regenerate byte for byte from this build's ggml-cpu: run on GCC 13.x (family gcc) and MSVC 19.4x (family msvc), skipped (77) elsewhere 103add_test ( NAME quant_golden_regen COMMAND quant_parity --regen-check ) 104set_tests_properties ( quant_golden_regen PROPERTIES TIMEOUT 600 SKIP_RETURN_CODE 77 ) 105endif () 106# the speed of the layer's rows against ggml-cpu's (interleaved rounds); not a test 107add_executable ( quant_bench_ggml ${ _sq } /tests/quant/quant_bench_ggml.cpp ) 108target_link_libraries ( quant_bench_ggml PRIVATE strata_quant ggml-cpu ggml-base ) 109endif () 110endif () 111endmacro ()