Repository navigation
Scope collective ID pool per query #24547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rapids-bot
merged 4 commits into
NVIDIA:main
from
Jesse-Bakker:collective-id-contextvar
Oct 15, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
python/cudf_polars/cudf_polars/streaming/actor_graph/collectives/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """Collective operations for building a RapidsMPF actor graph.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from cudf_polars.streaming.actor_graph.collectives.common import ( | ||
| ReserveOpIDs, | ||
| collective_id_scope, | ||
| reserve_op_id, | ||
| ) | ||
|
|
||
| __all__ = ["ReserveOpIDs", "reserve_op_id"] | ||
| __all__ = ["ReserveOpIDs", "collective_id_scope", "reserve_op_id"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import threading | ||
| import uuid | ||
| from typing import Any | ||
|
|
||
| import polars as pl | ||
|
|
||
| import cudf_polars.engine.core | ||
| from cudf_polars.dsl.translate import Translator | ||
| from cudf_polars.streaming.actor_graph.collectives import ( | ||
| ReserveOpIDs, | ||
| collective_id_scope, | ||
| reserve_op_id, | ||
| ) | ||
|
|
||
|
|
||
| def _sort_ir(): | ||
| q = pl.LazyFrame({"a": [3, 1, 2]}).sort("a") | ||
| return Translator(q._ldf.visit(), pl.GPUEngine()).translate_ir() | ||
|
|
||
|
|
||
| def _reserved_ids(ir) -> list[int]: | ||
| with ReserveOpIDs(ir) as collective_id_map: | ||
| return [i for ids in collective_id_map.values() for i in ids] | ||
|
|
||
|
|
||
| def test_collective_id_scope_ignores_held_ids() -> None: | ||
| with collective_id_scope(), reserve_op_id() as expected: | ||
| pass | ||
| # Another query (possibly on another communicator) holds an ID. | ||
| with ( | ||
| collective_id_scope(), | ||
| reserve_op_id(), | ||
| collective_id_scope(), | ||
| reserve_op_id() as op_id, | ||
| ): | ||
| assert op_id == expected | ||
|
|
||
|
|
||
| def test_collective_id_scope_ignores_history() -> None: | ||
| ir = _sort_ir() | ||
| with collective_id_scope(): | ||
| expected = _reserved_ids(ir) | ||
| assert len(expected) > 1 | ||
|
|
||
| with collective_id_scope(): | ||
| # IDs are not released in reverse order of reservation, so earlier | ||
| # queries permute the pool. | ||
| _reserved_ids(ir) | ||
| assert _reserved_ids(ir) != expected | ||
| with collective_id_scope(): | ||
| assert _reserved_ids(ir) == expected | ||
|
|
||
|
|
||
| def test_collective_id_scope_per_thread() -> None: | ||
| with collective_id_scope(), reserve_op_id() as expected: | ||
| pass | ||
| held = threading.Event() | ||
| release = threading.Event() | ||
|
|
||
| def hold() -> None: | ||
| with collective_id_scope(), reserve_op_id(): | ||
| held.set() | ||
| release.wait() | ||
|
|
||
| thread = threading.Thread(target=hold) | ||
| thread.start() | ||
| try: | ||
| held.wait() | ||
| with collective_id_scope(), reserve_op_id() as op_id: | ||
| assert op_id == expected | ||
| finally: | ||
| release.set() | ||
| thread.join() | ||
|
|
||
|
|
||
| def test_evaluate_on_rank_uses_fresh_scope(monkeypatch) -> None: | ||
| with collective_id_scope(), reserve_op_id() as expected: | ||
| pass | ||
| seen: list[int] = [] | ||
|
|
||
| def fake_evaluate_on_rank(*args, **kwargs): | ||
| with reserve_op_id() as op_id: | ||
| seen.append(op_id) | ||
|
|
||
| monkeypatch.setattr( | ||
| cudf_polars.engine.core, "_evaluate_on_rank", fake_evaluate_on_rank | ||
| ) | ||
| unused: Any = None | ||
| with reserve_op_id(): | ||
| cudf_polars.engine.core.evaluate_on_rank( | ||
| unused, unused, unused, unused, unused, query_id=uuid.uuid4() | ||
| ) | ||
| assert seen == [expected] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: What would happen if you ran two queries on the same communicator concurrently with this setup? Because each query generates its own id scope, I think that you would get overlapping ids, and message passing could get mixed up.
Should we key the id scope by the communicator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running concurrent queries on the same communicator would probably fail miserably, whereas before this change, it would probably work as long as the queries are started in the same order on all ranks. Keying the scope by communicator would work for that, but again, only if the queries are started in the same order on all ranks. It should be possible for a caller to sequence that using a queue and a barrier though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, no, it also depends on release timing and each rank can time that slightly differently, so that can still race and cause mismatched collective ids I think.