Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/native-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ jobs:
- name: Release static
pgo: false
config: '-DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF'
- name: Release AVX2 fallback
pgo: false
config: '-DCMAKE_BUILD_TYPE=Release -DDPDF_ENABLE_AVX_VNNI=OFF'
- name: ASan and UBSan
pgo: false
config: '-DCMAKE_BUILD_TYPE=RelWithDebInfo -DDPDF_SANITIZE=ON -DBUILD_SHARED_LIBS=ON'
Expand All @@ -29,6 +32,8 @@ jobs:
working-directory: optimized_inference
steps:
- uses: actions/checkout@v4
- name: Check generated VNNI assembly
run: python3 tools/generate_vnni.py --check
- name: Verify both model weights
run: sha256sum -c models/SHA256SUMS
- name: Build recommended PGO release and run contracts
Expand Down
57 changes: 56 additions & 1 deletion optimized_inference/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ function(dpdf_compile target)
endif()
endif()
endfunction()
# Keep AVX-VNNI optional both at build time and at model initialization.
# Baseline objects must remain executable on AVX2/FMA CPUs without VNNI.
option(DPDF_ENABLE_AVX_VNNI "Build runtime-dispatched AVX-VNNI kernels" ON)
include(CheckCSourceCompiles)
include(CMakePushCheckState)
if(DPDF_ENABLE_AVX_VNNI)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_FLAGS "-mavx2 -mfma -mavxvnni")
check_c_source_compiles("#include <immintrin.h>
int main(void) { __m256i x = _mm256_set1_epi32(__builtin_cpu_supports(\"avxvnni\"));
x = _mm256_dpbusd_avx_epi32(x, x, x);
return __builtin_cpu_supports(\"avxvnni\") + _mm256_extract_epi32(x, 0); }"
DPDF_COMPILER_HAS_AVX_VNNI)
cmake_pop_check_state()
endif()
foreach(size IN ITEMS 2 8)
set(model_dir "${CMAKE_CURRENT_SOURCE_DIR}/src/models/dpdfnet${size}_48khz_hr")
add_library(backend${size} OBJECT src/model_backend.c
Expand All @@ -56,9 +71,20 @@ foreach(size IN ITEMS 2 8)
${model_dir}/qaffine4pair64_fused_avx2.S)
target_compile_definitions(backend2 PRIVATE DPDF_QFUSED256 DPDF_QPAIR_FUSED64)
endif()
if(DPDF_ENABLE_AVX_VNNI AND DPDF_COMPILER_HAS_AVX_VNNI)
target_compile_definitions(backend${size} PRIVATE DPDF_HAS_AVX_VNNI)
target_sources(backend${size} PRIVATE src/common/int8_vnni.c
src/common/qdot64_fixed_vnni.S src/common/qdot4pair_fixed_vnni.S
${model_dir}/qaffine64_fused_vnni.S)
if(size EQUAL 2)
target_sources(backend2 PRIVATE ${model_dir}/qaffine256_fused_vnni.S
${model_dir}/qaffine4pair64_fused_vnni.S)
endif()
endif()
dpdf_compile(backend${size})
endforeach()
set_source_files_properties(src/common/avx2.c src/common/int8.c PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma")
set_source_files_properties(src/common/int8_vnni.c PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mavxvnni")
set_source_files_properties(src/common/fp16.c PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c")
add_library(dpdfnet src/runtime.c $<TARGET_OBJECTS:backend2> $<TARGET_OBJECTS:backend8>)
add_library(DPDFNet::dpdfnet ALIAS dpdfnet)
Expand Down Expand Up @@ -95,13 +121,16 @@ if(BUILD_TESTING)
"${CMAKE_CURRENT_SOURCE_DIR}/models/dpdfnet8_48khz_hr/weights.f32")
foreach(size IN ITEMS 2 8)
foreach(test IN ITEMS full_contract extended_contract layout_contract generated_gru_contract
conv_helpers_contract int8_contract fused_contract pair_contract)
conv_helpers_contract int8_contract int8_dispatch_contract fused_contract pair_contract)
set(target "model${size}_${test}")
add_executable(${target} tests/${test}.c $<TARGET_OBJECTS:backend${size}>)
target_include_directories(${target} PRIVATE include src src/private)
target_compile_options(${target} PRIVATE
-include "${CMAKE_CURRENT_SOURCE_DIR}/src/private/namespace${size}.h" -frounding-math)
target_compile_definitions(${target} PRIVATE DPDF_X86_DISPATCH DPDF_CONV_PAIR)
if(DPDF_ENABLE_AVX_VNNI AND DPDF_COMPILER_HAS_AVX_VNNI)
target_compile_definitions(${target} PRIVATE DPDF_HAS_AVX_VNNI)
endif()
target_link_libraries(${target} PRIVATE m)
if(DPDF_PGO_MODE STREQUAL "GENERATE" AND size EQUAL 8)
target_link_options(${target} PRIVATE -fprofile-generate=${DPDF_PROFILE_DIR})
Expand All @@ -119,6 +148,32 @@ if(BUILD_TESTING)
dpdf_compile(model2_${test})
add_test(NAME model2_${test} COMMAND model2_${test})
endforeach()
# Reuse the guarded scalar assembly oracles for each VNNI specialization.
if(DPDF_ENABLE_AVX_VNNI AND DPDF_COMPILER_HAS_AVX_VNNI)
foreach(size IN ITEMS 2 8)
set(vnni_contracts fused_contract pair_contract)
if(size EQUAL 2)
list(APPEND vnni_contracts wide_contract pair_fused_contract)
endif()
foreach(test IN LISTS vnni_contracts)
set(target "model${size}_${test}_vnni")
add_executable(${target} tests/${test}.c $<TARGET_OBJECTS:backend${size}>)
target_include_directories(${target} PRIVATE include src src/private)
target_compile_options(${target} PRIVATE
-include "${CMAKE_CURRENT_SOURCE_DIR}/src/private/namespace${size}.h"
"SHELL:-include \"${CMAKE_CURRENT_SOURCE_DIR}/src/private/int8_vnni_aliases.h\""
-frounding-math -fno-tree-vectorize)
target_compile_definitions(${target} PRIVATE DPDF_TEST_AVX_VNNI)
target_link_libraries(${target} PRIVATE m)
if(DPDF_PGO_MODE STREQUAL "GENERATE" AND size EQUAL 8)
target_link_options(${target} PRIVATE -fprofile-generate=${DPDF_PROFILE_DIR})
endif()
dpdf_compile(${target})
add_test(NAME ${target} COMMAND ${target})
set_tests_properties(${target} PROPERTIES SKIP_RETURN_CODE 77)
endforeach()
endforeach()
endif()
get_property(tests DIRECTORY PROPERTY TESTS)
set_tests_properties(${tests} PROPERTIES TIMEOUT 120
ENVIRONMENT "ASAN_OPTIONS=halt_on_error=1:abort_on_error=1:handle_segv=0;UBSAN_OPTIONS=halt_on_error=1")
Expand Down
9 changes: 8 additions & 1 deletion optimized_inference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

A small, single-threaded C runtime for **`dpdfnet2_48khz_hr`** and
**`dpdfnet8_48khz_hr`**, with bundled weights and optimized AVX2 assembly kernels.
Both models use the final W7A8 implementation: quantized matrix weights and
AVX-VNNI kernels are selected automatically on supported CPUs, with AVX2
fallback in the same binary. Both models use the final W7A8 implementation:
quantized matrix weights and
activations, FP32 recurrent state, and fitted GRU gate functions.

The supported target is **Linux x86-64 with AVX2/FMA and GCC**. Windows users can
Expand Down Expand Up @@ -55,6 +57,11 @@ build/dpdfnet_benchmark 8 models/dpdfnet8_48khz_hr/weights.f32 1000 paced

## Results

The published AVX2 reference measurements below precede runtime dispatch.
Dispatch adds a small amount of context bookkeeping (see
[build instructions](docs/BUILD.md#optional-avx-vnni-kernels)); AVX-VNNI
performance depends on the target CPU.

Intel i7-8700, Linux/WSL2, one thread; mean ± SD over 32 fresh runs/model
(1,000 timed hops/run at 10 ms cadence).

Expand Down
36 changes: 36 additions & 0 deletions optimized_inference/docs/BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,39 @@ python3 tools/verify_audio.py --data /path/to/fixtures --workers 4
`validation/reference_streams.json` records relative fixture paths and input,
spectrum, state and PCM checksums. Audio datasets and listening outputs are
not bundled. Run correctness jobs separately from latency benchmarks.

## Optional AVX-VNNI kernels

`DPDF_ENABLE_AVX_VNNI` defaults to `ON`. CMake checks that GCC supports both
VEX-encoded AVX-VNNI intrinsics and the CPU feature query. If that check fails,
the build retains the AVX2 kernels. To explicitly build only AVX2 kernels:

```sh
cmake -S . -B build/avx2 -DDPDF_ENABLE_AVX_VNNI=OFF -DDPDF_PGO_MODE=OFF
cmake --build build/avx2
ctest --test-dir build/avx2 --output-on-failure
```

The normal binary supports the same AVX2/FMA baseline as before. Each quantized
matrix selects its implementation during creation, using GCC's CPU feature
query. The audio callback performs no feature detection or allocation. Only
`int8_vnni.c` and the corresponding assembly require AVX-VNNI; no global
`-march=native`, AVX-512, or model format change is needed. Initialization adds
two private function pointers per quantized matrix (4,016 bytes for model 2 and
5,552 bytes for model 8 on x86-64). The public ABI is unchanged.

The generated VNNI assembly keeps the addressing, floating-point instruction
order, and ABI of the AVX2 assembly. Regenerate it after editing those sources:

```sh
python3 tools/generate_vnni.py
python3 tools/generate_vnni.py --check
```

The W7A8 activation range is 0–254 and the weight range is -63–63. Therefore,
the AVX2 intermediate signed 16-bit pair sums cannot saturate
(`2 * 254 * 63 = 32004`). Replacing its multiply/add reduction with the
non-saturating `VPDPBUSD` preserves integer sums, quantization, scaling, and
floating-point output. `int8_dispatch_contract` checks bitwise equality with
the AVX2 implementation for both models, including single and paired kernels,
row tiles and tails, extreme input values, and unaligned buffers.
Loading
Loading