pytorch / botorch

Bayesian optimization in PyTorch
https://botorch.org/
MIT License
3.06k stars 390 forks source link

[Bug] posteriors.TorchPosterior can't be torch.load-ed #1639

Closed mrcslws closed 1 year ago

mrcslws commented 1 year ago

🐛 Bug

botorch.posteriors.TorchPosterior doesn't support torch.load. Loading it throws an error.

Use case: Previously (a few months ago), during cross validation it was possible to torch.save the results and analyze them later. Now an error occurs when you try to torch.load a saved result.

To reproduce

import botorch
import torch

posterior = botorch.posteriors.TorchPosterior(
    torch.distributions.Normal(0.0, 1.0)
)

torch.save(posterior, "my-posterior.pt")
torch.load("my-posterior.pt")
  File "/Users/mrcslws/miniforge3/lib/python3.9/site-packages/torch/serialization.py", line 789, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
  File "/Users/mrcslws/miniforge3/lib/python3.9/site-packages/torch/serialization.py", line 1131, in _load
    result = unpickler.load()
  File "/Users/mrcslws/dev/src/botorch/botorch/posteriors/torch.py", line 79, in __getattr__
    return getattr(self.distribution, name)
  File "/Users/mrcslws/dev/src/botorch/botorch/posteriors/torch.py", line 79, in __getattr__
    return getattr(self.distribution, name)
  File "/Users/mrcslws/dev/src/botorch/botorch/posteriors/torch.py", line 79, in __getattr__
    return getattr(self.distribution, name)
  [Previous line repeated 993 more times]
RecursionError: maximum recursion depth exceeded

System information

Please complete the following information: botorch: 0.7.3.dev22+g208470e7.d20221102 gpytorch: 1.9.1.dev32+g23b068b5 torch: 1.13.1 Mac OS 13.1 M1

Balandat commented 1 year ago

@saitcakmak this seems to be due to the __getattr__ passthrough. Not sure how this interacts with pickle but apparently not well. Could you take a look please?

saitcakmak commented 1 year ago

Fix and explanation of what was going on is in https://github.com/pytorch/botorch/pull/1644.