bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.05k stars 4.03k forks source link

Using bazel to cross-compile tensorflow for other targets. #1353

Closed sachinpro closed 7 years ago

sachinpro commented 8 years ago

I compiled the tensorflow source using https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/label_image example for C++ on Ubuntu 14.04. Now I want to learn how to cross-compile for some other target architecture. I started with ARM. I installed the arm-linux-gnueabi-gcc tools and edited the bazel/tools/cpp/CROSSTOOL.tpl file and its android toolchain for the armeabi-v7a cpu, so that, my new toolchain looks like this :

toolchain { abi_version: "armeabi-v7a" abi_libc_version: "armeabi-v7a" builtin_sysroot: "" compiler: "compiler" host_system_name: "armeabi-v7a" needsPic: true supports_gold_linker: false supports_incremental_linker: false supports_fission: false supports_interface_shared_objects: false supports_normalizing_ar: false supports_start_end_lib: false supports_thin_archives: false target_libc: "armeabi-v7a" target_cpu: "armeabi-v7a" target_system_name: "armeabi-v7a" toolchain_identifier: "stub_armeabi-v7a"

tool_path { name: "ar" path: "/usr/bin/arm-linux-gnueabi-ar" } tool_path { name: "compat-ld" path: "/bin/false" } tool_path { name: "cpp" path: "/usr/bin/arm-linux-gnueabi-cpp" } tool_path { name: "dwp" path: "/bin/false" } tool_path { name: "gcc" path: "/usr/bin/arm-linux-gnueabi-gcc" } tool_path { name: "gcov" path: "/bin/false" } tool_path { name: "ld" path: "/usr/bin/arm-linux-gnueabi-ld" }

tool_path { name: "nm" path: "/bin/false" } tool_path { name: "objcopy" path: "/bin/false" } tool_path { name: "objdump" path: "/bin/false" } tool_path { name: "strip" path: "/bin/false" } linking_mode_flags { mode: DYNAMIC } }

But, when compiling tensorflow label_image using the command $bazel build --cpu=armeabi-v7a -s tensorflow/examples/label_image/... , I get an error:

INFO: Found 2 targets... ERROR: /home/sachin/.cache/bazel/_bazel_sachin/dc84bb70b01d0f4a792c48d02549c4f8/external/highwayhash/BUILD:17:1: C++ compilation of rule '@highwayhash//:sip_hash' failed: arm-linux-gnueabi-gcc failed: error executing command /usr/bin/arm-linux-gnueabi-gcc '-frandom-seed=bazel-out/stub_armeabi-v7a-fastbuild/bin/external/highwayhash/_objs/sip_hash/external/highwayhash/highwayhash/sip_hash.pic.o' -fPIC -iquote ... (remaining 20 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1. In file included from external/highwayhash/highwayhash/sip_hash.h:23:0, from external/highwayhash/highwayhash/sip_hash.cc:15: external/highwayhash/highwayhash/statehelpers.h: In function 'void highwayhash::UpdateState(const char, highwayhash::uint64, State_)': external/highwayhash/highwayhash/state_helpers.h:36:76: error: there are no arguments to 'static_assert' that depend on a template parameter, so a declaration of 'static_assert' must be available [-fpermissive] external/highwayhash/highwayhash/state_helpers.h:36:76: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated) In file included from external/highwayhash/highwayhash/sip_hash.cc:15:0: external/highwayhash/highwayhash/sip_hash.h: At global scope: external/highwayhash/highwayhash/sip_hash.h:31:9: error: expected nested-name-specifier before 'Key' external/highwayhash/highwayhash/sip_hash.h:31:9: error: using-declaration for non-member at class scope external/highwayhash/highwayhash/sip_hash.h:31:13: error: expected ';' before '=' token external/highwayhash/highwayhash/sip_hash.h:31:13: error: expected unqualified-id before '=' token

I am pretty clueless about these errors and I am new to bazel. Is there anything more I have to do?If yes, can someone tell me what steps I have to follow to have my build for different architectures? I am using bazel 0.2.3.

Thanks in advance.

uvaidya commented 8 years ago

Sure will do this, and let you know Thanks

uvaidya commented 8 years ago

did this : _ln -s tools/arm_compiler/linaro_linux_gcc/clang_bin/as tools/arm_compiler/linaro_linux_gcc/clangbin/arm-linux-gnueabihf-as _ln -s tools/arm_compiler/linaro_linux_gcc/clang_bin/ld tools/arm_compiler/linaro_linux_gcc/clangbin/arm-linux-gnueabihf-ld

But got same error ...

uvaidya commented 8 years ago

Hi Sachin made mistake in above command , basically the wrapper scripts in arm_compiler should be pointed to by a symlink inside clang-bin right ? Also to create symlink in another folder , we need to give full path of file . After doing above atleast above mentioned error is not seen. :-) . Thanks for pointing it out .

sachinpro commented 8 years ago

Yes , you need to give full path. Are there any more errors?

uvaidya commented 8 years ago

Build is going on , will let you know once its finished .

uvaidya commented 8 years ago

Compilation got completed successfully .Thanks Sachin. one more thing, Now this binary should run on arm based android device, right ?

