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] Allow any tensorclass to have a data field #906

Merged
merged 1 commit into from
Jul 22, 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
14 changes: 13 additions & 1 deletion tensordict/tensorclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def __torch_function__(
if not hasattr(cls, "names"):
cls.require = property(_names, _names_setter)
if not _is_non_tensor and not hasattr(cls, "data"):
cls.data = property(_data)
cls.data = property(_data, _data_setter)
if not hasattr(cls, "grad"):
cls.grad = property(_grad)
if not hasattr(cls, "to_dict"):
Expand Down Expand Up @@ -1719,9 +1719,21 @@ def _names(self) -> torch.Size:


def _data(self):
# We allow data to be a field of the class too
if "data" in self.__dataclass_fields__:
data = self._tensordict.get("data", None)
if data is None:
data = self._non_tensordict.get("data")
return data
return self._from_tensordict(self._tensordict.data, self._non_tensordict)


def _data_setter(self, new_data):
if "data" in self.__dataclass_fields__:
return self.set("data", new_data)
raise AttributeError("property 'data' is read-only.")


def _grad(self):
grad = self._tensordict._grad
if grad is None:
Expand Down
Loading