[RLlib] Add ability to compute percentiles to MetricsLogger/Stats#52963
Merged
ArturNiederfahrenhorst merged 43 commits intoray-project:masterfrom Jun 12, 2025
Merged
Conversation
simonsays1980
approved these changes
Jun 12, 2025
Contributor
simonsays1980
left a comment
There was a problem hiding this comment.
Awesome work @ArturNiederfahrenhorst ! Just a few nits and a question of why not using numpy.percentile to compute percentiles?
| reduce: Optional[str] = "mean", | ||
| window: Optional[Union[int, float]] = None, | ||
| ema_coeff: Optional[float] = None, | ||
| percentiles: Union[List[int], bool] = False, |
| for p in percentiles: | ||
| index = (p / 100) * (n - 1) | ||
|
|
||
| if index.is_integer(): |
Contributor
There was a problem hiding this comment.
Dumb question: why not using here numpy.percentile?
Contributor
Author
There was a problem hiding this comment.
numpy.percentiles does not assume sorted values. So it will end up sorting with at best n log n.
So we could just not sort values when aggregating and us np.percentiles in the end.
But if we train at scale and have 1000 parallel env runners, it will be much quicker to sort on env runners first so that we can use heapq to merge the already sorted lists in linear time after aggregation.
| check(nan_stats3.values, stats_with_values3.values) | ||
|
|
||
|
|
||
| def test_percentiles(): |
elliot-barn
pushed a commit
that referenced
this pull request
Jun 18, 2025
…2963) ## Why are these changes needed? Today, it's not possible to report percentiles of metrics. This PR introduces optional tracking of percentiles in RLlib's metrics reporting. Even after this change, we don't compute percentiles by default because of the considerable overhead. We rather keep this option for powerusers who are able to modify the relevant RLlib components (for example, change the relevant `MetricsLogger.log_value()` call. I did some benchmarking to validate that this would not eat up too much runtime. https://gist.github.com/ArturNiederfahrenhorst/5ded71ebb5ac28d24d1d63c37c4600f2 Sorting Performance (average times): 1,000 values: Stats peek(compile=False): 77.8 μs Stats peek(compile=True): 67.2 μs Pure list.sort(): 72.8 μs 10,000 values: Stats peek(compile=False): 951.1 μs Stats peek(compile=True): 916.2 μs Pure list.sort(): 929.2 μs 1,000,000 values: Stats peek(compile=False): 174.7 ms Stats peek(compile=True): 157.5 ms Pure list.sort(): 158.2 ms Merging Performance (average times): (These assume merging 10 stats objects.) ~1,000 total values: Stats merge_in_parallel(): 229.5 μs Pure heapq.merge(): 177.5 μs ~10,000 total values: Stats merge_in_parallel(): 2.1 ms Pure heapq.merge(): 1.8 ms ~1,000,000 total values: Stats merge_in_parallel(): 302.9 ms Pure heapq.merge(): 192.1 ms With the above in mind, we are efficiently using heapq and list.sort() to implement distributed sorting to compute exact percentiles. In the future, we may look to approximate methods of computing percentiles. Since we only have to reduce once per Algorithm iteration, the above values don't seem overly expensive. Signed-off-by: elliot-barn <elliot.barnwell@anyscale.com>
elliot-barn
pushed a commit
that referenced
this pull request
Jul 2, 2025
…2963) ## Why are these changes needed? Today, it's not possible to report percentiles of metrics. This PR introduces optional tracking of percentiles in RLlib's metrics reporting. Even after this change, we don't compute percentiles by default because of the considerable overhead. We rather keep this option for powerusers who are able to modify the relevant RLlib components (for example, change the relevant `MetricsLogger.log_value()` call. I did some benchmarking to validate that this would not eat up too much runtime. https://gist.github.com/ArturNiederfahrenhorst/5ded71ebb5ac28d24d1d63c37c4600f2 Sorting Performance (average times): 1,000 values: Stats peek(compile=False): 77.8 μs Stats peek(compile=True): 67.2 μs Pure list.sort(): 72.8 μs 10,000 values: Stats peek(compile=False): 951.1 μs Stats peek(compile=True): 916.2 μs Pure list.sort(): 929.2 μs 1,000,000 values: Stats peek(compile=False): 174.7 ms Stats peek(compile=True): 157.5 ms Pure list.sort(): 158.2 ms Merging Performance (average times): (These assume merging 10 stats objects.) ~1,000 total values: Stats merge_in_parallel(): 229.5 μs Pure heapq.merge(): 177.5 μs ~10,000 total values: Stats merge_in_parallel(): 2.1 ms Pure heapq.merge(): 1.8 ms ~1,000,000 total values: Stats merge_in_parallel(): 302.9 ms Pure heapq.merge(): 192.1 ms With the above in mind, we are efficiently using heapq and list.sort() to implement distributed sorting to compute exact percentiles. In the future, we may look to approximate methods of computing percentiles. Since we only have to reduce once per Algorithm iteration, the above values don't seem overly expensive. Signed-off-by: elliot-barn <elliot.barnwell@anyscale.com>
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.
Why are these changes needed?
Today, it's not possible to report percentiles of metrics. This PR introduces optional tracking of percentiles in RLlib's metrics reporting. Even after this change, we don't compute percentiles by default because of the considerable overhead.
We rather keep this option for powerusers who are able to modify the relevant RLlib components (for example, change the relevant
MetricsLogger.log_value()call.I did some benchmarking to validate that this would not eat up too much runtime.
https://gist.github.com/ArturNiederfahrenhorst/5ded71ebb5ac28d24d1d63c37c4600f2
Sorting Performance (average times):
1,000 values:
Stats peek(compile=False): 77.8 μs
Stats peek(compile=True): 67.2 μs
Pure list.sort(): 72.8 μs
10,000 values:
Stats peek(compile=False): 951.1 μs
Stats peek(compile=True): 916.2 μs
Pure list.sort(): 929.2 μs
1,000,000 values:
Stats peek(compile=False): 174.7 ms
Stats peek(compile=True): 157.5 ms
Pure list.sort(): 158.2 ms
Merging Performance (average times):
(These assume merging 10 stats objects.)
~1,000 total values:
Stats merge_in_parallel(): 229.5 μs
Pure heapq.merge(): 177.5 μs
~10,000 total values:
Stats merge_in_parallel(): 2.1 ms
Pure heapq.merge(): 1.8 ms
~1,000,000 total values:
Stats merge_in_parallel(): 302.9 ms
Pure heapq.merge(): 192.1 ms
With the above in mind, we are efficiently using heapq and list.sort() to implement distributed sorting to compute exact percentiles. In the future, we may look to approximate methods of computing percentiles. Since we only have to reduce once per Algorithm iteration, the above values don't seem overly expensive.