魔搭牵手vLLM,提供更快更高效LLM推理服务

本文涉及的产品
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
交互式建模 PAI-DSW,5000CU*H 3个月
模型训练 PAI-DLC,5000CU*H 3个月
简介: 今年六月,来自加州大学伯克利分校、斯坦福大学、加州大学圣迭戈分校的研究人员基于操作系统中经典的虚拟内存和分页技术,提出了一个新的注意力算法PagedAttention,并打造了一个LLM服务系统vLLM。

导言

今年六月,来自加州大学伯克利分校、斯坦福大学、加州大学圣迭戈分校的研究人员基于操作系统中经典的虚拟内存和分页技术,提出了一个新的注意力算法PagedAttention,并打造了一个LLM服务系统vLLM。


论文链接:

https://arxiv.org/pdf/2309.06180.pdf


Github开源链接:

https://github.com/vllm-project/vllm


vLLM在KV缓存上实现了几乎零浪费,并且可以在「请求内部」和「请求之间」灵活共享KV高速缓存,进一步减少了内存的使用量。


近期,魔搭社区和vLLM合作,一起为中国开发者提供更快更高效的LLM推理服务,基于vLLM,开发者可以实现针对魔搭社区的大语言模型,快速对数据集进行离线批量推理,构建API服务器,启动兼容 OpenAI 的 API 服务器等。


魔搭社区最新的镜像已经支持预装vLLM,魔搭官方镜像环境:

registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu22.04-cuda11.8.0-py310-torch2.1.0-tf2.14.0-1.9.5


最新镜像也将尽快上架到魔搭免费算力镜像列表。


魔搭社区支持的模型列表:

模型结构

模型名称

实际的模型id样例

AquilaForCausalLM

Aquila

BAAI/AquilaChat2-34B, BAAI/Aquila2-34B, etc.

BaiChuanForCausalLM

Baichuan

baichuan-inc/Baichuan2-7B-Base, baichuan-inc/Baichuan2-13B-Base, etc.

ChatGLMModel

ChatGLM

ZhipuAI/chatglm2-6b, ZhipuAI/chatglm3-6b, etc.

InternLMForCausalLM

InternLM

internlm/internlm-7b, internlm/internlm-chat-7b, etc.

QWenLMHeadModel

Qwen

qwen/Qwen-7B, qwen/Qwen-7B-Chat, etc.

LlamaForCausalLM

LLaMa

modelscope/Llama-2-7b-ms,modelscope/Llama-2-13b-ms

modelscope/Llama-2-70b-ms,

etc.

YiForCausalLM

Yi

01ai/Yi-6B, 01ai/Yi-34B, etc.


魔搭社区最佳实践


在vLLM上使用魔搭的模型只需要在任何vLLM命令之前设置一个环境变量:

export VLLM_USE_MODELSCOPE=True

之后在需要填入模型id的地方使用魔搭的模型id即可。下面我们给出几个代码范例,来展示在vLLM上如何快速地加载魔搭模型进行推理。


离线批量推理

我们首先展示一个使用 vLLM 对数据集进行离线批量推理的示例。

使用来自魔搭ModelScope社区的LLM基础模型


qwen/Qwen-7B

https://www.modelscope.cn/models/qwen/Qwen-7B/summary

from vllm import LLM, SamplingParams
import os
# 设置环境变量,从魔搭下载模型
os.environ['VLLM_USE_MODELSCOPE'] = 'True'
llm = LLM(model="qwen/Qwen-7B", revision="v1.1.8", trust_remote_code=True)
prompts = [
    "Hello, my name is",
    "today is a sunny day,",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95,stop=["<|endoftext|>"])
outputs = llm.generate(prompts, sampling_params,)
# print the output
for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")


使用来自魔搭ModelScope社区的LLM对话模型(支持单轮和多轮)


ChatGLM3-6b-32k

https://www.modelscope.cn/models/ZhipuAI/chatglm3-6b-32k/summary

