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

相关推荐
海边捡石子3 分钟前
用 Nuitka 保护 Python 核心业务源码
linux·后端·python
jiecy5 分钟前
用AI做了个吉他调音器节奏器
python·ai编程
qinglongcn8 分钟前
2026 Agent 记忆安全威胁全景
网络·人工智能·安全·web安全·编辑器·ai编程
ARM+FPGA+AI工业主板定制专家9 分钟前
工业视觉检测:Jetson 整机硬件方案与部署落地总结
人工智能·计算机视觉·视觉检测
Lucky高13 分钟前
大模型世界概述
深度学习·机器学习·ai
鲜于言悠90514 分钟前
2026 GitHub AI开源榜单:冠军换人,Agent技能时代爆发
人工智能
呆呆虫1215 分钟前
批量采 Google Videos 视频搜索与元数据:SerpBase 等 5 种接口方案对比
python·serp api·serpbase·视频元数据·google videos
用户23398617957318 分钟前
TypeScript源码解析:系统提示词组装管线
人工智能
数据世界120 分钟前
医院、零售、电商三端销售数据如何放进同一体系?分层抽样放大模型的分立与统一
大数据·数据库·人工智能
user44651179179120 分钟前
拆解 AgentScope Agent 运行时:一次 reply 从输入到结束的完整链路
人工智能