Nazila-H:

        model_global.to(device)





 Nazila-H:

        grad = torch.autograd.grad(outputs=loss, inputs=data)





 Nazila-H:

        if iteration_count >= max_nr_batches >= 0:
           break



I am not sure what task you are working on. But the lines above seems not needed.
Can you clarify why you need those?