Shreya_Saha:

grad can be implicitly created only for scalar outputs


you need to make sure that lossB is a scalar (i.e. a single number) so make sure you sum over your batch dimension as well. If you want per-sample gradients, this can be done via the use of hooks (but gets quite messy to implement)
Also, if lossB doesn’t depend on the parameters of modelA the gradients will be zero by definition. So, check they depend on each other!