|
19 | 19 | from examples.speech_to_text.simultaneous_translation.agents.v1_1.streaming.streaming_st_agent import StreamingSTAgent, \
|
20 | 20 | get_class_from_string
|
21 | 21 | from examples.speech_to_text.simultaneous_translation.agents.v1_1.streaming.text_first_history_selection import \
|
22 |
| - PunctuationHistorySelection |
| 22 | + PunctuationHistorySelection, FixedAudioHistorySelection |
23 | 23 | from simuleval.agents import ReadAction, WriteAction
|
24 | 24 |
|
25 | 25 | from fbk_simul_uts.v1_1.test_base_simulst_agent import BaseSTAgentTestCaseV2, MockedLoadModelVocab
|
@@ -218,6 +218,57 @@ def test_prefix_punctuation_selection(self, get_hypo_and_prefix):
|
218 | 218 | # Check first no frame discarded
|
219 | 219 | self.assertEqual(len(self.states.source[0]), 24)
|
220 | 220 |
|
| 221 | + @patch('examples.speech_to_text.simultaneous_translation.agents.v1_1.' |
| 222 | + 'simul_offline_alignatt.AlignAttSTAgent._get_hypo_and_prefix') |
| 223 | + def test_fixed_audio_selection(self, get_hypo_and_prefix): |
| 224 | + hypo = { |
| 225 | + "tokens": torch.tensor([4, 5, 7, 8, 0]), # I am quokka. |
| 226 | + "attention": torch.tensor([ |
| 227 | + [0.5, 0.05, 0.05, 0.05, 0.05, 0.3], # first frame mostly attended |
| 228 | + [0.0, 0.6, 0.05, 0.03, 0.02, 0.3], # second frame mostly attended |
| 229 | + [0.05, 0.5, 0.05, 0.05, 0.05, 0.3], # second frame mostly attended |
| 230 | + [0.0, 0.6, 0.05, 0.03, 0.02, 0.3], # second frame mostly attended |
| 231 | + [0.05, 0.05, 0.05, 0.5, 0.05, 0.3], # last frame mostly attended |
| 232 | + ]).transpose(0, 1) |
| 233 | + } |
| 234 | + |
| 235 | + self.args.history_words = 1 |
| 236 | + self.agent.history_selection_method = FixedAudioHistorySelection( |
| 237 | + self.agent.simulst_agent.tgtdict, self.agent.simulst_agent.args) |
| 238 | + |
| 239 | + # No prefix |
| 240 | + get_hypo_and_prefix.return_value = hypo, 0 |
| 241 | + self.states.target_indices = [] |
| 242 | + self.states.source = [torch.rand(280 // 10 * 4)] |
| 243 | + action = self.agent.policy(self.states) |
| 244 | + self.assertIsInstance(action, WriteAction) |
| 245 | + self.assertEqual(action.content, "I am") |
| 246 | + # "I am" should be written but only "am" should be retained as textual history (since |
| 247 | + # history_words is set to 1), therefore 280ms (corresponding to one word) should be |
| 248 | + # discarded |
| 249 | + self.assertEqual(len(self.states.source[0]), 280 // 10 * 3) |
| 250 | + |
| 251 | + # History len 1: "I" |
| 252 | + get_hypo_and_prefix.return_value = hypo, 1 |
| 253 | + self.states.target_indices = [4] |
| 254 | + self.states.source = [torch.rand(280 // 10 * 4)] |
| 255 | + action = self.agent.policy(self.states) |
| 256 | + self.assertIsInstance(action, WriteAction) |
| 257 | + self.assertEqual(action.content, "am") |
| 258 | + # "am" should be written and retained as textual history (since history_words is set to 1) |
| 259 | + # while "I" should be discarded, therefore 280ms (corresponding to one word) should be |
| 260 | + # discarded |
| 261 | + self.assertEqual(len(self.states.source[0]), 280 // 10 * 3) |
| 262 | + |
| 263 | + # History len 1: "am" |
| 264 | + get_hypo_and_prefix.return_value = hypo, 2 |
| 265 | + self.agent.states.target_indices = [5] |
| 266 | + self.states.source = [torch.rand(280 // 10 * 4)] |
| 267 | + action = self.agent.policy(self.states) |
| 268 | + self.assertIsInstance(action, ReadAction) |
| 269 | + # Check no frame discarded |
| 270 | + self.assertEqual(len(self.states.source[0]), 280 // 10 * 4) |
| 271 | + |
221 | 272 | @patch('examples.speech_to_text.simultaneous_translation.agents.v1_1.'
|
222 | 273 | 'simul_offline_alignatt.AlignAttSTAgent._get_hypo_and_prefix')
|
223 | 274 | def test_no_token_emitted(self, get_hypo_and_prefix):
|
|
0 commit comments