File size: 10,589 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 | # Copyright (c) 2023, 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 pytest
import torch
from nemo.collections.asr.models.configs.asr_models_config import CacheAwareStreamingConfig
from nemo.collections.asr.parts.utils.multispk_transcribe_utils import MultiTalkerInstanceManager
from tests.collections.asr.test_asr_rnnt_encoder_model_bpe import asr_model as offline_asr_model
from tests.collections.speaker_tasks.test_diar_sortformer_models import sortformer_model as diar_model
@pytest.fixture()
def asr_model(offline_asr_model):
"""Wrapper fixture that adds streaming_cfg to the asr_model from test_asr_rnnt_encoder_model_bpe"""
# Add streaming_cfg to encoder for streaming tests
streaming_cfg = CacheAwareStreamingConfig(
valid_out_len=1,
drop_extra_pre_encoded=7,
chunk_size=8,
shift_size=4,
cache_drop_size=4,
last_channel_cache_size=64,
pre_encode_cache_size=0,
last_channel_num=0,
last_time_num=0,
)
offline_asr_model.encoder.streaming_cfg = streaming_cfg
# Mock get_initial_cache_state method for MultiTalkerInstanceManager tests
def get_initial_cache_state(batch_size=1):
"""Mock method to return initial cache state for streaming"""
# Return dummy cache state tensors
cache_last_channel = torch.zeros(2, batch_size, 64)
cache_last_time = torch.zeros(2, batch_size, 64)
cache_last_channel_len = torch.zeros(batch_size)
return (cache_last_channel, cache_last_time, cache_last_channel_len)
offline_asr_model.encoder.get_initial_cache_state = get_initial_cache_state
return offline_asr_model
class TestMultiTalkerInstanceManagerMethods:
"""Test methods of the MultiTalkerInstanceManager class"""
@pytest.mark.unit
def test_reset_active_speaker_buffers(self, asr_model, diar_model):
"""Test _reset_active_speaker_buffers method"""
instance_manager = MultiTalkerInstanceManager(
asr_model=asr_model,
diar_model=diar_model,
batch_size=2,
max_num_of_spks=4,
sent_break_sec=5.0,
)
# Populate some buffers first
# pylint: disable=protected-access
instance_manager._active_chunk_audio = [torch.randn(100)]
instance_manager._active_chunk_lengths = [torch.tensor(100)]
instance_manager._active_speaker_targets = [torch.randn(10)]
# Reset the buffers
instance_manager._reset_active_speaker_buffers()
# Verify all buffers are empty
assert len(instance_manager._active_chunk_audio) == 0
assert len(instance_manager._active_chunk_lengths) == 0
assert len(instance_manager._active_speaker_targets) == 0
assert len(instance_manager._inactive_speaker_targets) == 0
assert len(instance_manager._active_previous_hypotheses) == 0
assert len(instance_manager._active_asr_pred_out_stream) == 0
assert len(instance_manager._active_cache_last_channel) == 0
assert len(instance_manager._active_cache_last_time) == 0
assert len(instance_manager._active_cache_last_channel_len) == 0
# pylint: enable=protected-access
@pytest.mark.unit
def test_reset_with_new_params(self, asr_model, diar_model):
"""Test reset method with new batch_size and max_num_of_spks"""
instance_manager = MultiTalkerInstanceManager(
asr_model=asr_model,
diar_model=diar_model,
batch_size=2,
max_num_of_spks=4,
sent_break_sec=5.0,
)
# Reset with new parameters
instance_manager.reset(batch_size=3, max_num_of_spks=6)
# Verify new parameters are applied
assert instance_manager.batch_size == 3
assert instance_manager.max_num_of_spks == 6
assert len(instance_manager.batch_asr_states) == 3
@pytest.mark.unit
def test_add_speaker(self, asr_model, diar_model):
"""Test add_speaker method"""
instance_manager = MultiTalkerInstanceManager(
asr_model=asr_model,
diar_model=diar_model,
batch_size=2,
max_num_of_spks=4,
sent_break_sec=5.0,
)
instance_manager.reset()
# Initially, batch 0 should have speaker [0]
speakers_before = instance_manager.get_speakers(batch_idx=0)
assert 0 in speakers_before
# Add speaker 1
instance_manager.add_speaker(batch_idx=0, speaker_id=1)
# Verify speaker 1 is added
speakers_after = instance_manager.get_speakers(batch_idx=0)
assert 0 in speakers_after
assert 1 in speakers_after
@pytest.mark.unit
def test_update_diar_state(self, asr_model, diar_model):
"""Test update_diar_state method"""
instance_manager = MultiTalkerInstanceManager(
asr_model=asr_model,
diar_model=diar_model,
batch_size=2,
max_num_of_spks=4,
sent_break_sec=5.0,
)
instance_manager.reset()
# Create mock diarization data
diar_pred_out_stream = torch.randn(2, 20, 4)
previous_chunk_preds = torch.randn(2, 10, 4)
# Get initial streaming state from diar_model
diar_streaming_state = diar_model.sortformer_modules.init_streaming_state(batch_size=2)
# Update diar state
instance_manager.update_diar_state(
diar_pred_out_stream=diar_pred_out_stream,
previous_chunk_preds=previous_chunk_preds,
diar_streaming_state=diar_streaming_state,
)
# Verify diar state is updated
assert torch.equal(instance_manager.diar_states.diar_pred_out_stream, diar_pred_out_stream)
assert torch.equal(instance_manager.diar_states.previous_chunk_preds, previous_chunk_preds)
assert instance_manager.diar_states.streaming_state is not None
@pytest.mark.unit
def test_update_asr_state(self, asr_model, diar_model):
"""Test update_asr_state method"""
instance_manager = MultiTalkerInstanceManager(
asr_model=asr_model,
diar_model=diar_model,
batch_size=2,
max_num_of_spks=4,
sent_break_sec=5.0,
)
instance_manager.reset()
# Get the initial cache state structure
asr_state = instance_manager.batch_asr_states[0]
# Create mock ASR cache data with correct shapes
cache_shape = asr_state.cache_last_channel.shape
time_shape = asr_state.cache_last_time.shape
cache_last_channel = torch.randn(cache_shape[0], cache_shape[2]) # Remove speaker dimension
cache_last_time = torch.randn(time_shape[0], time_shape[2])
cache_last_channel_len = torch.tensor([10])
# Create a simple mock hypothesis
from nemo.collections.asr.parts.utils.rnnt_utils import Hypothesis
previous_hypothesis = Hypothesis(score=0.0, y_sequence=[], text="test")
previous_pred_out = torch.randn(1, 10, 128)
# Update ASR state for batch 0, speaker 0
instance_manager.update_asr_state(
batch_idx=0,
speaker_id=0,
cache_last_channel=cache_last_channel,
cache_last_time=cache_last_time,
cache_last_channel_len=cache_last_channel_len,
previous_hypotheses=previous_hypothesis,
previous_pred_out=previous_pred_out,
)
# Verify the state was updated
updated_asr_state = instance_manager.batch_asr_states[0]
assert updated_asr_state.previous_hypothesis[0] is previous_hypothesis
assert updated_asr_state.previous_pred_out[0] is previous_pred_out
@pytest.mark.unit
def test_get_active_speakers_info(self, asr_model, diar_model):
"""Test get_active_speakers_info with both empty and active speakers"""
instance_manager = MultiTalkerInstanceManager(
asr_model=asr_model,
diar_model=diar_model,
batch_size=2,
max_num_of_spks=4,
sent_break_sec=5.0,
)
instance_manager.reset()
# Set up diar state with mock data
previous_chunk_preds = torch.randn(2, 10, 4)
instance_manager.diar_states.previous_chunk_preds = previous_chunk_preds
# Test 1: No active speakers - should return None
active_speakers_empty = [[], []]
chunk_audio = torch.randn(2, 1600)
chunk_lengths = torch.tensor([1600, 1600])
result = instance_manager.get_active_speakers_info(active_speakers_empty, chunk_audio, chunk_lengths)
assert result == (None, None, None, None)
# Test 2: Active speakers - batch 0 has speaker 0, batch 1 has speakers 0 and 1
active_speakers = [[0], [0, 1]]
active_chunk_audio, active_chunk_lengths, active_speaker_targets, inactive_speaker_targets = (
instance_manager.get_active_speakers_info(active_speakers, chunk_audio, chunk_lengths)
)
# Should have 3 active speakers total (1 from batch 0, 2 from batch 1)
assert active_chunk_audio is not None
assert active_chunk_audio.shape[0] == 3
assert active_chunk_lengths.shape[0] == 3
assert active_speaker_targets.shape[0] == 3
assert inactive_speaker_targets.shape[0] == 3
@pytest.mark.unit
def test_update_seglsts(self, asr_model, diar_model):
"""Test update_seglsts method"""
instance_manager = MultiTalkerInstanceManager(
asr_model=asr_model,
diar_model=diar_model,
batch_size=2,
max_num_of_spks=4,
sent_break_sec=5.0,
)
instance_manager.reset()
# Call update_seglsts (should not raise an error)
offset = 0.0
instance_manager.update_seglsts(offset=offset)
# Verify seglsts are updated in each ASR state
for asr_state in instance_manager.batch_asr_states:
assert isinstance(asr_state.seglsts, list)
|