@@ -360,6 +360,40 @@ def my_func(self, tensor: torch.Tensor, *, an_integer: int):
360360
361361 assert td ["b" ] == 4
362362
363+ def test_repr (self ):
364+ class MyModule (TensorDictModuleBase ):
365+ in_keys = ["a" ]
366+ out_keys = ["b" ]
367+
368+ def __init__ (self ):
369+ super ().__init__ ()
370+ self .net = nn .Linear (3 , 4 )
371+
372+ def forward (self , tensordict ):
373+ tensordict .set ("b" , self .net (tensordict .get ("a" )))
374+ return tensordict
375+
376+ string = repr (MyModule ())
377+ assert "MyModule(" in string
378+ assert "net=Linear(in_features=3, out_features=4" in string
379+ assert "device=" not in string
380+ assert "in_keys=['a']" in string
381+ assert "out_keys=['b']" in string
382+
383+ # an nn.Module payload is a child: listed once, and not through __dict__
384+ mod = TensorDictModule (nn .Linear (3 , 4 ), in_keys = ["a" ], out_keys = ["b" ])
385+ assert "module" not in mod .__dict__
386+ string = repr (mod )
387+ assert string .count ("module=" ) == 1
388+ assert "module=Linear(in_features=3, out_features=4" in string
389+
390+ # a callable that is not an nn.Module is not a child: it comes from __dict__
391+ mod = TensorDictModule (lambda x : x + 1 , in_keys = ["a" ], out_keys = ["b" ])
392+ assert dict (mod .named_children ()) == {}
393+ string = repr (mod )
394+ assert string .count ("module=" ) == 1
395+ assert "module=<function" in string
396+
363397 def test_mutable_sequence (self ):
364398 in_keys = self .MyMutableSequence (["a" , "b" , "c" ])
365399 out_keys = self .MyMutableSequence (["d" , "e" , "f" ])
0 commit comments