Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upPython/C++ API Parity: torch.nn modules and functional #25883
Comments
Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. Note: this is a WIP PR. Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. Note: this is a WIP PR. Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/) ghstack-source-id: 89882567 Pull Request resolved: #25981
Summary: Pull Request resolved: pytorch#25981 Per pytorch#25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. Note: this is a WIP PR. ghstack-source-id: 89882567 Test Plan: Added a unit test. Differential Revision: D17312367 fbshipit-source-id: e249b401002b6e01536acaa29fadd71706ce6209
Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 89882567 Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 89882567 Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/) ghstack-source-id: 90026828 Pull Request resolved: #26140
Per #25883, we want to work towards C++/Python API parity. This diff adds `clip_grad_norm_` to the C++ API to improve parity. The implementation and tests are ported over directly from the python api (see https://pytorch.org/docs/stable/_modules/torch/nn/utils/clip_grad.html) Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Pull Request resolved: #26140 Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 90029046 ghstack-source-id: 90029046 Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Per #25883, we want to work towards C++/Python API parity. This diff adds `clip_grad_norm_` to the C++ API to improve parity. The implementation and tests are ported over directly from the python api (see https://pytorch.org/docs/stable/_modules/torch/nn/utils/clip_grad.html) Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Pull Request resolved: #26140 Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 90033213 ghstack-source-id: 90033213 Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Per #25883, we want to work towards C++/Python API parity. This diff adds `clip_grad_norm_` to the C++ API to improve parity. The implementation and tests are ported over directly from the python api (see https://pytorch.org/docs/stable/_modules/torch/nn/utils/clip_grad.html) Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Pull Request resolved: #26140 Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 90033526 ghstack-source-id: 90033526 Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Per #25883, we want to work towards C++/Python API parity. This diff adds `clip_grad_norm_` to the C++ API to improve parity. The implementation and tests are ported over directly from the python api (see https://pytorch.org/docs/stable/_modules/torch/nn/utils/clip_grad.html) Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Pull Request resolved: #26140 Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 90039953 ghstack-source-id: 90039953 Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Per #25883, we want to work towards C++/Python API parity. This diff adds `clip_grad_norm_` to the C++ API to improve parity. The implementation and tests are ported over directly from the python api (see https://pytorch.org/docs/stable/_modules/torch/nn/utils/clip_grad.html) Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Pull Request resolved: #26140 Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 90077057 ghstack-source-id: 90077057 Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Per #25883, we want to work towards C++/Python API parity. This diff adds `clip_grad_norm_` to the C++ API to improve parity. The implementation and tests are ported over directly from the python api (see https://pytorch.org/docs/stable/_modules/torch/nn/utils/clip_grad.html) Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Pull Request resolved: #26140 Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 90094458 ghstack-source-id: 90094458 Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Per #25883, we want to work towards C++/Python API parity. This diff adds `clip_grad_norm_` to the C++ API to improve parity. The implementation and tests are ported over directly from the python api (see https://pytorch.org/docs/stable/_modules/torch/nn/utils/clip_grad.html) Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Pull Request resolved: #26140 Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 90103121 ghstack-source-id: 90103121 Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Per #25883, we want to work towards C++/Python API parity. This diff adds `clip_grad_norm_` to the C++ API to improve parity. The implementation and tests are ported over directly from the python api (see https://pytorch.org/docs/stable/_modules/torch/nn/utils/clip_grad.html) Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Pull Request resolved: #26140 Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 90109145 ghstack-source-id: 90109145 Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Per #25883, we want to work towards C++/Python API parity. This diff adds `clip_grad_norm_` to the C++ API to improve parity. The implementation and tests are ported over directly from the python api (see https://pytorch.org/docs/stable/_modules/torch/nn/utils/clip_grad.html) Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
Pull Request resolved: #26140 Per #25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 90122570 ghstack-source-id: 90122570 Differential Revision: [D17312367](https://our.internmc.facebook.com/intern/diff/D17312367/)
This comment has been minimized.
This comment has been minimized.
@Yunseong-Jeong Yup I am working on the module hooks now and will let you know as soon as I finished it. Thanks so much for your help! |
Summary: Adds `torch::nn::HingeEmbeddingLoss` module support for the C++ API. **Issue**: pytorch#25883 **Reviewer**: yf225 Pull Request resolved: pytorch#27101 Differential Revision: D17680489 Pulled By: yf225 fbshipit-source-id: 1f8f41775a9e1272a98232c8f899418b2b907eca
Summary: Adds `torch::nn::functional::pdist` module support for the C++ API. Issue: pytorch#25883, pytorch#27082 Reviewer: yf225 Pull Request resolved: pytorch#27122 Differential Revision: D17685823 Pulled By: yf225 fbshipit-source-id: f8ceb09635385ef2e16a002e5fc255be8eb2ebf4
Summary: Pull Request resolved: pytorch#26140 Per pytorch#25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 91334333 ghstack-source-id: 91334333 Test Plan: Added a unit test Differential Revision: D17312367 fbshipit-source-id: 753ba3a4d084d01f3cc8919da3108e67c809ad65
This comment has been minimized.
This comment has been minimized.
|
@yf225, I'd like to work on
|
This comment has been minimized.
This comment has been minimized.
|
@yf225 Where should the tests for the utility functions like |
This comment has been minimized.
This comment has been minimized.
@Suyash458 It would be awesome to put them in |
This comment has been minimized.
This comment has been minimized.
@RithvikSharma Thanks a lot for your interest! We are going through some internal planning at the moment, and I will update on its availability at the earliest possible :D |
This comment has been minimized.
This comment has been minimized.
|
Hi, I would like to work on PackedSequence. If it is available. @yf225 |
Summary: Adds `torch::nn::HingeEmbeddingLoss` module support for the C++ API. **Issue**: pytorch#25883 **Reviewer**: yf225 Pull Request resolved: pytorch#27101 Differential Revision: D17680489 Pulled By: yf225 fbshipit-source-id: 1f8f41775a9e1272a98232c8f899418b2b907eca
Summary: Adds `torch::nn::functional::pdist` module support for the C++ API. Issue: pytorch#25883, pytorch#27082 Reviewer: yf225 Pull Request resolved: pytorch#27122 Differential Revision: D17685823 Pulled By: yf225 fbshipit-source-id: f8ceb09635385ef2e16a002e5fc255be8eb2ebf4
Summary: Pull Request resolved: pytorch#26140 Per pytorch#25883, we want to work towards C++/Python API parity. This diff adds clip_grad_norm_ to the c++ API to improve parity. ghstack-source-id: 91334333 ghstack-source-id: 91334333 Test Plan: Added a unit test Differential Revision: D17312367 fbshipit-source-id: 753ba3a4d084d01f3cc8919da3108e67c809ad65
Summary: Adds `torch::nn::CosineEmbeddingLoss` module and functional support for the C++ API. Issue: pytorch#25883 Reviewer: yf225 Pull Request resolved: pytorch#27345 Differential Revision: D17801402 Pulled By: yf225 fbshipit-source-id: 0eabe80d7d36397e6667b331c3fa2f56d7a15962
Summary: Add torch::nn::Softmax module support for the C++ API Related Issue: pytorch#25883 Reviewer: yf225 Pull Request resolved: pytorch#27446 Differential Revision: D17839546 Pulled By: yf225 fbshipit-source-id: 7c7fb55111b261614de7c3a75fa1019fbde93c67
Summary: Adds `SELU` functional and module support for the C++ API. Issue: pytorch#25883 Pull Request resolved: pytorch#27434 Differential Revision: D17782762 Pulled By: yf225 fbshipit-source-id: 96c7ce84b9baf9e219a63e631929b8997ba6f3f0
Summary: Adds`torch::nn::functional::affine_grid` functional support for the C++ API. Issue: pytorch#25883, pytorch#27196 Reviewer: yf225 Pull Request resolved: pytorch#27263 Differential Revision: D17802350 Pulled By: yf225 fbshipit-source-id: e823ee53da4a4cc6a1650d2dfc09b0ef6a74e249
Summary: Add torch::nn::LogSoftmax module and functional support for the C++ API. Related Issue: pytorch#25883 Reviewer: yf225 Pull Request resolved: pytorch#27462 Differential Revision: D17867121 Pulled By: yf225 fbshipit-source-id: dae8ac981c1c6ccdef013cd2d886ad4a043f6243
Summary: Add torch::nn::Softmin module and functional support for the C++ API. Related Issue: pytorch#25883 Reviewer: yf225 Pull Request resolved: pytorch#27459 Differential Revision: D17892852 Pulled By: yf225 fbshipit-source-id: db15b06e8ad33947e7d65995df700f5e90c3b6a8
Summary: Add torch::nn::Softmax2d module support for the C++ API. Softmax2d only supports module in Python API, so this PR adds only module support as well. This PR is WIP because it uses the function in pytorch#27446 . After pytorch#27446 is merged, I will remove WIP. Related Issue: pytorch#25883 Reviewer: yf225 Pull Request resolved: pytorch#27509 Differential Revision: D17899715 Pulled By: yf225 fbshipit-source-id: bd891bc995f5a92bf4f5405f8bf07d1bd5de2479
Summary: Adds `unfold` functional and module support for the C++ API. Issue: pytorch#25883 Reviewer: yf225 Pull Request resolved: pytorch#27809 Differential Revision: D17901792 Pulled By: yf225 fbshipit-source-id: ff58a1866bf240f37ebc589463c60593b8931f51
Summary: In accordance with pytorch#25883, I added the `MultiLabelSoftMarginLoss` module and `multilabel_soft_margin_loss` functional. It looks like there isn't a C++ ATen implementation of `multilabel_soft_margin_loss`, so I translated the python version, which does not rely on a C/C++ backend either. Pull Request resolved: pytorch#27669 Differential Revision: D17907608 Pulled By: yf225 fbshipit-source-id: ccb02951e009973c2adbe604593ce929f10c39eb
Summary: In accordance with pytorch#25883, I added the `SoftMarginLoss` module and `soft_margin_loss` functional. Pull Request resolved: pytorch#27660 Differential Revision: D17958325 Pulled By: yf225 fbshipit-source-id: c14422765e6e1fdabf6c9687080e6d5ff490d300
Summary: In accordance with pytorch#25883, I added the `MultiLabelMarginLoss` module and `multilabel_margin_loss` functional. Pull Request resolved: pytorch#27659 Differential Revision: D17931905 Pulled By: yf225 fbshipit-source-id: 3642f75c79843dda55ac38de9f6f970f3e237847
Summary: Added `PixelShuffle` module and functional pytorch#25883 Pull Request resolved: pytorch#28140 Differential Revision: D18008474 Pulled By: yf225 fbshipit-source-id: f482495bb56998701c79a61ef065a121bf5a5154
Summary: Add torch::nn::LPPool1d module and functional support for the C++ API. Related Issue: pytorch#25883 Reviewer: yf225 Pull Request resolved: pytorch#27800 Differential Revision: D18045040 Pulled By: yf225 fbshipit-source-id: e61fefe9efec3423f7a93dd1e946f3e380122927
Summary: This PR updates `test/cpp_api_parity/parity-tracker.md` to reflect changes in pytorch#25883. Pull Request resolved: pytorch#28419 Differential Revision: D18061479 Pulled By: yf225 fbshipit-source-id: dbdc2e44e835f6125a42cf11e59723ef61903cff
Summary: pytorch#25883 I put grid_sample in vision.h with affine grid. I have a question in string argument(interpolation mode, padding mode) I reuse torch::native::detail::GridSamplerInterpolation in GridSampler.h instead of using string. It follows the way that uses reduction enum in loss functions. I am not sure this is right. yf225 Pull Request resolved: pytorch#28354 Differential Revision: D18109333 Pulled By: yf225 fbshipit-source-id: 1bf972b671b107464f73b937bbe0de76fb259fbf
Currently, PyTorch C++ API is missing many
torch::nnlayers that are available in the Python API. As part of the Python/C++ API parity work, we would like to add the followingtorch::nnmodules and utilities in C++ API:Containers
register_forward_hook/register_forward_pre_hook)Convolution layers
Pooling layers
Padding layers
Non-linear activations (weighted sum, nonlinearity)
Non-linear activations (other)
Normalization layers
Recurrent layers
Sequentialmodule)Sequentialmodule)Sequentialmodule)Sequentialmodule)Sequentialmodule)Sequentialmodule)Transformer layers
Linear layers
Dropout layers
Sparse layers
Distance functions
Loss functions
Vision layers
torch::nn::functional::interpolate) (@jon-tow #28413)Utilities
Module._forward_pre_hooks)Module._forward_pre_hooks)Module._forward_pre_hooks)Module._forward_pre_hooks)PackedSequence)PackedSequence)PackedSequence)torch.nn.functional
Implementation Notes:
torch.nnmodules, Python and C++ implementation must only differ in language-specific syntax, and their data member fields, control flow and logic must be exactly the same.torch.nnmodules call the correspondingtorch.nn.functionalfunctions. When implementing the C++ version of those modules, we should also add the correspondingtorch::nn::functionalfunctions, in order to preserve the call structure.torch::nnmodule:torch::nnmodule must subclass frompublic Cloneable<ModuleName>. For example,class TORCH_API LinearImpl : public Cloneable<LinearImpl>(intorch/csrc/api/include/torch/nn/modules/linear.h).torch::nnmodule in? Answer: We should look at where the Python version of module is located and try to mirror that file structure. For example, the Python version oftorch.nn.Linearlives intorch/nn/modules/linear.py, so the C++ versiontorch::nn::Linearshould live intorch/csrc/api/include/torch/nn/modules/linear.h.torch/csrc/api/include/torch/nn/modules.h..cppfile, you must also add it intocaffe2/CMakeLists.txtandtools/build_variables.py. (Please search for other modules in those files to see how this should be done.)test/cpp/api/modules.cpp. In particular, make sure the module'spretty_printis tested and it outputs the same value as the Python version.MaxPoolImpl), you must add explicit template instantiation in the module’s.cppfile (e.g. search fortemplate classintorch/csrc/api/src/nn/modules/pooling.cppto see how this is done).torch::nn::functionalfunction:torch::nn::functionalfunction? Answer: We should look at where the correspondingtorch::nnmodule is located. For example,torch::nn::PairwiseDistancelives intorch/csrc/api/include/torch/nn/modules/distance.h(which, following the rule above, is determined bytorch/nn/modules/distance.py), sotorch::nn::functional::pairwise_distanceshould live intorch/csrc/api/include/torch/nn/functional/distance.h.test/cpp/api/functional.cpp.torch/csrc/api/include/torch/nn/functional.h.options = {}, to allow users to call the functional without passing options. And we must add a test for this./* implicit */ Options(value_type value);), so that we are able to dofunctional(input, options_arg_value)/auto m = Module(options_arg_value)instead offunctional(input, Options(options_arg_value))/auto m = Module(Options(options_arg_value)). And we must add a test for this. (SeeDropoutOptionsintorch/csrc/api/include/torch/nn/options/dropout.has example.)torch::Tensor(withTensor()as default “null" value), and don’t usec10::optional<Tensor>.MaxPoolOptionsintorch/csrc/api/include/torch/nn/options/pooling.h), you must add explicit template instantiation in the options’.cppfile (search fortemplate structintorch/csrc/api/src/nn/options/pooling.cppto see how this is done).torch::IntArrayRef, usestd::vector<int64_t>instead.How do I run tests?
test/cpp/api/modules.cpp, run./build/bin/test_api --gtest_filter=ModulesTest* --gtest_stack_trace_depth=10 --gmock_verbose=info.test/cpp/api/functional.cpp, run./build/bin/test_api --gtest_filter=FunctionalTest* --gtest_stack_trace_depth=10 --gmock_verbose=info.Please ask for @yf225's review when you open a PR to add a
torch::nnmodule from this list.Tracking post on PyTorch forum: https://discuss.pytorch.org/t/55650
cc @yf225