I think something like this:
full_grad = torch.zeros_like(xin)
full_grad[:,:,r_start:r_end,c_start:c_end] = xin.grad.data[:,:,r_start:r_end,c_start:c_end]
xin.grad.data.copy_(full_grad)
Or you can do the same thing zeroing necessary elements in xin.grad.data directly without additional full_grad variable.