[Feature] Add generation-stamped replay slots for round-robin writer
Description
Adds generation-stamped slots to the round-robin replay writer (step 1 of the
work in #4041). Every physical storage slot carries an int64 generation counter:
the first write of a slot has generation 0 and every reuse increments it, so a
sampled (index, generation) pair identifies a specific record and becomes
detectably stale once its slot is reused.
- Generation tracking is on by default for round-robin writers, exposed via
writer.generations_of(index)and thetracks_generationscapability flag. Writers that do not track reuse report-1. sample(return_info=True)returns an"index_generation"entry aligned with"index", read under the sampling lock;TensorDictbuffers surface it as a sample key.empty()is monotonic: previously handed-out(index, generation)handles are never revived.- Generations survive
state_dict/load_state_dictanddumps/loads; older checkpoints still load. Lazy (unbounded) storages grow the counter dynamically.
The conditional priority-update API that consumes these stamps is submitted
separately. Cross-process visibility of stamps for share()-d buffers is not
yet supported and is deferred to a follow-up.
Motivation and Context
With a round-robin writer a slot can be overwritten while a learner computes a loss, so a delayed priority update may apply a TD-error to a different record that now occupies the same slot. Stamping each index with a generation lets callers detect that reuse. This PR adds the generation-tracking and sampling side.
Part of #4041.
- [x] I have raised an issue to propose this change
Types of changes
- [x] New feature (non-breaking change which adds core functionality)
Checklist
- [x] I have read the CONTRIBUTION guide (required)
- [ ] My change requires a change to the documentation.
- [x] I have updated the tests accordingly (required for a bug fix or a new feature).
- [ ] I have updated the documentation accordingly.
:link: Helpful Links
:test_tube: See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4046
- :page_facing_up: Preview Python docs built from this PR
Note: Links to docs will display an error until the docs builds have been completed.
This comment was automatically generated by Dr. CI and updates every 15 minutes.
Hi @harryfrzz!
Thank you for your pull request and welcome to our community.
Action Required
In 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.
Process
In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.
If you have received this in error or have any questions, please contact us at [email protected]. Thanks!
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!
Left some comments. Otherwise LGTM! cc @vmoens
sure! i’ve pushed the changes you mentioned @theap06.
@theap06 @vmoens any changes that i need to do here?
With 84c1146 I made it optional to enable this - we can think about enabling it by default 2 releases after the next but the plan must be announced via deprecation warnings since this would be bc breaking. I also added some doc. Happy with this version if you are. Deprecation plan can follow in a follow up PR.
Happy with it, thanks. Opt-in is the right call and the storage owned counters fix the shared storage case. one thing that i want to point out is that the four CUDA tests I added in cfaa0abb still build default writers and assert on _writer._generation, which is None now that tracking is gated. They skip on CPU and are -m gpu-selected on the GPU job. full test/rb is green here (4332 passed, 898 skipped). Should I add track_generations=True to those four? deprecation plan in a follow-up works for me!
Yes you are correct the tests had to be corrected which i did in 6d3af1aec3bb114fa9144514a717f9e04f909828