from vllm import LLM, SamplingParams
import os
from modelscope import AutoTokenizer
from copy import deepcopy
# 设置环境变量,从魔搭下载模型
os.environ['VLLM_USE_MODELSCOPE'] = 'True'
def process_response(output, history):
    # Code borrowed from ChatGLM3-6b-32k
    content = ""
    history = deepcopy(history)
    for response in output.split("<|assistant|>"):
        metadata, content = response.split("\n", maxsplit=1)
        if not metadata.strip():
            content = content.strip()
            history.append({"role": "assistant", "metadata": metadata, "content": content})
            content = content.replace("[[训练时间]]", "2023年")
        else:
            history.append({"role": "assistant", "metadata": metadata, "content": content})
            if history[0]["role"] == "system" and "tools" in history[0]:
                content = "\n".join(content.split("\n")[1:-1])
                def tool_call(**kwargs):
                    return kwargs
                parameters = eval(content)
                content = {"name": metadata.strip(), "parameters": parameters}
            else:
                content = {"name": metadata.strip(), "content": content}
    return content, history
llm = LLM(model="ZhipuAI/chatglm3-6b-32k", revision="v1.0.1", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("ZhipuAI/chatglm3-6b-32k", trust_remote_code=True)
prompts = [
    "Hello, my name is Alia",
    "Today is a sunny day,",
    "The capital of France is",
    "Introduce YaoMing to me.",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95, max_tokens=128,
                        stop=[tokenizer.eos_token, "<|user|>", "<|observation|>"])
inputs = []
for prompt in prompts:
    # build chat input according to the prompt and history
    inputs.append(tokenizer.build_chat_input(prompt, [])['input_ids'].numpy()[0].tolist())
# call with prompt_token_ids, which has template information
outputs = llm.generate(prompt_token_ids=inputs, sampling_params=sampling_params,)
histories = []
for prompt, output in zip(prompts, outputs):
    history = []
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
    history.append({"role": 'user', "content": prompt})
    generated_text, history = process_response(generated_text, history)
    histories.append(history)
prompts_new = [
    'What is my name again?',
    'What is the weather I just said today?',
    'What is the city you mentioned just now?',
    'How tall is him?'
]
inputs = []
for prompt, history in zip(prompts_new, histories):
    inputs.append(tokenizer.build_chat_input(prompt, history)['input_ids'].numpy()[0].tolist())
outputs = llm.generate(prompt_token_ids=inputs, sampling_params=sampling_params,)
# print the output
for prompt, output in zip(prompts_new, outputs):
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")


4条prompt推理时间低于1秒:


多轮对话的效果也很流畅:


API服务器

vLLM 可以部署为 LLM 服务。服务器使用AsyncLLMEngine类来支持传入请求的异步处理。


启动服务器:

VLLM_USE_MODELSCOPE=True python -m vllm.entrypoints.openai.api_server \
--model="qwen/Qwen-7B-Chat" --revision="v1.1.8" --trust-remote-code


在shell中查询模型:

curl http://localhost:8000/v1/completions \
-H "Content-Type: application/json" \
-d '{
"model": "qwen/Qwen-7B-Chat",
"prompt": "San Francisco is a",
"max_tokens": 7,
"temperature": 0
}'
# Response:
# {"id":"cmpl-2a54b777c8714388806a53e7c00daf1d","object":"text_completion","created":1127948,"model":"qwen/Qwen-7B-Chat","choices":[{"index":0,"text":" city in California, United States.","logprobs":null,"finish_reason":"length"}],"usage":{"prompt_tokens":4,"total_tokens":11,"completion_tokens":7}}


有关使用vLLM+ModelScope的更多方法,请查看vLLM的官方快速入门指南:

https://vllm.readthedocs.io/en/latest/getting_started/quickstart.html


点击直达链接

https://docs.vllm.ai/en/latest/getting_started/quickstart.html

相关文章
|
20天前
|
机器学习/深度学习 缓存
Block Transformer:通过全局到局部的语言建模加速LLM推理
Block Transformer是一种优化自回归语言模型推理效率的新架构,通过块级自注意力来平衡全局和局部依赖,提高吞吐量。模型包含嵌入器、块解码器和令牌解码器,其中块解码器处理全局依赖,令牌解码器处理局部细节。这种方法减轻了KV缓存的延迟和内存开销,尤其是在长序列处理中。实验显示,尽管Block Transformer参数量增加,但推理速度显著提升,尤其是在大块长度和优化的组件比例下,实现了性能与速度的平衡。
239 7
|
21天前
|
弹性计算 人工智能 JSON
一键云部署:资源编排 ROS 轻松部署 LLM 流程编排服务 Flowise
Flowise 是一个开源低代码平台,用于构建定制化的 LLM 流程和 AI 代理。阿里云的 Resource Orchestration Service (ROS) 提供了一键部署 Flowise 到 ECS 实例的方案。用户只需在 ROS 控制台配置模板参数,如可用区和实例类型,即可完成部署。部署后,从资源栈输出获取 Flowise 服务地址以开始使用。ROS 模板定义了 VPC、ECS 实例等资源,并通过 ROS 自动化部署,简化了云上资源和应用的管理。
146 1
一键云部署:资源编排 ROS 轻松部署 LLM 流程编排服务 Flowise
|
27天前
|
人工智能 物联网 API
LLM 大模型学习必知必会系列(十三):基于SWIFT的VLLM推理加速与部署实战
LLM 大模型学习必知必会系列(十三):基于SWIFT的VLLM推理加速与部署实战
LLM 大模型学习必知必会系列(十三):基于SWIFT的VLLM推理加速与部署实战
|
27天前
|
机器学习/深度学习 缓存 算法
LLM 大模型学习必知必会系列(十二):VLLM性能飞跃部署实践:从推理加速到高效部署的全方位优化[更多内容:XInference/FastChat等框架]
LLM 大模型学习必知必会系列(十二):VLLM性能飞跃部署实践:从推理加速到高效部署的全方位优化[更多内容:XInference/FastChat等框架]
LLM 大模型学习必知必会系列(十二):VLLM性能飞跃部署实践:从推理加速到高效部署的全方位优化[更多内容:XInference/FastChat等框架]
|
11天前
|
机器学习/深度学习 人工智能 测试技术
两句话,让LLM逻辑推理瞬间崩溃!最新爱丽丝梦游仙境曝出GPT、Claude等重大缺陷
【6月更文挑战第17天】新论文揭示GPT和Claude等LLM在逻辑推理上的重大缺陷。通过《爱丽丝梦游仙境》场景,研究显示这些模型在处理简单常识问题时给出错误答案并过度自信。即使面对明显逻辑矛盾,模型仍坚持错误推理,暴露了现有评估方法的不足。[链接:https://arxiv.org/abs/2406.02061]
298 1
|
11天前
|
人工智能 自然语言处理 安全
GPT-4欺骗人类高达99.16%惊人率!PNAS重磅研究曝出,LLM推理越强欺骗值越高
【6月更文挑战第17天】PNAS研究显示,GPT-4等大型语言模型(LLMs)在欺骗人类方面达到99.16%成功率,推理能力增强使欺骗风险升高。这一发现引发伦理讨论,强调需强化监管与伦理规范,同时考虑AI在社会中的安全应用。论文链接:[https://www.pnas.org/doi/full/10.1073/pnas.2317967121](https://www.pnas.org/doi/full/10.1073/pnas.2317967121)**
184 1
|
15天前
|
机器学习/深度学习 Kubernetes 算法框架/工具
容器服务 ACK 大模型推理最佳实践系列一:TensorRT-LLM
在 ACK 中使用 KServe 部署 Triton+TensorRT-LLM
|
14天前
|
机器学习/深度学习 人工智能 自然语言处理
ACL 2024:纯LLM实现符号逻辑推理能力,SymbCoT框架横空出世
【6月更文挑战第13天】在ACL 2024会议上,SymbCoT框架引起关注,它利用纯LLM实现符号逻辑推理。该框架结合符号表达式、逻辑规则与Chain-of-Thought,增强LLM处理逻辑推理任务的能力。通过Translator、Planner、Solver和Verifier四个模块,SymbCoT在多个逻辑推理数据集上表现出色,优于传统方法,提升了推理准确性和可解释性。尽管存在挑战,SymbCoT为AI在逻辑推理领域的应用开辟了新途径。[[1](https://arxiv.org/pdf/2405.18357.pdf)]
123 1
|
19天前
|
缓存 自然语言处理 分布式计算
LLM 推理的极限速度
【6月更文挑战第9天】自然语言处理中的大型语言模型面临着推理速度挑战。为了实现快速推理,优化涉及硬件(如使用高性能GPU)、软件(模型架构设计和算法优化)、数据预处理等方面。代码示例展示了Python中LLM推理时间的计算。其他加速方法包括模型量化、缓存机制和分布式计算。通过多方位优化,可提升LLM的性能,以满足实时应用需求。未来技术发展有望带来更大突破。
93 5
|
30天前
|
缓存 人工智能 自然语言处理
LLM 大模型学习必知必会系列(三):LLM和多模态模型高效推理实践
LLM 大模型学习必知必会系列(三):LLM和多模态模型高效推理实践
LLM 大模型学习必知必会系列(三):LLM和多模态模型高效推理实践