MCPcopy
hub / github.com/FareedKhan-dev/train-llm-from-scratch / reduce_scalar

Function reduce_scalar

src/post_training/distributed.py:82–90  ·  view source on GitHub ↗

All-reduce a python scalar across ranks (sum or mean). No-op when single-process.

(value: float, ctx: DDPContext, average: bool = True)

Source from the content-addressed store, hash-verified

80
81
82def reduce_scalar(value: float, ctx: DDPContext, average: bool = True) -> float:
83 """All-reduce a python scalar across ranks (sum or mean). No-op when single-process."""
84 if not ctx.enabled:
85 return value
86 t = torch.tensor([value], device=ctx.device, dtype=torch.float32)
87 dist.all_reduce(t, op=dist.ReduceOp.SUM)
88 if average:
89 t /= ctx.world_size
90 return t.item()
91
92
93def barrier(ctx: DDPContext) -> None:

Callers 6

mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected