Download models/utils/ops.py from ICML2022/resefa: direct link, hf CLI and curl.
- Browser
- Download file 502 Bytes
-
https://huggingface.co/spaces/ICML2022/resefa/resolve/main/models/utils/ops.py
- Command line
-
hf download hf://spaces/ICML2022/resefa/models/utils/ops.py
-
curl -L -o ops.py https://huggingface.co/spaces/ICML2022/resefa/resolve/main/models/utils/ops.py
502 Bytes
| # python3.7 | |
| """Contains operators for neural networks.""" | |
| import torch | |
| import torch.distributed as dist | |
| __all__ = ['all_gather'] | |
| def all_gather(tensor): | |
| """Gathers tensor from all devices and executes averaging.""" | |
| if not dist.is_initialized(): | |
| return tensor | |
| world_size = dist.get_world_size() | |
| tensor_list = [torch.ones_like(tensor) for _ in range(world_size)] | |
| dist.all_gather(tensor_list, tensor, async_op=False) | |
| return torch.stack(tensor_list, dim=0).mean(dim=0) | |