Skip to content

Commit 9836f0e

Browse files
[docs] Regional compilation docs (#11556)
* add regional compilation docs. * minor. * reviwer feedback. * Update docs/source/en/optimization/torch2.0.md Co-authored-by: Ilyas Moutawwakil <[email protected]> --------- Co-authored-by: Ilyas Moutawwakil <[email protected]>
1 parent 20379d9 commit 9836f0e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

docs/source/en/optimization/torch2.0.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ For more information and different options about `torch.compile`, refer to the [
7878
> [!TIP]
7979
> Learn more about other ways PyTorch 2.0 can help optimize your model in the [Accelerate inference of text-to-image diffusion models](../tutorials/fast_diffusion) tutorial.
8080
81+
### Regional compilation
82+
83+
Compiling the whole model usually has a big problem space for optimization. Models are often composed of multiple repeated blocks. [Regional compilation](https://pytorch.org/tutorials/recipes/regional_compilation.html) compiles the repeated block first (a transformer encoder block, for example), so that the Torch compiler would re-use its cached/optimized generated code for the other blocks, reducing (often massively) the cold start compilation time observed on the first inference call.
84+
85+
Enabling regional compilation might require simple yet intrusive changes to the
86+
modeling code. However, 🤗 Accelerate provides a utility [`compile_regions()`](https://huggingface.co/docs/accelerate/main/en/usage_guides/compilation#how-to-use-regional-compilation) which automatically compiles
87+
the repeated blocks of the provided `nn.Module` sequentially, and the rest of the model separately. This helps with reducing cold start time while keeping most (if not all) of the speedup you would get from full compilation.
88+
89+
```py
90+
# Make sure you're on the latest `accelerate`: `pip install -U accelerate`.
91+
from accelerate.utils import compile_regions
92+
93+
pipe.unet = compile_regions(pipe.unet, mode="reduce-overhead", fullgraph=True)
94+
```
95+
96+
As you may have noticed `compile_regions()` takes the same arguments as `torch.compile()`, allowing flexibility.
97+
8198
## Benchmark
8299

83100
We conducted a comprehensive benchmark with PyTorch 2.0's efficient attention implementation and `torch.compile` across different GPUs and batch sizes for five of our most used pipelines. The code is benchmarked on 🤗 Diffusers v0.17.0.dev0 to optimize `torch.compile` usage (see [here](https://github.com/huggingface/diffusers/pull/3313) for more details).

0 commit comments

Comments
 (0)