Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion rllib/execution/segment_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def __setitem__(self, idx: int, val: float) -> None:
Inserts/overwrites a value in/into the tree.

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!

val: The value to insert.
"""
assert 0 <= idx < self.capacity, f"idx={idx} capacity={self.capacity}"
Expand Down Expand Up @@ -192,6 +192,11 @@ def find_prefixsum_idx(self, prefixsum: float) -> int:
# Global sum node.
idx = 1

# 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 :)

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 Calculation Error

The edge case fix in find_prefixsum_idx can make prefixsum negative when the total sum is very small or zero. This leads to incorrect tree traversal, causing the function to return 0 instead of the expected highest valid index.

Fix in Cursor Fix in Web

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


# While non-leaf (first half of tree).
while idx < self.capacity:
update_idx = 2 * idx
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import unittest

import numpy as np

from ray.rllib.execution.segment_tree import MinSegmentTree, SumSegmentTree
from ray.rllib.env.single_agent_episode import SingleAgentEpisode
from ray.rllib.execution.segment_tree import SumSegmentTree, MinSegmentTree
from ray.rllib.utils.replay_buffers import PrioritizedEpisodeReplayBuffer


class TestSegmentTree(unittest.TestCase):
Expand Down Expand Up @@ -95,6 +96,55 @@ def test_max_interval_tree(self):
assert np.isclose(tree.min(2, -1), 4.0)
assert np.isclose(tree.min(3, 4), 3.0)

@staticmethod
def _get_episode(episode_len=None, id_=None, with_extra_model_outs=False):
eps = SingleAgentEpisode(id_=id_, observations=[0.0], infos=[{}])
ts = np.random.randint(1, 200) if episode_len is None else episode_len
for t in range(ts):
eps.add_env_step(
observation=float(t + 1),
action=int(t),
reward=0.1 * (t + 1),
infos={},
extra_model_outputs=(
{k: k for k in range(2)} if with_extra_model_outs else None
),
)
eps.is_terminated = np.random.random() > 0.5
eps.is_truncated = False if eps.is_terminated else np.random.random() > 0.8
return eps

def test_find_prefixsum_idx(self, buffer_size=80):
"""Fix edge case related to https://github.com/ray-project/ray/issues/54284"""
replay_buffer = PrioritizedEpisodeReplayBuffer(capacity=buffer_size)
sum_segment = replay_buffer._sum_segment

for i in range(10):
replay_buffer.add(self._get_episode(id_=str(i), episode_len=10))

self.assertTrue(sum_segment.capacity >= buffer_size)

# standard cases
for sample in np.linspace(0, sum_segment.sum(), 50):
prefixsum_idx = sum_segment.find_prefixsum_idx(sample)
self.assertTrue(
prefixsum_idx in replay_buffer._tree_idx_to_sample_idx,
f"{sum_segment.sum()=}, {sample=}, {prefixsum_idx=}",
)

# Edge cases (at the boundary then the binary tree can "clip" into invalid regions)
# Therefore, testing using values close to or above the max valid number
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

sum_segment.sum() - 0.00001,
sum_segment.sum(),
sum_segment.sum() + 0.00001,
]:
prefixsum_idx = sum_segment.find_prefixsum_idx(sample)
self.assertTrue(
prefixsum_idx in replay_buffer._tree_idx_to_sample_idx,
f"{sum_segment.sum()=}, {sample=}, {prefixsum_idx=}",
)


if __name__ == "__main__":
import sys
Expand Down