There are two separate tasks:

an auto-encoder for reconstruction
an encoder for regression

For the second task, I would like to use the code layer of the first task as my target, after the first model is trained; and the second model would be identical to the encoder part of the auto-encoder, instead of using a literally separate model.
That is, when training the second model:
# setting model1 = autoencoder(encoder_only=False) during task1 training
model1 = torch.load("path/to/trained/autoencoder") # After task1 training, load the trained auto-encoder
model1.eval()
model1.encode_only = True # set the flag to be True to make forward return the code layer output
model1_output = model1(input1)

# task2 training
model2 = autoencoder(encoder_only=True) # instantiate the second model whose forward return the code layer output
model2_output = model2(input2)
loss2 = loss(model2_output, model1_output)
loss2.backward()
optimizer2.step()

The encode_only flag tells the forward function to either just return the code layer output or the reconstructed input. It doesn’t change network architecture at all.
My question is, will loss2.step() update the rest of the unintended network of model2 (the decoder part of the model1), or the loss2 only backprop through the network prior to the model2_output (the intended, encoder part of model1)?