|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import pytest |
| 17 | +import torch |
| 18 | +from omegaconf import DictConfig |
| 19 | + |
| 20 | +from nemo.collections.tts.models import AudioCodecModel |
| 21 | + |
| 22 | + |
| 23 | +def create_codec_config(): |
| 24 | + audio_encoder = { |
| 25 | + 'cls': 'nemo.collections.tts.modules.audio_codec_modules.MultiResolutionSTFTEncoder', |
| 26 | + 'params': { |
| 27 | + 'out_dim': 40, |
| 28 | + 'resolutions': [[960, 240, 960], [1920, 480, 1920]], |
| 29 | + 'resolution_filter_list': [256, 512], |
| 30 | + }, |
| 31 | + } |
| 32 | + audio_decoder = { |
| 33 | + 'cls': 'nemo.collections.tts.modules.audio_codec_modules.ResNetDecoder', |
| 34 | + 'params': { |
| 35 | + 'input_dim': 40, |
| 36 | + 'input_filters': 512, |
| 37 | + 'n_hidden_layers': 6, |
| 38 | + 'hidden_filters': 512, |
| 39 | + 'pre_up_sample_rates': [], |
| 40 | + 'pre_up_sample_filters': [], |
| 41 | + 'resblock_up_sample_rates': [10, 8, 6], |
| 42 | + 'resblock_up_sample_filters': [256, 128, 32], |
| 43 | + }, |
| 44 | + } |
| 45 | + vector_quantizer = { |
| 46 | + 'cls': 'nemo.collections.tts.modules.audio_codec_modules.GroupFiniteScalarQuantizer', |
| 47 | + 'params': { |
| 48 | + 'num_groups': 8, |
| 49 | + 'num_levels_per_group': [4, 4, 4, 4, 4], |
| 50 | + }, |
| 51 | + } |
| 52 | + generator_loss = { |
| 53 | + 'cls': 'nemo.collections.tts.losses.audio_codec_loss.GeneratorSquaredLoss', |
| 54 | + } |
| 55 | + discriminator_loss = { |
| 56 | + 'cls': 'nemo.collections.tts.losses.audio_codec_loss.DiscriminatorSquaredLoss', |
| 57 | + } |
| 58 | + |
| 59 | + model_cfg = DictConfig( |
| 60 | + { |
| 61 | + 'sample_rate': 24000, |
| 62 | + 'samples_per_frame': 480, |
| 63 | + 'loss_resolutions': [[960, 240, 960], [1920, 480, 1920]], |
| 64 | + 'mel_loss_dims': [160, 320], |
| 65 | + 'commit_loss_scale': 0.0, |
| 66 | + 'audio_encoder': DictConfig(audio_encoder), |
| 67 | + 'audio_decoder': DictConfig(audio_decoder), |
| 68 | + 'vector_quantizer': DictConfig(vector_quantizer), |
| 69 | + 'generator_loss': DictConfig(generator_loss), |
| 70 | + 'discriminator_loss': DictConfig(discriminator_loss), |
| 71 | + } |
| 72 | + ) |
| 73 | + return model_cfg |
| 74 | + |
| 75 | + |
| 76 | +@pytest.fixture() |
| 77 | +def codec_model(): |
| 78 | + model_cfg = create_codec_config() |
| 79 | + codec_model = AudioCodecModel(cfg=model_cfg) |
| 80 | + return codec_model |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture() |
| 84 | +def acoustic_codec_model(): |
| 85 | + semantic_model_cfg = create_codec_config() |
| 86 | + semantic_model_cfg.vector_quantizer.params.num_groups = 1 |
| 87 | + semantic_model_cfg.audio_encoder.params.out_dim = 5 |
| 88 | + semantic_model_cfg.audio_decoder.params.input_dim = 5 |
| 89 | + |
| 90 | + acoustic_model_cfg = create_codec_config() |
| 91 | + acoustic_model_cfg.semantic_codec = semantic_model_cfg |
| 92 | + acoustic_model_cfg.audio_encoder.params.out_dim = 35 |
| 93 | + acoustic_codec_model = AudioCodecModel(cfg=acoustic_model_cfg) |
| 94 | + |
| 95 | + return acoustic_codec_model |
| 96 | + |
| 97 | + |
| 98 | +class TestAudioCodecModel: |
| 99 | + @pytest.mark.unit |
| 100 | + def test_forward(self, codec_model): |
| 101 | + batch_size = 2 |
| 102 | + audio = torch.randn(size=(batch_size, 20000)) |
| 103 | + audio_len = torch.randint(size=[batch_size], low=10000, high=20000) |
| 104 | + output_audio, output_audio_len = codec_model.forward( |
| 105 | + audio=audio, audio_len=audio_len, sample_rate=codec_model.sample_rate |
| 106 | + ) |
| 107 | + assert output_audio.shape[0] == batch_size |
| 108 | + assert output_audio.shape[1] == output_audio_len.max() |
| 109 | + |
| 110 | + @pytest.mark.unit |
| 111 | + def test_forward_with_acoustic_codec(self, acoustic_codec_model): |
| 112 | + batch_size = 3 |
| 113 | + audio = torch.randn(size=(batch_size, 20000)) |
| 114 | + audio_len = torch.randint(size=[batch_size], low=10000, high=20000) |
| 115 | + output_audio, output_audio_len = acoustic_codec_model.forward( |
| 116 | + audio=audio, audio_len=audio_len, sample_rate=acoustic_codec_model.sample_rate |
| 117 | + ) |
| 118 | + assert output_audio.shape[0] == batch_size |
| 119 | + assert output_audio.shape[1] == output_audio_len.max() |
| 120 | + |
| 121 | + @pytest.mark.unit |
| 122 | + def test_encode_and_decode(self, codec_model): |
| 123 | + batch_size = 4 |
| 124 | + audio = torch.randn(size=(batch_size, 20000)) |
| 125 | + audio_len = torch.randint(size=[batch_size], low=10000, high=20000) |
| 126 | + |
| 127 | + tokens, tokens_len = codec_model.encode(audio=audio, audio_len=audio_len, sample_rate=codec_model.sample_rate) |
| 128 | + assert tokens.shape[0] == batch_size |
| 129 | + assert tokens.shape[2] == tokens_len.max() |
| 130 | + |
| 131 | + output_audio, output_audio_len = codec_model.decode(tokens=tokens, tokens_len=tokens_len) |
| 132 | + assert output_audio.shape[0] == batch_size |
| 133 | + assert output_audio.shape[1] == output_audio_len.max() |
| 134 | + |
| 135 | + @pytest.mark.unit |
| 136 | + def test_encode_and_decode_with_acoustic_codec(self, acoustic_codec_model): |
| 137 | + batch_size = 5 |
| 138 | + audio = torch.randn(size=(batch_size, 20000)) |
| 139 | + audio_len = torch.randint(size=[batch_size], low=10000, high=20000) |
| 140 | + |
| 141 | + tokens, tokens_len = acoustic_codec_model.encode( |
| 142 | + audio=audio, audio_len=audio_len, sample_rate=acoustic_codec_model.sample_rate |
| 143 | + ) |
| 144 | + assert tokens.shape[0] == batch_size |
| 145 | + assert tokens.shape[2] == tokens_len.max() |
| 146 | + |
| 147 | + output_audio, output_audio_len = acoustic_codec_model.decode(tokens=tokens, tokens_len=tokens_len) |
| 148 | + assert output_audio.shape[0] == batch_size |
| 149 | + assert output_audio.shape[1] == output_audio_len.max() |
0 commit comments