# Buggy code
def forward(self, *args):
    left_tensor = None
    right_tensor = None
    matrix_args = None
    if self.has_left:
        left_tensor, right_tensor, *matrix_args = args
    else:
        right_tensor, *matrix_args = args
    orig_right_tensor = right_tensor
    lazy_tsr = self.representation_tree(*matrix_args)

    self.is_vector = False
    if right_tensor.ndimension() == 1:
        right_tensor = right_tensor.unsqueeze(-1)
        self.is_vector = True

    # Perform solves (for inv_quad) and tridiagonalization (for estimating logdet)
    if self.has_left:
        rhs = torch.cat([left_tensor.transpose(-1, -2), right_tensor], -1)
        solves = lazy_tsr._solve(rhs, self.preconditioner)
        res = solves[..., left_tensor.size(-2) :]
        res = left_tensor @ res
    else:
        solves = lazy_tsr._solve(right_tensor, self.preconditioner)
        res = solves

    if self.is_vector:
        res = res.squeeze(-1)

    if self.has_left:
        args = [solves, left_tensor, orig_right_tensor] + list(matrix_args)
    else:
        args = [solves, orig_right_tensor] + list(matrix_args)
    self.save_for_backward(*args)
    if settings.memory_efficient.off():
        self._lazy_tsr = lazy_tsr

    return res


# Traceback (RuntimeError)
$ python test_1D_grid_gp_regression.py
E.
======================================================================
ERROR: test_grid_gp_mean_abs_error (__main__.TestGridGPRegression)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_1D_grid_gp_regression.py", line 87, in test_grid_gp_mean_abs_error
loss.backward()
File "/usr/local/lib/python3.6/dist-packages/torch/tensor.py", line 102, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/usr/local/lib/python3.6/dist-packages/torch/autograd/__init__.py", line 90, in backward
allow_unreachable=True)  # allow_unreachable flag
File "/home/gpytorch/gpytorch/functions/_matmul.py", line 34, in backward
rhs = self.saved_tensors[0]
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

----------------------------------------------------------------------
Ran 2 tests in 0.088s

FAILED (errors=1)

# Fixed code
def forward(self, *args):
    left_tensor = None
    right_tensor = None
    matrix_args = None
    if self.has_left:
        left_tensor, right_tensor, *matrix_args = args
    else:
        right_tensor, *matrix_args = args
    orig_right_tensor = right_tensor
    lazy_tsr = self.representation_tree(*matrix_args)

    with torch.no_grad():
        self.preconditioner = lazy_tsr.detach()._inv_matmul_preconditioner()

    self.is_vector = False
    if right_tensor.ndimension() == 1:
        right_tensor = right_tensor.unsqueeze(-1)
        self.is_vector = True

    # Perform solves (for inv_quad) and tridiagonalization (for estimating logdet)
    if self.has_left:
        rhs = torch.cat([left_tensor.transpose(-1, -2), right_tensor], -1)
        solves = lazy_tsr._solve(rhs, self.preconditioner)
        res = solves[..., left_tensor.size(-2) :]
        res = left_tensor @ res
    else:
        solves = lazy_tsr._solve(right_tensor, self.preconditioner)
        res = solves

    if self.is_vector:
        res = res.squeeze(-1)

    if self.has_left:
        args = [solves, left_tensor, orig_right_tensor] + list(matrix_args)
    else:
        args = [solves, orig_right_tensor] + list(matrix_args)
    self.save_for_backward(*args)
    if settings.memory_efficient.off():
        self._lazy_tsr = lazy_tsr

    return res