|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""Regenerate the cache-aware streaming golden for the FastConformer-RNNT test. |
| 5 | +
|
| 6 | +This produces ``testdata/golden/speech/nemotron_fastconformer_rnnt_streaming.npz`` |
| 7 | +by driving the *real* NeMo encoder's streaming ``forward`` (with explicit |
| 8 | +``cache_last_channel`` / ``cache_last_time`` / ``cache_last_channel_len`` state) |
| 9 | +over two consecutive feature chunks. It must run inside an environment that has |
| 10 | +``nemo_toolkit`` installed (not a mobius runtime dependency):: |
| 11 | +
|
| 12 | + python -m venv /tmp/nemo_ref_venv |
| 13 | + source /tmp/nemo_ref_venv/bin/activate |
| 14 | + pip install "nemo_toolkit[asr]==2.7.3" |
| 15 | + python scripts/generate_nemo_rnnt_streaming_golden.py \ |
| 16 | + --model nvidia/nemotron-speech-streaming-en-0.6b \ |
| 17 | + --revision 7a9b763e6c5fb103da690219c049fac917aa50b1 \ |
| 18 | + --out testdata/golden/speech/nemotron_fastconformer_rnnt_streaming.npz |
| 19 | +
|
| 20 | +To keep the committed reference small, only the per-chunk feature inputs and the |
| 21 | +encoder outputs / lengths / cache-length scalars are stored (not the full |
| 22 | +multi-megabyte cache tensors). The streaming parity test validates cache |
| 23 | +correctness implicitly by chaining chunk-0's ONNX output caches into chunk-1 and |
| 24 | +matching chunk-1's encoder output against this NeMo reference. |
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +import argparse |
| 30 | +import json |
| 31 | + |
| 32 | +import numpy as np |
| 33 | +import torch |
| 34 | + |
| 35 | +_SEED = 0 |
| 36 | +_FEAT_DIM = 128 |
| 37 | +_CHUNK = 120 # feature frames per streaming chunk |
| 38 | + |
| 39 | + |
| 40 | +def main() -> None: |
| 41 | + parser = argparse.ArgumentParser(description=__doc__) |
| 42 | + parser.add_argument("--model", default="nvidia/nemotron-speech-streaming-en-0.6b") |
| 43 | + parser.add_argument( |
| 44 | + "--revision", |
| 45 | + default="7a9b763e6c5fb103da690219c049fac917aa50b1", |
| 46 | + help="HuggingFace Hub commit SHA to pin the reference model.", |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + "--out", |
| 50 | + default="testdata/golden/speech/nemotron_fastconformer_rnnt_streaming.npz", |
| 51 | + ) |
| 52 | + args = parser.parse_args() |
| 53 | + |
| 54 | + import nemo # type: ignore[import-not-found] |
| 55 | + import nemo.collections.asr as nemo_asr # type: ignore[import-not-found] |
| 56 | + from huggingface_hub import hf_hub_download |
| 57 | + |
| 58 | + torch.manual_seed(_SEED) |
| 59 | + |
| 60 | + nemo_path = hf_hub_download( |
| 61 | + repo_id=args.model, |
| 62 | + filename="nemotron-speech-streaming-en-0.6b.nemo", |
| 63 | + revision=args.revision, |
| 64 | + ) |
| 65 | + model = nemo_asr.models.ASRModel.restore_from(nemo_path, map_location="cpu") |
| 66 | + model.eval() |
| 67 | + enc = model.encoder |
| 68 | + enc.setup_streaming_params() |
| 69 | + |
| 70 | + def step(feats, ch, ct, cl): |
| 71 | + with torch.no_grad(): |
| 72 | + return enc( |
| 73 | + audio_signal=feats, |
| 74 | + length=torch.tensor([_CHUNK]), |
| 75 | + cache_last_channel=ch, |
| 76 | + cache_last_time=ct, |
| 77 | + cache_last_channel_len=cl, |
| 78 | + ) |
| 79 | + |
| 80 | + ch0, ct0, cl0 = enc.get_initial_cache_state(batch_size=1) |
| 81 | + f0 = torch.randn(1, _FEAT_DIM, _CHUNK) |
| 82 | + out0, len0, ch1, ct1, cl1 = step(f0, ch0, ct0, cl0) |
| 83 | + f1 = torch.randn(1, _FEAT_DIM, _CHUNK) |
| 84 | + out1, len1, ch2, ct2, cl2 = step(f1, ch1, ct1, cl1) |
| 85 | + del ch2, ct2 # full out-caches of the second chunk are not part of the golden |
| 86 | + |
| 87 | + meta = { |
| 88 | + "model_id": args.model, |
| 89 | + "revision": args.revision, |
| 90 | + "nemo_version": nemo.__version__, |
| 91 | + "dtype": "float32", |
| 92 | + "seed": _SEED, |
| 93 | + "feat_dim": _FEAT_DIM, |
| 94 | + "chunk_frames": _CHUNK, |
| 95 | + "last_channel_cache_size": int(enc.streaming_cfg.last_channel_cache_size), |
| 96 | + "drop_extra_pre_encoded": int(enc.streaming_cfg.drop_extra_pre_encoded), |
| 97 | + } |
| 98 | + |
| 99 | + np.savez_compressed( |
| 100 | + args.out, |
| 101 | + f0=f0.numpy().astype(np.float32), |
| 102 | + f1=f1.numpy().astype(np.float32), |
| 103 | + out0=out0.numpy().astype(np.float32), |
| 104 | + out1=out1.numpy().astype(np.float32), |
| 105 | + len0=len0.numpy().astype(np.int64), |
| 106 | + len1=len1.numpy().astype(np.int64), |
| 107 | + cl1=cl1.numpy().astype(np.int64), |
| 108 | + cl2=cl2.numpy().astype(np.int64), |
| 109 | + meta=np.array(json.dumps(meta)), |
| 110 | + ) |
| 111 | + print(f"saved {args.out}\n{json.dumps(meta, indent=2)}") |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments