[RFC] Add support for device extension autoloading#127074
[RFC] Add support for device extension autoloading#127074shink wants to merge 31 commits intopytorch:mainfrom
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/127074
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 45fd7ad with merge base 6b5fbc5 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Thanks for the pull request. Could you please add some tests? |
|
cc @bsochack |
Sure, I'm willing to do this! |
|
@shink , @jgong5 This is a nice work that directly reflect proposal (RFC). We have tried to test it against our device_extension and there was a crash : Problem seems to be that torch._dynamo is having a code checking all symbols exported from torch/init.py . |
|
@bsochack @jczaja Nice work! Thanks so much!
Yes, we alse met this crash. |
|
@jgong5 @bsochack @bkowalskiINTEL @fffrog @hipudding Please have a look at this unit test. See b0e08d3 |
There was a problem hiding this comment.
in fact, this file should be ignored, and the reason it is here is to declare the entry point of the backend package
There was a problem hiding this comment.
I think it would be better to show failure information when an exception is encountered.
There was a problem hiding this comment.
getenv with default value shoule be better.
There was a problem hiding this comment.
It would be better to think of this path operation as context.
There was a problem hiding this comment.
fxied, please take a look again
Please add @bkowalskiINTEL and @jczaja as co-authors. Lets also continue all addressing code review here. |
Sure, thanks so much, and any suggestions are welcome. |
There was a problem hiding this comment.
Besides checking the environment variable, are you going to add test to autoload an extension (maybe a mock one)?
There was a problem hiding this comment.
Thanks for your review. Please have a look at the code I just updated.
There was a problem hiding this comment.
Would it be possible to reuse the existing modules we already have for testing in test/cpp_extension/setup.py to test this without having to create a brand new extension?
There was a problem hiding this comment.
Thank you so much for your review. I'm testing this idea now.
There was a problem hiding this comment.
Could you please have a look at this commit 1e7cf76
There was a problem hiding this comment.
I'm not sure what this is testing?
There was a problem hiding this comment.
it's testing a function defined in the backend extension
|
@shink Seems there is still lint errors. You may repro with "lintrunner -a" locally. |
There was a problem hiding this comment.
Should we do things inside this entrypoint instead to make sure it is called?
There was a problem hiding this comment.
Not sure if I understand it correctly but when the cpp extension is explicitly imported from this test file, not autoloaded implicitly. Is that correct?
There was a problem hiding this comment.
The reason for putting this test case here is that I want to reuse the existing modules in test/cpp_extension/setup.py.
The extension is installed here:
Lines 667 to 671 in 7efaeb1
| """ | ||
| Whether autoloading out-of-the-tree device extensions is enabled. | ||
| The switch depends on the value of the environment variable | ||
| `TORCH_DEVICE_BACKEND_AUTOLOAD`. |
There was a problem hiding this comment.
@drisspg which .rst file should we add this new env variable to for proper documentation?
There was a problem hiding this comment.
The main env var RST is here:https://github.com/pytorch/pytorch/blob/main/docs/source/torch_environment_variables.rst
and they branch out to others
Co-authored-by: albanD <desmaison.alban@gmail.com>
|
@albanD any comments to this PR? |
albanD
left a comment
There was a problem hiding this comment.
Small nit on moving the doc to another file, but SGTM otherwise!
| miscellaneous_environment_variables | ||
| logging | ||
| torch_nccl_environment_variables | ||
| privateuse1_environment_variables |
There was a problem hiding this comment.
Could you please move this to the miscellaneous section? I do expect all users to be interested in this env variable, not just privateuse1 devs.
There was a problem hiding this comment.
@albanD Sure, it has been moved. Please take a look again. Thanks so much for your patient review.
|
@pytorchbot merge |
Merge failedReason: This PR needs a If not, please add the To add a label, you can comment to pytorchbot, for example For more information, see Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Fixes pytorch#122468 - Load device extensions at the end of `torch/__init__.py` - Enabled by default, or you can disable it with `TORCH_DEVICE_BACKEND_AUTOLOAD=0` run test: ```python python test/run_test.py -i test_autoload_enable python test/run_test.py -i test_autoload_disable ``` doc: https://docs-preview.pytorch.org/pytorch/pytorch/127074/miscellaneous_environment_variables.html co-author: @jgong5 @bsochack @bkowalskiINTEL @jczaja @fffrog @hipudding Co-authored-by: albanD <desmaison.alban@gmail.com> Co-authored-by: Jiong Gong <jiong.gong@intel.com> Pull Request resolved: pytorch#127074 Approved by: https://github.com/albanD, https://github.com/jgong5
This commit adds support of out-of-tree plugins autoloading using python package entry point specification: * https://packaging.python.org/en/latest/specifications/entry-points/#entry-points * https://packaging.python.org/en/latest/guides/creating-and-discovering-plugins/#using-package-metadata Out-of-tree plugins must register entry points as `torchcodec.backends`: ``` [project.entry-points.'torchcodec.backends'] device_backend = 'torchcodec_plugin:load_plugin' ``` Torchcodec will automatically load plugins if discovered. Loading can be explicitly suppressed with `TORCHCODEC_DEVICE_BACKEND_AUTOLOAD=1` environment variable. The same approach is being used to load PyTorch device backends. See: * pytorch/pytorch#127074 Signed-off-by: Dmitry Rogozhkin <dmitry.v.rogozhkin@intel.com>


Fixes #122468
torch/__init__.pyTORCH_DEVICE_BACKEND_AUTOLOAD=0run test:
doc:
https://docs-preview.pytorch.org/pytorch/pytorch/127074/miscellaneous_environment_variables.html
co-author: @jgong5 @bsochack @bkowalskiINTEL @jczaja @fffrog @hipudding
cc @albanD