本文简要介绍python语言中 torch.distributed.algorithms.Join
的用法。
用法:
class torch.distributed.algorithms.Join(joinables, enable=True, throw_on_early_termination=False, **kwargs)
此类定义通用连接上下文管理器,它允许在进程连接后调用自定义钩子。这些钩子应该遮蔽非连接进程的集体通信,以防止挂起和错误并确保算法的正确性。有关钩子定义的详细信息,请参阅
JoinHook
。警告
上下文管理器要求每个参与的
Joinable
在其自己的每次迭代集体通信之前调用方法notify_join_context()
以确保正确性。警告
上下文管理器要求
JoinHook
对象中的所有process_group
属性都相同。如果有多个JoinHook
对象,则使用第一个对象的device
。进程组和设备信息用于检查未加入的进程,并用于通知进程在启用throw_on_early_termination
时抛出异常,这两者都使用 all-reduce。例子:
>>> import os >>> import torch >>> import torch.distributed as dist >>> import torch.multiprocessing as mp >>> import torch.nn.parallel.DistributedDataParallel as DDP >>> import torch.distributed.optim.ZeroRedundancyOptimizer as ZeRO >>> from torch.distributed.algorithms.join import Join >>> >>> # On each spawned worker >>> def worker(rank): >>> dist.init_process_group("nccl", rank=rank, world_size=2) >>> model = DDP(torch.nn.Linear(1, 1).to(rank), device_ids=[rank]) >>> optim = ZeRO(model.parameters(), torch.optim.Adam, lr=0.01) >>> # Rank 1 gets one more input than rank 0 >>> inputs = [torch.tensor([1.]).to(rank) for _ in range(10 + rank)] >>> with Join([model, optim]): >>> for input in inputs: >>> loss = model(input).sum() >>> loss.backward() >>> optim.step() >>> # All ranks reach here without hanging/erroring
参数:
相关用法
- Python PyTorch JsonParser用法及代码示例
- Python PyTorch frexp用法及代码示例
- Python PyTorch jvp用法及代码示例
- Python PyTorch cholesky用法及代码示例
- Python PyTorch vdot用法及代码示例
- Python PyTorch ELU用法及代码示例
- Python PyTorch ScaledDotProduct.__init__用法及代码示例
- Python PyTorch gumbel_softmax用法及代码示例
- Python PyTorch get_tokenizer用法及代码示例
- Python PyTorch saved_tensors_hooks用法及代码示例
- Python PyTorch positive用法及代码示例
- Python PyTorch renorm用法及代码示例
- Python PyTorch AvgPool2d用法及代码示例
- Python PyTorch MaxUnpool3d用法及代码示例
- Python PyTorch Bernoulli用法及代码示例
- Python PyTorch Tensor.unflatten用法及代码示例
- Python PyTorch Sigmoid用法及代码示例
- Python PyTorch Tensor.register_hook用法及代码示例
- Python PyTorch ShardedEmbeddingBagCollection.named_parameters用法及代码示例
- Python PyTorch sqrt用法及代码示例
- Python PyTorch PackageImporter.id用法及代码示例
- Python PyTorch column_stack用法及代码示例
- Python PyTorch diag用法及代码示例
- Python PyTorch skippable用法及代码示例
- Python PyTorch EndOnDiskCacheHolder用法及代码示例
注:本文由纯净天空筛选整理自pytorch.org大神的英文原创作品 torch.distributed.algorithms.Join。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。