Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lightning/pytorch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Fixed `SIGTERMException` producing a zero exit code instead of 143 (128 + SIGTERM) ([#21623](https://github.com/Lightning-AI/pytorch-lightning/issues/21623))

- Fixed `BackboneFinetuning` not honoring `train_bn` during the initial frozen phase, leaving `BatchNorm` layers trainable when `train_bn=False` ([#21652](https://github.com/Lightning-AI/pytorch-lightning/pull/21652))

---

## [2.6.4] - 2026-05-20
Expand Down
2 changes: 1 addition & 1 deletion src/lightning/pytorch/callbacks/finetuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def on_fit_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -

@override
def freeze_before_training(self, pl_module: "pl.LightningModule") -> None:
self.freeze(pl_module.backbone)
self.freeze(pl_module.backbone, train_bn=self.train_bn)

@override
def finetune_function(self, pl_module: "pl.LightningModule", epoch: int, optimizer: Optimizer) -> None:
Expand Down
31 changes: 31 additions & 0 deletions tests/tests_pytorch/callbacks/test_finetuning_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,37 @@ def train_dataloader(self):
assert model.backbone.has_been_used


def test_finetuning_callback_train_bn_false(tmp_path):
"""Test that BackboneFinetuning respects train_bn=False during the initial freeze phase."""
seed_everything(42)

class FinetuningBoringModel(BoringModel):
def __init__(self):
super().__init__()
self.backbone = nn.Sequential(nn.Linear(32, 32, bias=False), nn.BatchNorm1d(32), nn.ReLU())
self.layer = nn.Linear(32, 2)

def forward(self, x):
return self.layer(self.backbone(x))

def configure_optimizers(self):
return torch.optim.SGD(self.layer.parameters(), lr=0.1)

def train_dataloader(self):
return DataLoader(RandomDataset(32, 64), batch_size=2)

model = FinetuningBoringModel()
callback = BackboneFinetuning(unfreeze_backbone_at_epoch=3, train_bn=False, verbose=False)

trainer = Trainer(limit_train_batches=4, default_root_dir=tmp_path, callbacks=[callback], max_epochs=1)
trainer.fit(model)

# With train_bn=False, BatchNorm should be fully frozen (not trainable, no running stats)
assert not model.backbone[1].weight.requires_grad
assert not model.backbone[1].bias.requires_grad
assert not model.backbone[1].track_running_stats


class TestBackboneFinetuningWarningCallback(BackboneFinetuning):
def finetune_function(self, pl_module, epoch: int, optimizer):
"""Called when the epoch begins."""
Expand Down
Loading