I pushed it to android device under system/bin did chmod +x also , but when gave label_image as an argument for execution of file it returned error: "No such file or directory" . Have you executed this binary on arm based android device ?

Thanks

sachinpro commented 8 years ago

I have not yet executed this binary on an arm device. I am working on it . Will let you know when I have done that.

uvaidya commented 8 years ago

ok , Please let me know . Thanks

uvaidya commented 8 years ago

Hi SachinPro ,

I checked binary created with cross compilation when performed with arm toolchain , details of file goes as follows :- ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, BuildID[md5/uuid]=0xf2fe70777ae59604b558c8c774fb5148, not stripped

details of default binary, which we build with bazel build //project_path is :-

ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[md5/uuid]=0x19510cf66ad7bedd2213e43a4f4e8ad1, not stripped

binary for arm is not executable instead its shared object .What needs to be done to make arm binary executable ? Do we need to have some change in toolchain which we made referring to https://github.com/bazelbuild/bazel/wiki/Building-with-a-custom-toolchain ?

sachinpro commented 8 years ago

Read this http://askubuntu.com/questions/690631/executables-vs-shared-objects . As such, there is no difference between them.

ghost commented 8 years ago

Did you ever succeed with this? I'm in a similar predicament, except I'm compiling another piece of deep learning code, not TensorFlow. Google should get its act together on this and start making sure its publicly released stuff builds with ARM and POWER. I know they do this internally, so it shouldn't be that much work.

rasmi commented 8 years ago

I'm also running into a similar error with external/highwayhash/highwayhash/state_helpers.h. Opened an issue for my particular error here.

vikas1981 commented 7 years ago

I am seeing this error:

ERROR: /home/ec2-user/src/tensorflow/tensorflow/core/BUILD:829:1: undeclared inclusion(s) in rule '//tensorflow/core:lib_internal': this rule is missing dependency declarations for the following files included by 'tensorflow/core/lib/png/png_io.cc': '/root/.cache/bazel/_bazel_root/2b8dc63c6c17d4d5be378207b06795d6/external/zlib_archive/zlib.h' '/root/.cache/bazel/_bazel_root/2b8dc63c6c17d4d5be378207b06795d6/external/zlib_archive/zconf.h'. In file included from /usr/include/string.h:25:0, from tensorflow/core/lib/png/png_io.cc:18: /usr/include/features.h:330:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]

warning _FORTIFY_SOURCE requires compiling with optimization (-O)

^

Target //tensorflow/examples/image_retraining:retrain failed to build

rasmi commented 7 years ago

@vikas1981 it seems you're missing zlib headers. Try running sudo apt-get install zlib1g-dev and running the build again.

vikas1981 commented 7 years ago

Thanks for replying: zlib is already installed

Loaded plugins: dkms-build-requires, priorities, update-motd, upgrade-helper 980 packages excluded due to repository priority protections Package zlib-devel-1.2.8-7.18.amzn1.x86_64 already installed and latest version Nothing to do

rasmi commented 7 years ago

@vikas1981 What build command are you using? Make sure you include -c opt in your bazel build command.

bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
StephenLeeUSTC commented 7 years ago

@uvaidya @uvaidya Wow, i am doing almost the same thing as you two!!! And, could you please tell me the basic version of tensorflow in https://github.com/sachinpro/sachinpro.github.io.git? thanks

madhutummi commented 7 years ago

@rasmi I am getting similiar error reported by @vikas1981 , but I do not "get warning _FORTIFY_SOURCE" warning

I am using bazel 3.0.2 with customized TF.

Can you please help?

Hi @vikas1981 your issue got fixed?

"ERROR: tensorflow/core/BUILD:853:1: undeclared inclusion(s) in rule '//tensorflow/core:lib_internal': this rule is missing dependency declarations for the following files included by 'tensorflow/core/lib/png/png_io.cc': ~/.cache/bazel/_bazel_madhu/a9aabe45cf3d94341ef4fb777deb58c5/external/zlib_archive/zlib.h' ~/.cache/bazel/_bazel_madhu/a9aabe45cf3d94341ef4fb777deb58c5/external/zlib_archive/zconf.h'."

hlopko commented 7 years ago

I'll close this issue now, I think it's more of a discussion thread than an issue. Feel free to continue on bazel-discuss or stack overflow. Thanks everybody!

gopinath-r commented 7 years ago

Hi, I have followed all the steps provided above and now i am getting this error. /home/gopinathr/Documents/work/tensorflow/tools/arm_compiler/BUILD:51:1: in cc_toolchain rule //tools/arm_compiler:cc-compiler-armeabi-v7a: The package '@linaro_linux_gcc_repo' is not valid

I am trying to build tensorflow as a pip package using cross compiler toolchains.

bazel build --crosstool_top=//tools/arm_compiler:toolchain --cpu=armeabi-v7a //tensorflow/tools/pip_package:build_pip_package

Build, Crosstool, Workspace file i am using are WORKSPACE.txt BUILD.txt CROSSTOOL.txt

Could anyone help out what is wrong in my setup

lhelontra commented 7 years ago

@gopinath-r try this: https://github.com/lhelontra/tensorflow-on-arm/blob/master/build_tensorflow/patchs.sh#L119