whisper : add abort_callback on lang detection (#4077)

This commit is contained in:
Vincenzo Reina
2026-09-23 11:45:29 +02:00
committed by GitHub
parent a44e078459
commit 398997ed68
3 changed files with 80 additions and 5 deletions
+10
View File
@@ -113,6 +113,16 @@ target_compile_definitions(${ZERO_SAMPLES_TEST} PRIVATE
add_test(NAME ${ZERO_SAMPLES_TEST} COMMAND ${ZERO_SAMPLES_TEST})
set_tests_properties(${ZERO_SAMPLES_TEST} PROPERTIES LABELS "tiny;gh")
# abort_callback must be honored during language auto-detection (#3888)
set(LANG_DETECT_ABORT_TEST test-whisper-lang-detect-abort)
add_executable(${LANG_DETECT_ABORT_TEST} ${LANG_DETECT_ABORT_TEST}.cpp)
target_include_directories(${LANG_DETECT_ABORT_TEST} PRIVATE ../include ../ggml/include ../examples)
target_link_libraries(${LANG_DETECT_ABORT_TEST} PRIVATE common)
target_compile_definitions(${LANG_DETECT_ABORT_TEST} PRIVATE
WHISPER_MODEL_PATH="${PROJECT_SOURCE_DIR}/models/for-tests-ggml-tiny.bin")
add_test(NAME ${LANG_DETECT_ABORT_TEST} COMMAND ${LANG_DETECT_ABORT_TEST})
set_tests_properties(${LANG_DETECT_ABORT_TEST} PROPERTIES LABELS "tiny;gh")
# VAD test tests VAD in isolation
set(VAD_TEST test-vad)
add_executable(${VAD_TEST} ${VAD_TEST}.cpp)
+50
View File
@@ -0,0 +1,50 @@
#include "whisper.h"
#include <cstdio>
#include <vector>
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <cassert>
static int n_encoder_begin = 0;
static bool encoder_begin_cb(struct whisper_context *, struct whisper_state *, void *) {
n_encoder_begin++;
return true; // don't block anything, just count
}
static bool abort_cb(void *) {
return true; // abort immediately
}
int main() {
ggml_backend_load_all();
struct whisper_context_params cparams = whisper_context_default_params();
cparams.use_gpu = false;
struct whisper_context * ctx = whisper_init_from_file_with_params(WHISPER_MODEL_PATH, cparams);
assert(ctx != nullptr);
std::vector<float> pcmf32(2*WHISPER_SAMPLE_RATE, 0.0f); // 2 s of silence
struct whisper_full_params params = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
params.language = "auto";
params.print_progress = false;
params.print_realtime = false;
params.encoder_begin_callback = encoder_begin_cb;
params.abort_callback = abort_cb;
const int rc = whisper_full(ctx, params, pcmf32.data(), pcmf32.size());
assert(rc != 0); // the call must fail because we aborted
assert(n_encoder_begin == 1); // aborted during auto-detect: main-loop encoder never started
whisper_free(ctx);
printf("test-whisper-lang-detect-abort: OK\n");
return 0;
}