ray icon indicating copy to clipboard operation
ray copied to clipboard

[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`)

Open SendoRay opened this issue 6 days ago • 1 comments

Summary

There are two bugs in python/ray/util/collective/collective.py (commit e4462656e827790245c66d69e800cdfd8df0630a, present at HEAD):

  1. Global class attribute mutation (allreduce lines 326-328, allreduce_multigpu lines 347-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.

  1. Operator-precedence bug in create_collective_group() (lines 231-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 at HEAD)
  • 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

  1. Instantiate opts = types.AllReduceOptions(reduceOp=op) (or opts = types.AllReduceOptions(); opts.reduceOp = op) in allreduce and allreduce_multigpu, and avoid mutable default instance creation issues in AllReduceOptions.
  2. Replace lines 231-234 with if not all(r >= 0 for r in ranks): and if not all(r < world_size for r in ranks):.

SendoRay avatar Nov 28 '26 06:11 SendoRay

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.

themanoj-025 avatar Nov 28 '26 10:11 themanoj-025