Cross compile tensorflow for armv7l targets via bazel

Refer:
https://github.com/bazelbuild/bazel/wiki/Building-with-a-custom-toolchain
https://www.tensorflow.org/tutorials/image_recognition



First  you should  have a cross tool chain like:
ls /home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64
armv7l-linux-gnueabi  bin  include  lib  libexec  share


Second, please followed the instructions to download the source installation of TensorFlow for your platform.

1. Modify WORKSPACE for tensorflow:


~/tensorflow$ git diff WORKSPACE  
diff --git a/WORKSPACE b/WORKSPACE
index e4f4c80..a0e2c06 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -516,3 +516,10 @@ http_file(
   name = "weblas_weblas_js",
   url = "https://raw.githubusercontent.com/waylonflinn/weblas/v0.9.0/dist/weblas.js",
 )
+
+
+new_local_repository(
+    name = 'toolchain_target_armv7l',
+    path = '/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64',
+    build_file = 'armv7l-compiler/cross_toolchain_target_armv7l_host_x86-64.BUILD'
+)


2. Prepare cross tool configurations:
 
~/tensorflow$ ls armv7l-compiler
BUILD  CROSSTOOL  cross_toolchain_armv7l_host_x86-64.BUILD


~/tensorflow/armv7l-compiler$ cat cross_toolchain_armv7l_host_x86-64.BUILD    
package(default_visibility = ['//visibility:public'])

filegroup(
  name = 'gcc',
  srcs = [
    'bin/armv7l-tizen-linux-gnueabi-gcc',
  ],
)

filegroup(
  name = 'ar',
  srcs = [
    'bin/armv7l-tizen-linux-gnueabi-ar',
  ],
)

filegroup(
  name = 'ld',
  srcs = [
    'bin/armv7l-tizen-linux-gnueabi-ld',
  ],
)

filegroup(
  name = 'nm',
  srcs = [
    'bin/armv7l-tizen-linux-gnueabi-nm',
  ],
)

filegroup(
  name = 'objcopy',
  srcs = [
    'bin/armv7l-tizen-linux-gnueabi-objcopy',
  ],
)

filegroup(
  name = 'objdump',
  srcs = [
    'bin/armv7l-tizen-linux-gnueabi-objdump',
  ],
)

filegroup(
  name = 'strip',
  srcs = [
    'bin/armv7l-tizen-linux-gnueabi-strip',
  ],
)

filegroup(
  name = 'as',
  srcs = [
    'bin/armv7l-tizen-linux-gnueabi-as',
  ],
)

filegroup(
  name = 'compiler_pieces',
  srcs = glob([
    'armv7l-tizen-linux-gnueabi/**',
    'libexec/**',
    'lib/gcc/armv7l-tizen-linux-gnueabi/**',
    'include/**',
  ]),
)

