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
33 changes: 32 additions & 1 deletion .github/workflows/native-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ jobs:
include:
- name: Release shared (PGO)
pgo: true
shared: 'ON'
- name: Release static (PGO)
pgo: true
shared: 'OFF'
- name: Release static
pgo: false
config: '-DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF'
Expand All @@ -36,9 +40,25 @@ jobs:
run: python3 tools/generate_vnni.py --check
- name: Verify both model weights
run: sha256sum -c models/SHA256SUMS
- name: Configure PGO generation with all contracts enabled
if: matrix.pgo
run: >-
cmake -S . -B build/ci-generate -DCMAKE_BUILD_TYPE=Release
-DBUILD_SHARED_LIBS=${{ matrix.shared }} -DBUILD_TESTING=ON
-DDPDF_BUILD_KERNEL_BENCHMARKS=ON
-DDPDF_PGO_MODE=GENERATE
-DDPDF_PROFILE_DIR="$PWD/build/ci-generate/profiles"
- name: Link every PGO generation executable
if: matrix.pgo
run: cmake --build build/ci-generate --parallel 2
- name: Run PGO generation contracts
if: matrix.pgo
run: ctest --test-dir build/ci-generate --output-on-failure --parallel 1
- name: Build recommended PGO release and run contracts
if: matrix.pgo
run: python3 tools/build.py --build-dir build/ci --jobs 2
run: >-
python3 tools/build.py --build-dir build/ci --jobs 2
--cmake-arg=-DBUILD_SHARED_LIBS=${{ matrix.shared }}
- name: Configure static or sanitizer build
if: '!matrix.pgo'
run: cmake -S . -B build/ci ${{ matrix.config }} -DDPDF_PGO_MODE=OFF -DBUILD_TESTING=ON
Expand All @@ -48,3 +68,14 @@ jobs:
- name: Run contracts for both models
if: '!matrix.pgo'
run: ctest --test-dir build/ci --output-on-failure --parallel 1
- name: Preserve configuration and contract logs
if: always()
uses: actions/upload-artifact@v4
with:
name: native-contract-logs-${{ strategy.job-index }}
path: |
optimized_inference/build/ci*/CMakeCache.txt
optimized_inference/build/ci*/Testing/Temporary/LastTest.log
optimized_inference/build/ci*/Testing/Temporary/LastTestsFailed.log
if-no-files-found: ignore
retention-days: 14
90 changes: 89 additions & 1 deletion optimized_inference/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if(NOT CMAKE_BUILD_TYPE)
endif()
option(BUILD_SHARED_LIBS "Build shared runtime" ON)
option(DPDF_SANITIZE "Enable address and undefined-behavior sanitizers" OFF)
option(DPDF_BUILD_KERNEL_BENCHMARKS "Build private hot-kernel measurement tools" OFF)
set(DPDF_PGO_MODE "OFF" CACHE STRING "Model-8 profile mode: OFF, GENERATE, USE")
set_property(CACHE DPDF_PGO_MODE PROPERTY STRINGS OFF GENERATE USE)
set(DPDF_PROFILE_DIR "${CMAKE_BINARY_DIR}/profiles" CACHE PATH "GCC profile directory")
Expand Down Expand Up @@ -81,11 +82,31 @@ foreach(size IN ITEMS 2 8)
${model_dir}/qaffine4pair64_fused_vnni.S)
endif()
endif()
if(size EQUAL 8)
target_sources(backend8 PRIVATE src/models/dpdfnet2_48khz_hr/qaffine4pair64_fused_avx2.S)
target_compile_definitions(backend8 PRIVATE DPDF_QBATCH_FUSED64_AVX2)
endif()
target_compile_options(backend${size} PRIVATE $<$<COMPILE_LANGUAGE:C>:-fno-math-errno>)
# Only model 8 had a confirmed incremental branch-alignment benefit.
if(size EQUAL 8)
target_compile_options(backend${size} PRIVATE -Wa,-mbranches-within-32B-boundaries)
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")
# Preserve the accepted VNNI machine code when aligning the AVX2/common backend.
# GNU as source options follow target options; boundary=0 cancels only the
# extra branch-boundary padding, leaving the existing assembly alignment.
set_property(SOURCE src/common/int8_vnni.c
src/common/qdot64_fixed_vnni.S src/common/qdot4pair_fixed_vnni.S
src/models/dpdfnet2_48khz_hr/qaffine64_fused_vnni.S
src/models/dpdfnet2_48khz_hr/qaffine256_fused_vnni.S
src/models/dpdfnet2_48khz_hr/qaffine4pair64_fused_vnni.S
src/models/dpdfnet8_48khz_hr/qaffine64_fused_vnni.S
APPEND PROPERTY COMPILE_OPTIONS -Wa,-malign-branch-boundary=0)

