Repository navigation
Conversation
Use unique-consecutive counts to avoid rescanning the full collector batch once per trajectory. Add an irregular-length equivalence test and a collector benchmark. AI assistance (Cursor) was used for this change. Co-authored-by: Cursor <cursoragent@cursor.com>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4707
Note: Links to docs will display an error until the docs builds have been completed.
|
|
Hi @Hang-get! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Problem
split_trajectoriesruns on each collector batch whensplit_trajs=True. It first found the consecutive trajectory IDs, then rescanned the full flattened ID tensor once per trajectory to compute each length. ForNsteps andTtrajectories, that count wasO(N * T)and called.item()Ttimes.Change
Use
torch.unique_consecutive(..., return_counts=True)to obtain all trajectory lengths in the existing pass, followed by one conversion to a Python list. The output layout and padding behavior are unchanged. This also adds an irregular-length equivalence test and a collector benchmark.Benchmark
Intel Xeon Platinum 8352Y, Linux, Python 3.11.18, PyTorch 2.15.0+cpu, one PyTorch thread. Input: 32,768 steps arranged as 1,024 adjacent trajectories of length 32, with a four-value observation. The benchmark calls the real
split_trajectoriesfunction.mainThis is a 2.34x speedup. Both measurements used pytest-benchmark with at least five rounds; the same output assertions passed before and after the change.
Duplicate check
I searched open and closed issues and PRs for
split_trajectories, trajectory splitting, collector performance, and slow collectors. I found no open implementation of this change.Tests
python -m pytest -q test/test_postprocs.py::TestSplits— 397 passedpython -m pytest -q test/test_postprocs.py::TestSplits::test_consecutive_trajectory_lengths— passed on the baseline and this changepython -m pytest -q benchmarks/test_collectors_benchmark.py::test_split_trajectories_many_short --benchmark-min-rounds=5— passedpre-commit run --files torchrl/collectors/utils.py test/test_postprocs.py benchmarks/test_collectors_benchmark.py— passedI did not run the full test suite or CUDA benchmarks.
AI assistance (Cursor) was used for this change.
Made with Cursor