pytorch ValueError:初始化torch时出错,使用env:// rendezvous分发:需要环境变量MASTER_ADDR,但未设置

axr492tv  于 2023-06-06  发布在  其他
关注(0)|答案(3)|浏览(1032)

我无法在PyTorch中初始化BERT模型的组进程,我曾尝试使用以下代码初始化:

import torch
import datetime

torch.distributed.init_process_group(
    backend='nccl',
    init_method='env://',
    timeout=datetime.timedelta(0, 1800),
    world_size=0,
    rank=0,
    store=None,
    group_name=''
)

并尝试访问get_world_size()函数:

num_train_optimization_steps = num_train_optimization_steps // torch.distributed.get_world_size()

完整代码:

train_examples = None
    num_train_optimization_steps = None
    if do_train:
        train_examples = processor.get_train_examples(data_dir)
        num_train_optimization_steps = int(
            len(train_examples) / train_batch_size / gradient_accumulation_steps) * num_train_epochs
        if local_rank != -1:
            import datetime
            torch.distributed.init_process_group(backend='nccl',init_method='env://', timeout=datetime.timedelta(0, 1800), world_size=0, rank=0, store=None, group_name='')
            num_train_optimization_steps = num_train_optimization_steps // torch.distributed.get_world_size()
            print(num_train_optimization_steps)
j1dl9f46

j1dl9f461#

我通过引用https://github.com/NVIDIA/apex/issues/99来解决这个问题。具体运行

python -m torch.distributed.launch xxx.py
ruarlubt

ruarlubt2#

只是更新,而不是运行:

$ python -m torch.distributed.launch --use_env train_script.py

现在你只需要运行:

$ torchrun train_script.py

here所示。

kmbjn2e3

kmbjn2e33#

如果你想在原生python中运行脚本,你也可以将这些行添加到你的脚本中(有助于调试)

os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '12355'

相关问题