From 5442825dfd774a1d1f55d4d0e9d2821abb551caf Mon Sep 17 00:00:00 2001 From: Gunhan Gulsoy Date: Fri, 21 Jul 2017 14:41:54 -0700 Subject: Re-enable using local MKL directory. Setting TF_MKL_ROOT environment variable (together with "--config=mkl") before build will enforce using a local copy of MKL during build. PiperOrigin-RevId: 162787130 --- configure | 35 ++++++++------------------ tensorflow/workspace.bzl | 25 ++++++++++-------- third_party/mkl/build_defs.bzl | 57 +++++++++++++++++++++++++++++++++++++++++- third_party/mkl/mkl.BUILD | 10 +++++++- 4 files changed, 89 insertions(+), 38 deletions(-) diff --git a/configure b/configure index cd957a06f8..1eeaffaf74 100755 --- a/configure +++ b/configure @@ -202,31 +202,16 @@ fi setup_python ## Set up MKL related environment settings -while [ "$TF_NEED_MKL" == "" ]; do - fromuser="" - read -p "Do you wish to build TensorFlow with MKL support? [y/N] " INPUT - fromuser="1" - case $INPUT in - [Yy]* ) echo "MKL support will be enabled for TensorFlow"; TF_NEED_MKL=1;; - [Nn]* ) echo "No MKL support will be enabled for TensorFlow"; TF_NEED_MKL=0;; - "" ) echo "No MKL support will be enabled for TensorFlow"; TF_NEED_MKL=0;; - * ) echo "Invalid selection: " $INPUT;; - esac -done - -OSNAME=`uname -s` - -if [ "$TF_NEED_MKL" == "1" ]; then # TF_NEED_MKL - if [ "$OSNAME" == "Linux" ]; then - write_to_bazelrc 'build:mkl --define with_mkl_support=true' - write_to_bazelrc 'build:mkl -c opt' - write_to_bazelrc 'build:mkl --copt="-DEIGEN_USE_VML"' - echo "Add \"--config=mkl\" to your bazel command to build with MKL support." - elif [ "$OSNAME" == "Darwin" ]; then - echo "Darwin is unsupported yet"; - exit 1 - fi -fi # TF_NEED_MKL +write_to_bazelrc 'build:mkl --define with_mkl_support=true' +write_to_bazelrc 'build:mkl --define using_mkl=true' +write_to_bazelrc 'build:mkl -c opt' +write_to_bazelrc 'build:mkl --copt="-DEIGEN_USE_VML"' +echo "" +echo "Add \"--config=mkl\" to your bazel command to build with MKL support." +echo "Please note that MKL on MacOS or windows is still not supported." +echo "If you would like to use a local MKL instead of downloading, please " +echo " set the environment variable \"TF_MKL_ROOT\" every time before build." +echo "" ## End MKL setup ## Set up architecture-dependent optimization flags. diff --git a/tensorflow/workspace.bzl b/tensorflow/workspace.bzl index 25a2b7e84f..71e4b8f5bb 100644 --- a/tensorflow/workspace.bzl +++ b/tensorflow/workspace.bzl @@ -2,6 +2,7 @@ load("//third_party/gpus:cuda_configure.bzl", "cuda_configure") load("//third_party/sycl:sycl_configure.bzl", "sycl_configure") +load("//third_party/mkl:build_defs.bzl", "mkl_repository") load("@io_bazel_rules_closure//closure/private:java_import_external.bzl", "java_import_external") load("@io_bazel_rules_closure//closure:defs.bzl", "filegroup_external") load("//third_party/py:python_configure.bzl", "python_configure") @@ -140,6 +141,19 @@ def tf_workspace(path_prefix="", tf_repo_name=""): cuda_configure(name="local_config_cuda") sycl_configure(name="local_config_sycl") python_configure(name="local_config_python") + + mkl_repository( + name = "mkl", + urls = [ + "http://mirror.bazel.build/github.com/01org/mkl-dnn/releases/download/v0.7/mklml_lnx_2018.0.20170425.tgz", + "https://github.com/01org/mkl-dnn/releases/download/v0.7/mklml_lnx_2018.0.20170425.tgz", + ], + sha256 = "3cc2501fb209e1fd0960a5f61c919438f9619c68a644dcebf0fdf69b07460c57", + strip_prefix = "mklml_lnx_2018.0.20170425", + build_file = str(Label("//third_party/mkl:mkl.BUILD")), + repository = tf_repo_name, + ) + if path_prefix: print("path_prefix was specified to tf_workspace but is no longer used " + "and will be removed in the future.") @@ -171,17 +185,6 @@ def tf_workspace(path_prefix="", tf_repo_name=""): actual = "@libxsmm_archive//third_party:xsmm_avx", ) - native.new_http_archive( - name = "mkl", - urls = [ - "http://mirror.bazel.build/github.com/01org/mkl-dnn/releases/download/v0.7/mklml_lnx_2018.0.20170425.tgz", - "https://github.com/01org/mkl-dnn/releases/download/v0.7/mklml_lnx_2018.0.20170425.tgz", - ], - sha256 = "3cc2501fb209e1fd0960a5f61c919438f9619c68a644dcebf0fdf69b07460c57", - strip_prefix = "mklml_lnx_2018.0.20170425", - build_file = str(Label("//third_party/mkl:mkl.BUILD")), - ) - native.new_http_archive( name = "ortools_archive", urls = [ diff --git a/third_party/mkl/build_defs.bzl b/third_party/mkl/build_defs.bzl index 9a28b312c2..533c0766c7 100644 --- a/third_party/mkl/build_defs.bzl +++ b/third_party/mkl/build_defs.bzl @@ -1,4 +1,16 @@ -# Macros for building MKL code. +# -*- Python -*- +"""Skylark macros for MKL. +if_mkl is a conditional to check if MKL is enabled or not. + +mkl_repository is a repository rule for creating MKL repository rule that can +be pointed to either a local folder, or download it from the internet. +mkl_repository depends on the following environment variables: + * `TF_MKL_ROOT`: The root folder where a copy of libmkl is located. +""" + + +_TF_MKL_ROOT = "TF_MKL_ROOT" + def if_mkl(if_true, if_false = []): """Shorthand for select()'ing on whether we're building with MKL. @@ -11,3 +23,46 @@ def if_mkl(if_true, if_false = []): "//third_party/mkl:using_mkl": if_true, "//conditions:default": if_false }) + + +def _enable_local_mkl(repository_ctx): + return _TF_MKL_ROOT in repository_ctx.os.environ + + +def _mkl_autoconf_impl(repository_ctx): + """Implementation of the local_mkl_autoconf repository rule.""" + + if _enable_local_mkl(repository_ctx): + # Symlink lib and include local folders. + mkl_root = repository_ctx.os.environ[_TF_MKL_ROOT] + mkl_lib_path = "%s/lib" % mkl_root + repository_ctx.symlink(mkl_lib_path, "lib") + mkl_include_path = "%s/include" % mkl_root + repository_ctx.symlink(mkl_include_path, "include") + mkl_license_path = "%s/license.txt" % mkl_root + repository_ctx.symlink(mkl_license_path, "license.txt") + else: + # setup remote mkl repository. + repository_ctx.download_and_extract( + repository_ctx.attr.urls, + sha256=repository_ctx.attr.sha256, + stripPrefix=repository_ctx.attr.strip_prefix, + ) + + # Also setup BUILD file. + repository_ctx.symlink(repository_ctx.attr.build_file, "BUILD") + + +mkl_repository = repository_rule( + implementation = _mkl_autoconf_impl, + environ = [ + _TF_MKL_ROOT, + ], + attrs = { + "build_file": attr.label(), + "repository": attr.string(), + "urls": attr.string_list(default = []), + "sha256": attr.string(default = ""), + "strip_prefix": attr.string(default = ""), + }, +) diff --git a/third_party/mkl/mkl.BUILD b/third_party/mkl/mkl.BUILD index 37daa52419..8db97232e1 100644 --- a/third_party/mkl/mkl.BUILD +++ b/third_party/mkl/mkl.BUILD @@ -1,6 +1,14 @@ licenses(["notice"]) # 3-Clause BSD -exports_files(["LICENSE"]) +exports_files(["license.txt"]) + +filegroup( + name = "LICENSE", + srcs = [ + "license.txt", + ], + visibility = ["//visibility:public"], +) cc_library( name = "mkl_headers", -- cgit v1.2.3