# Buggy code
def forward(self, x1, x2, diag=False, **params):
    x1_ = x1.div(self.period_length)
    x2_ = x2.div(self.period_length)
    diff = self.covar_dist(x1_, x2_, diag=diag, **params)
    res = torch.sin(diff.mul(math.pi)).pow(2).mul(-2 / self.lengthscale).exp_()
    if diff.ndimension() == 2 or diag:
        res = res.squeeze(0)
    return res


# Traceback (IndexError)
Traceback (most recent call last):

File "<ipython-input-13-effc04c4ab77>", line 61, in <module>
loss = -mll(output, train_y) ## NOTE fails here.

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/module.py", line 24, in __call__
outputs = self.forward(*inputs, **kwargs)

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/mlls/exact_marginal_log_likelihood.py", line 51, in forward
res = output.log_prob(target)

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/distributions/multivariate_normal.py", line 135, in log_prob
inv_quad, logdet = covar.inv_quad_logdet(inv_quad_rhs=diff.unsqueeze(-1), logdet=True)

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/lazy/lazy_tensor.py", line 1038, in inv_quad_logdet
args = self.representation()

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/lazy/lazy_tensor.py", line 1268, in representation
representation += list(arg.representation())

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/lazy/lazy_evaluated_kernel_tensor.py", line 311, in representation
return self.evaluate_kernel().representation()

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/utils/memoize.py", line 34, in g
add_to_cache(self, cache_name, method(self, *args, **kwargs))

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/lazy/lazy_evaluated_kernel_tensor.py", line 278, in evaluate_kernel
res = self.kernel(x1, x2, diag=False, last_dim_is_batch=self.last_dim_is_batch, **self.params)

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/kernels/kernel.py", line 395, in __call__
res = super(Kernel, self).__call__(x1_, x2_, last_dim_is_batch=last_dim_is_batch, **params)

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/module.py", line 24, in __call__
outputs = self.forward(*inputs, **kwargs)

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/kernels/scale_kernel.py", line 90, in forward
orig_output = self.base_kernel.forward(x1, x2, diag=diag, last_dim_is_batch=last_dim_is_batch, **params)

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/kernels/grid_interpolation_kernel.py", line 177, in forward
base_lazy_tsr = lazify(self._inducing_forward(last_dim_is_batch=last_dim_is_batch, **params))

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/kernels/grid_interpolation_kernel.py", line 143, in _inducing_forward
return super().forward(self.grid, self.grid, last_dim_is_batch=last_dim_is_batch, **params)

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/kernels/grid_kernel.py", line 133, in forward
covars = [ToeplitzLazyTensor(c.squeeze(-2)) for c in covars]

File "/Users/val/opt/anaconda3/lib/python3.7/site-packages/gpytorch/kernels/grid_kernel.py", line 133, in <listcomp>
covars = [ToeplitzLazyTensor(c.squeeze(-2)) for c in covars]

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2)

# Fixed code
def forward(self, x1, x2, diag=False, **params):
    x1_ = x1.div(self.period_length)
    x2_ = x2.div(self.period_length)
    diff = self.covar_dist(x1_, x2_, diag=diag, **params)
    res = torch.sin(diff.mul(math.pi)).pow(2).mul(-2 / self.lengthscale).exp_()
    if diag:
        res = res.squeeze(0)
    return res