mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 23:38:47 +00:00
1. CUDA fast lane (cold experts computed on the CPU): new ink_host_avx512.cpp has the three host kernels (SF4 row, MX8 row, S1.5..S8_K sub-group) for
AVX-512 F/DQ/BW/VL (no VNNI, so Skylake-X runs it too). The file is built with the AVX-512 flags and nothing else is; its gate, host_avx512::available(),
is in ink_host_avx512_gate.cpp, a baseline-flag file (a compiler may use AVX-512 anywhere in a file built with -mavx512f, so the gate cannot live there):
caps().avx512, closed by STRATA_FORCE_ISA=avx2. ink_hybrid.cuh tries AVX-512, then AVX2, then plain C++.
ink_host_avx2.cpp unpacked the S2_K / S3_K / S5_K / S6_K fields with scalar loops (0.7 to 1.1 GB/s of weights per thread): shuffles now, integer sums exact.
2. The CPU backend and the HIP / SYCL CPU-expert lane (eval_inkling2_expert_batch -> sk_codecs.cpp) ran plain x86-64 C++: strata_quant builds sk_codecs.cpp with
no ISA flag (its AVX2 / AVX-512 code is selected by __AVX2__ at build time). It is now compiled three times (sk_codecs_base.cpp, x86/sk_codecs_avx2.cpp,
x86/sk_codecs_avx512.cpp) into strata::quant::sk::{base,avx2,avx512} and sk_dispatch.cpp picks the tier from caps() (AVX-512 needs VNNI there, as strata_quant's
own tier does). A CPU without AVX2 never executes an AVX2 instruction of it, and a -march=native host cannot leak wide code into the base tier. Tier files are
-ffp-contract=off: the integer sums are exact and the float steps are the base tier's, so every S_K result is bit-identical across tiers (the aarch64 path and the
builds that compile sk_codecs.cpp themselves (hy4flash, Metal, the SYCL script) are unchanged). AVX2 gets vector unpackers and an abs/sign int8 dot.
SF4 / MX8 sum in lane order, so they differ from the scalar tier by float rounding.
Tests: tests/core/ink_host_simd_test.cpp (all formats, scalar reference, AVX2 and AVX-512, F8_E4M3 decode on all 256 codes), tests/quant/sk_tier_test.cpp (tiers
against each other: S_K bit-identical, dispatcher = reported tier), benches ink_host_simd_bench / sk_tier_bench. Run on Zen 4 and Zen 5 (AVX-512 + VNNI), Zen 2 and under
qemu -cpu Nehalem / SandyBridge / IvyBridge (gates closed, nothing executed above the CPU's level), tools/check --only cmake-cpu: 160 passed.
One thread, S_K gemm of a 2304 x 2304 expert matrix, one token, base -> avx2 (Zen 4): S2_K 10x, S3_K 13x, S4_K 2.9x, S5_K 9x, S6_K 8x, S8_K 2.7x. AVX-512 over AVX2: 1.0x on
Zen 4, 1.0 to 1.7x on Zen 5 (ink_host kernels: SF4 1.57x, MX8 1.69x, S3_K 1.35x, S5_K 1.5x).
147 lines
13 KiB
CMake
147 lines
13 KiB
CMake
# cmake/backends/quant.cmake - the Strata quant layer (own-stack phase 2): strata_quant, its self-test and the ggml-cpu oracle test.
|
|
#
|
|
# strata_quant has no ggml dependency (the block layouts and codebooks come from include/strata/quant/strata-quant-common.h, generated by tools/gen_quant_header.py).
|
|
# 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
|
|
# 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).
|
|
macro(strata_quant_layer)
|
|
if(NOT STRATA_QUANT_ROOT)
|
|
set(STRATA_QUANT_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
|
|
endif()
|
|
set(_sq ${STRATA_QUANT_ROOT})
|
|
set(_strata_quant_srcs ${_sq}/src/quant/types.cpp ${_sq}/src/quant/isa.cpp ${_sq}/src/quant/dispatch.cpp ${_sq}/src/quant/selfcheck.cpp
|
|
${_sq}/src/quant/act_ref.cpp ${_sq}/src/quant/dot_generic.cpp ${_sq}/src/quant/dequant.cpp)
|
|
add_library(strata_quant STATIC ${_strata_quant_srcs})
|
|
target_include_directories(strata_quant PUBLIC ${_sq}/include PRIVATE ${_sq}/src/quant)
|
|
if(NOT STRATA_CPU_X86)
|
|
# arm64 and the rest: the S_K / SF4 / MX8 codecs are one compile-time-selected TU (NEON dotprod is checked at run time inside it); x86 builds them as run-time tiers below
|
|
target_sources(strata_quant PRIVATE ${_sq}/src/quant/sk_codecs.cpp)
|
|
endif()
|
|
if(NOT MSVC)
|
|
# the Generic contract is ggml's _generic C code, summed in order: no contraction into FMA whatever the target's baseline
|
|
set_source_files_properties(${_sq}/src/quant/dot_generic.cpp ${_sq}/src/quant/act_ref.cpp PROPERTIES COMPILE_OPTIONS "-ffp-contract=off")
|
|
endif()
|
|
if(STRATA_CPU_X86)
|
|
# 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
|
|
# select what ggml-cpu selects for that flavour (the release builds are AVX2; STRATA_ISA_FLOOR=avx is the AVX-only one)
|
|
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
|
|
${_sq}/src/quant/x86/act_avx.cpp ${_sq}/src/quant/x86/dot_avx512.cpp ${_sq}/src/quant/x86/act_avx512.cpp)
|
|
target_compile_definitions(strata_quant PRIVATE STRATA_QUANT_X86_TIERS=1)
|
|
# The S_K / SF4 / MX8 codecs (sk_codecs.cpp) as three tiers (base / avx2 / avx512) chosen at run time by sk_dispatch.cpp from caps() (src/quant/sk_tiers.hpp):
|
|
# a CPU without AVX2 never executes an AVX2 instruction of them, and a -march=native host cannot leak wide code into the base tier. The tier files are
|
|
# -ffp-contract=off: the float steps are the base tier's in the base tier's order and the integer sums are exact, so the S_K dots are bit-identical per tier.
|
|
target_sources(strata_quant PRIVATE ${_sq}/src/quant/sk_dispatch.cpp ${_sq}/src/quant/sk_codecs_base.cpp
|
|
${_sq}/src/quant/x86/sk_codecs_avx2.cpp ${_sq}/src/quant/x86/sk_codecs_avx512.cpp)
|
|
target_compile_definitions(strata_quant PRIVATE STRATA_SK_X86_TIERS=1)
|
|
if(MSVC)
|
|
set_source_files_properties(${_sq}/src/quant/x86/sk_codecs_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
|
|
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
|
set_source_files_properties(${_sq}/src/quant/x86/sk_codecs_avx512.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX512;/clang:-mavx512vnni;/clang:-ffp-contract=off")
|
|
else()
|
|
set_source_files_properties(${_sq}/src/quant/x86/sk_codecs_avx512.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX512")
|
|
endif()
|
|
else()
|
|
set_source_files_properties(${_sq}/src/quant/sk_codecs_base.cpp PROPERTIES COMPILE_OPTIONS "-ffp-contract=off;-mno-avx;-mno-avx2;-mno-fma;-mno-f16c")
|
|
set_source_files_properties(${_sq}/src/quant/x86/sk_codecs_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c;-ffp-contract=off")
|
|
set_source_files_properties(${_sq}/src/quant/x86/sk_codecs_avx512.cpp PROPERTIES
|
|
COMPILE_OPTIONS "-mavx512f;-mavx512bw;-mavx512vl;-mavx512dq;-mavx512vnni;-mavx2;-mfma;-mf16c;-ffp-contract=off")
|
|
endif()
|
|
if(MSVC)
|
|
# 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
|
|
# differ from GCC's in the Q5_K and IQ1_S dots, see below. Under clang-cl (MSVC frontend + Clang compiler), /arch:AVX512 enables Skylake-X only,
|
|
# so pass /clang:-mavx512vnni explicitly so dot_avx512.cpp emits VPDPBUSD.
|
|
set_source_files_properties(${_sq}/src/quant/x86/dot_avx2.cpp ${_sq}/src/quant/x86/act_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
|
|
set_source_files_properties(${_sq}/src/quant/x86/dot_avx.cpp ${_sq}/src/quant/x86/act_avx.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX")
|
|
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
|
set_source_files_properties(${_sq}/src/quant/x86/dot_avx512.cpp ${_sq}/src/quant/x86/act_avx512.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX512;/clang:-mavx512vnni")
|
|
else()
|
|
set_source_files_properties(${_sq}/src/quant/x86/dot_avx512.cpp ${_sq}/src/quant/x86/act_avx512.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX512")
|
|
endif()
|
|
else()
|
|
# 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
|
|
# 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,
|
|
# 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
|
|
# x86_dots.inc (STRATA_NOFUSE), and dot_generic.cpp (Q2_0 under every contract) is -ffp-contract=off above.
|
|
# 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
|
|
# (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
|
|
# (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
|
|
# built here is compared for information. MSVC (branch above, no pin): ours equals MSVC's ggml, which does not contract the intrinsics, so
|
|
# Q5_K and IQ1_S dots differ from GCC's by 1-512 ulp (the golden file's msvc lines).
|
|
set(STRATA_QUANT_AVX2_EXTRA "" CACHE STRING "extra compile options for the AVX2 tier files (experiments), semicolon separated")
|
|
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}")
|
|
# AVX2, FMA and F16C explicitly off: a -march=native build host must not leak wider code into the AVX-only tier
|
|
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")
|
|
# the 512-bit kernels of the Avx2 contract: F/DQ/BW/VL + VNNI (what Isa::Avx512 means), called only behind caps()
|
|
set_source_files_properties(${_sq}/src/quant/x86/dot_avx512.cpp ${_sq}/src/quant/x86/act_avx512.cpp PROPERTIES COMPILE_OPTIONS
|
|
"-mavx512f;-mavx512bw;-mavx512vl;-mavx512dq;-mavx512vnni;-mavx2;-mfma;-mf16c;-ffp-contract=fast")
|
|
endif()
|
|
endif()
|
|
if(NOT STRATA_CPU_X86 AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|ARM64)$")
|
|
# 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;
|
|
# 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
|
|
# 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
|
|
# (-ffp-contract=fast); Clang's default (contract within one expression) is what ggml-cpu gets there, so only GCC is given the flag.
|
|
# 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
|
|
# 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
|
|
# 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".
|
|
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)")
|
|
if(NOT "${STRATA_QUANT_REF_CONTRACT}" STREQUAL "")
|
|
set(_sq_ref_contract ${STRATA_QUANT_REF_CONTRACT})
|
|
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
|
set(_sq_ref_contract fast)
|
|
else()
|
|
set(_sq_ref_contract on)
|
|
endif()
|
|
set_source_files_properties(${_sq}/src/quant/act_ref.cpp ${_sq}/src/quant/dequant.cpp PROPERTIES COMPILE_OPTIONS "-ffp-contract=${_sq_ref_contract}")
|
|
target_sources(strata_quant PRIVATE ${_sq}/src/quant/arm/dot_neon.cpp ${_sq}/src/quant/arm/act_neon.cpp)
|
|
target_compile_definitions(strata_quant PRIVATE STRATA_QUANT_ARM_TIERS=1)
|
|
set(STRATA_QUANT_NEON_EXTRA "" CACHE STRING "extra compile options for the Arm tier files (experiments), semicolon separated")
|
|
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
|
set_source_files_properties(${_sq}/src/quant/arm/dot_neon.cpp ${_sq}/src/quant/arm/act_neon.cpp PROPERTIES COMPILE_OPTIONS
|
|
"-march=armv8.2-a+dotprod+fp16+i8mm;-ffp-contract=fast;${STRATA_QUANT_NEON_EXTRA}")
|
|
else()
|
|
set_source_files_properties(${_sq}/src/quant/arm/dot_neon.cpp ${_sq}/src/quant/arm/act_neon.cpp PROPERTIES COMPILE_OPTIONS
|
|
"-march=armv8.2-a+dotprod+fp16+i8mm;${STRATA_QUANT_NEON_EXTRA}")
|
|
endif()
|
|
endif()
|
|
add_executable(quant_bench ${_sq}/tests/quant/quant_bench.cpp) # one-thread GB/s of every kernel (no ggml); not a test
|
|
target_link_libraries(quant_bench PRIVATE strata_quant)
|
|
if(STRATA_BUILD_TESTS)
|
|
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)
|
|
if(STRATA_CPU_X86)
|
|
# the run-time ISA tiers of the S_K / SF4 / MX8 codecs against each other: S_K bit-identical (no GPU); run it under qemu -cpu EPYC-Genoa for the avx512 tier
|
|
add_executable(sk_tier_test ${_sq}/tests/quant/sk_tier_test.cpp)
|
|
target_include_directories(sk_tier_test PRIVATE ${_sq}/src/quant)
|
|
target_link_libraries(sk_tier_test PRIVATE strata_quant)
|
|
add_test(NAME sk_tier_test COMMAND sk_tier_test)
|
|
endif()
|
|
# 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)
|
|
add_executable(nvfp4_decode_model_test ${_sq}/tests/quant/nvfp4_decode_model_test.cpp)
|
|
target_link_libraries(nvfp4_decode_model_test PRIVATE strata_quant)
|
|
add_test(NAME nvfp4_decode_model_test COMMAND nvfp4_decode_model_test)
|
|
if(NOT STRATA_QUANT_NO_GGML) # (the CPU check without ggml builds the tests above only; everything below links ggml)
|
|
# 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)
|
|
target_include_directories(quant_parity PRIVATE ${_sq}/src/quant)
|
|
# leg 1 of the oracle: the golden reference digests (tests/quant/quant_parity.cpp says how they are made and checked)
|
|
target_compile_definitions(quant_parity PRIVATE STRATA_QUANT_GOLDEN_FILE="${_sq}/tests/quant/golden_x86.txt")
|
|
add_test(NAME quant_parity COMMAND quant_parity)
|
|
set_tests_properties(quant_parity PROPERTIES TIMEOUT 600)
|
|
if(STRATA_CPU_X86 AND EXISTS ${_sq}/tests/quant/golden_x86.txt)
|
|
# 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
|
|
add_test(NAME quant_golden_regen COMMAND quant_parity --regen-check)
|
|
set_tests_properties(quant_golden_regen PROPERTIES TIMEOUT 600 SKIP_RETURN_CODE 77)
|
|
endif()
|
|
# the speed of the layer's rows against ggml-cpu's (interleaved rounds); not a test
|
|
add_executable(quant_bench_ggml ${_sq}/tests/quant/quant_bench_ggml.cpp)
|
|
target_link_libraries(quant_bench_ggml PRIVATE strata_quant ggml-cpu ggml-base)
|
|
endif()
|
|
endif()
|
|
endmacro()
|