def test_backward_count_simple(torch_backward, num_steps):
    """Test that backward is called exactly once per step."""
    model = BoringModel()
    trainer = Trainer(max_steps=num_steps, logger=False, enable_checkpointing=False)
    trainer.fit(model)
    assert torch_backward.call_count == num_steps

    torch_backward.reset_mock()

    trainer.test(model)
    assert torch_backward.call_count == 0
Test that backward is called exactly once per step.
tests/tests_pytorch/trainer/optimization/test_backward_calls.py