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 option to register installed plugins automatically #6328

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,18 @@ def run(self):
'absl-py>=1.0.0',
'cloud-tpu-client>=0.10.0',
'pyyaml',
# importlib.metadata backport required for PJRT plugin discovery prior
# to Python 3.10
'importlib_metadata>=4.6;python_version<"3.10"',
],
package_data={
'torch_xla': ['lib/*.so*',],
},
entry_points={
'console_scripts': [
'stablehlo-to-saved-model = torch_xla.tf_saved_model_integration:main'
]
],
'torch_xla.plugins': ['tpu = torch_xla._internal.tpu:TpuPlugin',],
},
extras_require={
# On Cloud TPU VM install with:
Expand Down
6 changes: 6 additions & 0 deletions torch_xla/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,9 @@ def _init_xla_lazy_backend():
torch._dynamo.config.automatic_dynamic_shapes = False

from .stablehlo import save_as_stablehlo, save_torch_model_as_stablehlo

from .experimental import plugins

if os.getenv('XLA_REGISTER_INSTALLED_PLUGINS') == '1':
JackCaoG marked this conversation as resolved.
Show resolved Hide resolved
plugins.use_dynamic_plugins()
plugins.register_installed_plugins()
24 changes: 24 additions & 0 deletions torch_xla/experimental/plugins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import logging
import os
import sys

# TODO: delete this and just use importlib.metadata after we drop Python 3.9
# support.
if sys.version_info < (3, 10):
import importlib_metadata
else:
import importlib.metadata as importlib_metadata

import torch_xla
import torch_xla.core.xla_env_vars as xenv
Expand Down Expand Up @@ -65,3 +74,18 @@ def default() -> DevicePlugin:
def register_plugin(name: str, device_plugin: DevicePlugin):
_plugin_registry[name.upper()] = device_plugin
torch_xla._XLAC._register_pjrt_plugin(name, device_plugin.library_path())


def register_installed_plugins():
pjrt_entry_points = importlib_metadata.entry_points(group='torch_xla.plugins')
Copy link
Collaborator

@vanbasten23 vanbasten23 Jan 19, 2024

Choose a reason for hiding this comment

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

I see that you have 'torch_xla.plugins': ['tpu = torch_xla._internal.tpu:TpuPlugin',], in the setup.py. I wonder what this line does under the hood

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Entry points are weird. setuptools' docs have the best explanation that I've read.

for ep in pjrt_entry_points:
device_plugin_class = ep.load()

# HACK: TpuPlugin raises EnvironmentError if libtpu is not installed.
# TODO(wcromar): Decide if catching `EnvironmentError` is a permanent
# behavior or temporary hack.
try:
register_plugin(ep.name.upper(), device_plugin_class())
except EnvironmentError as e:
logging.warning(
"Failed to register plugin {}".format(ep.name), exc_info=e)
Loading