batch API
maai.batch
Offline batch inference for MaAI models over recorded audio.
:class:~maai.model.Maai is built for live audio: it runs the audio encoder and
the transformer once per frame, so scoring a recording that is already on disk
costs one pair of forward passes per frame. :class:MaaiBatch scores whole
recordings instead:
-
The audio encoder runs over long chunks (
encoder_chunk_framesframes per call) and over several recordings at once, carrying its streaming state from chunk to chunk, so the features are those of the frame-by-frame stream. -
Mimi (
model_type="normal-ver2"): the resampler, convolution padding and transformer KV cache are carried over. -
CPC (
model_type="normal"): the realtime path encodes each frame together with 320 samples of overlap and drops the outputs at the window edges, so only the valid region of the convolutions is used; a window of several frames gives the same outputs. The LSTM state is carried over, and each model's downsampling layer runs window by window as in realtime. -
Each model's transformer runs over the whole sequence in one forward pass. The
context_limitband mask makes this the same computation as the realtime KV cache, which is trimmed to the lastcontext_len_secseconds. Long inputs are folded into overlapping windows that are processed as one batch (seefold_context_frames). - With several models, the audio is encoded once and the features are shared
(the encoder core is the same for all of them, as in
MaaiMultiple).
Timing follows the realtime Maai. With Mimi, the realtime default
(use_mimi_onnx=True) drops the first encoder output after a reset, so the
first prediction is made after two frames of audio (see drop_first_frame);
with CPC, after one frame. Small numerical differences remain (ONNX Runtime vs
PyTorch for the Mimi default, and convolution algorithms).
MaaiBatch
Batch (offline) inference over whole recordings.
Inputs are 16 kHz mono float32 waveforms, one per speaker. Results are
numpy arrays with one row per frame, holding the same values that
Maai.get_result() would return frame by frame.
Example
batch = MaaiBatch("vap", "jp", device="cuda") out = batch.process(wav_ch1, wav_ch2) out["p_now"].shape # (n_frames, 2) out["time_sec"][:3] # [0.16, 0.24, 0.32] outs = batch.process_many([(a1, a2), (b1, b2), (c1, c2)])
Several models share one encoding pass when given as configs; results
are then nested by label (default: the mode name)::
>>> batch = MaaiBatch(configs=[{"mode": "vap", "lang": "jp"},
... {"mode": "bc", "lang": "jp"}])
>>> out = batch.process(wav_ch1, wav_ch2)
>>> out["vap"]["p_now"], out["bc"]["p_bc"]
Both encoders are supported: Mimi (model_type="normal-ver2", the
default, at 12.5 Hz) and CPC (model_type="normal", 10 Hz by default).
vap_prompt is not supported.
Source code in src/maai/batch.py
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 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | |
__init__(mode=None, lang=None, *, configs=None, frame_rate=None, context_len_sec=20, device='cpu', model_type='normal-ver2', mono=False, mc=True, local_model=None, return_p_bins=False, cpc_model=os.path.expanduser('~/.cache/cpc/60k_epoch4-d0f474de.pt'), mimi_model_name='kyutai/mimi', cache_dir=None, force_download=False, batch_size=8, encoder_chunk_frames=50, fold_context_frames=None, fold_step_frames=None, drop_first_frame=None)
Load the model(s) and the shared audio encoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str | None
|
Model to run ('vap', 'vad', 'bc_det', 'bc',
'bc_2type', 'nod', 'nod_timing', 'nod_para'). Give either
|
None
|
lang
|
str | None
|
Language of the model (e.g. 'jp', 'en'). |
None
|
configs
|
list | None
|
Several models sharing one encoding pass. Each entry is a dict with 'mode' and 'lang' and optionally 'mc', 'local_model', 'return_p_bins' and 'label' (the result key; defaults to the mode). |
None
|
frame_rate
|
float | None
|
Frame rate of the models. Default: 12.5 for Mimi (the only rate it supports) and 10 for CPC. |
None
|
context_len_sec
|
float
|
Context length of the models in seconds. |
20
|
device
|
str
|
'cpu', 'cuda' or 'cuda:N'. |
'cpu'
|
model_type
|
str
|
'normal-ver2' (Mimi encoder) or 'normal' (CPC). |
'normal-ver2'
|
mono
|
bool
|
Single-channel input (only |
False
|
mc
|
bool
|
Prefer the noise-robust 'vap' model (default for configs). |
True
|
local_model
|
str | None
|
Local checkpoint for |
None
|
return_p_bins
|
bool
|
Also return |
False
|
cpc_model
|
str
|
Path of the pretrained CPC weights (CPC only). |
expanduser('~/.cache/cpc/60k_epoch4-d0f474de.pt')
|
mimi_model_name
|
str
|
Hugging Face name of the Mimi model. |
'kyutai/mimi'
|
cache_dir
|
str | None
|
Cache directory for the model weights. |
None
|
force_download
|
bool
|
Force re-downloading the model weights. |
False
|
batch_size
|
int
|
Recordings encoded together, and windows per transformer forward. Lower it if memory runs out. |
8
|
encoder_chunk_frames
|
int
|
Frames per encoder call (4 s at 12.5 Hz). Memory grows with it; the result does not depend on it. |
50
|
fold_context_frames
|
int | None
|
Inputs longer than
|
None
|
fold_step_frames
|
int | None
|
New frames per window. Default: twice the context length. |
None
|
drop_first_frame
|
bool | None
|
Drop the first encoder output like
the realtime ONNX Mimi encoder does. Default: True for Mimi
(set False to match |
None
|
Source code in src/maai/batch.py
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 | |
process(audio_ch1, audio_ch2=None)
Run the model(s) over one recording.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_ch1
|
16 kHz mono float32 waveform of speaker 1 (np.ndarray). |
required | |
audio_ch2
|
Waveform of speaker 2 (omit with |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
|
dict
|
input position the prediction is made at (the end of the audio it |
|
dict
|
has seen). The model outputs follow as arrays with one row per |
|
dict
|
frame, e.g. |
|
dict
|
are nested under each label. An incomplete trailing frame is not |
|
dict
|
scored, as in the realtime stream. |
Source code in src/maai/batch.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
process_many(items)
Run the model(s) over several recordings, batch_size at a time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
One result per item, in input order (see :meth: |
Source code in src/maai/batch.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
to_frames(result)
staticmethod
Convert a :meth:process result to one dict per frame.
The dicts hold the same keys as Maai.get_result() (without t,
x1 and x2) plus end_sample and time_sec.
Source code in src/maai/batch.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |