Create an optimizer with the parameters of modelA only, calculate the gradients using loss.backward() can call optimizerA.step().
If you never want to update modelB, you could set the requires_grad attributes of its parameters to False additionally.