code: def test_complex_nested_model():
      """Test flattening, freezing, and thawing of models which contain parent (non-leaf) modules with parameters
      directly themselves rather than exclusively their submodules containing parameters.
      """
  
      model = nn.Sequential(
          OrderedDict(
              [
                  (
                      "encoder",
                      nn.Sequential(
                          ConvBlockParam(3, 64), ConvBlock(64, 128)
                      ),
                  ),
                  ("decoder", ConvBlock(128, 10)),
              ]
          )
      )
  
      # There are 10 leaf modules or parent modules w/ parameters in the test model
      assert len(BaseFinetuning.flatten_modules(model)) == 10
  
      BaseFinetuning.freeze(model.encoder, train_bn=True)
      assert (
          not model.encoder[0].module_dict["conv"].weight.requires_grad
      )  # Validate a leaf module parameter is frozen
      assert not model.encoder[
          0
      ].parent_param.requires_grad  # Validate the parent module parameter is frozen
      assert model.encoder[0].bn.weight.requires_grad
  
      BaseFinetuning.make_trainable(model)
      encoder_params = list(
          BaseFinetuning.filter_params(model.encoder, train_bn=True)
      )
      # The 9 parameters of the encoder are:
      # conv0.weight, conv0.bias, bn0.weight, bn0.bias, parent_param
      # conv1.weight, conv1.bias, bn1.weight, bn1.bias
      assert len(encoder_params) == 9
  
imports: from collections import OrderedDict
  import pytest
  import torch
  from lightning.fabric.utilities.imports import _TORCH_GREATER_EQUAL_1_13
  from lightning.pytorch import LightningModule, Trainer, seed_everything
  from lightning.pytorch.callbacks import BackboneFinetuning, BaseFinetuning, ModelCheckpoint
  from lightning.pytorch.demos.boring_classes import BoringModel, RandomDataset
  from torch import nn
  from torch.optim import SGD, Optimizer
  from torch.utils.data import DataLoader
  from tests_pytorch.helpers.runif import RunIf
label_desc: Use image node_id 6 for calling a global function with example usage: test_complex_nested_model() without return types