[Bug] ray.util.collective allreduce/allreduce_multigpu mutate class attribute types.AllReduceOptions.reduceOp (and create_collective_group has precedence bug in `not all(ranks) < world_size`)
Summary
There are two bugs in python/ray/util/collective/collective.py (commit e4462656e827790245c66d69e800cdfd8df0630a, present at HEAD):
- Global class attribute mutation (
allreducelines326-328,allreduce_multigpulines347-349):
opts = types.AllReduceOptions
opts.reduceOp = op
g.allreduce([tensor], opts)
Because types.AllReduceOptions is assigned without instantiating types.AllReduceOptions(), opts.reduceOp = op mutates the class attribute AllReduceOptions.reduceOp globally! Because TorchGLOOGroup.allreduce(tensor, allreduce_options=AllReduceOptions()) (and allreduce(tensor) instantiating defaults) reads AllReduceOptions.reduceOp, any subsequent call using default options silently runs with the wrong ReduceOp (e.g., MAX instead of SUM), and concurrent callers race on the shared class object.
- Operator-precedence bug in
create_collective_group()(lines231-234):
if not all(ranks) >= 0:
raise RuntimeError("Ranks must be non-negative.")
if not all(ranks) < world_size:
raise RuntimeError("Ranks cannot be greater than world_size.")
not all(ranks) < world_size parses in Python as (not all(ranks)) < world_size, comparing a bool (0 or 1) against world_size instead of checking all(r < world_size for r in ranks)! With ranks = [0, 1] and world_size = 1, all([0, 1]) is False, not False is True (1), 1 < 1 is False, so the check silently passes instead of raising RuntimeError! (Same issue on not all(ranks) >= 0: all([0, -1]) is True, not True is False == 0 >= 0 -> True, falsely rejecting valid [0, 1]? No: (not all([0, 1])) is True >= 0 -> True, while [1, 2] fails only because of set(ranks) != set(range(len(ranks))) above.)
Affected Location
- Repository:
ray-project/ray(e4462656e827790245c66d69e800cdfd8df0630a, present atHEAD) - File & lines:
python/ray/util/collective/collective.py:231-234,326-328,347-349
Minimal Standalone Reproduction
from ray.util.collective import types
# Bug 1: class-level mutation (identical to collective.py:326-328)
opts = types.AllReduceOptions
opts.reduceOp = types.ReduceOp.MAX
assert types.AllReduceOptions().reduceOp == types.ReduceOp.MAX # Corrupted default SUM -> MAX!
# Bug 2: operator precedence (collective.py:233)
ranks, world_size = [0, 1], 1
assert ((not all(ranks)) < world_size) is False # Should have raised RuntimeError!
Suggested Fix
- Instantiate
opts = types.AllReduceOptions(reduceOp=op)(oropts = types.AllReduceOptions(); opts.reduceOp = op) inallreduceandallreduce_multigpu, and avoid mutable default instance creation issues inAllReduceOptions. - Replace lines
231-234withif not all(r >= 0 for r in ranks):andif not all(r < world_size for r in ranks):.
I have investigated and submitted a fix in PR #68621:
- Instantiated \opts = types.AllReduceOptions()\ in \llreduce\ and \llreduce_multigpu\ so that \opts.reduceOp = op\ no longer mutates the class attribute \ ypes.AllReduceOptions.reduceOp\ globally. Also added type annotations to dataclasses in \ ypes.py.
- Corrected operator precedence in \create_collective_group\ from
ot all(ranks) < world_size\ to
ot all(r < world_size for r in ranks)\ and
ot all(r >= 0 for r in ranks). - Added regression tests in \ est_allreduce.py\ and \ est_basic_apis.py.