本地部署 gemma-3-27b-it

0. 引言

gemma-3-27b-it支持多模态,下面是一个简单测试。
在这里插入图片描述

1. 创建虚拟环境

conda create -n vllm_nightly python=3.12 -y
conda activate vllm_nightly 

2. 安装 Vllm 及其他依赖库

pip install vllm --pre --extra-index-url https://wheels.vllm.ai/nightly
pip install flash-attn --no-build-isolation
pip install flashinfer-python -i https://flashinfer.ai/whl/cu124/torch2.6
pip install git+https://github.com/huggingface/transformers@v4.50.0

3. 启动 gemma-3-27b-it

创建 examples/tool_chat_template_gemma3_pythonic.jinja,支持 tool calling,

And the custom chat template was created by starting with the google/gemma-3-4b-it model’s default template here and combined with parts of the llama pythonic tool template

# cat examples/tool_chat_template_gemma3_pythonic.jinja
{{ bos_token }}
{%- if messages[0]['role'] == 'system' -%}
    {%- if messages[0]['content'] is string -%}
        {%- set first_user_prefix = messages[0]['content'] + '\n\n' -%}
    {%- else -%}
        {%- set first_user_prefix = messages[0]['content'][0]['text'] + '\n\n' -%}
    {%- endif -%}
    {%- set loop_messages = messages[1:] -%}
{%- else -%}
    {%- set first_user_prefix = "" -%}
    {%- set loop_messages = messages -%}
{%- endif -%}
{%- if not tools is defined %}
    {%- set tools = none %}
{%- endif %}
{%- for message in loop_messages | rejectattr("role", "equalto", "tool") | selectattr("tool_calls", "undefined") -%}
    {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}
        {#使用Agent时,并不保证是user/assistant/user/assistant/...的顺序,所以不抛异常#}
        {#{{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}#}
    {%- endif -%}
{%- endfor -%}
{%- for message in loop_messages -%}
    {%- if (message['role'] == 'assistant') -%}
        {%- set role = "model" -%}
    {%- elif (message['role'] == 'tool') -%}
        {%- set role = "user" -%}
    {%- else -%}
        {%- set role = message['role'] -%}
    {%- endif -%}
    {{ '<start_of_turn>' + role + '\n' -}}
    {%- if loop.first -%}
        {{ first_user_prefix }}
        {%- if tools is not none -%}
            {{- "You have access to the following tools to help respond to the user. To call tools, please respond with a python list of the calls. DO NOT USE MARKDOWN SYNTAX.\n" }}
            {{- 'Respond in the format [func_name1(params_name1=params_value1, params_name2=params_value2...), func_name2(params)] \n' }}
            {{- "Do not use variables.\n\n" }}
            {%- for t in tools -%}
                {{- t | tojson(indent=4) }}
                {{- "\n\n" }}
            {%- endfor -%}
        {%- endif -%}
    {%- endif -%}

    {%- if 'tool_calls' in message -%}
        {{- '[' -}}
        {%- for tool_call in message.tool_calls -%}
            {%- if tool_call.function is defined -%}
                {%- set tool_call = tool_call.function -%}
            {%- endif -%}
            {{- tool_call.name + '(' -}}
            {%- for param in tool_call.arguments -%}
                {{- param + '=' -}}
                {{- "%sr" | format(tool_call.arguments[param]) -}}
                {%- if not loop.last -%}, {% endif -%}
            {%- endfor -%}
            {{- ')' -}}
            {%- if not loop.last -%},{%- endif -%}
        {%- endfor -%}
        {{- ']' -}}
    {%- endif -%}

    {%- if (message['role'] == 'tool') -%}
        {{ '<tool_response>\n' -}}
    {%- endif -%}
    {%- if message['content'] is string -%}
        {{ message['content'] | trim }}
    {%- elif message['content'] is iterable -%}
        {%- for item in message['content'] -%}
            {%- if item['type'] == 'image' -%}
                {{ '<start_of_image>' }}
            {%- elif item['type'] == 'text' -%}
                {{ item['text'] | trim }}
            {%- endif -%}
        {%- endfor -%}
    {%- else -%}
        {{ raise_exception("Invalid content type") }}
    {%- endif -%}
    {%- if (message['role'] == 'tool') -%}
        {{ '</tool_response>' -}}
    {%- endif -%}
    {{ '<end_of_turn>\n' }}
{%- endfor -%}
{%- if add_generation_prompt -%}
    {{'<start_of_turn>model\n'}}
{%- endif -%}
#eval "$(conda shell.bash hook)"
#conda activate vllm_nightly
CUDA_VISIBLE_DEVICES=3,1,0,2 \
VLLM_USE_V1=1 \
VLLM_WORKER_MULTIPROC_METHOD=spawn \
TRANSFORMERS_OFFLINE=1 \
HF_DATASETS_OFFLINE=1 \
#VLLM_MM_INPUT_CACHE_GiB=0.1 \
vllm serve /root/HuggingFaceCache/models--google--gemma-3-27b-it \
--trust-remote-code --served-model-name gpt-4o \
--gpu-memory-utilization 0.99 \
--tensor-parallel-size 4 --port 8000 \
--api-key sk-123456 --max-model-len 32768 \
--enable-chunked-prefill --limit-mm-per-prompt image=3 \
--max-num-seqs 256 \
--enable-auto-tool-choice \
--tool-call-parser pythonic \
--chat-template examples/tool_chat_template_gemma3_pythonic.jinja

4. 测试一下 gemma-3-27b-it

在这里插入图片描述

完结!


参考资料:

<think>好的,我现在需要帮助用户了解如何使用vLLM部署GEMMA3 27B模型。首先,我需要确认用户的需求。用户可能已经知道vLLM是一个高效推理和服务框架,想用它来部署较大的模型如GEMMA3 27B。我需要整理相关步骤,并确保信息准确。 首先,查阅vLLM的官方文档,看看是否支持GEMMA模型。根据之前的引用,Gemma是谷歌发布的轻量级开源模型,而vLLM支持多种架构,比如Llama、Mistral等,但需要确认是否适配Gemma的结构。可能需要用户自行验证兼容性,或者是否有社区贡献的支持。 然后,安装vLLM是关键步骤。用户需要安装最新版本的vLLM,可能通过pip或从源码安装。需要注意CUDA版本兼容性,因为vLLM依赖CUDA进行加速。 接下来是模型下载。用户需要从Hugging Face或官方仓库获取GEMMA3 27B的模型文件,确保下载的是支持PyTorch格式的版本。例如,Hugging Face上的模型可能需要转换或直接使用。 编写启动脚本时,需要了解vLLM的启动命令和参数。例如,使用`vllm.entrypoints.api_server`启动API服务,指定模型路径、tensor并行度、端口等。此外,可能需要调整参数如dtype为bfloat16以节省显存,设置gpu_memory_utilization来优化显存使用。 调用服务部分,用户可以通过curl或Python客户端发送请求。需要提供示例代码,展示如何构造请求体,处理响应。同时,可能需要处理授权问题,如果模型需要API token,需在请求头中添加。 在注意事项里,显存需求是一个重点。GEMMA3 27B可能需要多块GPU,比如使用tensor并行度2或4。需要提醒用户检查硬件资源是否足够,以及如何监控显存使用。 此外,用户可能遇到的问题包括模型格式不兼容、CUDA版本不匹配、依赖缺失等。需要建议安装正确的依赖,如特定版本的PyTorch和Transformers库。 最后,生成相关问题,帮助用户进一步了解模型部署的优化、性能监控、量化方法,以及结合其他工具的使用,比如LangChain。这些问题需要覆盖用户可能感兴趣的扩展话题,帮助他们深入应用。 确保回答中所有数学公式和代码符合格式要求,使用中文,结构清晰,引用正确。检查是否有遗漏的步骤或潜在问题,确保用户能够顺利按照指导进行操作。</think>### 使用vLLM部署GEMMA3 27B模型的步骤 #### 1. **环境准备** - 确保GPU驱动、CUDA(>=11.8)和PyTorch(>=2.0)已安装。 - 安装vLLM: ```bash # 从源码安装(推荐) git clone https://github.com/vllm-project/vllm.git cd vllm && pip install -e . # 或直接通过pip安装 pip install vllm ``` #### 2. **下载GEMMA3 27B模型** - 从Hugging Face下载模型权重(需访问权限): ```bash huggingface-cli download google/gemma-27b --local-dir ./gemma-27b ``` 若模型需要API token,需在Hugging Face账户中申请[^4]。 #### 3. **编写启动脚本** 创建`launch_gemma.py`文件,使用vLLM的API服务器: ```python from vllm.entrypoints import api_server api_server.start_http_server( model="google/gemma-27b", tensor_parallel_size=4, # 根据GPU数量调整(如4块A100) port=8000, dtype="bfloat16", # 显存优化 gpu_memory_utilization=0.9, trust_remote_code=True # 允许加载自定义模型代码 ) ``` #### 4. **启动服务** ```bash python launch_gemma.py ``` 服务启动后,可通过`http://localhost:8000`进行访问。 #### 5. **调用模型API** 使用Python客户端调用: ```python from vllm import LLM, SamplingParams sampling_params = SamplingParams(temperature=0.7, max_tokens=200) llm = LLM(model="google/gemma-27b", tensor_parallel_size=4) prompts = ["请解释量子计算的原理。"] outputs = llm.generate(prompts, sampling_params) for output in outputs: print(output.outputs[0].text) ``` #### 6. **注意事项** - **显存需求**:27B模型需至少4块40GB显存的GPU(如A100-40G),若显存不足可启用`swap_space`参数使用磁盘交换。 - **模型格式**:确保模型目录包含`config.json`和`pytorch_model.bin`文件。 - **授权问题**:若从Hugging Face下载模型时提示需要认证,需在代码中添加`token="YOUR_TOKEN"`参数[^1]。 --- ### 性能优化建议 1. **量化部署**:使用`dtype="float16"`或AWQ量化减少显存占用。 2. **批处理**:通过`batch_size`参数提高吞吐量,例如`api_server(..., max_num_batched_tokens=4096)`。 3. **监控工具**:结合`nvidia-smi`和vLLM内置的Prometheus指标监控显存和吞吐量。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值