---
title: "推测性解码：实现 Whisper 推理速度提升两倍 [译]"
url: "https://paooo.com/ai-study/7130/"
author: "宝玉的分享"
published_at: "2024-04-16T18:16:03+08:00"
updated_at: "2026-07-22T13:27:45+08:00"
categories: "强化 AI 学习"
tags: "AI音频与语音, OpenAI, 大模型微调, 开源大模型"
source: "Prompt 语宙 (https://paooo.com)"
ai_tools_directory: "语宙 AI 导航 (https://ai.paooo.com)"
---

# 推测性解码：实现 Whisper 推理速度提升两倍 [译]

> 来源：[Prompt 语宙 (paooo.com)](https://paooo.com/ai-study/7130/) · 作者：宝玉的分享 · 发布时间：2024-04-16

## AI 30 秒核心看点 (TL;DR)

- 用 distil-large-v2 充当助手模型，为 Whisper 推测性解码提供更快的候选生成。
- 实测基线为 73 秒、WER 3.5%，说明可在较低误差下优化推理吞吐。
- 文章给出可复现的 Whisper 推理与评测流程，适合关注语音识别加速的开发者。

```
import torch

from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor

device ="cuda:0"if torch.cuda.is_available()else"cpu"

torch_dtype = torch.float16 if torch.cuda.is_available()else torch.float32

model_id ="openai/whisper-large-v2"

model = AutoModelForSpeechSeq2Seq.from_pretrained(

    model_id,

    torch_dtype=torch_dtype,

    low_cpu_mem_usage=True,

    use_safetensors=True,

    attn_implementation="sdpa",

)

model.to(device)

processor = AutoProcessor.from_pretrained(model_id)

from datasets import load_dataset

dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy","clean", split="validation")

import time

defgenerate_with_time(model, inputs,**kwargs):

    start_time = time.time()

    outputs = model.generate(**inputs,**kwargs)

    generation_time = time.time()- start_time

return outputs, generation_time

from tqdm import tqdm

all_time =0

predictions =[]

references =[]

for sample in tqdm(dataset):

    audio = sample["audio"]

    inputs = processor(audio["array"], sampling_rate=audio["sampling_rate"], return_tensors="pt")

    inputs = inputs.to(device=device, dtype=torch.float16)

    output, gen_time = generate_with_time(model, inputs)

    all_time += gen_time

    predictions.append(processor.batch_decode(output, skip_special_tokens=True, normalize=True)[0])

    references.append(processor.tokenizer._normalize(sample["text"]))

print(all_time)

100%|██████████| 73/73 [01:37

## 常见问题解答 (FAQ)

### Whisper 推测性解码能提升多少速度？
文中展示的是基于 Whisper 的推测性解码思路，并给出基线实测：73 秒、WER 3.5%。文章核心是通过助手模型加速生成流程，目标是在尽量不牺牲识别质量的前提下提升推理速度。

### 为什么用 distil-large-v2 作为助手模型？
因为 distil-large-v2 与 Whisper 共享相同编码器，只保留 32 层解码器中的 2 层，推理更快，同时在分布外测试集上仍能保持接近 Whisper 的转写精度，适合作为助手模型。

### 这篇文章适合哪些人参考？
适合做语音识别、ASR 推理优化或 Hugging Face 模型部署的开发者。文章包含模型加载、数据集评测、生成耗时统计与 WER 计算，便于复现 Whisper 的加速方案。

---
探索 2,800+ 全球实用 AI 工具与官方直达入口：[语宙 AI 导航 (https://ai.paooo.com/)](https://ai.paooo.com/)
