llama大模型部署

看模型加载的参数设置.

复制代码
import torch

# 初始化Half Tensor
h = torch.tensor([1.0,2.0,3.0], dtype=torch.half)
# h = torch.tensor([1.0,2.0,3.0], dtype=torch.float16) # 跟上面一行一样.

# 查看数据类型
print(h.dtype)
import accelerate
import bitsandbytes
from transformers import AutoTokenizer, AutoModelForCausalLM,TextIteratorStreamer
from transformers import AlbertTokenizer, AlbertModel
model = AlbertModel.from_pretrained('./albert',device_map='auto',torch_dtype=torch.float16,load_in_8bit=True,low_cpu_mem_usage=True)
# torch_dtype 模型本身的类型, 不写的话就自己根据权重文件查询出来.这个是权重文件本身决定的,一般在config.json里面
# load_in_8bit 会把模型转化为8bit类型.这个可以自己设置.

print(1)
  • low_cpu_mem_usage algorithm:

    复制代码
    This is an experimental function that loads the model using ~1x model size CPU memory
    
      Here is how it works:
    
      1. save which state_dict keys we have
      2. drop state_dict before the model is created, since the latter takes 1x model size CPU memory
      3. after the model has been instantiated switch to the meta device all params/buffers that
      are going to be replaced from the loaded state_dict
      4. load state_dict 2nd time
      5. replace the params/buffers from the state_dict
    
      Currently, it can't handle deepspeed ZeRO stage 3 and ignores loading errors

这个算法low_cpu_mem 如果设置True

那么他会进行.

把权重字典的keys保存下来.

然后把state_dict删除.

初始化模型.把需要加载的参数位置放到meta device里面.

再加载state_dict

可以节省cpu内存. 小内存时候需要打开.

相关推荐
啊阿狸不会拉杆2 分钟前
《机器学习导论》第 19 章 - 机器学习实验的设计与分析
人工智能·python·算法·决策树·机器学习·统计检验·评估方法
路人与大师4 分钟前
大庆油田全链路智能体化设计草案
网络·人工智能
格林威7 分钟前
Baumer相机薄膜厚度均匀性评估:基于光学干涉条纹的 6 个核心方法,附 OpenCV+Halcon 实战代码!
人工智能·opencv·计算机视觉·视觉检测·工业相机·智能相机·堡盟相机
张3蜂9 分钟前
python知识点点亮
开发语言·python
好学且牛逼的马10 分钟前
【Hot100|26-LeetCode 21. 合并两个有序链表 - 完整解法详解】
开发语言·python
Katecat9966311 分钟前
矿井地雷检测与识别:Yolo11-HAFB-1模型应用详解
python
星月总相伴12 分钟前
python作用域
开发语言·python
维度攻城狮16 分钟前
Python控制系统仿真案例-串联PID控制
python·simulink·pid·串级pid
qq_242188633222 分钟前
微信小程序AI象棋游戏开发指南
人工智能·微信小程序·小程序
AI英德西牛仔24 分钟前
千问 文心 元宝 Kimi 输出无乱码
人工智能