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

相关推荐
ZzT14 小时前
AI 时代吃香的三种人:有想法的懒人、替别人着想的人、不被 AI 带偏的人
人工智能·ai编程
龙腾AI白云14 小时前
大模型检索增强生成(RAG)的技术原理与架构设计
数据库·python·机器学习·知识图谱
姚瑞南14 小时前
【PRD】招投标商机雷达爬虫系统
人工智能·经验分享·笔记·爬虫·产品运营·产品经理
fanruitian15 小时前
python 可视化 panda seaborn matplotlib.pyplot
开发语言·python·matplotlib
4SAPI15 小时前
多国产模型混合调度与企业级治理生产选型指南:4SAPI、硅基流动与 OpenRouter 的架构权衡
大数据·人工智能·架构·php
梦想不只是梦与想15 小时前
Python 中的 json 模块
python·json·对象转字符串
武子康15 小时前
Cosmos Curator 总览:从一条样本的组成,选对数据处理入口
人工智能·后端·agent
星河耀银海15 小时前
Python文件操作:read、readline、readlines读取方式对比
python·文件操作·面向对象·基础函数·教程学习
XMAIPC_Robot15 小时前
-40~85℃宽温实测:RK3588 机器人控制器硬件底座,长时间 SLAM 定位稳定性验证
人工智能·fpga开发·机器人·边缘计算·arm+fpga·rk3588+fpga
智购科技自动售货机厂家15 小时前
让AI经营自动售货机:从Project Vend看工具权限与业务规则~YH
大数据·服务器·单片机·嵌入式硬件·深度学习