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 non-deterministic key order in stack #1230

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion tensordict/_torch_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,6 @@ def stack_fn(key, values, is_not_init, is_tensor):
key: stack_fn(key, values, is_not_init, is_tensor)
for key, (values, is_not_init, is_tensor) in out.items()
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this added space is on purpose.

result = clz._new_unsafe(
out,
batch_size=LazyStackedTensorDict._compute_batch_size(
Expand Down
14 changes: 8 additions & 6 deletions tensordict/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,7 @@ def _check_keys(
strict: bool = False,
include_nested: bool = False,
leaves_only: bool = False,
) -> set[str]:
) -> set[str] | list[str]:
from tensordict.base import _is_leaf_nontensor

if not len(list_of_tensordicts):
Expand All @@ -1769,27 +1769,29 @@ def _check_keys(
)
# TODO: compile doesn't like set() over an arbitrary object
if is_compiling():
keys = {k for k in keys} # noqa: C416
keys_set = {k for k in keys} # noqa: C416
else:
keys: set[str] = set(keys)
keys_set: set[str] = set(keys)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, is it much more efficient using set rather than using always the other option? Or there is another reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torch.compile used to not understand set() that's all. I should check if it's still the case

for td in list_of_tensordicts[1:]:
k = td.keys(
include_nested=include_nested,
leaves_only=leaves_only,
is_leaf=_is_leaf_nontensor,
)
if not strict:
keys = keys.intersection(k)
keys_set = keys_set.intersection(k)
else:
if is_compiling():
k = {v for v in k} # noqa: C416
else:
k = set(k)
if k != keys:
if k != keys_set:
raise KeyError(
f"got keys {keys} and {set(td.keys())} which are incompatible"
)
return keys
if strict:
return keys
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should actually make it a list

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return keys
return list(keys)

pretty sure that's what you mean with your comment, but just to be on the safe side. Rn, the return type is not consistent with typing.

return keys_set
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If keys can be exclusive, their order becomes arbitrary

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By curiosity, what are the downstream functions that would be impacted by this? In other words, in which context is _check_keys(strict=False) used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes when using lazy stacks iirc



def _set_max_batch_size(source: T, batch_dims=None):
Expand Down
14 changes: 14 additions & 0 deletions test/test_tensorclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,20 @@ class MyDataNested:
):
torch.stack([data1, data3], dim=0)

def test_stack_keyorder(self):

class MyTensorClass(TensorClass):
foo: Tensor
bar: Tensor

tc1 = MyTensorClass(foo=torch.zeros((1,)), bar=torch.ones((1,)))

for _ in range(10000):
assert list(torch.stack([tc1, tc1], dim=0)._tensordict.keys()) == [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert list(torch.stack([tc1, tc1], dim=0)._tensordict.keys()) == [
assert list(torch.stack([tc1, tc1], dim=0).keys()) == [

Copy link
Contributor Author

@vmoens vmoens Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was on purpose to avoid any artifacts caused by @tensorclass (if there had been any)

"foo",
"bar",
]

def test_statedict_errors(self):
@tensorclass
class MyClass:
Expand Down
Loading