Skip to content

Commit a032051

Browse files
committed
Apply isort and black reformatting
Signed-off-by: andrusenkoau <andrusenkoau@users.noreply.github.com>
1 parent eef5587 commit a032051

3 files changed

Lines changed: 25 additions & 18 deletions

File tree

examples/asr/asr_chunked_inference/rnnt/speech_to_text_streaming_infer_rnnt.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ class TranscriptionConfig:
133133
)
134134
right_context_secs: float = 2 # right context
135135

136-
att_context_size_as_chunk: bool = True # whether to use the att_context_size as chunk size (importand for extra-low latency)
136+
att_context_size_as_chunk: bool = (
137+
True # whether to use the att_context_size as chunk size (importand for extra-low latency)
138+
)
137139

138140
# Set `cuda` to int to define CUDA device. If 'None', will look for CUDA
139141
# device anyway, and do inference on CPU only if CUDA device is not found.
@@ -303,7 +305,7 @@ def main(cfg: TranscriptionConfig) -> TranscriptionConfig:
303305
# unified ASR model: use the att_context_size as chunk size (important for extra-low latency)
304306
if asr_model.cfg.encoder.att_context_style == 'chunked_limited_with_rc' and cfg.att_context_size_as_chunk:
305307
asr_model.encoder.set_default_att_context_size(
306-
att_context_size=[context_encoder_frames.left,context_encoder_frames.chunk,context_encoder_frames.right]
308+
att_context_size=[context_encoder_frames.left, context_encoder_frames.chunk, context_encoder_frames.right]
307309
)
308310

