#Auto的全部去掉,本地读取
from tokenization_chatglm import ChatGLMTokenizer
from modeling_chatglm import ChatGLMForConditionalGeneration
model = ChatGLMForConditionalGeneration.from_pretrained("/local_user/code/chatGLM6B", device_map='auto')
#model = ChatGLMForConditionalGeneration.from_pretrained("THUDM/chatglm-6b", load_in_8bit=True, trust_remote_code=True, device_map='auto')
tokenizer = ChatGLMTokenizer.from_pretrained("/local_user/code/chatGLM6B")
#tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
1. 将model 建立时的 load_in_8bit=False,无法执行 model.generate ,model.generate()时报错“RuntimeError: expected scalar type Half but found Float”;但能运行训练,速度比8bit的快一些,loss正常下降。
2. 使用““torch.set_default_tensor_type(torch.cuda.HalfTensor)”后能正常运行 model.generate ;但无法开始训练,trainer.train()报错“Expected a ‘cuda‘ device type for generator but found ‘cpu‘”;
/usr/local/lib/python3.8/dist-packages/transformers/trainer.py --> line: 773
def _get_train_sampler(self) -> Optional[torch.utils.data.Sampler]:
if self.train_dataset is None or not has_length(self.train_dataset):
return None
generator = None
if self.args.world_size <= 1:
+++ generator = torch.Generator(device=self.args.device)
--- generator = torch.Generator()
这个问题好像一直没有解决,我目前也无法使用fp16训练并预测,我将 load_in_8bit=True,然后finetune 命令中fp16=False 是可以正常训练并预测的,但是当我load_in_8bit=False+fp16=True时是可以正常训练,但是无法预测的,预测时会报错:expected scalar type Half but found Float
机器: 云服务器,24G内存,单卡24G显存 环境:PyTorch 1.12.1,Cuda 11.3.1, Python 3.8,Ubuntu 20.04 (torch使用1.12而非推荐的1.13是因为云服务器提供的镜像只有这个,不知道GPU实际是什么卡不敢贸然换) 问题描述: 把模型和配置文件都放在本地,改动读取地址后,按照项目提供的finetune.ipynb执行没有错误,能正常开始训练,loss的下降也和示例类似。尝试在大模型16位的状态下finetune则出现读取模型后只可以预测或只可以训练LoRA。
(1) load_in_8bit=False 时只能训练/推理
(2) load_in_8bit=False 同时改动transformers的trainer部分代码,使其能推理和开始训练,开始训练后就无法推理。
求助,这里出错的原因是什么,让大模型在16位的状态下训练LoRA应该怎么做? 为什么 “load_in_8bit=True” 的时候既可以预测又可以训练但到FP16的情况下则不行? (PS:16的训练完的LoRA还没尝试保存下来之后是否能够正常使用)
本地读取相关的改动如下:
1. 将model 建立时的 load_in_8bit=False,无法执行 model.generate ,model.generate()时报错“RuntimeError: expected scalar type Half but found Float”;但能运行训练,速度比8bit的快一些,loss正常下降。
2. 使用““torch.set_default_tensor_type(torch.cuda.HalfTensor)”后能正常运行 model.generate ;但无法开始训练,trainer.train()报错“Expected a ‘cuda‘ device type for generator but found ‘cpu‘”;
强行在 transformers 的源代码 trainer 那里把 generator 设成和传入的训练参数一个设备(我这里都是cuda0)后尝试训练, 仍无法开始训练,trainer.train()报错“ValueError: Attempting to unscale FP16 gradients.”
3.在2的基础上将下面 fp16=True 注释掉
前50个step的loss看着也正常,2.+左右,但从第二个报告即100开始loss直接变为0;
即使前50个step,loss正常,一旦开始训练模型就”损坏了“,在执行 generate 时失败,详细报错如下:
查看输出时发现和之前的对比,非0部分的完全一样:取的第一个问答的输出