大模型-vllm 投机解码实现

一、vllm的官方链接

https://docs.vllm.ai/en/latest/examples/features/speculative_decoding/?h=speculative+decoding

https://github.com/vllm-project/vllm/tree/main/examples/features/speculative_decoding

服务端启用投机解码:

复制代码
python -m vllm.entrypoints.openai.api_server \
  --model meta-llama/Llama-3-70B \
  --speculative-model meta-llama/Llama-3-8B
  --num-speculative-tokens 4 ## 小模型一次推测几个token , 2-4常用

vllm 投机解码支持的范式:

二、客户端访问示例

复制代码
# Draft Models

The following code configures vLLM in an offline mode to use speculative decoding with a draft model, speculating 5 tokens at a time.

```python
from vllm import LLM, SamplingParams

prompts = ["The future of AI is"]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)

llm = LLM(
    model="Qwen/Qwen3-8B",
    tensor_parallel_size=1,
    speculative_config={
        "model": "Qwen/Qwen3-0.6B",
        "num_speculative_tokens": 5,
        "method": "draft_model",
    },
)
outputs = llm.generate(prompts, sampling_params)

for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
```

To perform the equivalent launch in online mode, use the following server-side code:

```bash
vllm serve Qwen/Qwen3-4B-Thinking-2507 \
    --host 0.0.0.0 \
    --port 8000 \
    --seed 42 \
    -tp 1 \
    --max_model_len 2048 \
    --gpu_memory_utilization 0.8 \
    --speculative_config '{"model": "Qwen/Qwen3-0.6B", "num_speculative_tokens": 5, "method": "draft_model"}'
```

The code used to request as completions as a client remains unchanged:

??? code

    ```python
    from openai import OpenAI

    # Modify OpenAI's API key and API base to use vLLM's API server.
    openai_api_key = "EMPTY"
    openai_api_base = "http://localhost:8000/v1"

    client = OpenAI(
        # defaults to os.environ.get("OPENAI_API_KEY")
        api_key=openai_api_key,
        base_url=openai_api_base,
    )

    models = client.models.list()
    model = models.data[0].id

    # Completion API
    stream = False
    completion = client.completions.create(
        model=model,
        prompt="The future of AI is",
        echo=False,
        n=1,
        stream=stream,
    )

    print("Completion results:")
    if stream:
        for c in completion:
            print(c)
    else:
        print(completion)
    ```

!!! warning
    Note: Please 

三、自投机解码算法实现paper 思路

https://arxiv.org/pdf/2603.11243

相关推荐
AI情绪识别开源1 小时前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
ZGIAI1 小时前
ZGI 迭代节点:批量资料的逐项处理
人工智能·架构
ZGIAI1 小时前
ZGI 知识检索:让业务回答有据可查
人工智能·架构
Asize1 小时前
框架的说明书是写给 AI 看的:我用 Next.js 搭了个博客
人工智能·代码规范·next.js
2601_955662461 小时前
AI 配音工具 7 款实测:短视频、影视解说、小说推文音质横向对比
人工智能·音视频·语音识别·视频
AI创界者2 小时前
PinkCherry-MiniMax-H3 全能AI视频整合包:8G显存开箱即用,支持首尾帧/超分补帧/自动提示词
人工智能·aigc
罗西的思考2 小时前
【Agentic RL / 强化学习框架】Molt 设计解读
人工智能·算法·机器学习
Mr数据杨2 小时前
GNSS伪距误差预测实战案例 从Kaggle回归任务到城市定位误差补偿
人工智能·数据分析·kaggle竞赛
冬奇Lab2 小时前
Code Agent 解剖(15):Harness 设计之五——可观测性
人工智能·开源