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

[BE] Check ordering and exclusivity of tensorclass registers #1176

Merged
merged 3 commits into from
Jan 9, 2025
Merged
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
29 changes: 28 additions & 1 deletion test/test_tensorclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ def test_tensorclass_stub_methods():
]

if missing_methods:
raise Exception(f"Missing methods in tensorclass.pyi: {missing_methods}")
raise Exception(
f"Missing methods in tensorclass.pyi: {sorted(missing_methods)}"
)
Expand Down Expand Up @@ -156,6 +155,34 @@ class X:
)


def test_sorted_methods():
from tensordict.tensorclass import (
_FALLBACK_METHOD_FROM_TD,
_FALLBACK_METHOD_FROM_TD_FORCE,
_FALLBACK_METHOD_FROM_TD_NOWRAP,
_METHOD_FROM_TD,
)

lists_to_check = [
_FALLBACK_METHOD_FROM_TD_NOWRAP,
_METHOD_FROM_TD,
_FALLBACK_METHOD_FROM_TD_FORCE,
_FALLBACK_METHOD_FROM_TD,
]
# Check that each list is sorted and has unique elements
for lst in lists_to_check:
assert lst == sorted(lst), f"List {lst} is not sorted"
assert len(lst) == len(set(lst)), f"List {lst} has duplicate elements"
# Check that no two lists share any elements
for i, lst1 in enumerate(lists_to_check):
for j, lst2 in enumerate(lists_to_check):
if i != j:
shared_elements = set(lst1) & set(lst2)
assert (
not shared_elements
), f"Lists {lst1} and {lst2} share elements: {shared_elements}"


def _make_data(shape):
return MyData(
X=torch.rand(*shape),
Expand Down
Loading