aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_inference.py
diff options
context:
space:
mode:
authorDjshinshin <129614060+Djshinshin@users.noreply.github.com>2026-08-14 14:46:38 +0800
committerDjshinshin <129614060+Djshinshin@users.noreply.github.com>2026-08-14 14:46:38 +0800
commit0a91c5fe084bb4643410c52f72a12001871f8ec6 (patch)
tree05f925fb1d6d2888a7b880238c21bdd0bbe169b7 /tests/test_inference.py
parentbe0a056079414683d1e1b89d136415882c82acb8 (diff)
downloadapollo-gfx803-0a91c5fe084bb4643410c52f72a12001871f8ec6.tar.gz
Add chunked inference for long audio
Diffstat (limited to 'tests/test_inference.py')
-rw-r--r--tests/test_inference.py95
1 files changed, 95 insertions, 0 deletions
diff --git a/tests/test_inference.py b/tests/test_inference.py
index 7e0e64f..7857820 100644
--- a/tests/test_inference.py
+++ b/tests/test_inference.py
@@ -17,12 +17,22 @@ class IdentityModel(torch.nn.Module):
super().__init__()
self.anchor = torch.nn.Parameter(torch.zeros(()))
self.input_device = None
+ self.input_devices = []
+ self.input_lengths = []
def forward(self, audio):
self.input_device = audio.device
+ self.input_devices.append(audio.device)
+ self.input_lengths.append(audio.shape[-1])
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 +139,91 @@ 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,
+ )
+
+ 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(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_refuses_to_overwrite_input(self):
path = Path("same.wav")
with self.assertRaisesRegex(ValueError, "must be different"):