#!/usr/bin/env python3 """Generate multi-length PyTorch reference logits for native MLX parity tests. Needs torch + transformers (NOT a runtime dependency of the MLX port); run it in a throwaway environment against a local download of the upstream repo, pinned to the revision recorded in native_manifest.json. The upstream modeling code is executed (trust_remote_code), so read it first. Cases are crops of the JFK clip run through the upstream torch frontend and model, plus one padded batch to exercise the attention/conv masking path. """ import argparse import json from pathlib import Path import numpy as np import soundfile as sf import torch from transformers import AutoFeatureExtractor, AutoModelForCTC # (name, seconds). 201/551 mel frames are the lengths the first benchmark only shape-checked. CROPS = [('crop_0p5s', 0.5), ('crop_2p0s', 2.0), ('crop_3p37s', 3.37), ('crop_5p5s', 5.5), ('full_11s', None)] BATCH = ('crop_2p0s', 'full_11s') # padded batch pair, uses cases defined above def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument('--upstream', type=Path, required=True, help='local download of AuroraAI-Research/AuroraASR-1M') p.add_argument('--audio', type=Path, required=True) p.add_argument('--out', type=Path, required=True) args = p.parse_args() wave, sr = sf.read(str(args.audio), dtype='float32') if wave.ndim == 2: wave = wave.mean(axis=1) if sr != 16000: # Same resampler as aurora_runtime.read_audio, so the waveform matches the shipped fixture. import soxr wave = soxr.resample(wave, sr, 16000, quality='HQ').astype(np.float32) model = AutoModelForCTC.from_pretrained(str(args.upstream), trust_remote_code=True).eval() fe = AutoFeatureExtractor.from_pretrained(str(args.upstream), trust_remote_code=True) arrays, meta = {}, {} feats_by_name = {} with torch.no_grad(): for name, seconds in CROPS: w = wave if seconds is None else wave[:int(seconds * 16000)] f = fe(w, sampling_rate=16000, return_tensors='pt') out = model(f['input_features'], attention_mask=f['attention_mask']) feats_by_name[name] = (w, f['input_features'].numpy()) arrays[f'{name}__waveform'] = w arrays[f'{name}__input'] = f['input_features'].numpy() arrays[f'{name}__logits'] = out.logits.numpy() meta[name] = {'samples': int(w.size), 'mel_frames': int(f['input_features'].shape[1]), 'logit_frames': int(out.logits.shape[1]), 'text': model.greedy_decode(out.logits, out.output_lengths, str(args.upstream / 'bpe256.model'))[0]} # Padded batch: both utterances in one tensor, right-padded, with the attention mask. fb = fe([feats_by_name[n][0] for n in BATCH], sampling_rate=16000, return_tensors='pt') out = model(fb['input_features'], attention_mask=fb['attention_mask']) arrays['batch__input'] = fb['input_features'].numpy() arrays['batch__lengths'] = fb['attention_mask'].sum(-1).numpy().astype(np.int32) arrays['batch__logits'] = out.logits.numpy() arrays['batch__output_lengths'] = out.output_lengths.numpy().astype(np.int32) meta['batch'] = {'members': list(BATCH), 'input_shape': list(fb['input_features'].shape)} manifest = {'torch': torch.__version__, 'cases': meta} arrays['meta_json'] = np.frombuffer(json.dumps(manifest).encode(), dtype=np.uint8) args.out.parent.mkdir(parents=True, exist_ok=True) np.savez_compressed(args.out, **arrays) print(json.dumps(manifest, indent=2)) if __name__ == '__main__': main()