-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Open
Labels
good first issuemodule: cppRelated to C++ APIRelated to C++ APImodule: nnRelated to torch.nnRelated to torch.nntriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
Description
Currently, PyTorch C++ API is missing many torch::nn layers that are available in the Python API. As part of the Python/C++ API parity work, we would like to add the following torch::nn modules and utilities in C++ API:
Containers
- Module (TODO: some APIs are missing in C++, e.g.
register_forward_hook/register_forward_pre_hook) - Sequential (@ShahriarSS)
- ModuleList (@ShahriarSS)
- ModuleDict (@ejguan Implement C++ ModuleDict #47707)
- ParameterList (@yyn19951228 Impl for ParameterList #41259)
- ParameterDict (@yyn19951228 Python/C++ API Parity: Add impl and tests for ParameterDict #40654)
Convolution layers
- Conv1d (@yf225 C++/Python API parity for Conv{1,2,3}d layers, and add F::conv{1,2,3}d functionals #28917)
- Conv2d (@yf225 C++/Python API parity for Conv{1,2,3}d layers, and add F::conv{1,2,3}d functionals #28917)
- Conv3d (@yf225 C++/Python API parity for Conv{1,2,3}d layers, and add F::conv{1,2,3}d functionals #28917)
- ConvTranspose1d (@nuka137 C++ API: torch::nn::ConvTranspose{1,2,3}d #29721)
- ConvTranspose2d (@nuka137 C++ API: torch::nn::ConvTranspose{1,2,3}d #29721)
- ConvTranspose3d (@nuka137 C++ API: torch::nn::ConvTranspose{1,2,3}d #29721)
- Unfold (@jon-tow C++ API parity: Unfold #27809)
- Fold (@ShahriarSS C++ Fold nn module #24160)
Pooling layers
- MaxPool1d (@ShahriarSS C++ MaxPool Module #24860)
- MaxPool2d (@ShahriarSS C++ MaxPool Module #24860)
- MaxPool3d (@ShahriarSS C++ MaxPool Module #24860)
- MaxUnpool1d (@pbelevich C++ API parity: MaxUnpool1d #26896)
- MaxUnpool2d (@pbelevich C++ API parity: MaxUnpool2d #26915)
- MaxUnpool3d (@pbelevich C++ API parity: MaxUnpool3d #27027)
- AvgPool1d (@ShahriarSS C++ Average Pool Module #25800)
- AvgPool2d (@ShahriarSS C++ Average Pool Module #25800)
- AvgPool3d (@ShahriarSS C++ Average Pool Module #25800)
- FractionalMaxPool2d (@yf225 [C++ API] torch::nn::FractionalMaxPool{2,3}d module and functional #29933)
- FractionalMaxPool3d (@yf225 [C++ API] torch::nn::FractionalMaxPool{2,3}d module and functional #29933)
- LPPool1d (@nuka137 C++ API: torch::nn::LPPool1d #27800)
- LPPool2d (@nuka137 C++ API: torch::nn::LPPool2d #28492)
- AdaptiveMaxPool1d (@pbelevich C++ API parity: AdaptiveMaxPool1d #26755)
- AdaptiveMaxPool2d (@pbelevich C++ API parity: AdaptiveMaxPool2d #26772)
- AdaptiveMaxPool3d (@pbelevich C++ API parity: AdaptiveMaxPool3d #26775)
- AdaptiveAvgPool1d (@pbelevich C++ API parity: AdaptiveAvgPool1d #26808)
- AdaptiveAvgPool2d (@pbelevich C++ API parity: AdaptiveAvgPool2d #26818)
- AdaptiveAvgPool3d (@pbelevich C++ API parity: AdaptiveAvgPool3d #26819)
Padding layers
- ReflectionPad1d (@yf225 C++ nn::ReflectionPad1d and nn::ReflectionPad2d #28538)
- ReflectionPad2d (@yf225 C++ nn::ReflectionPad1d and nn::ReflectionPad2d #28538)
- ReplicationPad1d (@yf225 C++ nn::ReplicationPad{1,2,3}d #28539)
- ReplicationPad2d (@yf225 C++ nn::ReplicationPad{1,2,3}d #28539)
- ReplicationPad3d (@yf225 C++ nn::ReplicationPad{1,2,3}d #28539)
- ZeroPad2d (@yf225 C++ nn::ZeroPad2d #28540)
- ConstantPad1d (@yf225 C++ nn::ConstantPad{1,2,3}d #28541)
- ConstantPad2d (@yf225 C++ nn::ConstantPad{1,2,3}d #28541)
- ConstantPad3d (@yf225 C++ nn::ConstantPad{1,2,3}d #28541)
Non-linear activations (weighted sum, nonlinearity)
- ELU (@pbelevich C++ API parity: ELU #27028)
- Hardshrink (@pbelevich C++ API parity: Hardshrink #27035)
- Hardtanh (@pbelevich C++ API parity: Hardtanh #27038)
- LeakyReLU (@pbelevich C++ API parity: LeakyReLU #27059)
- LogSigmoid (@pbelevich C++ API parity: LogSigmoid #27060)
- MultiheadAttention (@pbelevich C++ API parity: MultiheadAttention #27309)
- PReLU (@pbelevich C++ API parity: PReLU #27429)
- ReLU (@pbelevich C++ API parity: ReLU #27435)
- ReLU6 (@pbelevich C++ API parity: ReLU6 #27436)
- RReLU (@pbelevich C++ API parity: RReLU #27437)
- SELU (@jon-tow C++ API parity: SELU #27434)
- CELU (@pbelevich C++ API parity: CELU #27487)
- GELU (@BIT-silence Add torch.nn.GELU for GELU activation #28944)
- Sigmoid (@pbelevich C++ API parity: Sigmoid #27488)
- Softplus (@pbelevich C++ API parity: Softplus #27489)
- Softshrink (@pbelevich C++ API parity: Softshrink #27534)
- Softsign (@pbelevich C++ API parity: Softsign #27535)
- Tanh (@pbelevich C++ API parity: Tanh #27536)
- Tanhshrink (@pbelevich C++ API parity: Tanhshrink #27537)
- Threshold (@pbelevich C++ API parity: Threshold #27538)
- GLU (@yf225 [C++ API] torch::nn::GLU and F::glu #29922)
- SiLU (@heitorschueroff Added SiLU activation function #41034)
Non-linear activations (other)
- Softmin (@nuka137 C++ API: torch::nn::Softmin #27459)
- Softmax (@nuka137 C++ API: torch::nn::Softmax #27446)
- Softmax2d (@nuka137 C++ API: torch::nn::Softmax2d #27509)
- LogSoftmax (@nuka137 C++ API: torch::nn::LogSoftmax #27462)
- AdaptiveLogSoftmaxWithLoss (@mansoorcheema [C++ API] AdaptiveLogSoftmaxWithLoss #29076)
Normalization layers
- BatchNorm1d (@nuka137 C++ API: torch::nn::BatchNorm1d #28176)
- BatchNorm2d (@nuka137 C++ API: torch::nn::BatchNorm{2,3}d #28936)
- BatchNorm3d (@nuka137 C++ API: torch::nn::BatchNorm{2,3}d #28936)
- GroupNorm (@yf225 [C++ API] torch::nn::GroupNorm and F::group_norm #29920)
- SyncBatchNorm (Needs distributed data parallel support)
- InstanceNorm1d (@divyanshsinghvi [C++ API] InstanceNorm{1,2,3}d #28790)
- InstanceNorm2d (@divyanshsinghvi [C++ API] InstanceNorm{1,2,3}d #28790)
- InstanceNorm3d (@divyanshsinghvi [C++ API] InstanceNorm{1,2,3}d #28790)
- LayerNorm (@anjali411 torch::nn::LayerNorm #28032)
- LocalResponseNorm (@mansoorcheema Local response norm #28759)
- CrossMapLRN2d (@lsrock1 C++ parity, nn::CrossMapLRN2d #29039)
Recurrent layers
- RNN (@yf225 [C++ API] RNN / GRU / LSTM layer refactoring #34322)
- LSTM (@yf225 [C++ API] RNN / GRU / LSTM layer refactoring #34322)
- GRU (@yf225 [C++ API] RNN / GRU / LSTM layer refactoring #34322)
- RNNCell (@yf225 [C++ API] RNNCell / LSTMCell / GRUCell layers #34400)
- LSTMCell (@yf225 [C++ API] RNNCell / LSTMCell / GRUCell layers #34400)
- GRUCell (@yf225 [C++ API] RNNCell / LSTMCell / GRUCell layers #34400)
Transformer layers
- Transformer (@glaringlee C++ APIs Transformer NN Module Top Layer #44333)
- TransformerEncoder (@glaringlee C++ APIs TransformerEncoder #43187)
- TransformerDecoder (@VinodSKumar Python/C++ API Parity: TransformerDecoder #42886)
- TransformerEncoderLayer (@glaringlee C++ API TransformerEncoderLayer #42633)
- TransformerDecoderLayer (@VinodSKumar Python/C++ API Parity: TransformerDecoderLayer #42717)
Linear layers
- Identity (@jon-tow Add C++ nn::Identity #26713)
- Linear (@pbelevich [BC-breaking] C++ API parity: Linear #27382)
- Bilinear (@MJ10 [C++] Add nn.Bilinear to C++ Frontend #26082)
- Flatten (@mrsalehi Add nn::Flatten to C++ Frontend #28072)
- Unflatten (@heitorschueroff Implemented torch::nn::Unflatten in libtorch #42613)
Dropout layers
- Dropout (@pbelevich C++ API parity: Dropout, Dropout2d, Dropout3d #29761)
- Dropout2d (@pbelevich C++ API parity: Dropout, Dropout2d, Dropout3d #29761)
- Dropout3d (@pbelevich C++ API parity: Dropout, Dropout2d, Dropout3d #29761)
- AlphaDropout (@suyash458 C++/Python API Parity: add AlphaDropout and FeatureAlphaDropout #28424)
- FeatureAlphaDropout (@suyash458 C++/Python API Parity: add AlphaDropout and FeatureAlphaDropout #28424)
Sparse layers
- Embedding (@anjali411 Implement torch.nn.Embedding / EmbeddingBag in PyTorch C++ API #26358)
- EmbeddingBag (@anjali411 Implement torch.nn.Embedding / EmbeddingBag in PyTorch C++ API #26358)
Distance functions
- CosineSimilarity (@jon-tow [C++ API] Distance module #26424)
- PairwiseDistance (@jon-tow [C++ API] Distance module #26424)
Loss functions
- L1Loss (@ShahriarSS [C++ API] L1Loss module #25902)
- MSELoss (@ShahriarSS Adding MSELoss, KLDivLoss and BCELoss to C++ front-end #27156)
- CrossEntropyLoss (@PyExtreme C++ API parity: NLLLoss & CrossEntropyLoss #29812)
- CTCLoss (@pbelevich C++ API parity: CTCLoss #28654)
- NLLLoss (@PyExtreme C++ API parity: NLLLoss & CrossEntropyLoss #29812)
- PoissonNLLLoss (@pbelevich C++ API parity: PoissonNLLLoss #28755)
- KLDivLoss (@ShahriarSS Adding MSELoss, KLDivLoss and BCELoss to C++ front-end #27156)
- BCELoss (@ShahriarSS Adding MSELoss, KLDivLoss and BCELoss to C++ front-end #27156)
- BCEWithLogitsLoss (@pbelevich C++ API parity: BCEWithLogitsLoss #28783)
- MarginRankingLoss (@pbelevich C++ API parity: MarginRankingLoss #29000)
- HingeEmbeddingLoss (@jon-tow Add C++ torch::nn::HingeEmbeddingLoss #27101)
- MultiLabelMarginLoss (@CarMiranda [C++ API parity] Multi Label Margin loss #27659)
- SmoothL1Loss (@CarMiranda [C++ API parity] Smooth L1 loss #27661)
- SoftMarginLoss (@CarMiranda [C++ API parity] Soft Margin loss #27660)
- MultiLabelSoftMarginLoss (@CarMiranda [C++ API parity] Multi-Label Soft Margin loss #27669)
- CosineEmbeddingLoss (@jon-tow Add C++ torch::nn::CosineEmbeddingLoss #27345)
- MultiMarginLoss (please see details in [Contributor Welcome] Implement C++ API version of torch.nn.MultiMarginLoss #27198) (@CarMiranda Implement C++ API torch::nn::MultiMarginLoss. #27424)
- TripletMarginLoss (please see details in [Contributor Welcome] Implement C++ API version of torch.nn.TripletMarginLoss #27197) (@PyExtreme [C++ API Parity] - TripletMarginLoss #27713)
Vision layers
- PixelShuffle (@CarMiranda [C++ API parity] PixelShuffle module and functional #28140)
- Upsample (along with
torch::nn::functional::interpolate) (@jon-tow C++ API parity: Upsample #28413)
Utilities
- clip_grad_norm_ (@rohan-varma [c++ api] Add clip_grad_norm_ to c++ api #26140)
- clip_grad_value_ (@jokerkeny Add C++ API clip_grad_value_ for nn:utils #28736)
- parameters_to_vector (@lsrock1 C++ parity, convert_parameters #29267)
- vector_to_parameters (@lsrock1 C++ parity, convert_parameters #29267)
- weight_norm (@bernsm3, has dependency on
Module._forward_pre_hooks) - remove_weight_norm (@bernsm3, has dependency on
Module._forward_pre_hooks) - spectral_norm (has dependency on
Module._forward_pre_hooks) - remove_spectral_norm (has dependency on
Module._forward_pre_hooks) - PackedSequence (@yf225 [C++ API] Add PackedSequence / pack_padded_sequence / pad_packed_sequence / pack_sequence #33652)
- pack_padded_sequence (@yf225 [C++ API] Add PackedSequence / pack_padded_sequence / pad_packed_sequence / pack_sequence #33652)
- pad_packed_sequence (@yf225 [C++ API] Add PackedSequence / pack_padded_sequence / pad_packed_sequence / pack_sequence #33652)
- pad_sequence (@suyash458 C++/Python API Parity: add pad_sequence #32387)
- pack_sequence (@yf225 [C++ API] Add PackedSequence / pack_padded_sequence / pad_packed_sequence / pack_sequence #33652)
torch.nn.functional
- normalize (please see details in [Contributor Welcome] Implement C++ API version of torch.nn.functional.normalize #27048) (@gtamba Add nn::functional::normalize() to C++ Frontend #27280)
- gumbel_softmax (please see details in [Contributor Welcome] Implement C++ API version of torch.nn.functional.gumbel_softmax #27078) (@Naresh1318 torch.nn.functional.gumbel_softmax #27078 #28121)
- one_hot (please see details in [Contributor Welcome] Implement C++ API version of torch.nn.functional.one_hot #27081) (@bzinodev Implement C++ API version of torch.nn.functional.one_hot (#27081) #27177)
- pdist (please see details in [Contributor Welcome] Implement C++ API version of torch.nn.functional.pdist #27082) (@jon-tow Add C++ torch::nn::functional::pdist #27122)
- grid_sample (@lsrock1 C++ parity, grid_sample functional #28354)
- affine_grid (please see details in [Contributor Welcome] Implement C++ API version of torch.nn.functional.affine_grid #27196) (@jon-tow Add C++ torch::nn::functional::affine_grid #27263)
- gelu (@anjali411 [C++ API Parity, F::gelu] #28433)
Implementation Notes:
- For
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.- You might see that some of the
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.
- You might see that some of the
- When you are adding a new C++
torch::nnmodule:- The new
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). - How do we decide which file to put the new C++
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. - If you add a new module header, you must also add the include statement for the new header in
torch/csrc/api/include/torch/nn/modules.h. - If you add a new module
.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.) - You must add tests for the new module in
test/cpp/api/modules.cpp. In particular, make sure the module'spretty_printis tested and it outputs the same value as the Python version. - if the module is templatized (e.g.
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).
- The new
- When you are adding a new C++
torch::nn::functionalfunction:- How do we decide which file to put the new C++
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. - You must add tests for the new functional in
test/cpp/api/functional.cpp. - If you add a new header file for the functional, you must add include statement for the header file in
torch/csrc/api/include/torch/nn/functional.h. - The name of the functional must match the Python version.
- How do we decide which file to put the new C++
- When you are adding a new module/functional options:
- If the functional options only has optional arguments, the functional's function signature should have
options = {}, to allow users to call the functional without passing options. And we must add a test for this. - If the functional / module options only has one non-optional argument, we need the implicit constructor for the options that only takes the non-optional argument (i.e.
/* 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.) - for optional Tensor argument, we should use
torch::Tensor(withTensor()as default “null" value), and don’t usec10::optional<Tensor>. - If the options is templatized (e.g.
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).
- If the functional options only has optional arguments, the functional's function signature should have
- Do not use
torch::IntArrayRef, usestd::vector<int64_t>instead.
How do I run tests?
- Follow https://github.com/pytorch/pytorch#from-source to build your branch of PyTorch from source.
- To test
test/cpp/api/modules.cpp, run./build/bin/test_api --gtest_filter=ModulesTest* --gtest_stack_trace_depth=10 --gmock_verbose=info. - To test
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::nn module from this list.
Tracking post on PyTorch forum: https://discuss.pytorch.org/t/55650
cc @yf225
krshrimali, nmerrill67, nuka137, dfalbel, madcato and 2 more
Metadata
Metadata
Assignees
Labels
good first issuemodule: cppRelated to C++ APIRelated to C++ APImodule: nnRelated to torch.nnRelated to torch.nntriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module