怎么建个私人网站,网络营销就业前景和薪水,淮南网咖,天下房产网一、目录
模型参数单位内存计算案例显卡算力推理显存计算训练显存计算huggface 官网计算 模型推理/训练 需要的显存大模型输入长度与显存的关系大模型推理 多线程与显存的关系
参考#xff1a;https://blog.csdn.net/Johntill/article/details/132629075
二、实现 模型参数…一、目录
模型参数单位内存计算案例显卡算力推理显存计算训练显存计算huggface 官网计算 模型推理/训练 需要的显存大模型输入长度与显存的关系大模型推理 多线程与显存的关系
参考https://blog.csdn.net/Johntill/article/details/132629075
二、实现 模型参数单位 “10b”、“13b”、70b等术语通常指的是大型神经网络模型的参数数量。“10b” 意味着模型有大约 100 亿个参数。 内存计算案例 ● fp32 精度一个参数需要 32 bits, 4 bytes. ● fp16 精度一个参数需要 16 bits, 2 bytes. ● int8 精度一个参数需要 8 bits, 1 byte. 内存分配 1.模型参数 2. 梯度 3.优化器参数。 显卡算力 显卡算力是什么 显卡算力是指显卡能够在给定时间内完成多少次浮点运算。它用于评估显卡的性能。通常被表示为每秒执行的浮点运算次数也称为 FLOPSFloating Point Operations Per Second。 计算显卡算力涉及到几个因素。首先需要知道显卡的核心数量、时钟速度和每个核心的浮点运算单元数量。然后将这些因素结合在一起使用以下公式计算显卡算力 显卡算力 核心数量 x 时钟速度 x 浮点运算单元数量 例如如果显卡具有1280个核心时钟速度为1400 MHz每个核心具有两个浮点运算单元则该显卡的算力为 算力 1280 x 1400 x 2 3.584 TFLOPS 推理显存计算 Llama-2-7b-hf 为例全精度模型参数是float32类型 1b(10亿)个模型参数约占用4G显存(实际大小10^9 * 4 / 1024^3 ~ 3.725 GB)那么LLaMA的参数量为7b那么加载模型参数需要的显存为3.725 * 7 ~ 26.075 GB 训练显存计算 大小模型参数占用梯度占用优化器占用CUDA kernel占用 LLaMA-6B为例 模型参数等于参数量每个参数所需内存。 对于 fp32LLaMA-6B 需要 6B4 bytes 24GB内存 对于 int8LLaMA-6B 需要 6B1 byte 6GB 梯度同上等于参数量每个梯度参数所需内存。 对于 fp32LLaMA-6B 需要 6B4 bytes 24GB内存 对于 int8LLaMA-6B 需要 6B1 byte 6GB 优化器参数不同的优化器所储存的参数量不同。 对于常用的 AdamW 来说需要储存两倍的模型参数用来储存一阶和二阶momentum。 fp32 的 LLaMA-6BAdamW 需要 6B8 bytes 48 GB int8 的 LLaMA-6BAdamW 需要 6B2 bytes 12 GB 除此之外CUDA kernel也会占据一些 RAM大概 1.3GB 左右查看方式如下。 综上int8 精度的 LLaMA-6B 模型部分大致需要 6GB6GB12GB1.3GB 25.3GB 左右 huggface 官网计算 推理/训练 需要的显存 https://huggingface.co/spaces/hf-accelerate/model-memory-usage 大模型输入长度与显存的关系
当前程序从启动到目前 查看显存最大占用情况torch.cuda.max_memory_allocated(cuda:0)/1024**2
以chatglm2为例
from transformers import AutoModel, AutoTokenizer
import torch
tokenizer AutoTokenizer.from_pretrained(../chatglm2-6b, trust_remote_codeTrue)
model AutoModel.from_pretrained(../chatglm2-6b, trust_remote_codeTrue).half().to(torch.device(cuda:0))
model model.eval()
res[]
for i in range(100,6000,200):prompttext[:i]max_length len(prompt)top_p 1temperature 0.8response, historymodel.chat(tokenizer, prompt, [], max_lengthmax_length, top_ptop_p,temperaturetemperature)print(torch.cuda.max_memory_allocated(cuda:0)/1024**2) #默认返回当前程序从开始所占用的最大显存print()res.append(torch.cuda.max_memory_allocated(cuda:0)/1024**2)
#查看显存
import matplotlib.pyplot as plt
plt.rcParams[font.sans-serif][SimHei]
x[i for i in range(100,6000,200)]
#服务器无法绘图进行拷贝查看yres
y[11981.630859375, 12013.05419921875, 12056.77099609375, 12104.171875, 12147.87890625, 12194.2587890625, 12246.16259765625, 12283.880859375, 12326.7431640625, 12372.0830078125, 12418.00439453125, 12463.4248046875, 12504.59521484375, 12553.66162109375, 12593.26708984375, 12638.40234375, 12682.5390625, 12728.97021484375, 12790.80615234375, 12846.7744140625, 12894.125, 12944.5283203125, 12990.2294921875, 13030.0068359375, 13080.41064453125, 13126.8134765625, 13173.08203125, 13218.35302734375, 13265.4736328125, 13305.5966796875]
plt.plot(x,y,cr)
plt.ylabel(显存占用:M)
plt.xlabel(序列长度token)
plt.show()8. 大模型推理 多线程与显存的关系
print(torch.cuda.memory_allocated(cuda:0)/1024**2) #torch 模型占用显存
print(torch.cuda.max_memory_allocated(cuda:0)/1024**2) #torch 模型占用显存最大值
显存大小torch.cuda.memory_allocated(cuda:0)/1024**2 (torch.cuda.max_memory_allocated(cuda:0)/1024**2)* 线程数