【vLLM 学习】Mlpspeculator

vLLM 是一款专为大语言模型推理加速而设计的框架,实现了 KV 缓存内存几乎零浪费,解决了内存管理瓶颈问题。

更多 vLLM 中文文档及教程可访问 →vllm.hyper.ai/

*在线运行 vLLM 入门教程:零基础分步指南

源码 examples/offline_inference/mlpspeculator.py

ini 复制代码
# SPDX-License-Identifier: Apache-2.0

import gc
import time

from vllm import LLM, SamplingParams


def time_generation(llm: LLM, prompts: list[str],
                    sampling_params: SamplingParams):
    # 从提示中生成文本。输出是 RequestOutput 的包含提示,生成文本和其他信息的对象列表。
    # 首先预热
    llm.generate(prompts, sampling_params)
    llm.generate(prompts, sampling_params)
    start = time.time()
    outputs = llm.generate(prompts, sampling_params)
    end = time.time()
    print((end - start) / sum([len(o.outputs[0].token_ids) for o in outputs]))
    # 打印输出。
    for output in outputs:
        generated_text = output.outputs[0].text
        print(f"text: {generated_text!r}")


if __name__ == "__main__":

    template = (
        "Below is an instruction that describes a task. Write a response "
        "that appropriately completes the request.\n\n### Instruction:\n{}"
        "\n\n### Response:\n")

    # 样本提示。
    prompts = [
        "Write about the president of the United States.",
    ]
    prompts = [template.format(prompt) for prompt in prompts]
    # 创建一个采样参数对象。
    sampling_params = SamplingParams(temperature=0.0, max_tokens=200)

    # 创建一个不使用规格解码的 LLM
    llm = LLM(model="meta-llama/Llama-2-13b-chat-hf")

    print("Without speculation")
    time_generation(llm, prompts, sampling_params)

    del llm
    gc.collect()

    # 与规格解码创建一个 LLM
    llm = LLM(
        model="meta-llama/Llama-2-13b-chat-hf",
        speculative_config={
            "model": "ibm-ai-platform/llama-13b-accelerator",
        },
    )

    print("With speculation")
    time_generation(llm, prompts, sampling_params)
相关推荐
吴佳浩1 天前
Langchain 浅出
python·langchain·llm
山顶夕景1 天前
【RL】Does RLVR enable LLMs to self-improve?
深度学习·llm·强化学习·rlvr
AndrewHZ1 天前
【图像处理基石】如何使用大模型进行图像处理工作?
图像处理·人工智能·深度学习·算法·llm·stablediffusion·可控性
mwq301231 天前
LLM 推理的“显存墙”与“通信墙”:从显存分布到部署原则
llm
纵有疾風起1 天前
C++——多态
开发语言·c++·经验分享·面试·开源
rgb2gray1 天前
增强城市数据分析:多密度区域的自适应分区框架
大数据·python·机器学习·语言模型·数据挖掘·数据分析·llm
炸裂狸花猫1 天前
开源域名证书工具 - cert-manager
云原生·容器·kubernetes·开源·cert-manager
Seal软件1 天前
GPUStack v2:推理加速释放算力潜能,开源重塑大模型推理下半场
llm·gpu
信也科技布道师FTE1 天前
当AMIS遇见AI智能体:如何为低代码开发装上“智慧大脑”?
人工智能·低代码·llm
智泊AI1 天前
建议所有初学者都这样去微调大模型!
llm