You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] TypedTensorDict reshape, masked_select and repeat_interleave with inplace=True return a new object instead of self #2494
inplace=True promises to keep the object identity, and code that relies on it, such as assert td.reshape(6, inplace=True) is td or holding a reference across the call, breaks for TypedTensorDict. The TypedTensorDict is reshaped in place, but reshape, masked_select and repeat_interleave return a second wrapper around the same data. flatten, repeat, gather and pad return self as expected.
Expected: True for every line, as on a dense TensorDict, where td.reshape(6, inplace=True) is td holds.
Cause: reshape, masked_select and repeat_interleave are in _WRAP_DELEGATES (tensordict/typedtensordict.py:1015-1030). Their delegate (_make_delegate_wrap, tensordict/typedtensordict.py:277-288) calls the method on self._source, which changes the source in place and returns it, and then wraps that result in a new instance with type(self)._wrap_td(result). The methods that return self are not overridden, so they run the shared implementation on the TypedTensorDict itself.
Possible fix: in _make_delegate_wrap, return self when result is self._source. Only these in-place calls change; the out-of-place calls still return a new wrapper.
tensordict main (eba7879), torch 2.14.1, Python 3.12.12.
Activity