问题描述
ChatGLM3 finetune_demo 运行的时候报错:
使用命令行开始微调,我们使用 lora 进行微调
!CUDA_VISIBLE_DEVICES=0 NCCL_P2P_DISABLE=“1” NCCL_IB_DISABLE=“1” python finetune_hf.py data/AdvertiseGen_fix /root/autodl-tmp/ChatGLM3/chatglm3-6b configs/lora.yaml
│ /root/miniconda3/lib/python3.10/site-packages/transformers/tokenization_util │
│ s_base.py:800 in <dictcomp> │
│ │
│ 797 │ │ # Otherwise it passes the casts down and casts the LongTensor │
│ 798 │ │ # into a HalfTensor │
│ 799 │ │ if isinstance(device, str) or is_torch_device(device) or isin │
│ ❱ 800 │ │ │ self.data = {k: v.to(device=device) for k, v in self.data │
│ 801 │ │ else: │
│ 802 │ │ │ logger.warning(f"Attempting to cast a BatchEncoding to ty │
│ 803 │ │ return self │
╰──────────────────────────────────────────────────────────────────────────────╯
AttributeError: 'NoneType' object has no attribute 'to'
解决方案:
AttributeError: ‘NoneType’ object has no attribute ‘to’ 这个错误。我看到在 transformers/tokenization_utils_base.py 文件的第800行,有一个字典推导式 {k: v.to(device=device) for k, v in self.data.items()},这里 v 变量是 None 导致的错误。
修复这个问题的一种方式是在字典推导式中添加一个条件来检查 v 是否为 None。这是修改后的代码:
self.data = {k: v.to(device=device) for k, v in self.data.items() if v is not None}
这行代码将跳过 self.data 中值为 None 的项,只对非 None 的项调用 .to(device=device) 方法。这应该可以解决您遇到的问题。