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

相关推荐
爱吃羊的老虎4 分钟前
【JAVA】python转java:Spring Boot 入门
java·spring boot·python
道友可好7 分钟前
OpenSpec:轻到起飞的 AI 编程规范层
前端·人工智能·后端
后端小肥肠8 分钟前
小红书篇篇 5 位数阅读!我自研了一套全栈爆款笔记 Skills
人工智能·aigc·agent
新加坡内哥谈技术14 分钟前
AI 勇敢新世界中的技术债务
人工智能
ruanyongjing15 分钟前
从机器翻译到智驾:规则派的黄昏与数据革命的终局(五)
人工智能·自然语言处理·机器翻译
Mahi笔记15 分钟前
AI 正在改变独立站运营的 5 个环节
人工智能
IT_陈寒20 分钟前
React状态管理这个坑,我爬了整整三天才出来
前端·人工智能·后端
阿里云大数据AI技术20 分钟前
Skill即服务:用Agent安全玩转云上Flink
人工智能·flink
AI人工智能+20 分钟前
融合图像处理与模式识别算法的智能银行卡识别系统,为金融行业带来了革命性的效率提升
人工智能·深度学习·ocr·银行卡识别
鲲鹏AI探索局21 分钟前
飞书 CLI 实测:让 Codex 接入真实办公流程
人工智能·aigc·飞书cli