Repository: pytorch
Issue: Optimizer support via Libtorch C++ on Android
Labels: module: cpp,module: optimizer,triaged,oncall: mobile

## 🐛 Bug

We are building libtorch using ./scripts/build_anroid.sh.

We need the support for Aten Ops and TorchScript, so building without the BUILD_CAFFE2_MOBILE option.

The build is successful, but the library does not link ${TORCH_SRC_DIR}/csrc/api/src/optim/adam.cpp and dependencies because of NO_API being set for Mobile Builds.

Because of this I am unable to train the model and instantiate Adam Optimizer instance from the code.

torch::optim::Adam optimizer(parameters, lr); //Linker Error
optimizer.zero_grad(); //Linker Error
optimizer.step(); //Linker Error

Following is the Linker error:
/home/atibrewal/work/apprecommender/src/RNNRecommender.cpp:374: undefined reference to `torch::optim::AdamOptions::AdamOptions(double)'
/home/atibrewal/work/apprecommender/src/RNNRecommender.cpp:375: undefined reference to `torch::optim::Optimizer::zero_grad()'
/home/atibrewal/work/apprecommender/src/RNNRecommender.cpp:395: undefined reference to `torch::optim::Adam::step(std::__ndk1::function<at::Tensor ()>)'
CMakeFiles/hxRecommenderEngine.dir/src/RNNRecommender.cpp.o: In function `OptimizerParamGroup':
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/optimizer.h:68: undefined reference to `torch::optim::OptimizerParamGroup::params() const'
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/optimizer.h:68: undefined reference to `torch::optim::OptimizerParamGroup::has_options() const'
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/optimizer.h:68: undefined reference to `torch::optim::OptimizerParamGroup::options() const'
CMakeFiles/hxRecommenderEngine.dir/src/RNNRecommender.cpp.o: In function `AdamOptions':
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/adam.h:21: undefined reference to `vtable for torch::optim::AdamOptions'
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/adam.h:21: undefined reference to `vtable for torch::optim::AdamOptions'
CMakeFiles/hxRecommenderEngine.dir/src/RNNRecommender.cpp.o: In function `Adam':
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/adam.h:52: undefined reference to `vtable for torch::optim::Adam'
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/adam.h:52: undefined reference to `vtable for torch::optim::Adam'
CMakeFiles/hxRecommenderEngine.dir/src/RNNRecommender.cpp.o: In function `OptimizerOptions':
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/optimizer.h:49: undefined reference to `vtable for torch::optim::OptimizerOptions'
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/optimizer.h:49: undefined reference to `vtable for torch::optim::OptimizerOptions'
CMakeFiles/hxRecommenderEngine.dir/src/RNNRecommender.cpp.o: In function `Optimizer':
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/optimizer.h:91: undefined reference to `vtable for torch::optim::Optimizer'
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/optimizer.h:91: undefined reference to `vtable for torch::optim::Optimizer'
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/optimizer.h:93: undefined reference to `torch::optim::Optimizer::add_param_group(torch::optim::OptimizerParamGroup const&)'
CMakeFiles/hxRecommenderEngine.dir/src/RNNRecommender.cpp.o: In function `~Optimizer':
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/optimizer.h:103: undefined reference to `vtable for torch::optim::Optimizer'
/home/atibrewal/work/hielibs_android/include/torch/csrc/api/include/torch/optim/optimizer.h:103: undefined reference to `vtable for torch::optim::Optimizer'
CMakeFiles/hxRecommenderEngine.dir/src/RNNRecommender.cpp.o:(.data.rel.ro._ZTVN5torch5optim25OptimizerCloneableOptionsINS0_11AdamOptionsEEE[_ZTVN5torch5optim25OptimizerCloneableOptionsINS0_11AdamOptionsEEE]+0x18): undefined reference to `torch::optim::OptimizerOptions::serialize(torch::serialize::InputArchive&)'
CMakeFiles/hxRecommenderEngine.dir/src/RNNRecommender.cpp.o:(.data.rel.ro._ZTVN5torch5optim25OptimizerCloneableOptionsINS0_11AdamOptionsEEE[_ZTVN5torch5optim25OptimizerCloneableOptionsINS0_11AdamOptionsEEE]+0x20): undefined reference to `torch::optim::OptimizerOptions::serialize(torch::serialize::OutputArchive&) const'
CMakeFiles/hxRecommenderEngine.dir/src/RNNRecommender.cpp.o:(.data.rel.ro._ZTIN5torch5optim25OptimizerCloneableOptionsINS0_11AdamOptionsEEE[_ZTIN5torch5optim25OptimizerCloneableOptionsINS0_11AdamOptionsEEE]+0x10): undefined reference to `typeinfo for torch::optim::OptimizerOptions'


## Environment

 - PyTorch Version (e.g., 1.0): Pytorch 1.5.1 and 1.6.0
 - OS (e.g., Linux): Android arm64-v8a
 - How you installed PyTorch (`conda`, `pip`, source): source
 - Build command you used (if compiling from source): /scripts/build_anroid.sh
 - Python version: NA
 - CUDA/cuDNN version: NA (using a CPU version)
 - GPU models and configuration: NA
 - Any other relevant information: Android NDK version 19c



cc @yf225 @glaringlee @vincentqb