I faced the same error recently in different scenario(not in jit) and I found the solution like below
      outer_loss = torch.tensor(0., device=args.device)
     avg_loss=F.cross_entropy(test_logit, test_target)
     outer_loss += avg_loss

     with torch.no_grad(): #if i don't change the loss using this it's throwing error as the model states for the claculated losses are different
              outer_loss.set_(torch.Tensor([InitialLoss-F.cross_entropy(test_logit, test_target)]).to(device)) 

This link also helped in understanding the problem