Skip to content

Add custom class pt2 tutorial #3431

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add custom class pt2 tutorial #3431

wants to merge 2 commits into from

Conversation

angelayi
Copy link
Contributor

@angelayi angelayi commented Jul 7, 2025

Copy link

pytorch-bot bot commented Jul 7, 2025

🔗 Helpful Links

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

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

✅ No Failures

As of commit ba48f42 with merge base 8476a99 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@angelayi angelayi force-pushed the angelayi/torchbind branch from 06880b9 to be8b159 Compare July 8, 2025 15:41
@angelayi angelayi requested review from zou3519 and ydwu4 July 8, 2025 15:41
@angelayi angelayi marked this pull request as ready for review July 8, 2025 15:41

import torch

torch.ops.load_library("//caffe2/test:test_torchbind_cpp_impl")
Copy link
Contributor

Choose a reason for hiding this comment

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

is this internal stuff?

Copy link
Contributor

Choose a reason for hiding this comment

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

probably, I hope the OSS buck/bazel target for torch isn't caffe2 lol

Copy link
Contributor

Choose a reason for hiding this comment

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

So it would be nice if we had some sample code someone could download that had the C++ extension set up with it. Then it would be clearer exactly what needs to be loaded (via load_library, or an import statement). But I'm happy to ship some version of this as-is without that because it already helps alot

@zou3519 zou3519 added the torch.compile Torch compile and other relevant tutorials label Jul 8, 2025
However, it may be difficult for users to write a fake class: the original class
uses some third-party library that determines the output shape of the methods,
or is complicated and written by others. Besides, users may not care about the
limitations listed above. In this case, please reach out to us!
Copy link

Choose a reason for hiding this comment

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

Can also mention users can define x.tracing_mode(), which returns "real" so as to skip fakification.

@ydwu4
Copy link

ydwu4 commented Jul 9, 2025

Thanks for porting this tutorial! Looks good to me.

This tutorial is a follow-on to the
:doc:`custom C++ classes <torch_script_custom_classes>` tutorial, and
introduces additional steps that are needed to support custom C++ classes in
PyTorch 2.
Copy link
Contributor

@zou3519 zou3519 Jul 10, 2025

Choose a reason for hiding this comment

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

torch.compile/torch.export. I don't think users understand the difference between 2 being a version and pt2 being the codename for torch.compile/torch.export.

ditto for the title.

:doc:`custom C++ classes <torch_script_custom_classes>` tutorial, and
introduces additional steps that are needed to support custom C++ classes in
PyTorch 2.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a sense of the stability of this? Does it always work now? If not, some warning about the rough edges around this (this is a "prototype" feature, right? or "unstable" in the current classification) would help

Comment on lines +202 to +203
Why do we need to make a Fake Class?
------------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

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

We should try to list out all the caveats around tensor aliasing. Namely, we're gonna assume that tensors inside and outside a torchbind object are different. If you do things like mutate one of them, there will be undefined behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed module: export new tutorial torch.compile Torch compile and other relevant tutorials
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants