Download tests/test_speech_runtime_contract.py from Tinghong-Ye22/code-HFD: direct link, hf CLI and curl.
- Browser
- Download file 2.14 kB
-
https://huggingface.co/Tinghong-Ye22/code-HFD/resolve/main/tests/test_speech_runtime_contract.py
- Command line
-
hf download hf://Tinghong-Ye22/code-HFD/tests/test_speech_runtime_contract.py
-
curl -L -o test_speech_runtime_contract.py https://huggingface.co/Tinghong-Ye22/code-HFD/resolve/main/tests/test_speech_runtime_contract.py
2.14 kB
| from __future__ import annotations | |
| from queue import Full | |
| import torch | |
| from psi.runtime import ( | |
| BoundedSpeechQueue, | |
| PCMClockPackets, | |
| SpeechChunk, | |
| SpeechGenerationContext, | |
| SpeechRuntimeConfig, | |
| ) | |
| def test_verified_speech_defaults_and_geometry(): | |
| config = SpeechRuntimeConfig().validate() | |
| assert config.solver == "euler" | |
| assert config.solver_steps == 6 | |
| assert config.codec_chunk_codes == 36 | |
| assert config.first_chunk_codes == 36 | |
| assert config.playback_buffer_samples == 4_800 | |
| assert config.action_packet_samples == 9_600 | |
| def test_context_positions_and_stale_result_gate(): | |
| context = SpeechGenerationContext("session") | |
| assert context.begin_utterance() == 1 | |
| context.mark_generated_text(3) | |
| context.mark_generated_codec(7) | |
| context.mark_pcm_queued(100) | |
| chunk = SpeechChunk(torch.zeros(100), "session", 1, 0) | |
| queue = BoundedSpeechQueue(context, maxsize=1) | |
| assert queue.submit(chunk) | |
| assert queue.get().utterance_id == 1 | |
| queue.task_done() | |
| context.cancel("user_interrupt") | |
| assert not queue.submit(chunk) | |
| assert context.snapshot()["generated_text_pos"] == 3 | |
| assert context.snapshot()["generated_codec_pos"] == 7 | |
| def test_queue_is_bounded_and_never_silently_drops_current_audio(): | |
| context = SpeechGenerationContext("session") | |
| context.begin_utterance() | |
| queue = BoundedSpeechQueue(context, maxsize=1) | |
| first = SpeechChunk(torch.zeros(2), "session", 1, 0) | |
| second = SpeechChunk(torch.zeros(2), "session", 1, 0) | |
| assert queue.submit(first) | |
| try: | |
| queue.submit(second, block=False) | |
| except Full: | |
| pass | |
| else: | |
| raise AssertionError("a full queue must apply backpressure") | |
| assert queue.qsize() == 1 | |
| def test_pcm_clock_reblocks_without_loss_or_duplication(): | |
| clock = PCMClockPackets(samples_per_packet=4) | |
| assert [x.tolist() for x in clock.push(torch.arange(3))] == [] | |
| packets = clock.push(torch.arange(3, 8)) | |
| packets += clock.finish() | |
| assert [x.tolist() for x in packets] == [[0, 1, 2, 3], [4, 5, 6, 7]] | |
| assert clock.received_samples == clock.emitted_samples == 8 | |