7.pytorch lightning 之GPU设置

文章介绍了如何在PyTorchLightning框架中利用GPU进行模型训练,包括指定使用GPU的数量,如`Trainer(accelerator=gpu,devices=k)`,以及设置精度以优化内存和速度,如`Trainer(precision=16-mixed)`,涵盖了16位、32位和64位精度的使用情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用gpus设备训练

这个也很简单,但要注意版本,官方文档给出的是2.0以上的使用方法。使用方法是设置Trainer的accelerator和device 参数。

# 使用k个gpu
# DEFAULT (int) specifies how many GPUs to use per node
Trainer(accelerator="gpu", devices=k)

# Above is equivalent to
Trainer(accelerator="gpu", devices=list(range(k)))

# 使用指定的两块gpu
# Specify which GPUs to use (don't use when running on cluster)
Trainer(accelerator="gpu", devices=[0, 1])


# 和上面一致
# Equivalent using a string
Trainer(accelerator="gpu", devices="0, 1")

# 使用所有的gpu
# To use all available GPUs put -1 or '-1'
# equivalent to list(range(torch.cuda.device_count()))
Trainer(accelerator="gpu", devices=-1)

训练精度设置

当显存、速度有额外要求时,可以牺牲部分精度换取最少的存储消耗和更快的速度。指定Trainer的precision参数。设置后lightning自动将tensor加载至相应设备,即不用调用.to(device)方法。

# 16位精度
Trainer(precision='16-mixed')

# 32位精度
Trainer(precision="32-true")

# or
Trainer(precision="32")

# or
Trainer(precision=32)

# 64位精度
Trainer(precision="64-true")

# or
Trainer(precision="64")

# or
Trainer(precision=64)
各设备支持的精度

Precision

CPU

GPU

TPU

IPU

16 Mixed

No

Yes

No

Yes

BFloat16 Mixed

Yes

Yes

Yes

No

32 True

Yes

Yes

Yes

Yes

64 True

Yes

Yes

No

No

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值