okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-pkg-nommq f1d2467b5 into the batch 19 candidate (packaging: strata_kq only where the tree has it, the Windows NVIDIA recipe; prefill sizes expert matrices from Strata's own type table)bc09a17e5

main
10.1 KiB111 linesraw
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)
7  if(NOT STRATA_QUANT_ROOT)
8    set(STRATA_QUANT_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
9  endif()
10  set(_sq ${STRATA_QUANT_ROOT})
11  set(_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)
13  add_library(strata_quant STATIC ${_strata_quant_srcs})
14  target_include_directories(strata_quant PUBLIC ${_sq}/include PRIVATE ${_sq}/src/quant ${_sq}/third_party/strata-ggml)
15  if(NOT MSVC)
16    # the Generic contract is ggml's _generic C code, summed in order: no contraction into FMA whatever the target's baseline
17    set_source_files_properties(${_sq}/src/quant/dot_generic.cpp ${_sq}/src/quant/act_ref.cpp PROPERTIES COMPILE_OPTIONS "-ffp-contract=off")
18  endif()
19  if(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)
22    target_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)
24    target_compile_definitions(strata_quant PRIVATE STRATA_QUANT_X86_TIERS=1)
25    if(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
28      set_source_files_properties(${_sq}/src/quant/x86/dot_avx2.cpp ${_sq}/src/quant/x86/act_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
29      set_source_files_properties(${_sq}/src/quant/x86/dot_avx.cpp ${_sq}/src/quant/x86/act_avx.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX")
30      set_source_files_properties(${_sq}/src/quant/x86/dot_avx512.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX512")
31    else()
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).
41      set(STRATA_QUANT_AVX2_EXTRA "" CACHE STRING "extra compile options for the AVX2 tier files (experiments), semicolon separated")
42      set_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
44      set_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()
46      set_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")
48    endif()
49  endif()
50  if(NOT STRATA_CPU_X86 AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(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".
58    set(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)")
59    if(NOT "${STRATA_QUANT_REF_CONTRACT}" STREQUAL "")
60      set(_sq_ref_contract ${STRATA_QUANT_REF_CONTRACT})
61    elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
62      set(_sq_ref_contract fast)
63    else()
64      set(_sq_ref_contract on)
65    endif()
66    set_source_files_properties(${_sq}/src/quant/act_ref.cpp ${_sq}/src/quant/dequant.cpp PROPERTIES COMPILE_OPTIONS "-ffp-contract=${_sq_ref_contract}")
67    target_sources(strata_quant PRIVATE ${_sq}/src/quant/arm/dot_neon.cpp ${_sq}/src/quant/arm/act_neon.cpp)
68    target_compile_definitions(strata_quant PRIVATE STRATA_QUANT_ARM_TIERS=1)
69    set(STRATA_QUANT_NEON_EXTRA "" CACHE STRING "extra compile options for the Arm tier files (experiments), semicolon separated")
70    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
71      set_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}")
73    else()
74      set_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}")
76    endif()
77  endif()
78  add_executable(quant_bench ${_sq}/tests/quant/quant_bench.cpp)   # one-thread GB/s of every kernel (no ggml); not a test
79  target_link_libraries(quant_bench PRIVATE strata_quant)
80  if(STRATA_BUILD_TESTS)
81    add_executable(quant_selftest ${_sq}/tests/quant/quant_selftest.cpp)
82    target_link_libraries(quant_selftest PRIVATE strata_quant)
83    add_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)
85    add_executable(nvfp4_decode_model_test ${_sq}/tests/quant/nvfp4_decode_model_test.cpp)
86    target_link_libraries(nvfp4_decode_model_test PRIVATE strata_quant)
87    add_test(NAME nvfp4_decode_model_test COMMAND nvfp4_decode_model_test)
88    if(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)
90    add_executable(type_geometry_oracle ${_sq}/tests/quant/type_geometry_oracle.cpp)
91    target_link_libraries(type_geometry_oracle PRIVATE strata_quant ggml-base)
92    add_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)
94    add_executable(quant_parity ${_sq}/tests/quant/quant_parity.cpp)
95    target_link_libraries(quant_parity PRIVATE strata_quant ggml-cpu ggml-base)
96    target_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)
98    target_compile_definitions(quant_parity PRIVATE STRATA_QUANT_GOLDEN_FILE="${_sq}/tests/quant/golden_x86.txt")
99    add_test(NAME quant_parity COMMAND quant_parity)
100    set_tests_properties(quant_parity PROPERTIES TIMEOUT 600)
101    if(STRATA_CPU_X86 AND 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
103      add_test(NAME quant_golden_regen COMMAND quant_parity --regen-check)
104      set_tests_properties(quant_golden_regen PROPERTIES TIMEOUT 600 SKIP_RETURN_CODE 77)
105    endif()
106    # the speed of the layer's rows against ggml-cpu's (interleaved rounds); not a test
107    add_executable(quant_bench_ggml ${_sq}/tests/quant/quant_bench_ggml.cpp)
108    target_link_libraries(quant_bench_ggml PRIVATE strata_quant ggml-cpu ggml-base)
109    endif()
110  endif()
111endmacro()