Repository navigation
[core] private worker._wait_generators_bulk - #62741
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new private API, _wait_and_fetch, which allows for per-object fetch_local settings during wait operations, extending the capabilities of the standard ray.wait. The changes include the Python API implementation, Cython bindings, and the underlying C++ CoreWorker::WaitAndFetch logic. Feedback highlights a critical potential integer overflow in the C++ timeout calculation, a request to align default timeout constants with existing APIs for consistency, and a fix for a missing time module import in the test suite.
Signed-off-by: Rueian Huang <[email protected]>
4aed807 to
9ae09a8
Compare
Signed-off-by: Rueian Huang <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d33e4cace3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Signed-off-by: Rueian Huang <[email protected]>
Signed-off-by: Rueian Huang <[email protected]>
Signed-off-by: Rueian Huang <[email protected]>
Signed-off-by: Rueian Huang <[email protected]>
Signed-off-by: Rueian Huang <[email protected]>
275962f to
9a6652d
Compare
Signed-off-by: Rueian Huang <[email protected]>
Signed-off-by: Rueian Huang <[email protected]>
Signed-off-by: Rueian Huang <[email protected]>
Kunchd
left a comment
There was a problem hiding this comment.
Thanks for answering all my previous questions! I left some final minor nits.
| if not fetch_local: | ||
| continue | ||
| required_local_refs.append(ref) | ||
| if ref not in ready_local_ref_set and ref not in refs_seen: |
There was a problem hiding this comment.
is refs_seen necessary? Is it possible for different generators to output the same refs? Or are we trying to catch the case where the user passes the same generator twice.
There was a problem hiding this comment.
Yeah, it is for catching duplicated generators, but agreed that it is not really necessary.
| auto num_items_to_consume = num_items; | ||
| if (end_of_stream_index_ != -1) { | ||
| num_items_to_consume = std::min(num_items, end_of_stream_index_ - next_index_); | ||
| } |
There was a problem hiding this comment.
Should we surface a python error to the caller if they violate our assumption?
Signed-off-by: Rueian Huang <[email protected]>
Signed-off-by: Rueian Huang <[email protected]>
145311b to
82065b1
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit aa61d0e. Configure here.
b8c0451 to
c7f69a9
Compare
Signed-off-by: Rueian Huang <[email protected]>
c7f69a9 to
00b858f
Compare
Signed-off-by: Rueian Huang <[email protected]>
Kunchd
left a comment
There was a problem hiding this comment.
LGTM! I left only one comment that I'll leave up to your judgement on.
| Args: | ||
| num_refs: The number of references to consume, starting from the | ||
| current head of the stream. Must be positive. | ||
| """ |
There was a problem hiding this comment.
Only concern I have left is if someone trying to use the consume_next_ref_n function doesn't have context that the refs needs to be read before consume can be called, so unready refs are silently consumed.
We chatted offline already so I'll leave it up to your judgement for how we should handle this.
There was a problem hiding this comment.
Makes sense. A guard is added in the latest commit.
Signed-off-by: Rueian Huang <[email protected]>
Kunchd
left a comment
There was a problem hiding this comment.
Nice thanks for adding the guard!
This PR adds a new private `_wait_generators_bulk` API for waiting for
batches of next refs from streaming generators to address the challenges
of the current `_next_sync` API.
### The current challenges of the `_next_sync` API are:
1. `_next_sync` is blocking on waiting for the next ref from a generator
because it is where we signal the backpressure of a streaming generator
by advancing the read index of the generator.
2. `_next_sync` doesn't allow waiting for multiple refs with different
`fetch_local` configurations.
These 2 challenges are inconvenient to Ray Data's use cases because:
1. Ray Data needs to wait for multiple generators in parallel to keep
the data pipeline running instead of idling.
2. Ray Data needs to wait for 2 refs from each generator each time, a
block ref, and a metadata ref, with different fetch_local
configurations, so they can't just use `ray.wait(...,
fetch_local=True)`.
### Proposed solution:
Add a new private `_wait_generators_bulk` API for waiting on multiple
next refs from streaming generators. The example usage is as follows:
```python
ready = _wait_generators_bulk(
[
(gen1, [True, False]),
(gen2, [False, True])
],
num_return=1,
timeout=2,
)
assert ready == [(gen1, [ref1, ref2])]
```
The API will return a list of tuples, each containing a generator and a
list of refs. The refs in the list are the next refs from the generator
that are all ready and pulled locally if the corresponding `fetch_local`
is true.
The API works by waiting for the last ref of each generator first, with
a new `_wait_and_fetch` helper, and then picking the ready generators
from there to issue a second `ray.wait` if their remaining refs are
required to be local.
## Benchmark
The initial integration is done at the PR
ray-project#63808 and is benchmarked on the
`worker_scaling_{{num_workers}}_{{worker_type}}` release tests with the
matrix:
```
num_workers: [500, 1000, 2000, 5000]
worker_type: [actors, tasks]
bulk: [0, 1] # whether _wait_generators_bulk path is enabled or not
```
Each config was run 5 times.
### E2E Execution Time
workers | type | bulk=0 | bulk=1 | delta
-- | -- | -- | -- | --
500 | actors | 67.375 +- 0.097 | 65.240 +- 0.979 | -3.17%
500 | tasks | 61.225 +- 0.529 | 53.870 +- 0.234 | -12.01%
1000 | actors | 123.388 +- 0.790 | 110.373 +- 0.534 | -10.55%
1000 | tasks | 115.860 +- 0.811 | 95.560 +- 0.607 | -17.52%
2000 | actors | 261.420 +- 2.266 | 216.533 +- 2.723 | -17.17%
2000 | tasks | 228.154 +- 4.027 | 178.552 +- 0.491 | -21.74%
5000 | actors | 555.034 +- 9.703 | 340.000 +- 10.101 | -38.74%
5000 | tasks | 479.263 +- 12.886 | 395.351 +- 15.269 | -17.51%
---------
Signed-off-by: Rueian Huang <[email protected]>
Signed-off-by: 400Ping <[email protected]>

This PR adds a new private
_wait_generators_bulkAPI for waiting for batches of next refs from streaming generators to address the challenges of the current_next_syncAPI.The current challenges of the
_next_syncAPI are:_next_syncis blocking on waiting for the next ref from a generator because it is where we signal the backpressure of a streaming generator by advancing the read index of the generator._next_syncdoesn't allow waiting for multiple refs with differentfetch_localconfigurations.These 2 challenges are inconvenient to Ray Data's use cases because:
ray.wait(..., fetch_local=True).Alternative solutions considered:
peek_nAPI that returns the next N refs from a generator without consuming them, but then Ray Data needs to manually advance the read index of the generator to consume the refs through anotherack_nAPI.peek_nAPI advance the previous index automatically, but this would require Ray Data to do one more peek to make sure the index is advanced correctly. This solution is really not ideal because the new requirement is implicit, and the backpressure can be broken easily if thepeek_napi is called without proper waiting on the previous refs.Proposed solution:
Add a new private
_wait_generators_bulkAPI for waiting on multiple next refs from streaming generators. The example usage is as follows:The API will return a list of tuples, each containing a generator and a list of refs. The refs in the list are the next refs from the generator that are all ready and pulled locally if the corresponding
fetch_localis true.The API works by waiting for the last ref of each generator first, with a new
_wait_and_fetchhelper, and then picking the ready generators from there to issue a secondray.waitif their remaining refs are required to be local.Benchmark
The initial integration is done at the PR #63808 and is benchmarked on the
worker_scaling_{{num_workers}}_{{worker_type}}release tests with the matrix:Each config was run 5 times.
E2E Execution Time