"Could not find schema for aten::empty.memory_format" in libtorch

I’m trying to use libtorch to load a PyTorch model file from native code on Android. When I load the file, I get the following exception:

terminating with uncaught exception of type c10::Error: Could not find schema for aten::empty.memory_format ()
Exception raised from findSchemaOrThrow at /Users/steve/work/torch/pytorch/aten/src/ATen/core/dispatch/Dispatcher.cpp:131 (most recent call first):

I had previously encountered this same problem on iOS, and fixed it by specifying the “-all_load” flag to the linker.

Based on what I saw on iOS, my guess is that there is some static initializer for the tensor code that is getting stripped out and is not getting called. However, what I thought was the equivalent for clang on Android (“-force_load”) causes some additional link errors that I am struggling to fix.

Is there another way to force that initialization? Where in the libtorch code is this schema usually getting registered?

Make sure you’re building the libraries using version 21 of the NDK, and then use something like this in the target_link_libraries section of your CMakeLists.txt file:

    -Wl,--gc-sections
    -Wl,--whole-archive
    ${TORCH_LIB_DIR}/libtorch.a
    ${TORCH_LIB_DIR}/libtorch_cpu.a
    -Wl,--no-whole-archive
    ${TORCH_LIB_DIR}/libc10.a
    ${TORCH_LIB_DIR}/libnnpack.a
    ${TORCH_LIB_DIR}/libXNNPACK.a
    ${TORCH_LIB_DIR}/libpytorch_qnnpack.a
    ${TORCH_LIB_DIR}/libpthreadpool.a
    ${TORCH_LIB_DIR}/libeigen_blas.a
    ${TORCH_LIB_DIR}/libcpuinfo.a
    ${TORCH_LIB_DIR}/libclog.a
    ${TORCH_LIB_DIR}/libVulkanWrapper.a