Skip to content
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

Add autoloading tutorial #3037

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Changes from 21 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
ee33c2d
docs: add autoload
shink Sep 5, 2024
6215e6e
update
shink Sep 5, 2024
9f23f16
update
shink Sep 5, 2024
37c72ee
update
shink Sep 5, 2024
67ad7ff
update
shink Sep 5, 2024
22ddca1
Merge branch 'main' into docs/autoload
svekars Sep 5, 2024
891753e
update
shink Sep 6, 2024
54dd911
Merge branch 'main' into docs/autoload
shink Sep 6, 2024
5c9d78b
update
shink Sep 6, 2024
aa47a21
update
shink Sep 6, 2024
7caaad8
Merge branch 'pytorch:main' into docs/autoload
shink Sep 9, 2024
b4e884d
update
shink Sep 9, 2024
523d289
update
shink Sep 9, 2024
b3766ed
update
shink Sep 9, 2024
5d9adfb
update
shink Sep 9, 2024
5d36b03
Update advanced_source/python_extension_autoload.rst
shink Sep 9, 2024
9123d82
Update advanced_source/python_extension_autoload.rst
shink Sep 9, 2024
e726565
Update advanced_source/python_extension_autoload.rst
shink Sep 9, 2024
84879c6
update
shink Sep 10, 2024
6ac2d2e
update
shink Sep 10, 2024
5a0f00e
update
shink Sep 10, 2024
a85ebed
update
shink Sep 10, 2024
2db4cee
update
shink Sep 10, 2024
4d44a78
Merge branch 'main' into docs/autoload
shink Sep 10, 2024
d5fe718
add authors
shink Sep 14, 2024
b6281bf
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
a4ace51
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
3980ab7
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
dcb5fd3
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
93087be
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
a48cfc4
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
d1217dc
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
23cfef4
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
3c0c1e0
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
dda22c4
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
0a47d48
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
bcbe0f6
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
80c8683
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
2ba51d0
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
5ea5a36
Merge branch 'main' into docs/autoload
shink Sep 14, 2024
f8365e8
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
0cc9850
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
33c60cc
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
ee5c353
Update advanced_source/python_extension_autoload.rst
shink Sep 14, 2024
e425fe9
update
shink Sep 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 37 additions & 23 deletions advanced_source/python_extension_autoload.rst
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
Out-of-tree extension autoloading in Python
===========================================
Autoloading Out-of-Tree Extension
=================================

shink marked this conversation as resolved.
Show resolved Hide resolved
What is it?
-----------
**Author:** `Yuanhao Ji <https://github.com/shink>`__

The extension autoloading mechanism enables PyTorch to automatically
load out-of-tree backend extensions without explicit import statements. This
mechanism is very useful for users. On the one hand, it improves the user
experience and enables users to adhere to the familiar PyTorch device
programming model without needing to explicitly load or import device-specific
extensions. On the other hand, it facilitates effortless
feature is beneficial for users as it enhances their
experience and enables them to follow the familiar PyTorch device
programming model without having to explicitly load or import device-specific
extensions. Additionally, it facilitates effortless
adoption of existing PyTorch applications with zero-code changes on
out-of-tree devices. For further details, refer to the
`[RFC] Autoload Device Extension <https://github.com/pytorch/pytorch/issues/122468>`_.

shink marked this conversation as resolved.
Show resolved Hide resolved
.. grid:: 2

.. grid-item-card:: :octicon:`mortar-board;1em;` What you will learn
:class-card: card-prerequisites

* How to use out-of-tree extension autoloading in PyTorch
* Review examples with Intel Gaudi HPU, Huawei Ascend NPU

.. grid-item-card:: :octicon:`list-unordered;1em;` Prerequisites
:class-card: card-prerequisites

* PyTorch v2.5 or later

.. note::

This feature is enabled by default and can be disabled using
This feature is enabled by default and can be disabled by using
``export TORCH_DEVICE_BACKEND_AUTOLOAD=0``.
If you get an error like this: "Failed to load the backend extension",
this error is independent with PyTorch, you should disable this feature
Expand All @@ -26,14 +38,14 @@ How to apply this mechanism to out-of-tree extensions?
------------------------------------------------------

For instance, suppose you have a backend named ``foo`` and a corresponding package named ``torch_foo``. Ensure that
your package is compatible with PyTorch 2.5+ and includes the following snippet in its ``__init__.py`` file:
your package is compatible with PyTorch 2.5 or later and includes the following snippet in its ``__init__.py`` file:

.. code-block:: python

def _autoload():
print("No need to import torch_foo anymore! Check things are working with `torch.foo.is_available()`.")
print("Check things are working with `torch.foo.is_available()`.")

Then the only thing you need to do is define an entry point within your Python package:
Then, the only thing you need to do is define an entry point within your Python package:

.. code-block:: python

Expand All @@ -47,25 +59,25 @@ Then the only thing you need to do is define an entry point within your Python p
}
)

Now the ``torch_foo`` module can be imported when running import torch:
Now you can import the ``torch_foo`` module by simply adding the ``import torch`` statement without the need to add ``import torch_foo``:

.. code-block:: python

>>> import torch
No need to import torch_foo anymore! Check things are working with `torch.foo.is_available()`.
Check things are working with `torch.foo.is_available()`.
>>> torch.foo.is_available()
True

You may encounter issues with circular imports, the following examples are intended to help you address them.
In some cases, you might encounter issues with circular imports. The examples below demonstrate how you can address them.

Examples
^^^^^^^^

Here we take Intel Gaudi HPU and Huawei Ascend NPU as examples to determine how to
integrate your out-of-tree extension with PyTorch using the autoloading mechanism.
In this example, we will be using Intel Gaudi HPU and Huawei Ascend NPU to determine how to
integrate your out-of-tree extension with PyTorch using the autoloading feature.

`habana_frameworks.torch`_ is a Python package that enables users to run
PyTorch programs on Intel Gaudi via the PyTorch ``HPU`` device key.
PyTorch programs on Intel Gaudi by using the PyTorch ``HPU`` device key.

.. _habana_frameworks.torch: https://docs.habana.ai/en/latest/PyTorch/Getting_Started_with_PyTorch_and_Gaudi/Getting_Started_with_PyTorch.html

Expand Down Expand Up @@ -101,7 +113,7 @@ In ``habana_frameworks/init.py``, we use a global variable to track if our modul
return
import habana_frameworks.torch

In ``habana_frameworks/torch/init.py``, We prevent circular imports by updating the state of the global variable:
In ``habana_frameworks/torch/init.py``, we prevent circular imports by updating the state of the global variable:

.. code-block:: python

Expand Down Expand Up @@ -151,13 +163,13 @@ How it works
:alt: Autoloading implementation
:align: center

This mechanism is implemented based on Python's `Entry points
Autoloading is implemented based on Python's `Entrypoints
<https://packaging.python.org/en/latest/specifications/entry-points/>`_
mechanism. We discover and load all of the specific entry points
in ``torch/__init__.py`` that are defined by out-of-tree extensions.

As shown above, after installing ``torch_foo``, your Python module can be imported
when loading the entry point you defined, and then you can do some necessary work when
when loading the entrypoint that you have defined, and then you can do some necessary work when
calling it.

See the implementation in this pull request: `[RFC] Add support for device extension autoloading
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should link to a pull request. Maybe you can link to the file - it looks like the changes have landed already.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section talks about the implementation of autoloading, I think it makes sense to put a PR here.
PR can be used to let users know what has changed and to follow discussions about the implementation.

Expand All @@ -166,5 +178,7 @@ See the implementation in this pull request: `[RFC] Add support for device exten
Conclusion
----------

This tutorial has guided you through the out-of-tree extension autoloading
mechanism, including its usage and implementation.
In this tutorial, we learned about the out-of-tree extension autoloading mechanism in PyTorch, which automatically
loads backend extensions eliminating the need to add additional import statements. We also learned how to apply
this mechanism to out-of-tree extensions by defining an entry point and how to prevent circular imports.
We also reviewed an example on how to use the autoloading mechanism with Intel Gaudi HPU and Huawei Ascend NPU.