NewYour coding agent can read the release notes before it upgrades.Set up the MCP server →
PyPI
PyPI, #3276 most downloaded on PyPI
A dict[str, torch.Tensor] that behaves like one tensor.
Latest stable
0.15.5
Last release 17 days ago
05 Nov 2026
Release timing varies
gaps range from 8 days to 5 months
Nearly every release is documented
notes for 41 of 41 stable releases
Nothing withdrawn
no release was ever pulled
4 years old
47 releases · first in 2022
Drag across the ruler to compare, or tap a release to open it.
A patch release of 23 bug fixes and 6 speed-ups, with no new features and no deprecations.
update_at_ with leading size-1 dims (#3469, #3502, #3499).TensorDictParams takes partial state dicts with strict=False, and TensorDict.from_module(model).load_state_dict(sd) loads into trainable parameters (#3427, #3479).all_reduce sums the leaves of the same key on every rank, and nested reads and to_tensordict() on lazy stacks run about three times as fast (#3495, #3490, #3489).Against 0.15.4, on one core of a Grace CPU node with torch 2.15, the 177 microbenchmarks of the 0.15.0 notes run 1.057 times as fast on average (geometric mean), and the 471 tests of the benchmark suite 1.018 times. import tensordict takes as long as in 0.15.4.
The numbers below are from the PR descriptions. They are CPU timings on one pinned core of a 24-core desktop (Intel Core Ultra 9 285K) with torch 2.15, against main just before each PR. Under torch.compile, each of these calls gives the same graphs as before.
td.apply(fn) on a TensorDict without dim names, with no other arguments, is about 25% faster: 25.2 instead of 34.4 us for 20 leaves and batch 32, and 5.8 instead of 13.5 us when fn returns None for most leaves (#3488, fixes #3460).to_tensordict() of a lazy stack of plain TensorDicts with the same batch size, device and keys is about 3 times as fast: 36.5 instead of 125.4 us for 8 members of 20 leaves and batch 32 (#3489, #3513, fixes #3451).ls["next", "obs"], is about 3 times as fast: 2.8 instead of 8.9 us for 8 members of 20 leaves and batch 32, and 3.2 instead of 14.0 us one level deeper (#3490, fixes #3452).td[3] takes 12.8 instead of 14.7 us and td[idx] with 16 indices 27.4 instead of 29.7 us, for 20 leaves and batch 32 (#3491, fixes #3458).tc.x = t, takes 0.45 instead of 0.79 us (#3492, fixes #3453).td.unbind(0), list(td) and for row in td are 10 to 18% faster: td.unbind(0) of 32 rows of 20 leaves takes 218 instead of 260 us (#3494, fixes #3459).ls[:, 0] = td, ls[1, 0] = td or ls.set_at_(key, value, (1, 0)), works as in 0.14.3, and update_at_ with such an index works too. Since 0.15.0, these raised IndexError: index 0 is out of bounds for dimension 1 with size -1 (#3469, fixes #3441).update_at_ and set_at_ on such a stack, with a slice or an Ellipsis on the ragged dim, such as ls.update_at_(value, (1, slice(0, 2))), write the selected rows of each member. Since 0.15.0, they raised ValueError: length should not be negative (#3502, fixes #3500).update_at_ on a lazy stack, with an index that selects one element and a value with leading size-1 dims, such as a value of batch size [1], writes the value, as on a TensorDict and as in 0.14.3. In 0.15, it raised RuntimeError (#3499, fixes #3498).t.copy_(t[perm]) and t.update_(t[perm]) on a lazy stack write the rows that the reordered read held. Before, members that an earlier step had overwritten were read again: perm = [3, 2, 1, 0] gave rows [3, 2, 2, 3] (#3493, fixes #3444).get(key, as_padded_tensor=True) pads a dim of size 1 when other members are longer there. Before, it repeated the member's values into the padding, so the result held values that are not in the data and depended on the order of the members (#3474, fixes #3445).h5[1][1:3].set(("p", "q"), v) or the same on a TensorDict or a lazy stack, is written to the source. Before, set and update returned without an error and the value was lost (#3482, fixes #3443)..tdz archives and append_memmap save leaves with one element and a stride other than 1, such as obs[:1, 0]. Before, they raised RuntimeError: self.stride(-1) must be 1 to view Float as Byte (#3468, fixes #3433).to_safetensors saves a conjugate view, such as x.conj(), and a negative view with the values they hold, and consolidate() accepts both. Before, the file held the values before conjugation, with no error, and consolidate() raised RuntimeError (#3473, fixes #3434).bits* and bcomplex32 dtypes load back with load_memmap and load, and consolidate() and pickling a consolidated tensordict accept them. Before, these raised KeyError: 'torch.float8_e4m3fn' (#3475, fixes #3432).to_h5, to_zarr and PersistentTensorDict.from_dict raise a ValueError that names both entries when a key that holds a /, such as "c/d", lands on the path of another entry, such as ("c", "d"). Before, the file kept only one of them and only a UserWarning about replacing an array showed it (#3478, #3516, fixes #3435).memmap, memmap_ and save warn when they write an entry named shape, device or _type, at any depth: load_memmap does not restore such an entry. Before, the load dropped the entry with no warning. Rename the entry to keep it (#3485, fixes #3455).TensorDictParams, such as a TorchRL loss module, loads a partial checkpoint with load_state_dict(sd, strict=False) and returns the missing and unexpected keys, as an nn.Module does. assign=True assigns the tensors, and load pre-hooks run. Before, strict=False raised RuntimeError, assign=True copied, and pre-hooks were skipped (#3427, #3514, fixes #3425).TensorDict.from_module(model).load_state_dict(sd) loads into the model's trainable parameters, as nn.Module.load_state_dict does. Before, it raised _InplaceWriteError for any parameter that requires grad (#3479, fixes #3439).model.to_empty(device=...) on a model built on the meta device, and after to("meta"), a TensorDictParams holds the tensors that model.parameters() returns. Before, the forward read the old meta tensors, or the call raised RuntimeError when the TensorDictParams held a buffer. Conversions under torch.__future__.set_overwrite_module_params_on_conversion(True) keep the two in sync too (#3483, fixes #3440).reset_parameters_recursive(params) and EnsembleModule work on modules with integer or boolean buffers, such as BatchNorm's num_batches_tracked. Before, they raised RuntimeError: only Tensors of floating point dtype can require gradients (#3484, fixes #3462).torch.vmap over a function that runs with params.to_module(module):, the module holds its own parameters again and trains. Before, they were new Parameters with requires_grad=False, and a frozen weight swapped with tensors that require grad came back requiring grad (#3486, fixes #3461).ProbabilisticTensorDictSequential(..., return_composite=True).log_prob(data) conditions each later head on the sample stored in data. Before, heads that read the sample of an earlier head read a new draw: the mean by default, or a random value under set_interaction_type("random") (#3477, fixes #3447).copy.copy and copy.deepcopy of a shadow=True tensorclass with a field named copy or clone work, and a class with a field named set can be built. Before, they raised TypeError; copy.copy and copy.deepcopy worked in 0.14.3 (#3476, fixes #3438).update() with a dict or keyword arguments on a nocast tensorclass with a batch size keeps Python numbers and NumPy scalars, as tc.n = 9 does. Before, tc.update(n=9) raised RuntimeError: batch dimension mismatch (#3480, #3515, fixes #3436).all_reduce sums the leaves of the same key on every rank when the ranks hold the keys in different orders, as after td.broadcast(src=0) of a nested tensordict. Before, it added one key's values to another's with no error, or hung when their shapes differed (#3495, fixes #3464).broadcast, gather_and_stack, scatter and init_remote(use_broadcast=True) work in a group made with dist.new_group, with src and dst given as global ranks, as reduce already took them. Before, a root whose rank in the group differed from its global rank made some ranks raise and others hang (#3487, fixes #3463).torch.stack(tds, dim, out=buffer) with a locked buffer stacks tensordicts that hold strings or other non-tensor data. Before, it raised RuntimeError: Cannot modify locked TensorDict after writing the tensors (#3470, fixes #3429).with td.flatten_keys(separator="/") as flat: and with td.unflatten_keys(separator="/") as nested:, with the separator as a keyword, keep the writes made in the block. Before, the entries kept their old values and a stray key such as "b/c" appeared (#3472, fixes #3430).These fixes can change the behavior of existing code. Many of them change only the exception of a call that fails either way.
Lazy stacks and views:
update_at_ or set_at_ by slice or Ellipsis (#3502) that still fail raise the error of their actual problem: IndexError with the member's own size for an int out of range, RuntimeError for a value that does not fit a member, or ValueError for a slice on a second ragged dim. Before, they raised the IndexError about size -1 (#3469) or ValueError: length should not be negative (#3502).TensorDict.update_at_ do. For example, ls[:, 1] = td on members of 2, 3 and 1 rows writes row 1 of the first two members, then raises IndexError (#3469, #3502).NonTensorStack, such as s[0:0, 0], return an empty NonTensorStack of shape (0,), where they raised IndexError (#3469).update_at_ on a lazy stack, with a value with leading size-1 dims at an element index that fails for another reason, raises that reason's error, such as KeyError for a key that the members lack, or TypeError for keys_to_update on a NonTensorStack, instead of RuntimeError from expand. With a NonTensorData value, it no longer emits the FutureWarning about expanding a NonTensorData (#3499).get(key, as_padded_tensor=True) and lazy_stack(..., as_padded_tensor=True) return padding where they returned copies of size-1 dims, for members of one dtype. Under torch.compile, members of one dtype whose trailing dims differ without broadcasting give the eager result, where they raised RuntimeError (#3474).update(..., inplace=True) on a locked, shared or memory-mapped source raises the lock error, where it returned and dropped the write. update of a leaf that requires grad, through a view whose outer index is a tensor or a mask, raises RuntimeError as set_ does, where it wrote nothing. set(("p", "q"), "hello") through h5[1][1:3] raises ValueError on h5 and TypeError on zarr, after it creates the entry in the file, where it returned and lost the value (#3482).Saving and loading:
to_safetensors of a conjugate view, or of a contiguous negative view, writes the values the tensor holds (x.tolist()) where it wrote the values before conjugation or negation. Under torch.compile(fullgraph=True), consolidate() of such a leaf raises torch._dynamo.exc.Unsupported instead of TorchRuntimeError (#3473).load_memmap of a lazy stack with device="meta" and a bits8, bits16, bits1x8, bits2x4 or bits4x2 leaf raises ValueError: Invalid dtype! instead of KeyError. Loading a bcomplex32 leaf emits torch's "BComplex32 support is experimental" UserWarning (#3475).to_h5, to_zarr and PersistentTensorDict.from_dict with two entries on one path write the file, then raise ValueError, where they returned. When both keys hold the same value of the same type, NaN included, as after td.update(td.flatten_keys("/")), the call returns as before (#3478, #3516).memmap, memmap_, memmap_like, save and PersistentTensorDict.memmap emit a UserWarning for an entry named shape, device or _type. Tests that turn warnings into errors raise there. The files written are unchanged (#3485).tensordict.nn and TensorDictParams:
load_state_dict on a TensorDictParams: strict=False returns the missing keys where it raised. strict=True raises torch's Missing key(s) in state_dict error for a missing nested key, which was skipped, and gives that message instead of "Cannot load state-dict because the key sets don't match" for a missing top-level key (both RuntimeError) (#3427).load_state_dict(sd, assign=True) on a TensorDictParams assigns the tensors, where it copied into the leaves. As in torch, tied leaves come apart, a checkpoint of another dtype changes the leaf's dtype, and an nn.Parameter loaded into a buffer makes it a parameter. Load pre-hooks registered on the TensorDictParams run (#3427).load_state_dict into the parameters of TensorDict.from_module(model) bumps their version counter, as nn.Module.load_state_dict does, so backward() through a graph built before the load that saved a parameter raises torch's "modified by an inplace operation" error (#3479).set_overwrite_module_params_on_conversion(True), any conversion of a module that holds a TensorDictParams, such as double(), to(...) or cpu(), replaces its leaves with the registered parameters, as for a plain nn.Module. An optimizer built before the conversion no longer trains them. A buffer that cannot take the new data in place, as with to_empty or to("meta"), is replaced by the new tensor (#3483).reset_parameters_recursive(params) with an integer or boolean leaf in params runs custom reset_parameters methods under torch.no_grad(), so a custom reset that differentiates its own parameters raises another RuntimeError (#3484).with ... to_module(module): block with the default preserve_module_state=True and without use_state_dict=True, the module gets back its own nn.Parameter objects, with their own requires_grad. Before, a parameter whose requires_grad differed from that of the tensor in its slot came back as a new Parameter. Under torch.compile, a later bare with params: does nothing, where it raised TypeError (#3486).log_prob(data) of a composite sequence returns other values, and other gradients, for the heads that read a stored earlier sample: they are conditioned on the stored sample and no longer back-propagate through the draw of the earlier head. The call makes the same draws as before, but when an earlier head itself reads a stored sample, its draw has other parameters, so rejection samplers such as Gamma's can leave another random state (#3477).Tensorclasses:
shadow=True tensorclass with a field named set from bad input raises the error of the input, such as RuntimeError for a batch size mismatch, instead of TypeError: 'property' object is not callable (#3476).nocast tensorclass with a batch size, update() with a dict that holds numbers and a value that raises writes the numbers before it raises, as a default tensorclass does. Calls that fail either way can raise RuntimeError instead of ValueError: through a sub-tensordict view, and for a dict that holds an int beyond int64 next to a value that doesn't fit (#3480).Distributed:
src and dst of broadcast, gather_and_stack, scatter and init_remote are global ranks. Misuses in a subgroup that raise either way raise the exception of the same misuse on the default group, for example TypeError instead of ValueError from scatter when the root passes no tensordicts. In init_remote(use_broadcast=True), a wrong src on the receivers makes the sender wait for the group's timeout before it raises (#3487).all_reduce(async_op=True) returns its Work objects in key order instead of insertion order, and with bucket_bytes the list can mix Future and Work objects. Ranks with different key names, which the docstring does not support, are paired by sorted key instead of by position, so such a call can hang where it returned, or the reverse. With bucket_bytes and 3 or more ranks, the sums can differ from 0.15.4's in the last bits, because the leaves are packed in key order (#3495).Other:
torch.stack(tds, out=locked) that fails for another reason raises that error, such as TypeError for an input that lacks a key, instead of RuntimeError: Cannot modify locked TensorDict. When a locked out still raises because a tensor entry would receive non-tensor values, the non-tensor entries before it are already replaced (#3470).flatten_keys(separator=s) or unflatten_keys(separator=s), with s as a keyword, an added key that collides with a leaf raises KeyError, as the positional form does, where the block returned with stray keys. On a locked TensorDictParams, a colliding key raises KeyError instead of RuntimeError, and deleting a key raises RuntimeError instead of KeyError (#3472).These calls are slower:
update() with a dict on a nocast tensorclass with a batch size takes about 1.08 times as long, 0.2 to 0.3 us, for the scan for numbers (#3480).get(key, as_padded_tensor=True) on members of 2 or more dims with equal trailing dims takes 1.13 to 1.18 times as long, about 0.12 us per member plus 0.5 us per call (#3474).to_tensordict() of a lazy stack that only its last member keeps off the fast path, such as a last member with a leaf that requires grad or with a nested key of its own, takes 1.13 times as long for 8 members and 1.20 times for 32, on a Grace CPU node (#3489).copy_ and update_ on a lazy stack from a read of the same stack that only reads ahead, such as t[[1, 2, 3, 3]], copy the source first: about twice as long, 79.7 instead of 40.0 us for 4 members on a Grace CPU node, and one more copy of the source in memory. Under torch.compile, a t[list] source gives 3 graphs instead of 2 (#3493).all_reduce sorts the leaves once per call: under 5% on a Grace CPU node (#3495).load_state_dict(sd, assign=True) on a TensorDictParams with 600 leaves takes about 1.6 times as long, since it now assigns, as torch does. Every other load_state_dict call is faster (#3427).double() and float() round trip of a module that holds two TensorDictParams takes about 5% longer (#3483).update_, copy_ and update_at_(value, ()) keep the old value of a non-tensor entry stored once for the batch, as set_non_tensor stores it, when the source holds one value per row. td[:] = value and update_at_(value, slice(None)) write the rows (#3501).MetaData entries, the legacy lazy views and other containers, and for rows of other kinds, such as dicts, lists or NumPy arrays (#3508).update_at_ on a ragged lazy stack with a slice on the ragged dim still raises for a value whose nested tensordict broadcasts over that dim, for a dict value, and for a value with extra leading size-1 dims. A TensorDict value works where the same dict raises (#3503).to() and cpu() on a TensorDictParams raise RuntimeError after update_at_ or set_at_ writes strings into some of its rows, since 0.15.1. module.to(...) on a module that holds it still works (#3504).share_memory_() raises on a locked tensordict that holds a non-tensor value. Call it before lock_() (#3505).update_, copy_ and update_at_(source, ()) with a one-row source write one row of a NonTensorStack entry, then raise ValueError: zip() argument 2 is shorter than argument 1 (#3506).td[1:] or a transpose, saved back to the directory that it maps can leave wrong data there. Save it with atomic=True, copy_existing=True, or to another directory (#3431).copy.deepcopy of a TensorDict or a tensorclass unties keys that share a tensor, such as tied weights. Deep-copy the module, then call from_module on the copy (#3448).update_ and copy_ raise NotImplementedError on a tensordict that holds a nested tensor. apply and update(..., inplace=True) work (#3449).update() with a dict or keyword arguments, and update_(), on a tensorclass with __post_init__ reset the fields that __post_init__ fills, or raise. Set the fields as attributes (#3454).get(key, as_padded_tensor=True) casts members of mixed dtypes to the first member's dtype, so floats after an int64 member are truncated (#3466).update through a view of a view of a nested lazy stack, and set of a new nested key under a TensorDictParams root through a view of a view, lose the value (#3481)."key" in tc on a tensorclass iterates over the rows, so it returns False for a field that is set. Test "key" in tc.keys() (#3437).numel() of a lazy stack whose members have different batch sizes returns 1. Add up the counts of the members (#3442).TensorDictParams.update() with nested plain tensors under an existing key turns those parameters into buffers. Use tuple keys, dict(other.items(True, True)), or update_ (#3446).get(key, as_padded_tensor=True) puts the stack dim of each lazy stack first. Build the lazy stacks along dim 0, or call contiguous() on the inner stacks (#3450).reduce on a lazy stack reduces only the keys that every member holds. Use all_reduce, or call reduce on each member (#3465).unflatten_keys and flatten_keys with-blocks with the separator as a keyword raise KeyError on a locked tensordict that holds nested keys and keys with the separator. Pass the separator by position (#3517).torch.compile, copy_ and update_ on a lazy stack from a reordered read of the same stack still write the wrong rows, as in 0.15.4, from a torch._foreach_copy_ issue in inductor. Eager calls write the right rows (#3518).__post_init__, writes to directories loaded with mode="r", the keys that update_ and copy_ skip, torch 2.15 and weights_only=True with nested tensors, DataLoader(td, batch_size=None), non-tensor entries in send and recv, torch.save of views of a consolidated tensordict, to_h5 and to_zarr of lazy stacks, TensorDictParams with torch.func, torch.export and FSDP2, and the parameter order after to_module (#3467).copy.deepcopy of tied tensors, saving a view back to its own directory, nested TensorDictParams.update(), as_padded_tensor=True on stacks along other dims, reduce and numel() on lazy stacks, tensorclass update() with __post_init__, and "key" in tc (#3497).pip install tensordict==0.15.5
Thanks to @peterdsharpe for the contributions to this release.
Full Changelog: https://github.com/pytorch/tensordict/compare/v0.15.4...v0.15.5
Your coding agent can read these notes before it upgrades. Set up the MCP server →
The parquet and tabular extras need pyarrow 14.0.1 or later, the first release with the fix for CVE-2023-47248 , against pyarrow 10 before ( #3400 ).
tensordict.nn.functional_vmap calls a module once per set of stacked parameters in one torch.vmap and stacks the outputs (#3327).h5[0], reads nothing from the file (#3375, #3408).append_memmap after a killed append, saving a consolidated file over an existing one, lazy stacks, and in-place shape ops on views.zarr extra does: with zarr 3.0.8 to 3.1.0, to_zarr, from_zarr and PersistentTensorDict(backend="zarr") raise an ImportError that names the version and the upgrade command. Before, they ran and warned on every string array. zarr 3.1.1 came out in July 2025 (#3364, fixes #3352).parquet and tabular extras need pyarrow 14.0.1 or later, the first release with the fix for CVE-2023-47248, against pyarrow 10 before (#3400).to_h5 stores each group's batch size and dim names in a __tensordict__ attribute, as zarr stores already do. Older versions of tensordict ignore it, and files without it load as before (#3414).tensordict.nn.functional_vmap(module, params=params, args=(x,)) calls module once per set of parameters in params, such as the output of TensorDict.from_modules, and stacks the outputs along the batch dims of params. It takes in_dims for inputs that differ per set, randomness, chunk_size, method, and loop=True to call the module in a Python loop instead of torch.vmap (#3327, fixes #2813).Against 0.15.3, on one core of an NVIDIA Grace CPU with torch 2.15.0, the 177 microbenchmarks of the 0.15.0 notes run 1.009 times as fast on average (geometric mean), and the 446 tests of the benchmark suite 0.997 times. import tensordict takes about as long: 1.375 s against 1.384 s.
h5[0], build the text from the metadata and read no tensor: 2.3 to 3.8 times as fast on that machine. The text is the same as in 0.15.3 (#3375, #3408, #3423, fixes #3403).These calls are slower:
update(src, keys_to_update=...) with a lazy stack as src and keys deeper than an entry that the tensordict lacks takes 1.17 to 1.20 times as long, and 1.4 to 1.9 times with non-tensor entries in the members. This is the check behind the new FutureWarning of #3368, which goes away with the change in 0.17.consolidate(filename=p) over an existing file of equal or smaller size now writes a new file, so it costs what a save to a new file costs: 1.26 times as long for a small tensordict on that machine, about 2 times for 64 MiB (#3421).from_h5(batch_size=...) and to_h5 take 8 to 9% longer, for the stored batch sizes and names of #3414.td["n", "m"] on an h5 or zarr file gives the nested group the batch size and names that td["n"]["m"] gives. Before, h5 gave it the root's batch size and zarr no names, and some writes raised (#3417, fixes #3410).clone(newfile=) copies empty nested tensordicts, and zarr clones store each group's own batch size. Before, the clone raised KeyError, or reopened with the wrong batch size (#3416, fixes #3409, fixes #3412).from_h5 and from_zarr load a file or group that holds only strings, with no batch dims for such a group. Before, they raised IndexError (#3418, fixes #3411).from_zarr(auto_batch_size=True) gives one dim name per batch dim, as auto_batch_size_() does. Before, later calls such as to_tensordict() raised ValueError (#3419, fixes #3413).to_h5 and to_zarr name the entry and the backend when they can't store a bfloat16 or nested tensor, and say what to do instead (#3373, fixes #3265).append_memmap after an append that was killed midway finishes or rolls back the killed append, then appends. Before, it raised until you renamed the leftover .meta.json.append.tmp files by hand. A directory saved again since the kill keeps its new data (#3376, #3424).consolidate(filename=p) over an existing file of equal or smaller size saves the right data when a tensordict still maps the old file, or when an older result refreshes its metadata after a newer save. Before, the file loaded without an error and held the wrong data (#3421, fixes #3197).consolidate(return_early=True) on a tensorclass gives back the tensorclass from future.result(), as the eager call does. Before, it gave a plain TensorDict (#3365, fixes #3149).update(src, keys_to_update=[("nested", "b")]) with a dict source that has tuple keys writes only the listed entries, as a tensordict source does (#3361, fixes #3216).set_ with a nested key whose entry is missing raises KeyError on a TensorDictParams, a sub-tensordict (including a view of an h5 or zarr file) and a locked tensordict. Before, TensorDictParams dropped the value with no error, and the others raised another error or left an empty entry in the source (#3394).densify() gives a key that some members of a lazy stack lack the batch size of each member that lacks it. Before, the empty entry had the batch size of a member that has the key (#3372, fixes #3219).add_ on a TypedTensorDict that wraps another TypedTensorDict over a lazy stack no longer add entries such as ("0", "x") to the members (#3393, fixes #3392).reshape and repeat_interleave on a slice of an h5 or zarr file, and on the legacy lazy views, raise NotImplementedError before they change anything. Before, they failed midway with an unrelated error, or wrote leaves that no longer matched the view's source (#3407, #3415, fixes #3371, fixes #3406).flatten, unflatten, repeat and gather on a view name the view's class in their error (#3378).pad_sequence(..., return_mask=...) raises ValueError when its masks key is already an entry of the inputs. Before, the masks overwrote that entry or mixed with it (#3356, fixes #3313).to_module(inplace=True) works on modules that override __setattr__, and under torch.compile(fullgraph=True). Before, it raised UnboundLocalError (#3380, fixes #3369).TensorDict.stack and TensorDict.cat return the type of their inputs. Before, they gave Any (mypy) or Unknown (pyright) (#3377).modules= on TensorDictSequential and ProbabilisticTensorDictSequential, which raises at runtime (#3357, fixes #3353).These calls keep their current result and emit a FutureWarning where the result will change:
update(src, keys_to_update=...) with a key deeper than an entry that the tensordict lacks writes the whole sub-tensordict of src. Starting with TensorDict 0.17, it will write only the listed keys, as select does (#3368).update_(src, keys_to_update=...) with an entry that is not a key, such as None, writes every entry of src. Starting with TensorDict 0.18, it will raise (#3362).set_ with a nested key whose entry is missing creates the entry on a TensorDict, a lazy stack or a tensorclass. Starting with TensorDict 0.17, it will raise KeyError, as its docstring says (#3394).TensorDictSequential and ProbabilisticTensorDictSequential with inplace=False, inplace="empty", tensordict_out= or selected_out_keys return entries that are not output keys when an output key is nested. Starting with TensorDict 0.17, they will return only the output keys (#3405).from_h5 warns where restoring the stored batch sizes and dim names, as it will starting with TensorDict 0.17, changes its result. A malformed __tensordict__ attribute from another program is ignored (#3414, #3422).cache_dist argument of ProbabilisticTensorDictModule stays after 0.17. Only cache_dist=True is deprecated, as before (#3401).These fixes can change the behavior of existing code:
ImportError; pip install 'zarr>=3.1.1' fixes it (#3364).set_ raises KeyError in the cases listed under "Keys, set and update". The error is also a RuntimeError, so except RuntimeError still catches it (#3394).reshape and repeat_interleave on a view raise NotImplementedError, also for invalid arguments that raised TypeError, KeyError or ValueError before, and for repeat_interleave(1, inplace=True) on a view without tensor leaves or a legacy lazy view, which changed nothing. A reshape that keeps the batch size on a lazy stack now returns the stack (#3407, #3415).pad_sequence raises ValueError for inputs with an entry at its masks key, where it raised RuntimeError for some and returned mixed masks for others. Pass another key as return_mask (#3356).consolidate(filename=p) over an existing file of equal or smaller size replaces the file: tensordicts and other processes that map the old file, and hard links to it, keep the old data, and the file's extended attributes and ACLs are not kept (#3421).densify(layout=torch.jagged) raises for a lazy stack whose members have different batch sizes and a key that some members lack, where it returned the wrong number of rows for that key (#3372).update(src, keys_to_update=...) with a flat or half-flat dict source writes only the listed entries (#3361).pip install tensordict==0.15.4Thanks to @mottopanikeiku, @peterdsharpe, and @phoeenniixx (first contribution) for their contributions to this release.
Full Changelog: v0.15.3...v0.15.4
13 features and 40 bug fixes. 12 calls warn ahead of changes in TensorDict 0.17 and 0.18; see "Deprecations" and "Behavior changes" below.
print(td) shows the compact form by default, and notebooks show a foldable tree of dtypes and shapes. set_printoptions(compact=False) restores the detailed form (#3143, #3106).append_memmap to add rows to a saved tensordict (#3139, #3145, #3141, #3142).DataLoader with its default collate_fn stacks tensordicts and tensorclasses, and its workers read the rows of h5 and zarr files (#3180, #3223).py.typed marker, so mypy and pyright check code that uses it; typing fixes remove the common false errors (#3097).py.typed marker, so mypy and pyright check code that calls an installed tensordict. Code that passed type checks may report new errors (#3097).h5 extra needs h5py 3.11 or later, against 3.8 before: h5py 3.8 fails tensordict's h5 tests with NumPy 1.25 or later, and h5py 3.9 and 3.10 don't import with NumPy 2 (#3295, #3339, fixes #3274).zarr extra needs zarr 3.1.1 or later, against 3.0 before: PyPI yanked 3.0.0 to 3.0.7, and 3.0.8 to 3.1.0 warn on every string array (#3295, #3307).safetensors extra, pip install "tensordict[safetensors]", installs safetensors 0.7.0 or later, the first release that saves complex tensors (#3246, #3307).from_arrow and to_arrow need pyarrow 15 or later, though the parquet and tabular extras accept pyarrow 10. Older pyarrow raises an ImportError naming its version (#3145, #3248, fixes #3238, fixes #3273).group_by(td, key) splits the rows of a tensordict by the value of an entry, with no mask per group: values, groups = group_by(td, "uid") (#3098, fixes #3089).print(td) shows the compact form by default, such as TensorDict(batch_size=[32], fields={image: f32[3,64,64], label: i64[]}). set_printoptions(compact=False) restores the detailed form (#3143, fixes #3094).compact, such as set_printoptions(show_grad=True) or plain=True, switches to the detailed form, so it applies as in 0.15.2 (#3254, fixes #3240).set_printoptions(html=False) turns it off; compact=False shows the detailed text (#3106, #3300, fixes #3099).split, chunked, unbinded and torch.cated along the first batch dim, as in td[1:3]. Before, these raised (#3113, fixes #3085).td[key].tolist() on a non-tensor entry returns a plain list, as td.get(key).tolist() does; before, it raised AttributeError (#3127, fixes #3077).pad_sequence(samples, padding_value={"labels": -100}) pads each key with its own value, and collate takes pad_keys, nested_keys and mask_key, for use as a collate_fn (#3130, fixes #3076).DataLoader with its default collate_fn stacks tensordicts and tensorclasses, also inside tuples, as tensordict.collate does. Before, it raised TypeError (#3180, fixes #3164).TypeError (#3247, #3309, #3316, #3338, #3349, fixes #3236, fixes #3302, fixes #3306, fixes #3330).to_safetensors(td, path) and from_safetensors(path) save and load tensordicts with their batch sizes, names, tensorclasses, lazy stacks and UnbatchedTensor leaves. Hub checkpoints load as nested tensordicts (#3139, #3246, #3279, fixes #3087, fixes #3235, fixes #3267).td.save(path, atomic=True) and td.memmap(path, atomic=True) write to a temporary directory and rename it into place, so a killed job leaves the old or new directory (#3141, fixes #3092).tensordict.append_memmap(path, rows) adds rows to a saved tensordict along its first batch dim without rewriting it. A failed append leaves the files as they were (#3142, fixes #3093).meta.json records "_format_version": 1 and the class, such as "tensordict:TensorDict", and a new page specifies the format. Older versions still load these files (#3114, fixes #3091).from_arrow(table) and to_arrow(td) convert between Arrow tables and tensordicts without copying numeric columns. Lists become jagged nested tensors and structs nested tensordicts. to_arrow takes jagged leaves with their ragged dim at position 1, as from_arrow gives them, and raises for others (#3145, #3336, #3337, fixes #3104, fixes #3328, fixes #3329).from_arrow raises for nulls by default, also inside lists. nulls="fill" fills them, including unsigned columns, and nulls="mask" adds masks under "_mask" or mask_key (#3292, #3317, #3343, #3350, fixes #3260, fixes #3261, fixes #3334).InitVar fields, dataclasses.field(repr=False) and the class option kw_only=True, as dataclasses do. Before, InitVar and kw_only=True raised, and repr=False was ignored (#3147, fixes #3103).Against 0.15.2, on one core of an NVIDIA Grace CPU with torch 2.14.1, the 177 microbenchmarks of the 0.15.0 notes run 1.012 times as fast on average (geometric mean), and the 446 tests of the benchmark suite 1.021 times. Two calls are slower. Reusing one tensordict of plain tensors across many with td.to_module(model): blocks takes 2.3 times as long, the cost of #3122's fix; pass preserve_module_state=False to avoid it. update_ with a keys_to_update list that warns under #3227's deprecation takes 2 to 2.8 times as long, until it lists only the sub-tensordict key or only the deeper keys.
import tensordict after import torch takes about as long as in 0.15.2, 1.394 s against 1.377 s on the same machine, because it still imports torch._dynamo (#3242, fixes #3231).
The numbers below are from the PR descriptions. Unless a bullet names another machine, they are CPU timings with torch 2.14.1 on a 24-core desktop, against main just before each PR.
pack() along the last dim, its default, skips one no-op movedim per leaf, eager and compiled: 13 leaves pack in 37.1 instead of 43.6 us (#3070).gather_and_stack with gloo sends consolidated buffers instead of pickling each leaf: 2.6-5.2x faster on 2-4 ranks, for 100 small leaves or 4 large ones (#3132, fixes #3078).all_reduce and reduce take bucket_bytes, which packs small leaves into one collective per bucket: 100 leaves of 64 floats reduce 20-74x faster with gloo (#3119, fixes #3088).torch.cat([td, td]) of 1,024 strings takes 0.27 instead of 1.32 ms (#3144, fixes #3102).keys(True) and entry_class on a lazy stack of HDF5 PersistentTensorDicts read no dataset: 0 reads instead of 12 in #3203's example (#3208, fixes #3203).to("cuda", non_blocking_pin=True) pins on the calling thread by default, not a thread per leaf: 1.4-38x faster on an NVIDIA H100 with two Xeon Platinum 8480C (#3228, fixes #3210).DataLoader workers, an epoch over a 20 MB file takes 0.039 instead of 0.091 s for h5, 0.086 instead of 0.164 s for zarr (Intel Core Ultra 9 285K) (#3223).TensorDictParams.apply, named_apply and flatten_keys take 12.6, 12.9 and 6.3 us instead of 20.0, 20.1 and 9.2 us (one core, Intel Core Ultra 9 285K) (#3325).TensorDict.from_dict(td.to_dict(), batch_size=...) reads Python and NumPy ints, floats and bools back as NonTensorData, at any level; before, it raised "batch dimension mismatch" (#3296, #3344, #3348, fixes #3266, fixes #3335).tc.set("x", tc), tc.x = tc and tc.update({"x": tc}) raise ValueError, as for a TensorDict; before, a later repr or clone raised RecursionError (#3170).create_nested(("a", "b")) keeps the entries under "a" on views, TypedTensorDicts, nested lazy stacks and h5 or zarr files; before, it dropped them or raised (#3173, fixes #3008).td.replace(other, b=...) leaves other unchanged when it is, or holds, an h5 or zarr file or a store; before, b was written into it (#3178, #3192, fixes #2984).update(other, keys_to_update=["nested"]) writes every entry of the sub-tensordict "nested", as update_ does; before, it silently wrote none of them (#3212, fixes #3211).maximum_, minimum_, clamp_min_, clamp_max_, lerp_ and & with a dense operand work on a tensordict that holds a lazy stack; before, they raised KeyError (#3159, fixes #1932).clamp(min=lo, max=hi), give the dense result on a lazy stack inside a tensordict or tensorclass; before, they were wrong or raised (#3174).lazy.clamp(lo, max=hi) or lazy.lerp(end, weight=w), gives the dense result; before, the values differed (#3191).lazy.lerp(end=td, weight=0.3) or lazy.add(other=td), gives the dense result; before, it raised KeyError (#3195, fixes #3193).softmax on a lazy stack, nested or not, matches the dense result along every batch dim; before, it was wrong from the stack dim on (#3175).lazy.where(cond, other, pad=0) fills the keys that one side lacks with pad in members that cond selects whole, as TensorDict.where does; before, they were missing (#3194).densify(layout=torch.jagged) gives a (B, j, 3) tensor for a key such as forces (n, 3) that some members lack; before, it raised or misplaced the ragged dim (#3220, fixes #3215).repr() of a lazy stack mixing tensors with strings or tensordicts under one key lists the members' types; before, it raised TypeError or printed ... (#3244, #3281, fixes #3233, fixes #3258).UnbatchedTensor leaves to h5, zarr or a .tdz archive warns that they load back as plain tensors; before, the save dropped their class silently (#3285).tensor_only and autocast tensorclasses accept jaxtyping annotations such as Float[Tensor, "*batch 3"]; before, the class failed to build or to construct (#3107, fixes #3074).copy.replace(tc, x=...) on Python 3.13 and later keeps the batch size, device and names; before, the copy had batch size [] and neither device nor names (#3120, fixes #3072).field(init=False) or field(compare=False); before, it ignored them without a word (#3126, fixes #3073).bf16-true and 64-true precision work on a tensorclass batch; before, they raised AttributeError before the first step (#3183, fixes #3162).td[key] returns for strings can be pickled, copied and deep-copied; before, pickling it raised AttributeError (#3183, fixes #3182).torch.export keeps a dynamic batch dim for a tensorclass with a str field; before, it raised UserError: Constraints violated (#3189, fixes #3163).with td.to_module(model):, td keeps its own tensors, also under shared submodules, so in-place updates such as td.lerp_(params, 0.01) work; before, they raised (#3122, #3161).module(tensordict=td) works like module(td) on TensorDictModule, TensorDictSequential and the probabilistic modules, also through torch.func.functional_call; before, it raised TypeError (#3201, fixes #3199).TensorDictParams takes the arguments that TensorDict takes, such as flatten_keys(is_leaf=...) and cpu(non_blocking=...); before, these raised TypeError, and type checkers rejected set(..., non_blocking=...) (#3325, fixes #3323).consolidate(pin_memory=True) pins when an accelerator is present, and consolidate(filename=other) warns that it writes nothing; before, both were ignored without a word (#3121, #3169).consolidate(filename=...) writes a tensordict that holds a tensorclass, and from_consolidated gives the tensorclass back; before, it raised RuntimeError (#3131, fixes #3117).consolidate(filename=p) over a larger existing file, such as an older checkpoint, reloads the new data; before, it reloaded the old batch size and garbage values (#3198, fixes #3135).state_dict() raises KeyError when a dotted key "a.b" and a nested key ("a", "b") collide; before, one value was silently lost (#3200, #3340, fixes #3133, fixes #3331).load_memmap and TensorDict.load give back the dim names, and a lazy stack's stack-dim name, saved by memmap, save or append_memmap; before, the names were lost (#3284, fixes #3262).load_memmap(path, num_threads=n) of a deflate-compressed archive no longer fails at random with ValueError: operation forbidden on released memoryview object (#3229, fixes #3226).h["n", "d", "e"] gives the file's content after a child deleted, replaced or renamed that group; before, the root returned stale cached entries (#3188, fixes #3181).num_workers > 0, DataLoader workers read the batches of an h5 or zarr PersistentTensorDict; before, the main process read them, so workers slowed loading (#3223).to_zarr with zarr 2 installed raises the ImportError that from_zarr raises, asking for zarr 3; before, it raised TypeError from inside zarr (#3301, fixes #3298).from_store closes its Redis connection when reading the metadata fails, for example on a WRONGTYPE reply; before, the connection stayed open and emitted ResourceWarnings (#3067).from_json keeps the fields that only later records have, as it does with pandas; before, it kept only the first record's fields (#3065, fixes #3063).TensorDict.from_parquet reads numeric fixed_shape_tensor columns without nulls with their row shape, such as [N, 2, 3]; before, it flattened them to [N, 6] (#3109, #3245, fixes #3101, fixes #3234).TensorDict.from_parquet and from_csv read files on pyarrow 10 to 12; before, they raised TypeError: ChunkedArray.to_numpy() takes no keyword arguments (#3248, fixes #3272).FutureWarnings of LazyStackedTensorDict.split, empty(), from_list, update(update_batch_size=True) and others show once per call site; before, once per new size or shape (#3282, #3308, fixes #3256).import tensordict, torch.export.load of a program with list inputs works, and pytree specs load lists as list; before, they loaded as tensordict's LinkedList (#3278, fixes #3255).set_printoptions(plain=True) prints a summary for jagged nested-tensor leaves, and one-element float leaves no longer make torch warn; before, jagged leaves raised NotImplementedError (#3283, fixes #3259).set_printoptions(plain=True) summaries of complex leaves keep the imaginary part, such as mean=1+1j; before, they described the real part only and warned (#3321, fixes #3286).tensordict.stack, cat and lazy_stack accept tensorclasses, and apply returns the tensorclass type, not an Optional, when it cannot return None (#3129).Self as tensordicts instead of Any (#3291, #3311, fixes #3290, fixes #3304).td.items(), td.values() and update() with nested keys or a dict[str, Tensor] type-check (#3251, fixes #3237).memmap, save and memmap_like accept a pathlib.Path; td == other is a tensordict, not a bool; unbind and split pieces are tensordicts (#3280, fixes #3268, fixes #3269, fixes #3270).save, dumps, memmap and memmap_like accept archive and compression (#3319, fixes #3314).save and memmap on TensorDictParams and PersistentTensorDict return TensorDict, and TensorDictParams.memmap_like accepts its keywords (#3322, fixes #3320).== on TensorDictParams and NonTensorStack is typed correctly, and NonTensorStack("a", "b"), default_interaction_type=InteractionType.RANDOM and a TypedTensorDict's batch_size type-check (#3315, fixes #3305).tensordict.nn: TensorDictSequential(m1, m2), ProbabilisticTensorDictSequential(m1, m2), a plain dict of modules, and assigning self.out_keys type-check. Type checkers reject positional options after the modules of ProbabilisticTensorDictSequential, which fail at runtime (#3251, #3310, #3341, fixes #3303, fixes #3332).TensorDictBase for type checkers, as at runtime, so passing one where a TensorDict is required is an error (#3172, fixes #1958).Each of these 12 calls warns, and gives the result described in the first column, until the release in the "Changes in" column.
DeprecationWarning)| What now warns | What to do | Changes in | PRs |
|---|---|---|---|
qint8(), qint32(), quint8() and quint4x2() on a tensordict or a tensorclass, which raise for any float entry |
Use td.apply(lambda x: torch.quantize_per_tensor(x, scale, zero_point, torch.qint8)), with the matching dtype |
0.18 (removed) | #3160, fixes #3150 |
FutureWarning)| What now warns | What to do | Changes in | PRs |
|---|---|---|---|
td[:, idx] with an index past the batch dims, which indexes the leaves as 0.14 did (0.15.0 to 0.15.2 raised IndexError), and writes through such an index |
Read with td.apply(lambda x: x[:, idx]); write with for leaf in td.values(True, True): leaf[:, idx] = value |
0.17 (raises IndexError) |
#3206, #3243, fixes #3232 |
td[0] = value on a tensordict or tensorclass without batch dims, which writes into the first row of every leaf |
Write for leaf in td.values(True, True): leaf[idx] = value, or td[...] = value for whole leaves |
0.17 (raises IndexError) |
#3225 |
update_ and update_at_ with keys_to_update that lists a sub-tensordict and a key under it, which write the whole sub-tensordict |
List only the sub-tensordict key to keep this result, or only the deeper keys for the 0.17 one | 0.17 (writes only the deeper keys, as select and update do) |
#3227 |
from_json without pandas on a number field with missing or null values, which gives a non-tensor entry holding None |
Install pandas to get a float tensor with NaN now | 0.17 (a float tensor with NaN) | #3086 |
A tensorclass field with a tensor default, such as s: torch.Tensor = torch.zeros(2), which every instance shares. It warns when the class is defined |
Use dataclasses.field(default_factory=lambda: torch.zeros(2)) |
0.17 (each instance gets a clone) | #3123 |
A Python number given to a tensorclass field annotated int, float or bool with an empty batch size, which becomes a 0-d tensor |
Annotate the field torch.Tensor to keep the tensor, or use the nocast class option to keep the number now |
0.17 (keeps the number as NonTensorData) |
#3146 |
A tensorclass call whose positional arguments fill fields in declaration order where the __init__ signature differs, because of keyword-only or init=False fields |
Pass these fields by keyword | 0.17 (positional arguments follow the signature, as in dataclasses) | #3148 |
LazyStackedTensorDict.where(cond, other) with a tensor other, where the result differs from TensorDict.where |
Add size-1 dims to other after its stack dim, or pass a tensordict |
0.17 (broadcasts like TensorDict.where) |
#3177 |
td.lerp(end, weight=w) with lazy stacks, where w does not broadcast with the batch size, which aligns w with each leaf from the right where a dense tensordict raises |
Pass a weight that lines up with the batch dims |
0.17 (raises, as a dense tensordict does) | #3191 |
consolidate(device=...) or consolidate(share_memory=True) on a consolidated tensordict, which ignores these arguments and returns it as is |
Call td.contiguous().consolidate(...) |
0.17 (honours them and returns a new consolidated tensordict) | #3169, #3196 |
from_arrow with a null inside a map, a list view or a dictionary-encoded column, which returns it as None |
Pass nulls="fill" to keep this result without the warning |
0.18 (raises ValueError, as for nulls inside lists) |
#3318 |
These changes can affect existing code:
print(td) and repr(td) give the compact form (#3143). Doctests and tests that compare reprs need set_printoptions(compact=False).set(), for example in a conftest.py.parse_tensor_dict_string(repr(td)) no longer parses keys that contain ": " (#3143).set_printoptions(html=False) gives the text back (#3106).show_grad or plain, passed without compact selects the detailed form, in print and in notebooks, as in 0.15.2 (#3254, #3300).plain=True, complex leaves print a complex mean, such as mean=1+1j, std=1.414, instead of the real part's statistics (#3321).plain=True, the min=/max= of a jagged tensor built with torch.nested.narrow no longer include the elements that the narrow hides (#3283).plain=True, printing a large jagged leaf copies it row by row: an integer leaf of 100,000 rows takes about 1.2 s, not 1 ms (#3283).meta.json files gain "_format_version": 1 and "_class", so tests that compare them with a fixture must drop these keys. Newer format versions raise RuntimeError (#3114).meta.json files gain "_names" and, for lazy stacks, "stack_dim_name"; the format version stays 1, and 0.15.2 loads these files without names (#3284).RuntimeError, as it would in memory (#3284).LazyStackedTensorDict.load_memmap(path, out=stack) returns a stack with the stack-dim name saved in the file (#3284).consolidate(filename=...) writes for a tensordict holding a tensorclass cannot be read by earlier versions; their from_consolidated raises KeyError (#3131).consolidate(filename=other) on a consolidated tensordict emits a UserWarning that it writes nothing (#3169).consolidate(filename=p) replaces a larger file, older results that map it keep their data, hardlinks keep the old file, and only its mode is copied (#3198).state_dict() also raises KeyError for colliding leaves that hold equal values, and for colliding empty levels whose batch sizes or devices differ. Two colliding empty levels with the same batch size and device give a state dict, as in 0.15.2. state_dict(flatten=False) keeps both (#3200, #3340).UnbatchedTensor leaves to h5, zarr or .tdz emits a UserWarning; the files and the loaded results are unchanged (#3285).import tensordict registers a default_collate entry, so a DataLoader with its default collate_fn stacks tensordicts and tensorclasses where it raised TypeError (#3180).default_collate batch that mixes tensordicts and tensorclasses with a tensor, dict, None or another tensorclass, or tensordicts that don't stack, such as tensordicts with different numbers of batch dims, raises TypeError, as 0.15.2 did (#3247, #3309, #3316, #3338, #3349).TensorDictBase, TensorClass or a tensorclass base still takes precedence over tensordict's collate, as in 0.15.2 (#3247).DataLoader workers, h5 and zarr PersistentTensorDict batches are in-memory TensorDicts: writes skip the file, and every key is read. num_workers=0 is unchanged (#3223).persistent_workers=True, workers keep their HDF5 file handle across epochs, so they may miss writes that the main process makes between epochs (#3223).to_zarr raises ImportError instead of TypeError (#3301).from_json raises as with pandas when a later record adds a field: TC.from_json for a class without that field, and conflicting keys with separator="." (#3065).from_parquet keeps the Arrow dtype of fixed_shape_tensor columns: float64 stays float64 instead of float32, int8 instead of int64, uint32 becomes torch.uint32 (#3109).td[:, idx] with an index past the batch dims, which raised IndexError in 0.15.0 to 0.15.2, indexes the leaves as 0.14 did, with a FutureWarning (#3206).IndexError instead of NotImplementedError, as on a dense tensordict (#3113).update(other, keys_to_update=[key]), where key names a sub-tensordict that the destination holds, writes that sub-tensordict's entries, as update(other.select(key)) does (#3212).td.update({"x": w}), where w is a tensorclass that wraps td, raises ValueError; before, it stored a cycle (#3170).RuntimeError, instead of KeyError (#3159).softmax of integer leaves along a dim after the stack dim raises NotImplementedError, not IndexError; along the stack dim, float dtypes are promoted (#3175).lazy.lerp(end, weight=w) with a weight that only aligns from the right keeps its result with a FutureWarning; from TensorDict 0.17 it raises, as dense does (#3191).lazy.where(..., pad=...) adds the other side's keys to whole selected members, so TorchRL resets with heterogeneous specs get keys such as action filled with 0 (#3194).RuntimeError or IndexError, as by position, instead of KeyError (#3195).densify(layout=torch.strided), the empty entry for a member that lacks a key keeps its feature dims: (0, 3) instead of (2, 0). Padded results are unchanged (#3220).tensor_only error message says "Tensor-subtypes" instead of "Tensor-subtrypes"; code that matches the old text must change (#3107).copy.replace(tc, names=...) sets the names through the names setter, so names that do not fit the batch dims raise ValueError or warn (#3120).init=False or compare=False, or kw_only=True without the class option, warns with a UserWarning when defined (#3126).TC.field, read on the tensorclass, returns the field getter instead of raising AttributeError, and LinkedList(iterable) builds an unlinked list (#3183).tree_map(lambda t: t[:2], tc) gives the str fields of a tensorclass the result's batch size, as for a TensorDict; before, they kept the old one (#3189).LinkedList saves under "tensordict.utils.LinkedList", which older tensordict versions cannot load; specs saved by older versions load those nodes as list (#3278).backward() inside with td.to_module(model):, td["weight"].grad is None for plain tensors; make the leaves require grad, or Parameters, to get gradients (#3122).with td.to_module(model): blocks is about 2.3 times slower; preserve_module_state=False keeps the old speed (#3122).TensorDictParams.map(...) and memmap_(...) with TensorDict's keywords, such as out= or existsok=, raise the RuntimeError they raise without them, instead of TypeError (#3325).to(device, non_blocking_pin=True) with the default num_threads=None pins on the calling thread and starts no threads; the results are unchanged (#3228).FutureWarnings no longer hold sizes, shapes or batch sizes; tests that match those values with pytest.warns(match=...) need updating (#3282, #3308).append_memmap can leave the saved directory unloadable; follow the recovery step of the saving guide, and do not read during an append (#3249).torch.load(weights_only=True) (pytorch/pytorch#194380). Opt in with torch.serialization.safe_globals.torch.compile, with td.to_module(model): still replaces the tensors of td with the module's parameters (#3115).DataLoader workers still send lazy views, read in the main process, for sampler=BatchSampler(...) with batch_size=None and for lazy stacks of h5 or zarr files (#3218).pad_sequence(..., return_mask=True) does not raise when its mask key is already an entry of the samples (#3313).nulls="raise", from_arrow and from_parquet return nulls inside unions and run-end encoded columns as None, without a warning (#3312).UnbatchedTensor leaves load back from h5, zarr and .tdz files as plain tensors, now with a warning at save time (#3264).memmap and save of a TypedTensorDict to a .tdz archive return a plain TensorDict; a directory path returns the class (#3324).torch.stack and torch.cat of tensordicts as Tensor; use tensordict.stack and tensordict.cat, which are typed (#3271).transformers.Trainer, DDP and torch.export (#3110, #3111, #3138, #3112, #3185, fixes #3080, fixes #3081, fixes #3082, fixes #3084, fixes #3166).TensorClass: a batched step, torch.compile, and a trajectory saved with memmap (#3186, fixes #3168).append_memmap: how to recover after a killed append, using the temporary file left in the top directory, and why not to read during an append (#3252, #3299, #3239).DataLoader workers can't open a file held open for writing, so open it with mode="r"; persistent workers may miss later writes (#3253, #3239, fixes #3250).from_pandas, from_csv, from_json) is one value for the batch; the docs show how to get one per row (#3125).to_namedtuple(), nested tensorclasses in to_pandas and a locked store's view return (#3171, fixes #2363, fixes #2404, fixes #3007).TensorClass right after the first TensorDict example, and the README lists more science projects that use tensordict (#3179, #3224, fixes #3167).pip install tensordict==0.15.3
Thanks to @akshitkhokhani (first contribution), @bashfullad78 (first contribution), @mottopanikeiku, and @peterdsharpe for their contributions to this release.
Full Changelog: https://github.com/pytorch/tensordict/compare/v0.15.2...v0.15.3
Removals ( DeprecationWarning )
TensorDict 0.15.2 supports torch 2.11 and 2.12 again, fixes 54 bugs, adds 8 features, and speeds up the calls that TorchRL, rsl_rl and verl make every step. 24 calls now warn ahead of changes in TensorDict 0.17, and tensordict no longer requires packaging or orjson. Several fixes change behavior that was previously wrong; see "Behavior changes" below.
torch.compile works on torch 2.11 and 2.12 as on 2.13. state_dict() breaks the graph there, as on 2.14 (#2934, #2935).tensor_only tensorclass whose custom __init__ calls object.__setattr__ raises inside a compiled function; build it outside (#2934, #2935).pip install tensordict no longer installs packaging, nor orjson on Python 3.10 to 3.12. Code that got either only through tensordict must install it (#2915).json module, also where orjson is installed. tensordict.utils.set_json_backend("orjson") still selects orjson (#2914, #2915).set_printoptions(compact=True) prints each entry as its dtype and feature shape, such as fields={image: f32[3,64,64], label: i64[]}, without reading data (#2822, fixes #2687).td.rename_keys({"a": "b", "b": "a"}) renames several keys at once, swaps and chains included, into a new tensordict that shares the leaves. On a TensorDictParams it returns a TensorDict, as select does, so that it compiles (#2824, #2849, fixes #2685).unpad_sequence(padded) turns the output of pad_sequence(..., return_mask=True) back into a lazy stack of samples, and pad_sequence(..., padding_side="left") pads on the left (#2826, fixes #2808).to() and the dtype shorthands take cast_filter: td.bfloat16(cast_filter=tensordict.is_inexact) keeps integer and boolean leaves. Tensorclasses can declare a default cast_filter (#2828, fixes #2683).tensordict.KEY_SEPARATOR is the default separator of flatten_keys and unflatten_keys, for code that joins or splits nested keys the same way (#2844, fixes #2686).DataLoader(samples, batch_size=32, collate_fn=tensordict.collate) batches tensordicts, tensorclasses, (td, label) tuples and samples of different shapes (#2835, fixes #2809).tensordict.TensorCollection, tensordict.nn.interaction_type() and tensordict.unravel_key_to_tuple() can be imported from public modules; before, they were only in private ones (#2843, fixes #2810).split_keys takes a callable wherever it takes a key list: td.split_keys(lambda key, value: value.is_floating_point()) splits the floating-point leaves from the rest (#2821, fixes #2684).Against 0.15.1, on one core of an NVIDIA Grace CPU with torch 2.14.1, the 177 microbenchmarks of the 0.15.0 notes run 1.08 times as fast on average (geometric mean), and the 433 tests of the benchmark suite 1.05 times.
The numbers below are from the PR descriptions. Unless a bullet names another machine, they are CPU timings with torch 2.14.1 on one pinned core of an AWS m8a.4xlarge (AMD EPYC 9R45), against main just before each PR. The CUDA timings are from an AWS g6.2xlarge (NVIDIA L4).
torch.stack of plain TensorDicts, as in TorchRL's rollout and LeanRL's PPO, is 9-23% faster: LeanRL's 24-step stack takes 143.6 instead of 162.2 us (#2876, fixes #2860).to() of a tensordict without a device is 25-48% faster, and 13-36% on CUDA: rsl_rl's per-step obs.to(device) takes 3.95 instead of 5.29 us (#2877, fixes #2857).copy_ and update_ between tensordicts of the same structure are 29-65% faster: rsl_rl's step write storage[i].copy_(obs) takes 5.65 instead of 7.97 us (#2880, fixes #2859).update_(..., keys_to_update=...), which TorchRL's batched environments call every step, is 30-62% faster: SerialEnv(4).step() takes 220.1 instead of 242.0 us (#2887, fixes #2870).TensorDictModule calls are 8-17% faster and TensorDictSequential calls 9% faster: a TorchRL policy of batch 16 takes 20.6 instead of 22.0 us (#2878, fixes #2865)..data is 26-49% faster and lock_() with unlock_() 11-20% faster, so TorchRL's SoftUpdate.step() for SAC takes 41.3 instead of 52.1 us (#2882, fixes #2866).td["next", "obs"] are 18% faster, 0.37 instead of 0.45 us, and TorchRL's step bookkeeping is 10% faster (#2888, fixes #2871).pad of a TensorDict is 15-20% faster, so TorchRL's split_trajectories of a 512-step batch takes 744 instead of 836 us (#2875, fixes #2861).apply and clone are 3-16% faster, most back to 0.14.3 speed; reshape and td[:] stay 3% and about 10% slower than 0.14.3 (#2884, fixes #2874).consolidate() of 8 chunks of 32 rows takes 13.0 instead of 19.3 ms (#2879, fixes #2862).NonTensorStack column with an index tensor, list or mask is 10-48% faster: verl's index_select_tensor_dict takes 1.24 instead of 1.41 ms (#2885, fixes #2872).non_tensor_as_stack=True, reading 256 of 100,000 rows of an h5 string column takes 1.1 ms instead of 570-590 ms (24-core x86 machine) (#2833, fixes #2814).TensorDict.from_namedtuple takes 1.46 instead of 1.57 us, undoing the slowdown that torch 2.11 support brought on torch 2.13 and later (workstation, torch 2.14.1) (#2946).td.to(device, dtype, False) and the other calls with a positional non_blocking=False copy blocking, as the keyword form does; before, the positional value was ignored (#3004).TensorDict.from_dataclass(T), from_any(T) and from_dict with a dataclass type whose fields lack class-level defaults raise TypeError asking for an instance; before, they raised AttributeError (#3003).split_keys on a lazy stack takes an entry that only some members hold, and puts it in that key set's part; before, it raised KeyError (#2851).td.replace(entries, **kwargs) leaves entries unchanged; before, it wrote kwargs into the caller's dict, tensordict or tensorclass, and raised when that was locked (#2985, fixes #2949).create_nested(("a", "b")) keeps the entries under "a" when it is a plain TensorDict; before, it emptied "a", which dropped actions in TorchRL's RemoveEmptySpecs (#2987, fixes #2950).get_item_shape(key) with a missing key raises the KeyError that td[key] raises, on every container; before, it raised AttributeError about NoneType (#2989, fixes #2955).except RuntimeError catches a failed set_, set(..., inplace=True) or update(..., inplace=True), as it does for update_; before, only except ValueError caught it (#3017).pad_sequence(..., return_mask=True) pads each mask along its own dim, so entries like [batch, heads, tokens, head_dim] padded along tokens get masks; before, it raised RuntimeError (#2937, fixes #2936).pad_dim, such as -2, on a batch dim gives the result of the positive index; before, it raised IndexError or RuntimeError (#2945, fixes #2938).return_mask=True, pad_sequence pads each mask once instead of once per sample: 400 samples take 4.6 ms instead of 472.1 ms (#2945, fixes #2939).rename(None) and rename_(None) clear the dim names for any batch size and on lazy stacks; before, they raised ValueError unless there was one batch dim (#2995, fixes #2956).td.sum(keepdim=True) and td.mean(keepdim=True) without a dim reduce over every batch dim, as nansum and the other reductions do; before, they raised TypeError (#3000).td.clamp(..., out=out) writes the result into out and returns it; before, it raised TypeError, or ValueError with a None bound (#3002, fixes #2961).maximum_, minimum_, clamp_min_ and clamp_max_ take a batch-shaped tensor, as maximum, minimum, clamp_min and clamp_max do; before, they raised TypeError (#3002, fixes #2962).lerp, addcmul, addcdiv and their in-place forms take one tensordict operand and one batch-shaped tensor, in either order; before, they raised TypeError (#3002, fixes #2963).lazy.transpose(i, j) with the stack dim and a non-adjacent dim gives the dense batch size; before, it misordered the dims or raised ValueError (#2931).lazy.apply(fn, out=out) and named_apply, with out stacked along another dim, fill the matching entries of out; before, they transposed the values or raised (#2997, fixes #2982).td.add(w), give dense results when td holds a lazy stack; before, they raised or gave wrong values (#3041, fixes #3005).lazy.clamp(min=lo, max=hi), and bitwise_and, logical_and and mod with keyword tensors, give the dense result on a lazy stack; before, they gave wrong values or raised (#3060).add_, clamp_min_ and the other in-place ops through sub-tensordict views, as in TorchRL's replay buffers, reach nested lazy stacks; before, those writes were lost (#3046, #3050, fixes #3043, fixes #3048).NonTensorData.tolist(as_linked_list=True) leaves the list unchanged; before, the list kept the new item, and the error was generic (#2998, fixes #2978).CompositeDistribution, custom log_prob_keys hold the log-probabilities in forward, log_prob and a sequence's log_prob; before, they were missing, stale or swapped (#2905, #2926, fixes #2902, fixes #2912).CompositeDistribution; before, it raised TypeError or RuntimeError (#2926, fixes #2910).log_prob_key equal to a sample key, or inside one, raises ValueError in the constructor; before, the log-probability silently overwrote the sample (#2930, fixes #2911).select_subsequence works on a TensorDictSequential built from a dict that holds a nested sequence; before, it raised ValueError from zip() (#2906, fixes #2903).seq["name"] = module and del seq["name"] work on a TensorDictSequential built from a dict; before, both raised TypeError about an unexpected idx argument (#2907, fixes #2904).TensorDictSequential keep its partial_tolerant and inplace options; before, they could raise KeyError or write into their input (#2925).TensorDictModule returning a dict or TensorDict writes each value at its own key after select_out_keys; before, a subset raised and a reordering swapped values (#2941, fixes #2940).TensorDictModuleWrapper runs the wrapped module's forward hooks once, and accepts a module narrowed with select_out_keys; before, hooks ran twice, and the latter raised TypeError (#2944, fixes #2942).as_td_module() on a TensorClassModuleBase subclass without type arguments raises a ValueError that says how to set them; before, it raised AttributeError or TypeError (#2994, fixes #2977).log_prob_key on a ProbabilisticTensorDictSequential with one log-probability key returns that key; before, it raised AttributeError about a ModuleList (#2996, fixes #2975).get_dist_params, and with return_composite=False also forward, get_dist and log_prob, pass keyword arguments to the modules of a ProbabilisticTensorDictSequential; before, they raised RuntimeError or TypeError (#3036, fixes #2976).CompositeDistribution.event_shape is torch.Size([]), so torch.distributions.Independent can wrap a composite; before, reading it raised AttributeError (#2993, fixes #2974).TensorDictModule.select_out_keys with missing keys raises an error that is both ValueError and RuntimeError, naming the keys; before, the type depended on the key count (#3000).as_td_module() returns starts in its module's eval or training mode; before, it was always in training mode, and torch.onnx.export warned (#3024).CudaGraphModule(module, in_keys=..., out_keys=...) warns when it ignores keys that differ from the module's own; before, it dropped them silently (#3027, fixes #2973).td.get(key, None) compiles again when key appears in a later input; before, it kept returning the default, with no error and no recompile (#2986, fixes #2980).dynamic=True, lazy[:, a:] on a lazy stack compiles once for every bound a; before, each new bound compiled a new graph (#2900, fixes #2896).save, memmap or consolidate(filename=...); before, they loaded as None (#2914, fixes #2913).MemoryMappedTensor(filename, dtype=..., shape=...) maps the file, as from_filename does, and every constructor call takes index=; before, both raised TypeError (#2992, fixes #2970).load_memmap of a saved legacy lazy view raises ValueError unless its saved operation is a shape op; before, it ran whatever method the file named (#2918).num_threads above 1; before, parallel decompression skipped these checks (#2922).close(), raise; before, they were dropped (#2981, fixes #2964).PersistentTensorDict pickles after a nested view has been read; before, pickle.dumps raised TypeError: cannot pickle 'weakref.ReferenceType' object (#2924, fixes #2920).from_zarr, to_zarr and PersistentTensorDict take allow_pickle. False rejects pickled non-tensor fields; the default reads them with a FutureWarning, and becomes False in 0.17 (#2921).add_(1) or mul_(tensor), write into an h5 file that the tensordict holds; before, the file kept its values (#3046, #3051, fixes #3049).clone or from_store, connect with its TLS settings; before, they connected without TLS (#2917).from_store raises TypeError when the class it gets, or the class path stored with the data, is not a tensorclass; before, it failed during reconstruction (#2919).td.reduce(dst, group=g) reduces nested entries on g; before, they went to the default group, so it hung or timed out when ranks were outside g (#2990, fixes #2965).recv_tensordict(endpoint, td=dest) writes into dest when its layout, size and device match, and returns a new tensordict otherwise; before, it wrote into any consolidated dest (#2923).consolidated=False, a tensordict sent through UCXX arrives with its nested entries, batch size and JSON-friendly non-tensor entries; before, only the root tensors arrived (#2999, #3034, fixes #2966).recv_tensordict(..., consolidated=False) reads what send_tensordict(..., consolidated=False) sends; before, recv_tensordict read only the consolidated format and raised ValueError (#3034).td.reduce(dst) leaves the reduced values in lazy stacks, at the root or nested, on the destination rank; before, that rank kept its own values (#3039, fixes #3006).to_parquet writes with pyarrow alone, which is what the parquet extra installs; before, it raised ModuleNotFoundError: No module named 'pandas' without pandas (#2991, fixes #2969).to_pandas, to_csv and to_parquet with a separator write one column per leaf of a nested @tensorclass; before, to_parquet raised ArrowInvalid and to_csv wrote its repr (#3038).map(fn, out=out) returns out when it is in shared memory or memory-mapped; before, it returned None, although it filled out (#2988, fixes #2953).assert_close adds torch's description of the mismatch, such as a dtype, to its message; before, it said only got mse = 0.0000 (#3001).inspect.signature of bfloat16, int32 and the other dtype shorthands lists their keyword-only cast_filter; before, IDEs could not show it (#3001).Each of these 24 calls warns and keeps its 0.15.1 result until TensorDict 0.17.
DeprecationWarning)| What now warns | What to do | Changes in | PRs |
|---|---|---|---|
tc[idx] = td with a TensorDict that holds keys that are not fields of the tensorclass |
Write only fields. These writes will raise, as set() does |
0.17 (raises) | #2767, fixes #2756 |
tensordict.utils.implement_for |
Use pyvers.implement_for |
0.17 (removed) | #2916 |
FutureWarning)| What now warns | What to do | Changes in | PRs |
|---|---|---|---|
empty(batch_size=..., names=...) without recurse=True, with names that do not fit the batch size: another length, a name used twice, or names on a 0-d batch |
Pass one name per batch dim, with no name used twice | 0.17 (applies the names as the names setter does, which raises or drops them) | #2746 |
is_empty() on a lazy stack that returns True while its members hold leaves |
Check code that drops what is_empty() reports as empty |
0.17 (True only if every member is empty) |
#2823 |
A tensorclass's get(key, as_list=True) or get(key, as_nested_tensor=True), which returns the stored tensor |
Drop the keyword to keep the tensor | 0.17 (returns a list or a nested tensor, as a lazy stack does) | #2831 |
copy_at_(source, idx) with the default fast=True, which skips the non-tensor entries of a source that also holds tensors |
Use fast=False or update_at_ to write the non-tensor entries |
0.17 (raises RuntimeError) |
Note truncated.
…and torch.compile. There are no new features or deprecations. Several fixes change behavior that was previously wrong; see "Behavior changes" below.
TensorDict 0.15.1 is a patch release with 46 bug fixes. They cover update_, copy_ and in-place math on lazy stacks, h5 files and TypedTensorDict, UnbatchedTensor and non-tensor data, NumPy conversion, tensordict.nn, and torch.compile. There are no new features or deprecations. Several fixes change behavior that was previously wrong; see "Behavior changes" below.
td.new_tensor(data) takes a list, tuple, NumPy array or scalar, as torch.Tensor.new_tensor does; before, it raised AttributeError (#2748, fixes #2747).TensorDict(...) or set becomes a NonTensorStack, as with from_dict; before, it raised AttributeError (#2751).ValueError (#2853, #2855, fixes #2852, fixes #2854).td.update({"x": td}), and p["x"] = p on a TensorDictParams, raise ValueError; before, they left a tensordict that holds itself, or raised RecursionError (#2764).update_ and copy_; before, backward() through the written leaves raised, and compiled code with these calls failed to compile (#2881, fixes #2863, fixes #2886).copy_, update_ (also with keys_to_update) and in-place math write the h5 files and lazy stacks that a TensorDict holds; before, these silently kept their values (#2777, #2838, fixes #2775, fixes #2780).split_keys(..., reproduce_struct=True) puts each non-tensor entry, and each entry that only some lazy-stack members hold, in one part; before, every part got a copy (#2802, #2841, fixes #2782, fixes #2803).torch.where(mask, 0, td) and torch.where(mask, tensor, td) work on tensordicts and tensorclasses; before, they raised AttributeError and TypeError (#2762, fixes #2743).named_apply(fn, out=buf) writes into buf and returns it, as apply does; before, it returned a new tensordict and left buf unchanged (#2846, fixes #2845).TypedTensorDict over one stacks no tensors; before, it stacked every entry twice, which can exhaust GPU memory (#2799, #2829, fixes #2783, fixes #2800).filter_empty_() and split_keys() work on a lazy stack whose members hold empty sub-tensordicts; before, they raised KeyError (#2806, fixes #2796).update_ or copy_, nothing is written; before, the earlier members were already written (#2837, fixes #2776).update_ and copy_ broadcast a 0-d or size-1 value into a lazy stack inside a TensorDict; before, the value was silently dropped (#2850, fixes #2834).empty(recurse=True) drops the tensors of a lazy stack inside a tensordict, as at the root; before, member-only tensors kept their values (#2847, fixes #2840).flatten_keys() on a lazy stack keeps each member's values and shares their tensors; before, every member got the first member's UnbatchedTensor, and leaves were copies (#2890, fixes #2869).NonTensorData.update_at_ with a NumPy array index raises the RuntimeError that a list or tensor index raises; before, it raised a confusing ValueError (#2727).True or None index works on a tensordict without batch dims; before, it raised IndexError (#2750).set of an UnbatchedTensor into a lazy stack stores it in every member, and flatten_keys() and unflatten_keys() work on such stacks; before, these raised (#2867, fixes #2864).update_ and copy_ of an UnbatchedTensor in a lazy stack work along any stack dim; before, a stack dim other than 0 raised ValueError (#2873, fixes #2868).get_at takes default= and passes other keywords, such as as_list=True, to get, as TensorDict.get_at does; before, it raised KeyError: 'default' (#2832, fixes #2830).TypedTensorDict over lazy stacks whose members hold different keys works; before, it raised or wrote entries it should not (#2760, #2769, #2839, fixes #2739, fixes #2759).dense.copy_(src) and dense.update_(src) write the nested entries of a TypedTensorDict over a lazy stack; before, those entries kept their old values (#2770, fixes #2768).t + 1, copy_ and update_ work on a TypedTensorDict over a lazy stack, whatever its members; before, they raised or skipped nested entries (#2774, fixes #2771, fixes #2772, fixes #2773).TypedTensorDict over a lazy stack, such as t[1:].add_(1), reach the members; before, flat entries went to a copy (#2778).ProbabilisticTensorDictSequential whose last module is another probabilistic sequence runs; before, it raised a RuntimeError about keyword arguments (#2752).set_at_ and update_at_ on a TensorDictParams write its strings and other non-tensor entries; before, those entries kept their old values (#2763, fixes #2740).TensorDictSequential(partial_tolerant=True), a module that returns a new tensordict instead of writing in place keeps its outputs; before, they were silently dropped (#2766, fixes #2757).masked_fill on a lazy stack of TensorDictParams returns filled tensordicts and leaves the parameters untouched; before, it raised a leaf-variable RuntimeError (#2795, fixes #2779).AddStateIndependentNormalScale() with its default 0-dim scale accepts a loc with dims; before, it raised RuntimeError for any such loc (#2805, fixes #2804).TensorDictSequential built from a dict runs at both positions; before, it ran only at the first (#2894, fixes #2893).td.new_tensor(data) keeps the dtype of each entry, as in eager mode; before, the entries silently took the dtype of data (#2753, fixes #2744).torch.gather on a TensorDictParams returns a TensorDict with the gathered batch size; before, its batch size was [], and fullgraph=True raised (#2761, fixes #2742).td[mask], td.masked_select(mask) and lazy-stack td[mask] = 1 compile with fullgraph=True, and eager td[0] = 1 works on a tensordict holding MetaData; before, all raised (#2815, fixes #2785, fixes #2794).update(), clone(recurse=False) and the shape ops of a TensorDictParams compile with fullgraph=True; before, Dynamo raised Unsupported (#2817).detach_() compiles with fullgraph=True on a tensordict whose leaves do not require grad; before, it raised Unsupported (#2818).unbind, iteration, chunk and split of a memory-mapped tensordict, and chunk and split of a shared one, compile with fullgraph=True; before, they raised (#2820, fixes #2788).chunk, split, torch.cat, torch.stack and update give UnbatchedTensor entries the eager batch size; before, dynamic shapes raised, and static shapes kept the input's (#2819, fixes #2789).permute and get(key, None) on a lazy stack compile with fullgraph=True, as does add_ on a TypedTensorDict over one on torch nightly; before, they raised (#2816, fixes #2786, fixes #2787).key in td compiles once for every order of the keys; before, each new key order compiled a new graph, up to the recompile limit (#2895, fixes #2892).dynamic=True, td[:, a:] compiles once for every bound a and batch size; before, each new bound or batch size compiled a new graph (#2897, fixes #2889).to_mds writes float scalar entries and tensordicts without batch dims; before, the first raised TypeError, and the second raised or left an empty directory (#2765, fixes #2754, fixes #2755).memmap, consolidate or memmap_like with num_threads > 0 (#2801, fixes #2798).h5[1:3] = value on a locked PersistentTensorDict with a nested group writes every entry; before, it wrote some entries, then raised (#2758, fixes #2745).memmap() on an h5 file that holds an enum or a non-tensor list stores those entries as NonTensorData; before, it raised AttributeError: is_nested (#2797, fixes #2781).unlock_() fails because a parent is locked; before, it lost them (#2749).These fixes can change the behavior of existing code:
NonTensorStack, not as the raw list (#2751).ProbabilisticTensorDictSequential, a probabilistic module resamples a sample that the input already holds, as a standalone module does, unless set_skip_existing(True) is set (#2752).td.update({"x": td}) raises ValueError. On a TensorDictParams, p["x"] = p, p.set("x", p) and p.update({"x": p}) raise ValueError instead of RecursionError (#2764).unlock_(), memmap_() on a shared child and share_memory_() on a memory-mapped child raise "memmap and shared memory are mutually exclusive features" (#2749).to_mds with an invalid writer keyword on a tensordict without batch dims raises ValueError; before, the keyword was dropped and nothing was written (#2765).partial_tolerant TensorDictSequential, a module that returns a copy raises on a locked stack, as does an output whose leading dims do not fit (#2766).update_ and copy_ from a source whose nested lazy stack has leaves of different shapes raise the stacking RuntimeError before writing (#2770).TypedTensorDict over members of different shapes, zero_() and mul_ leave member-only keys unchanged, and with h5 nested entries, zero_(), mul_ and add_ raise (#2774).update_ and copy_ on a TensorDict that holds a read-only h5 file warn that the file is left unchanged (#2777).TypedTensorDict over a lazy stack returns views of the members, locked if the stack is; an int index returns the member itself (#2778).masked_fill on a lazy stack of h5 or store members returns a lazy stack of in-memory TensorDicts (#2795).filter_empty_() keeps a lazy stack held by a tensordict when one of its members still has a leaf; before, it deleted the stack (#2806).split_keys(..., reproduce_struct=True) puts each non-tensor entry in one part only. A str field of a tensorclass is None in the other parts (#2802, #2841).stack_dim of the result of permute, movedim and transpose on a lazy stack is an int, not a numpy.int64 (#2816).torch.compile, UnbatchedTensor entries of shape-op results get the eager batch size, where they kept the input's (#2819).torch.compile, unbind, iteration, chunk and split of a memory-mapped tensordict return plain Tensor leaves, not MemoryMappedTensor leaves, over the same storage (#2820).update_ or copy_, no member is written (#2837).update_ into a TypedTensorDict over a lazy stack inside a TensorDict raises for a source that does not fit; before, it did nothing (#2838).TypedTensorDict over a lazy stack whose members hold lazy stacks, in-place math leaves their member-only keys unchanged; an operand that broadcasts self raises (#2839).empty(recurse=True) drops the tensors that only some members of a nested lazy stack hold (#2847).update_ and copy_ raise RuntimeError for a source entry that cannot broadcast into a nested lazy stack, and write UnbatchedTensor leaves into every member (#2850).set of an UnbatchedTensor whose size along the stack dim equals the number of members stores the whole tensor in each member, not one row (#2867).update_ and copy_, a destination leaf whose source does not require grad no longer requires grad (#2881).lazy.flatten_keys() share the members' storage, and is_leaf sees the members' sub-tensordicts instead of lazy stacks of them (#2890).TensorDictSequential holds under two names runs at both positions, which changes the outputs and gradients (#2894).pip install tensordict==0.15.1
Thanks to @jackiectl2 (first contribution), @mottopanikeiku (first contribution), @peterdsharpe, and @YeonwooSung for their contributions to this release.
Full Changelog: https://github.com/pytorch/tensordict/compare/v0.15.0...v0.15.1
One column per quarter.