Skip to content

Commit 5c9d78b

Browse files
committed
update
1 parent 54dd911 commit 5c9d78b

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

advanced_source/python_extension_autoload.rst

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,50 @@ adoption of existing PyTorch applications with zero-code changes on
1414
out-of-tree devices. For more information,
1515
see `[RFC] Autoload Device Extension <https://github.com/pytorch/pytorch/issues/122468>`_.
1616

17+
.. note::
18+
19+
This feature is enabled by default and can be disabled using
20+
``export TORCH_DEVICE_BACKEND_AUTOLOAD=0``.
21+
If you get an error like this: "Failed to load the backend extension",
22+
this error has nothing to do with PyTorch, you should disable this feature
23+
and ask the out-of-tree extension maintainer for help.
24+
25+
How to apply this mechanism to out-of-tree extensions?
26+
--------------------------------------------
27+
28+
For example, if you have a backend named ``foo`` and a package named
29+
``torch_foo``. Make sure your package is based on PyTorch 2.5+ and includes
30+
the following in its ``__init__.py``:
31+
32+
.. code-block:: python
33+
34+
def _autoload():
35+
print("No need to import torch_foo anymore! You can run torch.foo.is_available() directly.")
36+
37+
Then the only thing you need to do is add an entry point to your Python
38+
package:
39+
40+
.. code-block:: python
41+
42+
setup(
43+
name="torch_foo",
44+
version="1.0",
45+
entry_points={
46+
"torch.backends": [
47+
"torch_foo = torch_foo:_autoload",
48+
],
49+
}
50+
)
51+
52+
Now the ``torch_foo`` module can be imported when running import torch:
53+
54+
.. code-block:: python
55+
56+
>>> import torch
57+
No need to import torch_foo anymore! You can run torch.foo.is_available() directly.
58+
>>> torch.foo.is_available()
59+
True
60+
1761
Examples
1862
^^^^^^^^
1963

@@ -63,49 +107,6 @@ in ``torch/__init__.py`` that are defined by out-of-tree extensions.
63107
Its implementation is in `[RFC] Add support for device extension autoloading
64108
<https://github.com/pytorch/pytorch/pull/127074>`_.
65109

66-
.. note::
67-
68-
This feature is enabled by default and can be disabled using
69-
``export TORCH_DEVICE_BACKEND_AUTOLOAD=0``.
70-
If you get an error like this: "Failed to load the backend extension",
71-
this error has nothing to do with PyTorch, you should disable this feature
72-
and ask the out-of-tree extension maintainer for help.
73-
74-
How to apply this to out-of-tree extensions?
75-
--------------------------------------------
76-
77-
For example, if you have a backend named ``foo`` and a package named
78-
``torch_foo``, and your package includes the following in its ``__init__.py``:
79-
80-
.. code-block:: python
81-
82-
def _autoload():
83-
print("No need to import torch_foo anymore! You can run torch.foo.is_available() directly.")
84-
85-
Then the only thing you need to do is add an entry point to your Python
86-
package.
87-
88-
.. code-block:: python
89-
90-
setup(
91-
name="torch_foo",
92-
version="1.0",
93-
entry_points={
94-
"torch.backends": [
95-
"torch_foo = torch_foo:_autoload",
96-
],
97-
}
98-
)
99-
100-
Now the ``torch_foo`` module can be imported when running import torch.
101-
102-
.. code-block:: python
103-
104-
>>> import torch
105-
No need to import torch_foo anymore! You can run torch.foo.is_available() directly.
106-
>>> torch.foo.is_available()
107-
True
108-
109110
Conclusion
110111
----------
111112

0 commit comments

Comments
 (0)