解决方法直接可以看3(仅记录我的解决历程,不一定好使)
1. 出现问题的场景:
windows使用pytorch_lightning
from pytorch_lightning import Trainer
trainer = Trainer.from_argparse_args()
2. 可能发生:
初次配置时可能出现,调用时出现GPU调用出错的情况。
- 查看GPU是否能够调用
torch.cuda.is_available()
#返回false则torch安装错误
- 可能pytorch未安装GPU版
#库版本
torchmetrics 0.5
pytorch-lightning 1.3.3
torch 1.13+cu116 #我的cuda是12.x
安装GPU版torch可以在官网查询pip指令: PyTorch
3.解决方案(可能能成功?)
3.1 自己的main函数加
import os
os.environ["PL_TORCH_DISTRIBUTED_BACKEND"] = "gloo"
3.2 有做如下修改成功的
scaling_config = ScalingConfig(num_workers=1, use_gpu=True)
torch_config = TorchConfig(backend="gloo")
trainer = TorchTrainer(
train_loop_per_worker=train_loop_per_worker,
torch_config=torch_config,
scaling_config=scaling_config,
datasets={"train": train_dataset}
)
results = trainer.fit()
3.3 有更改pytorch-ligntning版本成功的,但是可能会使用到的其他库也修改(2是我的库版本)
3.4 我使用了pytorch-lightning的Trainer,将设置的distributed_backend使用默认即可。
# trainer = Trainer.from_argparse_args(args,
# checkpoint_callback=checkpoint_callback,
# distributed_backend="ddp",
# logger=logger)
trainer = Trainer.from_argparse_args(args,
checkpoint_callback=checkpoint_callback,
logger=logger)