transcribe-cpp-sys 0.4.1

Native FFI bindings for transcribe.cpp, a C/C++ speech-to-text library built on ggml
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// arch/qwen3_asr/encoder.cpp - audio encoder graph + host-side helpers.
//
// Reference: Qwen3ASRAudioEncoder in modeling_qwen3_asr.py (qwen_asr
// package, v0.0.6).

#include "encoder.h"

#include "ggml.h"
#include "transcribe-debug.h"
#include "transcribe-log.h"

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <vector>

namespace transcribe::qwen3_asr {

// Timing

int32_t aftercnn_len(int32_t mel_len) {
    // Three rounds of Conv2d(stride=2, pad=1, kernel=3):
    //   out = floor((L + 2*1 - 3) / 2) + 1 = floor((L-1)/2) + 1
    // which equals (L + 1) / 2 for L >= 1 under C integer division.
    auto step = [](int32_t L) {
        return (L + 1) / 2;
    };
    return step(step(step(mel_len)));
}

EncoderTiming compute_encoder_timing(int32_t n_mel_frames, const QwenAsrHParams & hp) {
    EncoderTiming t{};
    t.n_mel_frames  = n_mel_frames;
    t.mel_per_chunk = hp.enc_n_window * 2;
    if (n_mel_frames <= 0 || t.mel_per_chunk <= 0) {
        return t;
    }
    t.n_chunks   = (n_mel_frames + t.mel_per_chunk - 1) / t.mel_per_chunk;
    int32_t last = n_mel_frames - (t.n_chunks - 1) * t.mel_per_chunk;
    if (last == 0) {
        last = t.mel_per_chunk;
    }
    t.last_chunk_real_mel = last;
    t.per_chunk_aftercnn  = aftercnn_len(t.mel_per_chunk);
    t.last_chunk_aftercnn = aftercnn_len(last);
    t.T_enc_padded        = t.n_chunks * t.per_chunk_aftercnn;
    t.T_enc               = (t.n_chunks - 1) * t.per_chunk_aftercnn + t.last_chunk_aftercnn;
    t.aftercnn_lens_total = aftercnn_len(n_mel_frames);
    return t;
}

std::vector<float> build_sinusoid_pe(int32_t d_model, int32_t length, double max_timescale) {
    std::vector<float> pe(static_cast<size_t>(d_model) * length, 0.0f);
    if (d_model <= 0 || length <= 0 || (d_model % 2) != 0) {
        return pe;
    }

    const int32_t half   = d_model / 2;
    const double  log_ts = std::log(max_timescale) / (half - 1);

    for (int32_t p = 0; p < length; ++p) {
        for (int32_t k = 0; k < half; ++k) {
            const double inv_ts                             = std::exp(-log_ts * k);
            const double scaled                             = static_cast<double>(p) * inv_ts;
            pe[static_cast<size_t>(p) * d_model + k]        = static_cast<float>(std::sin(scaled));
            pe[static_cast<size_t>(p) * d_model + k + half] = static_cast<float>(std::cos(scaled));
        }
    }
    return pe;
}

// Graph construction

namespace {

constexpr float kLayerNormEps = 1e-5f;

ggml_tensor * named(ggml_tensor * t, const char * name) {
    if (t != nullptr && name != nullptr) {
        ggml_set_name(t, name);
    }
    return t;
}

ggml_tensor * add_conv_bias(ggml_context * ctx, ggml_tensor * x, ggml_tensor * bias) {
    // bias is [channels]; reshape to [1, 1, channels, 1] to broadcast
    // across the spatial and batch axes of conv output [W, H, C, N].
    ggml_tensor * b4 = ggml_reshape_4d(ctx, bias, 1, 1, bias->ne[0], 1);
    return ggml_add(ctx, x, b4);
}

ggml_tensor * layer_norm(ggml_context * ctx, ggml_tensor * x, ggml_tensor * gamma, ggml_tensor * beta) {
    ggml_tensor * y = ggml_norm(ctx, x, kLayerNormEps);
    y               = ggml_mul(ctx, y, gamma);
    if (beta != nullptr) {
        y = ggml_add(ctx, y, beta);
    }
    return y;
}

ggml_tensor * linear(ggml_context * ctx, ggml_tensor * x, ggml_tensor * w, ggml_tensor * b) {
    ggml_tensor * y = ggml_mul_mat(ctx, w, x);
    if (b != nullptr) {
        y = ggml_add(ctx, y, b);
    }
    return y;
}

// Conv subsample + conv_out (no PE) for `mel` with ne=[mel_per_chunk,
// n_mels, 1, n] (any n). Returns [d_model, T_per_chunk, n], or nullptr if the
// conv output width does not match T_per_chunk.
ggml_tensor * subsample_group(ggml_context *         ctx,
                              const QwenAsrWeights & weights,
                              int64_t                ds_h,
                              int64_t                T_per_chunk,
                              ggml_tensor *          mel) {
    const int64_t n = mel->ne[3];

    // Reference layout: PyTorch Conv2d on [B, 1, H=n_mels, W=mel_per_chunk].
    // For ggml_conv_2d we use [W=mel_per_chunk, H=n_mels, C=1, N=B] —
    // ggml swaps (W, H) relative to PyTorch, but the 3x3 kernel is
    // symmetric and stride/pad are (2,2)/(1,1), so the arithmetic is
    // invariant. Kernels are stored as ne=[KW=3, KH=3, IC, OC].
    ggml_tensor * x = mel;
    x               = ggml_conv_2d(ctx, weights.enc_subsample.conv0_w, x,
                                   /*s0=*/2, /*s1=*/2, /*p0=*/1, /*p1=*/1, /*d0=*/1, /*d1=*/1);
    x               = add_conv_bias(ctx, x, weights.enc_subsample.conv0_b);
    x               = ggml_gelu_erf(ctx, x);
    x               = ggml_conv_2d(ctx, weights.enc_subsample.conv1_w, x, 2, 2, 1, 1, 1, 1);
    x               = add_conv_bias(ctx, x, weights.enc_subsample.conv1_b);
    x               = ggml_gelu_erf(ctx, x);
    x               = ggml_conv_2d(ctx, weights.enc_subsample.conv2_w, x, 2, 2, 1, 1, 1, 1);
    x               = add_conv_bias(ctx, x, weights.enc_subsample.conv2_b);
    x               = ggml_gelu_erf(ctx, x);
    // Now ne = [W=mel_ds, H=n_mels_ds, C=ds_h, N=n].

    const int64_t W_ds = x->ne[0];
    const int64_t H_ds = x->ne[1];
    if (W_ds != T_per_chunk) {
        log_msg(TRANSCRIBE_LOG_LEVEL_ERROR,
                "qwen3_asr encoder: post-conv W=%lld does not match "
                "per_chunk_aftercnn=%lld",
                static_cast<long long>(W_ds), static_cast<long long>(T_per_chunk));
        return nullptr;
    }

    // Reshape for the linear projection. Reference does:
    //   permute(0, 3, 1, 2).contiguous().view(b, t, c*f)
    // i.e. [B, C, F_ds, T_ds] -> [B, T_ds, C, F_ds] -> [B, T_ds, C*F_ds].
    // The flat inner axis is (c * F_ds + f).
    //
    // In ggml ne layout we have [W=T_ds, H=F_ds, C=ds_h, N=B]. To get a
    // flat axis of size ds_h * F_ds where c is the slower sub-axis and
    // f the faster — matching the reference — we want axes in order
    // [f, c, T, B]: ne = [F_ds, ds_h, T_ds, B]. ggml_permute uses INVERSE
    // semantics vs PyTorch: the i-th argument says which NEW axis old axis
    // i goes to (new[a_i] = old[i]). To mirror PyTorch's `permute(0,3,1,2)`
    // on [B, C, F, T] (equivalently, map [W=13, H=16, C=480, N=11] →
    // [F=16, C=480, T=13, B=11]), we need new[0]=old[1], new[1]=old[2],
    // new[2]=old[0], new[3]=old[3], which is ggml_permute args (2, 0, 1, 3).
    x = ggml_permute(ctx, x, /*a0=*/2, /*a1=*/0, /*a2=*/1, /*a3=*/3);
    x = ggml_cont(ctx, x);
    x = ggml_reshape_3d(ctx, x, H_ds * ds_h, T_per_chunk, n);

    // Linear conv_out: [ds_h*F_ds, d_model] weight; maps to d_model.
    x = ggml_mul_mat(ctx, weights.enc_subsample.conv_out, x);
    return x;  // ne = [d_model, T_per_chunk, n]
}

// subsample_group over every chunk of `mel` (ne[3] = n_chunks), in groups of
// kConvChunkGroup, concatenated back along the chunk axis. Returns
// [d_model, T_per_chunk, n_chunks], or nullptr on a shape mismatch.
ggml_tensor * subsample_chunks(ggml_context *         ctx,
                               const QwenAsrWeights & weights,
                               int64_t                ds_h,
                               int64_t                T_per_chunk,
                               ggml_tensor *          mel) {
    const int64_t n_chunks = mel->ne[3];
    ggml_tensor * out      = nullptr;
    for (int64_t c0 = 0; c0 < n_chunks; c0 += kConvChunkGroup) {
        const int64_t n = std::min<int64_t>(kConvChunkGroup, n_chunks - c0);
        ggml_tensor * g = (n == n_chunks) ? mel :
                                            ggml_view_4d(ctx, mel, mel->ne[0], mel->ne[1], mel->ne[2], n, mel->nb[1],
                                                         mel->nb[2], mel->nb[3], static_cast<size_t>(c0) * mel->nb[3]);
        ggml_tensor * y = subsample_group(ctx, weights, ds_h, T_per_chunk, g);
        if (y == nullptr) {
            return nullptr;
        }
        // Chaining the concat keeps the groups in dependency order, so the
        // allocator retires one group's activations before the next group's.
        out = (out == nullptr) ? y : ggml_concat(ctx, out, y, /*dim=*/2);
    }
    return out;
}

// One encoder block: pre-LN self-attention (bidirectional; `mask` may be
// null for unmasked full attention) + pre-LN GELU FFN. Residuals are
// full 1.0.
ggml_tensor * build_enc_block(ggml_context *          ctx,
                              ggml_tensor *           x,
                              ggml_tensor *           mask,
                              const QwenAsrEncBlock & w,
                              int                     d_model,
                              int                     n_heads,
                              bool                    use_flash) {
    const int     head_dim = d_model / n_heads;
    const float   scale    = 1.0f / std::sqrt(static_cast<float>(head_dim));
    const int64_t T        = x->ne[1];
    // Offline batch: utterances ride ne[2]. B == 1 is the single-shot path
    // and every reshape below collapses to the pre-batch topology exactly.
    const int64_t B        = x->ne[2];

    // ----- Self-attention sub-layer -----
    ggml_tensor * x_norm = layer_norm(ctx, x, w.norm_attn_w, w.norm_attn_b);

    ggml_tensor * q = linear(ctx, x_norm, w.attn_q_w, w.attn_q_b);
    ggml_tensor * k = linear(ctx, x_norm, w.attn_k_w, w.attn_k_b);
    ggml_tensor * v = linear(ctx, x_norm, w.attn_v_w, w.attn_v_b);

    // Split heads: [d_model, T, B] -> [head_dim, n_heads, T, B].
    q = ggml_reshape_4d(ctx, q, head_dim, n_heads, T, B);
    k = ggml_reshape_4d(ctx, k, head_dim, n_heads, T, B);
    v = ggml_reshape_4d(ctx, v, head_dim, n_heads, T, B);

    // Permute to [head_dim, T, n_heads, B] for attention (heads ne[2],
    // batch ne[3] — matches flash_attn_ext / batched soft_max_ext).
    q = ggml_cont(ctx, ggml_permute(ctx, q, 0, 2, 1, 3));
    k = ggml_cont(ctx, ggml_permute(ctx, k, 0, 2, 1, 3));
    v = ggml_cont(ctx, ggml_permute(ctx, v, 0, 2, 1, 3));

    ggml_tensor * o;
    if (use_flash) {
        o = ggml_flash_attn_ext(ctx, q, k, v, mask, scale, /*max_bias=*/0.0f,
                                /*logit_softcap=*/0.0f);
        o = (B == 1) ? ggml_reshape_2d(ctx, o, d_model, T) : ggml_reshape_3d(ctx, o, d_model, T, B);
    } else {
        ggml_tensor * kq      = ggml_mul_mat(ctx, k, q);
        ggml_tensor * kq_soft = ggml_soft_max_ext(ctx, kq, mask, scale, /*max_bias=*/0.0f);
        ggml_tensor * v_t     = ggml_cont(ctx, ggml_permute(ctx, v, 1, 0, 2, 3));
        o                     = ggml_mul_mat(ctx, v_t, kq_soft);
        o                     = ggml_cont(ctx, ggml_permute(ctx, o, 0, 2, 1, 3));
        o                     = (B == 1) ? ggml_reshape_2d(ctx, o, d_model, T) : ggml_reshape_3d(ctx, o, d_model, T, B);
    }

    o = linear(ctx, o, w.attn_out_w, w.attn_out_b);

    x = ggml_add(ctx, x, o);

    // ----- FFN sub-layer -----
    ggml_tensor * y = layer_norm(ctx, x, w.norm_ffn_w, w.norm_ffn_b);
    y               = linear(ctx, y, w.fc1_w, w.fc1_b);
    // PyTorch F.gelu default is the exact erf GELU.
    y               = ggml_gelu_erf(ctx, y);
    y               = linear(ctx, y, w.fc2_w, w.fc2_b);

    return ggml_add(ctx, x, y);
}

}  // namespace

SubsampleBuild build_subsample_graph(ggml_context *         ctx,
                                     const QwenAsrWeights & weights,
                                     const QwenAsrHParams & hp,
                                     int                    n_chunks) {
    SubsampleBuild sb{};
    if (ctx == nullptr || n_chunks <= 0) {
        log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, "qwen3_asr subsample: invalid arg (ctx=%p, n_chunks=%d)",
                static_cast<void *>(ctx), n_chunks);
        return sb;
    }

