Skip to content

Commit c784065

Browse files
authored
[BugFix] Propagate create-dataset kwargs to nested PersistentTensorDicts (#1759)
1 parent 720d743 commit c784065

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

tensordict/persistent.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ def _make_nested(self, key, group):
10221022
backend=self.backend,
10231023
# a persisted batch size was already validated at write time
10241024
validate_batch_size=nested_batch_size is None,
1025+
**self.kwargs,
10251026
)
10261027
# share the backend instance so per-store state (e.g. the consolidated
10271028
# metadata flag of the zarr backend) is tracked once per store
@@ -2015,6 +2016,7 @@ def _set_metadata(self, orig_metadata_container: PersistentTensorDict):
20152016
batch_size=td.batch_size,
20162017
device=td.device,
20172018
backend=self.backend,
2019+
**self.kwargs,
20182020
)
20192021
self._nested_tensordicts[key]._backend = self._backend
20202022
self._nested_tensordicts[key].names = td._td_dim_names
@@ -2070,6 +2072,7 @@ def _clone(self, recurse: bool = True, newfile=None) -> PersistentTensorDict:
20702072
backend=self.backend,
20712073
device=self.device,
20722074
batch_size=self.batch_size,
2075+
**self.kwargs,
20732076
)
20742077
clone._nested_tensordicts = nested_tds
20752078
clone._pin_mem = False

test/memmap/test_h5.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
143165
if __name__ == "__main__":
144166
args, unknown = argparse.ArgumentParser().parse_known_args()
145167
pytest.main([__file__, "--capture", "no", "--exitfirst"] + unknown)

test/memmap/test_zarr.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,26 @@ def test_kwargs_passthrough(self, tmp_path):
321321
root = zarr.open_group(str(tmp_path / "s.zarr"), mode="r")
322322
assert root["a"].chunks == (16, 64)
323323

324+
def test_kwargs_passthrough_nested(self, tmp_path):
325+
# create-kwargs must reach the leaves of nested tensordicts too
326+
# https://github.com/pytorch/tensordict/issues/1758
327+
from zarr.codecs import ZstdCodec
328+
329+
td = TensorDict(
330+
{
331+
"a": torch.zeros(64, 3),
332+
"b": {"c": torch.zeros(64, 3), "d": {"e": torch.zeros(64, 3)}},
333+
},
334+
batch_size=[64],
335+
)
336+
td.to_zarr(tmp_path / "s.zarr", chunks=(16,), compressors=ZstdCodec(level=3))
337+
root = zarr.open_group(str(tmp_path / "s.zarr"), mode="r")
338+
for key in ("a", "b/c", "b/d/e"):
339+
assert root[key].chunks == (16, 3), key
340+
assert root[key].compressors, key
341+
td_back = TensorDict.from_zarr(tmp_path / "s.zarr")
342+
assert (td_back == td).all()
343+
324344
def test_chunks_heterogeneous_ranks(self, tmp_path):
325345
# a single chunks spec constrains the leading dims of every leaf,
326346
# whatever its rank, and leaves non-tensor payloads untouched

0 commit comments

Comments
 (0)