add_library(dpdfnet src/runtime.c $<TARGET_OBJECTS:backend2> $<TARGET_OBJECTS:backend8>)
add_library(DPDFNet::dpdfnet ALIAS dpdfnet)
target_include_directories(dpdfnet PUBLIC
Expand All @@ -101,6 +122,11 @@ dpdf_compile(dpdfnet)
if(DPDF_PGO_MODE STREQUAL "GENERATE")
target_compile_options(backend8 PRIVATE $<$<COMPILE_LANGUAGE:C>:-fprofile-generate=${DPDF_PROFILE_DIR}>)
target_link_options(dpdfnet PRIVATE -fprofile-generate=${DPDF_PROFILE_DIR})
if(NOT BUILD_SHARED_LIBS)
# Static archives do not link themselves: their consumers need libgcov
# while generating the profile. The USE/OFF release builds do not inherit it.
target_link_options(dpdfnet INTERFACE -fprofile-generate=${DPDF_PROFILE_DIR})
endif()
elseif(DPDF_PGO_MODE STREQUAL "USE")
target_compile_options(backend8 PRIVATE $<$<COMPILE_LANGUAGE:C>:-fprofile-use=${DPDF_PROFILE_DIR};-Werror=missing-profile;-Werror=coverage-mismatch>)
endif()
Expand All @@ -119,9 +145,20 @@ if(BUILD_TESTING)
add_test(NAME public_contract COMMAND public_contract
"${CMAKE_CURRENT_SOURCE_DIR}/models/dpdfnet2_48khz_hr/weights.f32"
"${CMAKE_CURRENT_SOURCE_DIR}/models/dpdfnet8_48khz_hr/weights.f32")
add_executable(cpu_contract tests/cpu_contract.c)
target_link_libraries(cpu_contract PRIVATE dpdfnet)
dpdf_compile(cpu_contract)
add_test(NAME cpu_contract COMMAND cpu_contract 1)
find_package(Threads REQUIRED)
add_executable(concurrency_contract tests/concurrency_contract.c)
target_link_libraries(concurrency_contract PRIVATE dpdfnet m Threads::Threads)
dpdf_compile(concurrency_contract)
add_test(NAME concurrency_contract COMMAND concurrency_contract
"${CMAKE_CURRENT_SOURCE_DIR}/models/dpdfnet2_48khz_hr/weights.f32"
"${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 int8_dispatch_contract fused_contract pair_contract)
conv_helpers_contract depthwise_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)
Expand All @@ -139,6 +176,30 @@ if(BUILD_TESTING)
add_test(NAME ${target} COMMAND ${target} "${CMAKE_CURRENT_SOURCE_DIR}/models/dpdfnet${size}_48khz_hr/weights.f32")
endforeach()
endforeach()
# Scalar reference must retain its ordered multiply/add operations.
set_source_files_properties(tests/depthwise_contract.c PROPERTIES COMPILE_OPTIONS "-fno-tree-vectorize")
if(BUILD_SHARED_LIBS)
add_executable(compare_stream tests/compare_stream.c)
target_include_directories(compare_stream PRIVATE include)
target_link_libraries(compare_stream PRIVATE ${CMAKE_DL_LIBS} m)
dpdf_compile(compare_stream)
add_library(stream_fault SHARED tests/stream_fault.c)
target_include_directories(stream_fault PRIVATE include)
target_link_libraries(stream_fault PRIVATE ${CMAKE_DL_LIBS})
dpdf_compile(stream_fault)
endif()
find_package(Python3 COMPONENTS Interpreter QUIET)
if(Python3_Interpreter_FOUND)
add_test(NAME benchmark_statistics COMMAND "${Python3_EXECUTABLE}"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/benchmark_statistics_test.py")
if(BUILD_SHARED_LIBS)
add_test(NAME stream_comparator COMMAND "${Python3_EXECUTABLE}"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/stream_comparator_test.py"
--runner "$<TARGET_FILE:compare_stream>" --baseline "$<TARGET_FILE:dpdfnet>"
--fault "$<TARGET_FILE:stream_fault>"
--weights "${CMAKE_CURRENT_SOURCE_DIR}/models/dpdfnet2_48khz_hr/weights.f32")
endif()
endif()
foreach(test IN ITEMS wide_contract pair_fused_contract)
add_executable(model2_${test} tests/${test}.c $<TARGET_OBJECTS:backend2>)
target_include_directories(model2_${test} PRIVATE include src src/private)
Expand All @@ -148,6 +209,19 @@ if(BUILD_TESTING)
dpdf_compile(model2_${test})
add_test(NAME model2_${test} COMMAND model2_${test})
endforeach()
get_target_property(backend8_definitions backend8 COMPILE_DEFINITIONS)
if("DPDF_QBATCH_FUSED64_AVX2" IN_LIST backend8_definitions)
add_executable(model8_pair_fused_contract tests/pair_fused_contract.c $<TARGET_OBJECTS:backend8>)
target_include_directories(model8_pair_fused_contract PRIVATE include src src/private)
target_compile_options(model8_pair_fused_contract PRIVATE
-include "${CMAKE_CURRENT_SOURCE_DIR}/src/private/namespace8.h" -frounding-math -fno-tree-vectorize)
target_link_libraries(model8_pair_fused_contract PRIVATE m)
if(DPDF_PGO_MODE STREQUAL "GENERATE")
target_link_options(model8_pair_fused_contract PRIVATE -fprofile-generate=${DPDF_PROFILE_DIR})
endif()
dpdf_compile(model8_pair_fused_contract)
add_test(NAME model8_pair_fused_contract COMMAND model8_pair_fused_contract)
endif()
# 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)
Expand Down Expand Up @@ -178,6 +252,20 @@ if(BUILD_TESTING)
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")
endif()
if(DPDF_BUILD_KERNEL_BENCHMARKS)
foreach(size IN ITEMS 2 8)
add_executable(kernel_benchmark${size} benchmarks/kernels.c $<TARGET_OBJECTS:backend${size}>)
target_include_directories(kernel_benchmark${size} PRIVATE include src/private)
target_compile_options(kernel_benchmark${size} PRIVATE
-include "${CMAKE_CURRENT_SOURCE_DIR}/src/private/namespace${size}.h")
target_compile_definitions(kernel_benchmark${size} PRIVATE DPDF_X86_DISPATCH)
target_link_libraries(kernel_benchmark${size} PRIVATE m)
if(DPDF_PGO_MODE STREQUAL "GENERATE" AND size EQUAL 8)
target_link_options(kernel_benchmark${size} PRIVATE -fprofile-generate=${DPDF_PROFILE_DIR})
endif()
dpdf_compile(kernel_benchmark${size})
endforeach()
endif()
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
install(TARGETS dpdfnet EXPORT DPDFNetTargets
Expand Down
16 changes: 14 additions & 2 deletions optimized_inference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ build/dpdfnet_benchmark 8 models/dpdfnet8_48khz_hr/weights.f32 1000 paced

## Results

The published AVX2 reference measurements below precede runtime dispatch.
The current defaults were selected separately on an i7-7700T. Independent
confirmation in `performance` mode reduced neural-call time by **4.72% for
model 2** and **5.18% for model 8** against the original runtime built with the
same compiler. Every retained factor also had a confirmed conditional benefit.
The original-power follow-up confirmed model 2's gain; model 8 remained
inconclusive there. See [the per-model results](docs/PERFORMANCE.md#i7-7700t-final-per-model-selection-2026-10-27)
and [the exact result record](validation/i7_7700t_final.json).

The published AVX2 reference measurements below are historical: they precede
both runtime dispatch and the i7-7700T selection.
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.
Expand All @@ -77,7 +86,10 @@ Quality means on the same six 48 kHz EARS/WHAM mixtures:
| `dpdfnet2_48khz_hr` | 2.44130 | 0.935874 | 16.0841 | 3.86312 | 4.61457 | 3.28124 |
| `dpdfnet8_48khz_hr` | 2.52099 | 0.940449 | 16.6446 | 3.84506 | 4.63650 | 3.35663 |

Spectrum, state and PCM regression passes on 65 files per model.
Historical package spectrum/state/PCM regression covered 65 original files per
model. Current defaults pass every-hop spectrum/state comparisons on 65
independent EARS/stress streams per model; the historical corpus was unavailable
and no new perceptual scores were computed.
[Measurement methods and quality details](docs/PERFORMANCE.md).

## Project layout
Expand Down
50 changes: 47 additions & 3 deletions optimized_inference/benchmarks/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <sys/resource.h>
static int64_t nanoseconds(clockid_t clock) {
struct timespec t; if (clock_gettime(clock, &t)) { perror("clock_gettime"); exit(1); }
return (int64_t)t.tv_sec * INT64_C(1000000000) + t.tv_nsec;
Expand All @@ -16,6 +17,25 @@ static size_t rss(void) {
static int compare(const void *a, const void *b) {
double x = *(const double *)a, y = *(const double *)b; return (x > y) - (x < y);
}
/* perf stat/record --delay=-1 --control=fifo:CONTROL,ACK. Optional control is
* outside the timed region; ordinary latency runs never attach a profiler. */
static int profiler_command(FILE *control, FILE *ack, const char *command) {
if (!control) return 0;
if (fprintf(control, "%s\n", command) < 0 || fflush(control)) return -1;
char response[32];
/* perf versions may NUL-pad their acknowledgement. */
int first;
do { first = fgetc(ack); } while (first == 0);
if (first == EOF) {
fprintf(stderr, "perf acknowledgement missing after %s\n", command); return -1;
}
response[0] = (char)first;
if (!fgets(response + 1, sizeof(response) - 1, ack)) return -1;
if (strcmp(response, "ack\n")) {
fprintf(stderr, "Unexpected perf acknowledgement after %s: %s\n", command, response); return -1;
}
return 0;
}
int main(int argc, char **argv) {
if (argc < 3 || argc > 6) {
fprintf(stderr, "Usage: %s 2|8 weights.f32 [hops=1000] [paced|continuous] [spectra.f32]\n", argv[0]); return 2;
Expand All @@ -25,6 +45,10 @@ int main(int argc, char **argv) {
size_t hops = argc > 3 ? (size_t)strtoul(argv[3], NULL, 10) : 1000;
int paced = argc < 5 || !strcmp(argv[4], "paced");
if (!info || hops < 100 || hops > 1000000 || (argc > 4 && strcmp(argv[4], "paced") && strcmp(argv[4], "continuous"))) return 2;
const char *control_path = getenv("DPDF_PERF_CONTROL"), *ack_path = getenv("DPDF_PERF_ACK");
FILE *control = control_path ? fopen(control_path, "w") : NULL;
FILE *ack = ack_path ? fopen(ack_path, "r") : NULL;
if ((control_path || ack_path) && (!control || !ack)) return 1;
size_t frame_count = 256;
FILE *input = argc > 5 ? fopen(argv[5], "rb") : NULL;
if (argc > 5) {
Expand All @@ -46,31 +70,51 @@ int main(int argc, char **argv) {
dpdfnet_model *m = weights ? dpdfnet_create(id, weights, info->weight_floats) : NULL;
free(weights);
if (!m || dpdfnet_init_state(id, state)) return 1;
double sum = 0, cpu = 0; size_t over = 0, late = 0;
double sum = 0, cpu = 0, mean = 0, m2 = 0; size_t over = 0, late = 0;
struct rusage usage_before = {0}, usage_after = {0};
float output[962];
int64_t deadline = nanoseconds(CLOCK_MONOTONIC);
for (size_t i = 0; i < hops + 120; ++i) {
if (paced) {
struct timespec t = {deadline / INT64_C(1000000000), deadline % INT64_C(1000000000)};
while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL)) {}
}
if (i == 120 && (profiler_command(control, ack, "enable") || getrusage(RUSAGE_SELF, &usage_before))) return 1;
int64_t c0 = nanoseconds(CLOCK_THREAD_CPUTIME_ID), t0 = nanoseconds(CLOCK_MONOTONIC);
if (dpdfnet_process(m, frames + (i % frame_count) * 962, state, output, state)) return 1;
int64_t t1 = nanoseconds(CLOCK_MONOTONIC), c1 = nanoseconds(CLOCK_THREAD_CPUTIME_ID);
if (i >= 120) {
double ms = (double)(t1 - t0) / 1e6; times[i - 120] = ms; sum += ms;
double delta = ms - mean; mean += delta / (double)(i - 119); m2 += delta * (ms - mean);
cpu += (double)(c1 - c0) / 1e6; over += ms > 10;
late += paced && t1 > deadline + INT64_C(10000000);
}
deadline += INT64_C(10000000);
if (paced && t1 > deadline) deadline = t1; /* Record miss before restarting cadence. */
}
if (profiler_command(control, ack, "disable") || getrusage(RUSAGE_SELF, &usage_after)) return 1;
if (control) fclose(control);
if (ack) fclose(ack);
const char *samples = getenv("DPDF_BENCH_SAMPLES");
if (samples) {
FILE *f = fopen(samples, "wb");
if (!f) return 1;
size_t written = fwrite(times, sizeof(double), hops, f);
int closed = fclose(f);
if (written != hops || closed) return 1;
}
size_t after = rss(); qsort(times, hops, sizeof(double), compare);
printf("{\"model\":\"%s\",\"input\":\"%s\",\"mode\":\"%s\",\"hops\":%zu,"
"\"mean_ms\":%.6f,\"cpu_ms\":%.6f,\"p99_ms\":%.6f,\"max_ms\":%.6f,"
"\"mean_ms\":%.9f,\"cpu_ms\":%.9f,\"p99_ms\":%.9f,\"max_ms\":%.9f,"
"\"min_ms\":%.9f,\"median_ms\":%.9f,\"p95_ms\":%.9f,\"p999_ms\":%.9f,\"hop_sd_ms\":%.9f,"
"\"voluntary_switches\":%ld,\"involuntary_switches\":%ld,\"minor_faults\":%ld,\"major_faults\":%ld,"
"\"over_10ms\":%zu,\"late\":%zu,\"owned_bytes\":%zu,\"rss_bytes\":%zu,\"rss_increment_bytes\":%zd}\n",
info->name, input ? "file" : "synthetic", paced ? "paced" : "continuous", hops,
sum / hops, cpu / hops, times[(hops * 99) / 100], times[hops - 1], over, late,
sum / hops, cpu / hops, times[(hops * 99) / 100], times[hops - 1],
times[0], (times[(hops - 1) / 2] + times[hops / 2]) / 2,
times[(hops * 95) / 100], times[(hops * 999) / 1000], sqrt(m2 / (double)(hops - 1)),
usage_after.ru_nvcsw - usage_before.ru_nvcsw, usage_after.ru_nivcsw - usage_before.ru_nivcsw,
usage_after.ru_minflt - usage_before.ru_minflt, usage_after.ru_majflt - usage_before.ru_majflt, over, late,
dpdfnet_owned_bytes(m), after, (ptrdiff_t)after - (ptrdiff_t)before);
dpdfnet_destroy(m); free(state); free(times); free(frames);
return 0;
Expand Down
Loading
Loading