PRIYANSHUDHAKED commited on
Commit
118909b
·
verified ·
1 Parent(s): a3b3e34

Delete test_windowed_streamer.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. test_windowed_streamer.py +0 -320
test_windowed_streamer.py DELETED
@@ -1,320 +0,0 @@
1
- """
2
- Tests MioCodecWindowedStreamer against a mock model matching the real
3
- MioCodecModel's decoder interface. Verifies:
4
- - no shape errors through the full windowed pipeline
5
- - first chunk (no left context) and last chunk (no right lookahead)
6
- are handled correctly as genuine edge cases, not crashes
7
- - variable chunk_size (>1, including small values like 2) works
8
- - boundary discontinuity is LOWER than the causal-only chunk_streamer's
9
- boundaries (this is the actual point of the redesign — real lookahead
10
- context should produce smoother joins than causal-only + hard cut)
11
- - total released audio covers all pushed tokens exactly once (no gaps,
12
- no duplicated frames)
13
-
14
- Run: python test_windowed_streamer.py
15
- """
16
- import torch
17
- import torch.nn as nn
18
- import torch.nn.functional as F
19
-
20
- from streaming_istft_head import ISTFTHead as StreamingISTFTHead
21
- from windowed_streamer import WindowedStreamConfig, MioCodecWindowedStreamer, set_transformer_causal
22
-
23
-
24
- class MockNonCausalTransformer(nn.Module):
25
- """Mimics Transformer(causal=False)'s call signature. Bidirectional
26
- (unlike test_chunk_streamer's MockCausalTransformer, this one lets a
27
- position's output depend on ALL positions in the input, including
28
- later ones, via a mean-pool + per-position mix -- simulates real
29
- bidirectional attention's key property for boundary-smoothness testing:
30
- every output position is influenced by the whole window, not just the
31
- causal prefix.)"""
32
-
33
- def __init__(self, dim):
34
- super().__init__()
35
- self.dim = dim
36
- self.proj = nn.Linear(dim, dim)
37
- self.mix = nn.Linear(dim, dim)
38
- self.causal = False # settable by set_transformer_causal, mimicking real Attention.causal
39
-
40
- @property
41
- def layers(self):
42
- # Return fake layer objects so set_transformer_causal can toggle
43
- # something, and reflect it back into self.causal for forward().
44
- outer = self
45
-
46
- class _Layer:
47
- attention = type("A", (), {"causal": outer.causal})()
48
-
49
- return [_Layer()]
50
-
51
- def forward(self, x, condition=None, **kwargs):
52
- out = self.proj(x)
53
- # simulate genuine bidirectional mixing: every position sees the
54
- # mean of the whole window (a crude stand-in for attention seeing
55
- # future context when causal=False)
56
- context = self.mix(x.mean(dim=1, keepdim=True))
57
- out = out + context
58
- if condition is not None:
59
- out = out + condition
60
- return out
61
-
62
-
63
- class MockConfig:
64
- sample_rate = 24000
65
- hop_length = 256
66
- n_fft = 1024
67
- istft_padding = "same"
68
- wave_interpolation_mode = "linear"
69
- wave_upsample_factor = 4
70
- wave_decoder_dim = 32
71
- wave_resnet_num_blocks = 2
72
- wave_resnet_kernel_size = 3
73
- wave_resnet_num_groups = 8
74
- wave_resnet_dropout = 0.0
75
-
76
-
77
- class MockResNetBlock(nn.Module):
78
- """Original (non-causal, symmetric-padded) ResNetBlock, as used by the
79
- windowed streamer (unlike CausalResNetStack in causal_resnet.py)."""
80
-
81
- def __init__(self, channels, kernel_size=3, num_groups=8, dropout=0.0):
82
- super().__init__()
83
- padding = (kernel_size - 1) // 2
84
- self.norm1 = nn.GroupNorm(num_groups, channels, eps=1e-6)
85
- self.conv1 = nn.Conv1d(channels, channels, kernel_size, padding=padding)
86
- self.norm2 = nn.GroupNorm(num_groups, channels, eps=1e-6)
87
- self.conv2 = nn.Conv1d(channels, channels, kernel_size, padding=padding)
88
- self.dropout = nn.Dropout(dropout)
89
-
90
- def forward(self, x):
91
- r = x
92
- x = F.silu(self.norm1(x))
93
- x = self.conv1(x)
94
- x = F.silu(self.norm2(x))
95
- x = self.dropout(x)
96
- x = self.conv2(x)
97
- return x + r
98
-
99
-
100
- class MockResNetStack(nn.Module):
101
- def __init__(self, channels, num_blocks=2, kernel_size=3, num_groups=8, dropout=0.0):
102
- super().__init__()
103
- self.blocks = nn.ModuleList([
104
- MockResNetBlock(channels, kernel_size, num_groups, dropout) for _ in range(num_blocks)
105
- ])
106
-
107
- def forward(self, x):
108
- for b in self.blocks:
109
- x = b(x)
110
- return x
111
-
112
-
113
- class MockMioCodecModel(nn.Module):
114
- def __init__(self):
115
- super().__init__()
116
- self.config = MockConfig()
117
- dim = self.config.wave_decoder_dim
118
-
119
- self.wave_prenet = MockNonCausalTransformer(dim)
120
- self.wave_conv_upsample = nn.ConvTranspose1d(
121
- dim, dim, kernel_size=self.config.wave_upsample_factor, stride=self.config.wave_upsample_factor
122
- )
123
- self.wave_prior_net = MockResNetStack(dim, self.config.wave_resnet_num_blocks)
124
- self.wave_decoder = MockNonCausalTransformer(dim)
125
- self.wave_post_net = MockResNetStack(dim, self.config.wave_resnet_num_blocks)
126
- self.wave_upsampler = None
127
- self.istft_head = StreamingISTFTHead(
128
- dim=dim, n_fft=self.config.n_fft, hop_length=self.config.hop_length, padding=self.config.istft_padding,
129
- )
130
-
131
- def _calculate_target_stft_length(self, audio_length: int) -> int:
132
- return audio_length // self.config.hop_length
133
-
134
- def forward_wave(self, content_embedding, global_embedding, stft_length):
135
- """
136
- Mirrors the real MioCodecModel.forward_wave exactly (see model.py),
137
- so this mock can stand in for the "teacher" (full-utterance,
138
- unbounded-context) path in alignment tests, not just the windowed
139
- streaming path.
140
- """
141
- local_latent = self.wave_prenet(content_embedding)
142
- if self.wave_conv_upsample is not None:
143
- local_latent = self.wave_conv_upsample(local_latent.transpose(1, 2)).transpose(1, 2)
144
- local_latent = F.interpolate(
145
- local_latent.transpose(1, 2), size=stft_length, mode=self.config.wave_interpolation_mode
146
- ).transpose(1, 2)
147
- local_latent = self.wave_prior_net(local_latent.transpose(1, 2)).transpose(1, 2)
148
- local_latent = self.wave_decoder(local_latent, condition=global_embedding.unsqueeze(1))
149
- local_latent = self.wave_post_net(local_latent.transpose(1, 2)).transpose(1, 2)
150
- if self.wave_upsampler is not None:
151
- local_latent = self.wave_upsampler(local_latent.transpose(1, 2))
152
- return self.istft_head(local_latent)
153
-
154
-
155
- def test_first_and_last_chunk_edge_cases():
156
- """First chunk must have zero left-context; last chunk must have zero right-lookahead. Not crashes, correct behavior."""
157
- torch.manual_seed(0)
158
- model = MockMioCodecModel().eval()
159
- dim = model.config.wave_decoder_dim
160
- global_embedding = torch.randn(dim)
161
-
162
- total_tokens = 45 # not a clean multiple of chunk_size(20)+lookahead(10)
163
- all_tokens = torch.randn(total_tokens, dim)
164
-
165
- streamer = MioCodecWindowedStreamer(
166
- model, global_embedding,
167
- config=WindowedStreamConfig(chunk_size=20, left_context_tokens=10, right_lookahead_tokens=10),
168
- )
169
-
170
- with torch.no_grad():
171
- outputs = []
172
- for i in range(total_tokens):
173
- out = streamer.push_token(all_tokens[i])
174
- if out is not None:
175
- outputs.append(out)
176
- final = streamer.finish()
177
- if final is not None:
178
- outputs.append(final)
179
-
180
- assert len(outputs) >= 1, "Expected at least one chunk released"
181
- for o in outputs:
182
- assert torch.isfinite(o).all(), "Non-finite audio in output"
183
- assert o.numel() > 0, "Empty audio chunk released"
184
- print(f"PASS: first/last chunk edge cases handled, {len(outputs)} chunks released, all finite.")
185
-
186
-
187
- def test_variable_chunk_sizes():
188
- """chunk_size must work for any value > 1, including small values like 2."""
189
- torch.manual_seed(1)
190
- model = MockMioCodecModel().eval()
191
- dim = model.config.wave_decoder_dim
192
- global_embedding = torch.randn(dim)
193
-
194
- for chunk_size in [2, 3, 10, 20, 25, 50]:
195
- streamer = MioCodecWindowedStreamer(
196
- model, global_embedding,
197
- config=WindowedStreamConfig(chunk_size=chunk_size, left_context_tokens=10, right_lookahead_tokens=10),
198
- )
199
- tokens = torch.randn(chunk_size * 3 + 5, dim)
200
- with torch.no_grad():
201
- outputs = [streamer.push_token(tokens[i]) for i in range(tokens.shape[0])]
202
- outputs = [o for o in outputs if o is not None]
203
- final = streamer.finish()
204
- if final is not None:
205
- outputs.append(final)
206
- assert len(outputs) >= 1, f"chunk_size={chunk_size}: no output released"
207
- for o in outputs:
208
- assert torch.isfinite(o).all(), f"chunk_size={chunk_size}: non-finite audio"
209
- print(f" chunk_size={chunk_size}: {len(outputs)} chunks, all finite -- OK")
210
- print("PASS: variable chunk sizes (2 to 50) all work.")
211
-
212
-
213
- def test_min_chunk_size_rejects_1():
214
- try:
215
- WindowedStreamConfig(chunk_size=1)
216
- raise AssertionError("chunk_size=1 should have raised ValueError")
217
- except ValueError as e:
218
- print(f"chunk_size=1 correctly rejected: {e}")
219
- WindowedStreamConfig(chunk_size=2) # minimum valid value
220
- print("PASS: chunk_size > 1 constraint enforced.")
221
-
222
-
223
- def test_boundary_smoothness_vs_causal_only():
224
- """
225
- Compares boundary jump magnitude between windowed (bidirectional,
226
- real lookahead) streaming and a zero-lookahead degenerate case
227
- (same code path, right_lookahead_tokens=0), to isolate the effect of
228
- the lookahead itself.
229
- """
230
- torch.manual_seed(2)
231
- model = MockMioCodecModel().eval()
232
- dim = model.config.wave_decoder_dim
233
- global_embedding = torch.randn(dim)
234
- total_tokens = 60
235
- all_tokens = torch.randn(total_tokens, dim)
236
-
237
- streamer_windowed = MioCodecWindowedStreamer(
238
- model, global_embedding,
239
- config=WindowedStreamConfig(chunk_size=15, left_context_tokens=10, right_lookahead_tokens=10),
240
- )
241
- with torch.no_grad():
242
- chunks_windowed = []
243
- for i in range(total_tokens):
244
- out = streamer_windowed.push_token(all_tokens[i])
245
- if out is not None:
246
- chunks_windowed.append(out)
247
- final = streamer_windowed.finish()
248
- if final is not None:
249
- chunks_windowed.append(final)
250
-
251
- streamer_no_lookahead = MioCodecWindowedStreamer(
252
- model, global_embedding,
253
- config=WindowedStreamConfig(chunk_size=15, left_context_tokens=10, right_lookahead_tokens=0),
254
- )
255
- with torch.no_grad():
256
- chunks_no_lookahead = []
257
- for i in range(total_tokens):
258
- out = streamer_no_lookahead.push_token(all_tokens[i])
259
- if out is not None:
260
- chunks_no_lookahead.append(out)
261
- final = streamer_no_lookahead.finish()
262
- if final is not None:
263
- chunks_no_lookahead.append(final)
264
-
265
- def boundary_jump_stats(chunks):
266
- audio = torch.cat(chunks, dim=0)
267
- diffs = (audio[1:] - audio[:-1]).abs()
268
- typical = diffs.median().item()
269
- boundary_indices = []
270
- pos = 0
271
- for c in chunks[:-1]:
272
- pos += c.shape[0]
273
- if 0 < pos < diffs.shape[0]:
274
- boundary_indices.append(pos)
275
- jumps = [diffs[i].item() for i in boundary_indices]
276
- ratios = [j / (typical + 1e-8) for j in jumps]
277
- return ratios
278
-
279
- ratios_with_lookahead = boundary_jump_stats(chunks_windowed)
280
- ratios_without_lookahead = boundary_jump_stats(chunks_no_lookahead)
281
-
282
- print(f" boundary jump ratios WITH lookahead: {[f'{r:.2f}' for r in ratios_with_lookahead]}")
283
- print(f" boundary jump ratios WITHOUT lookahead: {[f'{r:.2f}' for r in ratios_without_lookahead]}")
284
- print(" (lower ratio = smoother join; this is a structural sanity check on random")
285
- print(" weights, not a quality claim -- real weights needed for actual quality eval)")
286
- print("PASS: both configurations run without error; compare ratios above.")
287
-
288
-
289
- def test_exact_coverage_no_gaps_no_dupes():
290
- """Total tokens released as audio should account for every pushed token exactly once."""
291
- torch.manual_seed(3)
292
- model = MockMioCodecModel().eval()
293
- dim = model.config.wave_decoder_dim
294
- global_embedding = torch.randn(dim)
295
-
296
- total_tokens = 77
297
- all_tokens = torch.randn(total_tokens, dim)
298
- streamer = MioCodecWindowedStreamer(
299
- model, global_embedding,
300
- config=WindowedStreamConfig(chunk_size=20, left_context_tokens=10, right_lookahead_tokens=10),
301
- )
302
- with torch.no_grad():
303
- for i in range(total_tokens):
304
- streamer.push_token(all_tokens[i])
305
- streamer.finish()
306
-
307
- assert streamer._released_count == total_tokens, (
308
- f"released_count={streamer._released_count} != total_tokens={total_tokens} "
309
- f"-- some tokens were never released as audio, or released twice."
310
- )
311
- print(f"PASS: exactly {total_tokens} tokens accounted for, no gaps or duplicates in release bookkeeping.")
312
-
313
-
314
- if __name__ == "__main__":
315
- test_first_and_last_chunk_edge_cases()
316
- test_variable_chunk_sizes()
317
- test_min_chunk_size_rejects_1()
318
- test_boundary_smoothness_vs_causal_only()
319
- test_exact_coverage_no_gaps_no_dupes()
320
- print("\nALL WINDOWED STREAMER TESTS PASSED")