From bab0d14036efd0adcd4e48303d045cee3c342cb0 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Fri, 4 Dec 2020 14:09:19 +0100 Subject: [PATCH 1/9] Make TensorFlow build on Apple Silicon --- .bazelrc | 9 +++++ tensorflow/BUILD | 34 +++++++++++++++++-- tensorflow/compiler/aot/tfcompile.bzl | 3 +- .../platform/default/build_config_root.bzl | 1 + tensorflow/lite/kernels/internal/BUILD | 8 +++++ tensorflow/tensorflow.bzl | 20 ++++++----- .../tools/pip_package/build_pip_package.sh | 4 +-- third_party/cpuinfo/BUILD.bazel | 18 ++++++++++ third_party/llvm/llvm.bzl | 10 +++++- third_party/nccl/archive.BUILD | 5 ++- 10 files changed, 95 insertions(+), 17 deletions(-) diff --git a/.bazelrc b/.bazelrc index 640cfa87320..f25447d45a4 100644 --- a/.bazelrc +++ b/.bazelrc @@ -17,6 +17,9 @@ # ios_x86_64: # ios_fat: # +# Macosx options +# darwin_arm64: +# # Compiler options: # cuda_clang: Use clang when building CUDA code. # c++17: Build with C++17 options (links with libc++) @@ -134,6 +137,11 @@ build:ios_x86_64 --cpu=ios_x86_64 build:ios_fat --config=ios build:ios_fat --ios_multi_cpus=armv7,arm64,i386,x86_64 +# Enables all the macos config options for macos_arm64 +build:macos_arm64 --config=macos +build:macos_arm64 --apple_platform_type=macos +build:macos_arm64 --cpu=darwin_arm64 + # Config to use a mostly-static build and disable modular op registration # support (this will revert to loading TensorFlow with RTLD_GLOBAL in Python). # By default, TensorFlow will build with a dependence on @@ -272,6 +280,7 @@ build:c++1z_gcc --config=c++17_gcc build --enable_platform_specific_config build:android --noenable_platform_specific_config build:ios --noenable_platform_specific_config +build:macos_arm64 --noenable_platform_specific_config # Suppress C++ compiler warnings, otherwise build logs become 10s of MBs. build:android --copt=-w diff --git a/tensorflow/BUILD b/tensorflow/BUILD index 52eb4e35c92..d34ac399bde 100644 --- a/tensorflow/BUILD +++ b/tensorflow/BUILD @@ -178,7 +178,7 @@ config_setting( ) config_setting( - name = "macos", + name = "macos_x86_64", values = { "apple_platform_type": "macos", "cpu": "darwin", @@ -186,6 +186,33 @@ config_setting( visibility = ["//visibility:public"], ) +config_setting( + name = "macos_arm64e", + values = { + "apple_platform_type": "macos", + "cpu": "darwin_arm64e", + }, + visibility = ["//visibility:public"], +) + +config_setting( + name = "macos_arm64", + values = { + "apple_platform_type": "macos", + "cpu": "darwin_arm64", + }, + visibility = ["//visibility:public"], +) + +selects.config_setting_group( + name = "macos", + match_any = [ + ":macos_x86_64", + ":macos_arm64", + ":macos_arm64e", + ], +) + config_setting( name = "ios", values = {"apple_platform_type": "ios"}, @@ -391,7 +418,6 @@ config_setting( }, values = { "apple_platform_type": "macos", - "cpu": "darwin", }, visibility = ["//visibility:public"], ) @@ -719,7 +745,9 @@ tf_cc_shared_object( name = "tensorflow_framework", framework_so = [], linkopts = select({ - "//tensorflow:macos": [], + "//tensorflow:macos": [ + "-Wl,-rename_section,__TEXT,text_env,__TEXT,__text" + ], "//tensorflow:windows": [], "//tensorflow:freebsd": [ "-Wl,--version-script,$(location //tensorflow:tf_framework_version_script.lds)", diff --git a/tensorflow/compiler/aot/tfcompile.bzl b/tensorflow/compiler/aot/tfcompile.bzl index 29f37bf7498..c94d95fa3e3 100644 --- a/tensorflow/compiler/aot/tfcompile.bzl +++ b/tensorflow/compiler/aot/tfcompile.bzl @@ -432,7 +432,8 @@ def target_llvm_triple(): "//tensorflow:ios": "arm64-none-ios", "//tensorflow:ios_x86_64": "x86_64-apple-ios", "//tensorflow:linux_ppc64le": "ppc64le-ibm-linux-gnu", - "//tensorflow:macos": "x86_64-none-darwin", + "//tensorflow:macos_x86_64": "x86_64-none-darwin", + "//tensorflow:macos_arm64": "aarch64-none-darwin", "//tensorflow:windows": "x86_64-none-windows", "//tensorflow:linux_s390x": "systemz-none-linux-gnu", "//conditions:default": "x86_64-pc-linux", diff --git a/tensorflow/core/platform/default/build_config_root.bzl b/tensorflow/core/platform/default/build_config_root.bzl index 38aeb9fb6ba..b0b32df4c9f 100644 --- a/tensorflow/core/platform/default/build_config_root.bzl +++ b/tensorflow/core/platform/default/build_config_root.bzl @@ -56,6 +56,7 @@ def if_static(extra_deps, otherwise = [], macos = []): } if macos: ret[str(Label("//tensorflow:macos_with_framework_shared_object"))] = macos + return select(ret) def if_static_and_not_mobile(extra_deps, otherwise = []): diff --git a/tensorflow/lite/kernels/internal/BUILD b/tensorflow/lite/kernels/internal/BUILD index e595457f8ca..05ed72dcf8f 100644 --- a/tensorflow/lite/kernels/internal/BUILD +++ b/tensorflow/lite/kernels/internal/BUILD @@ -186,6 +186,13 @@ config_setting( }, ) +config_setting( + name = "darwin_arm64", + values = { + "cpu": "darwin_arm64", + }, +) + config_setting( name = "freebsd", values = { @@ -806,6 +813,7 @@ cc_library( ":ios_armv7", ":ios_arm64", ":ios_arm64e", + ":darwin_arm64", ":raspberry_pi_with_neon", ): [":neon_tensor_utils"], ( diff --git a/tensorflow/tensorflow.bzl b/tensorflow/tensorflow.bzl index afe0c88504a..6e7d48d8115 100644 --- a/tensorflow/tensorflow.bzl +++ b/tensorflow/tensorflow.bzl @@ -446,6 +446,7 @@ def _rpath_linkopts(name): return select({ clean_dep("//tensorflow:macos"): [ "-Wl,%s" % (_make_search_paths("@loader_path", levels_to_root),), + "-Wl,-rename_section,__TEXT,text_env,__TEXT,__text" ], clean_dep("//tensorflow:windows"): [], "//conditions:default": [ @@ -1920,22 +1921,24 @@ def pywrap_tensorflow_macro( if not version_script: version_script = select({ - "@local_config_cuda//cuda:darwin": clean_dep("//tensorflow:tf_exported_symbols.lds"), + "//tensorflow:macos": clean_dep("//tensorflow:tf_exported_symbols.lds"), "//conditions:default": clean_dep("//tensorflow:tf_version_script.lds"), }) vscriptname = name + "_versionscript" _append_init_to_versionscript( name = vscriptname, is_version_script = select({ - "@local_config_cuda//cuda:darwin": False, + "//tensorflow:macos": False, "//conditions:default": True, }), module_name = module_name, template_file = version_script, ) extra_linkopts = select({ - "@local_config_cuda//cuda:darwin": [ - "-Wl,-exported_symbols_list,$(location %s.lds)" % vscriptname, + clean_dep("//tensorflow:macos"): [ + # TODO: the -w suppresses a wall of harmless warnings about hidden typeinfo symbols + # not being exported. There should be a better way to deal with this. + "-Wl,-w", "-Wl,-exported_symbols_list,$(location %s.lds)" % vscriptname, ], clean_dep("//tensorflow:windows"): [], "//conditions:default": [ @@ -1944,9 +1947,6 @@ def pywrap_tensorflow_macro( ], }) extra_deps += select({ - "@local_config_cuda//cuda:darwin": [ - "%s.lds" % vscriptname, - ], clean_dep("//tensorflow:windows"): [], "//conditions:default": [ "%s.lds" % vscriptname, @@ -2578,8 +2578,10 @@ def pybind_extension( ], }), linkopts = linkopts + _rpath_linkopts(name) + select({ - "@local_config_cuda//cuda:darwin": [ - "-Wl,-exported_symbols_list,$(location %s)" % exported_symbols_file, + clean_dep("//tensorflow:macos"): [ + # TODO: the -w suppresses a wall of harmless warnings about hidden typeinfo symbols + # not being exported. There should be a better way to deal with this. + "-Wl,-w", "-Wl,-exported_symbols_list,$(location %s)" % exported_symbols_file, ], clean_dep("//tensorflow:windows"): [], "//conditions:default": [ diff --git a/tensorflow/tools/pip_package/build_pip_package.sh b/tensorflow/tools/pip_package/build_pip_package.sh index 8b2772b5072..d1eb0d65ce8 100755 --- a/tensorflow/tools/pip_package/build_pip_package.sh +++ b/tensorflow/tools/pip_package/build_pip_package.sh @@ -216,10 +216,10 @@ function prepare_src() { if [ -d "${TMPDIR}/tensorflow/_api/v1/" ] then cp -r ${TMPDIR}/tensorflow/python/keras/api/_v1/keras/ ${TMPDIR}/tensorflow/keras/ - sed -i'.original' -e 's/.python.keras.api._v1/tensorflow/g' ${TMPDIR}/tensorflow/__init__.py + find . -type f -maxdepth 0 -exec sed -i'.original' -e 's/.python.keras.api._v1/tensorflow/g' ${TMPDIR}/tensorflow/__init__.py {} + else cp -r ${TMPDIR}/tensorflow/python/keras/api/_v2/keras/ ${TMPDIR}/tensorflow/keras/ - sed -i'.original' -e 's/.python.keras.api._v2/tensorflow/g' ${TMPDIR}/tensorflow/__init__.py + find . -type f -maxdepth 0 -exec sed -i'.original' -e 's/.python.keras.api._v2/tensorflow/g' ${TMPDIR}/tensorflow/__init__.py {} + fi } diff --git a/third_party/cpuinfo/BUILD.bazel b/third_party/cpuinfo/BUILD.bazel index 50846a6f107..2631c04bc80 100644 --- a/third_party/cpuinfo/BUILD.bazel +++ b/third_party/cpuinfo/BUILD.bazel @@ -105,6 +105,7 @@ cc_library( ":linux_mips64": COMMON_SRCS + LINUX_SRCS, ":linux_s390x": COMMON_SRCS + LINUX_SRCS, ":macos_x86_64": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS, + ":darwin_arm64": COMMON_SRCS + MACH_SRCS + MACH_ARM_SRCS, ":windows_x86_64": COMMON_SRCS + X86_SRCS + WINDOWS_X86_SRCS, ":android_armv7": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS + ANDROID_ARM_SRCS, ":android_arm64": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM64_SRCS + ANDROID_ARM_SRCS, @@ -293,6 +294,23 @@ config_setting( }, ) +config_setting( + name = "darwin_arm64", + values = { + "apple_platform_type": "macos", + "cpu": "darwin_arm64", + }, +) + +config_setting( + name = "macos_arm64", + values = { + "apple_platform_type": "macos", + "cpu": "darwin_arm64", + }, +) + + config_setting( name = "ios_x86", values = { diff --git a/third_party/llvm/llvm.bzl b/third_party/llvm/llvm.bzl index 5bd4ff2c4b9..e025fb2eb13 100644 --- a/third_party/llvm/llvm.bzl +++ b/third_party/llvm/llvm.bzl @@ -299,7 +299,7 @@ win32_cmake_vars = { # TODO(phawkins): use a better method to select the right host triple, rather # than hardcoding x86_64. llvm_all_cmake_vars = select({ - "@org_tensorflow//tensorflow:macos": cmake_var_string( + "@org_tensorflow//tensorflow:macos_x86_64": cmake_var_string( _dict_add( cmake_vars, llvm_target_cmake_vars("X86", "x86_64-apple-darwin"), @@ -307,6 +307,14 @@ llvm_all_cmake_vars = select({ darwin_cmake_vars, ), ), + "@org_tensorflow//tensorflow:macos_arm64": cmake_var_string( + _dict_add( + cmake_vars, + llvm_target_cmake_vars("AArch64", "arm64-apple-darwin"), + posix_cmake_vars, + darwin_cmake_vars, + ), + ), "@org_tensorflow//tensorflow:linux_ppc64le": cmake_var_string( _dict_add( cmake_vars, diff --git a/third_party/nccl/archive.BUILD b/third_party/nccl/archive.BUILD index 028b348caff..c629955d871 100644 --- a/third_party/nccl/archive.BUILD +++ b/third_party/nccl/archive.BUILD @@ -89,7 +89,10 @@ cc_library( ], hdrs = ["src/nccl.h"], include_prefix = "third_party/nccl", - linkopts = ["-lrt"], + linkopts = select({ + "//tensorflow:macos": [], + "//conditions:default": ["-lrt"], + }), strip_include_prefix = "src", visibility = ["//visibility:public"], deps = [ From 1cf4ff6f68320370377673e7a047edd827bd26c9 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Fri, 4 Dec 2020 15:11:48 +0100 Subject: [PATCH 2/9] Remove config_setting for darwin_arm64e for now --- tensorflow/BUILD | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tensorflow/BUILD b/tensorflow/BUILD index d34ac399bde..a41df90eb13 100644 --- a/tensorflow/BUILD +++ b/tensorflow/BUILD @@ -186,15 +186,6 @@ config_setting( visibility = ["//visibility:public"], ) -config_setting( - name = "macos_arm64e", - values = { - "apple_platform_type": "macos", - "cpu": "darwin_arm64e", - }, - visibility = ["//visibility:public"], -) - config_setting( name = "macos_arm64", values = { @@ -209,7 +200,6 @@ selects.config_setting_group( match_any = [ ":macos_x86_64", ":macos_arm64", - ":macos_arm64e", ], ) From 0f55ccb2424326a05d51e478475eb42be58516de Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Fri, 4 Dec 2020 15:17:29 +0100 Subject: [PATCH 3/9] Clean redundant config_setting --- third_party/cpuinfo/BUILD.bazel | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/third_party/cpuinfo/BUILD.bazel b/third_party/cpuinfo/BUILD.bazel index 2631c04bc80..2b3387c0463 100644 --- a/third_party/cpuinfo/BUILD.bazel +++ b/third_party/cpuinfo/BUILD.bazel @@ -105,7 +105,7 @@ cc_library( ":linux_mips64": COMMON_SRCS + LINUX_SRCS, ":linux_s390x": COMMON_SRCS + LINUX_SRCS, ":macos_x86_64": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS, - ":darwin_arm64": COMMON_SRCS + MACH_SRCS + MACH_ARM_SRCS, + ":macos_arm64": COMMON_SRCS + MACH_SRCS + MACH_ARM_SRCS, ":windows_x86_64": COMMON_SRCS + X86_SRCS + WINDOWS_X86_SRCS, ":android_armv7": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS + ANDROID_ARM_SRCS, ":android_arm64": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM64_SRCS + ANDROID_ARM_SRCS, @@ -294,14 +294,6 @@ config_setting( }, ) -config_setting( - name = "darwin_arm64", - values = { - "apple_platform_type": "macos", - "cpu": "darwin_arm64", - }, -) - config_setting( name = "macos_arm64", values = { From 67733d99c43c8fef81320f9c2690eb62b369451e Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Tue, 8 Dec 2020 17:13:16 +0100 Subject: [PATCH 4/9] local_execution_config_platform: return correct cpu value for arm64 platform --- third_party/remote_config/remote_platform_configure.bzl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/third_party/remote_config/remote_platform_configure.bzl b/third_party/remote_config/remote_platform_configure.bzl index 29520396905..0c03c7e98b1 100644 --- a/third_party/remote_config/remote_platform_configure.bzl +++ b/third_party/remote_config/remote_platform_configure.bzl @@ -20,6 +20,8 @@ def _remote_platform_configure_impl(repository_ctx): cpu = "s390x" elif machine_type.startswith("aarch64"): cpu = "aarch64" + elif machine_type.startswith("arm64"): + cpu = "aarch64" elif machine_type.startswith("arm"): cpu = "arm" elif machine_type.startswith("mips64"): From 204a1330ca969c87b614aef1fe8db98698410971 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Mon, 11 Jan 2021 14:17:14 +0100 Subject: [PATCH 5/9] Fix macos_with_framework_shared_object --- tensorflow/BUILD | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tensorflow/BUILD b/tensorflow/BUILD index a41df90eb13..7d2c52b35c1 100644 --- a/tensorflow/BUILD +++ b/tensorflow/BUILD @@ -402,16 +402,37 @@ config_setting( ) config_setting( - name = "macos_with_framework_shared_object", + name = "macos_x86_64_with_framework_shared_object", define_values = { "framework_shared_object": "true", }, values = { "apple_platform_type": "macos", + "cpu": "darwin", }, visibility = ["//visibility:public"], ) +config_setting( + name = "macos_arm64_with_framework_shared_object", + define_values = { + "framework_shared_object": "true", + }, + values = { + "apple_platform_type": "macos", + "cpu": "darwin_arm64", + }, + visibility = ["//visibility:public"], +) + +selects.config_setting_group( + name = "macos_with_framework_shared_object", + match_any = [ + ":macos_x86_64_with_framework_shared_object", + ":macos_arm64_with_framework_shared_object", + ], +) + config_setting( name = "using_cuda_clang", define_values = {"using_cuda_clang": "true"}, From bb06df120dda0ce588105eb28811866619bb7c36 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Mon, 11 Jan 2021 14:20:38 +0100 Subject: [PATCH 6/9] Remove blank line --- tensorflow/core/platform/default/build_config_root.bzl | 1 - 1 file changed, 1 deletion(-) diff --git a/tensorflow/core/platform/default/build_config_root.bzl b/tensorflow/core/platform/default/build_config_root.bzl index b0b32df4c9f..38aeb9fb6ba 100644 --- a/tensorflow/core/platform/default/build_config_root.bzl +++ b/tensorflow/core/platform/default/build_config_root.bzl @@ -56,7 +56,6 @@ def if_static(extra_deps, otherwise = [], macos = []): } if macos: ret[str(Label("//tensorflow:macos_with_framework_shared_object"))] = macos - return select(ret) def if_static_and_not_mobile(extra_deps, otherwise = []): From ca7222c8a057f855a77703165070b1984529aadc Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Mon, 11 Jan 2021 15:56:36 +0100 Subject: [PATCH 7/9] Fix sanity check --- tensorflow/BUILD | 2 +- third_party/cpuinfo/BUILD.bazel | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow/BUILD b/tensorflow/BUILD index 7d2c52b35c1..6e52a0cea6f 100644 --- a/tensorflow/BUILD +++ b/tensorflow/BUILD @@ -757,7 +757,7 @@ tf_cc_shared_object( framework_so = [], linkopts = select({ "//tensorflow:macos": [ - "-Wl,-rename_section,__TEXT,text_env,__TEXT,__text" + "-Wl,-rename_section,__TEXT,text_env,__TEXT,__text", ], "//tensorflow:windows": [], "//tensorflow:freebsd": [ diff --git a/third_party/cpuinfo/BUILD.bazel b/third_party/cpuinfo/BUILD.bazel index 2b3387c0463..cac7cc39b47 100644 --- a/third_party/cpuinfo/BUILD.bazel +++ b/third_party/cpuinfo/BUILD.bazel @@ -302,7 +302,6 @@ config_setting( }, ) - config_setting( name = "ios_x86", values = { From efc73dee1a1463de4688cab1588dd0526c774928 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Mon, 11 Jan 2021 16:44:16 +0100 Subject: [PATCH 8/9] Fix nccl BUILD file --- third_party/nccl/archive.BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/nccl/archive.BUILD b/third_party/nccl/archive.BUILD index c629955d871..eeb6f650662 100644 --- a/third_party/nccl/archive.BUILD +++ b/third_party/nccl/archive.BUILD @@ -90,7 +90,7 @@ cc_library( hdrs = ["src/nccl.h"], include_prefix = "third_party/nccl", linkopts = select({ - "//tensorflow:macos": [], + "@org_tensorflow//tensorflow:macos": [], "//conditions:default": ["-lrt"], }), strip_include_prefix = "src", From 32fd3c1ffa07bb01151c61bec0d19a11506f85e2 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Wed, 13 Jan 2021 15:47:28 +0100 Subject: [PATCH 9/9] Remove trailing space --- .bazelrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.bazelrc b/.bazelrc index f25447d45a4..aaacb1c3190 100644 --- a/.bazelrc +++ b/.bazelrc @@ -139,7 +139,7 @@ build:ios_fat --ios_multi_cpus=armv7,arm64,i386,x86_64 # Enables all the macos config options for macos_arm64 build:macos_arm64 --config=macos -build:macos_arm64 --apple_platform_type=macos +build:macos_arm64 --apple_platform_type=macos build:macos_arm64 --cpu=darwin_arm64 # Config to use a mostly-static build and disable modular op registration