Skip to content

[BugFix] Register off-policy replay buffer extension on batch_process - #4800

Open
DaDerekkk wants to merge 1 commit into
pytorch:mainfrom
DaDerekkk:fix/off-policy-warmup-replay-extend
Open

DaDerekkk wants to merge 1 commit into
pytorch:mainfrom
DaDerekkk:fix/off-policy-warmup-replay-extend

Conversation

@DaDerekkk

Copy link
Copy Markdown

Fixes #4691

Summary

With synchronous collection (async_collection=False, learner_backend="local"), all six off-policy trainers (SACTrainer, DDPGTrainer, DQNTrainer, TD3Trainer, CQLTrainer, and IQLTrainer) registered rb_trainer.extend on the pre_epoch hook stage instead of batch_process.

Because pre_epoch runs inside Trainer.optim_steps() and Trainer.train() skips optim_steps until collected_frames >= collector.init_random_frames, this caused two issues (as analyzed by @sramireddy2 in #4691):

  1. Batches collected during init_random_frames warmup were dropped without entering the replay buffer.
  2. When num_epochs > 1 (e.g., TD3Trainer(num_epochs=N)), every collected batch was written to the replay buffer N times.

Changes

  • sac.py, ddpg.py, td3.py, cql.py, iql.py: Register rb_trainer.extend on "batch_process" instead of "pre_epoch".
  • dqn.py: Validate mixing_strategy and register self._aggregate_agent_rewards on "batch_process" before registering rb_trainer.extend on "batch_process", ensuring QMIX/VDN aggregated reward keys are populated prior to replay buffer storage.
  • test/test_trainer.py: Add TestOffPolicyReplayBufferExtension (10 tests) covering warmup storage across all 6 trainers (_optim_count == 0 during warmup), no transition duplication for TD3Trainer(num_epochs=3), QMIX/VDN aggregated reward storage with enable_logging=True, and ("collector", "mask") + pre_steps_log consistency.

Design & Scope Notes

  • Hook ordering tradeoff (Option 1 from [BUG] Off-policy trainers drop the init_random_frames warmup batches instead of storing them #4691): Trainer-internal batch_process transforms run before rb_trainer.extend inside __init__; user hooks registered via trainer.register_op("batch_process", ...) after construction will run after replay insertion.
  • On-policy trainers (OnPolicyTrainer, GRPOTrainer): Left on "pre_epoch".
  • OfflineToOnlineReplayBufferHook (offline_to_online.py): Left out of scope here since its extend() returns a key-stripped TensorDict (pre_steps_log expects keys such as ("next", "reward_sum")) and test/test_offline_to_online.py currently asserts _pre_epoch_ops. Happy to follow up separately if desired.

Verification

  • pytest test/test_trainer.py -v -k TestOffPolicyReplayBufferExtension: 10 failed on unmodified main, 10 passed after fix.
  • pytest test/test_trainer.py -q: 253 passed, 37 skipped (optional torchsnapshot, hydra, ale_py, gymnasium, CUDA).
  • pytest test/test_offline_to_online.py -q: 68 passed, 2 skipped.
  • flake8 --config=setup.cfg on all 7 modified files: passed.

@pytorch-bot

pytorch-bot Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4800

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 16 Awaiting Approval

As of commit 6a1034c with merge base 7f79387 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Nov 29, 2026

Copy link
Copy Markdown

Hi @DaDerekkk!

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 cla@meta.com. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Off-policy trainers drop the init_random_frames warmup batches instead of storing them

1 participant