Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Fix update_ KeyError when a key from source is missing in dest #1150

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions tensordict/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6544,12 +6544,16 @@ def update_(

named = True

def inplace_update(name, dest, source):
def inplace_update(name, source, dest):
if source is None:
return None
name = _unravel_key_to_tuple(name)
for key in keys_to_update:
if key == name[: len(key)]:
if dest is None:
raise KeyError(
f"The key {name} was not found in the dest tensordict."
)
dest.copy_(source, non_blocking=non_blocking)

else:
Expand All @@ -6564,16 +6568,20 @@ def inplace_update(name, dest, source):
vals = [vals[k] for k in new_keys]
_foreach_copy_(vals, other_val, non_blocking=non_blocking)
return self
named = False
named = True

def inplace_update(dest, source):
def inplace_update(name, source, dest):
if source is None:
return None
if dest is None:
raise KeyError(
f"The key {name} was not found in the dest tensordict."
)
dest.copy_(source, non_blocking=non_blocking)

self._apply_nest(
input_dict_or_td._apply_nest(
inplace_update,
input_dict_or_td,
self,
nested_keys=True,
default=None,
filter_empty=True,
Expand Down
22 changes: 22 additions & 0 deletions test/test_tensordict.py
Original file line number Diff line number Diff line change
Expand Up @@ -3033,6 +3033,28 @@ def make(val, todict=False, stack=False):
assert (td1.select(("a", "b")) == 2).all()
assert (td1.exclude(("a", "b")) == 1).all()

# Any extra key in dest will raise an exception
with pytest.raises(KeyError):
td_dest = TensorDict(a=0)
td_source = TensorDict(b=1)
td_dest.update_(td_source)
with pytest.raises(KeyError):
td_dest = TensorDict(a=0)
td_source = {"b": torch.ones(())}
td_dest.update_(td_source)
with pytest.raises(KeyError):
td_dest = TensorDict(a=0)
td_source = TensorDict(b=1)
td_dest.update_(td_source, keys_to_update="b")
with pytest.raises(KeyError):
td_dest = TensorDict(a=0)
td_source = {"b": torch.ones(())}
td_dest.update_(td_source, keys_to_update="b")

td_dest = TensorDict(a=0, b=1)
td_source = TensorDict(a=0)
td_dest.update_(td_source)

def test_update_nested_dict(self):
t = TensorDict({"a": {"d": [[[0]] * 3] * 2}}, [2, 3])
assert ("a", "d") in t.keys(include_nested=True)
Expand Down
Loading