diff --git a/WORKSPACE b/WORKSPACE index 06fb80e..6b15947 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -5,10 +5,6 @@ load("//third_party:version_check.bzl", "check_bazel_version_at_least") check_bazel_version_at_least("5.3.0") -load("//xla:workspace.bzl", "workspace") - -workspace() - # To update XLA to a new revision, # a) update URL and strip_prefix to the new git commit hash # b) get the sha256 hash of the commit by running: @@ -63,3 +59,7 @@ load( ) bazel_toolchains_repositories() + +load("//xla:workspace.bzl", "workspace") + +workspace() \ No newline at end of file diff --git a/configure.py b/configure.py index f6f6377..bdd27fe 100644 --- a/configure.py +++ b/configure.py @@ -36,7 +36,6 @@ _DEFAULT_SYCL_TOOLKIT_PATH = '/opt/intel/oneapi/compiler/latest/linux' -_DEFAULT_MKL_PATH='/opt/intel/oneapi/mkl/latest' _DEFAULT_AOT_CONFIG = '' _DEFAULT_GCC_TOOLCHAIN_PATH = '' _DEFAULT_GCC_TOOLCHAIN_TARGET = '' @@ -676,7 +675,6 @@ def toolkit_exists(toolkit_path): write_action_env_to_bazelrc('SYCL_TOOLKIT_PATH', sycl_toolkit_path) - write_action_env_to_bazelrc('SYCL_PATH', sycl_toolkit_path.split("compiler")[0]) lib_path = '%s/lib:%s/compiler/lib/intel64_lin' %( sycl_toolkit_path, sycl_toolkit_path, @@ -691,37 +689,11 @@ def toolkit_exists(toolkit_path): if library_path is not None and len(library_path) > 0: lib_path += ':' + library_path - mkl_path = os.getenv('ONEAPI_MKL_PATH') - if mkl_path is not None and len(mkl_path) > 0: - mkl_lib = '%s/lib/intel64' % (mkl_path) - lib_path += ':' + mkl_lib write_action_env_to_bazelrc('LD_LIBRARY_PATH', ld_lib_path) write_action_env_to_bazelrc('LIBRARY_PATH', lib_path) -def set_mkl_path(environ_cp): - """Set MKL Path.""" - def valid_mkl_path(mkl_home): - exists = ( - os.path.exists(os.path.join(mkl_home, 'include')) and - (os.path.exists(os.path.join(mkl_home, 'lib')))) - if not exists: - print( - 'Invalid path to the MKL Toolkit. %s or %s cannot be found' - % (os.path.join(mkl_home, 'include'), - os.path.exists(os.path.join(mkl_home, 'lib')))) - return exists - mkl_path = prompt_loop_or_load_from_env( - environ_cp, - var_name='ONEAPI_MKL_PATH', - var_default=_DEFAULT_MKL_PATH, - ask_for_var='Please specify the MKL toolkit folder.', - check_success=valid_mkl_path, - error_msg='Invalid path to the MKL Toolkit.', - suppress_default_error=True) - write_action_env_to_bazelrc('ONEAPI_MKL_PATH', mkl_path) - def system_specific_test_config(env): """Add default build and test flags required for TF tests to bazelrc.""" @@ -816,9 +788,6 @@ def main(): set_action_env_var(environ_cp, 'TF_NEED_SYCL', 'GPU', True) if environ_cp.get('TF_NEED_SYCL') == '1': set_sycl_toolkit_path(environ_cp) - set_action_env_var(environ_cp, 'TF_NEED_MKL', 'MKL', False) - if environ_cp.get('TF_NEED_MKL') == '1': - set_mkl_path(environ_cp) else: print('CPU is not supported.') sys.exit(1) diff --git a/third_party/gpus/compress_find_sycl_config.py b/third_party/gpus/compress_find_sycl_config.py new file mode 100644 index 0000000..7fb456f --- /dev/null +++ b/third_party/gpus/compress_find_sycl_config.py @@ -0,0 +1,36 @@ +# Copyright 2020 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Compresses the contents of 'find_sycl_config.py'. + +The compressed file is what is actually being used. It works around remote +config not being able to upload files yet. +""" +import base64 +import zlib + + +def main(): + with open('find_sycl_config.py', 'rb') as f: + data = f.read() + + compressed = zlib.compress(data) + b64encoded = base64.b64encode(compressed) + + with open('find_sycl_config.py.gz.base64', 'wb') as f: + f.write(b64encoded) + + +if __name__ == '__main__': + main() diff --git a/third_party/gpus/crosstool/BUILD.sycl.tpl b/third_party/gpus/crosstool/BUILD.sycl.tpl index a8af74e..5609edb 100755 --- a/third_party/gpus/crosstool/BUILD.sycl.tpl +++ b/third_party/gpus/crosstool/BUILD.sycl.tpl @@ -52,7 +52,7 @@ cc_toolchain_config( host_compiler_path = "%{host_compiler_path}", sycl_compiler_root = "%{sycl_compiler_root}", host_compiler_prefix = "%{host_compiler_prefix}", - host_unfiltered_compile_flags = ["%{unfiltered_compile_flags}"], + unfiltered_compile_flags = ["%{unfiltered_compile_flags}"], linker_bin_path = "%{linker_bin_path}", ) diff --git a/third_party/gpus/crosstool/bin/crosstool_wrapper_driver.tpl b/third_party/gpus/crosstool/bin/crosstool_wrapper_driver.tpl index 4fe1d18..9072f0b 100644 --- a/third_party/gpus/crosstool/bin/crosstool_wrapper_driver.tpl +++ b/third_party/gpus/crosstool/bin/crosstool_wrapper_driver.tpl @@ -43,6 +43,8 @@ if not os.path.exists(SYCL_PATH): HOST_COMPILER_PATH = "%{HOST_COMPILER_PATH}" SYCL_COMPILER_VERSION = "%{SYCL_COMPILER_VERSION}" +basekit_path = "%{basekit_path}" +basekit_version = "%{basekit_version}" def system(cmd): """Invokes cmd with os.system()""" @@ -56,7 +58,7 @@ def system(cmd): def call_compiler(argv, link = False, sycl = True, xetla = False): flags = argv -# common flags + # common flags common_flags = ['-fPIC'] sycl_device_only_flags = ['-fsycl'] sycl_device_only_flags.append('-fno-sycl-unnamed-lambda') @@ -79,7 +81,7 @@ def call_compiler(argv, link = False, sycl = True, xetla = False): else: compile_flags.append("-std=c++17") -# link flags + # link flags link_flags = ['-fPIC'] link_flags.append('-lsycl') link_flags.append("-fsycl") @@ -95,15 +97,15 @@ def call_compiler(argv, link = False, sycl = True, xetla = False): link_flags.append("-lze_loader") link_flags.append("-lOpenCL") -# oneMKL config - if '%{ONEAPI_MKL_PATH}': - common_flags.append('-DMKL_ILP64') - common_flags.append('-isystem %{ONEAPI_MKL_PATH}/include') - link_flags.append("-L%{ONEAPI_MKL_PATH}/lib/intel64") - link_flags.append("-lmkl_sycl") - link_flags.append("-lmkl_intel_ilp64") - link_flags.append("-lmkl_sequential") - link_flags.append("-lmkl_core") + # oneMKL config + # mkl_path = basekit_path + "/mkl/" + basekit_version + # common_flags.append('-DMKL_ILP64') + # common_flags.append('-isystem {}/include'.format(mkl_path)) + # link_flags.append("-L{}/lib/intel64".format(mkl_path)) + # link_flags.append("-lmkl_sycl") + # link_flags.append("-lmkl_intel_ilp64") + # link_flags.append("-lmkl_sequential") + # link_flags.append("-lmkl_core") flags += common_flags if link: diff --git a/third_party/gpus/crosstool/sycl_cc_toolchain_config.bzl.tpl b/third_party/gpus/crosstool/sycl_cc_toolchain_config.bzl.tpl index 9a4accb..df43484 100644 --- a/third_party/gpus/crosstool/sycl_cc_toolchain_config.bzl.tpl +++ b/third_party/gpus/crosstool/sycl_cc_toolchain_config.bzl.tpl @@ -176,11 +176,11 @@ def _impl(ctx): ], flag_groups = [ flag_group( - flags = ctx.attr.host_unfiltered_compile_flags, + flags = ctx.attr.unfiltered_compile_flags, ), ], ), - ] if ctx.attr.host_unfiltered_compile_flags else []), + ] if ctx.attr.unfiltered_compile_flags else []), ) determinism_feature = feature( @@ -601,7 +601,7 @@ def _impl(ctx): # ref: https://github.com/intel/llvm/blob/sycl/clang/docs/UsersManual.rst#controlling-floating-point-behavior "-fno-finite-math-only", "-fno-approx-func", - "-DITEX_USE_MKL=%{TF_NEED_MKL}", + "-DITEX_USE_MKL=1", "-DITEX_ENABLE_DOUBLE=1", "-DDNNL_USE_DPCPP_USM=1", "-DDNNL_WITH_LEVEL_ZERO=1", @@ -724,7 +724,7 @@ cc_toolchain_config = rule( "host_compiler_path": attr.string(), "host_compiler_prefix": attr.string(), "host_compiler_warnings": attr.string_list(), - "host_unfiltered_compile_flags": attr.string_list(), + "unfiltered_compile_flags": attr.string_list(), "linker_bin_path": attr.string(), }, provides = [CcToolchainConfigInfo], diff --git a/third_party/gpus/find_sycl_config.py b/third_party/gpus/find_sycl_config.py new file mode 100644 index 0000000..677468a --- /dev/null +++ b/third_party/gpus/find_sycl_config.py @@ -0,0 +1,132 @@ +# Copyright 2020 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Prints SYCL library and header directories and versions found on the system. + +The script searches for SYCL library and header files on the system, inspects +them to determine their version and prints the configuration to stdout. +The path to inspect is specified through an environment variable (SYCL_PATH). +If no valid configuration is found, the script prints to stderr and +returns an error code. + +The script takes the directory specified by the SYCL_PATH environment variable. +The script looks for headers and library files in a hard-coded set of +subdirectories from base path of the specified directory. If SYCL_PATH is not +specified, then "/opt/sycl" is used as it default value + +""" + +import io +import os +import re +import sys + + +class ConfigError(Exception): + pass + +def _get_default_sycl_toolkit_path(): + return "/opt/intel/oneapi/compiler/latest" + +def _get_toolkit_path(): + """Determines and returns the SYCL installation path.""" + sycl_toolkit_path = None + sycl_toolkit_path = _get_default_sycl_toolkit_path() + if "SYCL_TOOLKIT_PATH" in os.environ: + sycl_toolkit_path = os.environ["SYCL_TOOLKIT_PATH"] + return os.path.realpath(sycl_toolkit_path) + +def _get_basekit_path(): + return _get_toolkit_path().split("/compiler/")[0] + +def _get_basekit_version(): + return _get_toolkit_path().split("/compiler/")[1].split("/")[0] + +def _get_composite_version_number(major, minor, patch): + return 10000 * major + 100 * minor + patch + + +def _get_header_version(path, name): + """Returns preprocessor defines in C header file.""" + for line in io.open(path, "r", encoding="utf-8"): + match = re.match(r"#define %s +(\d+)" % name, line) + if match: + value = match.group(1) + return int(value) + + raise ConfigError('#define "{}" is either\n'.format(name) + + " not present in file {} OR\n".format(path) + + " its value is not an integer literal") + + +def _find_sycl_config(basekit_path): + + def sycl_version_numbers(path): + possible_version_files = [ + "compiler/latest/linux/include/sycl/version.hpp", + "compiler/latest/include/sycl/version.hpp", + ] + version_file = None + for f in possible_version_files: + version_file_path = os.path.join(path, f) + if os.path.exists(version_file_path): + version_file = version_file_path + break + if not version_file: + raise ConfigError( + "SYCL version file not found in {}".format(possible_version_files)) + + major = _get_header_version(version_file, "__LIBSYCL_MAJOR_VERSION") + minor = _get_header_version(version_file, "__LIBSYCL_MINOR_VERSION") + patch = _get_header_version(version_file, "__LIBSYCL_PATCH_VERSION") + return major, minor, patch + + major, minor, patch = sycl_version_numbers(basekit_path) + + sycl_config = { + "sycl_version_number": _get_composite_version_number(major, minor, patch), + "sycl_basekit_version_number": _get_basekit_version(), + } + + return sycl_config + + +def find_sycl_config(): + """Returns a dictionary of SYCL components config info.""" + basekit_path = _get_basekit_path() + toolkit_path = _get_toolkit_path() + if not os.path.exists(basekit_path): + raise ConfigError( + 'Specified SYCL_TOOLKIT_PATH "{}" does not exist'.format(basekit_path)) + + result = {} + + result["sycl_basekit_path"] = basekit_path + result["sycl_toolkit_path"] = toolkit_path + result.update(_find_sycl_config(basekit_path)) + + return result + + +def main(): + try: + for key, value in sorted(find_sycl_config().items()): + print("%s: %s" % (key, value)) + except ConfigError as e: + sys.stderr.write("\nERROR: {}\n\n".format(str(e))) + sys.exit(1) + + +if __name__ == "__main__": + main() diff --git a/third_party/gpus/find_sycl_config.py.gz.base64 b/third_party/gpus/find_sycl_config.py.gz.base64 new file mode 100644 index 0000000..5adfcd1 --- /dev/null +++ b/third_party/gpus/find_sycl_config.py.gz.base64 @@ -0,0 +1 @@ +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 \ No newline at end of file diff --git a/third_party/gpus/sycl/BUILD.tpl b/third_party/gpus/sycl/BUILD.tpl index ee9e7b5..8259008 100644 --- a/third_party/gpus/sycl/BUILD.tpl +++ b/third_party/gpus/sycl/BUILD.tpl @@ -6,3 +6,39 @@ config_setting( "define": "using_sycl=true", }, ) + +cc_library( + name = "sycl_headers", + hdrs = [ + %{sycl_headers} + ], + includes = [ + ".", + "sycl/include", + ], + visibility = ["//visibility:public"], +) + +cc_library( + name = "mkl", + srcs = [ + "sycl/lib/%{mkl_intel_ilp64_lib}", + "sycl/lib/%{mkl_sequential_lib}", + "sycl/lib/%{mkl_core_lib}", + %{mkl_sycl_libs} + ], + data = [ + "sycl/lib/%{mkl_intel_ilp64_lib}", + "sycl/lib/%{mkl_sequential_lib}", + "sycl/lib/%{mkl_core_lib}", + %{mkl_sycl_libs} + ], + includes = [ + ".", + "sycl/include", + ], + linkstatic = 1, + visibility = ["//visibility:public"], +) + +%{copy_rules} \ No newline at end of file diff --git a/third_party/gpus/sycl/build_defs.bzl.tpl b/third_party/gpus/sycl/build_defs.bzl.tpl index 75461b8..4a79671 100644 --- a/third_party/gpus/sycl/build_defs.bzl.tpl +++ b/third_party/gpus/sycl/build_defs.bzl.tpl @@ -11,10 +11,6 @@ def if_sycl(if_true, if_false = []): "//conditions:default": if_false, }) -def sycl_is_configured(): - """Returns true if SYCL was enabled during the configure process.""" - return %{sycl_is_configured} - def sycl_build_is_configured(): """Returns true if SYCL compiler was enabled during the configure process.""" return %{sycl_build_is_configured} @@ -25,7 +21,7 @@ def if_sycl_is_configured(x): Unlike if_sycl(), this does not require that we are building with --config=sycl. Used to allow non-SYCL code to depend on SYCL libraries. """ - if sycl_is_configured(): + if %{sycl_is_configured}: return select({"//conditions:default": x}) return select({"//conditions:default": []}) diff --git a/third_party/gpus/sycl_configure.bzl b/third_party/gpus/sycl_configure.bzl index 155974e..04eb332 100644 --- a/third_party/gpus/sycl_configure.bzl +++ b/third_party/gpus/sycl_configure.bzl @@ -7,6 +7,27 @@ * PYTHON_LIB_PATH: The path to the python lib """ +load( + "@xla//third_party/tsl/third_party/gpus:cuda_configure.bzl", + "make_copy_dir_rule", + "make_copy_files_rule", + "to_list_of_strings", +) +load( + "@xla//third_party/tsl/third_party/remote_config:common.bzl", + "config_repo_label", + "err_out", + "execute", + "files_exist", + "get_bash_bin", + "get_cpu_value", + "get_host_environ", + "get_python_bin", + "raw_exec", + "realpath", + "which", +) + _HOST_CXX_COMPILER = "HOST_CXX_COMPILER" _HOST_C_COMPILER = "HOST_C_COMPILER" @@ -15,33 +36,51 @@ _SYCL_TOOLKIT_PATH = "SYCL_TOOLKIT_PATH" _SYCL_COMPILER_VERSION = "SYCL_COMPILER_VERSION" -_ONEAPI_MKL_PATH = "ONEAPI_MKL_PATH" - -_TF_NEED_MKL = "TF_NEED_MKL" - -_AOT_CONFIG = "AOT_CONFIG" - _PYTHON_LIB_PATH = "PYTHON_LIB_PATH" _PYTHON_LIB_DIR = "PYTHON_LIB_DIR" _PYTHON_BIN_PATH = "PYTHON_BIN_PATH" +def _mkl_path(sycl_config): + return sycl_config.sycl_basekit_path + "/mkl/" + sycl_config.sycl_basekit_version_number + +def _sycl_header_path(repository_ctx, sycl_config, bash_bin): + sycl_header_path = sycl_config.sycl_basekit_path + "/compiler/" + sycl_config.sycl_basekit_version_number + include_dir = sycl_header_path + "/include" + if not files_exist(repository_ctx, [include_dir], bash_bin)[0]: + sycl_header_path = sycl_header_path + "/linux" + include_dir = sycl_header_path + "/include" + if not files_exist(repository_ctx, [include_dir], bash_bin)[0]: + auto_configure_fail("Cannot find sycl headers in {}".format(include_dir)) + return sycl_header_path + +def _sycl_include_path(repository_ctx, sycl_config, bash_bin): + """Generates the cxx_builtin_include_directory entries for sycl inc dirs. + + Args: + repository_ctx: The repository context. + sycl_config: The path to the gcc host compiler. + + Returns: + A string containing the Starlark string for each of the gcc + host compiler include directories, which can be added to the CROSSTOOL + file. + """ + inc_dirs = [] + + inc_dirs.append(_mkl_path(sycl_config) + "/include") + inc_dirs.append(_sycl_header_path(repository_ctx, sycl_config, bash_bin) + "/include") + inc_dirs.append(_sycl_header_path(repository_ctx, sycl_config, bash_bin) + "/lib/clang/17/include") + + return inc_dirs + def _enable_sycl(repository_ctx): if "TF_NEED_SYCL" in repository_ctx.os.environ: enable_sycl = repository_ctx.os.environ["TF_NEED_SYCL"].strip() return enable_sycl == "1" return False -def _enable_mkl(repository_ctx): - if _TF_NEED_MKL in repository_ctx.os.environ: - enable_mkl = repository_ctx.os.environ[_TF_NEED_MKL].strip() - return enable_mkl == "1" - return False - -def _enable_sycl_build(repository_ctx): - return _SYCL_TOOLKIT_PATH in repository_ctx.os.environ - def auto_configure_fail(msg): """Output failure message when auto configuration fails.""" red = "\033[0;31m" @@ -72,18 +111,15 @@ def find_cc(repository_ctx): fail("Cannot find C++ compiler, please correct your path.") return cc -def find_sycl_root(repository_ctx): - """Find DPC++ compiler.""" - sycl_name = "" - if _SYCL_TOOLKIT_PATH in repository_ctx.os.environ: - sycl_name = str(repository_ctx.path(repository_ctx.os.environ[_SYCL_TOOLKIT_PATH].strip()).realpath) +def find_sycl_root(repository_ctx, sycl_config): + sycl_name = str(repository_ctx.path(sycl_config.sycl_toolkit_path.strip()).realpath) if sycl_name.startswith("/"): return sycl_name fail("Cannot find DPC++ compiler, please correct your path") -def find_sycl_include_path(repository_ctx): +def find_sycl_include_path(repository_ctx, sycl_config): """Find DPC++ compiler.""" - base_path = find_sycl_root(repository_ctx) + base_path = find_sycl_root(repository_ctx, sycl_config) bin_path = repository_ctx.path(base_path + "/" + "bin" + "/" + "icpx") icpx_extra = "" if not bin_path.exists: @@ -104,12 +140,12 @@ def find_sycl_include_path(repository_ctx): include_dirs.append(str(repository_ctx.path(l.strip()).realpath)) return include_dirs -def get_sycl_version(repository_ctx): +def get_sycl_version(repository_ctx, sycl_config): """Get DPC++ compiler version yyyymmdd""" default_version = "00000000" macro = "__INTEL_LLVM_COMPILER" version_file = "include/sycl/CL/sycl/version.hpp" - base_path = find_sycl_root(repository_ctx) + base_path = find_sycl_root(repository_ctx, sycl_config) intel_llvm_macro = "00000000" compiler_bin_path = base_path + "/bin/icpx" compiler_macros = repository_ctx.execute([compiler_bin_path, "-dM", "-E", "-xc++", "/dev/null"]) @@ -133,60 +169,147 @@ def get_sycl_version(repository_ctx): default_version = l_list[-1] return default_version -def find_mkl_path(repository_ctx): - """Find MKL Path.""" - mkl_path = "" - if _ONEAPI_MKL_PATH in repository_ctx.os.environ: - mkl_path = repository_ctx.os.environ[_ONEAPI_MKL_PATH].strip() - if mkl_path.startswith("/"): - return mkl_path - fail("Cannot find OneAPI MKL, please correct your path") - -def find_aot_config(repository_ctx): - """Find AOT config.""" - device_tmp = " -Xs \'-device {}\'" - if _AOT_CONFIG in repository_ctx.os.environ: - devices = repository_ctx.os.environ[_AOT_CONFIG].strip() - device_list = [] - if devices: - device_list = devices.split(",") - else: - return "" - if device_list: - # check for security purpose only here - for d in device_list: - if len(d) > 20: - fail("Invalid AOT target: {}".format(d)) - device_tmp = device_tmp.format(devices) - return device_tmp - def find_python_lib(repository_ctx): """Returns python path.""" if _PYTHON_LIB_PATH in repository_ctx.os.environ: return repository_ctx.os.environ[_PYTHON_LIB_PATH].strip() fail("Environment variable PYTHON_LIB_PATH was not specified re-run ./configure") -def _check_lib(repository_ctx, toolkit_path, lib): - """Checks if lib exists under sycl_toolkit_path or fail if it doesn't. +def _lib_name(lib, version = "", static = False): + """Constructs the name of a library on Linux. + + Args: + lib: The name of the library, such as "hip" + version: The version of the library. + static: True the library is static or False if it is a shared object. + + Returns: + The platform-specific name of the library. + """ + if static: + return "lib%s.a" % lib + else: + if version: + version = ".%s" % version + return "lib%s.so%s" % (lib, version) + +def _sycl_lib_paths(repository_ctx, lib, basedir): + file_name = _lib_name(lib, version = "", static = False) + return [ + repository_ctx.path("%s/lib/%s" % (basedir, file_name)), + repository_ctx.path("%s/lib/intel64/%s" % (basedir, file_name)), + ] + +def _batch_files_exist(repository_ctx, libs_paths, bash_bin): + all_paths = [] + for _, lib_paths in libs_paths: + for lib_path in lib_paths: + all_paths.append(lib_path) + return files_exist(repository_ctx, all_paths, bash_bin) + +def _select_sycl_lib_paths(repository_ctx, libs_paths, bash_bin): + test_results = _batch_files_exist(repository_ctx, libs_paths, bash_bin) + + libs = {} + i = 0 + for name, lib_paths in libs_paths: + selected_path = None + for path in lib_paths: + if test_results[i] and selected_path == None: + # For each lib select the first path that exists. + selected_path = path + i = i + 1 + if selected_path == None: + auto_configure_fail("Cannot find sycl library %s" % name) + + libs[name] = struct(file_name = selected_path.basename, path = realpath(repository_ctx, selected_path, bash_bin)) + + return libs + +def _find_libs(repository_ctx, sycl_config, bash_bin): + """Returns the SYCL libraries on the system. Args: repository_ctx: The repository context. - toolkit_path: The toolkit directory containing the libraries. - ib: The library to look for under toolkit_path. + sycl_config: The SYCL config as returned by _get_sycl_config + bash_bin: the path to the bash interpreter + + Returns: + Map of library names to structs of filename and path """ - lib_path = toolkit_path + "/" + lib - if not repository_ctx.path(lib_path).exists: - auto_configure_fail("Cannot find %s" % lib_path) + mkl_path = _mkl_path(sycl_config) + libs_paths = [ + (name, _sycl_lib_paths(repository_ctx, name, path)) + for name, path in [ + # ("sycl", sycl_config.sycl_basekit_path + "/compiler/latest/linux/"), + ("mkl_intel_ilp64", mkl_path), + ("mkl_sequential", mkl_path), + ("mkl_core", mkl_path), + ] + ] + if sycl_config.sycl_basekit_version_number < "2024": + libs_paths.append(("mkl_sycl", _sycl_lib_paths(repository_ctx, "mkl_sycl", mkl_path))) + else: + libs_paths.append(("mkl_sycl_blas", _sycl_lib_paths(repository_ctx, "mkl_sycl_blas", mkl_path))) + libs_paths.append(("mkl_sycl_lapack", _sycl_lib_paths(repository_ctx, "mkl_sycl_lapack", mkl_path))) + libs_paths.append(("mkl_sycl_sparse", _sycl_lib_paths(repository_ctx, "mkl_sycl_sparse", mkl_path))) + libs_paths.append(("mkl_sycl_dft", _sycl_lib_paths(repository_ctx, "mkl_sycl_dft", mkl_path))) + libs_paths.append(("mkl_sycl_vm", _sycl_lib_paths(repository_ctx, "mkl_sycl_vm", mkl_path))) + libs_paths.append(("mkl_sycl_rng", _sycl_lib_paths(repository_ctx, "mkl_sycl_rng", mkl_path))) + libs_paths.append(("mkl_sycl_stats", _sycl_lib_paths(repository_ctx, "mkl_sycl_stats", mkl_path))) + libs_paths.append(("mkl_sycl_data_fitting", _sycl_lib_paths(repository_ctx, "mkl_sycl_data_fitting", mkl_path))) + return _select_sycl_lib_paths(repository_ctx, libs_paths, bash_bin) + +def _exec_find_sycl_config(repository_ctx, script_path): + python_bin = get_python_bin(repository_ctx) + + # If used with remote execution then repository_ctx.execute() can't + # access files from the source tree. A trick is to read the contents + # of the file in Starlark and embed them as part of the command. In + # this case the trick is not sufficient as the find_cuda_config.py + # script has more than 8192 characters. 8192 is the command length + # limit of cmd.exe on Windows. Thus we additionally need to compress + # the contents locally and decompress them as part of the execute(). + compressed_contents = repository_ctx.read(script_path) + decompress_and_execute_cmd = ( + "from zlib import decompress;" + + "from base64 import b64decode;" + + "from os import system;" + + "script = decompress(b64decode('%s'));" % compressed_contents.rstrip('\n') + + "f = open('script.py', 'wb');" + + "f.write(script);" + + "f.close();" + + "system('\"%s\" script.py');" % (python_bin) + ) + return execute(repository_ctx, [python_bin, "-c", decompress_and_execute_cmd]) + +def find_sycl_config(repository_ctx, script_path): + """Returns SYCL config dictionary from running find_sycl_config.py""" + exec_result = _exec_find_sycl_config(repository_ctx, script_path) + if exec_result.return_code: + auto_configure_fail("Failed to run find_sycl_config.py: %s" % err_out(exec_result)) -def _check_dir(repository_ctx, directory): - """Checks whether the directory exists and fail if it does not. + # Parse the dict from stdout. + return dict([tuple(x.split(": ")) for x in exec_result.stdout.splitlines()]) + +def _get_sycl_config(repository_ctx, bash_bin, find_sycl_config_script): + """Detects and returns information about the SYCL installation on the system. Args: repository_ctx: The repository context. - directory: The directory to check the existence of. + bash_bin: the path to the path interpreter """ - if not repository_ctx.path(directory).exists: - auto_configure_fail("Cannot find dir: %s" % directory) + config = find_sycl_config(repository_ctx, find_sycl_config_script) + sycl_basekit_path = config["sycl_basekit_path"] + sycl_toolkit_path = config["sycl_toolkit_path"] + sycl_version_number = config["sycl_version_number"] + sycl_basekit_version_number = config["sycl_basekit_version_number"] + return struct( + sycl_basekit_path = sycl_basekit_path, + sycl_toolkit_path = sycl_toolkit_path, + sycl_version_number = sycl_version_number, + sycl_basekit_version_number = sycl_basekit_version_number, + ) def _tpl_path(repository_ctx, labelname): return repository_ctx.path(Label("//third_party/gpus/%s.tpl" % labelname)) @@ -309,137 +432,186 @@ def _create_dummy_repository(repository_ctx): { "%{sycl_is_configured}": "False", "%{sycl_build_is_configured}": "False", - "%{mkl_is_configured}": "False", }, ) -def _sycl_autoconf_imp(repository_ctx): - """Implementation of the sycl_autoconf rule.""" +def _create_local_sycl_repository(repository_ctx): builtin_include_dirs = "" unfiltered_cxx_flags = "" linker_flags = "" sycl_defines = {} + tpl_paths = {labelname: _tpl_path(repository_ctx, labelname) for labelname in [ + "sycl:build_defs.bzl", + "sycl:BUILD", + "crosstool:BUILD.sycl", + "crosstool:sycl_cc_toolchain_config.bzl", + "crosstool/bin:crosstool_wrapper_driver", + ]} + + find_sycl_config_script = repository_ctx.path(Label("//third_party/gpus:find_sycl_config.py.gz.base64")) + + bash_bin = get_bash_bin(repository_ctx) + sycl_config = _get_sycl_config(repository_ctx, bash_bin, find_sycl_config_script) + + # Copy header and library files to execroot. + copy_rules = [ + # make_copy_dir_rule( + # repository_ctx, + # name = "sycl-include", + # src_dir = _sycl_header_path(repository_ctx, sycl_config, bash_bin) + "/include", + # out_dir = "sycl/include", + # exceptions = ["gtest", "gmock"], + # ), + ] + copy_rules.append(make_copy_dir_rule( + repository_ctx, + name = "mkl-include", + src_dir = _mkl_path(sycl_config) + "/include", + out_dir = "sycl/include", + exceptions = ["gtest", "gmock"], + )) + + sycl_libs = _find_libs(repository_ctx, sycl_config, bash_bin) + sycl_lib_srcs = [] + sycl_lib_outs = [] + for lib in sycl_libs.values(): + sycl_lib_srcs.append(lib.path) + sycl_lib_outs.append("sycl/lib/" + lib.file_name) + copy_rules.append(make_copy_files_rule( + repository_ctx, + name = "sycl-lib", + srcs = sycl_lib_srcs, + outs = sycl_lib_outs, + )) - if not _enable_sycl(repository_ctx): - _create_dummy_repository(repository_ctx) + # Set up BUILD file for sycl/ + repository_ctx.template( + "sycl/build_defs.bzl", + tpl_paths["sycl:build_defs.bzl"], + { + "%{sycl_is_configured}": "True", + "%{sycl_build_is_configured}": "True", + }, + ) + + if sycl_config.sycl_basekit_version_number < "2024": + mkl_sycl_libs = '"{}"'.format( + "sycl/lib/" + sycl_libs["mkl_sycl"].file_name) else: - tpl_paths = {labelname: _tpl_path(repository_ctx, labelname) for labelname in [ - # "rocm:build_defs.bzl", - # "rocm:BUILD", - "crosstool:BUILD.sycl", - "crosstool:sycl_cc_toolchain_config.bzl", - "crosstool/bin:crosstool_wrapper_driver", - # "rocm:rocm_config.h", - ]} - - # copy template files - _tpl(repository_ctx, "sycl:build_defs.bzl") - _tpl(repository_ctx, "sycl:BUILD") - - additional_cxxflags = [] - additional_linker_flags = [] - builtin_includes = [] - - builtin_includes += [find_sycl_root(repository_ctx) + "/include"] - builtin_includes += [find_sycl_root(repository_ctx) + "/lib/clang/12.0.0/include"] - builtin_includes += [find_sycl_root(repository_ctx) + "/lib/clang/13.0.0/include"] - - pwd = repository_ctx.os.environ["PWD"] - additional_inc = [] - if repository_ctx.os.environ.get("CPATH") != None: - for p in repository_ctx.os.environ["CPATH"].strip().split(":"): - if p != "": - additional_inc += [_normalize_include_path(repository_ctx, p)] - if len(additional_inc) > 0: - additional_inc = ",".join(additional_inc) - else: - additional_inc = "\"\"" - - if _enable_mkl(repository_ctx) and repository_ctx.os.environ.get("ONEAPI_MKL_PATH") != None: - sycl_defines["%{ONEAPI_MKL_PATH}"] = str(find_mkl_path(repository_ctx)) - builtin_includes += [find_mkl_path(repository_ctx) + "/include"] - else: - sycl_defines["%{ONEAPI_MKL_PATH}"] = "" - if repository_ctx.os.environ.get("TMPDIR") != None: - sycl_defines["%{TMP_DIRECTORY}"] = repository_ctx.os.environ.get("TMPDIR") - else: - tmp_suffix = repository_ctx.execute(["cat", "/proc/sys/kernel/random/uuid"]).stdout.rstrip() - tmp_dir = "/tmp/" + tmp_suffix - sycl_defines["%{TMP_DIRECTORY}"] = tmp_dir - - sycl_defines["%{cxx_builtin_include_directories}"] = str(builtin_includes) - sycl_defines["%{sycl_builtin_include_directories}"] = str(builtin_includes) - sycl_defines["%{extra_no_canonical_prefixes_flags}"] = "\"-fno-canonical-system-headers\"" - sycl_defines["%{unfiltered_compile_flags}"] = "" - sycl_defines["%{host_compiler}"] = "gcc" - sycl_defines["%{HOST_COMPILER_PATH}"] = "/usr/bin/gcc" - sycl_defines["%{host_compiler_prefix}"] = "/usr/bin" - sycl_defines["%{sycl_compiler_root}"] = str(find_sycl_root(repository_ctx)) - sycl_defines["%{linker_bin_path}"] = "/usr/bin" - sycl_defines["%{SYCL_ROOT_DIR}"] = str(find_sycl_root(repository_ctx)) - sycl_defines["%{AOT_DEVICES}"] = str(find_aot_config(repository_ctx)) - sycl_defines["%{TF_NEED_MKL}"] = repository_ctx.os.environ[_TF_NEED_MKL].strip() - sycl_defines["%{additional_include_directories}"] = additional_inc - sycl_defines["%{SYCL_COMPILER_VERSION}"] = str(get_sycl_version(repository_ctx)) - sycl_defines["%{PYTHON_LIB_PATH}"] = repository_ctx.os.environ[_PYTHON_LIB_PATH] - - sycl_internal_inc_dirs = find_sycl_include_path(repository_ctx) - sycl_internal_inc = "\", \"".join(sycl_internal_inc_dirs) - sycl_internal_isystem_inc = [] - for d in sycl_internal_inc_dirs: - sycl_internal_isystem_inc.append("-isystem\", \"" + d) - - if len(sycl_internal_inc_dirs) > 0: - sycl_defines["%{SYCL_ISYSTEM_INC}"] = "\"]), \n\tflag_group(flags=[ \"".join(sycl_internal_isystem_inc) - sycl_defines["%{SYCL_INTERNAL_INC}"] = sycl_internal_inc - else: - sycl_defines["%{SYCL_ISYSTEM_INC}"] = "" - sycl_defines["%{SYCL_INTERNAL_INC}"] = "" - - unfiltered_cxx_flags = "" if additional_cxxflags == [] else "unfiltered_cxx_flag: " - unfiltered_cxx_flags += "\n unfiltered_cxx_flag: ".join(additional_cxxflags) - - sycl_defines["%{unfiltered_compile_flags}"] = unfiltered_cxx_flags - - linker_flags = "" if additional_linker_flags == [] else "linker_flag: " - linker_flags += "\n linker_flag: ".join(additional_linker_flags) - - # Only expand template variables in the BUILD file - repository_ctx.template( - "crosstool/BUILD", - tpl_paths["crosstool:BUILD.sycl"], - sycl_defines, + mkl_sycl_libs = '"{}",\n"{}",\n"{}",\n"{}",\n"{}",\n"{}",\n"{}",\n"{}",\n'.format( + "sycl/lib/" + sycl_libs["mkl_sycl_blas"].file_name, + "sycl/lib/" + sycl_libs["mkl_sycl_lapack"].file_name, + "sycl/lib/" + sycl_libs["mkl_sycl_sparse"].file_name, + "sycl/lib/" + sycl_libs["mkl_sycl_dft"].file_name, + "sycl/lib/" + sycl_libs["mkl_sycl_vm"].file_name, + "sycl/lib/" + sycl_libs["mkl_sycl_rng"].file_name, + "sycl/lib/" + sycl_libs["mkl_sycl_stats"].file_name, + "sycl/lib/" + sycl_libs["mkl_sycl_data_fitting"].file_name, ) + repository_dict = { + "%{mkl_intel_ilp64_lib}": sycl_libs["mkl_intel_ilp64"].file_name, + "%{mkl_sequential_lib}": sycl_libs["mkl_sequential"].file_name, + "%{mkl_core_lib}": sycl_libs["mkl_core"].file_name, + "%{mkl_sycl_libs}": mkl_sycl_libs, + "%{copy_rules}": "\n".join(copy_rules), + "%{sycl_headers}": ('":mkl-include",\n'), + } + repository_ctx.template( + "sycl/BUILD", + tpl_paths["sycl:BUILD"], + repository_dict, + ) - # No templating of cc_toolchain_config - use attributes and templatize the - # BUILD file. - repository_ctx.template( - "crosstool/cc_toolchain_config.bzl", - tpl_paths["crosstool:sycl_cc_toolchain_config.bzl"], - sycl_defines, - ) + additional_cxxflags = [] + additional_linker_flags = [] + builtin_includes = [] + builtin_includes += _sycl_include_path(repository_ctx, sycl_config, bash_bin) + + pwd = repository_ctx.os.environ["PWD"] + additional_inc = [] + if repository_ctx.os.environ.get("CPATH") != None: + for p in repository_ctx.os.environ["CPATH"].strip().split(":"): + if p != "": + additional_inc += [_normalize_include_path(repository_ctx, p)] + if len(additional_inc) > 0: + additional_inc = ",".join(additional_inc) + else: + additional_inc = "\"\"" - repository_ctx.template( - "crosstool/bin/crosstool_wrapper_driver", - tpl_paths["crosstool/bin:crosstool_wrapper_driver"], - sycl_defines, - ) + if repository_ctx.os.environ.get("TMPDIR") != None: + sycl_defines["%{TMP_DIRECTORY}"] = repository_ctx.os.environ.get("TMPDIR") + else: + tmp_suffix = repository_ctx.execute(["cat", "/proc/sys/kernel/random/uuid"]).stdout.rstrip() + tmp_dir = "/tmp/" + tmp_suffix + sycl_defines["%{TMP_DIRECTORY}"] = tmp_dir + + sycl_defines["%{cxx_builtin_include_directories}"] = str(builtin_includes) + sycl_defines["%{sycl_builtin_include_directories}"] = str(builtin_includes) + sycl_defines["%{extra_no_canonical_prefixes_flags}"] = "\"-fno-canonical-system-headers\"" + sycl_defines["%{unfiltered_compile_flags}"] = to_list_of_strings([ + "-DMKL_ILP64", + ]) + sycl_defines["%{host_compiler}"] = "gcc" + sycl_defines["%{HOST_COMPILER_PATH}"] = "/usr/bin/gcc" + sycl_defines["%{host_compiler_prefix}"] = "/usr/bin" + sycl_defines["%{sycl_compiler_root}"] = str(sycl_config.sycl_toolkit_path) + sycl_defines["%{linker_bin_path}"] = "/usr/bin" + sycl_defines["%{SYCL_ROOT_DIR}"] = str(sycl_config.sycl_toolkit_path) + sycl_defines["%{additional_include_directories}"] = additional_inc + sycl_defines["%{SYCL_COMPILER_VERSION}"] = str(get_sycl_version(repository_ctx, sycl_config)) + sycl_defines["%{PYTHON_LIB_PATH}"] = repository_ctx.os.environ[_PYTHON_LIB_PATH] + sycl_defines["%{basekit_path}"] = str(sycl_config.sycl_basekit_path) + sycl_defines["%{basekit_version}"] = str(sycl_config.sycl_basekit_version_number) + + sycl_internal_inc_dirs = find_sycl_include_path(repository_ctx, sycl_config) + sycl_internal_inc = "\", \"".join(sycl_internal_inc_dirs) + sycl_internal_isystem_inc = [] + for d in sycl_internal_inc_dirs: + sycl_internal_isystem_inc.append("-isystem\", \"" + d) + + if len(sycl_internal_inc_dirs) > 0: + sycl_defines["%{SYCL_ISYSTEM_INC}"] = "\"]), \n\tflag_group(flags=[ \"".join(sycl_internal_isystem_inc) + sycl_defines["%{SYCL_INTERNAL_INC}"] = sycl_internal_inc + else: + sycl_defines["%{SYCL_ISYSTEM_INC}"] = "" + sycl_defines["%{SYCL_INTERNAL_INC}"] = "" + + unfiltered_cxx_flags = "" if additional_cxxflags == [] else "unfiltered_cxx_flag: " + unfiltered_cxx_flags += "\n unfiltered_cxx_flag: ".join(additional_cxxflags) + + sycl_defines["%{unfiltered_compile_flags}"] = unfiltered_cxx_flags + + linker_flags = "" if additional_linker_flags == [] else "linker_flag: " + linker_flags += "\n linker_flag: ".join(additional_linker_flags) - if _enable_sycl_build(repository_ctx): - sycl_build_defines = {} - sycl_build_defines["%{sycl_is_configured}"] = "True" - sycl_build_defines["%{sycl_build_is_configured}"] = "True" - if _enable_mkl(repository_ctx): - sycl_build_defines["%{mkl_is_configured}"] = "True" - sycl_root = find_sycl_root(repository_ctx) - _check_dir(repository_ctx, sycl_root) - - _tpl( - repository_ctx, - "sycl:build_defs.bzl", - sycl_build_defines, - ) + # Only expand template variables in the BUILD file + repository_ctx.template( + "crosstool/BUILD", + tpl_paths["crosstool:BUILD.sycl"], + sycl_defines, + ) + + # No templating of cc_toolchain_config - use attributes and templatize the + # BUILD file. + repository_ctx.template( + "crosstool/cc_toolchain_config.bzl", + tpl_paths["crosstool:sycl_cc_toolchain_config.bzl"], + sycl_defines, + ) + + repository_ctx.template( + "crosstool/bin/crosstool_wrapper_driver", + tpl_paths["crosstool/bin:crosstool_wrapper_driver"], + sycl_defines, + ) + +def _sycl_autoconf_imp(repository_ctx): + """Implementation of the sycl_autoconf rule.""" + if not _enable_sycl(repository_ctx): + _create_dummy_repository(repository_ctx) + else: + _create_local_sycl_repository(repository_ctx) sycl_configure = repository_rule( local = True, diff --git a/third_party/openxla.patch b/third_party/openxla.patch index 39d2817..3ff83a5 100644 --- a/third_party/openxla.patch +++ b/third_party/openxla.patch @@ -1,1204 +1,3 @@ -diff --git a/.bazelrc b/.bazelrc -index e26bf6de7..3e2d558ca 100644 ---- a/.bazelrc -+++ b/.bazelrc -@@ -48,6 +48,7 @@ - # xla: Build TF with XLA - # tpu: Build TF with TPU support - # cuda: Build with full cuda support. -+# sycl: Build with Intel GPU support (sycl). - # rocm: Build with AMD GPU support (rocm). - # mkl: Enable full mkl support. - # tensorrt: Enable Tensorrt support. -@@ -291,6 +292,8 @@ build:rocm --define=using_rocm_hipcc=true - build:rocm --define=tensorflow_mkldnn_contraction_kernel=0 - build:rocm --repo_env TF_NEED_ROCM=1 - -+build:sycl --repo_env TF_NEED_SYCL=1 -+ - # Options extracted from configure script - build:numa --define=with_numa_support=true - -diff --git a/configure.py b/configure.py -index 7b7c3c58d..c605d0e85 100644 ---- a/configure.py -+++ b/configure.py -@@ -767,7 +767,8 @@ def system_specific_test_config(environ_cp): - if is_windows(): - test_and_build_filters += ['-no_windows', '-windows_excluded'] - if ((environ_cp.get('TF_NEED_CUDA', None) == '1') or -- (environ_cp.get('TF_NEED_ROCM', None) == '1')): -+ (environ_cp.get('TF_NEED_ROCM', None) == '1') or -+ (environ_cp.get('TF_NEED_SYCL', None) == '1')): - test_and_build_filters += ['-no_windows_gpu', '-no_gpu'] - else: - test_and_build_filters.append('-gpu') -@@ -775,13 +776,16 @@ def system_specific_test_config(environ_cp): - test_and_build_filters += ['-gpu', '-nomac', '-no_mac', '-mac_excluded'] - elif is_linux(): - if ((environ_cp.get('TF_NEED_CUDA', None) == '1') or -- (environ_cp.get('TF_NEED_ROCM', None) == '1')): -+ (environ_cp.get('TF_NEED_ROCM', None) == '1') or -+ (environ_cp.get('TF_NEED_SYCL', None) == '1')): - test_and_build_filters.append('-no_gpu') - write_to_bazelrc('test --test_env=LD_LIBRARY_PATH') - else: - test_and_build_filters.append('-gpu') - if environ_cp.get('TF_NEED_ROCM', None) == '1': - test_and_build_filters.append('-no_rocm') -+ if environ_cp.get('TF_NEED_SYCL', None) == '1': -+ test_and_build_filters.append('-no_sycl') - - write_to_bazelrc('test --test_tag_filters=%s' % - ','.join(test_and_build_filters + test_only_filters)) -@@ -899,6 +903,18 @@ def get_gcc_compiler(environ_cp): - return None - - -+ -+def set_sycl_path(environ_cp): -+ """Set TF_CUDA_PATHS.""" -+ ask_sycl_path = ( -+ 'Please specify base path to look for SYCL ' -+ 'libraries and headers. [Leave empty to use the default]: ') -+ sycl_path = get_from_env_or_user_or_default(environ_cp, 'SYCL_PATH', -+ ask_sycl_path, '') -+ if sycl_path: -+ environ_cp['SYCL_PATH'] = sycl_path -+ -+ - def main(): - global _TF_WORKSPACE_ROOT - global _TF_BAZELRC -@@ -959,6 +975,13 @@ def main(): - write_to_bazelrc( - 'build --copt="-DEIGEN_ALTIVEC_ENABLE_MMA_DYNAMIC_DISPATCH=1"') - -+ set_action_env_var( -+ environ_cp, 'TF_NEED_SYCL', 'SYCL', False, bazel_config_name='sycl') -+ if (environ_cp.get('TF_NEED_SYCL') == '1'): -+ set_sycl_path(environ_cp) -+ if (environ_cp.get('TF_NEED_SYCL') == '1' and environ_cp.get('SYCL_PATH')): -+ write_action_env_to_bazelrc('SYCL_PATH', environ_cp.get('SYCL_PATH')) -+ - set_action_env_var( - environ_cp, 'TF_NEED_ROCM', 'ROCm', False, bazel_config_name='rocm') - if (environ_cp.get('TF_NEED_ROCM') == '1' and -diff --git a/third_party/tsl/third_party/gpus/compress_find_sycl_config.py b/third_party/tsl/third_party/gpus/compress_find_sycl_config.py -new file mode 100644 -index 000000000..7fb456fe9 ---- /dev/null -+++ b/third_party/tsl/third_party/gpus/compress_find_sycl_config.py -@@ -0,0 +1,36 @@ -+# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -+# -+# Licensed under the Apache License, Version 2.0 (the "License"); -+# you may not use this file except in compliance with the License. -+# You may obtain a copy of the License at -+# -+# http://www.apache.org/licenses/LICENSE-2.0 -+# -+# Unless required by applicable law or agreed to in writing, software -+# distributed under the License is distributed on an "AS IS" BASIS, -+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -+# See the License for the specific language governing permissions and -+# limitations under the License. -+# ============================================================================== -+"""Compresses the contents of 'find_sycl_config.py'. -+ -+The compressed file is what is actually being used. It works around remote -+config not being able to upload files yet. -+""" -+import base64 -+import zlib -+ -+ -+def main(): -+ with open('find_sycl_config.py', 'rb') as f: -+ data = f.read() -+ -+ compressed = zlib.compress(data) -+ b64encoded = base64.b64encode(compressed) -+ -+ with open('find_sycl_config.py.gz.base64', 'wb') as f: -+ f.write(b64encoded) -+ -+ -+if __name__ == '__main__': -+ main() -diff --git a/third_party/tsl/third_party/gpus/find_sycl_config.py b/third_party/tsl/third_party/gpus/find_sycl_config.py -new file mode 100644 -index 000000000..16a1b7ce2 ---- /dev/null -+++ b/third_party/tsl/third_party/gpus/find_sycl_config.py -@@ -0,0 +1,125 @@ -+# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -+# -+# Licensed under the Apache License, Version 2.0 (the "License"); -+# you may not use this file except in compliance with the License. -+# You may obtain a copy of the License at -+# -+# http://www.apache.org/licenses/LICENSE-2.0 -+# -+# Unless required by applicable law or agreed to in writing, software -+# distributed under the License is distributed on an "AS IS" BASIS, -+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -+# See the License for the specific language governing permissions and -+# limitations under the License. -+# ============================================================================== -+"""Prints SYCL library and header directories and versions found on the system. -+ -+The script searches for SYCL library and header files on the system, inspects -+them to determine their version and prints the configuration to stdout. -+The path to inspect is specified through an environment variable (SYCL_PATH). -+If no valid configuration is found, the script prints to stderr and -+returns an error code. -+ -+The script takes the directory specified by the SYCL_PATH environment variable. -+The script looks for headers and library files in a hard-coded set of -+subdirectories from base path of the specified directory. If SYCL_PATH is not -+specified, then "/opt/sycl" is used as it default value -+ -+""" -+ -+import io -+import os -+import re -+import sys -+ -+ -+class ConfigError(Exception): -+ pass -+ -+ -+def _get_default_sycl_path(): -+ return "/opt/intel/oneapi" -+ -+ -+def _get_sycl_install_path(): -+ """Determines and returns the SYCL installation path.""" -+ sycl_install_path = _get_default_sycl_path() -+ if "SYCL_PATH" in os.environ: -+ sycl_install_path = os.environ["SYCL_PATH"] -+ return sycl_install_path -+ -+ -+def _get_composite_version_number(major, minor, patch): -+ return 10000 * major + 100 * minor + patch -+ -+ -+def _get_header_version(path, name): -+ """Returns preprocessor defines in C header file.""" -+ for line in io.open(path, "r", encoding="utf-8"): -+ match = re.match(r"#define %s +(\d+)" % name, line) -+ if match: -+ value = match.group(1) -+ return int(value) -+ -+ raise ConfigError('#define "{}" is either\n'.format(name) + -+ " not present in file {} OR\n".format(path) + -+ " its value is not an integer literal") -+ -+ -+def _find_sycl_config(sycl_install_path): -+ -+ def sycl_version_numbers(path): -+ possible_version_files = [ -+ "compiler/latest/linux/include/sycl/version.hpp", -+ "compiler/latest/include/sycl/version.hpp", -+ ] -+ version_file = None -+ for f in possible_version_files: -+ version_file_path = os.path.join(path, f) -+ if os.path.exists(version_file_path): -+ version_file = version_file_path -+ break -+ if not version_file: -+ raise ConfigError( -+ "SYCL version file not found in {}".format(possible_version_files)) -+ -+ major = _get_header_version(version_file, "__LIBSYCL_MAJOR_VERSION") -+ minor = _get_header_version(version_file, "__LIBSYCL_MINOR_VERSION") -+ patch = _get_header_version(version_file, "__LIBSYCL_PATCH_VERSION") -+ return major, minor, patch -+ -+ major, minor, patch = sycl_version_numbers(sycl_install_path) -+ -+ sycl_config = { -+ "sycl_version_number": _get_composite_version_number(major, minor, patch) -+ } -+ -+ return sycl_config -+ -+ -+def find_sycl_config(): -+ """Returns a dictionary of SYCL components config info.""" -+ sycl_install_path = _get_sycl_install_path() -+ if not os.path.exists(sycl_install_path): -+ raise ConfigError( -+ 'Specified SYCL_PATH "{}" does not exist'.format(sycl_install_path)) -+ -+ result = {} -+ -+ result["sycl_toolkit_path"] = sycl_install_path -+ result.update(_find_sycl_config(sycl_install_path)) -+ -+ return result -+ -+ -+def main(): -+ try: -+ for key, value in sorted(find_sycl_config().items()): -+ print("%s: %s" % (key, value)) -+ except ConfigError as e: -+ sys.stderr.write("\nERROR: {}\n\n".format(str(e))) -+ sys.exit(1) -+ -+ -+if __name__ == "__main__": -+ main() -diff --git a/third_party/tsl/third_party/gpus/find_sycl_config.py.gz.base64 b/third_party/tsl/third_party/gpus/find_sycl_config.py.gz.base64 -new file mode 100644 -index 000000000..9c863dac6 ---- /dev/null -+++ b/third_party/tsl/third_party/gpus/find_sycl_config.py.gz.base64 -@@ -0,0 +1 @@ -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 -\ No newline at end of file -diff --git a/third_party/tsl/third_party/gpus/sycl/BUILD b/third_party/tsl/third_party/gpus/sycl/BUILD -new file mode 100644 -index 000000000..ffd0fb0cd ---- /dev/null -+++ b/third_party/tsl/third_party/gpus/sycl/BUILD -@@ -0,0 +1 @@ -+package(default_visibility = ["//visibility:public"]) -diff --git a/third_party/tsl/third_party/gpus/sycl/BUILD.tpl b/third_party/tsl/third_party/gpus/sycl/BUILD.tpl -new file mode 100644 -index 000000000..aa3688e33 ---- /dev/null -+++ b/third_party/tsl/third_party/gpus/sycl/BUILD.tpl -@@ -0,0 +1,182 @@ -+load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -+ -+licenses(["restricted"]) # MPL2, portions GPL v3, LGPL v3, BSD-like -+ -+package(default_visibility = ["//visibility:public"]) -+ -+config_setting( -+ name = "using_hipcc", -+ values = { -+ "define": "using_rocm_hipcc=true", -+ }, -+) -+ -+cc_library( -+ name = "rocm_headers", -+ hdrs = [ -+ "rocm/rocm_config.h", -+ %{rocm_headers} -+ ], -+ includes = [ -+ ".", -+ "rocm/include", -+ "rocm/include/rocrand", -+ "rocm/include/roctracer", -+ ], -+ visibility = ["//visibility:public"], -+) -+ -+cc_library( -+ name = "hip", -+ srcs = ["rocm/lib/%{hip_lib}"], -+ data = ["rocm/lib/%{hip_lib}"], -+ includes = [ -+ ".", -+ "rocm/include", -+ ], -+ linkstatic = 1, -+ visibility = ["//visibility:public"], -+) -+ -+cc_library( -+ name = "rocblas", -+ srcs = ["rocm/lib/%{rocblas_lib}"], -+ data = ["rocm/lib/%{rocblas_lib}"], -+ includes = [ -+ ".", -+ "rocm/include", -+ ], -+ linkstatic = 1, -+ visibility = ["//visibility:public"], -+) -+ -+cc_library( -+ name = "%{hipfft_or_rocfft}", -+ srcs = ["rocm/lib/%{hipfft_or_rocfft_lib}"], -+ data = ["rocm/lib/%{hipfft_or_rocfft_lib}"], -+ includes = [ -+ ".", -+ "rocm/include", -+ ], -+ linkstatic = 1, -+ visibility = ["//visibility:public"], -+) -+ -+cc_library( -+ name = "hiprand", -+ srcs = ["rocm/lib/%{hiprand_lib}"], -+ data = ["rocm/lib/%{hiprand_lib}"], -+ includes = [ -+ ".", -+ "rocm/include", -+ "rocm/include/rocrand", -+ ], -+ linkstatic = 1, -+ visibility = ["//visibility:public"], -+) -+ -+cc_library( -+ name = "miopen", -+ srcs = ["rocm/lib/%{miopen_lib}"], -+ data = ["rocm/lib/%{miopen_lib}"], -+ includes = [ -+ ".", -+ "rocm/include", -+ ], -+ linkstatic = 1, -+ visibility = ["//visibility:public"], -+) -+ -+cc_library( -+ name = "rccl", -+ srcs = ["rocm/lib/%{rccl_lib}"], -+ data = ["rocm/lib/%{rccl_lib}"], -+ includes = [ -+ ".", -+ "rocm/include", -+ ], -+ linkstatic = 1, -+ visibility = ["//visibility:public"], -+) -+ -+cc_library( -+ name = "rocm", -+ visibility = ["//visibility:public"], -+ deps = [ -+ ":rocm_headers", -+ ":hip", -+ ":rocblas", -+ ":hipblas", -+ ":%{hipfft_or_rocfft}", -+ ":hiprand", -+ ":miopen", -+ ":hipsparse", -+ ":roctracer", -+ ":rocsolver", -+ ":hipsolver", -+ ], -+) -+ -+bzl_library( -+ name = "build_defs_bzl", -+ srcs = ["build_defs.bzl"], -+) -+ -+cc_library( -+ name = "rocprim", -+ srcs = [ -+ "rocm/include/hipcub/hipcub_version.hpp", -+ "rocm/include/rocprim/rocprim_version.hpp", -+ ], -+ hdrs = glob([ -+ "rocm/include/hipcub/**", -+ "rocm/include/rocprim/**", -+ ]), -+ includes = [ -+ ".", -+ "rocm/include/hipcub", -+ "rocm/include/rocprim", -+ ], -+ visibility = ["//visibility:public"], -+ deps = [ -+ "@local_config_rocm//rocm:rocm_headers", -+ ], -+) -+ -+cc_library( -+ name = "hipsparse", -+ srcs = ["rocm/lib/%{hipsparse_lib}"], -+ data = ["rocm/lib/%{hipsparse_lib}"], -+) -+ -+cc_library( -+ name = "roctracer", -+ data = ["rocm/lib/%{roctracer_lib}"], -+) -+ -+cc_library( -+ name = "rocsolver", -+ srcs = ["rocm/lib/%{rocsolver_lib}"], -+ data = ["rocm/lib/%{rocsolver_lib}"], -+) -+ -+cc_library( -+ name = "hipsolver", -+ srcs = ["rocm/lib/%{hipsolver_lib}"], -+ data = ["rocm/lib/%{hipsolver_lib}"], -+) -+ -+cc_library( -+ name = "hipblas", -+ srcs = ["rocm/lib/%{hipblas_lib}"], -+ data = ["rocm/lib/%{hipblas_lib}"], -+) -+ -+filegroup( -+ name = "rocm_root", -+ srcs = [ -+ "rocm/bin/clang-offload-bundler", -+ ], -+) -+ -+%{copy_rules} -diff --git a/third_party/tsl/third_party/gpus/sycl/build_defs.bzl.tpl b/third_party/tsl/third_party/gpus/sycl/build_defs.bzl.tpl -new file mode 100644 -index 000000000..51c0955d2 ---- /dev/null -+++ b/third_party/tsl/third_party/gpus/sycl/build_defs.bzl.tpl -@@ -0,0 +1,45 @@ -+# # Macros for building SYCL code. -+# def if_sycl(if_true, if_false = []): -+# """Shorthand for select()'ing on whether we're building with SYCL. -+# -+# Returns a select statement which evaluates to if_true if we're building -+# with SYCL enabled. Otherwise, the select statement evaluates to if_false. -+# -+# """ -+# return select({ -+# "@local_config_sycl//sycl:using_hipcc": if_true, -+# "//conditions:default": if_false -+# }) -+# -+# -+# def sycl_default_copts(): -+# """Default options for all SYCL compilations.""" -+# return if_sycl(["-x", "sycl"] + %{sycl_extra_copts}) -+# -+# def sycl_copts(opts = []): -+# """Gets the appropriate set of copts for (maybe) SYCL compilation. -+# -+# If we're doing SYCL compilation, returns copts for our particular SYCL -+# compiler. If we're not doing SYCL compilation, returns an empty list. -+# -+# """ -+# return sycl_default_copts() + select({ -+# "//conditions:default": [], -+# "@local_config_sycl//sycl:using_hipcc": ([ -+# "", -+# ]), -+# }) + if_sycl_is_configured(opts) -+ -+def if_sycl_is_configured(x): -+ """Tests if the SYCL was enabled during the configure process. -+ -+ Unlike if_sycl(), this does not require that we are building with -+ --config=sycl. Used to allow non-SYCL code to depend on SYCL libraries. -+ """ -+ if %{sycl_is_configured}: -+ return select({"//conditions:default": x}) -+ return select({"//conditions:default": []}) -+ -+# def sycl_library(copts = [], **kwargs): -+# """Wrapper over cc_library which adds default SYCL options.""" -+# native.cc_library(copts = sycl_default_copts() + copts, **kwargs) -diff --git a/third_party/tsl/third_party/gpus/sycl_configure.bzl b/third_party/tsl/third_party/gpus/sycl_configure.bzl -new file mode 100644 -index 000000000..320b3d303 ---- /dev/null -+++ b/third_party/tsl/third_party/gpus/sycl_configure.bzl -@@ -0,0 +1,682 @@ -+"""Repository rule for SYCL autoconfiguration. -+ -+`sycl_configure` depends on the following environment variables: -+ -+ * `TF_NEED_SYCL`: Whether to enable building with SYCL. -+ * `GCC_HOST_COMPILER_PATH`: The GCC host compiler path -+ * `SYCL_PATH`: The path to the SYCL toolkit. Default is `/opt/sycl`. -+ * `TF_SYCL_AMDGPU_TARGETS`: The AMDGPU targets. -+""" -+ -+load( -+ ":cuda_configure.bzl", -+ "make_copy_dir_rule", -+ "make_copy_files_rule", -+ "to_list_of_strings", -+) -+load( -+ "//third_party/remote_config:common.bzl", -+ "config_repo_label", -+ "err_out", -+ "execute", -+ "files_exist", -+ "get_bash_bin", -+ "get_cpu_value", -+ "get_host_environ", -+ "get_python_bin", -+ "raw_exec", -+ "realpath", -+ "which", -+) -+ -+_GCC_HOST_COMPILER_PATH = "GCC_HOST_COMPILER_PATH" -+_GCC_HOST_COMPILER_PREFIX = "GCC_HOST_COMPILER_PREFIX" -+_SYCL_TOOLKIT_PATH = "SYCL_PATH" -+_TF_SYCL_CONFIG_REPO = "TF_SYCL_CONFIG_REPO" -+ -+_DEFAULT_SYCL_TOOLKIT_PATH = "/opt/sycl" -+ -+def verify_build_defines(params): -+ """Verify all variables that crosstool/BUILD.sycl.tpl expects are substituted. -+ -+ Args: -+ params: dict of variables that will be passed to the BUILD.tpl template. -+ """ -+ missing = [] -+ for param in [ -+ "cxx_builtin_include_directories", -+ "extra_no_canonical_prefixes_flags", -+ "host_compiler_path", -+ "host_compiler_prefix", -+ "linker_bin_path", -+ "unfiltered_compile_flags", -+ ]: -+ if ("%{" + param + "}") not in params: -+ missing.append(param) -+ -+ if missing: -+ auto_configure_fail( -+ "BUILD.sycl.tpl template is missing these variables: " + -+ str(missing) + -+ ".\nWe only got: " + -+ str(params) + -+ ".", -+ ) -+ -+def find_cc(repository_ctx): -+ """Find the C++ compiler.""" -+ -+ # Return a dummy value for GCC detection here to avoid error -+ target_cc_name = "gcc" -+ cc_path_envvar = _GCC_HOST_COMPILER_PATH -+ cc_name = target_cc_name -+ -+ cc_name_from_env = get_host_environ(repository_ctx, cc_path_envvar) -+ if cc_name_from_env: -+ cc_name = cc_name_from_env -+ if cc_name.startswith("/"): -+ # Absolute path, maybe we should make this supported by our which function. -+ return cc_name -+ cc = which(repository_ctx, cc_name) -+ if cc == None: -+ fail(("Cannot find {}, either correct your path or set the {}" + -+ " environment variable").format(target_cc_name, cc_path_envvar)) -+ return cc -+ -+_INC_DIR_MARKER_BEGIN = "#include <...>" -+ -+def _cxx_inc_convert(path): -+ """Convert path returned by cc -E xc++ in a complete path.""" -+ path = path.strip() -+ return path -+ -+def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp): -+ """Compute the list of default C or C++ include directories.""" -+ if lang_is_cpp: -+ lang = "c++" -+ else: -+ lang = "c" -+ -+ # TODO: We pass -no-canonical-prefixes here to match the compiler flags, -+ # but in sycl_clang CROSSTOOL file that is a `feature` and we should -+ # handle the case when it's disabled and no flag is passed -+ result = raw_exec(repository_ctx, [ -+ cc, -+ "-no-canonical-prefixes", -+ "-E", -+ "-x" + lang, -+ "-", -+ "-v", -+ ]) -+ stderr = err_out(result) -+ index1 = stderr.find(_INC_DIR_MARKER_BEGIN) -+ if index1 == -1: -+ return [] -+ index1 = stderr.find("\n", index1) -+ if index1 == -1: -+ return [] -+ index2 = stderr.rfind("\n ") -+ if index2 == -1 or index2 < index1: -+ return [] -+ index2 = stderr.find("\n", index2 + 1) -+ if index2 == -1: -+ inc_dirs = stderr[index1 + 1:] -+ else: -+ inc_dirs = stderr[index1 + 1:index2].strip() -+ -+ return [ -+ str(repository_ctx.path(_cxx_inc_convert(p))) -+ for p in inc_dirs.split("\n") -+ ] -+ -+def get_cxx_inc_directories(repository_ctx, cc): -+ """Compute the list of default C and C++ include directories.""" -+ -+ # For some reason `clang -xc` sometimes returns include paths that are -+ # different from the ones from `clang -xc++`. (Symlink and a dir) -+ # So we run the compiler with both `-xc` and `-xc++` and merge resulting lists -+ includes_cpp = _get_cxx_inc_directories_impl(repository_ctx, cc, True) -+ includes_c = _get_cxx_inc_directories_impl(repository_ctx, cc, False) -+ -+ includes_cpp_set = depset(includes_cpp) -+ return includes_cpp + [ -+ inc -+ for inc in includes_c -+ if inc not in includes_cpp_set.to_list() -+ ] -+ -+def auto_configure_fail(msg): -+ """Output failure message when sycl configuration fails.""" -+ red = "\033[0;31m" -+ no_color = "\033[0m" -+ fail("\n%sSYCL Configuration Error:%s %s\n" % (red, no_color, msg)) -+ -+def auto_configure_warning(msg): -+ """Output warning message during auto configuration.""" -+ yellow = "\033[1;33m" -+ no_color = "\033[0m" -+ print("\n%sAuto-Configuration Warning:%s %s\n" % (yellow, no_color, msg)) -+ -+# END cc_configure common functions (see TODO above). -+ -+def _sycl_include_path(repository_ctx, sycl_config, bash_bin): -+ """Generates the cxx_builtin_include_directory entries for sycl inc dirs. -+ -+ Args: -+ repository_ctx: The repository context. -+ sycl_config: The path to the gcc host compiler. -+ -+ Returns: -+ A string containing the Starlark string for each of the gcc -+ host compiler include directories, which can be added to the CROSSTOOL -+ file. -+ """ -+ inc_dirs = [] -+ -+ sycl_toolkit_path = sycl_config.sycl_toolkit_path + "/compiler/latest/linux" -+ mkl_path = sycl_config.sycl_toolkit_path + "/mkl/latest" -+ inc_dirs.append(sycl_toolkit_path + "/include") -+ inc_dirs.append(mkl_path + "/include") -+ inc_dirs.append(sycl_toolkit_path + "/lib/clang/17/include") -+ -+ return inc_dirs -+ -+def _enable_sycl(repository_ctx): -+ enable_sycl = get_host_environ(repository_ctx, "TF_NEED_SYCL") -+ if enable_sycl == "1": -+ if get_cpu_value(repository_ctx) != "Linux": -+ auto_configure_warning("SYCL configure is only supported on Linux") -+ return False -+ return True -+ return False -+ -+def _hipcc_env(repository_ctx): -+ """Returns the environment variable string for hipcc. -+ -+ Args: -+ repository_ctx: The repository context. -+ -+ Returns: -+ A string containing environment variables for hipcc. -+ """ -+ hipcc_env = "" -+ for name in [ -+ "HIP_CLANG_PATH", -+ "DEVICE_LIB_PATH", -+ "HIP_VDI_HOME", -+ "HIPCC_VERBOSE", -+ "HIPCC_COMPILE_FLAGS_APPEND", -+ "HIPPCC_LINK_FLAGS_APPEND", -+ "HCC_AMDGPU_TARGET", -+ "HIP_PLATFORM", -+ ]: -+ env_value = get_host_environ(repository_ctx, name) -+ if env_value: -+ hipcc_env = (hipcc_env + " " + name + "=\"" + env_value + "\";") -+ return hipcc_env.strip() -+ -+def _crosstool_verbose(repository_ctx): -+ """Returns the environment variable value CROSSTOOL_VERBOSE. -+ -+ Args: -+ repository_ctx: The repository context. -+ -+ Returns: -+ A string containing value of environment variable CROSSTOOL_VERBOSE. -+ """ -+ return get_host_environ(repository_ctx, "CROSSTOOL_VERBOSE", "0") -+ -+def _lib_name(lib, version = "", static = False): -+ """Constructs the name of a library on Linux. -+ -+ Args: -+ lib: The name of the library, such as "hip" -+ version: The version of the library. -+ static: True the library is static or False if it is a shared object. -+ -+ Returns: -+ The platform-specific name of the library. -+ """ -+ if static: -+ return "lib%s.a" % lib -+ else: -+ if version: -+ version = ".%s" % version -+ return "lib%s.so%s" % (lib, version) -+ -+def _sycl_lib_paths(repository_ctx, lib, basedir): -+ file_name = _lib_name(lib, version = "", static = False) -+ return [ -+ repository_ctx.path("%s/lib/%s" % (basedir, file_name)), -+ repository_ctx.path("%s/lib/intel64/%s" % (basedir, file_name)), -+ ] -+ -+def _batch_files_exist(repository_ctx, libs_paths, bash_bin): -+ all_paths = [] -+ for _, lib_paths in libs_paths: -+ for lib_path in lib_paths: -+ all_paths.append(lib_path) -+ return files_exist(repository_ctx, all_paths, bash_bin) -+ -+def _select_sycl_lib_paths(repository_ctx, libs_paths, bash_bin): -+ test_results = _batch_files_exist(repository_ctx, libs_paths, bash_bin) -+ -+ libs = {} -+ i = 0 -+ for name, lib_paths in libs_paths: -+ selected_path = None -+ for path in lib_paths: -+ if test_results[i] and selected_path == None: -+ # For each lib select the first path that exists. -+ selected_path = path -+ i = i + 1 -+ if selected_path == None: -+ auto_configure_fail("Cannot find sycl library %s" % name) -+ -+ libs[name] = struct(file_name = selected_path.basename, path = realpath(repository_ctx, selected_path, bash_bin)) -+ -+ return libs -+ -+def _find_libs(repository_ctx, sycl_config, bash_bin): -+ """Returns the SYCL libraries on the system. -+ -+ Args: -+ repository_ctx: The repository context. -+ sycl_config: The SYCL config as returned by _get_sycl_config -+ bash_bin: the path to the bash interpreter -+ -+ Returns: -+ Map of library names to structs of filename and path -+ """ -+ libs_paths = [ -+ (name, _sycl_lib_paths(repository_ctx, name, path)) -+ for name, path in [ -+ # ("sycl", sycl_config.sycl_toolkit_path + "/compiler/latest/linux/"), -+ ("mkl_sycl", sycl_config.sycl_toolkit_path + "/mkl/latest/"), -+ ("mkl_intel_ilp64", sycl_config.sycl_toolkit_path + "/mkl/latest/"), -+ ("mkl_sequential", sycl_config.sycl_toolkit_path + "/mkl/latest/"), -+ ("mkl_core", sycl_config.sycl_toolkit_path + "/mkl/latest/"), -+ ] -+ ] -+ return _select_sycl_lib_paths(repository_ctx, libs_paths, bash_bin) -+ -+def _exec_find_sycl_config(repository_ctx, script_path): -+ python_bin = get_python_bin(repository_ctx) -+ -+ # If used with remote execution then repository_ctx.execute() can't -+ # access files from the source tree. A trick is to read the contents -+ # of the file in Starlark and embed them as part of the command. In -+ # this case the trick is not sufficient as the find_cuda_config.py -+ # script has more than 8192 characters. 8192 is the command length -+ # limit of cmd.exe on Windows. Thus we additionally need to compress -+ # the contents locally and decompress them as part of the execute(). -+ compressed_contents = repository_ctx.read(script_path) -+ decompress_and_execute_cmd = ( -+ "from zlib import decompress;" + -+ "from base64 import b64decode;" + -+ "from os import system;" + -+ "script = decompress(b64decode('%s'));" % compressed_contents.rstrip('\n') + -+ "f = open('script.py', 'wb');" + -+ "f.write(script);" + -+ "f.close();" + -+ "system('\"%s\" script.py');" % (python_bin) -+ ) -+ return execute(repository_ctx, [python_bin, "-c", decompress_and_execute_cmd]) -+ -+def find_sycl_config(repository_ctx, script_path): -+ """Returns SYCL config dictionary from running find_sycl_config.py""" -+ exec_result = _exec_find_sycl_config(repository_ctx, script_path) -+ if exec_result.return_code: -+ auto_configure_fail("Failed to run find_sycl_config.py: %s" % err_out(exec_result)) -+ -+ # Parse the dict from stdout. -+ return dict([tuple(x.split(": ")) for x in exec_result.stdout.splitlines()]) -+ -+def _get_sycl_config(repository_ctx, bash_bin, find_sycl_config_script): -+ """Detects and returns information about the SYCL installation on the system. -+ -+ Args: -+ repository_ctx: The repository context. -+ bash_bin: the path to the path interpreter -+ -+ Returns: -+ A struct containing the following fields: -+ sycl_toolkit_path: The SYCL toolkit installation directory. -+ amdgpu_targets: A list of the system's AMDGPU targets. -+ sycl_version_number: The version of SYCL on the system. -+ miopen_version_number: The version of MIOpen on the system. -+ hipruntime_version_number: The version of HIP Runtime on the system. -+ """ -+ config = find_sycl_config(repository_ctx, find_sycl_config_script) -+ sycl_toolkit_path = config["sycl_toolkit_path"] -+ sycl_version_number = config["sycl_version_number"] -+ return struct( -+ sycl_toolkit_path = sycl_toolkit_path, -+ sycl_version_number = sycl_version_number, -+ ) -+ -+def _tpl_path(repository_ctx, labelname): -+ return repository_ctx.path(Label("//third_party/gpus/%s.tpl" % labelname)) -+ -+def _tpl(repository_ctx, tpl, substitutions = {}, out = None): -+ if not out: -+ out = tpl.replace(":", "/") -+ repository_ctx.template( -+ out, -+ _tpl_path(repository_ctx, tpl), -+ substitutions, -+ ) -+ -+_DUMMY_CROSSTOOL_BZL_FILE = """ -+def error_gpu_disabled(): -+ fail("ERROR: Building with --config=sycl but TensorFlow is not configured " + -+ "to build with GPU support. Please re-run ./configure and enter 'Y' " + -+ "at the prompt to build with GPU support.") -+ -+ native.genrule( -+ name = "error_gen_crosstool", -+ outs = ["CROSSTOOL"], -+ cmd = "echo 'Should not be run.' && exit 1", -+ ) -+ -+ native.filegroup( -+ name = "crosstool", -+ srcs = [":CROSSTOOL"], -+ output_licenses = ["unencumbered"], -+ ) -+""" -+ -+_DUMMY_CROSSTOOL_BUILD_FILE = """ -+load("//crosstool:error_gpu_disabled.bzl", "error_gpu_disabled") -+ -+error_gpu_disabled() -+""" -+ -+def _create_dummy_repository(repository_ctx): -+ # Set up BUILD file for sycl/. -+ _tpl( -+ repository_ctx, -+ "sycl:build_defs.bzl", -+ { -+ "%{sycl_is_configured}": "False", -+ "%{sycl_extra_copts}": "[]", -+ "%{sycl_gpu_architectures}": "[]", -+ "%{sycl_version_number}": "0", -+ }, -+ ) -+ _tpl( -+ repository_ctx, -+ "sycl:BUILD", -+ { -+ "%{sycl_lib}": _lib_name("sycl"), -+ "%{rocblas_lib}": _lib_name("rocblas"), -+ "%{hipblas_lib}": _lib_name("hipblas"), -+ "%{miopen_lib}": _lib_name("miopen"), -+ "%{rccl_lib}": _lib_name("rccl"), -+ "%{hipfft_or_rocfft}": _lib_name("hipfft"), -+ "%{hipfft_or_rocfft_lib}": _lib_name("hipfft"), -+ "%{hiprand_lib}": _lib_name("hiprand"), -+ "%{hipsparse_lib}": _lib_name("hipsparse"), -+ "%{roctracer_lib}": _lib_name("roctracer64"), -+ "%{rocsolver_lib}": _lib_name("rocsolver"), -+ "%{hipsolver_lib}": _lib_name("hipsolver"), -+ "%{copy_rules}": "", -+ "%{sycl_headers}": "", -+ }, -+ ) -+ -+ # Create dummy files for the SYCL toolkit since they are still required by -+ # tensorflow/compiler/xla/stream_executor/sycl:sycl_rpath -+ repository_ctx.file("sycl/hip/include/hip/hip_runtime.h", "") -+ -+ # # Set up sycl_config.h, which is used by -+ # # tensorflow/compiler/xla/stream_executor/dso_loader.cc. -+ # _tpl( -+ # repository_ctx, -+ # "sycl:sycl_config.h", -+ # { -+ # "%{sycl_toolkit_path}": _DEFAULT_SYCL_TOOLKIT_PATH, -+ # }, -+ # "sycl/sycl/sycl_config.h", -+ # ) -+ -+ # If sycl_configure is not configured to build with GPU support, and the user -+ # attempts to build with --config=sycl, add a dummy build rule to intercept -+ # this and fail with an actionable error message. -+ repository_ctx.file( -+ "crosstool/error_gpu_disabled.bzl", -+ _DUMMY_CROSSTOOL_BZL_FILE, -+ ) -+ repository_ctx.file("crosstool/BUILD", _DUMMY_CROSSTOOL_BUILD_FILE) -+ -+def _norm_path(path): -+ """Returns a path with '/' and remove the trailing slash.""" -+ path = path.replace("\\", "/") -+ if path[-1] == "/": -+ path = path[:-1] -+ return path -+ -+def _genrule(src_dir, genrule_name, command, outs): -+ """Returns a string with a genrule. -+ -+ Genrule executes the given command and produces the given outputs. -+ """ -+ return ( -+ "genrule(\n" + -+ ' name = "' + -+ genrule_name + '",\n' + -+ " outs = [\n" + -+ outs + -+ "\n ],\n" + -+ ' cmd = """\n' + -+ command + -+ '\n """,\n' + -+ ")\n" -+ ) -+ -+def _compute_sycl_extra_copts(repository_ctx, amdgpu_targets): -+ amdgpu_target_flags = ["--amdgpu-target=" + -+ amdgpu_target for amdgpu_target in amdgpu_targets] -+ return str(amdgpu_target_flags) -+ -+def _create_local_sycl_repository(repository_ctx): -+ """Creates the repository containing files set up to build with SYCL.""" -+ -+ tpl_paths = {labelname: _tpl_path(repository_ctx, labelname) for labelname in [ -+ "sycl:build_defs.bzl", -+ "sycl:BUILD", -+ # "sycl:sycl_config.h", -+ ]} -+ -+ find_sycl_config_script = repository_ctx.path(Label("//third_party/gpus:find_sycl_config.py.gz.base64")) -+ -+ bash_bin = get_bash_bin(repository_ctx) -+ sycl_config = _get_sycl_config(repository_ctx, bash_bin, find_sycl_config_script) -+ -+ # For SYCL 4.1 and above use hipfft, older SYCL versions use rocfft -+ sycl_version_number = int(sycl_config.sycl_version_number) -+ # hipfft_or_rocfft = "rocfft" if sycl_version_number < 40100 else "hipfft" -+ -+ # Copy header and library files to execroot. -+ # sycl_toolkit_path -+ sycl_toolkit_path = sycl_config.sycl_toolkit_path -+ copy_rules = [ -+ make_copy_dir_rule( -+ repository_ctx, -+ name = "sycl-include", -+ src_dir = sycl_toolkit_path + "/include", -+ out_dir = "sycl/include", -+ exceptions = ["gtest", "gmock"], -+ ), -+ ] -+ -+ sycl_libs = _find_libs(repository_ctx, sycl_config, bash_bin) -+ sycl_lib_srcs = [] -+ sycl_lib_outs = [] -+ for lib in sycl_libs.values(): -+ sycl_lib_srcs.append(lib.path) -+ sycl_lib_outs.append("sycl/lib/" + lib.file_name) -+ copy_rules.append(make_copy_files_rule( -+ repository_ctx, -+ name = "sycl-lib", -+ srcs = sycl_lib_srcs, -+ outs = sycl_lib_outs, -+ )) -+ -+ clang_offload_bundler_path = sycl_toolkit_path + "/llvm/bin/clang-offload-bundler" -+ -+ # copy files mentioned in third_party/gpus/sycl/BUILD -+ copy_rules.append(make_copy_files_rule( -+ repository_ctx, -+ name = "sycl-bin", -+ srcs = [ -+ clang_offload_bundler_path, -+ ], -+ outs = [ -+ "sycl/bin/" + "clang-offload-bundler", -+ ], -+ )) -+ -+ # Set up BUILD file for sycl/ -+ repository_ctx.template( -+ "sycl/build_defs.bzl", -+ tpl_paths["sycl:build_defs.bzl"], -+ { -+ "%{sycl_is_configured}": "True", -+ # "%{sycl_extra_copts}": _compute_sycl_extra_copts( -+ # repository_ctx, -+ # sycl_config.amdgpu_targets, -+ # ), -+ # "%{sycl_gpu_architectures}": str(sycl_config.amdgpu_targets), -+ "%{sycl_version_number}": str(sycl_version_number), -+ }, -+ ) -+ -+ repository_dict = { -+ # "%{sycl_lib}": sycl_libs["sycl"].file_name, -+ # "%{rocblas_lib}": sycl_libs["rocblas"].file_name, -+ # "%{hipfft_or_rocfft}": hipfft_or_rocfft, -+ # "%{hipfft_or_rocfft_lib}": sycl_libs[hipfft_or_rocfft].file_name, -+ # "%{hiprand_lib}": sycl_libs["hiprand"].file_name, -+ # "%{miopen_lib}": sycl_libs["MIOpen"].file_name, -+ # "%{rccl_lib}": sycl_libs["rccl"].file_name, -+ # "%{hipsparse_lib}": sycl_libs["hipsparse"].file_name, -+ # "%{roctracer_lib}": sycl_libs["roctracer64"].file_name, -+ # "%{rocsolver_lib}": sycl_libs["rocsolver"].file_name, -+ "%{copy_rules}": "\n".join(copy_rules), -+ "%{sycl_headers}": ('":sycl-include",\n'), -+ } -+ -+ repository_ctx.template( -+ "sycl/BUILD", -+ tpl_paths["sycl:BUILD"], -+ repository_dict, -+ ) -+ -+ # Set up crosstool/ -+ -+ cc = find_cc(repository_ctx) -+ -+ host_compiler_includes = get_cxx_inc_directories(repository_ctx, cc) -+ -+ host_compiler_prefix = get_host_environ(repository_ctx, _GCC_HOST_COMPILER_PREFIX, "/usr/bin") -+ -+ sycl_defines = {} -+ -+ sycl_defines["%{host_compiler_prefix}"] = host_compiler_prefix -+ -+ sycl_defines["%{linker_bin_path}"] = sycl_config.sycl_toolkit_path + "/hcc/compiler/bin" -+ -+ # For gcc, do not canonicalize system header paths; some versions of gcc -+ # pick the shortest possible path for system includes when creating the -+ # .d file - given that includes that are prefixed with "../" multiple -+ # time quickly grow longer than the root of the tree, this can lead to -+ # bazel's header check failing. -+ sycl_defines["%{extra_no_canonical_prefixes_flags}"] = "\"-fno-canonical-system-headers\"" -+ -+ sycl_defines["%{unfiltered_compile_flags}"] = to_list_of_strings([ -+ "-DTENSORFLOW_USE_SYCL=1", -+ "-D__HIP_PLATFORM_HCC__", -+ "-DEIGEN_USE_HIP", -+ ]) -+ -+ sycl_defines["%{host_compiler_path}"] = "clang/bin/crosstool_wrapper_driver_is_not_gcc" -+ -+ sycl_defines["%{cxx_builtin_include_directories}"] = to_list_of_strings( -+ host_compiler_includes + _sycl_include_path(repository_ctx, sycl_config, bash_bin), -+ ) -+ -+ verify_build_defines(sycl_defines) -+ -+def _create_remote_sycl_repository(repository_ctx, remote_config_repo): -+ """Creates pointers to a remotely configured repo set up to build with SYCL.""" -+ _tpl( -+ repository_ctx, -+ "sycl:build_defs.bzl", -+ { -+ "%{sycl_is_configured}": "True", -+ "%{sycl_extra_copts}": _compute_sycl_extra_copts( -+ repository_ctx, -+ [], #_compute_capabilities(repository_ctx) -+ ), -+ }, -+ ) -+ repository_ctx.template( -+ "sycl/BUILD", -+ config_repo_label(remote_config_repo, "sycl:BUILD"), -+ {}, -+ ) -+ repository_ctx.template( -+ "sycl/build_defs.bzl", -+ config_repo_label(remote_config_repo, "sycl:build_defs.bzl"), -+ {}, -+ ) -+ repository_ctx.template( -+ "sycl/sycl/sycl_config.h", -+ config_repo_label(remote_config_repo, "sycl:sycl/sycl_config.h"), -+ {}, -+ ) -+ -+def _sycl_autoconf_impl(repository_ctx): -+ """Implementation of the sycl_autoconf repository rule.""" -+ if not _enable_sycl(repository_ctx): -+ _create_dummy_repository(repository_ctx) -+ elif get_host_environ(repository_ctx, _TF_SYCL_CONFIG_REPO) != None: -+ _create_remote_sycl_repository( -+ repository_ctx, -+ get_host_environ(repository_ctx, _TF_SYCL_CONFIG_REPO), -+ ) -+ else: -+ _create_local_sycl_repository(repository_ctx) -+ -+_ENVIRONS = [ -+ _GCC_HOST_COMPILER_PATH, -+ _GCC_HOST_COMPILER_PREFIX, -+ "TF_NEED_SYCL", -+ _SYCL_TOOLKIT_PATH, -+] -+ -+remote_sycl_configure = repository_rule( -+ implementation = _create_local_sycl_repository, -+ environ = _ENVIRONS, -+ remotable = True, -+ attrs = { -+ "environ": attr.string_dict(), -+ }, -+) -+ -+sycl_configure = repository_rule( -+ implementation = _sycl_autoconf_impl, -+ environ = _ENVIRONS + [_TF_SYCL_CONFIG_REPO], -+) -+"""Detects and configures the local SYCL toolchain. -+ -+Add the following to your WORKSPACE FILE: -+ -+```python -+sycl_configure(name = "local_config_sycl") -+``` -+ -+Args: -+ name: A unique name for this workspace rule. -+""" diff --git a/third_party/tsl/third_party/grpc/upb_platform_fix.patch b/third_party/tsl/third_party/grpc/upb_platform_fix.patch index 6edd66067..022c9d155 100644 --- a/third_party/tsl/third_party/grpc/upb_platform_fix.patch @@ -1375,26 +174,6 @@ index 980ca390d..2374e6d10 100644 }), ) -diff --git a/third_party/tsl/workspace2.bzl b/third_party/tsl/workspace2.bzl -index b18535eea..0a944da74 100644 ---- a/third_party/tsl/workspace2.bzl -+++ b/third_party/tsl/workspace2.bzl -@@ -4,6 +4,7 @@ - load("@bazel_skylib//lib:versions.bzl", "versions") - load("//third_party/gpus:cuda_configure.bzl", "cuda_configure") - load("//third_party/gpus:rocm_configure.bzl", "rocm_configure") -+load("//third_party/gpus:sycl_configure.bzl", "sycl_configure") - load("//third_party/tensorrt:tensorrt_configure.bzl", "tensorrt_configure") - load("//third_party/nccl:nccl_configure.bzl", "nccl_configure") - load("//third_party/git:git_configure.bzl", "git_configure") -@@ -74,6 +75,7 @@ def _tf_toolchains(): - syslibs_configure(name = "local_config_syslibs") - python_configure(name = "local_config_python") - rocm_configure(name = "local_config_rocm") -+ sycl_configure(name = "local_config_sycl") - remote_execution_configure(name = "local_config_remote_execution") - - # For windows bazel build diff --git a/xla/mlir_hlo/lhlo_gpu/IR/lhlo_gpu_ops.td b/xla/mlir_hlo/lhlo_gpu/IR/lhlo_gpu_ops.td index 72fd56b2a..87451f0c5 100644 --- a/xla/mlir_hlo/lhlo_gpu/IR/lhlo_gpu_ops.td @@ -2573,6 +1352,32 @@ index 3e171c793..6cad72bf2 100644 tsl::Status GpuExecutor::GetKernel(const MultiKernelLoaderSpec& spec, KernelBase* kernel) { GpuKernel* cuda_kernel = AsGpuKernel(kernel); +diff --git a/xla/stream_executor/gpu/BUILD b/xla/stream_executor/gpu/BUILD +index 84a92809e..8951c0686 100644 +--- a/xla/stream_executor/gpu/BUILD ++++ b/xla/stream_executor/gpu/BUILD +@@ -9,6 +9,10 @@ load( + "@local_config_rocm//rocm:build_defs.bzl", + "if_rocm_is_configured", + ) ++load( ++ "@local_config_sycl//sycl:build_defs.bzl", ++ "if_sycl_is_configured", ++) + load( + "@tsl//tsl:tsl.bzl", + "if_libtpu", +@@ -269,7 +273,9 @@ cc_library( + "@local_config_cuda//cuda:cuda_headers", + ]) + if_rocm_is_configured([ + "@local_config_rocm//rocm:rocm_headers", +- ]), ++ ]) + if_sycl_is_configured([ ++ "@local_config_sycl//sycl:sycl_headers", ++ ]) + ) + + cc_library( diff --git a/xla/stream_executor/gpu/gpu_driver.h b/xla/stream_executor/gpu/gpu_driver.h index 9fce9d68d..4badb209b 100644 --- a/xla/stream_executor/gpu/gpu_driver.h diff --git a/xla/service/gpu/BUILD b/xla/service/gpu/BUILD index cddb791..73ff455 100644 --- a/xla/service/gpu/BUILD +++ b/xla/service/gpu/BUILD @@ -1,6 +1,10 @@ load("//xla:xla.bzl", "xpu_library") load("//xla:xla.bzl", "xetla_library") load("//third_party/onednn:build_defs.bzl", "onednn_deps") +load( + "@local_config_sycl//sycl:build_defs.bzl", + "if_sycl_is_configured", +) package( default_visibility = [ @@ -887,7 +891,9 @@ cc_library( deps = [ "@tsl//tsl/framework:numeric_types", "@tsl//tsl/platform:logging", - ], + ] + if_sycl_is_configured([ + "@local_config_sycl//sycl:sycl_headers", + ]), ) cc_library( diff --git a/xla/stream_executor/sycl/BUILD b/xla/stream_executor/sycl/BUILD index 5b4215e..98fa3c0 100644 --- a/xla/stream_executor/sycl/BUILD +++ b/xla/stream_executor/sycl/BUILD @@ -1,4 +1,8 @@ load("//xla:xla.bzl", "xpu_library") +load( + "@local_config_sycl//sycl:build_defs.bzl", + "if_sycl_is_configured", +) package( default_visibility = [ @@ -57,7 +61,9 @@ cc_library( "@xla//xla/stream_executor/gpu:gpu_driver_header", "@xla//xla/stream_executor/platform", "@xla//xla/stream_executor/platform:dso_loader", - ], + ] + if_sycl_is_configured([ + "@local_config_sycl//sycl:mkl", + ]), ) cc_library(