Skip to content

Commit

Permalink
no-op _add_instantiators (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
ordabayevy authored Jul 10, 2024
1 parent d15baa0 commit 7ce2f8f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cellarium/ml/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ def __init__(self, args: ArgsType = None) -> None:
args=args,
)

def _add_instantiators(self) -> None:
# disable breaking dependency injection support change introduced in PyTorch Lightning 2.3
# https://github.com/Lightning-AI/pytorch-lightning/pull/18105
pass

def instantiate_classes(self) -> None:
with torch.device("meta"):
# skip the initialization of model parameters
Expand Down
14 changes: 14 additions & 0 deletions cellarium/ml/core/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def __init__(
self.save_hyperparameters(logger=False)
self.pipeline: CellariumPipeline | None = None

if optim_fn is None:
# Starting from PyTorch Lightning 2.3, automatic optimization doesn't allow to return None
# from the training_step during distributed training. https://github.com/Lightning-AI/pytorch-lightning/pull/19918
# Thus, we need to use manual optimization for the No Optimizer case.
self.automatic_optimization = False

def configure_model(self) -> None:
"""
.. note::
Expand Down Expand Up @@ -156,6 +162,14 @@ def training_step( # type: ignore[override]
if loss is not None:
# Logging to TensorBoard by default
self.log("train_loss", loss, sync_dist=True)

if not self.automatic_optimization:
# Note, that running .step() is necessary for incrementing the global step even though no backpropagation
# is performed.
no_optimizer = self.optimizers()
assert isinstance(no_optimizer, pl.core.optimizer.LightningOptimizer)
no_optimizer.step()

return loss

def forward(self, batch: dict[str, np.ndarray | torch.Tensor]) -> dict[str, np.ndarray | torch.Tensor]:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies = [
"crick>=0.0.4",
"google-cloud-storage",
"jsonargparse[signatures]==4.27.7",
"lightning>=2.2.0, <2.3",
"lightning>=2.2.0",
"pyro-ppl>=1.9.1",
"pytest",
"torch>=2.2.0",
Expand Down

0 comments on commit 7ce2f8f

Please sign in to comment.