File size: 11,339 Bytes
a1bd3a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
#!/usr/bin/env python3
"""AuroraASR-1M lightweight macOS runtime.

Dependencies: numpy soundfile sentencepiece, plus optional scipy for resampling,
soxr (HQ reference resampling); onnxruntime / onnxruntime-ep-mlx OR coremltools.
Exports contain the acoustic model only; this reproduces upstream log-mel + greedy CTC.
"""
import argparse
import json
import math
import platform
import time
from functools import lru_cache
from pathlib import Path

import numpy as np


@lru_cache(maxsize=8)
def mel_filterbank(sr=16000, n_fft=512, n_mels=80, fmin=20.0, fmax=8000.0):
    def hz2mel(f):
        return 2595.0 * np.log10(1.0 + f / 700.0)
    def mel2hz(m):
        return 700.0 * (10.0 ** (m / 2595.0) - 1.0)
    edges = mel2hz(np.linspace(hz2mel(fmin), hz2mel(fmax), n_mels + 2))
    frequencies = np.linspace(0, sr / 2, n_fft // 2 + 1)
    bank = np.zeros((n_mels, frequencies.size), np.float32)
    for k in range(n_mels):
        left, center, right = edges[k:k + 3]
        rise = (frequencies - left) / (center - left)
        fall = (right - frequencies) / (right - center)
        bank[k] = np.maximum(0.0, np.minimum(rise, fall))
        if bank[k].sum() == 0:
            bank[k, int(np.argmin(np.abs(frequencies - center)))] = 1.0
    return bank


def features(audio, sr=16000, *, n_fft=512, win_length=400, hop_length=160,
             n_mels=80, fmin=20.0, fmax=8000.0):
    """Match AuroraASRFeatureExtractor: reflect STFT, periodic Hann, per-bin unbiased std."""
    if sr != 16000:
        raise ValueError('Resample audio to 16000 Hz before feature extraction')
    wave = np.asarray(audio, dtype=np.float32)
    if wave.ndim != 1 or wave.size < n_fft // 2 + 1:
        raise ValueError('Expected mono audio with at least 257 samples')
    padded = np.pad(wave, (n_fft // 2, n_fft // 2), mode='reflect')
    frames = np.lib.stride_tricks.sliding_window_view(padded, n_fft)[::hop_length]
    # torch.hann_window(win_length) is periodic; centered in n_fft STFT window.
    window = np.hanning(win_length + 1)[:-1]
    left = (n_fft - win_length) // 2
    window = np.pad(window, (left, n_fft - win_length - left))
    spectrum = np.fft.rfft(frames * window[None, :], n=n_fft, axis=-1)
    power = spectrum.real ** 2 + spectrum.imag ** 2
    bank = mel_filterbank(sr, n_fft, n_mels, fmin, fmax)
    logmel = np.log(power @ bank.T + 1e-10)
    normalized = (logmel - logmel.mean(axis=0, keepdims=True)) / (
        logmel.std(axis=0, ddof=1, keepdims=True) + 1e-5)
    return np.ascontiguousarray(normalized[None].astype(np.float32))


@lru_cache(maxsize=4)
def load_sentencepiece(model_file):
    import sentencepiece as spm
    return spm.SentencePieceProcessor(model_file=str(model_file))


def ctc_text(logits, spm_path, blank_id=0):
    sp = load_sentencepiece(str(Path(spm_path).expanduser().resolve()))
    path = np.asarray(logits).argmax(axis=-1)[0].astype(np.int64)
    distinct = np.ones(path.size, dtype=bool)
    distinct[1:] = path[1:] != path[:-1]
    ids = [int(x) - 1 for x in path[distinct] if int(x) != blank_id]
    return sp.decode(ids)


def load_model(model_dir, backend='cpu', precision='fp32', dynamic=False, compute_unit='all'):
    model_dir = Path(model_dir)
    if backend in ('cpu', 'mlx'):
        import onnxruntime as ort
        path = model_dir / ('aurora_dynamic.onnx' if dynamic else 'aurora_asr.onnx')
        if not path.exists():
            raise FileNotFoundError(f'Missing {path}; dynamic export must pass Colab parity first')
        if backend == 'mlx':
            import onnxruntime_ep_mlx
            onnxruntime_ep_mlx.register_execution_provider_library()
            providers = ['MLXExecutionProvider', 'CPUExecutionProvider']
        else:
            providers = ['CPUExecutionProvider']
        sess = ort.InferenceSession(str(path), providers=providers)
        if backend == 'mlx' and 'MLXExecutionProvider' not in sess.get_providers():
            raise RuntimeError('MLX plugin was not registered; refusing to report MLX speed')
        name = sess.get_inputs()[0].name
        shape = sess.get_inputs()[0].shape
        def infer(x):
            if isinstance(shape[1], int) and x.shape[1] != shape[1]:
                raise ValueError(f'Model is static at {shape}; requested {x.shape}. Use a matching clip or --dynamic.')
            return sess.run(None, {name: x})[0]
        return infer, {'providers':sess.get_providers(), 'model':path.name}
    if backend == 'coreml':
        import coremltools as ct
        package = model_dir / f'aurora_{precision}.mlpackage'
        if not package.is_dir() or not (package/'Manifest.json').exists():
            raise FileNotFoundError(
                f'Core ML package missing: {package.resolve()}. '
                'Use the sibling AuroraASR-1M-CoreML export instead of the MLX export.')
        units = {'all':ct.ComputeUnit.ALL, 'cpu':ct.ComputeUnit.CPU_ONLY,
                 'gpu':ct.ComputeUnit.CPU_AND_GPU}[compute_unit]
        mdl = ct.models.MLModel(str(package), compute_units=units)
        name = mdl.get_spec().description.input[0].name
        out_name = mdl.get_spec().description.output[0].name
        expected = tuple(mdl.get_spec().description.input[0].type.multiArrayType.shape)
        def infer(x):
            if tuple(x.shape) != expected:
                raise ValueError(f'Core ML package has static input {expected}, got {x.shape}')
            return np.asarray(mdl.predict({name:x})[out_name])
        return infer, {'compute_units_requested':compute_unit, 'ane_placement_verified':False,
                       'model':package.name}
    raise ValueError(f'Invalid backend: {backend}')


def read_audio(filename, resampler='soxr_hq'):
    """Read/mono/resample identically to the v3 reference path by default."""
    import soundfile as sf
    wave, sr = sf.read(str(filename), dtype='float32')
    if wave.ndim == 2:
        wave = wave.mean(axis=1)
    if sr != 16000:
        if resampler == 'soxr_hq':
            import soxr
            wave = soxr.resample(wave, sr, 16000, quality='HQ').astype(np.float32)
        elif resampler == 'scipy_poly':
            from scipy.signal import resample_poly
            gcd = math.gcd(int(sr), 16000)
            wave = resample_poly(wave, 16000 // gcd, int(sr) // gcd).astype(np.float32)
        else:
            raise ValueError('Unknown resampler')
        sr = 16000
    return wave, sr


def main():
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument('--model-dir', type=Path, required=True)
    p.add_argument('--audio', type=Path, required=True)
    p.add_argument('--backend', choices=['cpu','mlx','coreml'], default='cpu')
    p.add_argument('--precision', choices=['fp32','fp16'], default='fp32')
    p.add_argument('--compute-unit', choices=['all','cpu','gpu'], default='all')
    p.add_argument('--dynamic', action='store_true')
    p.add_argument('--resampler', choices=['soxr_hq','scipy_poly'], default='soxr_hq')
    p.add_argument('--warmup', type=int, default=8)
    p.add_argument('--runs', type=int, default=40)
    args = p.parse_args()
    # The recorded file I/O latency can vary by seconds (cold cache, external volumes).
    read_start = time.perf_counter()
    wave, sr = read_audio(args.audio, args.resampler)
    audio_seconds = len(wave) / sr
    io_ms = (time.perf_counter() - read_start) * 1000
    fe_start = time.perf_counter()
    config = json.loads((args.model_dir/'upstream_reference'/'config.json').read_text())
    feat = features(wave, sr, n_fft=config.get('n_fft',512),
        win_length=config.get('win_length',400), hop_length=config.get('hop_length',160),
        n_mels=config.get('n_mels',80))
    feature_ms = (time.perf_counter() - fe_start) * 1000
    model_start = time.perf_counter()
    runner, details = load_model(args.model_dir, args.backend, args.precision,
                                 args.dynamic, args.compute_unit)
    model_load_ms = (time.perf_counter() - model_start) * 1000
    st = time.perf_counter()
    logits = runner(feat)
    first_inference_ms = (time.perf_counter() - st) * 1000
    if logits.ndim != 3 or logits.shape[0] != 1 or logits.shape[-1] != 257:
        raise RuntimeError(f'Unexpected CTC logits: {logits.shape}')
    # Do not benchmark the first inference: Core ML / MLX may compile or specialize.
    for _ in range(max(0,args.warmup)):
        warm_logits = runner(feat)
    timings = []
    for _ in range(max(1,args.runs)):
        st = time.perf_counter(); warm_logits = runner(feat)
        timings.append((time.perf_counter() - st) * 1000)
    if not np.array_equal(np.argmax(logits,axis=-1),np.argmax(warm_logits,axis=-1)):
        raise RuntimeError('First-vs-warm CTC output changed; investigate backend determinism')
    # Separate first tokenizer load (disk/initialization) from warm decode cost.
    spm_file=args.model_dir/'upstream_reference'/'bpe256.model'
    st = time.perf_counter()
    text = ctc_text(warm_logits, spm_file, config.get('blank_token_id',0))
    first_decode_ms=(time.perf_counter()-st)*1000
    decode_times=[]
    for _ in range(max(5,min(20,args.runs))):
        st=time.perf_counter()
        decoded=ctc_text(warm_logits,spm_file,config.get('blank_token_id',0))
        decode_times.append((time.perf_counter()-st)*1000)
        if decoded!=text:
            raise RuntimeError('CTC output changed on repeated decode')
    warm_decode_ms=float(np.median(decode_times))
    warm_median_ms = float(np.median(timings))
    warm_pipeline_ms = feature_ms + warm_median_ms + warm_decode_ms
    # Actually exercise a whole frontend+acoustic+CTC warm run. No disk I/O.
    def pipeline_once():
        x=features(wave,sr,n_fft=config.get('n_fft',512),
            win_length=config.get('win_length',400),hop_length=config.get('hop_length',160),
            n_mels=config.get('n_mels',80))
        return ctc_text(runner(x),spm_file,config.get('blank_token_id',0))
    for _ in range(3):
        if pipeline_once()!=text:
            raise RuntimeError('Whole-pipeline CTC mismatch between repeated passes')
    pipeline_times=[]
    for _ in range(max(5,min(20,args.runs))):
        st=time.perf_counter(); t=pipeline_once();pipeline_times.append((time.perf_counter()-st)*1000)
        if t!=text:raise RuntimeError('Whole-pipeline transcript changed')
    print(json.dumps({'backend':args.backend,'precision':args.precision,'shape':list(feat.shape),
       'text':text, 'resampler':args.resampler,'audio_seconds':audio_seconds,
       'file_io_ms':io_ms,'feature_ms':feature_ms,'model_load_ms':model_load_ms,
       'acoustic_first_inference_ms':first_inference_ms,
       'acoustic_warm_p50_ms':warm_median_ms,
       'acoustic_warm_p95_ms':float(np.percentile(timings,95)),
       'ctc_decode_first_ms':first_decode_ms,
       'ctc_decode_warm_p50_ms':warm_decode_ms,
       'warm_whole_pipeline_p50_ms':float(np.median(pipeline_times)),
       'warm_whole_pipeline_p95_ms':float(np.percentile(pipeline_times,95)),
       'warm_pipeline_estimate_excluding_io_and_model_load_ms':warm_pipeline_ms,
       'warm_pipeline_estimate_rtf':warm_pipeline_ms/(audio_seconds*1000),
       'warning':'Warm whole-pipeline measures repeated same-file in-memory runs; test separate real clips for actual workload performance.',
       'backend_details':details},indent=2))


if __name__ == '__main__':
    main()