1.为什么大模型推理时显存涨的那么多还一直占着? 2.大模型在gpu和cpu上推理速度如何?3.推理速度上,int8和fp16比起来怎么样?4.大模型有推理能力吗?5.大模型生成时的参数怎么设置?6.有哪些省内存的大语言模型训练/微调/推理方法?
6.1如何估算模型所需的RAM?6.2 Fp16-mixed precision6.3 Int8-bitsandbytes6.4 LoRA6.5 Gradient Checkpointing6.6 Torch FSDPCPU offload
8.应用模式变更 7.如何让大模型输出合规化9.模型输出的分布比较稀疏,怎么处理?
1.为什么大模型推理时显存涨的那么多还一直占着?
1.首先,序列太长了,有很多Q/K/V;2.其次,因为是逐个预测nexttoken,每次要缓存K/V加速解码.
2.大模型在gpu和cpu上推理速度如何?
7B量级下:
单卡A6000和8核AMD的推理速度通常为10:1. cpu推理速度约10token/s;
3.推理速度上,int8和fp16比起来怎么样?
根据实践经验,int8模式一般推理会明显变慢(huggingface的实现)
4.大模型有推理能力吗?
大模型有推理能力.有下面2个方面的体现:
ChatGPT拥有in-contextcorrection的能力,即如果说错了,给出矫正,ChatGPT能听懂错在哪儿了,并向正确的方向修正.in-contextcorrection要比in-contextlearning难了太多,描述越详细清楚,ChatGPT回答得越好.要知道,越详细的描述,在预训练的文本里越难匹配到的.
在询问ChatGPT互联网上并不存在内容的时候,能给出较好答案(如用ChatGPT学建模);ChatGPT能通过信息猜你心中的想法;你可以制定一个全新的游戏规则让ChatGPT和你玩,ChatGPT可以理解.
5.大模型生成时的参数怎么设置?
生成模型预测调参建议:
建议去调整下 top_p num_beams repetition_renalty temperature do_sample=True;
数据生成有重复,调高repetition_renalty;
生成任务表达单一的,样本也不多的,可适当调低temperature,生成的样子跟训练集的比较像;如果 要复现训练集的效果,temperature=0.01即可.
以上是经验参数,具体调参根据任务而定,不是固定的.
1.参数解释:
top_p=0.9
#Moderately increase the probability threshold of nucleus sampling to increasethe quantity of candidate tokens and increase generation diversity.
#The previous low temperature parameter could lead to a severe polarization inthe probability distribution of generated words which degenerates the generation strategy into greedy decoding.
#do_sample parameter is set to False by default. After setting to True thegeneration methods turn into beam-search multinomial sampling decoding strategy
#Configure the probability of the next repeating n-gram to 0 to ensure thatthere are no n-grams appearing twice.This setting is an empirical preliminaryexploration.
repetition_penalty=1.8
#For words that have appeared before in the subsequent prediction process we reduce the probability of their reoccurrence by introducing therepetition_penalty parameter. This setting is an empirical preliminaryexploration.
6.有哪些省内存的大语言模型训练/微调/推理方法?
动机:大模型(LLMs)现在是NLP领域的最主流方法之一,但是大模型的训练/微调/推理需要的内存也越来越多.
举例来说,即使RTX3090有着24GB的RAM,是除了A100之外显存最大的显卡.但使用一块RTX3090依然无法fp32精度训练最小号的LLaMA-6B
Memory-Efficient 的 LLMs 的训l练/微调/推理方法
▪fp16▪int8▪LoRA ●Gradient checkpointing●Torch FSDPCPU offloading
6.1如何估算模型所需的RAM?
首先,我们需要了解如何根据参数量估计模型大致所需的RAM,这在实践中有很重要的参考意义.我们需要通过估算设置batch_size,设置模型精度,选择微调方法和参数分布方法等.
接下来,我们用LLaMA-6B模型为例估算其大致需要的内存.
首先考虑精度对所需内存的影响:
fp32精度,-个参数需要32bits 4bytes. fp16精度,-个参数需要16bits 2bytesint8精度,一个参数需要8bits 1byte.
其次,考虑模型需要的RAM大致分三个部分:
模型参数梯度优化器参数模型参数:等于参数量*每个参数所需内存.
对于fp32,LLaMA-6B 需要 6B*4 bytes = 24GB内存
对于 int8 LLaMA-6B 需要 6B*1 byte = 6GB
梯度:同上,等于参数量*每个梯度参数所需内存.优化器参数:不同的优化器所储存的参数量不同.
对于常用的AdamW来说,需要储存两倍的模型参数(用来储存一阶和二阶momentum).
●fp32 的 LLaMA-6B AdamW 需要 6B*8 bytes = 48 GB●int8 的 LLaMA-6B AdamW 需要 6B*2 bytes = 12 GB
除此之外,CUDAkernel也会占据一些RAM,大概1.3GB左右,查看方式如下.
>torch.ones((1.1)).to("cuda")> print_gpu_utilization()GPU memory occupied: 1343 MB
综上,int8精度的LLaMA-6B模型部分大致需要6GB6GB12GB1.3GB=25.3GB左右.
再根据LLaMA的架构 (hidden_size = 4096 intermediate_size =11008 num_hidden_layers = 32 context_length=2048)计算中间变量内存.
每个instance需要:
所以一张A100(80GBRAM)大概可以在int8精度;batch_size=50的设定下进行全参数训练.
查看消费级显卡的内存和算力:
2023 GPU Benchmark and Graphics Card Comparison Chart: https:/
6.2Fp16-mixedprecision
混合精度训练的大致思路是在forwardpass和gradientputation的时候使用fp16来加速,但是在更新参数时使用fp32.
用torch实现:
CUDA Automatic Mixed Precision examples: hps:///docs/stable/notes/amp_examples.html
torch fp16推理:直接使用madel.half0将模型转换为fp16.
mode1.eva1()
mode1.half()
使用 Huggingface Transformers: 在 TrainingArguments 里声明 fp16=True
6.3Int8-bitsandbytes
Int8是个很极端的数据类型,它最多只能表示-128~127的数字,并且完全没有精度.
为了在训练和inference中使用这个数据类型,bitsandbytes使用了两个方法最大程度地降低了其带来的误差:
1. vector-wise quantization2. mixed precision depasition
Huggingface在这篇文章中用动图解释了quantization的实现:
论文:LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale: abs/2208.07339借助 Huggingface PEFT,使用int8训练opt-6.5B的完整流程: training/Finetune_opt_bnb_peft.ipynb
6.4LoRA
Low-RankAdaptation是微调 LLMs最常用的省内存方法之一,