-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Unify C++ API with C++ extensions #11510
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
Conversation
fmassa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
I have a few questions regarding model serialization in Python.
It might be worth looking into also adding equivalents of load_state_dict and state_dict maybe? Also, I have the impression that a cpp module extension will not be seen by the python nn.Module as a nn.Module, so it might not recurse into it, is that right?
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
More generally, would it be possible (or worth it) making the cpp module extensions (when loaded in Python) inherit from |
|
@fmassa That's a good and important question! The definite answer here, however, is that Python API and C++ API modules are currently not inter-operable at all, and so making one a submodule of the other will not work and is not intended to work right now. There could be future work of unifying them, but right now My intention here is simply to make it easier to add Python bindings to the C++ API, but not to make the C++ API inter-operate better with the Python API. The use case here is that you have a C++ API model and would like to do something with it in Python for experimentation, for which C++ extensions have been super quick in my personal attempts at this. Another use case (which we have internally) is that you could keep two copies of your model, one in the Python API which you use for quick experiments, and one in the C++ API which you use in prod, and then have your training loop, data wrangling and visualization in Python, and instantiate either the C++ or Python model dynamically inside Python and all your training loop code will work on both. What this unification does give to C++ extension users is the utilities the C++ API provides, that is simple things like For us developers it is useful because we no longer have to think of the C++ extension codebase (currently torch/csrc/torch.h) and the C++ API codebase as separate. |
|
@goldsborough I see, thanks for the explanation! |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
ea34e65 to
0718236
Compare
|
@ezyang could you review this? |
torch/utils/cpp_extension.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
C++ looks ok, but I'm concerned about the macro. |
23b1df1 to
917708d
Compare
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
goldsborough has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
2863bca to
09d619f
Compare
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
goldsborough has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
More unification Remove ../ include in one C++ extension Solve the constructor problem for ModuleHolder Create extension.h and deprecate torch/torch.h for C++ extensions Include serialize/*.h in conda package Skip cpp_api_extension test on Windows Small improvements
09d619f to
ddce1a9
Compare
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
goldsborough has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Summary: Currently the C++ API and C++ extensions are effectively two different, entirely orthogonal code paths. This PR unifies the C++ API with the C++ extension API by adding an element of Python binding support to the C++ API. This means the `torch/torch.h` included by C++ extensions, which currently routes to `torch/csrc/torch.h`, can now be rerouted to `torch/csrc/api/include/torch/torch.h` -- i.e. the main C++ API header. This header then includes Python binding support conditioned on a define (`TORCH_WITH_PYTHON_BINDINGS`), *which is only passed when building a C++ extension*. Currently stacked on top of pytorch/pytorch#11498 Why is this useful? 1. One less codepath. In particular, there has been trouble again and again due to the two `torch/torch.h` header files and ambiguity when both ended up in the include path. This is now fixed. 2. I have found that it is quite common to want to bind a C++ API module back into Python. This could be for simple experimentation, or to have your training loop in Python but your models in C++. This PR makes this easier by adding pybind11 support to the C++ API. 3. The C++ extension API simply becomes richer by gaining access to the C++ API headers. soumith ezyang apaszke Pull Request resolved: pytorch/pytorch#11510 Reviewed By: ezyang Differential Revision: D9998835 Pulled By: goldsborough fbshipit-source-id: 7a94b44a9d7e0377b7f1cfc99ba2060874d51535
Currently the C++ API and C++ extensions are effectively two different, entirely orthogonal code paths. This PR unifies the C++ API with the C++ extension API by adding an element of Python binding support to the C++ API. This means the
torch/torch.hincluded by C++ extensions, which currently routes totorch/csrc/torch.h, can now be rerouted totorch/csrc/api/include/torch/torch.h-- i.e. the main C++ API header. This header then includes Python binding support conditioned on a define (TORCH_WITH_PYTHON_BINDINGS), which is only passed when building a C++ extension.Currently stacked on top of #11498
Why is this useful?
torch/torch.hheader files and ambiguity when both ended up in the include path. This is now fixed.@soumith @ezyang @apaszke