tflite-rs icon indicating copy to clipboard operation
tflite-rs copied to clipboard

Support building on aarch64-apple-darwin

Open schell opened this issue 4 years ago • 6 comments
trafficstars

Hiya, first - thanks for your crate! I'm attempting to build on an M1 Mac and am running into the following error:

  --- stderr
  make command = "make" "TARGET=aarch64" "TARGET_ARCH=aarch64" "-j" "8" "BUILD_WITH_NNAPI=false" "-f" "tensorflow/lite/tools/make/Makefile" "micro" in dir  "/Users/my_user/path/target/debug/build/tflite-6dad9b7c59365c51/out/tensorflow"
  make: aarch64-linux-gnu-g++: No such file or directory
  make: aarch64-linux-gnu-g++: No such file or directory
  make: aarch64-linux-gnu-g++: No such file or directory
  make: aarch64-linux-gnu-g++: No such file or directory
  make: *** [/Users/my_user/path/target/debug/build/tflite-6dad9b7c59365c51/out/tensorflow/tensorflow/lite/tools/make/gen/aarch64_aarch64/obj/tensorflow/lite/core/api/error_reporter.o] Error 1
  make: *** Waiting for unfinished jobs....
  make: aarch64-linux-gnu-gcc: No such file or directory
  make: aarch64-linux-gnu-g++: No such file or directory
  make: *** [/Users/my_user/path/target/debug/build/tflite-6dad9b7c59365c51/out/tensorflow/tensorflow/lite/tools/make/gen/aarch64_aarch64/obj/tensorflow/lite/core/api/op_resolver.o] Error 1
  make: *** [/Users/my_user/path/target/debug/build/tflite-6dad9b7c59365c51/out/tensorflow/tensorflow/lite/tools/make/gen/aarch64_aarch64/obj/tensorflow/lite/core/subgraph.o] Error 1
  /bin/bash: aarch64-linux-gnu-g++: command not found
  make: aarch64-linux-gnu-g++: No such file or directory
  make: *** [/Users/my_user/path/target/debug/build/tflite-6dad9b7c59365c51/out/tensorflow/tensorflow/lite/tools/make/gen/aarch64_aarch64/obj/tensorflow/lite/core/api/flatbuffer_conversions.o] Error 1
  make: *** [/Users/my_user/path/target/debug/build/tflite-6dad9b7c59365c51/out/tensorflow/tensorflow/lite/tools/make/gen/aarch64_aarch64/obj/tensorflow/lite/experimental/resource_variable/*.o] Error 127
  make: *** [/Users/my_user/path/target/debug/build/tflite-6dad9b7c59365c51/out/tensorflow/tensorflow/lite/tools/make/gen/aarch64_aarch64/obj/tensorflow/lite/c/c_api_internal.o] Error 1
  make: *** [/Users/my_user/path/target/debug/build/tflite-6dad9b7c59365c51/out/tensorflow/tensorflow/lite/tools/make/gen/aarch64_aarch64/obj/tensorflow/lite/arena_planner.o] Error 1
  make: *** [/Users/my_user/path/target/debug/build/tflite-6dad9b7c59365c51/out/tensorflow/tensorflow/lite/tools/make/gen/aarch64_aarch64/obj/tensorflow/lite/allocation.o] Error 1
  thread 'main' panicked at 'Failed to build tensorflow', /Users/schell/.cargo/registry/src/github.com-1ecc6299db9ec823/tflite-0.9.4/build.rs:143:17

Furthermore, it looks like this may be an upstream error in tensorflow as their makefile seems to assume cross-compilation https://github.com/tensorflow/tensorflow/blob/5d80e1e8e6ee999be7db39461e0e79c90403a2e4/tensorflow/lite/tools/make/targets/aarch64_makefile.inc#L1-L6

Maybe this is possible to fix in build.rs?

schell avatar Feb 04 '21 01:02 schell

Changing this line in build.rs: https://github.com/boncheolgu/tflite-rs/blob/5346165a8e7517546a38cf5a9d183d3307d8b9fc/build.rs#L104

to

let target = &os;

fixes the problem for me.

~~Also - I had do git submodule init && git submodule update --recursive before running cargo build when building tflite-rs, how does this happen when pulling tflite-rs in as a dependency? Is there some magic doing that step?~~

There is magic! Cargo magic.

Thanks again :)

schell avatar Feb 04 '21 02:02 schell

Changing build.rs as above allows the library to build, but the test suite doesn't link. It seems that libtflite is still being built for x86_64. I have multiple linker errors when running cargo test:

...
  = note: ld: in /Users/me/tflite-rs/target/debug/deps/libtflite-feb96be6dbb85303.rlib(interpreter.o), archive member 'interpreter.o' with length 17456 is not mach-o or llvm bitcode for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
...
= note: ld: warning: ignoring file /Users/me/tflite-rs/target/debug/build/tflite-a4cf886b9a9b0210/out/libtensorflow-lite.a, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
          Undefined symbols for architecture arm64:
            "tflite::ops::builtin::BuiltinOpResolver::BuiltinOpResolver()", referenced from:
                __cpp_closure_5487857119937832276_impl() in librust_cpp_generated.a(cpp_closures.o)
            "tflite::DefaultErrorReporter()", referenced from:
                __cpp_closure_15728844474447527566_impl(char const* const&, unsigned long const&) in librust_cpp_generated.a(cpp_closures.o)
            "tflite::InterpreterBuilder::operator()(std::__1::unique_ptr<tflite::Interpreter, std::__1::default_delete<tflite::Interpreter> >*)", referenced from:
                __cpp_closure_4312898919749812071_impl(tflite::InterpreterBuilder* const&) in librust_cpp_generated.a(cpp_closures.o)
            "tflite::InterpreterBuilder::~InterpreterBuilder()", referenced from:
                ___cpp_closure_14336757939743675283 in librust_cpp_generated.a(cpp_closures.o)
            "tflite::Interpreter::Invoke()", referenced from:
                __cpp_closure_11793350538227341524_impl(tflite::Interpreter* const&) in librust_cpp_generated.a(cpp_closures.o)
            "tflite::FlatBufferModel::BuildFromBuffer(char const*, unsigned long, tflite::ErrorReporter*)", referenced from:
                __cpp_closure_15728844474447527566_impl(char const* const&, unsigned long const&) in librust_cpp_generated.a(cpp_closures.o)
            "tflite::FlatBufferModel::~FlatBufferModel()", referenced from:
                ___cpp_closure_618643496048306620 in librust_cpp_generated.a(cpp_closures.o)
                std::__1::default_delete<tflite::FlatBufferModel>::operator()(tflite::FlatBufferModel*) const in librust_cpp_generated.a(cpp_closures.o)
            "tflite::Interpreter::~Interpreter()", referenced from:
                ___cpp_closure_14875002098205916398 in librust_cpp_generated.a(cpp_closures.o)
                std::__1::default_delete<tflite::Interpreter>::operator()(tflite::Interpreter*) const in librust_cpp_generated.a(cpp_closures.o)
            "tflite::InterpreterBuilder::InterpreterBuilder(tflite::FlatBufferModel const&, tflite::OpResolver const&)", referenced from:
                __cpp_closure_5789543540351621158_impl(tflite::FlatBufferModel const* const&, tflite::OpResolver const* const&) in librust_cpp_generated.a(cpp_closures.o)
            "tflite::Interpreter::AllocateTensors()", referenced from:
                __cpp_closure_436463730886203910_impl(tflite::Interpreter* const&) in librust_cpp_generated.a(cpp_closures.o)
          ld: symbol(s) not found for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

schell avatar Feb 05 '21 01:02 schell

It also seems that std::process::Command may be selecting the x86_64 shell https://github.com/rust-lang/cargo/issues/9158, which causes make to set tensorflow's Makefile::HOST_ARCH variable to x86_64.

schell avatar Feb 09 '21 21:02 schell

I was building w/ my editor which was causing cargo to choose an x86_64 shell, which in turn set the host architecture to x86_64. After making the initial target change in the comment above tflite-rs builds, but does not pass tests because of a problem that seems to exist on x86 and arm, having to do with memory layout.

schell avatar Feb 10 '21 00:02 schell

Just ran into the same issue. Any updates ?

azw413 avatar Apr 14 '22 15:04 azw413

@azw413 I've just been maintaining my own fork :(

schell avatar Apr 16 '22 08:04 schell