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

Add autoloading tutorial #3037

wants to merge 45 commits into from

Conversation

shink
Copy link
Contributor

@shink shink commented Sep 5, 2024

Fixes #3039

This PR adds a tutorial about the out-of-tree extension autoloading mechanism introduced in pytorch/pytorch#122468 and implemented in pytorch/pytorch#127074

cc: @jgong5 @Yikun @hipudding @FFFrog @albanD

Copy link

pytorch-bot bot commented Sep 5, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/3037

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@svekars
Copy link
Contributor

svekars commented Sep 5, 2024

Please make sure to review the Tutorial Submission Policy

Copy link
Contributor

@albanD albanD left a comment

Choose a reason for hiding this comment

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

Thanks for writing a tutorial on this. I think it is indeed a good topic to cover.
I think it would be good to take a step back and clarify who the audience is for this and focus the content on providing the information that audience is looking for.

In particular, my expectation is that the audience are the third party extension writers who should use this feature to improve seamless integration within PyTorch. In that case,
I will keep the intro at the top but make the "How to apply this to out-of-tree extensions the main section after that.

The How it works is covered in the RFC IIRC, maybe linking to it would be enough? I do agree that pointing out the env variable to disable this feature is a good thing to mention here though.

For the examples section, that makes sense to have this at the end as "example projects using this feature as well" but I would limit it to code pointers into how they did the integration on their github repos code. I don't think the code samples add much?

@shink
Copy link
Contributor Author

shink commented Sep 6, 2024

@svekars @albanD Thanks so much for your comments!

Please make sure to review the Tutorial Submission Policy

Oh I'm so sorry, I will make sure to review this policy and adhere to it.

Thanks for writing a tutorial on this. I think it is indeed a good topic to cover. I think it would be good to take a step back and clarify who the audience is for this and focus the content on providing the information that audience is looking for.

In particular, my expectation is that the audience are the third party extension writers who should use this feature to improve seamless integration within PyTorch. In that case, I will keep the intro at the top but make the "How to apply this to out-of-tree extensions the main section after that.

The How it works is covered in the RFC IIRC, maybe linking to it would be enough? I do agree that pointing out the env variable to disable this feature is a good thing to mention here though.

For the examples section, that makes sense to have this at the end as "example projects using this feature as well" but I would limit it to code pointers into how they did the integration on their github repos code. I don't think the code samples add much?

Thanks for your professional review! I totally agree with your suggestion. I'm making changes.

@shink shink marked this pull request as draft September 6, 2024 02:53
Copy link
Contributor

@albanD albanD left a comment

Choose a reason for hiding this comment

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

Re-shuffle looks good. Thanks for the quick update.
Only the TODO in the examples section but the rest sounds good to me at a high level. Will let @svekars review in case there is any finer grain issue needed.

@shink
Copy link
Contributor Author

shink commented Sep 9, 2024

@jgong5 Please review the HPU example section, thanks!

advanced_source/python_extension_autoload.rst Outdated Show resolved Hide resolved
advanced_source/python_extension_autoload.rst Outdated Show resolved Hide resolved
advanced_source/python_extension_autoload.rst Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

The diagram seems missing a pointer from import torch to the entry points to load - the loading of entry points is triggered by import torch, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, I will make some changes to this diagram

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jgong5 updated, please have a look

Comment on lines 68 to 83
`habana_frameworks.torch`_ is a Python package that enables users to run
PyTorch programs on Intel Gaudi via the PyTorch ``HPU`` device key.
``import habana_frameworks.torch`` is no longer necessary after this mechanism
is applied.

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

.. code-block:: diff

import torch
import torchvision.models as models
- import habana_frameworks.torch # <-- extra import
model = models.resnet50().eval().to("hpu")
input = torch.rand(128, 3, 224, 224).to("hpu")
output = model(input)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jgong5 should the implementation example of this mechanism be added here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, please leave it here for now. @bsochack is the habana bridge ready for autoloading?

Copy link

Choose a reason for hiding this comment

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

It is implemented and verified. Not yet released.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bsochack we need an implementation example here, can you provide one? Thanks!

Copy link

Choose a reason for hiding this comment

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

Any suggestion how it should be provided?

The only challenge that we had is the circular dependencies. We solved it in the way below. Other than that, it worked out of box.

habana_frameworks/init.py:

import os

is_loaded = False  # A member variable of habana_frameworks module to track if our module has been imported

def __autoload():
    # This is an entrypoint for pytorch autoload mechanism
    # If the following confition is true, that means our backend has already been loaded, either explicitly
    # or by the autoload mechanism and importing it again should be skipped to avoid circular imports
    global is_loaded
    if is_loaded:
        return
    import habana_frameworks.torch

habana_frameworks/torch/init.py:

import os

# This is to prevent torch autoload mechanism from causing circular imports
import habana_frameworks

habana_frameworks.is_loaded = True

and one addition in setup.py:

entry_points={
        "torch.backends": [
            "device_backend = habana_frameworks:__autoload",
        ],
},

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bsochack thanks so much for your quick reply!

The circular dependency issue is worth noting, I will state this in this doc.

I noticed habana_frameworks is not an open-source project, so are there any public links about the autoloading mechanism? It's OK if not.

Copy link

Choose a reason for hiding this comment

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

Right, habana_frameworks is not open source and there is no official release with the above change.
Btw, it requires PT 2.5 (not yet released) so it is hard to provide a real example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bsochack OK it's alright

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bsochack updated, please take a look at the HPU section

Choose a reason for hiding this comment

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

lgtm

@shink shink marked this pull request as ready for review September 10, 2024 08:40
@shink
Copy link
Contributor Author

shink commented Sep 11, 2024

@svekars it's ready for review, please take a look, thanks!

Copy link
Contributor

@svekars svekars left a comment

Choose a reason for hiding this comment

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

Sorry for the delay in reviewing this. I added editorial suggestion. Please make sure you have What you will learn and the Prerequisites sections - I added how they could look like in a comment. Let me know if you have any questions!

advanced_source/python_extension_autoload.rst Outdated Show resolved Hide resolved
advanced_source/python_extension_autoload.rst Show resolved Hide resolved
advanced_source/python_extension_autoload.rst Outdated Show resolved Hide resolved
advanced_source/python_extension_autoload.rst Outdated Show resolved Hide resolved
advanced_source/python_extension_autoload.rst Outdated Show resolved Hide resolved
advanced_source/python_extension_autoload.rst Outdated Show resolved Hide resolved
when loading the entry point you 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.

advanced_source/python_extension_autoload.rst Outdated Show resolved Hide resolved
advanced_source/python_extension_autoload.rst Outdated Show resolved Hide resolved
advanced_source/python_extension_autoload.rst Show resolved Hide resolved
shink and others added 21 commits September 14, 2024 09:48
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
@shink
Copy link
Contributor Author

shink commented Sep 14, 2024

@svekars Thanks so much for your professional and patient review! I made some changes which can be found at: https://github.com/pytorch/tutorials/pull/3037/files/4d44a78d348b918a5f6a3ff60f804b330e983546..e425fe90aeca2b734c390ddd4e0fadc1f0f715fd

@shink
Copy link
Contributor Author

shink commented Sep 18, 2024

Hi @svekars, just a gentle reminder about this PR. I'm hoping your review and thanks for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

💡 [REQUEST] - Tutorial on out-of-tree extension autoloading mechanism
6 participants