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内存. 小内存时候需要打开.

相关推荐
人工智能AI技术4 分钟前
给LLM装上长期记忆!用Milvus向量数据库解决AI金鱼记忆问题
人工智能
Evand J12 分钟前
【MATLAB例程,图像滤波降噪12】加权中值滑动窗口滤波(WMF)图像降噪,包含滤波质量评价。附代码下载链接
开发语言·图像处理·人工智能·计算机视觉·matlab·滑动窗口·滤波
tyler_download15 分钟前
揉扁搓圆transformer架构:NAG优化器算法详解
深度学习·算法·transformer
智行合一科技17 分钟前
肖利华博士受邀参加第九届OTC品牌与科普宣传月暨全民健康嘉年华启动大会
人工智能
weixin1997010801620 分钟前
[特殊字符]《京东POP二手品类对接:B2C订单模型 vs 二手C2C属性的字段转换难题》(附Python源码)
前端·python·算法
承渊政道20 分钟前
Python IDLE鸿蒙PC适配全记录:从Tkinter桌面程序到ArkUI原生开发闭环
开发语言·python·harmonyos·鸿蒙系统·桌面程序
七灵微28 分钟前
【AI】代码实战- 基于时间序列的轴承检测1
人工智能·机器学习·ai·pandas·matplotlib
OFIRM碳基硅基35 分钟前
西游金蝉劫 IP · 之 《金蝉子前传渡缘劫》电影 20 亿票房触发 · 项目专项扶持协议 总览 拉格朗日光影动画-西游金蝉劫项目组
大数据·人工智能·西游金蝉劫·蝎子精·蝎子精女妖王·金蝉子前传渡缘劫
人工干智能41 分钟前
爱因斯坦求和约定(Einstein Summation Convention)及einsum() 函数
python
YMWM_42 分钟前
查看realsense相机序列号脚本
开发语言·python·数码相机