MCPcopy Create free account
hub / github.com/eric-mitchell/direct-preference-optimization / all_gather_if_needed

Function all_gather_if_needed

utils.py:83–91  ·  view source on GitHub ↗

Gather and stack/cat values from all processes, if there are multiple processes.

(values: torch.Tensor, rank: int, world_size: int)

Source from the content-addressed store, hash-verified

81
82
83def all_gather_if_needed(values: torch.Tensor, rank: int, world_size: int) -> torch.Tensor:
84 """Gather and stack/cat values from all processes, if there are multiple processes."""
85 if world_size == 1:
86 return values
87
88 all_values = [torch.empty_like(values).to(rank) for _ in range(world_size)]
89 dist.all_gather(all_values, values)
90 cat_function = torch.cat if values.dim() > 0 else torch.stack
91 return cat_function(all_values, dim=0)
92
93
94def formatted_dict(d: Dict) -> Dict:

Callers 2

get_batch_samplesMethod · 0.90
get_batch_metricsMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected