From 502bb910294307d310a520213a4f9b0a484de007 Mon Sep 17 00:00:00 2001 From: Oguz Ulgen Date: Thu, 20 Jun 2024 13:23:12 -0700 Subject: [PATCH 1/3] Add tutorial about inductor caching [ghstack-poisoned] --- .../torch_compile_caching_tutorial.rst | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 recipes_source/torch_compile_caching_tutorial.rst diff --git a/recipes_source/torch_compile_caching_tutorial.rst b/recipes_source/torch_compile_caching_tutorial.rst new file mode 100644 index 00000000000..3a7bd213bce --- /dev/null +++ b/recipes_source/torch_compile_caching_tutorial.rst @@ -0,0 +1,55 @@ +Compile Time Caching in ``torch.compile`` +========================================================= +**Author:** `Oguz Ulgen `_ and `Sam Larsen `_ + +Introduction +------------------ + +PyTorch Inductor implements several caches to reduce compilation latency. These caches are transparent to the user. +This recipes demonstrates how you to 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 `__ + * `Introduction to torch.compile `__ + +* 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 the FX graph cache that stores compiled FX graphs. This cache allows Inductor to avoid recompilation across process boundaries when it encounters the same graph with the same Tensor input shapes (and the same configuration, etc.). 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 Redis. + +There are a few settings relevant to caching and to FX graph caching in particular. The settings are accessible via environment variables, or can be hard-coded in Inductor’s config file. + +TORCHINDUCTOR_FX_GRAPH_CACHE +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This setting enables the local FX graph cache feature, i.e., by storing artifacts on the host’s temp directory. ``1`` enables, and any other value disables. By default, the disk location is per username, but users can enable sharing across usernames by specifying ``TORCHINDUCTOR_CACHE_DIR`` (below). + +TORCHINDUCTOR_CACHE_DIR +~~~~~~~~~~~~~~~~~~~~~~~~ +This setting specifies the location of all on-disk caches. By default, the location is in the system temp directory under ``torchinductor_``, e.g., ``/tmp/torchinductor_myusername``. + +Note that if ``TRITON_CACHE_DIR`` is not set in the environment, Inductor sets the Triton cache directory to this same temp location (under the Triton subdirectory). + +TORCHINDUCTOR_FX_GRAPH_REMOTE_CACHE +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This setting enables the remote FX graph cache feature. The current implementation uses Redis. ``1`` enables, and any other value disables. The following environment variables configure the host and port of the Redis server: + +``TORCHINDUCTOR_REDIS_HOST`` (defaults to ``localhost``) +``TORCHINDUCTOR_REDIS_PORT`` (defaults to ``6379``) + +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. + +TORCHINDUCTOR_AUTOTUNE_REMOTE_CACHE +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This setting enables a remote cache for Inductor’s autotuner. As with the remote FX graph cache, the current implementation uses Redis. ``1`` enables, and any other value disables. The same host / port environment variables listed above apply to this cache. + +TORCHINDUCTOR_FORCE_DISABLE_CACHES +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Set this value to ``1`` to disable all Inductor caching. This setting is useful to, e.g., experiment with cold-start compile time, or to force recompilation for debugging purposes. From c634774300a31ee406cb3f462d0a99fc9fa85f80 Mon Sep 17 00:00:00 2001 From: Oguz Ulgen Date: Thu, 20 Jun 2024 13:27:18 -0700 Subject: [PATCH 2/3] Update on "Add tutorial about inductor caching" [ghstack-poisoned] --- recipes_source/recipes_index.rst | 9 +++++++++ recipes_source/torch_compile_caching_tutorial.rst | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/recipes_source/recipes_index.rst b/recipes_source/recipes_index.rst index 51b2880a16d..7d92fa14c0b 100644 --- a/recipes_source/recipes_index.rst +++ b/recipes_source/recipes_index.rst @@ -317,6 +317,15 @@ Recipes are bite-sized, actionable examples of how to use specific PyTorch featu :link: ../recipes/torch_compile_user_defined_triton_kernel_tutorial.html :tags: Model-Optimization +.. Compile Time Caching in ``torch.compile`` + +.. customcarditem:: + :header: Compile Time Caching in ``torch.compile`` + :card_description: Learn how to configure compile time caching in ``torch.compile`` + :image: ../_static/img/thumbnails/cropped/generic-pytorch-logo.png + :link: ../recipes/torch_compile_caching_tutorial.html + :tags: Model-Optimization + .. Intel(R) Extension for PyTorch* .. customcarditem:: diff --git a/recipes_source/torch_compile_caching_tutorial.rst b/recipes_source/torch_compile_caching_tutorial.rst index 3a7bd213bce..7145ee74f4a 100644 --- a/recipes_source/torch_compile_caching_tutorial.rst +++ b/recipes_source/torch_compile_caching_tutorial.rst @@ -1,6 +1,6 @@ Compile Time Caching in ``torch.compile`` ========================================================= -**Author:** `Oguz Ulgen `_ and `Sam Larsen `_ +**Authors:** `Oguz Ulgen `_ and `Sam Larsen `_ Introduction ------------------ @@ -21,7 +21,7 @@ Before starting this recipe, make sure that you have the following: * 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 the FX graph cache that stores compiled FX graphs. This cache allows Inductor to avoid recompilation across process boundaries when it encounters the same graph with the same Tensor input shapes (and the same configuration, etc.). 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 Redis. From e6b6dd23491791f644cdecd4ac32b49132dad946 Mon Sep 17 00:00:00 2001 From: Oguz Ulgen Date: Thu, 20 Jun 2024 15:00:43 -0700 Subject: [PATCH 3/3] Update on "Add tutorial about inductor caching" cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 ColinPeppler amjames desertfire chauhang [ghstack-poisoned] --- .../torch_compile_caching_tutorial.rst | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/recipes_source/torch_compile_caching_tutorial.rst b/recipes_source/torch_compile_caching_tutorial.rst index 7145ee74f4a..16f7ee0d183 100644 --- a/recipes_source/torch_compile_caching_tutorial.rst +++ b/recipes_source/torch_compile_caching_tutorial.rst @@ -6,7 +6,7 @@ Introduction ------------------ PyTorch Inductor implements several caches to reduce compilation latency. These caches are transparent to the user. -This recipes demonstrates how you to configure various parts of the caching in ``torch.compile``. +This recipe demonstrates how you can configure various parts of the caching in ``torch.compile``. Prerequisites ------------------- @@ -23,9 +23,10 @@ Before starting this recipe, make sure that you have the following: 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 the FX graph cache that stores compiled FX graphs. This cache allows Inductor to avoid recompilation across process boundaries when it encounters the same graph with the same Tensor input shapes (and the same configuration, etc.). 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 Redis. +Most of these caches are in-memory, only used within the same process, and are transparent to the user. An exception is the FX graph cache that stores compiled FX graphs. This cache allows 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, or can be hard-coded in Inductor’s config file. +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 the Inductor’s config file. TORCHINDUCTOR_FX_GRAPH_CACHE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -33,13 +34,13 @@ This setting enables the local FX graph cache feature, i.e., by storing artifact TORCHINDUCTOR_CACHE_DIR ~~~~~~~~~~~~~~~~~~~~~~~~ -This setting specifies the location of all on-disk caches. By default, the location is in the system temp directory under ``torchinductor_``, e.g., ``/tmp/torchinductor_myusername``. +This setting specifies the location of all on-disk caches. By default, the location is in the system temp directory under ``torchinductor_``, for example, ``/tmp/torchinductor_myusername``. -Note that if ``TRITON_CACHE_DIR`` is not set in the environment, Inductor sets the Triton cache directory to this same temp location (under the Triton subdirectory). +Note that if ``TRITON_CACHE_DIR`` is not set in the environment, Inductor sets the Triton cache directory to this same temp location, under the Triton subdirectory. TORCHINDUCTOR_FX_GRAPH_REMOTE_CACHE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -This setting enables the remote FX graph cache feature. The current implementation uses Redis. ``1`` enables, and any other value disables. The following environment variables configure the host and port of the Redis server: +This setting enables the remote FX graph cache feature. The current implementation uses Redis. ``1`` enables cache, and any other value disables. The following environment variables configure the host and port of the Redis server: ``TORCHINDUCTOR_REDIS_HOST`` (defaults to ``localhost``) ``TORCHINDUCTOR_REDIS_PORT`` (defaults to ``6379``) @@ -52,4 +53,9 @@ This setting enables a remote cache for Inductor’s autotuner. As with the remo TORCHINDUCTOR_FORCE_DISABLE_CACHES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Set this value to ``1`` to disable all Inductor caching. This setting is useful to, e.g., experiment with cold-start compile time, or to force recompilation for debugging purposes. +Set this value to ``1`` to disable all Inductor caching. This setting is useful for tasks like experimenting with cold-start compile times or forcing recompilation for debugging purposes. + +Conclusion +------------- +In this recipe, we have learned that PyTorch Inductor's caching mechanisms significantly reduce compilation latency by utilizing both local and remote caches, which operate seamlessly in the background without requiring user intervention. +Additionally, we explored the various settings and environment variables that allow users to configure and optimize these caching features according to their specific needs.