[NPU][CosyVoice3] Add CPU STFT fallback (#7520)
Signed-off-by: dengchangxiong <dengchangxiong1@huawei.com> Co-authored-by: dengchangxiong <dengchangxiong1@huawei.com>
This commit is contained in:
1 parent
65693b30fb
commit
f420cfcfbf
2 files changed
+106
-4
No files matched your search
@@ -76,6 +76,74 @@ def test_causal_hift_stft_moves_window_to_input_device(causal_hift):
|
||||
assert causal_hift.stft_window.device == waveform.device
|
||||
|
||||
|
||||
def test_causal_hift_routes_npu_transforms_to_cpu_fallback(causal_hift, monkeypatch):
|
||||
class NpuTensor:
|
||||
device = SimpleNamespace(type="npu")
|
||||
|
||||
tensor = NpuTensor()
|
||||
stft_calls = []
|
||||
istft_calls = []
|
||||
monkeypatch.setattr(
|
||||
causal_hift,
|
||||
"_stft_on_cpu",
|
||||
lambda value: stft_calls.append(value) or ("real", "imag"),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
causal_hift,
|
||||
"_istft_on_cpu",
|
||||
lambda magnitude, phase: istft_calls.append((magnitude, phase)) or "waveform",
|
||||
)
|
||||
|
||||
assert causal_hift._stft(tensor) == ("real", "imag")
|
||||
assert causal_hift._istft(tensor, tensor) == "waveform"
|
||||
assert stft_calls == [tensor]
|
||||
assert istft_calls == [(tensor, tensor)]
|
||||
|
||||
|
||||
def test_causal_hift_stft_cpu_fallback_uses_float32(causal_hift, monkeypatch):
|
||||
original_stft = torch.stft
|
||||
|
||||
def assert_cpu_float32_stft(input, *args, window, **kwargs):
|
||||
assert input.device.type == "cpu"
|
||||
assert input.dtype == torch.float32
|
||||
assert window.device.type == "cpu"
|
||||
assert window.dtype == torch.float32
|
||||
return original_stft(input, *args, window=window, **kwargs)
|
||||
|
||||
monkeypatch.setattr(torch, "stft", assert_cpu_float32_stft)
|
||||
|
||||
waveform = torch.randn(1, 64, dtype=torch.float64)
|
||||
real, imag = causal_hift._stft_on_cpu(waveform)
|
||||
|
||||
assert real.device == waveform.device
|
||||
assert imag.device == waveform.device
|
||||
assert real.dtype == waveform.dtype
|
||||
assert imag.dtype == waveform.dtype
|
||||
|
||||
|
||||
def test_causal_hift_istft_cpu_fallback_uses_float32(causal_hift, monkeypatch):
|
||||
original_istft = torch.istft
|
||||
|
||||
def assert_cpu_complex64_istft(input, *args, window, **kwargs):
|
||||
assert input.device.type == "cpu"
|
||||
assert input.dtype == torch.complex64
|
||||
assert window.device.type == "cpu"
|
||||
assert window.dtype == torch.float32
|
||||
return original_istft(input, *args, window=window, **kwargs)
|
||||
|
||||
monkeypatch.setattr(torch, "istft", assert_cpu_complex64_istft)
|
||||
|
||||
waveform = torch.randn(1, 64, dtype=torch.float32)
|
||||
spec = torch.stft(waveform, 16, 4, 16, window=causal_hift.stft_window, return_complex=True)
|
||||
magnitude = torch.abs(spec).to(torch.float64)
|
||||
phase = torch.angle(spec).to(torch.float64)
|
||||
reconstructed = causal_hift._istft_on_cpu(magnitude, phase)
|
||||
|
||||
assert reconstructed.device == magnitude.device
|
||||
assert reconstructed.dtype == magnitude.dtype
|
||||
torch.testing.assert_close(reconstructed, waveform.to(torch.float64), rtol=1e-5, atol=1e-5)
|
||||
|
||||
|
||||
class TestPreLookaheadLayer:
|
||||
"""Tests for PreLookaheadLayer."""
|
||||
|
||||
|
||||
Reference in new issue
Block a user