    const int64_t mel_per_chunk = hp.enc_n_window * 2;
    const int64_t T_per_chunk   = aftercnn_len(static_cast<int32_t>(mel_per_chunk));

    sb.mel_in = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, mel_per_chunk, hp.enc_num_mel_bins, 1, n_chunks);
    named(sb.mel_in, "enc.mel_in");
    ggml_set_input(sb.mel_in);

    sb.out = subsample_chunks(ctx, weights, hp.enc_downsample_hidden, T_per_chunk, sb.mel_in);
    if (sb.out == nullptr) {
        return sb;
    }
    named(sb.out, "enc.subsample.pass");
    ggml_set_output(sb.out);

    // ~36 nodes per group (3 convs + GELUs + projection + concat).
    const int n_groups = (n_chunks + kConvChunkGroup - 1) / kConvChunkGroup;
    sb.graph           = ggml_new_graph_custom(ctx, /*size=*/64 + 48 * n_groups, /*grads=*/false);
    if (sb.graph == nullptr) {
        log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, "qwen3_asr subsample: ggml_new_graph_custom failed");
        return sb;
    }
    ggml_build_forward_expand(sb.graph, sb.out);
    return sb;
}

EncoderBuild build_encoder_graph(ggml_context *         ctx,
                                 const QwenAsrWeights & weights,
                                 const QwenAsrHParams & hp,
                                 const EncoderTiming &  timing,
                                 bool                   use_flash) {
    EncoderBuild eb{};
    eb.timing = timing;

    if (ctx == nullptr || timing.n_chunks <= 0) {
        log_msg(TRANSCRIBE_LOG_LEVEL_ERROR,
                "qwen3_asr encoder: invalid arg "
                "(ctx=%p, n_chunks=%d)",
                static_cast<void *>(ctx), timing.n_chunks);
        return eb;
    }

    // Ragged-tail note: when the last chunk has fewer real mel frames
    // than mel_per_chunk, the batched conv still emits per_chunk_aftercnn
    // frames for it (the tail is zero-padded in the mel-input packer).
    // The reference ragged-selects only the real aftercnn frames via
    //   `hidden_states = padded_embed[padded_mask_after_cnn]`
    // before the 18 encoder blocks. We mirror that: reshape the per-
    // chunk conv output to [d_model, T_enc_padded], then VIEW the first
    // T_enc rows — dropping the trailing padded rows of the last chunk
    // — so the encoder blocks, the attention mask, and every downstream
    // shape see T_enc (not T_enc_padded). This matches the reference
    // byte-for-byte at the pos_add.out boundary and upward.

    const int64_t d_model      = hp.enc_d_model;
    const int64_t n_heads      = hp.enc_n_heads;
    const int64_t n_chunks     = timing.n_chunks;
    const int64_t T_per_chunk  = timing.per_chunk_aftercnn;
    const int64_t T_enc_padded = timing.T_enc_padded;
    const int64_t T_enc        = timing.T_enc;

    // ----- Graph inputs -----
    // Conv subsample output [d_model, T_per_chunk, n_chunks], computed by
    // build_subsample_graph (pre-PE).
    eb.subsample_in = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, d_model, T_per_chunk, n_chunks);
    named(eb.subsample_in, "enc.subsample.out");
    ggml_set_input(eb.subsample_in);
    eb.dumps.subsample_out = eb.subsample_in;
    transcribe::debug::mark_tensor_for_dump(eb.subsample_in);

    eb.pos_emb_in = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, d_model, T_per_chunk);
    named(eb.pos_emb_in, "enc.pos_emb.in");
    ggml_set_input(eb.pos_emb_in);

    // Add sinusoidal PE (broadcast across n_chunks). pos_emb_in has
    // ne=[d_model, T_per_chunk]; ggml_add broadcasts missing axes.
    ggml_tensor * x = ggml_add(ctx, eb.subsample_in, eb.pos_emb_in);

    // Flatten batch: [d_model, T_per_chunk, n_chunks] -> [d_model, T_enc_padded].
    // Row-major semantics: position r in the flattened sequence is
    // chunk = r / T_per_chunk, pos_in_chunk = r % T_per_chunk, which
    // matches pad_sequence(..., batch_first=True)[mask].reshape() when
    // every chunk has exactly T_per_chunk real frames.
    //
    // ggml_reshape_2d requires the input to be contiguous. The ggml_add
    // result is a freshly-allocated tensor so it IS contiguous in the
    // usual sense, BUT at graph-build time ggml's contiguity check
    // cares about strides, not allocation — pending ggml_cont ensures
    // strides match ne without relying on scheduler behaviour.
    x = ggml_cont(ctx, x);
    // Mark the contiguous (pre-reshape) tensor as an output before
    // reshape. The reshape is a view on this buffer; without pinning
    // the source, the scheduler is free to recycle its memory after
    // the downstream blocks consume it, and later dumps read stale
    // data.
    transcribe::debug::mark_tensor_for_dump(x);
    x = ggml_reshape_2d(ctx, x, d_model, T_enc_padded);

    // Drop padded aftercnn rows before encoder blocks. Matches
    // reference's `padded_embed[padded_mask_after_cnn]` step — without
    // this, the 18 blocks see the full T_enc_padded and the attention
    // pattern diverges from ref on any utterance whose mel length is
    // not a multiple of `n_window*2` (nearly all LibriSpeech).
    if (T_enc < T_enc_padded) {
        x = ggml_view_2d(ctx, x, d_model, T_enc, x->nb[1], 0);
        x = ggml_cont(ctx, x);
    }

    named(x, "enc.pos_add.out");
    eb.dumps.pos_add_out = x;
    transcribe::debug::mark_tensor_for_dump(x);

    // ----- 18 encoder blocks (unmasked full attention, see encoder.h) -----
    const int n_layers = static_cast<int>(weights.enc_blocks.size());
    for (int i = 0; i < n_layers; ++i) {
        x = build_enc_block(ctx, x, /*mask=*/nullptr, weights.enc_blocks[i], static_cast<int>(d_model),
                            static_cast<int>(n_heads), use_flash);
        if (i == 0) {
            named(x, "enc.block.0.out");
            eb.dumps.block_0_out = x;
            transcribe::debug::mark_tensor_for_dump(x);
        } else if (i == n_layers - 1) {
            char bname[64];
            std::snprintf(bname, sizeof(bname), "enc.block.%d.out", i);
            named(x, bname);
            eb.dumps.block_last_out = x;
            transcribe::debug::mark_tensor_for_dump(x);
        }
    }

    // ----- Post head: LN -> proj1 -> GELU -> proj2 -----
    x = layer_norm(ctx, x, weights.enc_head.ln_post_w, weights.enc_head.ln_post_b);
    named(x, "enc.ln_post.out");
    eb.dumps.ln_post_out = x;
    transcribe::debug::mark_tensor_for_dump(x);

    x = linear(ctx, x, weights.enc_head.proj1_w, weights.enc_head.proj1_b);
    x = ggml_gelu_erf(ctx, x);
    x = linear(ctx, x, weights.enc_head.proj2_w, weights.enc_head.proj2_b);
    named(x, "enc.proj.out");
    eb.dumps.proj_out = x;
    transcribe::debug::mark_tensor_for_dump(x);

    eb.out = x;
    ggml_set_output(eb.out);

    // 18 blocks * ~20 ops + subsample + head => ~450 graph nodes. 8192
    // leaves headroom for debug-name nodes.
    eb.graph = ggml_new_graph_custom(ctx, /*size=*/8192, /*grads=*/false);
    if (eb.graph == nullptr) {
        log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, "qwen3_asr encoder: ggml_new_graph_custom failed");
        return eb;
    }
    ggml_build_forward_expand(eb.graph, eb.out);

    // Force evaluation of non-output debug tensors so they survive the
    // scheduler's live-range packing and are readable after compute.
    if (eb.dumps.subsample_out) {
        ggml_build_forward_expand(eb.graph, eb.dumps.subsample_out);
    }
    if (eb.dumps.pos_add_out) {
        ggml_build_forward_expand(eb.graph, eb.dumps.pos_add_out);
    }
    if (eb.dumps.block_0_out) {
        ggml_build_forward_expand(eb.graph, eb.dumps.block_0_out);
    }
    if (eb.dumps.block_last_out) {
        ggml_build_forward_expand(eb.graph, eb.dumps.block_last_out);
    }
    if (eb.dumps.ln_post_out) {
        ggml_build_forward_expand(eb.graph, eb.dumps.ln_post_out);
    }
    if (eb.dumps.proj_out) {
        ggml_build_forward_expand(eb.graph, eb.dumps.proj_out);
    }

    return eb;
}

