-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add a new tutorial for compile time caching #3277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -691,3 +691,7 @@ XPU | |
XPUs | ||
impl | ||
overrideable | ||
TorchServe | ||
Inductor’s | ||
onwards | ||
recompilations |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,76 @@ | ||||||||||
Compile Time Caching Configurations | ||||||||||
========================================================= | ||||||||||
**Authors:** `Oguz Ulgen <https://github.com/oulgen>`_ and `Sam Larsen <https://github.com/masnesral>`_ | ||||||||||
|
||||||||||
Introduction | ||||||||||
------------------ | ||||||||||
|
||||||||||
PyTorch Compiler implements several caches to reduce compilation latency. | ||||||||||
This recipe demonstrates how you can configure various parts of the caching in ``torch.compile``. | ||||||||||
|
||||||||||
Prerequisites | ||||||||||
------------------- | ||||||||||
|
||||||||||
Before starting this recipe, make sure that you have the following: | ||||||||||
|
||||||||||
* Basic understanding of ``torch.compile``. See: | ||||||||||
|
||||||||||
* `torch.compiler API documentation <https://pytorch.org/docs/stable/torch.compiler.html#torch-compiler>`__ | ||||||||||
* `Introduction to torch.compile <https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html>`__ | ||||||||||
* `Compile Time Caching in torch.compile <https://pytorch.org/tutorials/recipes/torch_compile_caching_tutorial.html>`__ | ||||||||||
|
||||||||||
* PyTorch 2.4 or later | ||||||||||
|
||||||||||
Inductor Cache Settings | ||||||||||
---------------------------- | ||||||||||
|
||||||||||
Most of these caches are in-memory, only used within the same process, and are transparent to the user. An exception is caches that store compiled FX graphs (``FXGraphCache``, ``AOTAutogradCache``). These caches allow Inductor to avoid recompilation across process boundaries when it encounters the same graph with the same Tensor input shapes (and the same configuration). The default implementation stores compiled artifacts in the system temp directory. An optional feature also supports sharing those artifacts within a cluster by storing them in a Redis database. | ||||||||||
|
||||||||||
There are a few settings relevant to caching and to FX graph caching in particular. | ||||||||||
The settings are accessible via environment variables listed below or can be hard-coded in Inductor’s config file. | ||||||||||
|
The settings are accessible via environment variables listed below or can be hard-coded in Inductor’s config file. | |
The settings are accessible via environment variables listed below or can be hard-coded in the Inductor’s config file. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This setting enables the local FX graph cache feature, i.e., by storing artifacts in the host’s temp directory. ``1`` enables, and any other value disables it. By default, the disk location is per username, but users can enable sharing across usernames by specifying ``TORCHINDUCTOR_CACHE_DIR`` (below). | |
This setting enables the local FX graph cache feature, which stores artifacts in the host's temporary directory. Setting it to ``1`` enables, enables the feature, while any other value disables it. By default, the disk location is per username, but users can enable sharing across usernames by specifying ``TORCHINDUCTOR_CACHE_DIR`` (below). |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This setting extends FXGraphCache to store cached results at the AOTAutograd level, instead of at the Inductor level. ``1`` enables, and any other value disables it. | |
This setting extends ``FXGraphCache`` to store cached results at the ``AOTAutograd`` level, rather than at the Inductor level. Setting it to ``1`` enables this feature, while any other value disables it. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`TORCHINDUCTOR_AUTOGRAD_CACHE` requires `TORCHINDUCTOR_FX_GRAPH_CACHE` to work. The same cache dir stores cache entries for ``AOTAutogradCache`` (under `{TORCHINDUCTOR_CACHE_DIR}/aotautograd`) and ``FXGraphCache`` (under `{TORCHINDUCTOR_CACHE_DIR}/fxgraph`). | |
``TORCHINDUCTOR_AUTOGRAD_CACHE`` requires ``TORCHINDUCTOR_FX_GRAPH_CACHE`` to work. The same cache dir stores cache entries for ``AOTAutogradCache`` (under ``{TORCHINDUCTOR_CACHE_DIR}/aotautograd``) and ``FXGraphCache`` (under ``{TORCHINDUCTOR_CACHE_DIR}/fxgraph``). |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that if Inductor locates a remote cache entry, it stores the compiled artifact in the local on-disk cache; that local artifact would be served on subsequent runs on the same machine. | |
.. note:: | |
If Inductor locates a remote cache entry, it stores the compiled artifact in the local on-disk cache. That local artifact would be served on subsequent runs on the same machine. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like ``TORCHINDUCTOR_FX_GRAPH_REMOTE_CACHE``, this setting enables the remote ``AOTAutogradCache`` feature. The current implementation uses Redis. ``1`` enables caching, and any other value disables it. The following environment variables configure the host and port of the ``Redis`` server: | |
Similar to ``TORCHINDUCTOR_FX_GRAPH_REMOTE_CACHE``, this setting enables the remote ``AOTAutogradCache`` feature. The current implementation uses Redis. Setting it to ``1`` enables caching, while any other value disables it. The following environment variables are used to configure the host and port of the Redis server: | |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
``TORCHINDUCTOR_REDIS_HOST`` (defaults to ``localhost``) | |
* ``TORCHINDUCTOR_REDIS_HOST`` (defaults to ``localhost``) |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
``TORCHINDUCTOR_REDIS_PORT`` (defaults to ``6379``) | |
* ``TORCHINDUCTOR_REDIS_PORT`` (defaults to ``6379``) |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`TORCHINDUCTOR_AUTOGRAD_REMOTE_CACHE`` depends on ``TORCHINDUCTOR_FX_GRAPH_REMOTE_CACHE`` to be enabled to work. The same Redis server can store both AOTAutograd and FXGraph cache results. | |
``TORCHINDUCTOR_AUTOGRAD_REMOTE_CACHE`` requires ``TORCHINDUCTOR_FX_GRAPH_REMOTE_CACHE`` to be enabled in order to function. The same Redis server can be used to store both AOTAutograd and FXGraph cache results. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This setting enables a remote cache for ``TorchInductor``’s autotuner. As with the remote FX graph cache, the current implementation uses Redis. ``1`` enables caching, and any other value disables it. The same host / port environment variables listed above apply to this cache. | |
This setting enables a remote cache for ``TorchInductor``’s autotuner. Similar to the remote FX graph cache, the current implementation uses Redis. Setting it to ``1`` enables caching, while any other value disables it. The same host and port environment variables mentioned above apply to this cache. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,12 +1,16 @@ | ||||||
Compile Time Caching in ``torch.compile`` | ||||||
========================================================= | ||||||
**Authors:** `Oguz Ulgen <https://github.com/oulgen>`_ and `Sam Larsen <https://github.com/masnesral>`_ | ||||||
**Author:** `Oguz Ulgen <https://github.com/oulgen>`_ | ||||||
|
||||||
Introduction | ||||||
------------------ | ||||||
|
||||||
PyTorch Inductor implements several caches to reduce compilation latency. | ||||||
This recipe demonstrates how you can configure various parts of the caching in ``torch.compile``. | ||||||
PyTorch Compiler provides several caching offerings to reduce compilation latency. | ||||||
oulgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
This recipe will explain these offerings in detail to help users pick the best option for their use case. | ||||||
|
||||||
Check out `Compile Time Caching Configurations <https://pytorch.org/tutorials/recipes/torch_compile_caching_configuration_tutorial.html>` for how to configure these caches. | ||||||
|
||||||
Also check out our caching benchmark at `PT CacheBench Benchmarks <https://hud.pytorch.org/benchmark/llms?repoName=pytorch%2Fpytorch&benchmarkName=TorchCache+Benchmark>`. | ||||||
|
||||||
Prerequisites | ||||||
------------------- | ||||||
|
@@ -17,60 +21,83 @@ Before starting this recipe, make sure that you have the following: | |||||
|
||||||
* `torch.compiler API documentation <https://pytorch.org/docs/stable/torch.compiler.html#torch-compiler>`__ | ||||||
* `Introduction to torch.compile <https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html>`__ | ||||||
* `Triton language documentation <https://triton-lang.org/main/index.html>`__ | ||||||
|
||||||
* PyTorch 2.4 or later | ||||||
|
||||||
Inductor Cache Settings | ||||||
---------------------------- | ||||||
Caching Offerings | ||||||
--------------------- | ||||||
|
||||||
``torch.compile`` provides the following caching offerings: | ||||||
|
||||||
* End to end caching (also known as ``Mega-Cache``) | ||||||
* Modular caching of ``TorchDynamo``, ``TorchInductor``, and ``Triton`` | ||||||
|
||||||
It is important to note that caching validates that the cache artifacts are used with the same PyTorch and Triton version, as well as, same GPU when device is set to be cuda. | ||||||
|
||||||
``torch.compile`` end-to-end caching (a.k.a. ``Mega-Cache``) | ||||||
|
``torch.compile`` end-to-end caching (a.k.a. ``Mega-Cache``) | |
``torch.compile`` end-to-end caching |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
``Mega-Cache`` provides two compiler APIs | |
``Mega-Cache`` provides two compiler APIs: |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An example to this is as follows. First, compile and save the cache artifacts. | |
Consider the following example. First, compile and save the cache artifacts: |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Later, the user can jump-start their cache by the following. | |
Later, you can jump-start the cache by the following: |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above described MegaCache is also compromised of individual components that can be used without any user intervention. By default, PyTorch Compiler comes with local on-disk caches for ``TorchDynamo``, ``TorchInductor``, and ``Triton``. These caches are as following. | |
The aforementioned ``MegaCache``is composed of individual components that can be used without any user intervention. By default, PyTorch Compiler comes with local on-disk caches for ``TorchDynamo``, ``TorchInductor``, and ``Triton``. These caches include: |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* ``FXGraphCache``: cache of graph-based IR components used in compilation | |
* ``FXGraphCache``: A cache of graph-based IR components used in compilation. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* ``Triton Cache``: cache of Triton-compilation results (``cubin`` files generated by ``Triton`` as well as other caching artifacts) | |
* ``Triton Cache``: A cache of Triton-compilation results, including ``cubin`` files generated by Triton and other caching artifacts. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* ``InductorCache``: bundling of ``FXGraphCache`` and ``Triton`` cache | |
* ``InductorCache``: A bundle of ``FXGraphCache`` and ``Triton`` cache. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* ``AOTAutogradCache``: caching of joint graph artifacts | |
* ``AOTAutogradCache``: A cache of joint graph artifacts. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* ``PGO-cache``: cache of dynamic shape decisions to reduce number of recompilations | |
* ``PGO-cache``: A cache of dynamic shape decisions to reduce number of recompilations. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also provide a remote caching option for users who would like to take advantage of a ``Redis`` based cache. Check out `Compile Time Caching Configurations <https://pytorch.org/tutorials/recipes/torch_compile_caching_configuration_tutorial.html>` to learn more about how to enable the ``Redis`` based caching. | |
We also provide a remote caching option for users who would like to take advantage of a Redis-based cache. Check out `Compile Time Caching Configurations <https://pytorch.org/tutorials/recipes/torch_compile_caching_configuration_tutorial.html>`__ to learn more about how to enable the Redis-based caching. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.