Skip to content

Conversation

@pseudo-rnd-thoughts
Copy link
Member

Why are these changes needed?

SegmentTree is a component of the rllib PrioritizedEpisodeReplayBuffer however for extreme edge case prefix sum values then find_prefixsum_idx will return invalid out of bounds value.
I couldn't find a bug, rather if the prefixsum_value is equal to the SegmentTree.sum() then traversing down the tree could cause it to return invalid indexes.

I've added unittests to reproduce the original error and check against it

Related issue number

Close #54284

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run pre-commit jobs to lint the changes in this PR. (pre-commit setup)
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

Signed-off-by: Mark Towers <mark@anyscale.com>
cursor[bot]

This comment was marked as outdated.

Copy link
Contributor

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

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 addresses an edge case in SegmentTree.find_prefixsum_idx that could lead to returning an invalid index, particularly when the prefixsum value aligns with the sum of a subtree. The fix involves a small adjustment to the prefixsum value. The addition of a unit test to reproduce and verify the fix is a great step. My feedback focuses on making the logical fix more robust and less dependent on a fixed magic number, which could be brittle under certain conditions.

Signed-off-by: Mark Towers <mark@anyscale.com>
cursor[bot]

This comment was marked as outdated.

@pseudo-rnd-thoughts pseudo-rnd-thoughts added the rllib RLlib related issues label Oct 9, 2025
Copy link
Contributor

@simonsays1980 simonsays1980 left a comment

Choose a reason for hiding this comment

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

Awesome PR @pseudo-rnd-thoughts!! Thanks for fixing this hard-to-find one.

