From cc1c01dc30b80098c1a73ecb0aca93adb6d1ea36 Mon Sep 17 00:00:00 2001 From: Ben Sprenger Date: Mon, 30 Nov 2026 00:19:08 +0100 Subject: [PATCH] Fix target updater hook default name --- test/test_configs.py | 48 +++++++++++++++++++++++++++++++++++- torchrl/trainers/trainers.py | 2 +- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/test/test_configs.py b/test/test_configs.py index fd529548953..e8c695acb26 100644 --- a/test/test_configs.py +++ b/test/test_configs.py @@ -78,6 +78,7 @@ ValueOperator, ) from torchrl.modules.tensordict_module.exploration import AdditiveGaussianModule +from torchrl.objectives import DQNLoss, SoftUpdate from torchrl.objectives.common import LossModule from torchrl.objectives.ppo import ClipPPOLoss, KLPENPPOLoss, PPOLoss from torchrl.record.loggers import ( @@ -87,7 +88,7 @@ from torchrl.record.loggers.trackio import TrackioLogger from torchrl.record.loggers.wandb import WandbLogger from torchrl.testing.mocking_classes import ContinuousActionVecMockEnv, CountingEnv -from torchrl.trainers import Trainer +from torchrl.trainers import OptimizationStepper, TargetNetUpdaterHook, Trainer from torchrl.trainers.trainers import CountFramesLog # Test if configs can be imported (requires hydra) @@ -3705,6 +3706,51 @@ def load_state_dict(self, state_dict): # implemented (Trainer.state_dict iterates registered modules). hook.load_state_dict(hook.state_dict()) + @pytest.mark.parametrize("name", [None, "custom_name"]) + def test_target_net_updater_hook_registration(self, name): + class OneBatchCollector: + init_random_frames = 0 + + def __iter__(self): + yield TensorDict({}, batch_size=[1]) + + def shutdown(self): + pass + + class NoOpStepper(OptimizationStepper): + def step(self, trainer, sub_batch): + return TensorDict({"loss": torch.zeros(())}, []) + + loss = DQNLoss( + TensorDictModule(torch.nn.Linear(1, 2), ["observation"], ["action_value"]), + action_space="one-hot", + delay_value=True, + ) + with torch.no_grad(): + for parameter in loss.value_network_params.values(True, True): + parameter.zero_() + updater = SoftUpdate(loss, eps=0.5) + with torch.no_grad(): + for parameter in loss.value_network_params.values(True, True): + parameter.fill_(1) + trainer = Trainer( + collector=OneBatchCollector(), + total_frames=1, + frame_skip=1, + optim_steps_per_batch=1, + loss_module=loss, + optimization_stepper=NoOpStepper(), + progress_bar=False, + ) + hook = TargetNetUpdaterHook(updater) + if name is None: + hook.register(trainer) + else: + hook.register(trainer, name=name) + trainer.train() + for target in loss.target_value_network_params.values(True, True): + torch.testing.assert_close(target, torch.full_like(target, 0.5)) + @pytest.mark.skipif(not _has_gymnasium, reason="Gymnasium is not installed") def test_ddpg_trainer_config(self): from torchrl.trainers.algorithms.configs.trainers import DDPGTrainerConfig diff --git a/torchrl/trainers/trainers.py b/torchrl/trainers/trainers.py index be75c98cb48..7aca516dbd3 100644 --- a/torchrl/trainers/trainers.py +++ b/torchrl/trainers/trainers.py @@ -3606,7 +3606,7 @@ def __call__(self, tensordict: TensorCollection | None = None): self.target_params_updater.step() return tensordict - def register(self, trainer: Trainer, name: str): + def register(self, trainer: Trainer, name: str = "target_net_updater"): trainer.register_op("post_steps", self)