# Buggy code
def evaluate_kernel(self):
    """
    NB: This is a meta LazyTensor, in the sense that evaluate can return
    a LazyTensor if the kernel being evaluated does so.
    """
    if not self.is_batch:
        x1 = self.x1.unsqueeze(0)
        x2 = self.x2.unsqueeze(0)
    else:
        x1 = self.x1
        x2 = self.x2

    with settings.lazily_evaluate_kernels(False):
        res = self.kernel(x1, x2, diag=False, batch_dims=self.batch_dims, **self.params)
    if self.squeeze_row:
        res.squeeze_(-2)
    if self.squeeze_col:
        res.squeeze_(-1)

    if not self.is_batch and res.ndimension() == 3 and res.size(0) == 1:
        res = res[0]

    return lazify(res)


# Traceback (RuntimeError)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-67-cb234b5fe124> in <module>
15     output = model(X)
16     # Calc loss and backprop gradients
---> 17     loss = -mll(output, y)
18     loss.backward()
19     if not i % 30:

~/.virtualenvs/GenRS/lib/python3.7/site-packages/gpytorch/module.py in __call__(self, *inputs, **kwargs)
18
19     def __call__(self, *inputs, **kwargs):
---> 20         outputs = self.forward(*inputs, **kwargs)
21         if isinstance(outputs, list):
22             return [_validate_module_outputs(output) for output in outputs]

~/.virtualenvs/GenRS/lib/python3.7/site-packages/gpytorch/mlls/exact_marginal_log_likelihood.py in forward(self, output, target, *params)
26         # Get the log prob of the marginal distribution
27         output = self.likelihood(output, *params)
---> 28         res = output.log_prob(target)
29
30         # Add terms for SGPR / when inducing points are learned

~/.virtualenvs/GenRS/lib/python3.7/site-packages/gpytorch/distributions/multivariate_normal.py in log_prob(self, value)
123
124         # Get log determininat and first part of quadratic form
--> 125         inv_quad, logdet = covar.inv_quad_logdet(inv_quad_rhs=diff.unsqueeze(-1), logdet=True)
126
127         res = -0.5 * sum([inv_quad, logdet, diff.size(-1) * math.log(2 * math.pi)])

~/.virtualenvs/GenRS/lib/python3.7/site-packages/gpytorch/lazy/lazy_tensor.py in inv_quad_logdet(self, inv_quad_rhs, logdet, reduce_inv_quad)
745                 )
746
--> 747         args = self.representation()
748         if inv_quad_rhs is not None:
749             args = [inv_quad_rhs] + list(args)

~/.virtualenvs/GenRS/lib/python3.7/site-packages/gpytorch/lazy/lazy_tensor.py in representation(self)
980                 representation.append(arg)
981             elif hasattr(arg, "representation") and callable(arg.representation):  # Is it a LazyTensor?
--> 982                 representation += list(arg.representation())
983             else:
984                 raise RuntimeError("Representation of a LazyTensor should consist only of Tensors")

~/.virtualenvs/GenRS/lib/python3.7/site-packages/gpytorch/lazy/lazy_evaluated_kernel_tensor.py in representation(self)
306         # representation
307         else:
--> 308             return self.evaluate_kernel().representation()
309
310     def representation_tree(self):

~/.virtualenvs/GenRS/lib/python3.7/site-packages/gpytorch/utils/memoize.py in g(self, *args, **kwargs)
15         cache_name = name if name is not None else method
16         if cache_name not in self._memoize_cache:
---> 17             self._memoize_cache[cache_name] = method(self, *args, **kwargs)
18         return self._memoize_cache[cache_name]
19

~/.virtualenvs/GenRS/lib/python3.7/site-packages/gpytorch/lazy/lazy_evaluated_kernel_tensor.py in evaluate_kernel(self)
260         with settings.lazily_evaluate_kernels(False):
261             res = self.kernel(
--> 262                 x1, x2, diag=False, batch_dims=self.batch_dims, **self.params
263             )
264         if self.squeeze_row:

~/.virtualenvs/GenRS/lib/python3.7/site-packages/gpytorch/kernels/kernel.py in __call__(self, x1, x2, diag, batch_dims, **params)
313         # Select the active dimensions
314         if self.active_dims is not None:
--> 315             x1_ = x1_.index_select(-1, self.active_dims)
316             if x2_ is not None:
317                 x2_ = x2_.index_select(-1, self.active_dims)

RuntimeError: invalid argument 3: out of range at /Users/soumith/b101/2019_02_04/wheel_build_dirs/wheel_3.7/pytorch/aten/src/TH/generic/THTensor.cpp:350

# Fixed code
def evaluate_kernel(self):
    """
    NB: This is a meta LazyTensor, in the sense that evaluate can return
    a LazyTensor if the kernel being evaluated does so.
    """
    if not self.is_batch:
        x1 = self.x1.unsqueeze(0)
        x2 = self.x2.unsqueeze(0)
    else:
        x1 = self.x1
        x2 = self.x2

    with settings.lazily_evaluate_kernels(False):
        temp_active_dims = self.kernel.active_dims
        self.kernel.active_dims = None
        res = self.kernel(x1, x2, diag=False, batch_dims=self.batch_dims, **self.params)
        self.kernel.active_dims = temp_active_dims
    if self.squeeze_row:
        res.squeeze_(-2)
    if self.squeeze_col:
        res.squeeze_(-1)

    if not self.is_batch and res.ndimension() == 3 and res.size(0) == 1:
        res = res[0]

    return lazify(res)