diff options
| author | Kai Li (李凯) <tsinghua.kaili@gmail.com> | 2026-08-14 15:15:16 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-14 15:15:16 +0800 |
| commit | b17bf536b11fa527cddce5dd6694c4b04a44d3f6 (patch) | |
| tree | 3e17a3facbbb1d9a53bf8e0d2837238745d049ce /tests/test_inference.py | |
| parent | 4f3ff075428b261f43e94b92b081553ee87791ec (diff) | |
| parent | 802f429831f257c7e6295594c39ccfc864cd490f (diff) | |
| download | apollo-gfx803-b17bf536b11fa527cddce5dd6694c4b04a44d3f6.tar.gz | |
Merge pull request #41 from Djshinshin/codex/chunked-inference
Thanks for implementing configurable chunk batching in response to the review feedback.
I reviewed the latest changes, and this now addresses the requested behavior. The validation, bounded-memory design, overlap-add handling, and documentation look good. Keeping `--chunk-batch-size=1` as the default is reasonable given the backend-specific memory and throughput tradeoff.
The 20/20 test suite passes, and the MPS validation results are clearly documented. I have no further changes requested and approve this PR.
Please mark the PR as ready for review. Once it is out of draft, I will merge it.
Diffstat (limited to 'tests/test_inference.py')
| -rw-r--r-- | tests/test_inference.py | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/tests/test_inference.py b/tests/test_inference.py index 7e0e64f..b9778cf 100644 --- a/tests/test_inference.py +++ b/tests/test_inference.py @@ -17,12 +17,24 @@ class IdentityModel(torch.nn.Module): super().__init__() self.anchor = torch.nn.Parameter(torch.zeros(())) self.input_device = None + self.input_devices = [] + self.input_lengths = [] + self.input_batch_sizes = [] def forward(self, audio): self.input_device = audio.device + self.input_devices.append(audio.device) + self.input_lengths.append(audio.shape[-1]) + self.input_batch_sizes.append(audio.shape[0]) return audio +class ShortOutputModel(IdentityModel): + def forward(self, audio): + super().forward(audio) + return audio[..., :-1] + + class DeviceSelectionTests(unittest.TestCase): @mock.patch.object(torch.backends.mps, "is_available", return_value=True) @mock.patch.object(torch.cuda, "is_available", return_value=True) @@ -129,6 +141,131 @@ class InferenceTests(unittest.TestCase): hashlib.sha256(input_path.read_bytes()).hexdigest(), input_digest ) + def test_chunked_cpu_inference_preserves_samples_and_limits_chunk_size(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + input_path = root / "input.wav" + output_path = root / "output.wav" + checkpoint_path = root / "checkpoint.bin" + checkpoint_path.touch() + + sample_count = int(inference.SAMPLE_RATE * 1.8) + samples = np.linspace(-0.5, 0.5, sample_count, dtype=np.float32) + stereo = np.stack((samples, -samples), axis=1) + sf.write(input_path, stereo, inference.SAMPLE_RATE, subtype="FLOAT") + input_digest = hashlib.sha256(input_path.read_bytes()).hexdigest() + + model = IdentityModel() + with mock.patch.object( + inference.look2hear.models.BaseModel, + "from_pretrain", + return_value=model, + ): + device = inference.run_inference( + input_path, + output_path, + checkpoint_path, + requested_device="cpu", + chunk_seconds=0.5, + overlap_seconds=0.1, + chunk_batch_size=2, + ) + + output, sample_rate = sf.read( + output_path, dtype="float32", always_2d=True + ) + self.assertEqual(device, torch.device("cpu")) + self.assertEqual(sample_rate, inference.SAMPLE_RATE) + self.assertEqual(output.shape, stereo.shape) + np.testing.assert_allclose(output, stereo, rtol=0, atol=1e-6) + self.assertGreater(len(model.input_lengths), 1) + self.assertEqual( + set(model.input_lengths), {int(inference.SAMPLE_RATE * 0.5)} + ) + self.assertEqual(model.input_batch_sizes, [2, 2, 1]) + self.assertEqual(set(model.input_devices), {torch.device("cpu")}) + self.assertEqual( + hashlib.sha256(input_path.read_bytes()).hexdigest(), input_digest + ) + + def test_short_input_uses_original_full_file_path(self): + audio = torch.zeros(1, 2, inference.SAMPLE_RATE // 4) + model = IdentityModel() + + output = inference.run_model( + model, + audio, + torch.device("cpu"), + chunk_samples=inference.SAMPLE_RATE, + overlap_samples=inference.SAMPLE_RATE // 4, + ) + + self.assertEqual(model.input_lengths, [audio.shape[-1]]) + self.assertTrue(torch.equal(output, audio)) + + def test_chunk_settings_reject_invalid_durations(self): + invalid_settings = ( + (0, 0, "positive finite"), + (float("inf"), 0, "positive finite"), + (1, -0.1, "non-negative finite"), + (1, float("nan"), "non-negative finite"), + (1, 0.6, "half the chunk"), + ) + for chunk_seconds, overlap_seconds, message in invalid_settings: + with self.subTest( + chunk_seconds=chunk_seconds, overlap_seconds=overlap_seconds + ), self.assertRaisesRegex(ValueError, message): + inference.resolve_chunking(chunk_seconds, overlap_seconds) + + def test_chunking_rejects_short_model_output(self): + audio = torch.zeros(1, 2, 100) + with self.assertRaisesRegex(RuntimeError, "shorter"): + inference.run_model( + ShortOutputModel(), + audio, + torch.device("cpu"), + chunk_samples=60, + overlap_samples=10, + ) + + def test_chunking_rejects_invalid_batch_size(self): + audio = torch.zeros(1, 2, 100) + for batch_size in (0, -1, 1.5, True): + with self.subTest(batch_size=batch_size), self.assertRaisesRegex( + ValueError, "positive integer" + ): + inference.run_model( + IdentityModel(), + audio, + torch.device("cpu"), + chunk_samples=60, + overlap_samples=10, + chunk_batch_size=batch_size, + ) + + def test_invalid_batch_size_fails_before_checkpoint_download(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + input_path = root / "input.wav" + sf.write( + input_path, + np.zeros((100, 2), dtype=np.float32), + inference.SAMPLE_RATE, + ) + + with mock.patch.object(inference, "hf_hub_download") as download: + with self.assertRaisesRegex(ValueError, "positive integer"): + inference.run_inference( + input_path, + root / "output.wav", + inference.OFFICIAL_CHECKPOINT, + "cpu", + chunk_seconds=1, + overlap_seconds=0, + chunk_batch_size=0, + ) + download.assert_not_called() + def test_refuses_to_overwrite_input(self): path = Path("same.wav") with self.assertRaisesRegex(ValueError, "must be different"): |
