code: def test_loop_hierarchy():
      @dataclass
      class SimpleProgress(_BaseProgress):
          increment: int = 0
  
      class Simple(_Loop):
          def __init__(self, trainer, a):
              super().__init__(trainer)
              self.a = a
              self.progress = SimpleProgress()
  
          def run(self):
              while not self.progress.increment > 0:
                  try:
                      self.advance()
                      self.progress.increment += 1
                      self._restarting = False
                  except StopIteration:
                      break
              self._restarting = False
  
          def advance(self) -> None:
              loop = getattr(self, "loop_child", None)
              if not loop:
                  return
              loop.run()
  
          def on_save_checkpoint(self) -> Dict:
              return {"a": self.a}
  
          def on_load_checkpoint(self, state_dict: Dict) -> None:
              self.a = state_dict["a"]
  
      trainer = Trainer()
      loop_parent = Simple(trainer, 1)
      loop_child = Simple(trainer, 2)
      loop_parent.loop_child = loop_child
  
      state_dict = loop_parent.state_dict()
      assert state_dict == {
          "state_dict": {"a": 1},
          "progress": {"increment": 0},
          "loop_child.state_dict": {"a": 2},
          "loop_child.progress": {"increment": 0},
      }
  
      state_dict["loop_child.state_dict"]["a"] = 3
      # check restarting after `load_state_dict`
      loop_parent.load_state_dict(state_dict)
      assert loop_parent.restarting
  
      loop_parent.run()
  
      # check the new state after `run`
      state_dict = loop_parent.state_dict()
      assert state_dict == {
          "state_dict": {"a": 1},
          "progress": {"increment": 1},
          "loop_child.state_dict": {"a": 3},
          "loop_child.progress": {"increment": 1},
      }
  
      loop_parent_copy = deepcopy(loop_parent)
      assert loop_parent_copy.state_dict() == loop_parent.state_dict()
  
      assert (
          loop_parent_copy.on_save_checkpoint()
          == state_dict["state_dict"]
      )
      assert (
          loop_parent_copy.loop_child.on_save_checkpoint()
          == state_dict["loop_child.state_dict"]
      )
  
      loop_parent = Simple(trainer, 1)
      loop_child = Simple(trainer, 2)
      loop_parent.loop_child = loop_child
      loop_parent.load_state_dict(state_dict)
      assert loop_parent.progress.increment == 1
      assert loop_parent.loop_child.progress.increment == 1
  
      del loop_parent.loop_child
      state_dict = loop_parent.state_dict()
      assert state_dict == {
          "state_dict": {"a": 1},
          "progress": {"increment": 1},
      }
  
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 3 for calling a global function with example usage: test_loop_hierarchy() and returns: dict