Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are common pitfalls when using torch.compile


What are common pitfalls when using torch.compile


When using `torch.compile`, several common pitfalls can arise:

1. Performance Variability: `torch.compile` does not always improve performance and can sometimes result in slower execution or increased memory usage. It's crucial to compare the performance of the compiled model against the original model[2][4].

2. Graph Breaks: If `torch.compile` encounters unsupported code or complex operations, it may introduce "graph breaks," splitting the code into optimized and unoptimized parts. While not fatal, these breaks can limit optimization benefits. Rewriting problematic sections can help[2][4].

3. Cryptic Error Messages: Compilation failures often produce cryptic error messages that are difficult to debug without assistance from the PyTorch team. This can make troubleshooting challenging[2][4].

4. Long Compilation Times: The compilation process itself can be time-consuming, sometimes taking several minutes or even longer for larger models. This makes it advisable to evaluate `torch.compile` towards the end of development rather than during initial stages[1][4].

5. Dynamic Shapes and Recompilation: `torch.compile` initially assumes static tensor shapes. If shapes change, recompilation may occur unless dynamic shapes are enabled. This can lead to slower initial runs due to recompilation[1].

6. Multithreading Issues: Currently, multithreading with `torch.compile` is buggy, which can limit its use in concurrent environments[3].

7. Unsupported Features: Some NumPy features or complex operations may not be supported by `torch.compile`, leading to compilation issues or graph breaks[5].

To mitigate these issues, it's recommended to apply `torch.compile` to high-level functions, use dynamic shapes when necessary, and thoroughly test the compiled model's performance compared to the original[1][2].

Citations:
[1] https://pytorch.org/docs/stable/torch.compiler_troubleshooting.html
[2] https://lightning.ai/docs/fabric/2.2.3/advanced/compile.html
[3] https://blog.ezyang.com/2024/11/ways-to-use-torch-compile/
[4] https://lightning.ai/docs/pytorch/stable/advanced/compile.html
[5] https://pytorch.org/docs/stable/torch.compiler_faq.html
[6] https://stackoverflow.com/questions/75886125/how-should-i-use-torch-compile-properly
[7] https://www.aionlinecourse.com/blog/how-should-i-use-torch-compile-properly
[8] https://github.com/patrick-kidger/jaxtyping/issues/196