Skip to content

Commit 756221a

Browse files
authored
Merge pull request #933 from weecology/bw4sz-patch-1
Update 07_scaling.md
2 parents 00bcbf5 + 90de8ab commit 756221a

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

docs/user_guide/07_scaling.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
11
# Scaling DeepForest using PyTorch Lightning
22

3-
Often we have a large number of tiles we want to predict. DeepForest uses [PyTorch Lightning](https://lightning.ai/docs/pytorch/stable/) to scale inference. This gives us access to powerful tools for scaling without any changes to user code. DeepForest automatically detects whether you are running on GPU or CPU. Within a single GPU node, you can scale training without needing to specify any additional arguments, since we use the ['auto' devices](https://lightning.ai/docs/pytorch/stable/common/trainer.html#devices) detection within PyTorch Lightning. For advanced users, DeepForest can [run across multiple SLURM nodes](https://lightning.ai/docs/pytorch/stable/clouds/cluster_advanced.html), each with multiple GPUs.
4-
5-
## Increase batch size
3+
### Increase batch size
64

75
It is more efficient to run a larger batch size on a single GPU. This is because the overhead of loading data and moving data between the CPU and GPU is relatively large. By running a larger batch size, we can reduce the overhead of these operations.
86

97
```
108
m.config["batch_size"] = 16
119
```
1210

13-
## Scaling inference across multiple GPUs
11+
## Training
12+
13+
DeepForest's create_trainer argument passes any argument to pytorch lightning. This means we can use pytorch lightnings amazing distributed training specifications. There is a large amount of documentation, but we find the most helpful section is
14+
15+
https://lightning.ai/docs/pytorch/stable/accelerators/gpu_intermediate.html
16+
17+
For example on a SLURM cluster, we use the following line to get 5 gpus on a single node.
18+
```
19+
m.create_trainer(logger=comet_logger, accelerator="gpu", strategy="ddp", num_nodes=1, devices=devices)
20+
```
21+
22+
While we rarely use multi-node GPU's, pytorch lightning has functionality at very large scales. We welcome users to share what configurations worked best.
23+
24+
A few notes that can trip up those less used to multi-gpu training. These are for the default configurations and may vary on a specific system. We use a large University SLURM cluster with 'ddp' distributed data parallel.
25+
26+
1. Batch-sizes are expressed _per_ _gpu_. If you tell DeepForest you want 2 images per batch and request 5 gpus, you are computing 10 images per forward pass across all GPUs. This is crucial for when profiling, make sure to scale any tests by the batch size!
27+
28+
2. Each device gets its own portion of the dataset. This means that they do not interact during forward passes.
29+
30+
3. Make sure to use srun when combining with SLURM! This is an easy one to miss and will cause training to hang without error. Documented here
31+
32+
https://lightning.ai/docs/pytorch/latest/clouds/cluster_advanced.html#troubleshooting.
33+
34+
35+
## Prediction
36+
37+
Often we have a large number of tiles we want to predict. DeepForest uses [PyTorch Lightning](https://lightning.ai/docs/pytorch/stable/) to scale inference. This gives us access to powerful tools for scaling without any changes to user code. DeepForest automatically detects whether you are running on GPU or CPU. The parallelization strategy is to run each tile on a separate GPU, we cannot parallelize crops from within the same tile across GPUs inside of main.predict_tile(). If you set m.create_trainer(accelerator="gpu", devices=4), and run predict_tile, you will only use 1 GPU per tile. This is because we need access to all crops to create a mosiac of the predictions.
38+
39+
### Scaling prediction across multiple GPUs
1440

1541
There are a few situations in which it is useful to replicate the DeepForest module across many separate Python processes. This is especially helpful when we have a series of non-interacting tasks, often called 'embarrassingly parallel' processes. In these cases, no DeepForest instance needs to communicate with another instance. Rather than coordinating GPUs with the associated annoyance of overhead and backend errors, we can just launch separate jobs and let them finish on their own. One helpful tool in Python is [Dask](https://www.dask.org/). Dask is a wonderful open-source tool for coordinating large-scale jobs. Dask can be run locally, across multiple machines, and with an arbitrary set of resources.
1642

@@ -69,4 +95,4 @@ We can wait to see the futures as they complete! Dask also has a beautiful visua
6995
for x in futures:
7096
completed_filename = x.result()
7197
print(completed_filename)
72-
```
98+
```

0 commit comments

Comments
 (0)