Skip to content

[Collective] Prevent AllReduceOptions class mutation and fix rank validation precedence - #68621

Open
themanoj-025 wants to merge 1 commit into
ray-project:masterfrom
themanoj-025:fix/collective-allreduce-options-mutation
Open

themanoj-025 wants to merge 1 commit into
ray-project:masterfrom
themanoj-025:fix/collective-allreduce-options-mutation

Conversation

@themanoj-025

Copy link
Copy Markdown

Description

This PR fixes two bugs in
ay.util.collective:

  1. Prevent mutation of \ ypes.AllReduceOptions\ class attribute: In \�llreduce()\ and \�llreduce_multigpu(), options were referenced as \opts = types.AllReduceOptions\ without instantiation (()). When modifying \opts.reduceOp = op, this mutated the class attribute \ ypes.AllReduceOptions.reduceOp\ globally, corrupting future default operations across all callers in the process. Added ()\ instantiation to match all other collective operations (\ReduceOptions(), \BroadcastOptions(), etc.), and properly typed the fields in \ ypes.py\ dataclasses.
  2. Fix operator precedence in \create_collective_group\ rank validation: In \create_collective_group(), the check \if not all(ranks) < world_size:\ had operator precedence issues because <\ evaluated before
    ot. For example with
    anks = [0, 1]\ and \world_size = 1, \�ll([0, 1]) == 0, \

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes several bugs in Ray's collective utility. It corrects logical errors in rank validation within create_collective_group, ensures options classes like AllReduceOptions are properly instantiated rather than mutated at the class level, adds type annotations to convert dataclass class attributes into fields, and introduces corresponding unit tests. The review feedback correctly notes that because ranks is already validated as a permutation, the non-negative check is redundant, and the rank limit check can be simplified to an O(1) length check.

Comment on lines +231 to 234
if not all(r >= 0 for r in ranks):
raise RuntimeError("Ranks must be non-negative.")
if not all(ranks) < world_size:
if not all(r < world_size for r in ranks):
raise RuntimeError("Ranks cannot be greater than world_size.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since ranks is already validated to be a permutation of range(len(ranks)) on line 220, all elements in ranks are guaranteed to be non-negative. Therefore, the check if not all(r >= 0 for r in ranks): is redundant and will never be triggered.\n\nFurthermore, because of the same permutation guarantee, the maximum rank in ranks is len(ranks) - 1. Thus, checking if any rank is greater than or equal to world_size is equivalent to checking if len(ranks) > world_size. This can be simplified to an $O(1)$ length check, avoiding the overhead of a generator expression.

    if len(ranks) > world_size:\n        raise RuntimeError("Ranks cannot be greater than world_size.")

@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant