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

相关推荐
Eloudy3 分钟前
一键构建 pytorch cpp lib 前端和 wheel
人工智能·pytorch·gpu
秋天的一阵风4 分钟前
🔥 Network 里那坨 "data:" 我真看吐了,自制开源 Chrome 插件,AI 流式调试直接开挂
前端·人工智能·后端
Wang's Blog5 分钟前
AI Agent白手起家53: 动态提示词与情感分析模块实战
人工智能
今天AI了吗8 分钟前
AI辅助数据库工具链对比:从SQL优化到架构设计的主流方案评估
数据库·人工智能·sql
circuitsosk8 分钟前
Prompt Engineering进阶:面向复杂业务场景的模板化管理与动态注入策略
python·langchain·prompt·跨境电商·rag·上下文管理·动态注入
3A Cloud11 分钟前
使用 iThinkAir 将“摄影级人像生图”Skill 开发成应用
图像处理·人工智能
AI英德西牛仔12 分钟前
手机文心怎么导出文档?AI 导出鸭一键解决格式崩塌与批量归档难题
人工智能·智能手机·excel·deepseek·ai导出鸭
invicinble13 分钟前
设计b端系统实际思路
人工智能
p***769813 分钟前
Open Minis – 免费开源本地运行 AI Agent 智能助手|实现手机电脑运行的开源 AI Agent 平台
人工智能·智能手机·开源
品牌测评14 分钟前
AI算力平台推荐分享:2026年算力消费透明化观察
人工智能