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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/usr/bin/env python3
"""
Generate Calibration Data for Supertonic-2 ONNX Models

Creates representative input/output pairs for:
1. duration_predictor.onnx
2. text_encoder.onnx
3. vector_estimator.onnx (multiple denoising steps)
4. vocoder.onnx

Used for:
- Quantization calibration (QNN/SNPE conversion)
- Accuracy validation after porting to QCS6490
- Performance profiling

Output structure:
calibration_data/
β”œβ”€β”€ sample_001/
β”‚   β”œβ”€β”€ metadata.json
β”‚   β”œβ”€β”€ duration_predictor/
β”‚   β”‚   β”œβ”€β”€ input_text_ids.raw
β”‚   β”‚   β”œβ”€β”€ input_style_dp.raw
β”‚   β”‚   β”œβ”€β”€ input_text_mask.raw
β”‚   β”‚   └── output_duration.raw
β”‚   β”œβ”€β”€ text_encoder/
β”‚   β”‚   β”œβ”€β”€ input_text_ids.raw
β”‚   β”‚   β”œβ”€β”€ input_style_ttl.raw
β”‚   β”‚   β”œβ”€β”€ input_text_mask.raw
β”‚   β”‚   └── output_text_emb.raw
β”‚   β”œβ”€β”€ vector_estimator/
β”‚   β”‚   β”œβ”€β”€ step_000/
β”‚   β”‚   β”‚   β”œβ”€β”€ input_noisy_latent.raw
β”‚   β”‚   β”‚   β”œβ”€β”€ input_text_emb.raw
β”‚   β”‚   β”‚   β”œβ”€β”€ input_style_ttl.raw
β”‚   β”‚   β”‚   β”œβ”€β”€ input_text_mask.raw
β”‚   β”‚   β”‚   β”œβ”€β”€ input_latent_mask.raw
β”‚   β”‚   β”‚   β”œβ”€β”€ input_current_step.raw
β”‚   β”‚   β”‚   β”œβ”€β”€ input_total_step.raw
β”‚   β”‚   β”‚   └── output_latent.raw
β”‚   β”‚   β”œβ”€β”€ step_001/...
β”‚   β”‚   └── step_N/...
β”‚   └── vocoder/
β”‚       β”œβ”€β”€ input_latent.raw
β”‚       └── output_waveform.raw
└── sample_002/...
"""

import argparse
import json
import shutil
from pathlib import Path
from typing import Dict, List, Tuple

import numpy as np

from supertonic_inference import SupertonicTTS


# Calibration test cases with varying complexity
# All English, F1 voice, 10 diffusion steps
CALIBRATION_SAMPLES = [
    # Short utterances (5-10 words)
    {
        "text": "Hello world, this is a test.",
        "voice": "F1",
        "lang": "en",
        "steps": 10,
        "seed": 42,
        "category": "short"
    },
    {
        "text": "Good morning everyone.",
        "voice": "F1",
        "lang": "en",
        "steps": 10,
        "seed": 100,
        "category": "short"
    },
    {
        "text": "The weather is nice today.",
        "voice": "F1",
        "lang": "en",
        "steps": 10,
        "seed": 150,
        "category": "short"
    },

    # Medium utterances (15-25 words)
    {
        "text": "The quick brown fox jumps over the lazy dog. This sentence contains every letter of the alphabet.",
        "voice": "F1",
        "lang": "en",
        "steps": 10,
        "seed": 200,
        "category": "medium"
    },
    {
        "text": "Machine learning models require careful calibration to ensure accuracy on edge devices.",
        "voice": "F1",
        "lang": "en",
        "steps": 10,
        "seed": 300,
        "category": "medium"
    },
    {
        "text": "Text to speech synthesis has improved dramatically with the advent of neural networks and deep learning.",
        "voice": "F1",
        "lang": "en",
        "steps": 10,
        "seed": 350,
        "category": "medium"
    },

    # Longer utterances (35-50 words)
    {
        "text": "Artificial intelligence has revolutionized many aspects of our daily lives, from smartphone assistants to recommendation systems. The deployment of neural networks on edge devices requires careful optimization, including quantization and calibration, to maintain accuracy while meeting strict latency and power constraints.",
        "voice": "F1",
        "lang": "en",
        "steps": 10,
        "seed": 400,
        "category": "long"
    },
    {
        "text": "Qualcomm's Hexagon processor provides dedicated hardware acceleration for neural network inference. By leveraging the tensor processing units and optimized kernels, developers can achieve real-time performance for complex models like text-to-speech systems on mobile and edge devices.",
        "voice": "F1",
        "lang": "en",
        "steps": 10,
        "seed": 450,
        "category": "long"
    },

    # Edge cases and special content
    {
        "text": "Numbers: 1, 2, 3, 4, 5. Dates: January 1st, 2024. Time: 3:45 PM.",
        "voice": "F1",
        "lang": "en",
        "steps": 10,
        "seed": 500,
        "category": "numbers"
    },
    {
        "text": "Dr. Smith's presentation at 9:00 AM covered topics including: AI, ML, NLP, and TTS. The conference runs from Mon. to Fri.",
        "voice": "F1",
        "lang": "en",
        "steps": 10,
        "seed": 600,
        "category": "abbreviations"
    },
]


