File size: 29,157 Bytes
a7c2243 | 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 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 | # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
from typing import Optional
import pytest
import torch
import torch.nn.functional as F
from omegaconf import open_dict
from tqdm.auto import tqdm
from nemo.collections.asr.models import ASRModel
from nemo.collections.asr.parts.context_biasing.biasing_multi_model import BiasingRequestItemConfig
from nemo.collections.asr.parts.context_biasing.boosting_graph_batched import BoostingTreeModelConfig
from nemo.collections.asr.parts.submodules.transducer_decoding.label_looping_base import (
BatchedLabelLoopingState,
GreedyBatchedLabelLoopingComputerBase,
)
from nemo.collections.asr.parts.utils.manifest_utils import read_manifest
from nemo.collections.asr.parts.utils.rnnt_utils import BatchedHyps, Hypothesis, batched_hyps_to_hypotheses
from tests.collections.asr.decoding.utils import load_audio, make_preprocessor_deterministic
def get_devices_for_testing(use_cpu_always: bool = False) -> list[torch.device]:
devices = [torch.device("cpu")] if use_cpu_always else []
if torch.cuda.is_available():
devices.append(torch.device("cuda:0"))
if torch.mps.is_available():
devices.append(torch.device("mps"))
if len(devices) == 0:
# no fast device for testing, add CPU
devices.append(torch.device("cpu"))
return devices
DEVICES = get_devices_for_testing(use_cpu_always=False)
def get_model_encoder_output(
test_audio_filenames,
num_samples: int,
model: ASRModel,
device: torch.device = torch.device("cpu"),
dtype: torch.dtype = torch.float32,
):
audio_filepaths = test_audio_filenames[:num_samples]
with torch.no_grad():
make_preprocessor_deterministic(model)
model.eval()
all_inputs, all_lengths = [], []
for audio_file in tqdm(audio_filepaths, desc="Loading audio files"):
audio_tensor, _ = load_audio(audio_file)
all_inputs.append(audio_tensor)
all_lengths.append(torch.tensor(audio_tensor.shape[0], dtype=torch.int64))
input_batch = torch.nn.utils.rnn.pad_sequence(all_inputs, batch_first=True).to(device=device, dtype=dtype)
length_batch = torch.tensor(all_lengths, dtype=torch.int64).to(device)
encoded_outputs, encoded_length = model(input_signal=input_batch, input_signal_length=length_batch)
return encoded_outputs, encoded_length
def get_batch_encoder_outputs_from_records(records, model, device):
"""Helper function to get encoder outputs for a batch of manifest records"""
filenames = [record["audio_filepath"] for record in records]
local_batch_size = len(filenames)
encoder_output, encoder_output_len = get_model_encoder_output(
test_audio_filenames=filenames, model=model, num_samples=local_batch_size, device=device
)
return encoder_output, encoder_output_len
@pytest.mark.with_downloads
@pytest.mark.parametrize(
"device,use_cuda_graph_decoder",
[(device, False) for device in DEVICES] + [(device, True) for device in DEVICES if device.type == "cuda"],
)
@pytest.mark.parametrize("is_tdt", [False, True])
@pytest.mark.parametrize("chunk_size", [1, 3])
@pytest.mark.parametrize("batch_size", [4])
@pytest.mark.parametrize("max_symbols", [10])
def test_label_looping_streaming_batched_state(
tmp_path_factory,
an4_val_manifest_corrected,
stt_en_fastconformer_transducer_large,
stt_en_fastconformer_tdt_large,
device: torch.device,
use_cuda_graph_decoder: bool,
is_tdt: bool,
chunk_size: int,
batch_size: int,
max_symbols: int,
):
"""Test streaming decoding with batched state"""
model = stt_en_fastconformer_tdt_large if is_tdt else stt_en_fastconformer_transducer_large
model.eval()
model.to(device=device)
decoding_cfg = copy.deepcopy(model.cfg.decoding)
decoding_cfg.strategy = "greedy_batch"
with open_dict(decoding_cfg):
decoding_cfg.greedy.use_cuda_graph_decoder = use_cuda_graph_decoder
decoding_cfg.greedy.max_symbols = max_symbols
model.change_decoding_strategy(decoding_cfg)
manifest = read_manifest(an4_val_manifest_corrected)
transcriptions = model.transcribe(audio=str(an4_val_manifest_corrected.absolute()), batch_size=batch_size)
ref_transcripts = [hyp.text for hyp in transcriptions]
all_hyps = []
decoding_computer: GreedyBatchedLabelLoopingComputerBase = model.decoding.decoding.decoding_computer
with torch.no_grad(), torch.inference_mode():
for i in range(0, len(manifest), batch_size):
encoder_output, encoder_output_len = get_batch_encoder_outputs_from_records(
manifest[i : i + batch_size], model=model, device=device
)
local_batch_size = encoder_output_len.shape[0]
# decode encoder output by chunks, passing state between decoder invocations
state: Optional[BatchedLabelLoopingState] = None
batched_hyps: BatchedHyps | None = None
encoder_output = encoder_output.transpose(1, 2)
for t in range(0, encoder_output.shape[1], chunk_size):
rest_len = encoder_output_len - t
current_len = torch.full_like(encoder_output_len, fill_value=chunk_size)
current_len = torch.minimum(current_len, rest_len)
current_len = torch.maximum(current_len, torch.zeros_like(current_len))
batched_hyps_chunk, _, state = decoding_computer(
x=encoder_output[:, t : t + chunk_size],
out_len=current_len,
prev_batched_state=state,
)
if batched_hyps is None:
batched_hyps = batched_hyps_chunk
else:
batched_hyps.merge_(batched_hyps_chunk)
assert batched_hyps is not None
all_hyps.extend(batched_hyps_to_hypotheses(batched_hyps, None, batch_size=local_batch_size))
streaming_transcripts = []
for hyp in all_hyps:
streaming_transcripts.append(model.tokenizer.ids_to_text(hyp.y_sequence.tolist()))
assert ref_transcripts == streaming_transcripts
@pytest.mark.with_downloads
@pytest.mark.parametrize(
"device,use_cuda_graph_decoder",
[(device, False) for device in DEVICES] + [(device, True) for device in DEVICES if device.type == "cuda"],
)
@pytest.mark.parametrize("is_tdt", [False, True])
@pytest.mark.parametrize("chunk_size", [1, 3])
@pytest.mark.parametrize("batch_size", [4])
@pytest.mark.parametrize("max_symbols", [10])
def test_label_looping_streaming_partial_hypotheses(
tmp_path_factory,
an4_val_manifest_corrected,
stt_en_fastconformer_transducer_large,
stt_en_fastconformer_tdt_large,
device: torch.device,
use_cuda_graph_decoder: bool,
is_tdt: bool,
chunk_size: int,
batch_size: int,
max_symbols: int,
):
"""Test streaming decoding with partial hypotheses"""
model = stt_en_fastconformer_tdt_large if is_tdt else stt_en_fastconformer_transducer_large
model.eval()
model.to(device=device)
decoding_cfg = copy.deepcopy(model.cfg.decoding)
decoding_cfg.strategy = "greedy_batch"
with open_dict(decoding_cfg):
decoding_cfg.greedy.use_cuda_graph_decoder = use_cuda_graph_decoder
decoding_cfg.greedy.max_symbols = max_symbols
model.change_decoding_strategy(decoding_cfg)
manifest = read_manifest(an4_val_manifest_corrected)
transcriptions = model.transcribe(audio=str(an4_val_manifest_corrected.absolute()), batch_size=batch_size)
ref_transcripts = [hyp.text for hyp in transcriptions]
all_hyps = []
rnnt_infer = model.decoding.decoding
with torch.no_grad(), torch.inference_mode():
for i in range(0, len(manifest), batch_size):
encoder_output, encoder_output_len = get_batch_encoder_outputs_from_records(
manifest[i : i + batch_size], model=model, device=device
)
# decode encoder output by chunks, passing state between decoder invocations
hyps: list[Hypothesis] | None = None
for t in range(0, encoder_output.shape[2], chunk_size):
rest_len = encoder_output_len - t
current_len = torch.full_like(encoder_output_len, fill_value=chunk_size)
current_len = torch.minimum(current_len, rest_len)
current_len = torch.maximum(current_len, torch.zeros_like(current_len))
(hyps,) = rnnt_infer(
encoder_output=encoder_output[:, :, t : t + chunk_size],
encoded_lengths=current_len,
partial_hypotheses=hyps,
)
# free up memory by resetting decoding state
for hyp in hyps:
hyp.clean_decoding_state_()
all_hyps.extend(hyps)
streaming_transcripts = []
for hyp in all_hyps:
streaming_transcripts.append(model.tokenizer.ids_to_text(hyp.y_sequence.tolist()))
assert ref_transcripts == streaming_transcripts
@pytest.mark.with_downloads
@pytest.mark.parametrize(
"device,use_cuda_graph_decoder",
[(device, False) for device in DEVICES] + [(device, True) for device in DEVICES if device.type == "cuda"],
)
@pytest.mark.parametrize("is_tdt", [False, True])
@pytest.mark.parametrize("chunk_size", [1, 3])
@pytest.mark.parametrize("batch_size", [4])
@pytest.mark.parametrize("max_symbols", [10])
def test_label_looping_continuous_streaming_batched_state(
tmp_path_factory,
an4_val_manifest_corrected,
stt_en_fastconformer_transducer_large,
stt_en_fastconformer_tdt_large,
device: torch.device,
use_cuda_graph_decoder: bool,
is_tdt: bool,
chunk_size: int,
batch_size: int,
max_symbols: int,
):
"""Test streaming continuos decoding with partial hypotheses"""
model = stt_en_fastconformer_tdt_large if is_tdt else stt_en_fastconformer_transducer_large
model.eval()
model.to(device=device)
decoding_cfg = copy.deepcopy(model.cfg.decoding)
decoding_cfg.strategy = "greedy_batch"
with open_dict(decoding_cfg):
decoding_cfg.greedy.use_cuda_graph_decoder = use_cuda_graph_decoder
decoding_cfg.greedy.max_symbols = max_symbols
model.change_decoding_strategy(decoding_cfg)
manifest = read_manifest(an4_val_manifest_corrected)
transcriptions = model.transcribe(audio=str(an4_val_manifest_corrected.absolute()), batch_size=batch_size)
ref_transcripts = [hyp.text for hyp in transcriptions]
all_hyps = [None for _ in range(len(manifest))]
decoding_computer: GreedyBatchedLabelLoopingComputerBase = model.decoding.decoding.decoding_computer
assert batch_size < len(
manifest
), "Batch size should be less than the number of records in the manifest for continuous streaming test."
with torch.no_grad(), torch.inference_mode():
# get first 2 batches
encoder_output, encoder_output_len = get_batch_encoder_outputs_from_records(
manifest[:batch_size], model=model, device=device
)
encoder_output_next, encoder_output_len_next = get_batch_encoder_outputs_from_records(
manifest[batch_size : batch_size + batch_size], model=model, device=device
)
# we always work with encoder_output, getting next utterances from encoder_output_next
# so we need to pad encoder_output if it is shorter than encoder_output_next
if encoder_output.shape[2] < encoder_output_next.shape[2]:
encoder_output = F.pad(encoder_output, (0, encoder_output_next.shape[2] - encoder_output.shape[2]))
expanded_batch_indices = torch.arange(batch_size, device=device).unsqueeze(1).expand(-1, chunk_size)
next_batch_i = 0
next_batch_global_i = batch_size
next_query_utterance_i = batch_size + batch_size
has_next = True # if we have anything in next batch to decode
hyps: list[Hypothesis | None] = [None for _ in range(batch_size)]
hyps_global_indices = list(range(batch_size))
encoder_output_t = torch.zeros_like(encoder_output_len)
state = None # decoding state
# while there is something to decode
while ((rest_len := encoder_output_len - encoder_output_t) > 0).any():
frame_indices = encoder_output_t[:, None] + torch.arange(chunk_size, device=device)[None, :]
frame_indices = torch.minimum(frame_indices, encoder_output_len[:, None] - 1)
current_len = torch.full_like(encoder_output_len, fill_value=chunk_size)
current_len = torch.minimum(current_len, rest_len)
encoder_frames = encoder_output[expanded_batch_indices, :, frame_indices]
batched_hyps, _, state = decoding_computer(
x=encoder_frames,
out_len=current_len,
prev_batched_state=state,
)
hyps_continuations = batched_hyps_to_hypotheses(batched_hyps, None, batch_size=batch_size)
for i, (hyp, hyp_continuation) in enumerate(zip(hyps, hyps_continuations)):
if hyp is None:
hyps[i] = hyp_continuation
else:
hyp.merge_(hyp_continuation)
encoder_output_t += current_len
rest_len -= current_len
decoding_computer.reset_state_by_mask(state, rest_len == 0)
finished_decoding_indices = torch.nonzero(rest_len == 0, as_tuple=True)[0].cpu().tolist()
for idx in finished_decoding_indices:
hyp = hyps[idx]
if all_hyps[hyps_global_indices[idx]] is None:
all_hyps[hyps_global_indices[idx]] = hyp
hyps[idx] = None # reset to None
if has_next:
# get next utterance to decode for finished hypothesis
encoder_output[idx] = encoder_output_next[next_batch_i]
encoder_output_len[idx] = encoder_output_len_next[next_batch_i]
hyps_global_indices[idx] = next_batch_global_i
encoder_output_t[idx] = 0
next_batch_i += 1
next_batch_global_i += 1
# if next_batch_i is out of bounds, get next batch of encoder outputs
if next_batch_i >= encoder_output_len_next.shape[0]:
if next_query_utterance_i < len(manifest):
encoder_output_next, encoder_output_len_next = get_batch_encoder_outputs_from_records(
manifest[next_query_utterance_i : next_query_utterance_i + batch_size],
model=model,
device=device,
)
# pad if needed to allow futher assignment of encoder_output_next to encoder_output
if encoder_output.shape[2] < encoder_output_next.shape[2]:
encoder_output = F.pad(
encoder_output, (0, encoder_output_next.shape[2] - encoder_output.shape[2])
)
next_batch_i = 0
next_query_utterance_i += batch_size
else:
has_next = False
streaming_transcripts = []
for hyp in all_hyps:
streaming_transcripts.append(model.tokenizer.ids_to_text(hyp.y_sequence.tolist()))
assert ref_transcripts == streaming_transcripts
@pytest.mark.with_downloads
@pytest.mark.parametrize(
"device,use_cuda_graph_decoder",
[(device, False) for device in DEVICES] + [(device, True) for device in DEVICES if device.type == "cuda"],
)
@pytest.mark.parametrize("is_tdt", [False, True])
@pytest.mark.parametrize("chunk_size", [1, 3])
@pytest.mark.parametrize("batch_size", [4])
@pytest.mark.parametrize("max_symbols", [10])
def test_label_looping_continuous_streaming_partial_hypotheses(
tmp_path_factory,
an4_val_manifest_corrected,
stt_en_fastconformer_transducer_large,
stt_en_fastconformer_tdt_large,
device: torch.device,
use_cuda_graph_decoder: bool,
is_tdt: bool,
chunk_size: int,
batch_size: int,
max_symbols: int,
):
"""Test streaming continuos decoding with partial hypotheses"""
model = stt_en_fastconformer_tdt_large if is_tdt else stt_en_fastconformer_transducer_large
model.to(device=device)
decoding_cfg = copy.deepcopy(model.cfg.decoding)
decoding_cfg.strategy = "greedy_batch"
with open_dict(decoding_cfg):
decoding_cfg.greedy.use_cuda_graph_decoder = use_cuda_graph_decoder
decoding_cfg.greedy.max_symbols = max_symbols
model.change_decoding_strategy(decoding_cfg)
manifest = read_manifest(an4_val_manifest_corrected)
transcriptions = model.transcribe(audio=str(an4_val_manifest_corrected.absolute()), batch_size=batch_size)
ref_transcripts = [hyp.text for hyp in transcriptions]
all_hyps = [None for _ in range(len(manifest))]
rnnt_infer = model.decoding.decoding
assert batch_size < len(
manifest
), "Batch size should be less than the number of records in the manifest for continuous streaming test."
with torch.no_grad(), torch.inference_mode():
# get first 2 batches
encoder_output, encoder_output_len = get_batch_encoder_outputs_from_records(
manifest[:batch_size], model=model, device=device
)
encoder_output_next, encoder_output_len_next = get_batch_encoder_outputs_from_records(
manifest[batch_size : batch_size + batch_size], model=model, device=device
)
# we always work with encoder_output, getting next utterances from encoder_output_next
# so we need to pad encoder_output if it is shorter than encoder_output_next
if encoder_output.shape[2] < encoder_output_next.shape[2]:
encoder_output = F.pad(encoder_output, (0, encoder_output_next.shape[2] - encoder_output.shape[2]))
expanded_batch_indices = torch.arange(batch_size, device=device).unsqueeze(1).expand(-1, chunk_size)
# NB: we assume that encoder_output_len and encoder_output_len_next
# have no zero elements (no empty utterances), and we do not check this condition further
next_batch_i = 0
next_batch_global_i = batch_size
next_query_utterance_i = batch_size + batch_size
has_next = True # if we have anything in next batch to decode
hyps: list[Hypothesis | None] = [None for _ in range(batch_size)]
hyps_global_indices = list(range(batch_size))
encoder_output_t = torch.zeros_like(encoder_output_len)
# while there is something to decode
while ((rest_len := encoder_output_len - encoder_output_t) > 0).any():
frame_indices = encoder_output_t[:, None] + torch.arange(chunk_size, device=device)[None, :]
frame_indices = torch.minimum(frame_indices, encoder_output_len[:, None] - 1)
current_len = torch.full_like(encoder_output_len, fill_value=chunk_size)
current_len = torch.minimum(current_len, rest_len)
encoder_frames = encoder_output[expanded_batch_indices, :, frame_indices].transpose(1, 2)
(hyps,) = rnnt_infer(
encoder_output=encoder_frames,
encoded_lengths=current_len,
partial_hypotheses=hyps,
)
encoder_output_t += current_len
rest_len -= current_len
finished_decoding_indices = torch.nonzero(rest_len == 0, as_tuple=True)[0].cpu().tolist()
for idx in finished_decoding_indices:
hyp = hyps[idx]
all_hyps[hyps_global_indices[idx]] = hyp
# NB: we clean decoding state and set hyp to None only if we have next utterances to decode
# otherwise for each decoder invocation with 0 length it will recreate the hypothesis object,
# which is computationally expensive
# decoding current hyp with 0 length will not change the hypothesis
if has_next:
hyp.clean_decoding_state_()
hyps[idx] = None # reset to None
# get next utterance to decode for finished hypothesis
encoder_output[idx] = encoder_output_next[next_batch_i]
encoder_output_len[idx] = encoder_output_len_next[next_batch_i]
hyps_global_indices[idx] = next_batch_global_i
encoder_output_t[idx] = 0
next_batch_i += 1
next_batch_global_i += 1
# if next_batch_i is out of bounds, get next batch of encoder outputs
if next_batch_i >= encoder_output_len_next.shape[0]:
if next_query_utterance_i < len(manifest):
encoder_output_next, encoder_output_len_next = get_batch_encoder_outputs_from_records(
manifest[next_query_utterance_i : next_query_utterance_i + batch_size],
model=model,
device=device,
)
# pad if needed to allow futher assignment of encoder_output_next to encoder_output
if encoder_output.shape[2] < encoder_output_next.shape[2]:
encoder_output = F.pad(
encoder_output, (0, encoder_output_next.shape[2] - encoder_output.shape[2])
)
next_batch_i = 0
next_query_utterance_i += batch_size
else:
has_next = False
for hyp in hyps:
if hyp is not None:
hyp.clean_decoding_state_()
streaming_transcripts = []
for hyp in all_hyps:
streaming_transcripts.append(model.tokenizer.ids_to_text(hyp.y_sequence.tolist()))
assert ref_transcripts == streaming_transcripts
@pytest.mark.with_downloads
@pytest.mark.parametrize(
"device,use_cuda_graph_decoder",
[(device, False) for device in DEVICES] + [(device, True) for device in DEVICES if device.type == "cuda"],
)
@pytest.mark.parametrize("is_tdt", [False, True])
@pytest.mark.parametrize("chunk_size", [1]) # Small chunk size to trigger more state updates
@pytest.mark.parametrize("batch_size", [4])
@pytest.mark.parametrize("max_symbols", [10])
def test_label_looping_streaming_boosting_with_ref_transcripts(
tmp_path_factory,
an4_val_manifest_corrected,
stt_en_fastconformer_transducer_large,
stt_en_fastconformer_tdt_large,
device: torch.device,
use_cuda_graph_decoder: bool,
is_tdt: bool,
chunk_size: int,
batch_size: int,
max_symbols: int,
):
"""
Metamorphic test: boosting with reference transcripts should yield identical results.
This test validates that when we boost with the exact transcripts that the model
would produce without boosting, the results remain the same. This is a metamorphic
property that should hold for correct implementations.
This test specifically validates the fix for TDT streaming boosting where
fusion states were incorrectly updated using `active_mask` instead of `found_labels_mask`.
"""
model = stt_en_fastconformer_tdt_large if is_tdt else stt_en_fastconformer_transducer_large
model.eval()
model.to(device=device)
# First, get reference transcripts without boosting
decoding_cfg = copy.deepcopy(model.cfg.decoding)
decoding_cfg.strategy = "greedy_batch"
with open_dict(decoding_cfg):
decoding_cfg.greedy.use_cuda_graph_decoder = use_cuda_graph_decoder
decoding_cfg.greedy.max_symbols = max_symbols
model.change_decoding_strategy(decoding_cfg)
manifest = read_manifest(an4_val_manifest_corrected)
transcriptions = model.transcribe(audio=str(an4_val_manifest_corrected.absolute()), batch_size=batch_size)
ref_transcripts = [hyp.text for hyp in transcriptions]
# Now set up per-stream boosting with reference transcripts
decoding_cfg_boosted = copy.deepcopy(model.cfg.decoding)
decoding_cfg_boosted.strategy = "greedy_batch"
with open_dict(decoding_cfg_boosted):
decoding_cfg_boosted.greedy.use_cuda_graph_decoder = use_cuda_graph_decoder
decoding_cfg_boosted.greedy.max_symbols = max_symbols
decoding_cfg_boosted.greedy.enable_per_stream_biasing = True
model.change_decoding_strategy(decoding_cfg_boosted)
all_hyps = []
decoding_computer: GreedyBatchedLabelLoopingComputerBase = model.decoding.decoding.decoding_computer
with torch.no_grad(), torch.inference_mode():
for i in range(0, len(manifest), batch_size):
batch_records = manifest[i : i + batch_size]
batch_ref_transcripts = ref_transcripts[i : i + batch_size]
encoder_output, encoder_output_len = get_batch_encoder_outputs_from_records(
batch_records, model=model, device=device
)
local_batch_size = encoder_output_len.shape[0]
# Create biasing requests for each sample in the batch
biasing_requests = []
multi_biasing_ids = torch.full([local_batch_size], fill_value=-1, dtype=torch.long, device=device)
for batch_idx, ref_text in enumerate(batch_ref_transcripts):
if ref_text: # Only boost non-empty transcripts
request = BiasingRequestItemConfig(
boosting_model_cfg=BoostingTreeModelConfig(key_phrases_list=[ref_text], unk_score=-100),
boosting_model_alpha=10.0,
)
request.add_to_multi_model(
tokenizer=model.tokenizer,
biasing_multi_model=decoding_computer.biasing_multi_model,
)
if request.multi_model_id is not None:
multi_biasing_ids[batch_idx] = request.multi_model_id
biasing_requests.append(request)
else:
biasing_requests.append(None)
# Decode encoder output by chunks, passing state between decoder invocations
state: Optional[BatchedLabelLoopingState] = None
batched_hyps: BatchedHyps | None = None
encoder_output = encoder_output.transpose(1, 2)
for t in range(0, encoder_output.shape[1], chunk_size):
rest_len = encoder_output_len - t
current_len = torch.full_like(encoder_output_len, fill_value=chunk_size)
current_len = torch.minimum(current_len, rest_len)
current_len = torch.maximum(current_len, torch.zeros_like(current_len))
batched_hyps_chunk, _, state = decoding_computer(
x=encoder_output[:, t : t + chunk_size],
out_len=current_len,
prev_batched_state=state,
multi_biasing_ids=multi_biasing_ids,
)
if batched_hyps is None:
batched_hyps = batched_hyps_chunk
else:
batched_hyps.merge_(batched_hyps_chunk)
# Clean up biasing models
for request in biasing_requests:
if request is not None and request.multi_model_id is not None:
decoding_computer.biasing_multi_model.remove_model(request.multi_model_id)
request.multi_model_id = None
assert batched_hyps is not None
all_hyps.extend(batched_hyps_to_hypotheses(batched_hyps, None, batch_size=local_batch_size))
streaming_transcripts = []
for hyp in all_hyps:
streaming_transcripts.append(model.tokenizer.ids_to_text(hyp.y_sequence.tolist()))
# The key assertion: boosting with ref transcripts should yield same results
assert ref_transcripts == streaming_transcripts, (
f"Boosting with reference transcripts should yield identical results.\n"
f"This failure indicates a bug in fusion state handling during streaming decoding.\n"
f"Differences found:\n"
+ "\n".join(
f" [{i}] ref: {ref!r} != boosted: {boosted!r}"
for i, (ref, boosted) in enumerate(zip(ref_transcripts, streaming_transcripts))
if ref != boosted
)
)
|