code: 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
  
imports: import os
  from copy import deepcopy
  from dataclasses import dataclass
  from typing import Dict, Iterator
  from unittest.mock import ANY, Mock
  import pytest
  import torch
  from lightning.pytorch import LightningModule, Trainer
  from lightning.pytorch.callbacks import Callback, ModelCheckpoint, OnExceptionCheckpoint
  from lightning.pytorch.demos.boring_classes import BoringModel, RandomDataset
  from lightning.pytorch.loops import _Loop
  from lightning.pytorch.loops.progress import _BaseProgress
  from torch.utils.data.dataloader import DataLoader, _MultiProcessingDataLoaderIter
  from tests_pytorch.helpers.runif import RunIf
label_desc: Use image node_id 10 for calling a global function with example usage: test_validation_during_gradient_accumulation_window(tmp_path) without return types