pytorch / cpuinfo

CPU INFOrmation library (x86/x86-64/ARM/ARM64, Linux/Windows/Android/macOS/iOS)
BSD 2-Clause "Simplified" License
990 stars 310 forks source link

How to cross-compile pytorch-cpuinfo? #131

Closed pietermarsman closed 1 year ago

pietermarsman commented 1 year ago

Hi!

First a bit of context. I'm trying to build onnxruntime for raspberry pi using cross-compilation (instructions here). The onnxruntime package depends on pytorch-cpuinfo and fetches and builds it as part of the build process.

I'm using this command.

VERBOSE=1 ./build.sh --config Release --build_shared_lib --arm --update --build --path_to_protoc_exe /build/bin/protoc

This triggers the following error:

[...]
[ 66%] Building C object _deps/pytorch_cpuinfo-build/CMakeFiles/cpuinfo.dir/src/x86/init.c.o
cd /build/onnxruntime/build/Linux/Release/_deps/pytorch_cpuinfo-build && /usr/bin/arm-linux-gnueabihf-gcc -DCPUINFO_LOG_LEVEL=2 -DEIGEN_MPL2_ONLY -DORT_ENABLE_STREAM -D_GNU_SOURCE=1 -I/build/onnxruntime/build/Linux/Release/_deps/pytorch_cpuinfo-src/src -I/build/onnxruntime/build/Linux/Release/_deps/pytorch_cpuinfo-src/include -I/build/onnxruntime/build/Linux/Release/_deps/pytorch_cpuinfo-src/deps/clog/include -ffunction-sections -fdata-sections -Wno-error=attributes -O3 -DNDEBUG -fPIC -std=c99 -MD -MT _deps/pytorch_cpuinfo-build/CMakeFiles/cpuinfo.dir/src/x86/init.c.o -MF CMakeFiles/cpuinfo.dir/src/x86/init.c.o.d -o CMakeFiles/cpuinfo.dir/src/x86/init.c.o -c /build/onnxruntime/build/Linux/Release/_deps/pytorch_cpuinfo-src/src/x86/init.c
In file included from /build/onnxruntime/build/Linux/Release/_deps/pytorch_cpuinfo-src/src/x86/init.c:5:
/build/onnxruntime/build/Linux/Release/_deps/pytorch_cpuinfo-src/src/x86/cpuid.h:5:11: fatal error: cpuid.h: No such file or directory
    5 |  #include <cpuid.h>
      |           ^~~~~~~~~
compilation terminated.
gmake[2]: *** [_deps/pytorch_cpuinfo-build/CMakeFiles/cpuinfo.dir/build.make:118: _deps/pytorch_cpuinfo-build/CMakeFiles/cpuinfo.dir/src/x86/init.c.o] Error 1
gmake[2]: Leaving directory '/build/onnxruntime/build/Linux/Release'
gmake[1]: *** [CMakeFiles/Makefile2:5506: _deps/pytorch_cpuinfo-build/CMakeFiles/cpuinfo.dir/all] Error 2
gmake[1]: Leaving directory '/build/onnxruntime/build/Linux/Release'
[...]

My take on this is that pytorch-cpuinfo errorneously tries to compile for x86 (the host for the cross-compile).

Looking at the CMakeList.txt in this project, I think the culprit is that it always assumes that the host architecture is also the target architecture: https://github.com/pytorch/cpuinfo/blob/3dc310302210c1891ffcfb12ae67b11a3ad3a150/CMakeLists.txt#L59

Would love to hear if I'm doing something wrong. Or if I can submit a PR for this to allow to override the target architecture from the environment variables.

Maratyszcza commented 1 year ago

You need a CMake toolchain file for cross-compilation. See e.g. scripts/android-arm64-build.sh for an example of cross-compilation.

pietermarsman commented 1 year ago

Thank you! That set me in the right direction!