|
| 1 | +"""Unit tests for Kokoro TTS layer implementations. |
| 2 | +
|
| 3 | +Tests the neural network layers used in Kokoro-82M TTS model. |
| 4 | +Uses mock weights to verify layer behavior without requiring actual model files. |
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pytest |
| 9 | + |
| 10 | +import pygpukit as gk |
| 11 | +from pygpukit.core.factory import from_numpy |
| 12 | + |
| 13 | +# Check if new TTS layers are available (they may not be in older installations) |
| 14 | +try: |
| 15 | + from pygpukit.tts.kokoro.layers import WeightNormConv1d # noqa: F401 |
| 16 | + |
| 17 | + HAS_TTS_LAYERS = True |
| 18 | +except ImportError: |
| 19 | + HAS_TTS_LAYERS = False |
| 20 | + |
| 21 | +pytestmark = pytest.mark.skipif(not HAS_TTS_LAYERS, reason="TTS layers not available") |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def skip_if_no_cuda(): |
| 26 | + """Skip test if CUDA is not available.""" |
| 27 | + if not gk.is_cuda_available(): |
| 28 | + pytest.skip("CUDA not available") |
| 29 | + |
| 30 | + |
| 31 | +class TestWeightNormConv1d: |
| 32 | + """Tests for WeightNormConv1d layer.""" |
| 33 | + |
| 34 | + def test_weight_normalization(self, skip_if_no_cuda): |
| 35 | + """Test that weight normalization computes W = g * (v / ||v||).""" |
| 36 | + from pygpukit.tts.kokoro.layers import WeightNormConv1d |
| 37 | + |
| 38 | + out_channels, in_channels, kernel_size = 4, 2, 3 |
| 39 | + |
| 40 | + # Create mock weights |
| 41 | + weight_g = from_numpy(np.ones((out_channels, 1, 1), dtype=np.float32) * 2.0) |
| 42 | + weight_v = from_numpy(np.random.randn(out_channels, in_channels, kernel_size).astype(np.float32)) |
| 43 | + |
| 44 | + conv = WeightNormConv1d(weight_g=weight_g, weight_v=weight_v) |
| 45 | + |
| 46 | + # Compute normalized weight |
| 47 | + weight = conv._compute_weight() |
| 48 | + |
| 49 | + # Verify: each output channel should have L2 norm equal to g |
| 50 | + for i in range(out_channels): |
| 51 | + channel_norm = np.sqrt((weight[i] ** 2).sum()) |
| 52 | + np.testing.assert_allclose(channel_norm, 2.0, rtol=1e-5) |
| 53 | + |
| 54 | + def test_forward_shape(self, skip_if_no_cuda): |
| 55 | + """Test that forward pass produces correct output shape.""" |
| 56 | + from pygpukit.tts.kokoro.layers import WeightNormConv1d |
| 57 | + |
| 58 | + batch, in_channels, length = 2, 4, 16 |
| 59 | + out_channels, kernel_size = 8, 3 |
| 60 | + padding = 1 |
| 61 | + |
| 62 | + weight_g = from_numpy(np.ones((out_channels, 1, 1), dtype=np.float32)) |
| 63 | + weight_v = from_numpy(np.random.randn(out_channels, in_channels, kernel_size).astype(np.float32)) |
| 64 | + bias = from_numpy(np.zeros(out_channels, dtype=np.float32)) |
| 65 | + |
| 66 | + conv = WeightNormConv1d(weight_g=weight_g, weight_v=weight_v, bias=bias, padding=padding) |
| 67 | + |
| 68 | + x = from_numpy(np.random.randn(batch, in_channels, length).astype(np.float32)) |
| 69 | + out = conv(x) |
| 70 | + |
| 71 | + # With padding=1 and kernel_size=3, output length should be same as input |
| 72 | + assert out.shape == (batch, out_channels, length) |
| 73 | + |
| 74 | + |
| 75 | +class TestInstanceNorm1d: |
| 76 | + """Tests for InstanceNorm1d layer.""" |
| 77 | + |
| 78 | + def test_normalization(self, skip_if_no_cuda): |
| 79 | + """Test that instance norm normalizes each channel to zero mean, unit variance.""" |
| 80 | + from pygpukit.tts.kokoro.layers import InstanceNorm1d |
| 81 | + |
| 82 | + channels = 4 |
| 83 | + gamma = from_numpy(np.ones(channels, dtype=np.float32)) |
| 84 | + beta = from_numpy(np.zeros(channels, dtype=np.float32)) |
| 85 | + |
| 86 | + norm = InstanceNorm1d(gamma=gamma, beta=beta) |
| 87 | + |
| 88 | + # Create input with known statistics |
| 89 | + batch, length = 2, 32 |
| 90 | + x = from_numpy(np.random.randn(batch, channels, length).astype(np.float32) * 5 + 3) |
| 91 | + |
| 92 | + out = norm(x) |
| 93 | + out_np = out.to_numpy() |
| 94 | + |
| 95 | + # Check each sample and channel has ~zero mean and ~unit variance |
| 96 | + for b in range(batch): |
| 97 | + for c in range(channels): |
| 98 | + mean = out_np[b, c].mean() |
| 99 | + var = out_np[b, c].var() |
| 100 | + np.testing.assert_allclose(mean, 0.0, atol=1e-5) |
| 101 | + np.testing.assert_allclose(var, 1.0, atol=1e-4) |
| 102 | + |
| 103 | + def test_affine_transform(self, skip_if_no_cuda): |
| 104 | + """Test that gamma and beta are applied correctly.""" |
| 105 | + from pygpukit.tts.kokoro.layers import InstanceNorm1d |
| 106 | + |
| 107 | + channels = 2 |
| 108 | + gamma = from_numpy(np.array([2.0, 0.5], dtype=np.float32)) |
| 109 | + beta = from_numpy(np.array([1.0, -1.0], dtype=np.float32)) |
| 110 | + |
| 111 | + norm = InstanceNorm1d(gamma=gamma, beta=beta) |
| 112 | + |
| 113 | + x = from_numpy(np.random.randn(1, channels, 100).astype(np.float32)) |
| 114 | + out = norm(x) |
| 115 | + out_np = out.to_numpy() |
| 116 | + |
| 117 | + # After normalization and affine: mean should be beta, std should be gamma |
| 118 | + np.testing.assert_allclose(out_np[0, 0].mean(), 1.0, atol=0.1) |
| 119 | + np.testing.assert_allclose(out_np[0, 1].mean(), -1.0, atol=0.1) |
| 120 | + np.testing.assert_allclose(out_np[0, 0].std(), 2.0, atol=0.1) |
| 121 | + np.testing.assert_allclose(out_np[0, 1].std(), 0.5, atol=0.1) |
| 122 | + |
| 123 | + |
| 124 | +class TestAdaIN: |
| 125 | + """Tests for Adaptive Instance Normalization layer.""" |
| 126 | + |
| 127 | + def test_style_conditioning(self, skip_if_no_cuda): |
| 128 | + """Test that style vector modulates scale and shift.""" |
| 129 | + from pygpukit.tts.kokoro.layers import AdaIN |
| 130 | + |
| 131 | + channels, style_dim = 4, 8 |
| 132 | + |
| 133 | + # FC layer: [2*channels, style_dim] |
| 134 | + fc_weight = from_numpy(np.random.randn(2 * channels, style_dim).astype(np.float32) * 0.1) |
| 135 | + fc_bias = from_numpy(np.zeros(2 * channels, dtype=np.float32)) |
| 136 | + |
| 137 | + adain = AdaIN(fc_weight=fc_weight, fc_bias=fc_bias) |
| 138 | + |
| 139 | + batch, length = 2, 16 |
| 140 | + x = from_numpy(np.random.randn(batch, channels, length).astype(np.float32)) |
| 141 | + style = from_numpy(np.random.randn(batch, style_dim).astype(np.float32)) |
| 142 | + |
| 143 | + out = adain(x, style) |
| 144 | + |
| 145 | + assert out.shape == (batch, channels, length) |
| 146 | + |
| 147 | + def test_different_styles_produce_different_outputs(self, skip_if_no_cuda): |
| 148 | + """Test that different style vectors produce different outputs.""" |
| 149 | + from pygpukit.tts.kokoro.layers import AdaIN |
| 150 | + |
| 151 | + channels, style_dim = 4, 8 |
| 152 | + |
| 153 | + fc_weight = from_numpy(np.random.randn(2 * channels, style_dim).astype(np.float32)) |
| 154 | + fc_bias = from_numpy(np.zeros(2 * channels, dtype=np.float32)) |
| 155 | + |
| 156 | + adain = AdaIN(fc_weight=fc_weight, fc_bias=fc_bias) |
| 157 | + |
| 158 | + x = from_numpy(np.random.randn(1, channels, 16).astype(np.float32)) |
| 159 | + style1 = from_numpy(np.random.randn(1, style_dim).astype(np.float32)) |
| 160 | + style2 = from_numpy(np.random.randn(1, style_dim).astype(np.float32)) |
| 161 | + |
| 162 | + out1 = adain(x, style1).to_numpy() |
| 163 | + out2 = adain(x, style2).to_numpy() |
| 164 | + |
| 165 | + # Outputs should be different |
| 166 | + assert not np.allclose(out1, out2) |
| 167 | + |
| 168 | + |
| 169 | +class TestALBERTLayer: |
| 170 | + """Tests for ALBERTLayer.""" |
| 171 | + |
| 172 | + def test_forward_shape(self, skip_if_no_cuda): |
| 173 | + """Test that ALBERT layer preserves sequence dimensions.""" |
| 174 | + from pygpukit.tts.kokoro.layers import ALBERTLayer, LayerNorm, Linear |
| 175 | + |
| 176 | + batch, seq_len, hidden_size = 2, 16, 64 |
| 177 | + num_heads = 4 |
| 178 | + intermediate_size = 128 |
| 179 | + |
| 180 | + # Create mock weights |
| 181 | + def make_linear(in_f, out_f): |
| 182 | + w = from_numpy(np.random.randn(out_f, in_f).astype(np.float32) * 0.02) |
| 183 | + b = from_numpy(np.zeros(out_f, dtype=np.float32)) |
| 184 | + return Linear(w, b) |
| 185 | + |
| 186 | + def make_norm(size): |
| 187 | + w = from_numpy(np.ones(size, dtype=np.float32)) |
| 188 | + b = from_numpy(np.zeros(size, dtype=np.float32)) |
| 189 | + return LayerNorm(w, b) |
| 190 | + |
| 191 | + layer = ALBERTLayer( |
| 192 | + query=make_linear(hidden_size, hidden_size), |
| 193 | + key=make_linear(hidden_size, hidden_size), |
| 194 | + value=make_linear(hidden_size, hidden_size), |
| 195 | + attention_dense=make_linear(hidden_size, hidden_size), |
| 196 | + attention_norm=make_norm(hidden_size), |
| 197 | + ffn=make_linear(hidden_size, intermediate_size), |
| 198 | + ffn_output=make_linear(intermediate_size, hidden_size), |
| 199 | + full_layer_norm=make_norm(hidden_size), |
| 200 | + num_attention_heads=num_heads, |
| 201 | + hidden_size=hidden_size, |
| 202 | + ) |
| 203 | + |
| 204 | + x = from_numpy(np.random.randn(batch, seq_len, hidden_size).astype(np.float32)) |
| 205 | + out = layer(x) |
| 206 | + |
| 207 | + assert out.shape == (batch, seq_len, hidden_size) |
| 208 | + |
| 209 | + |
| 210 | +class TestALBERTEncoder: |
| 211 | + """Tests for ALBERTEncoder.""" |
| 212 | + |
| 213 | + def test_forward_shape(self, skip_if_no_cuda): |
| 214 | + """Test that ALBERT encoder produces correct output shape.""" |
| 215 | + from pygpukit.tts.kokoro.layers import ALBERTEncoder, ALBERTLayer, LayerNorm, Linear |
| 216 | + |
| 217 | + vocab_size, embed_dim, hidden_size = 100, 32, 64 |
| 218 | + max_positions, num_heads = 128, 4 |
| 219 | + num_layers = 2 |
| 220 | + intermediate_size = 128 |
| 221 | + |
| 222 | + def make_linear(in_f, out_f): |
| 223 | + w = from_numpy(np.random.randn(out_f, in_f).astype(np.float32) * 0.02) |
| 224 | + b = from_numpy(np.zeros(out_f, dtype=np.float32)) |
| 225 | + return Linear(w, b) |
| 226 | + |
| 227 | + def make_norm(size): |
| 228 | + w = from_numpy(np.ones(size, dtype=np.float32)) |
| 229 | + b = from_numpy(np.zeros(size, dtype=np.float32)) |
| 230 | + return LayerNorm(w, b) |
| 231 | + |
| 232 | + # Embeddings |
| 233 | + word_emb = from_numpy(np.random.randn(vocab_size, embed_dim).astype(np.float32) * 0.02) |
| 234 | + pos_emb = from_numpy(np.random.randn(max_positions, embed_dim).astype(np.float32) * 0.02) |
| 235 | + type_emb = from_numpy(np.random.randn(2, embed_dim).astype(np.float32) * 0.02) |
| 236 | + |
| 237 | + # Shared layer |
| 238 | + layer = ALBERTLayer( |
| 239 | + query=make_linear(hidden_size, hidden_size), |
| 240 | + key=make_linear(hidden_size, hidden_size), |
| 241 | + value=make_linear(hidden_size, hidden_size), |
| 242 | + attention_dense=make_linear(hidden_size, hidden_size), |
| 243 | + attention_norm=make_norm(hidden_size), |
| 244 | + ffn=make_linear(hidden_size, intermediate_size), |
| 245 | + ffn_output=make_linear(intermediate_size, hidden_size), |
| 246 | + full_layer_norm=make_norm(hidden_size), |
| 247 | + num_attention_heads=num_heads, |
| 248 | + hidden_size=hidden_size, |
| 249 | + ) |
| 250 | + |
| 251 | + encoder = ALBERTEncoder( |
| 252 | + word_embeddings=word_emb, |
| 253 | + position_embeddings=pos_emb, |
| 254 | + token_type_embeddings=type_emb, |
| 255 | + embeddings_norm=make_norm(embed_dim), |
| 256 | + embedding_mapping=make_linear(embed_dim, hidden_size), |
| 257 | + layer=layer, |
| 258 | + num_hidden_layers=num_layers, |
| 259 | + ) |
| 260 | + |
| 261 | + batch, seq_len = 2, 16 |
| 262 | + input_ids = from_numpy(np.random.randint(0, vocab_size, (batch, seq_len)).astype(np.int32)) |
| 263 | + |
| 264 | + out = encoder(input_ids) |
| 265 | + |
| 266 | + assert out.shape == (batch, seq_len, hidden_size) |
| 267 | + |
| 268 | + |
| 269 | +class TestKokoroTextEncoder: |
| 270 | + """Tests for KokoroTextEncoder (CNN + BiLSTM).""" |
| 271 | + |
| 272 | + def test_forward_shape(self, skip_if_no_cuda): |
| 273 | + """Test that text encoder produces correct output shape.""" |
| 274 | + from pygpukit.tts.kokoro.layers import ( |
| 275 | + LSTM, |
| 276 | + InstanceNorm1d, |
| 277 | + KokoroTextEncoder, |
| 278 | + WeightNormConv1d, |
| 279 | + ) |
| 280 | + |
| 281 | + vocab_size, embed_dim = 100, 32 |
| 282 | + cnn_channels = 64 |
| 283 | + lstm_hidden = 128 |
| 284 | + |
| 285 | + # Embedding |
| 286 | + embedding = from_numpy(np.random.randn(vocab_size, embed_dim).astype(np.float32) * 0.02) |
| 287 | + |
| 288 | + # CNN layers |
| 289 | + cnn_layers = [] |
| 290 | + in_ch = embed_dim |
| 291 | + for _ in range(3): |
| 292 | + conv = WeightNormConv1d( |
| 293 | + weight_g=from_numpy(np.ones((cnn_channels, 1, 1), dtype=np.float32)), |
| 294 | + weight_v=from_numpy(np.random.randn(cnn_channels, in_ch, 5).astype(np.float32) * 0.02), |
| 295 | + padding=2, |
| 296 | + ) |
| 297 | + norm = InstanceNorm1d( |
| 298 | + gamma=from_numpy(np.ones(cnn_channels, dtype=np.float32)), |
| 299 | + beta=from_numpy(np.zeros(cnn_channels, dtype=np.float32)), |
| 300 | + ) |
| 301 | + cnn_layers.append((conv, norm)) |
| 302 | + in_ch = cnn_channels |
| 303 | + |
| 304 | + # BiLSTM |
| 305 | + lstm = LSTM( |
| 306 | + W_ih=from_numpy(np.random.randn(4 * lstm_hidden, cnn_channels).astype(np.float32) * 0.02), |
| 307 | + W_hh=from_numpy(np.random.randn(4 * lstm_hidden, lstm_hidden).astype(np.float32) * 0.02), |
| 308 | + b_ih=from_numpy(np.zeros(4 * lstm_hidden, dtype=np.float32)), |
| 309 | + b_hh=from_numpy(np.zeros(4 * lstm_hidden, dtype=np.float32)), |
| 310 | + bidirectional=True, |
| 311 | + W_ih_reverse=from_numpy(np.random.randn(4 * lstm_hidden, cnn_channels).astype(np.float32) * 0.02), |
| 312 | + W_hh_reverse=from_numpy(np.random.randn(4 * lstm_hidden, lstm_hidden).astype(np.float32) * 0.02), |
| 313 | + b_ih_reverse=from_numpy(np.zeros(4 * lstm_hidden, dtype=np.float32)), |
| 314 | + b_hh_reverse=from_numpy(np.zeros(4 * lstm_hidden, dtype=np.float32)), |
| 315 | + ) |
| 316 | + |
| 317 | + encoder = KokoroTextEncoder(embedding=embedding, cnn_layers=cnn_layers, lstm=lstm) |
| 318 | + |
| 319 | + batch, seq_len = 2, 16 |
| 320 | + input_ids = from_numpy(np.random.randint(0, vocab_size, (batch, seq_len)).astype(np.int32)) |
| 321 | + |
| 322 | + out = encoder(input_ids) |
| 323 | + |
| 324 | + # BiLSTM output: [batch, seq_len, 2 * lstm_hidden] |
| 325 | + assert out.shape == (batch, seq_len, 2 * lstm_hidden) |
| 326 | + |
| 327 | + |
| 328 | +class TestAdaINResBlock: |
| 329 | + """Tests for AdaINResBlock.""" |
| 330 | + |
| 331 | + def test_residual_connection(self, skip_if_no_cuda): |
| 332 | + """Test that residual connection is applied.""" |
| 333 | + from pygpukit.tts.kokoro.layers import AdaIN, AdaINResBlock, WeightNormConv1d |
| 334 | + |
| 335 | + channels, style_dim = 32, 16 |
| 336 | + |
| 337 | + def make_conv(in_ch, out_ch): |
| 338 | + return WeightNormConv1d( |
| 339 | + weight_g=from_numpy(np.ones((out_ch, 1, 1), dtype=np.float32)), |
| 340 | + weight_v=from_numpy(np.random.randn(out_ch, in_ch, 3).astype(np.float32) * 0.02), |
| 341 | + padding=1, |
| 342 | + ) |
| 343 | + |
| 344 | + def make_adain(ch, style_d): |
| 345 | + return AdaIN( |
| 346 | + fc_weight=from_numpy(np.random.randn(2 * ch, style_d).astype(np.float32) * 0.1), |
| 347 | + fc_bias=from_numpy(np.zeros(2 * ch, dtype=np.float32)), |
| 348 | + ) |
| 349 | + |
| 350 | + block = AdaINResBlock( |
| 351 | + conv1=make_conv(channels, channels), |
| 352 | + conv2=make_conv(channels, channels), |
| 353 | + norm1=make_adain(channels, style_dim), |
| 354 | + norm2=make_adain(channels, style_dim), |
| 355 | + ) |
| 356 | + |
| 357 | + batch, length = 2, 16 |
| 358 | + x = from_numpy(np.random.randn(batch, channels, length).astype(np.float32)) |
| 359 | + style = from_numpy(np.random.randn(batch, style_dim).astype(np.float32)) |
| 360 | + |
| 361 | + out = block(x, style) |
| 362 | + |
| 363 | + assert out.shape == (batch, channels, length) |
| 364 | + |
| 365 | + |
| 366 | +class TestBuildFunctions: |
| 367 | + """Tests for weight builder functions.""" |
| 368 | + |
| 369 | + def test_build_albert_missing_weights_raises(self, skip_if_no_cuda): |
| 370 | + """Test that missing weights raise KeyError.""" |
| 371 | + from pygpukit.tts.kokoro.layers import build_albert_from_weights |
| 372 | + |
| 373 | + weights = {} # Empty weights |
| 374 | + |
| 375 | + with pytest.raises(KeyError): |
| 376 | + build_albert_from_weights(weights) |
| 377 | + |
| 378 | + def test_build_text_encoder_missing_weights_raises(self, skip_if_no_cuda): |
| 379 | + """Test that missing weights raise KeyError.""" |
| 380 | + from pygpukit.tts.kokoro.layers import build_text_encoder_from_weights |
| 381 | + |
| 382 | + weights = {} # Empty weights |
| 383 | + |
| 384 | + with pytest.raises(KeyError): |
| 385 | + build_text_encoder_from_weights(weights) |
0 commit comments