Autograd isn't working on iOS

I have this sample code in file.m (compiled by Objective-C++ compiler) in Xcode iOS project (LibTorch is loaded via Cocoapods):

auto param = torch::tensor({1.}, torch::requires_grad());
auto result = 1 + param;
result.backward();

Executing gives this error:
libc++abi.dylib: terminating with uncaught exception of type c10::Error: Could not run ‘aten::_backward’ with arguments from the ‘CPU’ backend. This could be because the operator doesn’t exist for this backend, or was omitted during the selective/custom build process (if using custom build). If you are a Facebook employee using PyTorch on mobile, please visit https://fburl.com/ptmfixes for possible resolutions. ‘aten::_backward’ is only available for these backends: [BackendSelect, Named, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, Tracer, Autocast, Batched, VmapMode].

I searched a lot and still have no answer, please give any suggestions how can to fix it.

I think Autograd is disabled by default in the mobile backend. You might be able to build it from source and reenable it, but I haven’t tried it and am not sure if this would still be possible (it was in the past, if I’m not mistaken).

1 Like

Thanks! I’m sure that’s the only option I have. Can you please give some advice how to do it? What changes can I make in CMakeLists.txt? Can I still use python tools/build_libtorch.py? Seems there’s no instructions for mobile on GitHub page.

try part on aarch64-linux-gnu-gcc

build all dependencies by manual can use backward

Did you find out what the right flag was to enable backward on mobile?