EncoderBuildBatched build_encoder_graph_batched(ggml_context *         ctx,
                                                const QwenAsrWeights & weights,
                                                const QwenAsrHParams & hp,
                                                int                    n_chunks_max,
                                                int                    n_batch,
                                                bool                   use_flash) {
    EncoderBuildBatched eb{};
    if (ctx == nullptr || n_chunks_max <= 0 || n_batch <= 0) {
        log_msg(TRANSCRIBE_LOG_LEVEL_ERROR,
                "qwen3_asr encoder(batched): invalid arg "
                "(n_chunks_max=%d, n_batch=%d)",
                n_chunks_max, n_batch);
        return eb;
    }

    const int64_t d_model     = hp.enc_d_model;
    const int64_t n_heads     = hp.enc_n_heads;
    const int64_t T_per_chunk = aftercnn_len(hp.enc_n_window * 2);
    const int64_t B           = n_batch;
    const int64_t N           = B * n_chunks_max;  // packed chunk axis
    const int64_t T_pad_max   = n_chunks_max * T_per_chunk;

    eb.n_batch      = n_batch;
    eb.n_chunks_max = n_chunks_max;
    eb.T_per_chunk  = static_cast<int>(T_per_chunk);
    eb.T_pad_max    = static_cast<int>(T_pad_max);

    // ----- Graph inputs -----
    // Conv subsample output for all N packed chunks (build_subsample_graph).
    eb.subsample_in = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, d_model, T_per_chunk, N);
    named(eb.subsample_in, "enc.subsample.out");
    ggml_set_input(eb.subsample_in);

    eb.pos_emb_in = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, d_model, T_per_chunk);
    named(eb.pos_emb_in, "enc.pos_emb.in");
    ggml_set_input(eb.pos_emb_in);

    // Key-pad mask: per-utterance [T_pad_max, T_pad_max, 1, B]. Built host-side.
    eb.mask_in = ggml_new_tensor_4d(ctx, use_flash ? GGML_TYPE_F16 : GGML_TYPE_F32, T_pad_max, T_pad_max, 1, B);
    named(eb.mask_in, "enc.attn_mask.in");
    ggml_set_input(eb.mask_in);

    // Add sinusoidal PE (broadcast across the N = B*n_chunks_max axis).
    ggml_tensor * x = ggml_add(ctx, eb.subsample_in, eb.pos_emb_in);

    // Reorganise N=(b*n_chunks_max + c) -> [d_model, T_per_chunk, n_chunks_max, B],
    // then flatten (T_per_chunk, n_chunks_max) chunk-major into the sequence:
    // row r = chunk*T_per_chunk + pos. Result [d_model, T_pad_max, B].
    x = ggml_cont(ctx, x);
    x = ggml_reshape_4d(ctx, x, d_model, T_per_chunk, n_chunks_max, B);
    x = ggml_reshape_3d(ctx, x, d_model, T_pad_max, B);

    // ----- 18 encoder blocks (batch on ne[2], per-utterance key-pad mask) -----
    const int n_layers = static_cast<int>(weights.enc_blocks.size());
    for (int i = 0; i < n_layers; ++i) {
        x = build_enc_block(ctx, x, eb.mask_in, weights.enc_blocks[i], static_cast<int>(d_model),
                            static_cast<int>(n_heads), use_flash);
    }

    // ----- Post head: LN -> proj1 -> GELU -> proj2 -----
    x = layer_norm(ctx, x, weights.enc_head.ln_post_w, weights.enc_head.ln_post_b);
    x = linear(ctx, x, weights.enc_head.proj1_w, weights.enc_head.proj1_b);
    x = ggml_gelu_erf(ctx, x);
    x = linear(ctx, x, weights.enc_head.proj2_w, weights.enc_head.proj2_b);
    named(x, "enc.proj.out");

    eb.out = x;
    ggml_set_output(eb.out);

    eb.graph = ggml_new_graph_custom(ctx, /*size=*/8192, /*grads=*/false);
    if (eb.graph == nullptr) {
        log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, "qwen3_asr encoder(batched): ggml_new_graph_custom failed");
        return eb;
    }
    ggml_build_forward_expand(eb.graph, eb.out);
    return eb;
}

}  // namespace transcribe::qwen3_asr