Debian Codegraph @

benchmarks/torch.compile/README.md

raw
1 # Regional Compilation Benchmark
2
3 This benchmark compares different compilation strategies using PyTorch's `torch.compile` and Accelerate's `compile_regions` utility, which is based on the recipe in [PyTorch documentation](https://pytorch.org/tutorials/recipes/regional_compilation.html).
4
5 ## Overview
6
7 The benchmark evaluates three approaches:
8
9 - **Baseline**: No compilation, standard PyTorch eager execution.
10 - **Full compilation**: Using PyTorch's `torch.compile()` on the entire model.
11 - **Regional compilation**: Using `accelerate.utils.compile_regions()` which targets specific blocks of the model to optimize compilation time.
12
13 Each approach is tested with different batch sizes (1 and 4) and sequence lengths (128) on various LLaMA-based models ranging from 1B to 13B parameters. We purposefully run the forward pass outside of the `torch.no_grad()` context to simulate performance in a training environment, where gradients are needed.
14
15 ## Usage
16
17 To run this benchmark:
18
19 ```bash
20 python regional_compilation.py
21 ```
22
23 The script will automatically download the model configurations, create models, and benchmark both compilation and inference times across different scenarios.
24
25 ## Requirements
26
27 - Suitable GPU memory for the models being tested.
28 - PyTorch with CUDA support.
29 - Transformers library.
30 - Accelerate library.
31
32 ## Results
33
34 The benchmark results are summarized in the following figures:
35
36 - Compilation time is how long it takes to run the first forward pass.
37 - Speedup factor is the ratio of non-compiled baseline inference time to the fully/regionally compiled inference time.
38
39 <p align="center">
40 <img src="imgs/compilation_time.png" width="80%" alt="Compilation Time">
41 </p>
42 <p align="center">
43 <img src="imgs/speedup_factor.png" width="80%" alt="Speedup Factor">
44 </p>
45
46 Full results are available in the tables below:
47
48 ```markdown
49 [-------------------------------------------------- NousResearch/Llama-3.2-1B ---------------------------------------------------]
50 | Inference time (1x128) | Inference time (4x128) | Compile time (1x128) | Compile time (4x128)
51 1 threads: -----------------------------------------------------------------------------------------------------------------------
52 Baseline | 18.3 | 18.4 | |
53 Full compilation | 6.3 | 10.0 | 10696.4 | 10248.0
54 Regional compilation | 9.7 | 10.0 | 1952.7 | 2903.9
55
56 Times are in milliseconds (ms).
57
58 [---------------------------------------------- NousResearch/Hermes-3-Llama-3.2-3B ----------------------------------------------]
59 | Inference time (1x128) | Inference time (4x128) | Compile time (1x128) | Compile time (4x128)
60 1 threads: -----------------------------------------------------------------------------------------------------------------------
61 Baseline | 33.4 | 33.6 | |
62 Full compilation | 11.2 | 23.9 | 17857.5 | 17736.5
63 Regional compilation | 17.3 | 23.7 | 2993.2 | 2478.8
64
65 Times are in milliseconds (ms).
66
67 [---------------------------------------------- NousResearch/Hermes-3-Llama-3.1-8B ----------------------------------------------]
68 | Inference time (1x128) | Inference time (4x128) | Compile time (1x128) | Compile time (4x128)
69 1 threads: -----------------------------------------------------------------------------------------------------------------------
70 Baseline | 40.3 | 59.5 | |
71 Full compilation | 18.9 | 54.4 | 20437.8 | 20152.3
72 Regional compilation | 19.7 | 54.0 | 2903.1 | 2438.0
73
74 Times are in milliseconds (ms).
75
76 [--------------------------------------------- NousResearch/Nous-Hermes-Llama2-13b ----------------------------------------------]
77 | Inference time (1x128) | Inference time (4x128) | Compile time (1x128) | Compile time (4x128)
78 1 threads: -----------------------------------------------------------------------------------------------------------------------
79 Baseline | 45.5 | 100.4 | |
80 Full compilation | 29.4 | 89.7 | 23099.4 | 22885.9
81 Regional compilation | 29.4 | 87.5 | 2945.5 | 2526.2
82
83 Times are in milliseconds (ms).
84 ```
85
86 ## Results Summary
87
88 ### Compilation Time
89
90 Regional compilation provides significantly faster compilation times compared to full model compilation:
91
92 - **Full compilation**: Takes ~10-23 seconds depending on model size.
93 - **Regional compilation**: Takes only ~2-3 seconds across all model sizes.
94 - **Speed improvement**: Regional compilation is **5-9x faster** to compile.
95
96 ### Inference Time
97
98 Regional compilation delivers inference performance close to full compilation:
99
100 - For batch size 1:
101 - For smaller models (1B-3B): Full compilation has a slight edge over regional compilation.
102 - For larger models (8B-13B): Regional compilation performs similarly to full compilation.
103 - For batch size 4: Regional compilation performs similarly to full compilation across all models.
104
105 ## Key Takeaways
106
107 1. **Comparable Performance**: Regional compilation delivers performance speedups similar to full compilation, especially for larger models.
108 2. **Faster Compilation**: Regional compilation significantly reduces the time taken to compile models, making it a more efficient choice for deployment.
109 3. **Batch Size Impact**: At batch size 4, full compilation and regional compilation perform nearly identically.
110 4. **Model Size Impact**: Even with a small batch size, full compilation and regional compilation perform similarly for larger models (8B-13B).
111 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.