detach() does not effect the origin graph. Only if you want to get non-leaf variable, eg, z,  and perform operations on it without effecting the origin gradients( Here are gradients w.r.t x,y and z), you need add detach().
Eg.
x = torch.tensor(([1]),requires_grad=True)
y = x**2
z = 2*y
w= z**3

# This is the subpath
# Do not use detach()
p = z
q = torch.tensor(([2]), requires_grad=True)
pq = p*q
pq.backward(retain_graph=True)

w.backward()
print x.grad

The gradient will be accumulated in z, and it gives result of ‘56’.
However, when you try:
x = torch.tensor(([1]),requires_grad=True)
y = x**2
z = 2*y
w= z**3

# detach it, so the gradient w.r.t `p` does not effect `z`!
p = z.detach()
q = torch.tensor(([2]), requires_grad=True)
pq = p*q
pq.backward(retain_graph=True)

w.backward()
print x.grad

The gradient is still 48. So detach() makes the gradient flow in the subpath no harm to the main path.