@@ -140,6 +140,28 @@ def check(x, y):
140140 tree_map (check , td_dict , td_recon_dict )
141141
142142
143+ @pytest .mark .skipif (not _has_h5py , reason = "h5py not found." )
144+ def test_kwargs_passthrough_nested (tmpdir ):
145+ # create_dataset kwargs must reach the leaves of nested tensordicts too
146+ # https://github.com/pytorch/tensordict/issues/1758
147+ tmpdir = Path (tmpdir )
148+ td = TensorDict (
149+ {
150+ "a" : torch .zeros (64 , 3 ),
151+ "b" : {"c" : torch .zeros (64 , 5 ), "d" : {"e" : torch .zeros (64 , 7 )}},
152+ },
153+ batch_size = [64 ],
154+ )
155+ td .to_h5 (tmpdir / "file.h5" , compression = "gzip" , compression_opts = 9 )
156+ with h5py .File (tmpdir / "file.h5" , "r" ) as f :
157+ for key in ("a" , "b/c" , "b/d/e" ):
158+ assert f [key ].compression == "gzip" , key
159+ assert f [key ].compression_opts == 9 , key
160+ td_recon = TensorDict .from_h5 (tmpdir / "file.h5" )
161+ for key in (("a" ,), ("b" , "c" ), ("b" , "d" , "e" )):
162+ assert (td_recon [key ] == td [key ]).all (), key
163+
164+
143165if __name__ == "__main__" :
144166 args , unknown = argparse .ArgumentParser ().parse_known_args ()
145167 pytest .main ([__file__ , "--capture" , "no" , "--exitfirst" ] + unknown )
0 commit comments