Building for iOS with torch:nn features

I’m building an iOS application linking in libtorch that uses more than just the low level ops, and it seems and the cmake build are not set up to accommodate this. Am I missing something?

We need stuff in the torch::nn namespace, e.g. torch::nn::Module::register_parameter. It seems I have to modify CMakeLists.txt to have NO_API=0 to get this. Then I can try building with


That almost works, except there are link errors relating to torch::jit::ExportModule, that meant I had to comment out calls to this in csrc/api/src/serialize/output-archive.cpp.

CMakeLists.txt only compiles ${TORCH_SRC_DIR}/csrc/jit/serialization/export_module.cpp, where this method is defined, if INTERN_BUILD_MOBILE is false, which of course it isn’t for an iOS build. However, references to this method are compiled in if NO_API is false. So my build configuration apparently entails incompatible flags. Various hacks to the build to get around this resulted in other problems.

So as things stand I have to modify the source and CMakeLists.txt to get things running on iOS. Is there a better way? Is the mobile build intended to support these features, now or in the future?

Separately from this, I found I had to manually invoke

python -m tools.autograd.gen_autograd

to get necessary headers like tools/autograd/templates/VariableType.h.

Any insights appreciated.

I have the same problem. I’m trying to follow the changes here Android Adaptation · ankurtibrewal/pytorch@77c2e10 · GitHub
Mentioned in this issue: Optimizer support via Libtorch C++ on Android · Issue #42665 · pytorch/pytorch · GitHub

In the end I was able to get something running: Comparing pytorch:main...tdomhan:ios_build · pytorch/pytorch · GitHub
Need to test more, but at first glance things look ok. (I needed to additionally add the Accelerate framework in XCode under “Link Binary with Libraries”).