Shenava-Rizeh-v1.5-CoreML-iOS15-fp16 / run_coreml_stream.py
Reza2kn's picture
Add int8 NeuralNetwork encoder and iOS18 int4 streaming encoder
bc7190e verified
Raw History Blame Contribute Delete
1.74 kB
"""macOS check / reference streaming decode for Shenava Rizeh v1.5 CoreML iOS15 (pip install coremltools numpy).
Feeds the stored reference chunks (45 fbank frames each, hop 32 = 320 ms) through the encoder step, threading all caches,
greedy RNN-T with the English block masked; prints whether tokens match the onnxruntime reference."""
import json,glob,numpy as np,coremltools as ct
import sys;enc=ct.models.MLModel(sys.argv[1] if len(sys.argv)>1 else glob.glob('*encoder_streaming*_fp16.mlmodel')[0]);dec=ct.models.MLModel(glob.glob('*decoder*.mlmodel')[0]);joi=ct.models.MLModel(glob.glob('*joiner*.mlmodel')[0])
man=json.load(open(glob.glob('*_manifest.json')[0]));exp=json.load(open('reference/expected.json',encoding='utf-8'))
toks=[l.rstrip('\n').rsplit(' ',1)[0] for l in open('tokens.txt',encoding='utf-8')]
names=[k for k in man['inputs'] if k!='x']
state={k:np.zeros(man['inputs'][k],np.int32 if k=='processed_lens' else np.float32) for k in names}
mask=np.full(2080,-1e9,np.float32);mask[np.r_[0:32,1056:2080]]=0
ch=np.fromfile('reference/chunks.f32','<f4').reshape(exp['n'],exp['T'],80)
ctx=[0,0];out=[];d=dec.predict({'y':np.array([ctx],np.int32)})['decoder_out']
for c in range(exp['n']):
o=enc.predict({'x':ch[c][None],**state});state={k:o['new_'+k].reshape(man['inputs'][k]).astype(state[k].dtype) for k in names}
for e in o['encoder_out'][0]:
k=int((joi.predict({'encoder_out':e[None].astype(np.float32),'decoder_out':d})['logit'][0]+mask).argmax())
if k: out.append(k);ctx=[ctx[-1],k];d=dec.predict({'y':np.array([ctx],np.int32)})['decoder_out']
print('text :',''.join(toks[i] for i in out).replace('▁',' ').strip());print('expected:',exp['text']);print('token match:',out==exp['tokens'])