Hi all,
Inside one of my modules I have the following code to create a cost volume.
The cost is represented as a 3D volume of an image with each slice corresponds to a shift of d pixels. Pixels outside are set a zero in the volume.
def cost_volume(ref, tgt, max_disp):
    max_disp4 = max_disp // 4
    batch, channels, h, w = ref.size()
    cost = torch.zeros((batch, channels * 2, max_disp4, h, w), dtype=ref.dtype, device=ref.device,     requires_grad=False)

    cost[:, :channels, 0, :, :] = ref
    cost[:, channels:, 0, :, :] = tgt
    for d in range(1, max_disp4):
        cost[:, :channels, d, :, d:] = ref[:, :, :, d:]
        cost[:, channels:, d, :, d:] = tgt[:, :, :, :-d]

    return cost

When running in training mode, Pytorch complaints of not be able to compute the gradients due to some operations in-place, which is a fair error message.
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

I was wondering if an easy solution exists to convert this code from in-place to out-of-place? Or any trick to bypass this restriction for this scenario?
Thanks a lot for your help,