How to learn only the part of the networks?

# input x -> modelA -> modelB -> modelC

optimizer_c = optim.SGD(modelC, ...)

modelA = nn.Linear(10, 10)
modelB = nn.Linear(10, 10)
modelC = nn.Linear(10, 10)

# First phase: Train only modelC.
x = torch.randn(1, 10)
out = modelC(modelB(modelA(x)))
loss_c = ce(out, target)

optimizer_c.zero_grad()
loss_c.backward()
optimizer_c.step()

If I only want to train modelC, is this the correct code ?
No detach() or torch.no_grad() is required ?

This post might help you.