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

[Quality] Better error message for incongruent lists of keys #1077

Merged
merged 1 commit into from
Nov 6, 2024
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
75 changes: 30 additions & 45 deletions tensordict/_td.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,15 +691,7 @@ def __eq__(self, other: object) -> T | bool:
keys1 = set(self.keys())
keys2 = set(other.keys())
if len(keys1.difference(keys2)) or len(keys1) != len(keys2):
keys1 = sorted(
keys1,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
keys2 = sorted(
keys2,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
raise KeyError(f"keys in tensordicts mismatch, got {keys1} and {keys2}")
_mismatch_keys(keys1, keys2)
d = {}
for key, item1 in self.items():
d[key] = item1 == other.get(key)
Expand All @@ -721,15 +713,7 @@ def __ge__(self, other: object) -> T | bool:
keys1 = set(self.keys())
keys2 = set(other.keys())
if len(keys1.difference(keys2)) or len(keys1) != len(keys2):
keys1 = sorted(
keys1,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
keys2 = sorted(
keys2,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
raise KeyError(f"keys in tensordicts mismatch, got {keys1} and {keys2}")
_mismatch_keys(keys1, keys2)
d = {}
for key, item1 in self.items():
d[key] = item1 >= other.get(key)
Expand All @@ -751,15 +735,7 @@ def __gt__(self, other: object) -> T | bool:
keys1 = set(self.keys())
keys2 = set(other.keys())
if len(keys1.difference(keys2)) or len(keys1) != len(keys2):
keys1 = sorted(
keys1,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
keys2 = sorted(
keys2,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
raise KeyError(f"keys in tensordicts mismatch, got {keys1} and {keys2}")
_mismatch_keys(keys1, keys2)
d = {}
for key, item1 in self.items():
d[key] = item1 > other.get(key)
Expand All @@ -781,15 +757,7 @@ def __le__(self, other: object) -> T | bool:
keys1 = set(self.keys())
keys2 = set(other.keys())
if len(keys1.difference(keys2)) or len(keys1) != len(keys2):
keys1 = sorted(
keys1,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
keys2 = sorted(
keys2,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
raise KeyError(f"keys in tensordicts mismatch, got {keys1} and {keys2}")
_mismatch_keys(keys1, keys2)
d = {}
for key, item1 in self.items():
d[key] = item1 <= other.get(key)
Expand All @@ -811,15 +779,7 @@ def __lt__(self, other: object) -> T | bool:
keys1 = set(self.keys())
keys2 = set(other.keys())
if len(keys1.difference(keys2)) or len(keys1) != len(keys2):
keys1 = sorted(
keys1,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
keys2 = sorted(
keys2,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
raise KeyError(f"keys in tensordicts mismatch, got {keys1} and {keys2}")
_mismatch_keys(keys1, keys2)
d = {}
for key, item1 in self.items():
d[key] = item1 < other.get(key)
Expand Down Expand Up @@ -5168,3 +5128,28 @@ def memmap(
return_early=return_early,
share_non_tensor=share_non_tensor,
)


def _mismatch_keys(keys1, keys2):
keys1 = sorted(
keys1,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
keys2 = sorted(
keys2,
key=lambda key: "".join(key) if isinstance(key, tuple) else key,
)
if set(keys1) - set(keys2):
sub1 = rf"The first TD has keys {set(keys1) - set(keys2)} that the second does not have."
else:
sub1 = None
if set(keys2) - set(keys1):
sub2 = rf"The second TD has keys {set(keys2) - set(keys1)} that the first does not have."
else:
sub2 = None
main = [r"keys in tensordicts mismatch."]
if sub1 is not None:
main.append(sub1)
if sub2 is not None:
main.append(sub2)
raise KeyError(r" ".join(main))
Loading