# Buggy code
def __init__(
    self,
    representation_tree,
    dtype,
    device,
    matrix_shape,
    batch_shape=torch.Size(),
    inv_quad=False,
    logdet=False,
    preconditioner=None,
    logdet_correction=None,
    probe_vectors=None,
    probe_vector_norms=None,
):
    if not (inv_quad or logdet):
        raise RuntimeError("Either inv_quad or logdet must be true (or both)")

    self.representation_tree = representation_tree
    self.dtype = dtype
    self.device = device
    self.matrix_shape = matrix_shape
    self.batch_shape = batch_shape
    self.inv_quad = inv_quad
    self.logdet = logdet
    self.preconditioner = preconditioner
    self.logdet_correction = logdet_correction

    if (probe_vectors is None or probe_vector_norms is None) and logdet:
        num_random_probes = settings.num_trace_samples.value()
        probe_vectors = torch.empty(
            matrix_shape[-1], num_random_probes, dtype=dtype, device=device
        )
        probe_vectors.bernoulli_().mul_(2).add_(-1)
        probe_vector_norms = torch.norm(probe_vectors, 2, dim=-2, keepdim=True)
        if batch_shape is not None:
            probe_vectors = probe_vectors.expand(
                *batch_shape, matrix_shape[-1], num_random_probes
            )
            probe_vector_norms = probe_vector_norms.expand(
                *batch_shape, 1, num_random_probes
            )
        probe_vectors = probe_vectors.div(probe_vector_norms)

    self.probe_vectors = probe_vectors
    self.probe_vector_norms = probe_vector_norms


# 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 __init__(
    self,
    representation_tree,
    dtype,
    device,
    matrix_shape,
    batch_shape=torch.Size(),
    inv_quad=False,
    logdet=False,
    probe_vectors=None,
    probe_vector_norms=None,
):
    if not (inv_quad or logdet):
        raise RuntimeError("Either inv_quad or logdet must be true (or both)")

    self.representation_tree = representation_tree
    self.dtype = dtype
    self.device = device
    self.matrix_shape = matrix_shape
    self.batch_shape = batch_shape
    self.inv_quad = inv_quad
    self.logdet = logdet

    if (probe_vectors is None or probe_vector_norms is None) and logdet:
        num_random_probes = settings.num_trace_samples.value()
        probe_vectors = torch.empty(
            matrix_shape[-1], num_random_probes, dtype=dtype, device=device
        )
        probe_vectors.bernoulli_().mul_(2).add_(-1)
        probe_vector_norms = torch.norm(probe_vectors, 2, dim=-2, keepdim=True)
        if batch_shape is not None:
            probe_vectors = probe_vectors.expand(
                *batch_shape, matrix_shape[-1], num_random_probes
            )
            probe_vector_norms = probe_vector_norms.expand(
                *batch_shape, 1, num_random_probes
            )
        probe_vectors = probe_vectors.div(probe_vector_norms)

    self.probe_vectors = probe_vectors
    self.probe_vector_norms = probe_vector_norms