309311
logging.info(

nemo/collections/asr/losses/rnnt.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class RNNTLossConfig:
154154
installation_msg="Pure Pytorch implementation of TDT loss. Slow and for debugging purposes only.",
155155
),
156156
"rnnt_triton": RNNTLossConfig(
157-
loss_name="rnnt_triton", # will be added later
157+
loss_name="rnnt_triton", # will be added later
158158
lib_name="torch",
159159
min_version='0.0',
160160
is_available=True,
@@ -331,7 +331,7 @@ def resolve_rnnt_loss(loss_name: str, blank_idx: int, loss_kwargs: dict = None)
331331
loss_kwargs = _clean_kwargs(loss_name, loss_kwargs, GraphWTransducerLoss.__init__, ignore_params={"blank"})
332332
loss_func = GraphWTransducerLoss(blank=blank_idx, **loss_kwargs)
333333
elif loss_name == "rnnt_triton":
334-
loss_func = None # will be added later
334+
loss_func = None # will be added later
335335
else:
336336
raise ValueError(
337337
f"Invalid value of `loss_name`: {loss_name}. Allowed loss names are :" f"{loss_function_names}"
@@ -343,8 +343,7 @@ def resolve_rnnt_loss(loss_name: str, blank_idx: int, loss_kwargs: dict = None)
343343
class RNNTLoss(Loss):
344344
@property
345345
def input_types(self):
346-
"""Input types definitions for CTCLoss.
347-
"""
346+
"""Input types definitions for CTCLoss."""
348347
return {
349348
"log_probs": NeuralType(('B', 'T', 'T', 'D'), LogprobsType()),
350349
"targets": NeuralType(('B', 'T'), LabelsType()),
@@ -405,7 +404,7 @@ def __init__(self, num_classes, reduction: str = 'mean_batch', loss_name: str =
405404
standard blank, and the standard blank is the last symbol in the vocab)
406405
TDT: num_classes = V. Note, V here does not include any of the "duration outputs".
407406
408-
reduction: Type of reduction to perform on loss. Possible values are
407+
reduction: Type of reduction to perform on loss. Possible values are
409408
`mean_batch`, 'mean_volume`, `mean`, `sum` or None.
410409
`None` will return a torch vector comprising the individual loss values of the batch.
411410
`mean_batch` will average the losses in the batch

nemo/collections/asr/modules/conformer_encoder.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ def __init__(
361361
self.use_pytorch_sdpa_backends = use_pytorch_sdpa_backends
362362
self.sync_max_audio_length = sync_max_audio_length
363363

364-
365364
assert conv_context_style in ["regular", "dcc", "dcc_rc"], f"Invalid conv_context_style: {conv_context_style}!"
366365
self.conv_context_style = conv_context_style
367366
self.conv_kernel_size = conv_kernel_size
@@ -370,8 +369,12 @@ def __init__(
370369
self.dual_mode_training = dual_mode_training
371370
self.unified_asr_prob = unified_asr_prob
372371
if att_chunk_context_size is not None:
373-
assert att_context_style == "chunked_limited_with_rc", "att_chunk_context_size is only supported for chunked_limited_with_rc attention style!"
374-
assert len(att_chunk_context_size) == 3, "att_chunk_context_size must have 3 elements: [left_context, chunk_size, right_context]"
372+
assert (
373+
att_context_style == "chunked_limited_with_rc"
374+
), "att_chunk_context_size is only supported for chunked_limited_with_rc attention style!"
375+
assert (
376+
len(att_chunk_context_size) == 3
377+
), "att_chunk_context_size must have 3 elements: [left_context, chunk_size, right_context]"
375378
self.att_chunk_context_size = att_chunk_context_size
376379
else:
377380
self.att_chunk_context_size = None
@@ -855,8 +858,10 @@ def _create_masks(self, att_context_size, padding_length, max_audio_length, offs
855858
)
856859
att_mask = torch.logical_and(att_mask, chunked_limited_mask.unsqueeze(0))
857860
elif self.att_context_style == "chunked_limited_with_rc" and sum(att_context_size) != -3:
858-
assert len(att_context_size) == 3, "att_context_size must have 3 elements: [left_context, chunk_size, right_context]"
859-
861+
assert (
862+
len(att_context_size) == 3
863+
), "att_context_size must have 3 elements: [left_context, chunk_size, right_context]"
864+
860865
left_context_frames = att_context_size[0]
861866
chunk_size_frames = att_context_size[1]
862867
right_context_frames = att_context_size[2]
@@ -867,22 +872,23 @@ def _create_masks(self, att_context_size, padding_length, max_audio_length, offs
867872
window_start = chunk_idx * chunk_size_frames - left_context_frames
868873
window_start = torch.maximum(window_start, torch.zeros_like(window_start))
869874
window_end = chunk_idx * chunk_size_frames + chunk_size_frames - 1 + right_context_frames
870-
875+
871876
if self.training and self.skip_att_chunk_rc_prob > 0.0:
872877
chunks_num = max_audio_length // chunk_size_frames
873878
for chunk_step in range(chunks_num):
874879
if random.random() < self.skip_att_chunk_rc_prob:
875-
window_end[chunk_step*chunk_size_frames:chunk_step*chunk_size_frames+chunk_size_frames] -= right_context_frames
876-
880+
window_end[
881+
chunk_step * chunk_size_frames : chunk_step * chunk_size_frames + chunk_size_frames
882+
] -= right_context_frames
883+
877884
window_end = torch.minimum(window_end, torch.full_like(window_end, max_audio_length - 1))
878885
# Create the mask: frame i can see frame j if window_start[i] <= j <= window_end[i]
879886
j_indices = frame_idx.unsqueeze(0) # [1, T]
880887
window_start_expanded = window_start.unsqueeze(1) # [T, 1]
881-
window_end_expanded = window_end.unsqueeze(1) # [T, 1]
888+
window_end_expanded = window_end.unsqueeze(1) # [T, 1]
882889

883890
chunked_limited_mask = torch.logical_and(
884-
j_indices >= window_start_expanded,
885-
j_indices <= window_end_expanded
891+
j_indices >= window_start_expanded, j_indices <= window_end_expanded
886892
)
887893
att_mask = torch.logical_and(att_mask, chunked_limited_mask.unsqueeze(0))
888894
else:

0 commit comments

Comments
 (0)