def test_validation_during_gradient_accumulation_window(tmp_path):
    """Test that gradients don't get erased when the validation interval falls within the gradient accumulation
    phase."""

    class ValidationModel(BoringModel):
        def on_validation_start(self):
            batch_idx = self.trainer.fit_loop.epoch_loop.batch_progress.current.completed
            grad_expected = batch_idx % self.trainer.accumulate_grad_batches != 0
            if grad_expected:
                assert batch_idx in (2, 4)
                assert all(p.grad is not None for p in self.parameters())
            else:
                assert batch_idx == 6
                assert all(p.grad is None for p in self.parameters())
            self.ran_assert = True

    model = ValidationModel()
    trainer = Trainer(
        default_root_dir=tmp_path,
        limit_train_batches=6,
        limit_val_batches=1,
        accumulate_grad_batches=3,
        # validation happens in the middle of the first two accumulations, and at the end of the third
        val_check_interval=2,
        max_epochs=1,
        num_sanity_val_steps=0,
    )
    trainer.fit(model)
    assert model.ran_assert
Test that gradients don't get erased when the validation interval falls within the gradient accumulation
    phase.
tests/tests_pytorch/loops/test_loops.py