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

[Bug]: Dimension of empty Torch tensor is lost during to_json #679

Open
2 of 4 tasks
matthewcarbone opened this issue May 24, 2024 · 0 comments
Open
2 of 4 tasks
Labels

Comments

@matthewcarbone
Copy link
Contributor

Email (Optional)

No response

Version

v2024.4.17

Which OS(es) are you using?

  • MacOS
  • Windows
  • Linux

What happened?

Essentially, when saving an empty torch tensor of some dimension, that dimensionality information is lost when converting that tensor into json. This should be an easy fix: detect if the tensor is empty, and if so, explicitly save the dimension instead of the data.

from monty.json import MSONable
import torch

class Klass(MSONable):
    def __init__(self, a):
        self.a = a

k = Klass(torch.empty(0, 2))

print(k.a.shape)
print(k.as_dict())
print(k.to_json())

Results in

torch.Size([0, 2])
{'@module': '__main__', '@class': 'Klass', '@version': None, 'a': tensor([], size=(0, 2))}
{"@module": "__main__", "@class": "Klass", "@version": null, "a": {"@module": "torch", "@class": "Tensor", "dtype": "torch.DoubleTensor", "data": []}}

Where the dimension of the empty tensor has been lost.

I'd be happy to take a shot at fixing if you'd like.

By the way, why is this important (i.e., why on earth would we care about the dimension of an empty tensor)? Turns out when sampling from the dataless "uninformative" prior using gpytorch, we need to feed it an empty tensor of the correct dimension. I'd like to be able to save this data along with my model!

Code snippet

See above

Log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant