【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)
相关推荐
Mars酱12 小时前
1分钟编写贪吃蛇 | JSnake贪吃蛇单机版
java·后端·开源
2301_8227032013 小时前
开源鸿蒙跨平台Flutter开发:幼儿疫苗全生命周期追踪系统:基于 Flutter 的免疫接种档案与状态机设计
算法·flutter·华为·开源·harmonyos·鸿蒙
Tadas-Gao13 小时前
从“驯马”到“驭队”:Harness Engineering 如何重构 AI 产品化的底层逻辑
人工智能·语言模型·架构·大模型·llm·harness
wasp52013 小时前
从 Vibe Coding 到真·生产力:OpenHarness 的“Harness 方程式”及其实战分析
人工智能·架构·开源·agent
M ? A15 小时前
Vue转React实战:defineProps精准迁移实战
前端·javascript·vue.js·经验分享·react.js·开源·vureact
Hommy8816 小时前
【开源剪映小助手-客户端】桌面客户端
python·开源·node.js·github·剪映小助手
2301_8227032016 小时前
开源鸿蒙跨平台Flutter开发:蛋白质序列特征提取:氨基酸组成与理化性质计算
flutter·华为·开源·harmonyos·鸿蒙
二等饼干~za89866816 小时前
源码可控:云罗 GEO 源头工厂,开源搭建 + 二次开发全链路解决方案
服务器·开发语言·开源·php·音视频·ai-native
提子拌饭13316 小时前
开源鸿蒙跨平台Flutter开发:中小学百米跑信息记录表:基于 Flutter 的高精计时与运动学曲线引擎
flutter·华为·开源·harmonyos