Python 如何获取本地_rank属性
发动机代码如下所示:Python 如何获取本地_rank属性,python,machine-learning,Python,Machine Learning,发动机代码如下所示: class Engine(object): def __init__(self, cfg, custom_parser=None): self.version = 0.01 self.state = State() self.devices = None self.distributed = False self.logger = None self.cfg = cfg
class Engine(object):
def __init__(self, cfg, custom_parser=None):
self.version = 0.01
self.state = State()
self.devices = None
self.distributed = False
self.logger = None
self.cfg = cfg
if custom_parser is None:
self.parser = argparse.ArgumentParser()
else:
assert isinstance(custom_parser, argparse.ArgumentParser)
self.parser = custom_parser
self.inject_default_parser()
self.args = self.parser.parse_args()
self.continue_state_object = self.args.continue_fpath
if 'WORLD_SIZE' in os.environ:
self.distributed = int(os.environ['WORLD_SIZE']) > 1
if self.distributed:
self.local_rank = self.args.local_rank
self.world_size = int(os.environ['WORLD_SIZE'])
self.world_rank = int(os.environ['RANK'])
torch.cuda.set_device(self.local_rank)
dist.init_process_group(backend="nccl", init_method='env://')
dist.barrier()
self.devices = [i for i in range(self.world_size)]
else:
# todo check non-distributed training
self.world_rank = 1
self.devices = parse_torch_devices(self.args.devices)
def setup_log(self, name='train', log_dir=None, file_name=None):
if not self.logger:
self.logger = get_logger(
name, log_dir, self.args.local_rank, filename=file_name)
else:
self.logger.warning('already exists logger')
return self.logger
我想获取local\u rank,但当我只使用Engine.local\u rank时,它返回AttributeError:type对象“Engine”没有属性“local\u rank”
这样地
您的
local\u排名取决于self.distributed==True
或self.distributed=0
这意味着'WORLD\u SIZE'
需要在os.environ
中,所以只需添加环境变量WORLD\u SIZE
(应该是整数)引擎。local\u排名与self.local\u排名不同。Engine.local_rank建议local_rank是类变量,而不是实例变量。谢谢您的回答。我想我已经讲清楚了