Lightning-AI / Lightning-AI/pytorch-lightning

Training stuck when running on Slurm with multiprocessing

Open
#19,718 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

environment: slurm question ver: 2.1.x
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

### Bug description

Hi,
I'm trying to train a model on Slurm using a single GPU, and in the training_step I call multiprocessing.Pool() to parallel some function calls (function is executes on every example in the training data).
When I run multiprocessing.Pool from the training_step, the call never ends. I added multiple logs and prints to the code, and I see that all function calls were executed, but the pool was never joined and it stays "hanging".
I tried running the same code not on Slurm and it works as expected. I also tried running the same function using multiprocess on Slurm outside the training_step and it also worked.
The only thing that doesn't work is running the program on Slurm, and run multiprocess inside the training_step.

The trainer definition is:
```
trainer = pl.Trainer(
max_epochs=100,
callbacks=callbacks,
default_root_dir=root_dir
)
```

The training step (simplified):

```
def training_step(self, batch: List[torch.Tensor], batch_idx: int) -> torch.Tensor:
x, y = batch
if self.project:
with Pool() as pool:
x_projected = pool.starmap(self.projector.project, list(zip(x.to_cpu().numpy(), y.to_cpu.numpy())))
x = torch.tensor(x_projected).float().to(x.device)
logits = self(x)
return self.loss(logits, y)
```

And the Slurm file:
```
#! /bin/sh
#SBATCH --job-name=job
#SBATCH --output=logs/job.out # redirect stdout
#SBATCH --error=logs/job.err # redirect stderr
#SBATCH --partition=killable
#SBATCH --time=1000
#SBATCH --signal=USR1@120 # how to end job when time's up
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --mem=2500 # CPU memory (MB)
#SBATCH -c 8 # 8 cores
#SBATCH --gpus=1
python job.py

```
The lightning version I'm using is lightning 2.1.2.

Any ideas on what the problem is and how to solve it?
Thanks in advance!

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

v2.1

### How to reproduce the bug

_No response_

### Error messages and logs

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

### Environment

Current environment

```
#- Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow):
#- PyTorch Lightning Version (e.g., 1.5.0):
#- Lightning App Version (e.g., 0.5.2):
#- PyTorch Version (e.g., 2.0):
#- Python version (e.g., 3.9):
#- OS (e.g., Linux):
#- CUDA/cuDNN version:
#- GPU models and configuration:
#- How you installed Lightning(`conda`, `pip`, source):
#- Running environment of LightningApp (e.g. local, cloud):
```

### More info

_No response_

cc @awaelchli

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 reported job.py training script, the training_step implementation, and the Slurm submission script. Reproduce the multiprocessing.Pool hang under Slurm and compare it with the non-Slurm run and the standalone multiprocessing case. Done means the cause is identified and a verified fix or documented limitation is established, with a regression test or reproducible evidence.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
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.