大模型LLM(六)--AI部署编码大模型Qwen-Coder部署

1、Qwen-Coder的下载

选择的Qwen-COder大模型是Qwen3-Coder-30B-A3B-Instruct-FP8,路径如下:
https://www.modelscope.cn/models/Qwen/Qwen3-Coder-30B-A3B-Instruct-FP8
下载脚本

import torch
#通过modelscope的包进行模型下载
from modelscope import snapshot_download, AutoModel, AutoTokenizer
from modelscope import GenerationConfig
# 第一个参数为模型名称,参数cache_dir为模型的下载路径,
#Qwen3-Coder-30B-A3B-Instruct-FP8大小30G,Qwen3-Coder-30B-A3B-Instruct大小60G
model_dir = snapshot_download('Qwen/Qwen3-Coder-30B-A3B-Instruct-FP8', cache_dir='./')

2、启动

if __name__ == '__main__':
    # 加载预训练的分词器和模型
    print("正在加载模型... ...")
    model_path = "./Qwen/Qwen3-Coder-30B-A3B-Instruct-FP8"
    tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
    # 可以通过自定义device_map将模型的不同分层分配到不同的GPU达到GPU的高效使用
    model = AutoModelForCausalLM.from_pretrained(model_path,torch_dtype=TORCH_DTYPE, device_map="auto", trust_remote_code=True).eval()
    model.generation_config = GenerationConfig.from_pretrained(model_path, trust_remote_code=True) # 可指定
    model.eval()  # 设置模型为评估模式
    print("模型加载完成。")
    # 启动FastAPI应用
    uvicorn.run(app, host='0.0.0.0', port=6006)  # 在指定端口和主机上启动应用

3、解决报错问题

3.1 缺少libgomp依赖包

报错:

libgomp: Invalid value for environment variable OMP_NUM_THREADS

解决措施:
确认 libgomp 的版本号

conda list libgomp
libgomp的版本号

安装

conda install libgomp==15.2.0
安装成功

设置环境变量

export OMP_NUM_THREADS=4

再重新启动大模型


启动成功

4、部署过程中的问题记录

4.1、问题1:大模型回答问题的时候响应时间很长

模型运行环境配置信息
CPU :16 核心
内存:120 GB
GPU :NVIDIA GeForce RTX 4090, 1个

4.1.1、通过查看显卡资源使用情况

watch -n 1 nvidia-smi

在回答问题时候,显卡资源使用情况


显卡资源使用情况

4.1.2、使用情况分析

  1. GPU 显存使用情况:
Memory-Usage: 21391MiB / 24564MiB

表面看显存几乎占满(约 20.8GB),似乎模型加载到了 GPU。

  1. 但进程列表中显示:
Processes:
GPU  GI  CI        PID   Type   Process name                  GPU Memory Usage
0    N/A N/A          953   C            python                          0MiB

虽然总显存被占用了 21GB,但没有任何进程被记录为使用了 GPU 显存(GPU Memory Usage = 0MiB)!
这说明:显存是被“预分配”或“保留”的,但不是由当前运行的 Python 进程主动申请用于计算。
实际推理计算完全发生在 CPU 上。

  1. GPU 利用率低:
Volatile GPU-Util: 20%

即使有 20% 利用率,也远低于正常推理应有的 70%-100%。结合上面“进程显存使用为 0”,这 20% 很可能是系统开销或数据拷贝,而非真正的矩阵计算。

  1. 功耗较低:
Pwr:Usage/Cap: 64W / 450W

RTX 4090 在满载推理时通常功耗在 300W+,64W 说明 GPU 基本处于空闲状态。

4.1.3、问题定位及修改

问题代码:

model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=TORCH_DTYPE, device_map="auto", trust_remote_code=True).eval()

原因是device_map="auto" 导致静默失败,退回到 CPU 加载权重,计算图仍在 CPU 构建
修改:

model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=TORCH_DTYPE, device_map="cuda:0", trust_remote_code=True).eval()

启动后报错信息:

orch.OutOfMemoryError: CUDA out of memory. Tried to allocate 20.00 MiB. GPU 0 has a total capacity of 23.55 GiB of which 2.69 MiB is free. Including non-PyTorch memory, this process has 0 bytes memory in use. Of the allocated memory 23.10 GiB is allocated by PyTorch, and 1.28 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management 

原因是显存不足
解决方案:
换用 INT4 量化版本,或更换更大显存来运行大模型。
我选择更换到显存为32G的环境来执行


更换到更大显存的环境

GPU计算使用率依然很低

Volatile GPU-Util: 20%

大模型执行返回的信息

The attention mask is not set and cannot be inferred from input because pad token is same as eos token. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.
huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...
To disable this warning, you can either:
        - Avoid using `tokenizers` before the fork if possible
        - Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)
huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...
To disable this warning, you can either:
        - Avoid using `tokenizers` before the fork if possible
        - Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)

4.1.4、问题定位及修改

问题代码:

# 原代码
model_inputs = tokenizer([input_ids], return_tensors="pt").to(model.device)
# 然后直接传给 generate,没有 attention_mask
model.generate(input_ids=model_inputs.input_ids, ...)

修改:

# 1. 分词时确保返回 attention_mask
model_inputs = tokenizer(
    [input_ids], 
    return_tensors="pt", 
    add_special_tokens=True, 
    padding=True # 确保有 padding 逻辑
).to(model.device)

# 2. 提取 input_ids 和 attention_mask
input_ids = model_inputs.input_ids
attention_mask = model_inputs.attention_mask

# 3. 将 attention_mask 传给 generate
# 注意:Qwen 系列通常只需要 input_ids 和 attention_mask
generation_output = model.generate(
    input_ids=input_ids,
    attention_mask=attention_mask,  # <--- 必须加上这个!
    max_new_tokens=max_new_tokens, 
    temperature=temperature, 
    streamer=streamer,
    pad_token_id=tokenizer.eos_token_id # 显式指定 pad_token_id,防止警告
)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容