[Bug] _permute_batch has wrong behavior in some instances with LazyEvaluatedKernelTensor
See original GitHub issue🐛 Bug
In some situations _permute_batch
when operating on a LazyEvaluatedKernelTensor
that is batched will not properly permute dimensions but do some kind of expand
.
To reproduce
I tried hard to find a minimal example, but so far I’ve only been able to reliably reproduce this when using a SAASBO model in Ax. Full repro here: permute_batch_shape_issue_repro.ipynb.txt - this is on current master of gpytorch, botorch, and Ax.
** Stack trace/error message **
The offending part is this one:
ipdb> covar_blocks_lazy.lazy_tensors[0].shape
torch.Size([1, 4, 100, 100])
ipdb> covar_blocks_lazy.lazy_tensors[0]._permute_batch(1, 0).shape
torch.Size([4, 4, 100, 100])
Expected Behavior
Rather than lazy tensor of shape torch.Size([4, 4, 100, 100])
this should return a tensor of size torch.Size([4, 1, 100, 100])
, i.e., perform a proper permutation of batch dimensions.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I tried to dig into it a bit with my minimal knowledge of lazy tensors.
https://github.com/cornellius-gp/gpytorch/blob/70eb9f9561bc3ce1f5c00c36e1d865c6b78fca44/gpytorch/lazy/lazy_tensor.py#L190
This line takes in 2
4 x 1 x 100 x s
(s takes values from5, 6, 100
) tensors ascomponents
, and thekwargs
has the kernel and'last_dim_is_batch': False
. The result of the operation hasres.shape = 4 x 4 x 100 x 100
.Looking into what happens in
res.shape
call, it is retrieved from the cache even at the first call. Ignoring that and going intores._size()
, it goes into theelse
part of this check due tokernel.batch_shape=4
, which produces the shape4 x 4 x 100 x 100
(the firstexpected_shape
is4 x 1 x 100 x 100
, lines 250-260 makes it4 x 4 x 100 x 100
):https://github.com/cornellius-gp/gpytorch/blob/70eb9f9561bc3ce1f5c00c36e1d865c6b78fca44/gpytorch/lazy/lazy_evaluated_kernel_tensor.py#L237-L260
Hmm yeah interesting idea. I think there might be a few things to be careful about - e.g. we will need to have things like
.size()
and.shape()
return the already permuted shape even if the actual tensor itself hasn’t been permuted yet. But might not be too bad, I’ll give it a try.