filegroup(
  name = 'compiler_components',
  srcs = [
    ':gcc',
    ':ar',
    ':ld',
    ':nm',
    ':objcopy',
    ':objdump',
    ':strip',
    ':as',
  ],


~/tensorflow/armv7l-compiler$ cat CROSSTOOL 
major_version: "local"
minor_version: ""
default_target_cpu: "armv7l"

default_toolchain {
  cpu: "armv7l"
  toolchain_identifier: "armv7l"
}

default_toolchain {
  cpu: "k8"
  toolchain_identifier: "local"
}

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

  tool_path { name: "ar" path: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/bin/armv7l-tizen-linux-gnueabi-ar" }
  tool_path { name: "compat-ld" path: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/bin/armv7l-tizen-linux-gnueabi-ld" }
  tool_path { name: "cpp" path: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/bin/armv7l-tizen-linux-gnueabi-cpp" }
  tool_path { name: "dwp" path: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/bin/armv7l-tizen-linux-gnueabi-dwp" }
  tool_path { name: "gcc" path: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/bin/armv7l-tizen-linux-gnueabi-gcc" }
  tool_path { name: "gcov" path: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/bin/armv7l-tizen-linux-gnueabi-gcov" }
  tool_path { name: "ld" path: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/bin/armv7l-tizen-linux-gnueabi-ld" }
  tool_path { name: "nm" path: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/bin/armv7l-tizen-linux-gnueabi-nm" }
  tool_path { name: "objcopy" path: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/bin/armv7l-tizen-linux-gnueabi-objcopy" }
  objcopy_embed_flag: "-I"
  objcopy_embed_flag: "binary"
  tool_path { name: "objdump" path: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/bin/armv7l-tizen-linux-gnueabi-objdump" }
  tool_path { name: "strip" path: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/bin/armv7l-tizen-linux-gnueabi-strip" }

  cxx_builtin_include_directory: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/include"
  cxx_builtin_include_directory: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/lib/gcc/armv7l-tizen-linux-gnueabi/6.2.1/include"
  cxx_builtin_include_directory: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/armv7l-tizen-linux-gnueabi/sys-root/usr/include"
  cxx_builtin_include_directory: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/armv7l-tizen-linux-gnueabi/include"
  cxx_builtin_include_directory: "/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/lib/gcc/armv7l-tizen-linux-gnueabi/6.2.1/include-fixed"

  linker_flag: "--sysroot=/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/armv7l-tizen-linux-gnueabi/sys-root"
  linker_flag: "-lstdc++"
  linker_flag: "-L/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/lib"
  linker_flag: "-L/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/armv7l-tizen-linux-gnueabi/sys-root/lib"
  linker_flag: "-L/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/armv7l-tizen-linux-gnueabi/sys-root/usr/lib"
  linker_flag: "-L/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64/armv7l-tizen-linux-gnueabi/lib"

  # Anticipated future default.
  # This makes GCC and Clang do what we want when called through symlinks.
  unfiltered_cxx_flag: "-no-canonical-prefixes"
  linker_flag: "-no-canonical-prefixes"

  # Make C++ compilation deterministic. Use linkstamping instead of these
  # compiler symbols.
  unfiltered_cxx_flag: "-Wno-builtin-macro-redefined"
  unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
  unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
  unfiltered_cxx_flag: "-D__TIME__=\"redacted\""

  # Security hardening on by default.
  # Conservative choice; -D_FORTIFY_SOURCE=2 may be unsafe in some cases.
  # We need to undef it before redefining it as some distributions now have
  # it enabled by default.
  compiler_flag: "-U_FORTIFY_SOURCE"
  compiler_flag: "-fstack-protector"
  compiler_flag: "-fPIE"
  linker_flag: "-pie"
  linker_flag: "-Wl,-z,relro,-z,now"

  # Enable coloring even if there's no attached terminal. Bazel removes the
  # escape sequences if --nocolor is specified.
  compiler_flag: "-fdiagnostics-color=always"

    # All warnings are enabled. Maybe enable -Werror as well?
  compiler_flag: "-Wall"
  # Enable a few more warnings that aren't part of -Wall.
  compiler_flag: "-Wunused-but-set-parameter"
  # But disable some that are problematic.
  compiler_flag: "-Wno-free-nonheap-object" # has false positives

  # Keep stack frames for debugging, even in opt mode.
  compiler_flag: "-fno-omit-frame-pointer"

  # Stamp the binary with a unique identifier.
  linker_flag: "-Wl,--build-id=md5"
  linker_flag: "-Wl,--hash-style=gnu"

  compilation_mode_flags {
    mode: DBG
    # Enable debug symbols.
    compiler_flag: "-g"
  }
  compilation_mode_flags {
    mode: OPT

    # No debug symbols.
    # Maybe we should enable https://gcc.gnu.org/wiki/DebugFission for opt or
    # even generally? However, that can't happen here, as it requires special
    # handling in Bazel.
    compiler_flag: "-g0"

    # Conservative choice for -O
    # -O3 can increase binary size and even slow down the resulting binaries.
    # Profile first and / or use FDO if you need better performance than this.
    compiler_flag: "-O2"

    # Disable assertions
    compiler_flag: "-DNDEBUG"

    # Removal of unused code and data at link time (can this increase binary size in some cases?).
    compiler_flag: "-ffunction-sections"
    compiler_flag: "-fdata-sections"
    linker_flag: "-Wl,--gc-sections"
  }
}

toolchain {
  toolchain_identifier: "local"
  abi_libc_version: "local"
  abi_version: "local"
  builtin_sysroot: ""
  compiler: "compiler"
  compiler_flag: "-U_FORTIFY_SOURCE"
  compiler_flag: "-D_FORTIFY_SOURCE=2"
  compiler_flag: "-fstack-protector"
  compiler_flag: "-Wall"
  compiler_flag: "-Wl,-z,-relro,-z,now"
  compiler_flag: "-B/usr/bin"
  compiler_flag: "-B/usr/bin"
  compiler_flag: "-Wunused-but-set-parameter"
  compiler_flag: "-Wno-free-nonheap-object"
  compiler_flag: "-fno-omit-frame-pointer"
  cxx_builtin_include_directory: "/usr/include/c++/4.8"
  cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu/c++/4.8"
  cxx_builtin_include_directory: "/usr/include/c++/4.8/backward"
  cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/4.8/include"
  cxx_builtin_include_directory: "/usr/local/include"
  cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed"
  cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu"
  cxx_builtin_include_directory: "/usr/include"
  cxx_flag: "-std=c++0x"
  host_system_name: "local"
  linker_flag: "-lstdc++"
  linker_flag: "-lm"
  linker_flag: "-Wl,-no-as-needed"
  linker_flag: "-B/usr/bin"
  linker_flag: "-B/usr/bin"
  linker_flag: "-pass-exit-codes"
  needsPic: true
  objcopy_embed_flag: "-I"
  objcopy_embed_flag: "binary"
  supports_fission: false
  supports_gold_linker: false
  supports_incremental_linker: false
  supports_interface_shared_objects: false
  supports_normalizing_ar: false
  supports_start_end_lib: false
  supports_thin_archives: false
  target_cpu: "k8"
  target_libc: "local"
  target_system_name: "local"
  unfiltered_cxx_flag: "-fno-canonical-system-headers"
  unfiltered_cxx_flag: "-Wno-builtin-macro-redefined"
  unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
  unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
  unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
  tool_path {name: "ar" path: "/usr/bin/ar" }
  tool_path {name: "cpp" path: "/usr/bin/cpp" }
  tool_path {name: "dwp" path: "/usr/bin/dwp" }
  tool_path {name: "gcc" path: "/usr/bin/gcc" }
  tool_path {name: "gcov" path: "/usr/bin/gcov" }
  tool_path {name: "ld" path: "/usr/bin/ld" }
  tool_path {name: "nm" path: "/usr/bin/nm" }
  tool_path {name: "objcopy" path: "/usr/bin/objcopy" }
  tool_path {name: "objdump" path: "/usr/bin/objdump" }
  tool_path {name: "strip" path: "/usr/bin/strip" }

  compilation_mode_flags {
    mode: DBG
    compiler_flag: "-g"
  }
  compilation_mode_flags {
    mode: OPT
    compiler_flag: "-g0"
    compiler_flag: "-O2"
    compiler_flag: "-DNDEBUG"
    compiler_flag: "-ffunction-sections"
    compiler_flag: "-fdata-sections"
    linker_flag: "-Wl,--gc-sections"
  }
  linking_mode_flags { mode: DYNAMIC }
}


~/tensorflow/armv7l-compiler$ cat BUILD
package(default_visibility = ["//visibility:public"])

cc_toolchain_suite(
    name = "toolchain",
    toolchains = {
        "armv7l|compiler": ":cc-compiler-armv7l",
        "k8|compiler": ":cc-compiler-local",
    },
)

filegroup(
    name = "empty",
    srcs = [],
)

filegroup(
    name = "arm_linux_all_files",
    srcs = [
        "@toolchain_target_armv7l//:compiler_pieces",
    ],
)

cc_toolchain(
    name = "cc-compiler-local",
    all_files = ":empty",
    compiler_files = ":empty",
    cpu = "local",
    dwp_files = ":empty",
    dynamic_runtime_libs = [":empty"],
    linker_files = ":empty",
    objcopy_files = ":empty",
    static_runtime_libs = [":empty"],
    strip_files = ":empty",
    supports_param_files = 1,
)

cc_toolchain(
    name = "cc-compiler-armv7l",
    all_files = ":arm_linux_all_files",
    compiler_files = ":arm_linux_all_files",
    cpu = "armv7l",
    dwp_files = ":empty",
    dynamic_runtime_libs = [":empty"],
    linker_files = ":arm_linux_all_files",
    objcopy_files = "arm_linux_all_files",
    static_runtime_libs = [":empty"],
    strip_files = "arm_linux_all_files",
    supports_param_files = 1,
)


3. Configure tensorflow:

~/tensorflow$ ./configure
Please specify the location of python. [Default is /usr/bin/python]:
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: -march=armv7-a
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N]
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]
No XLA support will be enabled for TensorFlow
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]

Using python library path: /usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with OpenCL support? [y/N]
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N]
No CUDA support will be enabled for TensorFlow
Configuration finished
......................
INFO: All external dependencies fetched successfully.



armv7l-tizen-linux-gnueabi-gcc: error: unrecognized argument in option '-march=native'
armv7l-tizen-linux-gnueabi-gcc: note: valid arguments to '-march=' are: armv2 armv2a armv3 armv3m armv4 armv4t armv5 armv5e armv5t armv5te armv6 armv6-m armv6j armv6k armv6kz armv6s-m armv6t2 armv6z armv6zk armv7 armv7-a armv7-m armv7-r armv7e-m armv7ve armv8-a armv8-a+crc armv8-m.base armv8-m.main armv8-m.main+dsp armv8.1-a armv8.1-a+crc armv8.2-a armv8.2-a+fp16 iwmmxt iwmmxt2 native

Please remember specify -march=armv7-a here.

It rely on your target's march. 

4. Let's do cross build:
bazel build --crosstool_top=//armv7l-compiler:toolchain --cpu=armv7l --config=opt tensorflow/examples/label_image/...
INFO: Found 2 targets...

INFO: Elapsed time: 231.657s, Critical Path: 77.85s

specify crosstool via --crosstool_top
--crosstool_top=//armv7l-compiler:toolchain 

specify cpu
--cpu=armv7l  

build target here is label_image
tensorflow/examples/label_image/... 

You may need to change some codes if you encountered compile errors related: 
diff --git a/tensorflow/core/platform/platform.h b/tensorflow/core/platform/platform.h
index 7c44b22..cb720eb 100644
--- a/tensorflow/core/platform/platform.h
+++ b/tensorflow/core/platform/platform.h
@@ -45,7 +45,7 @@ limitations under the License.

 // Since there's no macro for the Raspberry Pi, assume we're on a mobile
 // platform if we're compiling for the ARM CPU.
-#define IS_MOBILE_PLATFORM
+// #define IS_MOBILE_PLATFORM


diff --git a/tensorflow/stream_executor/platform/default/mutex.h b/tensorflow/stream_executor/platform/default/mutex.h
index f28a2c9..d72fe42 100644
--- a/tensorflow/stream_executor/platform/default/mutex.h
+++ b/tensorflow/stream_executor/platform/default/mutex.h
@@ -23,7 +23,7 @@ limitations under the License.

 // std::shared_timed_mutex is a C++14 feature.
 #if (__cplusplus >= 201402L)
-#define STREAM_EXECUTOR_USE_SHARED_MUTEX
+//#define STREAM_EXECUTOR_USE_SHARED_MUTEX

 #endif  // __cplusplus >= 201402L

 #ifdef STREAM_EXECUTOR_USE_SHARED_MUTEX

Once build done, your arm target will be here:


~/tensorflow$ ls bazel-bin/tensorflow/examples/label_image/label_image
bazel-bin/tensorflow/examples/label_image/label_image


5.  Try label_image on your real target:


sh-3.2# ./label_image  --image=/lion.jpg
2017-09-18 18:26:43.869688: I tensorflow/examples/label_image/main.cc:206] lion (292): 0.976528
2017-09-18 18:26:43.869866: I tensorflow/examples/label_image/main.cc:206] stopwatch (827): 0.00053628
2017-09-18 18:26:43.869933: I tensorflow/examples/label_image/main.cc:206] leopard (289): 0.000467814
2017-09-18 18:26:43.869995: I tensorflow/examples/label_image/main.cc:206] cougar (287): 0.000456517
2017-09-18 18:26:43.870055: I tensorflow/examples/label_image/main.cc:206] tiger (293): 0.000347707


Well done,  it recognize a lion with 97% accuracy.

Comments

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. I have a question: where can I download cross_toolchain_target_armv7l_host_x86-64?
      waiting for your reply!

      Delete
    2. it is compiled from linaro-gcc.
      you can refer to:
      https://www.linaro.org/downloads/

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Can I please know how to resolve below issue
    arm-oe-linux-gnueabi-gcc: error: armv7-a: No such file or directory

    ReplyDelete
    Replies
    1. Dear Ashok bugude,

      Would you tell me your cross compile tool absolute path and list all files please ?
      And Your CROSSTOOL configuration please ?
      CROSSTOOL and BUILD

      Best Regards
      Errong Leng

      Delete
    2. I removed --config=opt and managed to resolve the issue. Below is my next issue
      while compiling for 32 bit architecture

      /home/ashok/Documents/{HOME}/das_toolchain/sysroots/aarch64-oe-linux/usr/include/c++/4.9.3/cstdlib:178:10: error: expected unqualified-id before '__int128'
      inline __int128

      Delete
    3. I managed to resolve the error by copying 32 bit cstdlib from linaro

      Now I get
      /home/ashok/Documents/{HOME}/das_toolchain/sysroots/aarch64-oe-linux/usr/include/c++/4.9.3/bits/random.h:106:26: error: expected unqualified-id before '__int128'
      { typedef unsigned __int128 type; };

      But in this case , the 32-bit also has same code

      Delete
    4. Able to resolve the above errors by changing '__int128' to 'long long' for 32 bit

      Delete
  6. Hi

    I am getting one last error while building tensorflow

    ERROR: /home/ashok/Ashok/tensorflow-das/tensorflow/BUILD:481:1: Linking of rule '//tensorflow:libtensorflow_cc.so' failed: link_dynamic_library.sh failed: error executing command external/bazel_tools/tools/cpp/link_dynamic_library.sh no ignored ignored ignored ... (remaining 19 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1
    arm-oe-linux-gnueabi-gcc: error: armv7a-arm-linux-gnueabi: No such file or directory
    arm-oe-linux-gnueabi-gcc: error: unrecognized command line option '-target'
    Target //tensorflow:libtensorflow_cc.so failed to build
    Use --verbose_failures to see the command lines of failed build steps.

    ReplyDelete
    Replies
    1. Resolved it . It was a target issue in cross toolchain.

      Delete
  7. Hi

    I am trying to build label_image.I am getting below error

    ERROR: /home/ashok/Ashok/tensorflow-das/tensorflow/examples/label_image/BUILD:12:1: Linking of rule '//tensorflow/examples/label_image:label_image' failed: arm-oe-linux-gnueabi-gcc failed: error executing command '/home/ashok/Documents/{HOME}/das_toolchain/sysroots/x86_64-oesdk-linux/usr/bin/arm-oe-linux-gnueabi/arm-oe-linux-gnueabi-gcc' -o ... (remaining 15 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1
    bazel-out/clang_linux_armhf-opt/bin/tensorflow/core/liblib_internal.a(png_io.o):png_io.cc:function tensorflow::png::(anonymous namespace)::ErrorHandler(png_struct_def*, char const*): error: undefined reference to 'png_set_longjmp_fn'

    ReplyDelete
  8. I am able to cross compile tensorflow by following the above steps.

    I would like to know how to build tensorflow with opencl support as tensorflow is running slow on arm platform.

    when we do './configure', there is an option to 'build with opencl support'

    But I guess it would be overridden by the setting in cross-toolchain file.
    So can I know if we need to do anything in tensorflow or cross-toolchain file to build with opencl support

    Thanks

    ReplyDelete
    Replies
    1. I guess it will not be overwritten by croos-toolchain.
      You can have a try and configure with opencl.

      Some host compiler and computecpp library are needed.
      Do you wish to build TensorFlow with OpenCL support? [y/N] y
      OpenCL support will be enabled for TensorFlow
      Do you wish to build TensorFlow with CUDA support? [y/N]
      No CUDA support will be enabled for TensorFlow
      Please specify which C++ compiler should be used as the host C++ compiler. [Default is ]: g++^H^H^H^H
      Invalid C++ compiler path. cannot be found
      Please specify which C++ compiler should be used as the host C++ compiler. [Default is ]: /usr/bin/g++
      Please specify which C compiler should be used as the host C compiler. [Default is ]: /ur^H
      Invalid C compiler path. /u cannot be found
      Please specify which C compiler should be used as the host C compiler. [Default is ]: /usr/bin/c^H
      Invalid C compiler path. /usr/bin/ cannot be found
      Please specify which C compiler should be used as the host C compiler. [Default is ]: /usr/bing^H
      Invalid C compiler path. /usr/bin cannot be found
      Please specify which C compiler should be used as the host C compiler. [Default is ]: /usr/bin/gcc
      Please specify the location where ComputeCpp for SYCL 1.2 is installed. [Default is /usr/local/computecpp]:
      Invalid SYCL 1.2 library path. /usr/local/computecpp/lib/libComputeCpp.so cannot be found
      Please specify the location where ComputeCpp for SYCL 1.2 is installed. [Default is /usr/local/computecpp]:

      https://github.com/tensorflow/tensorflow/issues/22

      I am sorry I do not know opencl.
      And if you done tensorflow with opencl support and arm target, would you share us please ?

      Best Regards
      Errong Leng

      Delete
  9. 你好, 非常感谢你的博文, 我按照你的说明试图交叉编译 tensorflow for armv7.

    我有一个问题希望大神能帮忙解答一下: 在你定义toolchain_target_armv7l的时候,我没有在文件夹里找到它呀,它只是个别名指向cross_toolchain_target_armv7l_host_x86-64文件夹吗?我因为不明白这个到底是怎么回事,我在自己编译的时候更改了一下名字,改成了tool_target_armv7. 然后
    当我build的时候,遇到了以下问题:ERROR: /home/yuan/Documents/tensorflow/armv7-compiler/BUILD:31:1: no such package '@tool_target_armv7//': In new_local_repository rule //external:tool_target_armv7 the 'build_file' attribute does not specify an existing file (/home/yuan/Documents/tensorflow/armv7-compiler/cross_toolchain_target_armv7.BUILD does not exist) and referenced by '//armv7-compiler:arm_linux_all_files'
    ERROR: Analysis of target '//tensorflow/examples/label_image:label_image_py' failed; build aborted: Loading failed

    非常希望大神帮忙答疑一下, 困扰了我好几天一直没有弄懂。

    ReplyDelete
    Replies
    1. Hi, Yuan,
      看来你是把
      +new_local_repository(
      + name = 'toolchain_target_armv7l',
      + path = '/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64',
      + build_file = 'armv7l-compiler/cross_toolchain_target_armv7l_host_x86-64.BUILD'
      +)
      改成了
      +new_local_repository(
      + name = 'toolchain_target_armv7',
      + path = '/home/lenger/toolchains/cross_toolchain_target_armv7l_host_x86-64',
      + build_file = 'armv7l-compiler/cross_toolchain_target_armv7.BUILD'
      +)

      既然你把build_file的路径换了,那么你也要把我原本的 cross_toolchain_armv7l_host_x86-64.BUILD文件重新命名为 cross_toolchain_target_armv7.BUILD 文件里里的内容不要变。
      你再试着编译一下,应该可以了。

      ~/tensorflow$ ls armv7l-compiler
      BUILD CROSSTOOL cross_toolchain_armv7l_host_x86-64.BUILD

      ~/tensorflow/armv7l-compiler$ cat cross_toolchain_armv7l_host_x86-64.BUILD

      new_local_repository 是 bazel 工具的workspace文件写法的一个命令。
      具体你可以参照
      https://docs.bazel.build/versions/master/be/workspace.html#new_local_repository
      就像你理解的,name确实只是一个名字,用来引用的,你换名字可以。

      Delete
    2. Hi, 谢谢大神的回答,并且我需要继续向大神求助:
      我想我应该已经按照大神的指点更改了相关的名称,比如:
      new_local_repository(
      name='tool_target_armv7',
      path='/home/yuan/Documents/tensorflow/toolchain/gcc-linaro-arm-linux-gnueabihf',
      build_file='armv7-compiler/cross_toolchain_target_armv7.BUILD'
      )

      这是我加在WORKSPACE里的最后一段,然后我在 armv7-compiler里的文件有:
      yuan@debian:~/Documents/tensorflow/armv7-compiler$ ls
      BUILD CROSSTOOL cross_toolchian_target_armv7.BUILD

      然后还有一处需要改名字的就是在 armv7-compiler/BUILD 里的 这一段
      filegroup(
      name = "arm_linux_all_files",
      srcs = [
      "@tool_target_armv7//:compiler_pieces",
      ],
      )

      我把其他所有遇到armv7l的地方全部改成了armv7.
      Configure的时候-march=armv7.
      我在CROSSTOOL里也已经把交叉编译工具链的地址都改成我下载存放的地址了。 但是我还是会出现这个错误:
      ERROR: /home/yuan/Documents/tensorflow/armv7-compiler/BUILD:31:1: no such package '@tool_target_armv7//': In new_local_repository rule //external:tool_target_armv7 the 'build_file' attribute does not specify an existing file (/home/yuan/Documents/tensorflow/armv7-compiler/cross_toolchain_target_armv7.BUILD does not exist) and referenced by '//armv7-compiler:arm_linux_all_files'

      还有一处我发现不同的是,我下载的交叉编译工具链里没有sys-root这个文件夹,所以在linker-flag 还有cxx_builtin_include_directory 的时候,遇到要添加sys-root的我都省略了,不知道这个会不会有导致错误的原因啊。我下载的工具链是想针对BeagleBone Black 的,下载地址是https://releases.linaro.org/components/toolchain/binaries/latest-7/arm-linux-gnueabihf/
      我选择下载的是gcc-linaro-7.2.1-2017.11-i686_arm-linux-gnueabihf.tar.xz
      解压保存后名为gcc-linaro-arm-linux-gnueabihf,保存在~/Documents/tensorflow/toolchain/

      解压后的目录内容是这样的:
      yuan@debian:~/Documents/tensorflow/toolchain/gcc-linaro-arm-linux-gnueabihf$ ls
      arm-linux-gnueabihf gcc-linaro-7.2.1-2017.11-linux-manifest.txt lib share
      bin include libexec


      yuan@debian:~/Documents/tensorflow/toolchain/gcc-linaro-arm-linux-gnueabihf/arm-linux-gnueabihf$ ls
      bin include lib libc


      请教大神这个交叉工具链的不同会是导致编译错误的原因吗?可是看错误的原因是没有找到tool_target_armv7这个package. 我不明白这个tool_target_armv7到底在这里起什么作用。

      希望我有把我的问题解释清楚,特别期待大神的回复。

      Delete
    3. Hi, Yuan,你的问题应该是这个。
      the 'build_file' attribute does not specify an existing file (/home/yuan/Documents/tensorflow/armv7-compiler/cross_toolchain_target_armv7.BUILD does not exist)

      /home/yuan/Documents/tensorflow/armv7-compiler/cross_toolchain_target_armv7.BUILD
      好像 对于 bazel 来说这个文件不存在哎
      但之前看你ls出来是有这个文件的啊
      真是奇怪哎
      你看看是文件权限问题吗?
      ls -all /home/yuan/Documents/tensorflow/armv7-compiler/cross_toolchain_target_armv7.BUILD
      你可以告知是什么 结果吗?

      Delete
    4. This comment has been removed by the author.

      Delete
  10. Hi 大神, 结果是这样的:
    yuan@debian:~$ ls -all /home/yuan/Documents/tensorflow/armv7-compiler/cross_toolchian_target_armv7.BUILD
    -rw-r--r-- 1 yuan yuan 1046 Jan 25 13:08 /home/yuan/Documents/tensorflow/armv7-compiler/cross_toolchian_target_armv7.BUILD

    ReplyDelete
  11. Hi,大神
    我又按照你的步骤,换了一个toolchain做实验. 这个toolchian里有sys-root这些文件夹。然后Build一会之后出现了以下问题:
    ERROR: /home/yuan/myBBB/raspi/tensorflow/tensorflow/python/BUILD:4684:1: C++ compilation of rule '//tensorflow/python:framework/fast_tensor_util.so' failed (Exit 1)
    In file included from /usr/include/python2.7/Python.h:8:0,
    from bazel-out/armv6-opt/genfiles/tensorflow/python/framework/fast_tensor_util.cpp:4:
    /usr/include/python2.7/pyconfig.h:24:54: fatal error: arm-linux-gnueabihf/python2.7/pyconfig.h: No such file or directory
    # include
    我按照这个链接https://stackoverflow.com/questions/33512541/cross-compiling-gdb-for-arm-with-python-failed 里的说明,下载了相关libpython,并且copy pyconfig.h到 /usr/inlude/python2.7/arm-linux-gnueabihf/python2.7/目录下,还copy到 /usr/local/include下,
    但是运行还是出现同样的错误,请问大神知道这个问题出在哪里吗?谢谢

    ReplyDelete
    Replies
    1. 嗨 Yuan
      抱歉 迟来的回复。
      你的问题解决了吗?
      这个和你的工具链有关系的。
      你看看修改一下CROSSTOOL
      把那个需要的头文件路径添加到下面类似的设置:
      xxx_builtin_include_directory
      再试试看。

      Delete
  12. Hi 大神,多谢你的回复, 按照你的建议那个问题解决了,然后新的问题又出现了:
    ERROR: /home/yuan/.cache/bazel/_bazel_yuan/045f9676c0df23e4a5a8660013723861/external/protobuf_archive/BUILD:656:1: undeclared inclusion(s) in rule '@protobuf_archive//:python/google/protobuf/internal/_api_implementation.so':
    this rule is missing dependency declarations for the following files included by 'external/protobuf_archive/python/google/protobuf/internal/api_implementation.cc':
    '/usr/include/python2.7/Python.h'
    '/usr/include/python2.7/patchlevel.h'
    '/usr/include/python2.7/pyconfig.h'
    '/usr/include/python2.7/pymacconfig.h'
    '/usr/include/python2.7/pyport.h'
    '/usr/include/python2.7/pymath.h'
    '/usr/include/python2.7/pymem.h'
    '/usr/include/python2.7/object.h'
    '/usr/include/python2.7/objimpl.h'
    '/usr/include/python2.7/pydebug.h'
    '/usr/include/python2.7/unicodeobject.h'
    '/usr/include/python2.7/intobject.h'
    '/usr/include/python2.7/boolobject.h'
    '/usr/include/python2.7/longobject.h'
    '/usr/include/python2.7/floatobject.h'
    '/usr/include/python2.7/complexobject.h'
    '/usr/include/python2.7/rangeobject.h'
    '/usr/include/python2.7/stringobject.h'
    '/usr/include/python2.7/memoryobject.h'
    '/usr/include/python2.7/bufferobject.h'
    '/usr/include/python2.7/bytesobject.h'
    '/usr/include/python2.7/bytearrayobject.h'
    '/usr/include/python2.7/tupleobject.h'
    '/usr/include/python2.7/listobject.h'
    '/usr/include/python2.7/dictobject.h'
    '/usr/include/python2.7/enumobject.h'
    '/usr/include/python2.7/setobject.h'
    '/usr/include/python2.7/methodobject.h'
    '/usr/include/python2.7/moduleobject.h'
    '/usr/include/python2.7/funcobject.h'
    '/usr/include/python2.7/classobject.h'
    '/usr/include/python2.7/fileobject.h'
    '/usr/include/python2.7/cobject.h'
    '/usr/include/python2.7/pycapsule.h'
    '/usr/include/python2.7/traceback.h'
    '/usr/include/python2.7/sliceobject.h'
    '/usr/include/python2.7/cellobject.h'
    '/usr/include/python2.7/iterobject.h'
    '/usr/include/python2.7/genobject.h'
    '/usr/include/python2.7/descrobject.h'
    '/usr/include/python2.7/warnings.h'
    '/usr/include/python2.7/weakrefobject.h'
    '/usr/include/python2.7/codecs.h'
    '/usr/include/python2.7/pyerrors.h'
    '/usr/include/python2.7/pystate.h'
    '/usr/include/python2.7/pyarena.h'
    '/usr/include/python2.7/modsupport.h'
    '/usr/include/python2.7/pythonrun.h'
    '/usr/include/python2.7/ceval.h'
    '/usr/include/python2.7/sysmodule.h'
    '/usr/include/python2.7/intrcheck.h'
    '/usr/include/python2.7/import.h'
    '/usr/include/python2.7/abstract.h'
    '/usr/include/python2.7/compile.h'
    '/usr/include/python2.7/code.h'
    '/usr/include/python2.7/eval.h'
    '/usr/include/python2.7/pyctype.h'
    '/usr/include/python2.7/pystrtod.h'
    '/usr/include/python2.7/pystrcmp.h'
    '/usr/include/python2.7/dtoa.h'
    '/usr/include/python2.7/pyfpe.h'

    我尝试在CROSSTOOL里面local部分加上了
    cxx_builtin_include_directory: "/usr/include/python2.7",不过没有解决问题。
    请问大神这个又是怎么回事呢?我一点也不懂,怎么解决这个missing dependency declarations 这个问题啊?

    ReplyDelete
  13. 大神,你好,有问题想咨询你,有其他的联系方式吗?谢谢了

    ReplyDelete
  14. Hi errong,

    I'm trying to build tensorflow for rpi3 steps i have followeed are,
    1-> builded bazel binary for ubuntu HOST.
    2-> followed steps for cross compiling bazel
    3-> i have understood above steps made my changes according to that but i'm facing issue while building.

    Ganesh:~/Projects/Tensorflow/src/bazel$ bazel build --crosstool_top=//compilers:toolchain --cpu=rpi3-aarch64 --config=opt --verbose_failures tensorflow/tensorflow/tools/pip_package:build_pip_package
    WARNING: Config value opt is not defined in any .rc file
    ERROR: Skipping 'tensorflow/tensorflow/tools/pip_package:build_pip_package': error loading package 'tensorflow/tensorflow/tools/pip_package': Extension file not found. Unable to load package for '@local_config_tensorrt//:build_defs.bzl': The repository could not be resolved
    WARNING: Target pattern parsing failed.
    ERROR: error loading package 'tensorflow/tensorflow/tools/pip_package': Extension file not found. Unable to load package for '@local_config_tensorrt//:build_defs.bzl': The repository could not be resolved
    INFO: Elapsed time: 0.096s
    FAILED: Build did NOT complete successfully (0 packages loaded)
    currently loading: tensorflow/tensorflow/tools/pip_package

    Do you have any idea what i'm doing wrong?

    ReplyDelete

Post a Comment

Popular posts from this blog