Repository navigation
[BugFix] Register TargetNetUpdaterHook at post_optim in register() - #4724
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4724
Note: Links to docs will display an error until the docs builds have been completed. ⏳ 5 Pending, 3 Unrelated FailuresAs of commit 7d000c4 with merge base 82a55f6 ( BROKEN TRUNK - The following jobs failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
A behavioral addition to the counter assertion: the patch below checks the actual target parameters after three optimizer-stepper calls with a hard-update interval of two. Online weights advance 0→1→2→3; target weights must contain 2, not the initial 0 or final 3. It uses the real Trainer, DQNLoss and HardUpdate with a deterministic custom stepper, extending your existing test class. At diff --git a/test/test_trainer.py b/test/test_trainer.py
index 1f32cc04..f5aacd3f 100644
--- a/test/test_trainer.py
+++ b/test/test_trainer.py
@@ -2764,6 +2764,42 @@ class TestOnPolicyTargetNetUpdater:
class TestTargetNetUpdaterHook:
+ def test_register_copies_post_step_parameters_at_interval(self):
+ loss_module = DQNLoss(
+ TensorDictModule(nn.Linear(3, 2), ["observation"], ["action_value"]),
+ action_space="one-hot",
+ delay_value=True,
+ )
+ with torch.no_grad():
+ for parameter in loss_module.value_network_params.values(True, True):
+ parameter.zero_()
+ updater = HardUpdate(loss_module, value_network_update_interval=2)
+
+ class IncrementingStepper(_CountingStepper):
+ def step(self, trainer, sub_batch):
+ with torch.no_grad():
+ for parameter in trainer.loss_module.value_network_params.values(
+ True, True
+ ):
+ parameter.add_(1)
+ return super().step(trainer, sub_batch)
+
+ trainer = Trainer(
+ collector=MockingCollector(),
+ total_frames=None,
+ frame_skip=None,
+ optim_steps_per_batch=3,
+ loss_module=loss_module,
+ optimization_stepper=IncrementingStepper(),
+ )
+ trainer._pbar_str = OrderedDict()
+ TargetNetUpdaterHook(updater).register(trainer, "target_net_updater")
+ trainer.optim_steps(TensorDict())
+ for parameter in loss_module.value_network_params.values(True, True):
+ torch.testing.assert_close(parameter, torch.full_like(parameter, 3))
+ for parameter in loss_module.target_value_network_params.values(True, True):
+ torch.testing.assert_close(parameter, torch.full_like(parameter, 2))
+
def test_register_steps_after_every_optim_step(self):
loss_module = DQNLoss(
TensorDictModule(nn.Linear(3, 2), ["observation"], ["action_value"]),
AI-assisted test development and local verification. |
|
Thanks for the extra test. I checked it: it passes on this branch and fails with |
|
Hi @bsprenger, could you approve CI on this when you have time? It's a one-line fix (post_steps to post_optim) with a test that fails on main. |
54f7d5b to
1fc062c
Compare
| ) | ||
|
|
||
|
|
||
| class TestTargetNetUpdaterHook: |
There was a problem hiding this comment.
Can we either work this test class into an existing one, or make this more generic (maybe TestTrainerHookRegistration or similar), rather than making a whole test class for really just one function?
There was a problem hiding this comment.
Done in cd0ecaa: renamed it to TestTrainerHookRegistration so other hooks can go there. It also checks the new warning.
Also move the test to a generic TestTrainerHookRegistration class.
b1a538b to
7d000c4
Compare
Description
Fixes #4723.
TargetNetUpdaterHook.register()registered the hook atpost_steps, so the target network was updated once per collected batch instead of after every optimizer step. It now registers atpost_optim, as the class docstring and the built-in trainers do.Motivation and Context
Types of changes
Checklist
I used an AI assistant (Claude Code) to help find the bug and run the checks. I've read and tested every changed line.