Qwen3-ASR / code /PYTHON.md
changh95's picture
tt-model push qwen3-asr (container)
d9f9acd verified
|
Raw History Blame Contribute Delete
2.84 kB

Qwen3-ASR on Tenstorrent p150: Python API

from tt_qwen3_asr import config as C
from tt_qwen3_asr.api import Qwen3ASR

asr = Qwen3ASR.from_config(C.resolve_config({"QWEN3ASR_VARIANT": "1.7b"}))   # opens the device, builds the programs
r = asr.transcribe("clip.wav")                       # path, bytes, (np.ndarray, sample_rate) or a 16 kHz array
print(r.language, r.text)
for s in r.segments:                                 # long audio: segments split at quiet points
    print(f"{s.start:7.2f} {s.end:7.2f} {s.text}")
r = asr.transcribe(audio, language="zh", prompt="Names: Zhang Wei, Li Na.", temperature=0.0)
r = asr.transcribe(audio, on_delta=lambda d: print(d, end="", flush=True))   # streamed text
sess = asr.stream_session(language=None, chunk_size_sec=2.0)                 # streaming input (official algorithm)
for upd in sess.feed(pcm16k_float32): print(upd["text"])
print(sess.finish()["text"])
asr.close()

transcribe(audio, *, sample_rate=None, language=None, prompt=None, temperature=None, top_k=None, top_p=None, seed=None, max_new_tokens=None, segment_seconds=None, postprocess=True, on_delta=None, cancel=None) returns a Transcription(text, language, duration, segments, timings); the parameters are those of the HTTP route (the model card's table). The compile-time configuration is config.ServeConfig (the QWEN3ASR_* environment variables of the card); QWEN3ASR_BACKEND=cpu runs the clean-room CPU reference instead of the device (slow, for checks), QWEN3ASR_BACKEND=fake a deterministic stand-in without weights (tests).

Qwen3ASR.from_config(cfg, device=dev) builds on a chip the caller opened (tt_qwen3_asr.api.open_serving_device(cfg)) and close() then leaves it open: open and close the chip on the same thread, ideally the main thread (UMD's per-chip lock belongs to the thread that took it; tt-metal tears its cluster down at exit on the main thread). The HTTP server does this; the model itself may be built and run on another thread.

Requirements: tt-metal at 44d66500520fda9f2c7060c0f6b41ec48f7ab37e with the two patches in patches/ (ETH dispatch: the 12 x 10 grid), and numpy<2 safetensors huggingface_hub tokenizers soundfile soxr (+ fastapi uvicorn websockets for the server). The weights are fetched from Qwen's repositories at the pinned commits (QWEN3ASR_WEIGHTS_DIR points at a local copy instead).

Device pipeline (tt_qwen3_asr/tt): ttnn_encoder.py (conv front-end + windowed transformer + projector, one trace per row bucket), ttnn_decoder.py (prefill, one trace per prompt bucket; the ttnn decode step), mk_dec/ (the decoder megakernel: one launch per token, on-device greedy / sampling, the stop rule), model.py (one segment: encoder -> prompt rows -> prefill -> decode). The clean-room CPU reference is tt_qwen3_asr/reference/model.py.