This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/tests/qwen3_asr_reference.py
Deadcode 1e968f6e1d radiance 1.7.0
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves
  every shipped container
- Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime
- /v1/messages works with Claude Code as it ships (gearwave00001)
- a long agent conversation keeps its cached history when the client rewrites its last turn
  (manueloverride)
- a stream's first event goes out with the first token, so llama-benchy measures prefill right
- opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42)
- four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b)
- NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA
  adapters (baKon)
- many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool
  arguments and the CPU's vision and audio encoders
- plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
2026-10-16 18:25:18 +02:00

86 lines
4.1 KiB
Python

"""qwen3_asr_reference.py MODEL_DIR WAV OUT_DIR [--language English] [--dtype bfloat16]
Transformers' own Qwen3-ASR, run on the CPU, as the reference arch/qwen3asr_bf16 is checked against.
It writes, for one 16 kHz mono WAV:
OUT_DIR/features.f32 the log-mel features [frames, 128] (Qwen3ASRFeatureExtractor), real
frames only, frame-major -- the layout core/mm stages
OUT_DIR/audio.bf16 the projected audio rows [tokens, hidden] the decoder's placeholders take
OUT_DIR/result.json shapes, the prompt token ids, the greedy reply's token ids and its text
RADIANCE_DEBUG_MM_DUMP writes the engine's own encoder rows as raw bf16, so the two files compare
byte for byte in layout. Every number here comes from transformers, not from this script."""
import argparse, json, os, wave
import numpy as np
import torch
from transformers import AutoProcessor, Qwen3ASRForConditionalGeneration
def read_wav(path):
with wave.open(path, "rb") as w:
if w.getsampwidth() != 2:
raise SystemExit(f"{path}: expected 16-bit PCM")
rate, ch, n = w.getframerate(), w.getnchannels(), w.getnframes()
x = np.frombuffer(w.readframes(n), dtype="<i2").astype(np.float32) / 32768.0
if ch > 1:
x = x.reshape(-1, ch).mean(axis=1)
if rate != 16000:
raise SystemExit(f"{path}: expected 16 kHz, got {rate}")
return x
def main():
ap = argparse.ArgumentParser()
ap.add_argument("model")
ap.add_argument("wav")
ap.add_argument("out")
ap.add_argument("--language", default=None, help="a name the model knows (English), or none")
ap.add_argument("--context", default="", help="system-prompt context text")
ap.add_argument("--dtype", default="bfloat16", choices=["bfloat16", "float32"])
ap.add_argument("--max-new-tokens", type=int, default=256)
a = ap.parse_args()
os.makedirs(a.out, exist_ok=True)
torch.manual_seed(0)
proc = AutoProcessor.from_pretrained(a.model)
model = Qwen3ASRForConditionalGeneration.from_pretrained(a.model, dtype=getattr(torch, a.dtype))
model.eval()
audio = read_wav(a.wav)
conv = [{"role": "system", "content": [{"type": "text", "text": a.context}]},
{"role": "user", "content": [{"type": "audio", "audio": audio}]}]
inputs = proc.apply_chat_template(conv, tokenize=True, add_generation_prompt=True,
return_dict=True, return_tensors="pt", sampling_rate=16000)
if a.language:
prefill = proc.tokenizer(f"language {a.language}<asr_text>", add_special_tokens=False,
return_tensors="pt").input_ids
inputs["input_ids"] = torch.cat([inputs["input_ids"], prefill], dim=1)
inputs["attention_mask"] = torch.ones_like(inputs["input_ids"])
inputs["input_features"] = inputs["input_features"].to(model.dtype)
feats = inputs["input_features"][0] # [128, padded frames]
fmask = inputs["input_features_mask"][0].bool()
real = feats[:, fmask].T.contiguous().float().numpy() # [frames, 128]
real.tofile(os.path.join(a.out, "features.f32"))
with torch.no_grad():
rows = model.get_audio_features(inputs["input_features"],
inputs["input_features_mask"]).pooler_output
rows.to(torch.bfloat16).view(torch.int16).numpy().tofile(os.path.join(a.out, "audio.bf16"))
out = model.generate(**{k: v for k, v in inputs.items()}, max_new_tokens=a.max_new_tokens,
do_sample=False)
prompt = inputs["input_ids"][0].tolist()
reply = out[0, len(prompt):].tolist()
text = proc.tokenizer.decode(reply, skip_special_tokens=True)
res = {"frames": int(real.shape[0]), "audio_rows": int(rows.shape[0]), "hidden": int(rows.shape[1]),
"prompt_ids": prompt, "reply_ids": reply, "text": text, "dtype": a.dtype,
"language": a.language}
with open(os.path.join(a.out, "result.json"), "w") as f:
json.dump(res, f, indent=1)
print(json.dumps({k: v for k, v in res.items() if k not in ("prompt_ids",)}, indent=1))
if __name__ == "__main__":
main()