Skip to content

Commit

Permalink
Refine MKL build (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShengYang1 authored Oct 26, 2023
1 parent 1c1e6ff commit d176642
Show file tree
Hide file tree
Showing 14 changed files with 620 additions and 1,459 deletions.
8 changes: 4 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -63,3 +59,7 @@ load(
)

bazel_toolchains_repositories()

load("//xla:workspace.bzl", "workspace")

workspace()
31 changes: 0 additions & 31 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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,
Expand All @@ -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."""
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions third_party/gpus/compress_find_sycl_config.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion third_party/gpus/crosstool/BUILD.sycl.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
)

Expand Down
24 changes: 13 additions & 11 deletions third_party/gpus/crosstool/bin/crosstool_wrapper_driver.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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()"""
Expand All @@ -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')
Expand All @@ -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")
Expand All @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions third_party/gpus/crosstool/sycl_cc_toolchain_config.bzl.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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],
Expand Down
132 changes: 132 additions & 0 deletions third_party/gpus/find_sycl_config.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions third_party/gpus/find_sycl_config.py.gz.base64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eJytV9tu2zgQfddXDBgUlTaunPZp4UUe3DRFvc3ahe22KJLAoCXaZiOLWpJKYhT5950hJVm+FEGL9UOiy3B4ZubM4egELlSx0XK5svDm7M0ZTFcCpiI3Sr/P1AP0S7tS2sTQzzIYk5mBsTBC34s0Dk6CE7iSCZqLFMo8FRosru8XPMF/1ZsOfBHaSJXDm/gMQjJg1SsW/YUeNqqENd9AriyURqALaWAhMwHiMRGFBZlDotZFJnmeCHiQduW2qZwgDPhWuVBzy9Gao32Bd4u2HXDrANNvZW3R63YfHh5i7sDGSi+7mTc03avBxeVwcvkKAbsln/NMGANa/FtKjaHON8ALxJPwOaLM+AMoDXypBb6zivA+aGllvuyAUQv7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z0NkhP/Ahl6g57X5+We/Mfjw58fBn9E3+Msbg0HPocgSnldvdHwM3JdFxTqqJWXBz0o8nnAJucla7cPz8uQuJ4u5j8fpGmk7ysBSUXex0nrGoriWCTX0v+pMjbJMdE4ybkJ2z2CWUCSsT2hYpZyQKfGPj5fscrhuUbE+VcB7Ly0cUriQrU+HktlutjldFwTo/X/nMmlv3t41kq0ueZAvK7XHMDRtaD1v64mTju5I1MRc1QZBN9WvxiJOeCQ88RL0mpD1wB6aN4Rwl6q6mKxWzbVr7O+RhixtOCZsJxW1Ifvy4hGlAojbEOpqRyHEdKmU4P9r/7QXYrbPZ1eCtU+B/+n+PxrMvl+MJzq/MJ8uLyq86GgwPHBVV5/+SIzwLLj7sOaq6+4hEUuhHHuOmR1tip3uC+hz0rYVrflR1YUcWs95vCHhnx+He0bLn+ODgocVPwfYIaGGt5OFAHfbVneNsldBYQCOb8rOV+5rDbqMhs4pc5gtVaXw7Q3Xpdg9YNDo2OBybFYjIe123p1/PtMfLSTMkHgwMXsNTJbyKOv+NjO9s4w8DYWg0xCI/bW+vdwtD1uwWbdoP9o3bgTrj9oPGOC6LFNUwfEbBo1Z9/cKqtGv8kvbltHrTa5TxTmw69emBjMDZVaThIQ1i5OfahFEjae6jImQvTA8PXTptw62niKpVfey3qkAztaiHORP7j5GYPq1FyG7yy/F4NO5hNm/y1tmHn9AhOoyaZVgWS8c0DuJ4nM3oeJ3N8LOUWp5inM1Yz/UwhRv8B886rVM=
36 changes: 36 additions & 0 deletions third_party/gpus/sycl/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Loading

0 comments on commit d176642

Please sign in to comment.