Skip to content

fix: add CUDA device support and bfloat16 precision - #2

Open
coo-2022 wants to merge 1 commit into
zhangqin200182:mainfrom
coo-2022:fix-cuda-support
Open

coo-2022 wants to merge 1 commit into
zhangqin200182:mainfrom
coo-2022:fix-cuda-support

Conversation

@coo-2022

Copy link
Copy Markdown

问题

当前代码在 CUDA GPU 上运行时存在两个问题:

  1. 设备检测缺失train.py 只检查 MPS (Mac) 和 CPU,完全遗漏了 CUDA 检测,导致模型始终加载到 CPU 上
  2. 模型未迁移:即使设置了 device='cuda'AutoModelForCausalLM.from_pretrained() 返回的模型仍留在 CPU,需要显式 .to(device)
  3. 精度选择不当:CUDA 下使用 float16 会导致训练时出现 inf/nan 崩溃(device-side assert),改用 bfloat16 更稳定

修复

  • 优先检测 CUDA (torch.cuda.is_available())
  • CUDA 下使用 bfloat16(如果硬件支持),否则回退到 float32
  • 加载模型后显式 .to(device)

测试

  • RTX 4060 Laptop GPU
  • 训练 64 题 × 2 epoch = 128 步
  • 10 分 35 秒完成,无崩溃
  • Reward: 0.867 → 0.914 (+5.4%)

- Detect CUDA availability before falling back to MPS/CPU
- Use bfloat16 when CUDA supports it (safer than float16 for training)
- Explicitly move model to target device after loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant