大模型-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

相关推荐
邵宇然6 分钟前
Rust Unsafe 代码规范:不安全块要小到能被审查
人工智能
Haoxuekeji26 分钟前
山东 AI 智能批改校园电子阅卷企业
大数据·人工智能·深度学习·安全·ai
视觉AI1 小时前
VS Code Remote-SSH 连接Jetson踩坑完整解决记录(网段不通+主机密钥变更双重故障)
运维·网络·人工智能·windows·ssh·边缘计算
科技发布1 小时前
拓氪科技一站式品牌营销平台使用手册 舆情风险预警管理教程
人工智能·科技
潜龙95271 小时前
AI时代下WebUI自动化实施架构图
人工智能·webui
酉鬼女又兒1 小时前
零基础入门 DeepSeek V4 Pro API 开发:从环境搭建、消息格式规范到翻译函数实战、少样本提示、多轮对话聊天机器人与常见报错全流程详解指南
大数据·网络·数据库·人工智能·macos·机器人·github
城事漫游Molly2 小时前
科研数据可视化的5步AI工作流:从原始数据到发表级图表
人工智能·信息可视化·数据分析·prompt·ai for science·科研数据绘图·博士生必读
To_OC2 小时前
跑了个最简 RAG demo 后,我终于搞懂向量检索那堆分数到底啥意思
人工智能·llm·agent
147API2 小时前
OpenAI新增工作区Admin keys,管理自动化怎么拆权限
运维·人工智能·自动化·openai·api
触底反弹2 小时前
🧠 别再让 AI 裸奔了!用 Skills 让 AI 秒变专属员工
人工智能