先说结论
全精度llama2 7B最低显存要求:28GB
全精度llama2 13B最低显存要求:52GB
全精度llama2 70B最低显存要求:280GB
16精度llama2 7B预测最低显存要求:14GB
16精度llama2 13B预测最低显存要求:26GB
16精度llama2 70B预测最低显存要求:140GB
8精度llama2 7B预测最低显存要求:7GB
8精度llama2 13B预测最低显存要求:13GB
8精度llama2 70B预测最低显存要求:70GB
4精度llama2 7B预测最低显存要求:3.5GB
4精度llama2 13B预测最低显存要求:6.5GB
4精度llama2 70B预测最低显存要求:35GB
理论基础
in full precision (float32), every parameter of the model is stored in 32 bits or 4 bytes. Hence 4 bytes / parameter * 7 billion parameters = 28 billion bytes = 28 GB of GPU memory required, for inference only. In half precision, each parameter would be stored in 16 bits, or 2 bytes. Hence you would need 14 GB for inference. There are now also 8 bit and 4 bit algorithms, so with 4 bits (or half a byte) per parameter you would need 3.5 GB of memory for inference.
For training, it depends on the optimizer you use.
In case you use regular AdamW, then you need 8 bytes per parameter (as it not only stores the parameters, but also their gradients and second order gradients). Hence, for a 7B model you would need 8 bytes per parameter * 7 billion parameters = 56 GB of GPU memory. If you use AdaFactor, then you need 4 bytes per parameter, or 28 GB of GPU memory. With the optimizers of bitsandbytes (like 8 bit AdamW), you would need 2 bytes per parameter, or 14 GB of GPU memory.
翻译如下
在全精度(float32)下,模型的每个参数都以 32 位或 4 个字节存储。
因此,4 字节/参数 * 70 亿个参数 = 280 亿字节 = 需要 28 GB GPU 内存,仅用于推理。
在半精度下,每个参数将以 16 位或 2 个字节存储。因此需要 14 GB 来进行推理。
现在还有 8 位和 4 位算法,因此每个参数 4 位(或半个字节)时,将需要 3.5 GB 内存用于推理。
对于训练,这取决于使用的优化器。
如果使用常规 AdamW,则每个参数需要 8 个字节(因为它不仅存储参数,还存储它们的梯度和二阶梯度)。
因此,对于 7B 模型,每个参数需要 8 个字节 * 70 亿个参数 = 56 GB GPU 内存。
如果使用 AdaFactor,则每个参数需要 4 个字节,或 28 GB 的 GPU 内存。
使用位和字节优化器(如 8 位 AdamW),每个参数需要 2 个字节,或 14 GB 的 GPU 内存。
详情见HuggingFace对于单卡GPU推理的介绍:Anatomy of Model’s Memory ,
根据对exllama、Llama-2-70B-chat-GPTQ等模型量化项目用户的反馈与llama2论文的研究,发现显存计算规律符合nielsr的结论。
可选部署方案
1、Llama-2-70B-chat-GPTQ
项目连接:Llama-2-70B-chat-GPTQ
开源协议:Meta AI对于llama2的用户协议
优点:可直接部署运行,可实现上下文记忆
缺点:int4量化,精度下降,目前仅支持70B-chat模型,等待作者后续开放更多型号的轻量化版本。
此项目是对llama2-70B-chat进行了int4量化,显存占用达到了预估水准。
+---------------------------------------------------------------------------------------+

文章详细列出了全精度和不同量化级别下Llama模型对显存的需求,如28GBforfullprecisioninferenceand14GBforhalfprecision.提供了几个轻量化部署方案,包括Llama-2-70B-chat-GPTQ、GGML和exllama,这些项目支持int4或int8量化以降低内存占用,并适用于不同的硬件平台,如GPU和AppleM系列芯片。此外,还提及了一个名为text-Generation-webui的WebUI工具,用于大模型的本地推理。
最低0.47元/天 解锁文章
1391





