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

相关推荐
测试秃头怪12 小时前
接口测试与常用接口测试工具详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
安吉升科技12 小时前
餐厅/食堂客流统计ai智能摄像头,优化餐厅运营效率!
人工智能
坐吃山猪12 小时前
【Nanobot】README03_LEVEL2_工具系统架构
python·源码·agent·nanobot
效能革命笔记12 小时前
DevOps工具链选型推荐:聚焦本土适配与安全可控
人工智能·安全·devops
怪祝浙12 小时前
AI学习-LangChain实战-多模态识别agent
人工智能·学习·langchain
逆境不可逃12 小时前
Hello-Agents 第二部分-第六章:框架开发实践
java·人工智能·分布式·学习·架构·rabbitmq
火星资讯12 小时前
优艾智合荣获“国际具身智能技术突破奖”
人工智能
小袁说公考12 小时前
2026公考培训机构硬核测评 | 师资、退费、管理三大核心指标横向对比
人工智能·经验分享·笔记
企服AI产品测评局12 小时前
2026委外加工管控实测:AI工具全流程跟踪能力横向对比与实在Agent深度测评
人工智能·ai·chatgpt
河阿里12 小时前
Python容器:特性、区别和使用场景
开发语言·python