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

相关推荐
2401_8654396314 小时前
C#怎么将控制台输出保存到TXT_C#如何重定向输出流【源码】
jvm·数据库·python
小超同学你好14 小时前
OpenClaw 深度解析与源代码导读 · 第3篇:Gateway——常驻控制面、单端口多协议与进程骨架
人工智能·深度学习·语言模型·gateway
克里斯蒂亚诺·罗纳尔达14 小时前
智能体学习21——知识检索(RAG)
人工智能·学习·ai
2301_8152795214 小时前
Golang和Node.js哪个适合后端_Golang Node对比教程【实战】
jvm·数据库·python
m0_7488394914 小时前
CSS如何制作圆形头像盒子_设置border-radius-50%
jvm·数据库·python
深蓝海拓14 小时前
基于QtPy (PySide6) 的PLC-HMI工程项目(八)在上位机中解析上行报文
网络·笔记·python·学习·plc
Wyz2012102414 小时前
如何进行SQL字符串大小写转换_运用UPPER与LOWER函数
jvm·数据库·python
qq_1898070314 小时前
SQL报表临时表过大问题_临时表生成机制优化
jvm·数据库·python
遇事不決洛必達14 小时前
某方数据库protobuf详解
爬虫·python·protobuf
XY_墨莲伊14 小时前
【编译原理】实验二:基于有穷自动机FA词法分析器设计与实现
c语言·开发语言·c++·python