Args:
idx: The index to insert to. Must be in [0, `self.capacity`[
idx: The index to insert to. Must be in [0, `self.capacity`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!


# Edge case when prefixsum can clip into the invalid regions
# https://github.com/ray-project/ray/issues/54284
if prefixsum >= self.value[idx]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome! Simple fix for a big problem :)

sum_segment.sum() + 0.00001,
]:
prefixsum_idx = sum_segment.find_prefixsum_idx(sample)
assert (
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, like above. Could we use the TestCase checking methods or the one from us? Or is there a specific reason why you would prefer the assert here?

Copy link
Member Author

@pseudo-rnd-thoughts pseudo-rnd-thoughts Oct 14, 2025

Choose a reason for hiding this comment

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

I'm more familiar with pytest rather than unittest and the rest of the file uses assert rather than self.assertTrue.

I've updated to use TestCase implementation

), f"{sum_segment.sum()=}, {sample=}, {prefixsum_idx=}"

# edge cases
for sample in [
Copy link
Contributor

Choose a reason for hiding this comment

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

Very nice! Could we add a comment of why this case could cause problems on the SumSegment?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added

Signed-off-by: Mark Towers <mark@anyscale.com>
cursor[bot]

This comment was marked as outdated.

Copy link
Contributor

@simonsays1980 simonsays1980 left a comment

Choose a reason for hiding this comment

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

LGTM

@simonsays1980 simonsays1980 added rllib-algorithms An RLlib algorithm/Trainer is not learning. go add ONLY when ready to merge, run all tests labels Oct 14, 2025
@simonsays1980 simonsays1980 enabled auto-merge (squash) October 14, 2025 17:08
@github-actions github-actions bot disabled auto-merge October 14, 2025 17:08
pseudo-rnd-thoughts and others added 3 commits October 21, 2025 10:51
# Conflicts:
#	rllib/utils/replay_buffers/tests/test_segment_tree_replay_buffer_api.py
Signed-off-by: Mark Towers <mark@anyscale.com>
# Edge case when prefixsum can clip into the invalid regions
# https://github.com/ray-project/ray/issues/54284
if prefixsum >= self.value[idx]:
prefixsum = self.value[idx] - 1e-5
Copy link

Choose a reason for hiding this comment

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

Bug: Prefix Sum Edge Case Handling Fails

The edge case handling in _sample_prefixsum incorrectly modifies the prefixsum. It clamps valid input values that are slightly above the total sum, and can also result in a negative prefixsum when the total sum is very small or zero. This leads to assertion failures and logically inconsistent states for tree traversal.

Fix in Cursor Fix in Web

@simonsays1980 simonsays1980 merged commit 48724e2 into ray-project:master Oct 21, 2025
6 checks passed
xinyuangui2 pushed a commit to xinyuangui2/ray that referenced this pull request Oct 22, 2025
## Why are these changes needed?

`SegmentTree` is a component of the rllib
`PrioritizedEpisodeReplayBuffer` however for extreme edge case prefix
sum values then `find_prefixsum_idx` will return invalid out of bounds
value.
I couldn't find a bug, rather if the `prefixsum_value` is equal to the
`SegmentTree.sum()` then traversing down the tree could cause it to
return invalid indexes.

I've added unittests to reproduce the original error and check against
it

## Related issue number

Close ray-project#54284

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run pre-commit jobs to lint the changes in this PR.
([pre-commit
setup](https://docs.ray.io/en/latest/ray-contribute/getting-involved.html#lint-and-formatting))
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Mark Towers <mark@anyscale.com>
Co-authored-by: Mark Towers <mark@anyscale.com>
Co-authored-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: xgui <xgui@anyscale.com>
elliot-barn pushed a commit that referenced this pull request Oct 23, 2025
## Why are these changes needed?

`SegmentTree` is a component of the rllib
`PrioritizedEpisodeReplayBuffer` however for extreme edge case prefix
sum values then `find_prefixsum_idx` will return invalid out of bounds
value.
I couldn't find a bug, rather if the `prefixsum_value` is equal to the
`SegmentTree.sum()` then traversing down the tree could cause it to
return invalid indexes.

I've added unittests to reproduce the original error and check against
it

## Related issue number

Close #54284

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run pre-commit jobs to lint the changes in this PR.
([pre-commit
setup](https://docs.ray.io/en/latest/ray-contribute/getting-involved.html#lint-and-formatting))
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Mark Towers <mark@anyscale.com>
Co-authored-by: Mark Towers <mark@anyscale.com>
Co-authored-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: elliot-barn <elliot.barnwell@anyscale.com>
landscapepainter pushed a commit to landscapepainter/ray that referenced this pull request Nov 17, 2025
## Why are these changes needed?

`SegmentTree` is a component of the rllib
`PrioritizedEpisodeReplayBuffer` however for extreme edge case prefix
sum values then `find_prefixsum_idx` will return invalid out of bounds
value.
I couldn't find a bug, rather if the `prefixsum_value` is equal to the
`SegmentTree.sum()` then traversing down the tree could cause it to
return invalid indexes.

I've added unittests to reproduce the original error and check against
it

## Related issue number

Close ray-project#54284

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run pre-commit jobs to lint the changes in this PR.
([pre-commit
setup](https://docs.ray.io/en/latest/ray-contribute/getting-involved.html#lint-and-formatting))
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Mark Towers <mark@anyscale.com>
Co-authored-by: Mark Towers <mark@anyscale.com>
Co-authored-by: simonsays1980 <simon.zehnder@gmail.com>
Aydin-ab pushed a commit to Aydin-ab/ray-aydin that referenced this pull request Nov 19, 2025
## Why are these changes needed?

`SegmentTree` is a component of the rllib
`PrioritizedEpisodeReplayBuffer` however for extreme edge case prefix
sum values then `find_prefixsum_idx` will return invalid out of bounds
value.
I couldn't find a bug, rather if the `prefixsum_value` is equal to the
`SegmentTree.sum()` then traversing down the tree could cause it to
return invalid indexes.

I've added unittests to reproduce the original error and check against
it

## Related issue number

Close ray-project#54284

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run pre-commit jobs to lint the changes in this PR.
([pre-commit
setup](https://docs.ray.io/en/latest/ray-contribute/getting-involved.html#lint-and-formatting))
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Mark Towers <mark@anyscale.com>
Co-authored-by: Mark Towers <mark@anyscale.com>
Co-authored-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: Aydin Abiar <aydin@anyscale.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests rllib RLlib related issues rllib-algorithms An RLlib algorithm/Trainer is not learning.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RLlib] Unexpected KeyError while training SAC

2 participants