📚 模块9:资源管理和常见问题

9.1 有限环境中的内存优化(Colab)

即使使用QLoRA,在Colab中仍可能出现内存耗尽。策略:

a) 减少per_device_train_batch_size

从1或2开始。通过gradient_accumulation_steps进行补偿。

b) 减少max_seq_length

如果内容允许,从512降低到256或384。

c) 使用torch.compile(实验性)

model = torch.compile(model)

可能加速训练并减少内存,但并不总是稳定。

d) 清理CUDA缓存

torch.cuda.empty_cache()

在加载模型后或实验之间很有用。

9.2 常见错误和解决方案

错误:CUDA out of memory

  • 减少批量大小。
  • 增加gradient_accumulation_steps
  • 减少max_seq_length
  • 重启Colab运行时并重新加载所有内容。

错误:Some weights of the model checkpoint ... were not used

如果使用trust_remote_code=True加载或使用PEFT,这是正常的。不严重。

错误:ValueError: Attempting to unscale FP16 gradients.

TrainingArguments中使用optim="adamw_bnb_8bit"optim="paged_adamw_8bit"

警告:The model is not in eval mode

忽略。Trainer会自动处理模式。


Course Info

Course: AI-course3

Language: ZH

Lesson: Module9