910b上跑Chatglm3-6b进行流式输出【pytorch框架】

2024-01-08 18:14:53

准备阶段

避坑阶段

  • 每个人的服务器都不一样,在ChatGLM3/issues中别人只需要修改指定驱动,但是我的不行
    在这里插入图片描述
  • 删除模型文件包中的model.safetensors.index.json,否则加载模型时会自动加载safetensors文件,而不加载bin文件
/home/anaconda3/envs/sakura/lib/python3.9/site-packages/torch_npu/contrib/transfer_to_npu.py:124: RuntimeWarning: torch.jit.script will be disabled by transfer_to_npu, which currently does not support it, if you need to enable torch.jit.script, please do not use transfer_to_npu.
  warnings.warn(msg, RuntimeWarning)
Loading checkpoint shards:   0%|                                                                                                                                     | 0/7 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "/home/HwHiAiUser/work/ChatGLM3/basic_demo/cli_demo.py", line 22, in <module>
    model = AutoModel.from_pretrained(MODEL_PATH, trust_remote_code=True).npu().eval()
  File "/home/anaconda3/envs/sakura/lib/python3.9/site-packages/transformers/models/auto/auto_factory.py", line 558, in from_pretrained
    return model_class.from_pretrained(
  File "/home/anaconda3/envs/sakura/lib/python3.9/site-packages/transformers/modeling_utils.py", line 3187, in from_pretrained
    ) = cls._load_pretrained_model(
  File "/home/anaconda3/envs/sakura/lib/python3.9/site-packages/transformers/modeling_utils.py", line 3560, in _load_pretrained_model
    state_dict = load_state_dict(shard_file)
  File "/home/anaconda3/envs/sakura/lib/python3.9/site-packages/transformers/modeling_utils.py", line 467, in load_state_dict
    with safe_open(checkpoint_file, framework="pt") as f:
FileNotFoundError: No such file or directory: "/home/HwHiAiUser/models/chatglm3-6b/model-00001-of-00007.safetensors"
/home/anaconda3/envs/sakura/lib/python3.9/tempfile.py:817: ResourceWarning: Implicitly cleaning up <TemporaryDirectory '/tmp/tmp1ygjyx3i'>
  _warnings.warn(warn_message, ResourceWarning)

在这里插入图片描述

添加代码

找到ChatGLM3/basic_demo/cli_demo.py
添加以下代码:

import torch
import torch_npu
import torchvision 
import torchvision_npu
from torch_npu.contrib import transfer_to_npu
import os
import platform
import time
torch_device = "npu:3" # 0~7
torch.npu.set_device(torch.device(torch_device))
torch.npu.set_compile_mode(jit_compile=False)
option = {}
option["NPU_FUZZY_COMPILE_BLACKLIST"] = "Tril"
torch.npu.set_option(option)
print("torch && torch_npu import successfully")

模型加载部分修改为:

model = AutoModel.from_pretrained(MODEL_PATH, trust_remote_code=True).npu().eval()

结果展示

在这里插入图片描述

文章来源:https://blog.csdn.net/weixin_46398647/article/details/135459421
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。