MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / reduce_boolean_flags

Function reduce_boolean_flags

tests/unit/common.py:575–586  ·  view source on GitHub ↗
(flag: bool, op=all)

Source from the content-addressed store, hash-verified

573
574
575def 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
589def allclose_on_all_ranks(actual, expected, assert_message=None, **kwargs) -> None:

Callers 2

testMethod · 0.90
allclose_on_all_ranksFunction · 0.85

Calls 5

get_acceleratorFunction · 0.90
get_world_sizeMethod · 0.80
is_initializedMethod · 0.45
current_device_nameMethod · 0.45

Tested by 1

testMethod · 0.72