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

相关推荐
程序员z72 分钟前
RAG 系统评测实战:从指标设计到落地闭环
人工智能
CCC:CarCrazeCurator4 分钟前
从算子到智能系统:理解 MLP、CNN、Transformer、JEPA、VLA 与世界模型
人工智能·深度学习
程序员z711 分钟前
Agent 评测体系怎么搭:从指标设计到工程闭环
人工智能
NeoGressAI外贸数字化13 分钟前
外贸独立站多语言缓存命中率低?5 个成因与排查脚本
人工智能
星核0penstarry15 分钟前
从“一次生成“到“持续进化“:自进化社媒 Agent 的工作流拆解
人工智能·开源·agent
昇腾知识体系17 分钟前
鲲鹏+昇腾 NUMA 亲和性调优:Kunpeng 920 双路服务器给 NPU 工作负载绑核
人工智能·华为·知识图谱
桃西西呀29 分钟前
你拍的一堆硬币,手机怎么一眼数出有几枚?聊聊边缘、轮廓和模板匹配
人工智能·llm·图像识别
米小虾30 分钟前
多智能体还在"说话":C2C 把 KV-Cache 直接传给另一个模型,2.5× 加速背后的五个死结
人工智能·agent
人工智能AI技术31 分钟前
Spring factory-method踩坑实录,Bean类型异常一次讲透
人工智能
AI职业加油站32 分钟前
数据要素价值释放年:大数据治理工程师,站上职业新风口
人工智能·学习·职场和发展·数据分析·职场发展