Lightning-AI / Lightning-AI/pytorch-lightning

Support `ThunderModule` models

Open
#19,746 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

### Description & Motivation

I'm trying to get the NeMo multimodel imagen example to use thunder, but Lightning itself does not support Thunder:

```
Error executing job with overrides: ['trainer.precision=16', 'trainer.num_nodes=1', 'trainer.devices=1', '++exp_manager.max_time_per_run=00:00:03:00', 'trainer.max_steps=20', 'model.conditioning.embed_dim=64', 'model.micro_batch_size=1', 'model.global_batch_size=1', 'model.data.synthetic_data=True', 'exp_manager.exp_dir=./foo-imagen-train', 'model.inductor=False', 'model.unet.flash_attention=False']
Traceback (most recent call last):
File "/home/tfogal/dev/nemo/examples/multimodal/text_to_image/imagen/imagen_training.py", line 61, in main
trainer.fit(model)
File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/trainer/trainer.py", line 538, in fit
model = _maybe_unwrap_optimized(model)
File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/utilities/compile.py", line 132, in _maybe_unwrap_optimized
raise TypeError(
TypeError: `model` must be a `LightningModule` or `torch._dynamo.OptimizedModule`, got `ThunderModule`
```

The patch to NeMo that hits this is:

```sh
$ git diff examples/
diff --git a/examples/multimodal/text_to_image/imagen/imagen_training.py b/examples/multimodal/text_to_image/imagen/imagen_training.py
index 23c1c9c1a..c30df18fc 100644
--- a/examples/multimodal/text_to_image/imagen/imagen_training.py
+++ b/examples/multimodal/text_to_image/imagen/imagen_training.py
@@ -23,6 +23,7 @@ from nemo.collections.nlp.parts.megatron_trainer_builder import MegatronTrainerB
from nemo.core.config import hydra_runner
from nemo.utils import logging
from nemo.utils.exp_manager import exp_manager
+import thunder


@hydra_runner(config_path='conf', config_name='base64-500m')
@@ -38,6 +39,7 @@ def main(cfg) -> None:
cfg.model.precision = cfg.trainer.precision

model = MegatronImagen(cfg.model, trainer)
+ model = thunder.jit(model)

if cfg.model.get("inductor", False):
# Temporary hack to get rid of TorchDynamo issue with DDP
```

### Pitch

It would be great if Lightning could be updated to interoperate with Thunder such that we could pass the entire model to Thunder and Lightning could use / make sense of the returned `ThunderModule`.

### Alternatives

Only use Thunder on smaller pieces of the model.

### Additional context

https://github.com/NVIDIA/NeMo/blob/23baa48e441ecb6cc6b49c23bf8cfc076db38bdc/examples/multimodal/text_to_image/imagen/imagen_training.py#L26 is the source for the model.

cc @borda

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Lightning traceback in pytorch_lightning/utilities/compile.py and compare its handling of LightningModule and torch._dynamo. Reproduce the NeMo Imagen example using thunder.jit(model), then determine the expected integration behavior for ThunderModule and add coverage for the supported model path.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.