Skip to content

Commit d49ae3c

Browse files
committed
Address PR bot review remarks
Signed-off-by: Piotr Żelasko <pzelasko@nvidia.com>
1 parent 56d9b52 commit d49ae3c

9 files changed

Lines changed: 39 additions & 17 deletions

File tree

nemo/collections/asr/modules/ggemm_transformer_encoder.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,6 @@ def _sequence_packed_grouped_attention_step(self, encs, state, layer_idx, fused_
16241624
fused_qkv=fused_qkv,
16251625
)
16261626
continue
1627-
names = [item[0] for item in group]
16281627
hidden = torch.stack([item[1] for item in group], dim=0)
16291628
compute_dtype = _autocast_compute_dtype(hidden)
16301629
hidden = hidden.to(compute_dtype)
@@ -1728,7 +1727,6 @@ def _sequence_packed_grouped_attention_step(self, encs, state, layer_idx, fused_
17281727
layer = encs[name].layers[layer_idx]
17291728
state[name]['x'] = state[name]['x'] + layer.drop(output)
17301729
continue
1731-
names = [item[0] for item in group]
17321730
hidden = torch.stack([item[1] for item in group], dim=0)
17331731
compute_dtype = _autocast_compute_dtype(hidden)
17341732
outputs = _grouped_linear(hidden.to(compute_dtype), [item[2] for item in group])

nemo/collections/asr/modules/parallel_expert_encoder.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,9 +1656,10 @@ def forward(
16561656
use_online = self.online_inference_enabled and self.online_inference_length > 0
16571657
runner = self._forward_online if use_online else self._forward
16581658
outputs, lengths, experts = runner(audio_signal=audio_signal, length=length, spk_targets=spk_targets)
1659+
result = (outputs, lengths)
16591660
if return_experts:
1660-
return outputs, lengths, experts
1661-
return outputs, lengths
1661+
result += (experts,)
1662+
return result
16621663

16631664
def forward_sequence_packed(
16641665
self,

nemo/collections/speechlm2/data/salm_dataset.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,28 +137,29 @@ def __getitem__(self, conversations: CutSet) -> dict | None:
137137
packed_audio_samples, audio_cu_seqlens, audio_lens, conversations = (
138138
collate_conversation_audio_packed_fault_tolerant(conversations, self.load_audio)
139139
)
140+
audio_inputs = {
141+
"packed_audio_samples": packed_audio_samples,
142+
"audio_cu_seqlens": audio_cu_seqlens,
143+
}
140144
else:
141145
audios, audio_lens, conversations = collate_conversation_audio_fault_tolerant(
142146
conversations, self.load_audio
143147
)
148+
audio_inputs = {"audios": audios}
144149
except Exception as e:
145150
logging.warning(f"Error collating conversations: {e}")
146151
return None
147152
if not conversations:
148153
return None
149154
batch = {
155+
**audio_inputs,
150156
"audio_lens": audio_lens,
151157
"input_ids": left_collate_vectors([c.input_ids for c in conversations], padding_value=self.pad_id),
152158
"loss_mask": left_collate_vectors(
153159
[getattr(c, "mask", torch.empty(0)) for c in conversations], padding_value=0
154160
).to(torch.bool),
155161
"conversations": drop_in_memory_data(conversations),
156162
}
157-
if self.pack_audio:
158-
batch["packed_audio_samples"] = packed_audio_samples
159-
batch["audio_cu_seqlens"] = audio_cu_seqlens
160-
else:
161-
batch["audios"] = audios
162163
if self.multispeaker_processor is not None:
163164
self.multispeaker_processor(batch)
164165
return batch

nemo/collections/speechlm2/modules/perception.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ def forward(
222222

223223
# b, c, t -> b, t, c
224224
encoded = self.proj(encoded.transpose(1, 2))
225+
result = (encoded, encoded_len)
225226
if return_encoder_emb:
226-
return encoded, encoded_len, encoder_emb.transpose(1, 2)
227-
else:
228-
return encoded, encoded_len
227+
result += (encoder_emb.transpose(1, 2),)
228+
return result
229229

230230
@typecheck.disable_checks()
231231
def forward_sequence_packed(

scripts/speech_recognition/benchmark_packed_asr_encoders.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,6 @@ def _run_iteration(encoder_name, model, inputs, lengths, speaker_targets, implem
474474
loss = loss + auxiliary_loss
475475
loss.backward()
476476
_clear_moe_auxiliary_loss(encoder_name, model)
477-
del output
478477

479478

480479
def _valid_output(encoder_name, model, inputs, lengths, speaker_targets, implementation):

tests/collections/asr/test_packed_pee_grouped.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import nemo.collections.asr.modules.ggemm_transformer_encoder as ggemm_module
2121
import nemo.collections.asr.modules.transformer_encoder as transformer_module
22-
from nemo.collections.asr.modules.ggemm_transformer_encoder import GGEMMTransformerEncoder, _ragged_grouped_mm
2322
from nemo.collections.asr.parts.packed_sequence import PackedEncoderActivations, pack_encoder_output
2423
from tests.collections.asr.test_parallel_expert_encoder import (
2524
_MEL_FEATURES,
@@ -462,7 +461,7 @@ def forward_sequence_packed(self, audio_signal, length, bypass_pre_encode=False)
462461
return PackedEncoderActivations(audio_signal, length, cu_seqlens, int(length.max()))
463462

464463
expert = PackedOnly()
465-
container = GGEMMTransformerEncoder({'custom': expert})
464+
container = ggemm_module.GGEMMTransformerEncoder({'custom': expert})
466465
data = torch.randn(3, 4)
467466
output = container.forward_all_sequence_packed(data, torch.tensor([3]))
468467
assert output['custom'].data is data
@@ -496,7 +495,7 @@ def test_ragged_grouped_mm_backward_handles_empty_expert_and_sum_loss(offset_val
496495
x = torch.randn(7, 16, device='cuda', dtype=torch.bfloat16, requires_grad=True)
497496
weights = [torch.randn(16, 8, device='cuda', requires_grad=True) for _ in range(3)]
498497
offsets = torch.tensor(offset_values, device='cuda', dtype=torch.int32)
499-
output = _ragged_grouped_mm(x, offsets, weights)
498+
output = ggemm_module._ragged_grouped_mm(x, offsets, weights)
500499
output.sum().backward()
501500

502501
assert x.grad is not None and torch.isfinite(x.grad).all()

tests/collections/asr/test_packed_sequence_round2.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,23 @@ def test_pee_packed_can_return_raw_expert_outputs_without_changing_default():
136136
assert experts["speaker_preds"] is not None
137137

138138

139+
def test_pee_legacy_optional_expert_return_contract_is_unchanged():
140+
encoder = build_toy_pe_encoder().eval()
141+
mels = torch.randn(2, _MEL_FEATURES, 24)
142+
lengths = torch.tensor([24, 11])
143+
targets = torch.zeros(2, 3, _N_SPK)
144+
145+
with torch.no_grad():
146+
default = encoder(mels, lengths, spk_targets=targets)
147+
with_experts = encoder(mels, lengths, spk_targets=targets, return_experts=True)
148+
149+
assert len(default) == 2
150+
assert len(with_experts) == 3
151+
torch.testing.assert_close(with_experts[0], default[0])
152+
assert torch.equal(with_experts[1], default[1])
153+
assert set(with_experts[2]) == {"speech", "sound", "speaker_preds"}
154+
155+
139156
@pytest.mark.skipif(not torch.cuda.is_available(), reason="PEE sound-gradient parity requires CUDA")
140157
def test_pee_packed_matches_legacy_trainable_sound_gradients():
141158
torch.manual_seed(0)

tests/collections/speechlm2/test_perception_packed_sequence.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,16 @@ def test_perception_sequence_packed_matches_legacy_and_preserves_state_dict():
6161

6262
with torch.no_grad():
6363
legacy, output_lengths = perception(input_signal=features, input_signal_length=lengths)
64+
legacy_with_encoder = perception(
65+
input_signal=features,
66+
input_signal_length=lengths,
67+
return_encoder_emb=True,
68+
)
6469
packed = perception.forward_sequence_packed(input_signal=features, input_signal_length=lengths)
6570

71+
assert len(legacy_with_encoder) == 3
72+
torch.testing.assert_close(legacy_with_encoder[0], legacy)
73+
assert torch.equal(legacy_with_encoder[1], output_lengths)
6674
restored = unpack_encoder_output(packed, total_length=legacy.shape[1])
6775
valid = torch.arange(legacy.shape[1])[None, :] < output_lengths[:, None]
6876
torch.testing.assert_close(restored[valid], legacy[valid], rtol=1e-5, atol=1e-6)

tests/collections/speechlm2/test_perception_packed_sequence_capabilities.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import pytest
1818
import torch
1919

20-
from nemo.collections.asr.parts.packed_sequence import unpack_encoder_output
2120
from tests.collections.speechlm2.test_perception_packed_sequence import _make_perception
2221

2322

0 commit comments

Comments
 (0)