Two comments here:
The autograd graph if per-tensor not per-element, so you cannot mask within a tensor to free the computational graph.
For much the same reason assigning values will not cut the graph but instead insert a new node CopySlice:
a = torch.randn(5, requires_grad=True)
b = torch.randn(5, requires_grad=True)
c = torch.zeros(4, 7, requires_grad=True)
d = c * 2
d[1, 1:-1] = a
d[2, 1:-1] = b

will give an autograd graph that looks like this:

If you were to draw intermediate graphs, we would see that while the tensor d stays the same, the autograd node directly attached to it moves from MulBackward0 to the CopySlices with the assignments.
Best regards
Thomas