Open rexxy-sasori opened 3 months ago
Is there a particular use case you have for training data with requires_grad=True
? Or is this an issue that you ran into without deliberately setting this? In general it's not clear what it would mean to fit the model if the training data itself were a parameter (you'd get a perfect fit all the time...). One could compute the gradient of the model parameters w.r.t. the training data at the optimum (MAP maximizer), but that's a different thing.
Thanks for reporting this issue. I'm not surprised this fails, because BoTorch figures out which tensors are parameters that need to be optimized by looking at which have requires_grad=True
. I second Max's question about the use case, since I'm not sure whether it would make sense to support this. Would it work to (perhaps temporarily) detach
the input data?
Sorry, perhaps I should give a little bit context. This is actually an issue I ran into without deliberately setting requires_grad. The example here is just a demonstration for anyone here to reproduce the bug. I want to use risk-averse BO for model predictive control, in which I first built a model in PyTorch that maps my control variable to my objective. However, the fit_gpytorch method always failed until I figured out that the issue went away until I used torch.no_grad()
However, the fit_gpytorch method always failed until I figured out that the issue went away until I used with torch.no_grad()
Just to make sure there is no confusion here, you are not putting the fit_gpytorch_mll()
call into a no_grad()
context, right? Just making sure the inputs to the GP model don't require gradients, i.e. do whatever prediction you do on your model in a no_grad()
context.
It may make sense on our end to explicitly check whether the training data requires grad when calling fit_gpytorch_mll
to emit a more informative error message.
No I am not putting the fit_gpytorch_mll() call into a no_grad() context.
Yes, I agree with your suggestion to explicitly check whether the training data requires grad when calling fit_gpytorch_mll to emit a more informative error message.
š Bug
The train_X and train_Y that go into SingleTaskGP will lead to a failing fit_gpytorch_mll if they have require_grad=True, i.e. grad_fn is not None. The error goes away when the flag require_grad=False
To reproduce
Code snippet to reproduce
Stack trace/error message
Expected Behavior
An error message is thrown:
"Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward"
System information
Please complete the following information:
Additional context