Debian Codegraph @

external/whispercpp/READ_ME.TXT

raw
1 praat/external/whispercpp/READ_ME.TXT
2 Anastasia Shchupak, 13 March 2026
3
4 This file describes the adaptations to the Whisper.cpp current sources
5 that are needed to make them compatible with Praat.
6 Last maintenance release of Whisper.cpp as of 12 March 2026 was v1.8.3 (Jan 15, 2025).
7 The source code in this edition was taken from commit 30c5194c9691e4e9a98b3dea9f19727397d3f46e.
8
9
10 1. Selecting files and flattening the file structure
11 ----------------------------------------------------
12 The Whisper.cpp sources are distributed over multiple folders, which are in different deep branches
13 of the whisper.cpp root folder. We use only a subset of files and flatten this hierarchy as shown below.
14
15 whisper.cpp
16 include
17 whisper.h
18 src
19 whisper-arch.h
20 whisper.cpp
21 ggml
22 include
23 ggml-alloc.h
24 ggml-backend.h
25 ggml-cpp.h
26 ggml-cpu.h
27 ggml.h
28 gguf.h
29 src
30 ggml-alloc.c -> ggml-alloc.cpp
31 ggml-backend-dl.cpp
32 ggml-backend-dl.h
33 ggml-backend-impl.h
34 ggml-backend-reg.cpp
35 ggml-backend.cpp
36 ggml-common.h
37 ggml-quants.cpp -> ggml-quants.cpp
38 ggml.c -> ggml.cpp
39 ggml-cpu
40 amx
41 amx.h
42 arch-fallback.h
43 binary-ops.cpp
44 binary-ops.h
45 common.h
46 ggml-cpu.cpp -> ggml-cpu.cpp
47 ggml-cpu.cpp -> ggml-cpu-cpp.cpp
48 ggml-cpu-impl.h
49 ops.cpp
50 ops.h
51 quants.c
52 quants.h
53 repack.h
54 simd-gemm.h
55 simd-mappings.h
56 traits.cpp
57 traits.h
58 unary-ops.cpp
59 unary-ops.h
60 vec.cpp
61 vec.h
62 ggml-impl.h
63 ggml-quants.h
64 ggml-threading.cpp
65 ggml-threading.h
66
67 All these files are put into the single external/whispercpp source folder.
68
69 2. New Files
70 ------------
71 2.1. New file: ggml-type-traits.c
72 ---------------------------------
73 When selecting files, we renamed the following files to enable C++ exceptions for graceful cleanup after GGML aborts and failing assertions:
74 ggml.c -> ggml.cpp
75 ggml-cpu.c -> ggml-cpu.cpp
76
77 The following step is necessary to be able to compile ggml.cpp and ggml-cpu.cpp as C++,
78 because C++ doesn't support non-trivial designated initializers.
79
80 We extracted the following array from ggml.cpp to ggml-type-traits.c.
81 ```
82 const struct ggml_type_traits type_traits[GGML_TYPE_COUNT]
83 ```
84 We also extracted the following array from ggml-cpu.cpp to ggml-type-traits.c.
85 ```
86 const struct ggml_type_traits_cpu type_traits_cpu[GGML_TYPE_COUNT]
87 ```
88
89 2.2. New files: ggml-memory-pool.cpp and ggml-memory-pool.h
90 ----------------------------------------------------------
91 To implement GGML cleanup in case it needs to abort, we added a memory pool, which is going to track all
92 memory allocations by GGML. If a new allocation fails, or there is any other reason for GGML to stop,
93 instead of abort (which causes Praat to crash), we will free all the memory registered in the pool.
94 This will allow Praat to continue running after a graceful end of whatever was using GGML: transcription or diarizarion.
95
96
97 3. File modifications
98 ---------------------
99 3.1. ggml.h
100 -----------
101 To make GGML and Whisper-cpp compatible with Praat, we add the following to the top of `ggml.h`:
102 ```
103 #define WHISPER_VERSION "1.8.3"
104 #define GGML_VERSION "0.9.7"
105 #define GGML_COMMIT "unknown"
106 #define GGML_USE_CPU
107 #define GGML_CPU_GENERIC
108 #if ! defined (_GNU_SOURCE)
109 #define _GNU_SOURCE
110 #endif
111 ```
112 This works because `ggml.h` is included, directly or indirectly,
113 at the very top of all `ggml` and `whisper` files (last checked 20260312), except:
114 ggml-common.h, ggml-backend-dl.cpp, ggml-backend-dl.h, unary-ops.cpp, and unary-ops.h.
115 Also, we add GGML_API memory wrappers declaration in `ggml.h` after declaring `ggml_abort()``:
116 ```
117 GGML_API void * ggml_malloc(size_t size);
118 GGML_API void * ggml_calloc(size_t num, size_t size);
119 GGML_API void * ggml_realloc(void * ptr, size_t size);
120 #ifdef __cplusplus
121 GGML_API void ggml_raw_free(void * ptr, bool toRemoveFromPool = true);
122 #else
123 GGML_API void ggml_raw_free(void * ptr, bool toRemoveFromPool);
124 #endif
125 ```
126
127 3.2. ggml.cpp
128 -------------
129 3.2.1. To make use of Melder_throw and TRACE/trace as well as GGML memory pool, we add the following to the top of `ggml.cpp`:
130 ```
131 #include "melder.h"
132 #include "ggml-memory-pool.h"
133 ```
134
135 3.2.2. To intercept GGML abortions and throw Melder exception instead, we change the following line in ggml_abort():
136 ```
137 //abort(); // this is the old line which needs to be removed
138 Melder_throw (Melder_peek8to32_u (message)); // this is the new line
139 ```
140
141 3.2.3. We also change the functions ggml_aligned_malloc(), ggml_aligned_free(), ggml_malloc(), ggml_calloc(),
142 so that all of them except ggml_calloc() do not return NULL, instead calling GGML_ABORT().
143 Also, to register the allocations in our GGML memory pool, we add the following to the ggml_aligned_malloc():
144 ```
145 theGgmlMemoryPool.add (aligned_memory, size, true); // third argument bool aligned = true
146 ```
147
148 and the following to both ggml_malloc() and ggml_calloc():
149 ```
150 theGgmlMemoryPool.add (result, size, false); // third argument bool aligned = false
151 ```
152
153 We add one parameter to ggml_aligned_free() and extend this function as follows:
154 ```
155 void ggml_aligned_free(void * ptr, size_t size, bool toRemoveFromPool) {
156 bool removedFromPool = false;
157 if (toRemoveFromPool)
158 removedFromPool = theGgmlMemoryPool.remove (ptr, size);
159 if (! toRemoveFromPool || removedFromPool) {
160 ... // <------------------------- HERE GOES THE OLD CONTENT OF THIS FUNCTION
161 }
162 ```
163
164 We define these two functions in ggml.cpp:
165 ```
166 void * ggml_realloc(void * ptr, size_t size) {
167 if (size == 0)
168 GGML_ABORT("Behavior may be unexpected when allocating 0 bytes for ggml_malloc!\n");
169
170 void * result = realloc(ptr, size);
171 if (! result)
172 GGML_ABORT("%s: failed to reallocate %6.2f MB\n", __func__, size/(1024.0*1024.0));
173
174 theGgmlMemoryPool.remove (ptr);
175 theGgmlMemoryPool.add (result, size, false);
176 return result;
177 }
178
179 void ggml_raw_free(void *ptr, bool toRemoveFromPool) {
180 bool removedFromPool = false;
181 if (toRemoveFromPool)
182 removedFromPool = theGgmlMemoryPool.remove (ptr);
183 if (! toRemoveFromPool || removedFromPool)
184 free (ptr);
185 }
186 ```
187
188 We remove `inline static` function specifiers from ggml_malloc() and ggml_calloc()
189 and move following three macro definitions to the top, after ggml_abort,
190 and we change `GGML_FREE` macro from `free(ptr)` to `ggml_raw_free(ptr)`:
191 ```
192 #define GGML_MALLOC(size) ggml_malloc(size)
193 #define GGML_CALLOC(num, size) ggml_calloc(num, size)
194 #define GGML_FREE(ptr) ggml_raw_free(ptr) // <------- CHANGE THIS
195 ```
196
197 3.3. ggml-impl.h
198 ----------------
199 We change the declaration of ggml_aligned_free(), adding a default parameter bool toRemoveFromPool = true
200 (also added support for C as a dummy, as this function is currently never called from C files)
201 ```
202 #ifdef __cplusplus
203 GGML_API void ggml_aligned_free(void * ptr, size_t size, bool toRemoveFromPool = true);
204 #else
205 GGML_API void ggml_aligned_free(void * ptr, size_t size, bool toRemoveFromPool);
206 #endif
207 ```
208
209 3.4. Find all the occurrences of raw `malloc`/`calloc`/`realloc`/`free` in the following files:
210 ggml.cpp,
211 ggml-alloc.cpp,
212 ggml-backend.cpp
213 ggml-quants.cpp
214 whisper.cpp
215 and replace them with `ggml_malloc`/`ggml_calloc`/`ggml_realloc`/`ggml_raw_free`(be careful, NOT `ggml_free`, but `ggml_raw_free`).
216
217 3.5. whisper.h and whisper.cpp
218 ------------------------------
219 3.5.1.
220 ------
221 To use silero model for speech detection from memory rather than from the external binary file,
222 we add the following two variables to the `struct whisper_full_params` in whisper.h (in the section for VAD):
223 ```
224 const void * vad_model_data; // Pointer to in-memory model data
225 size_t vad_model_data_size; // Size of in-memory model data
226 ```
227 And we initialize them in whisper.cpp in whisper_full_default_params() (also in the section for VAD):
228 ```
229 /*.vad_model_data =*/ nullptr,
230 /*.vad_model_data_size =*/ 0,
231 ```
232
233 3.5.2.
234 ------
235 Then we declare a function which is responsible for loading the silero model from internal memory.
236 We place it among other WHISPER_API in the section "Voice Activity Detection (VAD)"
237 right after function whisper_vad_init_from_file_with_params() in whisper.h:
238
239 WHISPER_API struct whisper_vad_context * whisper_vad_init_from_memory_with_params(const void * data, size_t size, struct whisper_vad_context_params params);
240
241 And we define it in whisper.cpp:
242 ```
243 struct whisper_vad_context * whisper_vad_init_from_memory_with_params (
244 const void * data, size_t size,
245 whisper_vad_context_params params) {
246 WHISPER_LOG_INFO("%s: loading VAD model from memory\n", __func__);
247 struct SileroVadStream {
248 const void * data;
249 size_t size;
250 size_t pos;
251 };
252 SileroVadStream stream {
253 data,
254 size,
255 0
256 };
257 whisper_model_loader loader {};
258 loader.context = &stream;
259
260 loader.read = [](void * ctx, void * output, size_t read_size) -> size_t {
261 auto * s = (SileroVadStream *)ctx;
262 size_t available = s->size - s->pos;
263 size_t to_read = std::min(read_size, available);
264 memcpy(output, (const unsigned char *)s->data + s->pos, to_read);
265 s->pos += to_read;
266 return to_read;
267 };
268 loader.eof = [](void * ctx) -> bool {
269 auto * s = (SileroVadStream *)ctx;
270 return s->pos >= s->size;
271 };
272 loader.close = [](void * ctx) { };
273 return whisper_vad_init_with_params(&loader, params);
274 }
275 ```
276
277 3.5.3.
278 ------
279 In the same section "Voice Activity Detection (VAD)" in whisper.h, we also declare 5 functions which are meant to
280 provide an interface for accessing information about VAD segments.
281 ```
282 WHISPER_API int whisper_full_n_vad_segments(struct whisper_context * ctx);
283 WHISPER_API int64_t whisper_full_get_vad_segment_orig_start(struct whisper_context * ctx, int i_vad_segment);
284 WHISPER_API int64_t whisper_full_get_vad_segment_orig_end(struct whisper_context * ctx, int i_vad_segment);
285 WHISPER_API int64_t whisper_full_get_vad_segment_vad_start(struct whisper_context * ctx, int i_vad_segment);
286 WHISPER_API int64_t whisper_full_get_vad_segment_vad_end(struct whisper_context * ctx, int i_vad_segment);
287 ```
288 And we define these functions in whisper.cpp:
289 ```
290 int whisper_full_n_vad_segments(struct whisper_context * ctx) {
291 if (!ctx->state->has_vad_segments) {
292 return 0;
293 }
294 return static_cast<int>(ctx->state->vad_segments.size());
295 }
296
297 int64_t whisper_full_get_vad_segment_orig_start(struct whisper_context * ctx, int i_vad_segment) {
298 return ctx->state->vad_segments[i_vad_segment].orig_start;
299 }
300
301 int64_t whisper_full_get_vad_segment_orig_end(struct whisper_context * ctx, int i_vad_segment) {
302 return ctx->state->vad_segments[i_vad_segment].orig_end;
303 }
304
305 int64_t whisper_full_get_vad_segment_vad_start(struct whisper_context * ctx, int i_vad_segment) {
306 return ctx->state->vad_segments[i_vad_segment].vad_start;
307 }
308
309 int64_t whisper_full_get_vad_segment_vad_end(struct whisper_context * ctx, int i_vad_segment) {
310 return ctx->state->vad_segments[i_vad_segment].vad_end;
311 }
312 ```
313
314 3.5.4.
315 ------
316 In whisper.cpp, function whisper_vad() is modified to include the reading from memory. This line:
317 ```
318 struct whisper_vad_context * vctx = whisper_vad_init_from_file_with_params(params.vad_model_path, vad_ctx_params);
319 ```
320 is changed to these lines:
321 ```
322 struct whisper_vad_context * vctx = nullptr;
323 if (params.vad_model_data && params.vad_model_data_size) {
324 vctx = whisper_vad_init_from_memory_with_params((void*)params.vad_model_data, params.vad_model_data_size, vad_ctx_params);
325 } else {
326 vctx = whisper_vad_init_from_file_with_params(params.vad_model_path, vad_ctx_params);
327 }
328 ```
329
330 3.5.5. Endianness
331 -----------------
332 Replace any occurrences of
333 ```
334 #if defined(WHISPER_BIG_ENDIAN)
335 ```
336 with
337 ```
338 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
339 ```
340
341 4. General compatibility with C++
342 ---------------------------------
343 4.1. Assigning from `void *`
344 ----------------------------
345 This is ruled out in C++, so we cast according to the following examples:
346 ```
347 galloc->node_allocs = ggml_calloc(graph->n_nodes, sizeof(struct node_alloc)); // C
348 galloc->node_allocs = (struct node_alloc *) ggml_calloc(graph->n_nodes, sizeof(struct node_alloc)); // C++
349
350 result->vals = GGML_CALLOC(result->set.size, sizeof(struct ggml_tensor *)); // C
351 result->vals = (struct ggml_tensor **) GGML_CALLOC(result->set.size, sizeof(struct ggml_tensor *)); // C++
352
353 char * const data = tensor->data; // C
354 char * const data = (char *) tensor->data; // C++
355
356 const float * l = left; // C
357 const float * l = (const float *) left; // C++
358
359 quantize_row_q2_K_ref(src, dst, (int64_t)nrow*n_per_row); // C
360 quantize_row_q2_K_ref(src, (block_q2_K *) dst, (int64_t)nrow*n_per_row); // C++
361
362 char (*atomic_current_chunk)[CACHE_LINE_SIZE] = blabla // C
363 char (*atomic_current_chunk)[CACHE_LINE_SIZE] = (char (*)[CACHE_LINE_SIZE]) blabla // C++
364 ```
365
366 4.2. Assigning int to enum
367 --------------------------
368 This is also ruled out in C++, so we cast according to the following examples:
369 ```
370 const enum ggml_op_pool op = ggml_get_op_params_i32(tensor, 0);
371 const enum ggml_op_pool op = (enum ggml_op_pool) ggml_get_op_params_i32(tensor, 0);
372
373 p->prio = 0; // C
374 p->prio = (enum ggml_sched_priority) 0; // C++
375
376 static struct ggml_state g_state = {0}; // C
377 static struct ggml_state g_state {}; // C++
378 ```
379
380 4.3. Assignments in initializer lists
381 -------------------------------------
382 Types have to match more closely in C++, so we cast according to the following examples:
383 ```
384 *cgraph = (struct ggml_cgraph) { size, ... // C
385 *cgraph = (struct ggml_cgraph) { (int) size, ... // C++
386
387 int32_t params[] = { nb1, nb2, nb3, offset, inplace ? 1 : 0 }; // C
388 int32_t params[] = { (int32_t) nb1, (int32_t) nb2, (int32_t) nb3, (int32_t) offset, inplace ? 1 : 0 }; // C++
389
390 MMID_MATRIX_ROW(i02, matrix_row_counts[i02]) = (struct mmid_row_mapping) {id, iid1}; // C
391 MMID_MATRIX_ROW(i02, matrix_row_counts[i02]) = (struct mmid_row_mapping) {id, (int32_t) iid1}; // C++
392
393 union { uint16_t u16; ggml_fp16_t fp16; } u = {i}; // C
394 union { uint16_t u16; ggml_fp16_t fp16; } u = { (uint16_t) i }; // C++
395
396 struct ggml_compute_params params = {
397 /*.ith =*/ state->ith,
398 /*.nth =*/ atomic_load_explicit(&tp->n_graph, memory_order_relaxed) & GGML_THREADPOOL_N_THREADS_MASK,
399 /*.wsize =*/ cplan->work_size,
400 /*.wdata =*/ cplan->work_data,
401 /*.threadpool =*/ tp,
402 /*.use_ref =*/ cplan->use_ref,
403 }; // C
404 struct ggml_compute_params params = {
405 /*.ith =*/ state->ith,
406 /*.nth =*/ (int) (atomic_load_explicit(&tp->n_graph, memory_order_relaxed) & GGML_THREADPOOL_N_THREADS_MASK),
407 /*.wsize =*/ cplan->work_size,
408 /*.wdata =*/ cplan->work_data,
409 /*.threadpool =*/ tp,
410 /*.use_ref =*/ cplan->use_ref,
411 }; // C++
412 ```
413
414 5. Models
415 ---------
416 5.1. Bringing Silero-VAD model to Praat source code
417 ---------------------------------------------------
418 First, we download the ggml Silero model from the original whisper.cpp repository:
419 ```
420 whisper.cpp/models/download-vad-model.sh silero-v6.2.0
421 ```
422 The result is `ggml-silero-v6.2.0.bin`, which is a C-compatible binary file, which can be loaded by whisper.cpp.
423
424 We then convert this binary to a C header using `xxd` and copy it to external/whispercpp directory:
425 ```
426 xxd -i -n ggml_silero_bin -n whisper.cpp/models/ggml-silero-v6.2.0.bin > praat/external/whispercpp/ggml-silero-vad-model-data.h
427 ```
428
429 5.2. Segmentation
430 -----------------
431 see generate/pyannote/README.txt
432
433 5.3. Embedding
434 --------------
435 see generate/pyannote/README.txt