[Feature Request] Allow VAD to run on a different device than the ASR model (Apple Silicon MPS regression: VAD 5x slower than CPU)
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 58/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- python, pytorch
调研方向
从 funasr/auto/auto_model.py 的第 470、483 和 499 行附近开始,然后跟踪 inference_with_vad() 以及特征张量如何传递到 VAD 模型。比较遵循显式 vad_kwargs["device"] 与采用提议的 vad_device 设计,同时让 ASR 保持在主设备上。当 VAD 和 ASR 能够在混合设备上运行,且无需脆弱的 monkey-patching 变通方案时,即表示完成;使用提供的 CPU/MPS 复现和计时比较进行验证。
由索引模型根据 Issue 内容生成。
描述
Summary
AutoModel forces vad_kwargs["device"] to equal the main ASR model's device (funasr/auto/auto_model.py:470). There is no way to run VAD on CPU while the ASR model runs on GPU/MPS. On Apple Silicon this is a measurable ~5x performance regression for the VAD stage, because the FSMN streaming VAD emits many tiny per-frame forwards that suffer from MPS per-op kernel-launch overhead.
Environment
- macOS / Apple M2 (8-core, 16GB)
- funasr 1.4.14
- torch 2.14.0,
torch.backends.mps.is_available() = True
Measured impact
Same 121-minute (7285s) audio, FSMN VAD only:
| Device | VAD wall-clock | Realtime factor |
|---|---|---|
cpu |
23.6s | 308x |
mps |
123.0s | 59x |
VAD on MPS is 5.2x slower than CPU. Full pipeline (paraformer-large + fsmn-vad + ct-punc), same 121-min audio:
| Config | Total | Notes |
|---|---|---|
device='mps' (VAD+ASR both on MPS) |
~259s | VAD=123s, ASR=91s |
| VAD on CPU + ASR on MPS (mixed) | ~156s | VAD=24s, ASR=91s |
Mixed device saves ~40% end-to-end. ASR (paraformer) genuinely benefits from MPS (large batched matmuls); VAD does not.
Root cause
funasr/auto/auto_model.py (1.4.14):
# AutoModel.__init__, ~line 465-470
vad_kwargs = {} if kwargs.get("vad_kwargs", {}) is None else kwargs.get("vad_kwargs", {})
if vad_model is not None:
vad_kwargs["model"] = vad_model
vad_kwargs["model_revision"] = kwargs.get("vad_model_revision", "master")
vad_kwargs["device"] = kwargs["device"] # <-- hardcoded to main device
So even passing vad_kwargs={"device": "cpu"} is overwritten. inference_with_vad() then runs self.inference(model=self.vad_model, kwargs=self.vad_kwargs) with vad_kwargs["device"] fixed to the main device, so feature tensors land on the main device and VAD weights must match.
Feature request
Expose a way to place the VAD model on a device independent of the ASR model, e.g.:
AutoModel(model=..., vad_model=..., punc_model=..., device='mps', vad_device='cpu')
which would build the VAD on vad_device and move feature tensors fed to self.vad_model onto vad_device before VAD forward, while the ASR model stays on device. (punc/spk sub-models have the same hardcoded coupling at lines 483/499, so a general per-submodel device would be ideal.)
This matters most on Apple Silicon today, but the pattern (streaming VAD = many tiny forwards) is device-agnostic: any accelerator with high per-op launch overhead is hurt by forcing VAD onto it.
Minimal reproduction
import time
from pathlib import Path
from funasr import AutoModel
models = [Path.home()/'.cache/modelscope/models'/('iic--'+n)/'snapshots/master' for n in [
'speech_seaco_paraformer_large_asr_nat-zh-cn-16k-common-vocab8404-pytorch',
'speech_fsmn_vad_zh-cn-16k-common-pytorch',
'punc_ct-transformer_cn-en-common-vocab471067-large',
]]
for dev in ('cpu', 'mps'):
m = AutoModel(model=str(models[1]), device=dev, disable_update=True, disable_pbar=True)
t = time.monotonic()
m.generate(input='your_16k_mono.wav', max_single_segment_time=60000)
print(dev, f'{time.monotonic()-t:.1f}s')
Workaround (1.4.14)
Build the main model on the accelerator, then replace MODEL.vad_model with a separately-built CPU VAD instance and patch ComputeScores to move feature tensors onto the CPU device:
main = AutoModel(model=asr, vad_model=vad, punc_model=punc, device='mps', ...)
cpu_vad = AutoModel(model=vad, device='cpu', ...).model
_orig = cpu_vad.ComputeScores
cpu_vad.ComputeScores = lambda feats, cache=None: _orig(feats.to('cpu') if hasattr(feats,'to') and feats.device.type!='cpu' else feats, cache=cache)
main.vad_model = cpu_vad
main.vad_kwargs['device'] = 'cpu'
Works (verified, 156s vs 259s) but fragile across versions, hence this request.
Happy to turn this into a PR — the design question is whether to honor vad_kwargs["device"] when explicitly provided (smallest change) vs. add a dedicated vad_device argument.
- 主要语言
- Python
- 星标
- 20.4k
- 派生
- 2k
- 平均合并
- 4 小时 55 分钟
- 30 天内合并 PR
- 169
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelscope/FunASR 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 86/100
modelscope/FunASR#3704 · 1 条评论 ·
-
bug needs feedback
难度 2/5 1-3 小时 新手友好度 76/100
modelscope/FunASR#3401 · 2 条评论 ·
-
needs triage question
难度 4/5 3-5 天 新手友好度 30/100
modelscope/FunASR#3718 · 3 条评论 ·
-
难度 3/5 1-2 天 新手友好度 48/100
modelscope/FunASR#3717 · 1 条评论 ·
-
难度 5/5 一周以上 新手友好度 25/100
modelscope/FunASR#3715 ·
查看 modelscope/FunASR 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 82/100
-
难度 2/5 1-3 小时 新手友好度 78/100
-
enhancement
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 74/100