visualears-corpus-transfer / meta /persianize_streaming.py
Reza2kn's picture
Upload meta/persianize_streaming.py with huggingface_hub
82496cf verified
Raw
History Blame Contribute Delete
2.81 kB
#!/usr/bin/env python3
"""Persianize an EN cache-aware streaming FastConformer-Hybrid base:
keep the streaming encoder, swap in our proven Persian BPE-1024 tokenizer (reuses the
tokenizer from our trained fa model so it's identical to the offline 115M's vocab),
reinit the decoder+joint for the new vocab. Output = fa-streaming starting checkpoint
ready for Phase A via train_ddp.py.
Usage:
python persianize_streaming.py --en-base nvidia/stt_en_fastconformer_hybrid_large_streaming_multi \
--out /workspace/clean/fa_stream_114m_base.nemo
python persianize_streaming.py --en-base nvidia/stt_en_fastconformer_hybrid_medium_streaming_80ms \
--out /workspace/clean/fa_stream_32m_base.nemo"""
import argparse, os, tarfile, glob
from nemo.collections.asr.models import ASRModel
ap=argparse.ArgumentParser()
ap.add_argument("--en-base", required=True, help="EN streaming HF model id or local .nemo")
ap.add_argument("--fa-tok-src", default="/workspace/clean/phaseB2/depoison_phaseB2/checkpoints/depoison_phaseB2.nemo",
help="our fa .nemo to lift the Persian BPE-1024 tokenizer from")
ap.add_argument("--out", required=True)
ap.add_argument("--tokdir", default="/workspace/clean/fa_tok_bpe1024")
a=ap.parse_args()
# 1) lift Persian SP tokenizer (.model/.vocab) out of our fa .nemo
os.makedirs(a.tokdir, exist_ok=True)
with tarfile.open(a.fa_tok_src) as t:
for mem in t.getmembers():
bn=os.path.basename(mem.name)
if bn.endswith("tokenizer.model") or bn.endswith("tokenizer.vocab") or (bn.endswith(".model") and "tokenizer" in bn.lower()) or bn.endswith("vocab.txt"):
mem.name=bn; t.extract(mem, a.tokdir)
mdl=glob.glob(a.tokdir+"/*tokenizer.model") or glob.glob(a.tokdir+"/*.model")
voc=glob.glob(a.tokdir+"/*tokenizer.vocab") or glob.glob(a.tokdir+"/*.vocab") or glob.glob(a.tokdir+"/*vocab.txt")
assert mdl, f"no tokenizer.model found in {a.fa_tok_src}; inspect with: tar tf <nemo> | grep -i token"
os.rename(mdl[0], a.tokdir+"/tokenizer.model")
if voc: os.rename(voc[0], a.tokdir+"/tokenizer.vocab")
print("[tok] lifted Persian tokenizer ->", a.tokdir, os.listdir(a.tokdir))
# 2) load EN streaming base (encoder/streaming cfg preserved)
m=ASRModel.from_pretrained(a.en_base) if not a.en_base.endswith(".nemo") else ASRModel.restore_from(a.en_base)
print("[base] loaded", a.en_base, "| att_context_size:", getattr(m.cfg.encoder,"att_context_size",None))
# 3) swap vocab -> reinit decoder+joint for Persian (encoder weights kept)
m.change_vocabulary(new_tokenizer_dir=a.tokdir, new_tokenizer_type="bpe")
# verify it still streams
print("[ok] new vocab size:", m.tokenizer.vocab_size, "| streaming att_context preserved:", getattr(m.cfg.encoder,"att_context_size",None))
m.save_to(a.out)
print("[done] persianized streaming base ->", a.out)