class CalibrationDataGenerator:
    """Generate and save calibration data for all 4 ONNX models"""

    def __init__(self, model_dir: str = "model/onnx", output_dir: str = "calibration_data"):
        self.tts = SupertonicTTS(model_dir=model_dir)
        self.output_dir = Path(output_dir)

    def save_tensor(self, filepath: Path, tensor: np.ndarray):
        """Save tensor as raw binary file with shape metadata"""
        # Save raw data - preserve original dtype
        tensor.tofile(filepath)

        # Save shape metadata as JSON
        shape_file = filepath.with_suffix('.shape.json')
        metadata = {
            'shape': list(tensor.shape),
            'dtype': str(tensor.dtype),
            'size_bytes': tensor.nbytes
        }
        with open(shape_file, 'w') as f:
            json.dump(metadata, f, indent=2)

    def generate_sample(self, sample_config: Dict, sample_idx: int):
        """Generate calibration data for one sample"""

        sample_dir = self.output_dir / f"sample_{sample_idx:03d}"
        sample_dir.mkdir(parents=True, exist_ok=True)

        # Save sample metadata
        metadata_file = sample_dir / "metadata.json"
        with open(metadata_file, 'w') as f:
            json.dump(sample_config, f, indent=2)

        print(f"\n{'='*80}")
        print(f"Sample {sample_idx:03d}: {sample_config['category']}")
        print(f"Text: '{sample_config['text'][:60]}...'")
        print(f"Voice: {sample_config['voice']} | Lang: {sample_config['lang']} | Steps: {sample_config['steps']}")
        print(f"{'='*80}")

        # Load voice style
        style_ttl, style_dp = self.tts.load_voice_style(sample_config['voice'])

        # Step 1: Text to IDs (preprocessing)
        print("[1/4] Text preprocessing...")
        text_ids, text_mask = self.tts.text_to_ids(sample_config['text'], sample_config['lang'])
        original_len = text_ids.shape[1]
        print(f"  Tokens: {original_len}")

        # Pad/truncate to fixed length 128 for QNN conversion
        max_len = 128
        if original_len > max_len:
            print(f"  WARNING: Truncating from {original_len} to {max_len}")
            text_ids = text_ids[:, :max_len]
            text_mask = text_mask[:, :, :max_len]
        elif original_len < max_len:
            # Pad text_ids with zeros
            padding = np.zeros((1, max_len - original_len), dtype=text_ids.dtype)
            text_ids = np.concatenate([text_ids, padding], axis=1)
            # Pad text_mask with zeros
            padding_mask = np.zeros((1, 1, max_len - original_len), dtype=text_mask.dtype)
            text_mask = np.concatenate([text_mask, padding_mask], axis=2)
            print(f"  Padded from {original_len} to {max_len}")

        # Step 2: Duration Predictor
        print("[2/4] Duration predictor...")
        dp_dir = sample_dir / "duration_predictor"
        dp_dir.mkdir(exist_ok=True)

        # Save inputs
        self.save_tensor(dp_dir / "input_text_ids.raw", text_ids)
        self.save_tensor(dp_dir / "input_style_dp.raw", style_dp)
        self.save_tensor(dp_dir / "input_text_mask.raw", text_mask)

        # Run model and save output
        duration_raw = self.tts.duration_predictor.run(None, {
            "text_ids": text_ids,
            "style_dp": style_dp,
            "text_mask": text_mask
        })[0]

        self.save_tensor(dp_dir / "output_duration.raw", duration_raw)
        print(f"  Duration: {duration_raw[0]:.2f}s")

        # Step 3: Text Encoder
        print("[3/4] Text encoder...")
        te_dir = sample_dir / "text_encoder"
        te_dir.mkdir(exist_ok=True)

        # Save inputs
        self.save_tensor(te_dir / "input_text_ids.raw", text_ids)
        self.save_tensor(te_dir / "input_style_ttl.raw", style_ttl)
        self.save_tensor(te_dir / "input_text_mask.raw", text_mask)

        # Run model and save output
        text_emb = self.tts.text_encoder.run(None, {
            "text_ids": text_ids,
            "style_ttl": style_ttl,
            "text_mask": text_mask
        })[0]

        self.save_tensor(te_dir / "output_text_emb.raw", text_emb)
        print(f"  Text embedding: {text_emb.shape}")

        # Step 4: Vector Estimator (multiple steps)
        print("[4/4] Vector estimator (flow matching)...")
        ve_dir = sample_dir / "vector_estimator"
        ve_dir.mkdir(exist_ok=True)

        # Initialize latent noise
        duration = duration_raw / sample_config.get('speed', 1.0)
        wav_length = int(duration[0] * self.tts.sample_rate)
        chunk_size = self.tts.base_chunk_size * self.tts.chunk_compress_factor
        latent_len = (wav_length + chunk_size - 1) // chunk_size
        latent_dim = self.tts.latent_dim * self.tts.chunk_compress_factor

        # Set seed for reproducibility
        np.random.seed(sample_config['seed'])
        noisy_latent = np.random.randn(1, latent_dim, latent_len).astype(np.float32)

        # Create latent mask
        latent_length = np.array([latent_len], dtype=np.int64)
        latent_mask_ids = np.arange(latent_len) < latent_length[:, None]
        latent_mask = latent_mask_ids.astype(np.float32).reshape(1, 1, -1)
        noisy_latent = noisy_latent * latent_mask

        # Pad/truncate latent to fixed length 192 for QNN conversion
        max_latent_len = 192
        original_latent_len = latent_len
        if latent_len > max_latent_len:
            print(f"  WARNING: Truncating latent from {latent_len} to {max_latent_len}")
            noisy_latent = noisy_latent[:, :, :max_latent_len]
            latent_mask = latent_mask[:, :, :max_latent_len]
            latent_len = max_latent_len
        elif latent_len < max_latent_len:
            # Pad noisy_latent with zeros
            padding = np.zeros((1, latent_dim, max_latent_len - latent_len), dtype=noisy_latent.dtype)
            noisy_latent = np.concatenate([noisy_latent, padding], axis=2)
            # Pad latent_mask with zeros
            padding_mask = np.zeros((1, 1, max_latent_len - latent_len), dtype=latent_mask.dtype)
            latent_mask = np.concatenate([latent_mask, padding_mask], axis=2)
            print(f"  Padded latent from {original_latent_len} to {max_latent_len}")
            latent_len = max_latent_len

        # Diffusion loop - save each step
        diffusion_steps = sample_config['steps']
        total_step = np.array([diffusion_steps], dtype=np.float32)

        for step in range(diffusion_steps):
            step_dir = ve_dir / f"step_{step:03d}"
            step_dir.mkdir(exist_ok=True)

            current_step = np.array([step], dtype=np.float32)

            # Save inputs for this step
            self.save_tensor(step_dir / "input_noisy_latent.raw", noisy_latent)
            self.save_tensor(step_dir / "input_text_emb.raw", text_emb)
            self.save_tensor(step_dir / "input_style_ttl.raw", style_ttl)
            self.save_tensor(step_dir / "input_text_mask.raw", text_mask)
            self.save_tensor(step_dir / "input_latent_mask.raw", latent_mask)
            self.save_tensor(step_dir / "input_current_step.raw", current_step)
            self.save_tensor(step_dir / "input_total_step.raw", total_step)

            # Run model
            noisy_latent = self.tts.vector_estimator.run(None, {
                "noisy_latent": noisy_latent,
                "text_emb": text_emb,
                "style_ttl": style_ttl,
                "text_mask": text_mask,
                "latent_mask": latent_mask,
                "current_step": current_step,
                "total_step": total_step
            })[0]

            # Save output
            self.save_tensor(step_dir / "output_latent.raw", noisy_latent)

            if (step + 1) % 5 == 0 or step == diffusion_steps - 1:
                print(f"  Step {step + 1}/{diffusion_steps}")

        # Step 5: Vocoder
        print("[5/5] Vocoder...")
        voc_dir = sample_dir / "vocoder"
        voc_dir.mkdir(exist_ok=True)

        # Save input (final denoised latent)
        self.save_tensor(voc_dir / "input_latent.raw", noisy_latent)

        # Run model and save output
        wav = self.tts.vocoder.run(None, {"latent": noisy_latent})[0]
        wav_trimmed = wav[0, :wav_length]

        self.save_tensor(voc_dir / "output_waveform.raw", wav_trimmed)
        print(f"  Waveform: {len(wav_trimmed)} samples @ {self.tts.sample_rate} Hz")
        print(f"  Duration: {len(wav_trimmed)/self.tts.sample_rate:.2f}s")

        print(f"\nβœ“ Sample {sample_idx:03d} complete: {sample_dir}")

    def generate_all(self, samples: List[Dict] = None):
        """Generate calibration data for all samples"""

        if samples is None:
            samples = CALIBRATION_SAMPLES

        # Clear existing calibration data
        if self.output_dir.exists():
            print(f"Clearing existing calibration data at {self.output_dir}...")
            shutil.rmtree(self.output_dir)

        self.output_dir.mkdir(parents=True, exist_ok=True)

        # Generate summary
        summary = {
            "num_samples": len(samples),
            "samples": [],
            "model_info": {
                "sample_rate": self.tts.sample_rate,
                "base_chunk_size": self.tts.base_chunk_size,
                "chunk_compress_factor": self.tts.chunk_compress_factor,
                "latent_dim": self.tts.latent_dim,
                "effective_chunk_size": self.tts.base_chunk_size * self.tts.chunk_compress_factor
            }
        }

        # Generate each sample
        for idx, sample_config in enumerate(samples, start=1):
            try:
                self.generate_sample(sample_config, idx)
                summary["samples"].append({
                    "sample_id": f"sample_{idx:03d}",
                    "text": sample_config["text"],
                    "voice": sample_config["voice"],
                    "lang": sample_config["lang"],
                    "category": sample_config["category"],
                    "diffusion_steps": sample_config["steps"],
                    "seed": sample_config["seed"]
                })
            except Exception as e:
                print(f"\nβœ— Error generating sample {idx:03d}: {e}")
                import traceback
                traceback.print_exc()
                continue

        # Save summary
        summary_file = self.output_dir / "calibration_summary.json"
        with open(summary_file, 'w') as f:
            json.dump(summary, f, indent=2)

        print(f"\n{'='*80}")
        print(f"βœ“ Calibration data generation complete!")
        print(f"  Total samples: {len(summary['samples'])}")
        print(f"  Output directory: {self.output_dir}")
        print(f"  Summary: {summary_file}")
        print(f"{'='*80}\n")

        # Print statistics
        print("Sample Statistics:")
        print(f"  Short utterances:  {sum(1 for s in summary['samples'] if 'short' in s['category'])}")
        print(f"  Medium utterances: {sum(1 for s in summary['samples'] if 'medium' in s['category'])}")
        print(f"  Long utterances:   {sum(1 for s in summary['samples'] if 'long' in s['category'])}")
        print(f"  English samples:   {sum(1 for s in summary['samples'] if s['lang'] == 'en')}")
        print(f"  Spanish samples:   {sum(1 for s in summary['samples'] if s['lang'] == 'es')}")
        print(f"  Korean samples:    {sum(1 for s in summary['samples'] if s['lang'] == 'ko')}")
        print(f"  Unique voices:     {len(set(s['voice'] for s in summary['samples']))}")

        return summary


def main():
    parser = argparse.ArgumentParser(
        description="Generate calibration data for Supertonic-2 ONNX models",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Usage Examples:
  # Generate all calibration samples
  python generate_calibration_data.py

  # Custom output directory
  python generate_calibration_data.py --output-dir /path/to/calibration

  # Use custom model directory
  python generate_calibration_data.py --model-dir /path/to/onnx/models

Output Structure:
  calibration_data/
  β”œβ”€β”€ sample_001/
  β”‚   β”œβ”€β”€ metadata.json
  β”‚   β”œβ”€β”€ duration_predictor/ (inputs + output)
  β”‚   β”œβ”€β”€ text_encoder/ (inputs + output)
  β”‚   β”œβ”€β”€ vector_estimator/
  β”‚   β”‚   β”œβ”€β”€ step_000/ (inputs + output)
  β”‚   β”‚   β”œβ”€β”€ step_001/
  β”‚   β”‚   └── ...
  β”‚   └── vocoder/ (input + output)
  └── calibration_summary.json

Files are saved as:
  - .raw: Binary float32 data
  - .shape.json: Shape and dtype metadata
"""
    )

    parser.add_argument(
        "--model-dir",
        type=str,
        default="model/onnx",
        help="Path to ONNX models directory (default: model/onnx)"
    )
    parser.add_argument(
        "--output-dir",
        type=str,
        default="calibration_data",
        help="Output directory for calibration data (default: calibration_data)"
    )

    args = parser.parse_args()

    # Generate calibration data
    generator = CalibrationDataGenerator(
        model_dir=args.model_dir,
        output_dir=args.output_dir
    )

    generator.generate_all()


if __name__ == "__main__":
    main()