Debian Codegraph @

docs/source/usage_guides/compilation.md

raw
1 # Compilation
2
3 ## Overview
4
5 Pytorch 2.0 introduced `torch.compile`, a powerful feature that makes PyTorch code run faster by JIT-compiling PyTorch code into optimized kernels. Key features of `torch.compile` include:
6
7 - **Performance Improvement**: Significantly speeds up model execution by optimizing the computation graph.
8 - **Ease of Use**: Requires minimal code changes to implement, making it highly accessible.
9 - **Compatibility**: Works seamlessly with existing PyTorch code and models.
10
11 When used with Accelerate, `torch.compile` integrates smoothly into distributed training workflows, allowing you to benefit from both distributed execution and compilation optimizations simultaneously.
12
13 The first execution of compiled code typically takes longer as it includes the compilation time, but subsequent runs are significantly faster. For optimal performance in different scenarios, `torch.compile` offers various modes like `"default"`, `"reduce-overhead"` (which uses CUDA graphs to further reduce overhead), and `"max-autotune"` (which performs extensive autotuning to find the best kernels for your model).
14
15 ## Using `torch.compile` with Accelerate
16
17 Accelerate provides `TorchDynamoPlugin` for easy and seamless integration of `torch.compile` into your training scripts.
18
19 ```python
20 from accelerate import Accelerator
21 from accelerate.utils import TorchDynamoPlugin
22
23 # Configure the compilation backend
24 dynamo_plugin = TorchDynamoPlugin(
25 backend="inductor", # Options: "inductor", "aot_eager", "aot_nvfuser", etc.
26 mode="default", # Options: "default", "reduce-overhead", "max-autotune"
27 fullgraph=True,
28 dynamic=False
29 )
30
31 # Initialize accelerator with the plugin
32 accelerator = Accelerator(dynamo_plugin=dynamo_plugin)
33 # This will apply torch.compile to your model
34 model = accelerator.prepare(model)
35 ```
36
37 It is compatible with all other features and plugins of Accelerate, including mixed precision, distributed training (DDP, FSDP, Deepspeed), etc.
38
39 ## Regional Compilation
40
41 Instead of trying to compile the whole model, which usually has a big problem space for optimization. Regional compilation targets repeated blocks of the same class and compiles them sequentially to hit the compiler's cache. For example, in `GPT2LMHeadModel`, the repeated block/class is `GPT2Block`, and can be accessed as `model.transformer.h[0]`. The rest of the model (e.g model.lm_head) is compiled separately.
42
43 This allows us to speed up the compilation overhead / cold start of models like LLMs and Transformers in general.
44 See <https://pytorch.org/tutorials/recipes/regional_compilation.html> for more details.
45
46 ### How to Use Regional Compilation
47
48 It can be enabled by setting `use_regional_compilation=True` in the `TorchDynamoPlugin` configuration:
49
50 ```python
51 # Configure the compilation backend
52 dynamo_plugin = TorchDynamoPlugin(
53 use_regional_compilation=True,
54 ... # other parameters
55 )
56 # Initialize accelerator with the plugin
57 accelerator = Accelerator(dynamo_plugin=dynamo_plugin)
58 # This will apply compile_regions to your model
59 model = accelerator.prepare(model)
60 ```
61
62 You could also use the `accelerate.utils.compile_regions` utility directly the same way you would use `torch.compile`.
63
64 ### Benefits of Regional Compilation
65
66 We have conducted extensive benchmarks comparing full compilation and regional compilation using the `torch.compile` feature in PyTorch. The full results are available in the [accelerate repository](https://github.com/huggingface/accelerate/tree/main/benchmarks/torch.compile/regional_compilation). The key findings from our benchmarks are:
67
68 1. **Comparable Performance**: Regional compilation delivers performance speedups similar to full compilation, especially for larger models.
69 2. **Faster Compilation**: Regional compilation significantly reduces the time taken to compile models, making it a more efficient choice for deployment.
70 3. **Batch Size Impact**: The performance difference between compilation strategies diminishes with larger batch sizes, indicating that the overhead of compilation is less impactful in those scenarios.
71 4. **Model Size Consideration**: The benefits of regional compilation are more pronounced in larger models, where the compilation time savings can be substantial.
72 5. **Practical Application**: For real-world applications, regional compilation is a practical choice for optimizing training cold start times, especially when working with large models.
73
74 ## Conclusion
75
76 Both full and regional compilation can significantly speed up your models. Regional compilation offers a practical balance between compilation time and runtime performance, especially for training large models with substantial batch sizes.