Fix Rainbow segment tree sampling for non-power-of-two capacities - #557
Open
discobot wants to merge 1 commit into
Open
Fix Rainbow segment tree sampling for non-power-of-two capacities#557discobot wants to merge 1 commit into
discobot wants to merge 1 commit into
Conversation
The implicit heap layout in SumSegmentTree and MinSegmentTree only preserves left-to-right leaf order when the leaf count is a power of two, so retrieve() returned permuted indices for any other capacity, including the default buffer size of 1000000. Pad the leaf array to the next power of two (zeros for the sum tree, inf for the min tree), keeping the public API unchanged. Clamp the index returned by retrieve() so float round-off near total() can never land on a padding leaf, and read sampled priorities through a new SumSegmentTree.get() accessor instead of indexing the tree directly. Add regression tests for prefix-sum order, priority reads, sampling proportionality, empty slots, and min tracking.
|
Someone is attempting to deploy a commit to the Costa Huang's projects Team on Vercel. A member of the Team first needs to authorize it. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Fixes #546.
SumSegmentTree.retrieve()returns permuted indices for any non-power-of-two capacity, including the default--buffer-size 1000000.One correction to the thread: this code did not come from stable-baselines3 — neither SB3 nor sb3-contrib contains a
SegmentTree(GitHub code search returns zero hits in both, while e.g.ReplayBufferreturns plenty). The in-file comment credits openai/baselinessegment_tree.py, whose constructor asserts"capacity must be positive and a power of 2."; the adaptation in #509 dropped that invariant. So there is no upstream to report to, and this PR fixes it here directly.The fix pads the leaf array to the next power of two (
0.0in the sum tree,float("inf")in the min tree), keeping the public API unchanged.retrieve()also clamps its result tocapacity - 1: float round-off can step one leaf past the last real index when the sampled value is within an ulp oftotal(), and unlike the old layout the padded one would otherwise turn that into an out-of-range index.sample()now reads sampled priorities through a newSumSegmentTree.get()accessor instead of indexing the tree array with the old layout's offset.Worth noting for anyone assessing impact on past results: the marginal sampling distribution was never broken — each index still occupied one contiguous mass interval, just in permuted order — so prioritized sampling stayed exactly proportional to priorities. The new test suite includes a 200k-draw proportionality test that passes on both the old and new code, alongside prefix-order regression tests (capacities 1–33) that fail on master. The new test file is wired into the core CI job (CI lists test files explicitly), and the existing
test_rainbow_atarismoke (--buffer-size 10, itself non-power-of-two) passes locally on CPU.Types of changes
Checklist:
pre-commit run --all-filespasses (required).mkdocs serve.If you need to run benchmark experiments for a performance-impacting changes:
--capture_video.python -m openrlbenchmark.rlops.python -m openrlbenchmark.rlopsutility to the documentation.python -m openrlbenchmark.rlops ....your_args... --report, to the documentation.