Lightning-AI / Lightning-AI/pytorch-lightning

Gradient checkpointing and ddp do not work together

Open
#20,395 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug repro needed ver: 2.4.x
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

### Bug description

Am launching a script taht trains a model which works well when trained without ddp and using gradient checkpointing, or using ddp but no gradient checkpointing, using fabric too. However, when setting both ddp and gradient checkpointing, activate thorugh gradient_checkpointing_enable() function of huggingface, we get error
```
[rank0]: File "/home/.../v2/lib/python3.10/site-packages/torch/autograd/graph.py", line 744, in _engine_run_backward
[rank0]: return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
[rank0]: RuntimeError: expect_autograd_hooks_ INTERNAL ASSERT FAILED at "../torch/csrc/distributed/c10d/reducer.cpp":1591, please report a bug to PyTorch.

```

Scripts where launched with
```
fabric = Fabric(accelerator="gpu",
loggers=loggers,
precision=opt.precision,
strategy=DDPStrategy(process_group_backend="nccl", find_unused_parameters=False, static_graph=True)
)
```

When i launch with options `strategy=DDPStrategy(process_group_backend="nccl", find_unused_parameters=True, static_graph=False)`, I get error instead:

```
[rank0]: Parameter at index 560 with name reader.decoder.transformer.h.11.mlp.c_proj.bias has been marked as ready twice. This means that multiple autograd engine hooks have fired for this particular parameter during this iteration.
```

Thanks in advance for your help.

### What version are you seeing the problem on?

v2.4

### How to reproduce the bug

_No response_

### Error messages and logs

```
# Error messages and logs here please
```

### Environment

Current environment

```
#- PyTorch Lightning Version (e.g., 2.4.0):
#- PyTorch Version (e.g., 2.4):
#- Python version (e.g., 3.12):
#- OS (e.g., Linux):
#- CUDA/cuDNN version:
#- GPU models and configuration:
#- How you installed Lightning(`conda`, `pip`, source):
```

### More info

_No response_

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 by reducing the reported Fabric setup to a reproducible script combining DDPStrategy with Hugging Face gradient_checkpointing_enable(), and collect the missing version and environment details. Compare the static_graph/find_unused_parameters configurations and inspect the DDP interaction with gradient checkpointing; done means the combination no longer raises either reported reducer error or the limitation is clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
distributed-systems, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.