| 573 | |
| 574 | |
| 575 | def reduce_boolean_flags(flag: bool, op=all) -> bool: |
| 576 | if not dist.is_initialized(): |
| 577 | return flag |
| 578 | # current_device() is a rank id on CPU, not a valid torch device; use the device name. |
| 579 | device = get_accelerator().current_device_name() |
| 580 | # gloo rejects 0-dim inputs to all_gather_into_tensor, so carry the flag in a 1-dim tensor. |
| 581 | tensor_flag = torch.tensor([1 if flag else 0], dtype=torch.int, device=device) |
| 582 | world_size = dist.get_world_size() |
| 583 | tensor_flag_buf = torch.zeros(world_size, dtype=torch.int, device=device) |
| 584 | dist.all_gather_into_tensor(tensor_flag_buf, tensor_flag) |
| 585 | list_flags = [bool(f) for f in tensor_flag_buf.tolist()] |
| 586 | return op(list_flags) |
| 587 | |
| 588 | |
| 589 | def allclose_on_all_ranks(actual, expected, assert_message=None, **kwargs) -> None: |