code: def test_freeze_unfreeze_function(tmpdir):
      """Test freeze properly sets requires_grad on the modules."""
      seed_everything(42)
  
      class FreezeModel(LightningModule):
          def __init__(self):
              super().__init__()
              self.backbone = nn.Sequential(
                  nn.Linear(32, 32),
                  nn.BatchNorm1d(32),
                  nn.ReLU(),
                  nn.Linear(32, 2),
              )
  
      model = FreezeModel()
      assert model.backbone[1].track_running_stats
      BaseFinetuning.freeze(model, train_bn=True)
      assert not model.backbone[0].weight.requires_grad
      assert model.backbone[1].weight.requires_grad
      assert model.backbone[1].track_running_stats
      assert not model.backbone[3].weight.requires_grad
  
      BaseFinetuning.freeze(model, train_bn=False)
      assert not model.backbone[0].weight.requires_grad
      assert not model.backbone[1].weight.requires_grad
      assert not model.backbone[1].track_running_stats
      assert not model.backbone[3].weight.requires_grad
  
      BaseFinetuning.make_trainable(model)
      assert model.backbone[0].weight.requires_grad
      assert model.backbone[1].weight.requires_grad
      assert model.backbone[1].track_running_stats
      assert model.backbone[3].weight.requires_grad
  
      BaseFinetuning.freeze(model.backbone[0], train_bn=False)
      assert not model.backbone[0].weight.requires_grad
  
      BaseFinetuning.freeze(
          ([(model.backbone[1]), [model.backbone[3]]]), train_bn=True
      )
      assert model.backbone[1].weight.requires_grad
      assert model.backbone[1].track_running_stats
      assert not model.backbone[3].weight.requires_grad
  
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 3 for calling a global function with example usage: test_freeze_unfreeze_function(tmpdir) without return types