diff --git a/third_party/gpus/rocm/BUILD.tpl b/third_party/gpus/rocm/BUILD.tpl index 1740ae4bf54dc0..e153abcab6c25b 100644 --- a/third_party/gpus/rocm/BUILD.tpl +++ b/third_party/gpus/rocm/BUILD.tpl @@ -1,9 +1,22 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") load("@local_config_rocm//rocm:build_defs.bzl", "rocm_version_number", "select_threshold") licenses(["restricted"]) # MPL2, portions GPL v3, LGPL v3, BSD-like -package(default_visibility = ["//visibility:public"]) +package(default_visibility = ["//visibility:private"]) + +bool_flag( + name = "hermetic", + build_setting_default = False, +) + +config_setting( + name = "build_hermetic", + flag_values = { + ":hermetic": "True", + }, +) config_setting( name = "using_hipcc", @@ -13,13 +26,34 @@ config_setting( ) cc_library( - name = "rocm_config", + name = "config", hdrs = [ "rocm_config/rocm_config.h", ], include_prefix = "rocm", strip_include_prefix = "rocm_config", +) + +cc_library( + name = "config_hermetic", + hdrs = [ + "rocm_config_hermetic/rocm_config.h", + ], + include_prefix = "rocm", + strip_include_prefix = "rocm_config_hermetic", +) + +cc_library( + name = "rocm_config", visibility = ["//visibility:public"], + deps = select({ + ":build_hermetic": [ + ":config_hermetic", + ], + "//conditions:default": [ + "config", + ], + }), ) cc_library( @@ -73,12 +107,35 @@ cc_library( ], linkstatic = 1, strip_include_prefix = "%{rocm_root}", - visibility = ["//visibility:public"], deps = [":rocm_config"], ) +cc_library( + name = "rocm_rpath", + linkopts = select({ + ":build_hermetic": [ + "-Wl,-rpath=%{rocm_toolkit_path}/lib", + "-Wl,-rpath=%{rocm_toolkit_path}/lib", + ], + "//conditions:default": [ + "-Wl,-rpath=/opt/rocm/lib", + ], + }), + visibility = ["//visibility:public"], +) + cc_library( name = "hip", + data = glob(["%{rocm_root}/lib/**"]), + visibility = ["//visibility:public"], + deps = [ + ":rocm_hip", + ":rocm_rpath", + ], +) + +cc_library( + name = "rocm_hip", srcs = glob(["%{rocm_root}/lib/libamdhip*.so*"]), hdrs = glob(["%{rocm_root}/include/hip/**"]), data = glob(["%{rocm_root}/lib/hip/**"]), @@ -86,52 +143,20 @@ cc_library( includes = [ "%{rocm_root}/include", ], - linkstatic = 1, strip_include_prefix = "%{rocm_root}", - visibility = ["//visibility:public"], deps = [ ":amd_comgr", ":hsa_rocr", ":rocm_config", + ":rocm_smi", ":rocprofiler_register", + ":system_libs", ], ) -cc_library( - name = "rocprofiler_register", - srcs = glob([ - "%{rocm_root}/lib/librocprofiler-register.so*", - ]), - include_prefix = "rocm", - includes = [ - "%{rocm_root}/include", - ], - strip_include_prefix = "%{rocm_root}", - visibility = ["//visibility:public"], - deps = [":rocm_config"], -) - -cc_library( - name = "amd_comgr", - srcs = glob([ - "%{rocm_root}/lib/libamd_comgr.so*", - ]), - hdrs = glob(["%{rocm_root}/include/amd_comgr/**"]), - include_prefix = "rocm", - includes = [ - "%{rocm_root}/include", - ], - strip_include_prefix = "%{rocm_root}", - visibility = ["//visibility:public"], - deps = [":rocm_config"], -) - cc_library( name = "rocblas", hdrs = glob(["%{rocm_root}/include/rocblas/**"]), - # workaround to bring tensile files to the same fs layout as expected in the lib - # rocblas assumes that tensile files are located in ../roblas/libraries directory - copts = ["-rpath local_config_rocm/rocm/rocm_dis/lib"], data = glob([ "%{rocm_root}/lib/librocblas*.so*", "%{rocm_root}/lib/rocblas/**", @@ -140,6 +165,9 @@ cc_library( includes = [ "%{rocm_root}/include", ], + # workaround to bring tensile files to the same fs layout as expected in the lib + # rocblas assumes that tensile files are located in ../roblas/libraries directory + linkopts = ["-Wl,-rpath=local_config_rocm/rocm/rocm_dis/lib"], strip_include_prefix = "%{rocm_root}", visibility = ["//visibility:public"], deps = [":rocm_config"], @@ -154,6 +182,7 @@ cc_library( "%{rocm_root}/include", ], linkstatic = 1, + visibility = ["//visibility:public"], deps = [":rocm_config"], ) @@ -188,9 +217,6 @@ cc_library( cc_library( name = "miopen", hdrs = glob(["%{rocm_root}/include/rccl/**"]), - # workaround to bring miopen db files to the same fs layout as expected in the lib - # rocblas assumes that miopen db files are located in ../share/miopen/db directory - copts = ["-rpath local_config_rocm/rocm/rocm_dis/lib"], data = glob([ "%{rocm_root}/lib/libMIOpen*.so*", "%{rocm_root}/share/miopen/**", @@ -199,6 +225,9 @@ cc_library( includes = [ "%{rocm_root}/include", ], + # workaround to bring miopen db files to the same fs layout as expected in the lib + # rocblas assumes that miopen db files are located in ../share/miopen/db directory + linkopts = ["-Wl,-rpath=local_config_rocm/rocm/rocm_dis/lib"], strip_include_prefix = "%{rocm_root}", visibility = ["//visibility:public"], deps = [":rocm_config"], @@ -219,15 +248,10 @@ cc_library( deps = [":rocm_config"], ) -filegroup( - name = "rocm_bin", - srcs = glob(["%{rocm_root}/bin/**/*"]), - visibility = ["//visibility:public"], -) - bzl_library( name = "build_defs_bzl", srcs = ["build_defs.bzl"], + visibility = ["//visibility:public"], ) cc_library( @@ -325,9 +349,6 @@ cc_library( cc_library( name = "hipblaslt", hdrs = glob(["%{rocm_root}/include/hipblaslt/**"]), - # workaround to bring tensile files to the same fs layout as expected in the lib - # hibplatslt assumes that tensile files are located in ../hipblaslt/libraries directory - copts = ["-rpath local_config_rocm/rocm/rocm_dis/lib"], data = glob([ "%{rocm_root}/lib/hipblaslt/**", "%{rocm_root}/lib/libhipblaslt.so*", @@ -336,6 +357,9 @@ cc_library( includes = [ "%{rocm_root}/include/", ], + # workaround to bring tensile files to the same fs layout as expected in the lib + # hibplatslt assumes that tensile files are located in ../hipblaslt/libraries directory + linkopts = ["-Wl,-rpath=local_config_rocm/rocm/rocm_dis/lib"], strip_include_prefix = "%{rocm_root}", visibility = ["//visibility:public"], deps = [":rocm_config"], @@ -354,14 +378,74 @@ cc_library( deps = [":rocm_config"], ) +cc_library( + name = "rocprofiler_register", + srcs = glob([ + "%{rocm_root}/lib/librocprofiler-register.so*", + ]), + include_prefix = "rocm", + includes = [ + "%{rocm_root}/include", + ], + strip_include_prefix = "%{rocm_root}", + deps = [":rocm_config"], +) + +cc_library( + name = "amd_comgr", + srcs = glob([ + "%{rocm_root}/lib/libamd_comgr.so*", + ]), + hdrs = glob(["%{rocm_root}/include/amd_comgr/**"]), + include_prefix = "rocm", + includes = [ + "%{rocm_root}/include", + ], + strip_include_prefix = "%{rocm_root}", + deps = [":rocm_config"], +) + +cc_library( + name = "rocm_smi", + srcs = glob([ + "%{rocm_root}/lib/librocm_smi64.so*", + "%{rocm_root}/lib/libroam.so*", + ]), + hdrs = glob([ + "%{rocm_root}/include/oam/**", + "%{rocm_root}/include/rocm_smi/**", + ]), + include_prefix = "rocm", + includes = [ + "%{rocm_root}/include", + ], + strip_include_prefix = "%{rocm_root}", + deps = [":rocm_config"], +) + +cc_library( + name = "system_libs", + srcs = glob([ + "rocm_dist/usr/lib/**/libelf.so*", + "rocm_dist/usr/lib/**/libdrm.so*", + "rocm_dist/usr/lib/**/libnuma.so*", + "rocm_dist/usr/lib/**/libdrm_amdgpu.so*", + ]), + data = glob([ + "rocm_dist/usr/**", + ]), +) + filegroup( name = "rocm_root", srcs = [ "%{rocm_root}/bin/clang-offload-bundler", ], + visibility = ["//visibility:public"], ) filegroup( name = "all_files", srcs = glob(["%{rocm_root}/**"]), + visibility = ["//visibility:public"], ) diff --git a/third_party/gpus/rocm/rocm_redist_ubuntu_20_04.bzl b/third_party/gpus/rocm/rocm_redist_ubuntu_20_04.bzl index 3970c29de1cdcb..fd2d8c363aebb3 100644 --- a/third_party/gpus/rocm/rocm_redist_ubuntu_20_04.bzl +++ b/third_party/gpus/rocm/rocm_redist_ubuntu_20_04.bzl @@ -121,6 +121,10 @@ rocm_redist_ubuntu_20_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocm-llvm-dev/rocm-llvm-dev_18.0.0.24292.60200-66~20.04_amd64.deb", sha256 = "322ca8425c3a8f2ec17c551bad606b96d957b0c1eea07196dd66ac9f15460ed5", ), + struct( + url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocm-smi-lib6.2.0/rocm-smi-lib6.2.0_7.3.0.60200-66~20.04_amd64.deb", + sha256 = "1bbdb32d21dbc12bf9a736f6ca8726df9673e4401465d2b9b537c47b358b67f1", + ), struct( url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocprim-dev6.2.0/rocprim-dev6.2.0_3.2.0.60200-66~20.04_amd64.deb", sha256 = "e74e1907eb90a692344626e881cb88eeed5565ac3b487eb94ad4ac02ffd838ed", @@ -149,6 +153,22 @@ rocm_redist_ubuntu_20_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocsolver-dev6.2.0/rocsolver-dev6.2.0_3.26.0.60200-66~20.04_amd64.deb", sha256 = "21e4aa1957e7bc5d293a418a983d9b3c3917fb78eb79d3d4d55a253b9bae7743", ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm2_2.4.101-2_amd64.deb", + sha256 = "4cd2e10f9486456a2782487f8bfd39f330f35a4d5bd6d693412b9e4ca2a6acbd", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm-amdgpu1_2.4.101-2_amd64.deb", + sha256 = "d4567a30f7d68b4dcf794f8677b96e89083693c94e88279fecf577ceba8b9774", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libelf1_0.176-1.1build1_amd64.deb", + sha256 = "78a8761227efc04a1e37527f2f33ba608c6fb5d6c911616346ada5d7b9b72ee3", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libnuma1_2.0.12-1_amd64.deb", + sha256 = "0b1edf08cf9befecd21fe94e298ac25e476f87fd876ddd4adf42ef713449e637", + ), ], "rocm_root": "opt/rocm-6.2.0", }, diff --git a/third_party/gpus/rocm/rocm_redist_ubuntu_22_04.bzl b/third_party/gpus/rocm/rocm_redist_ubuntu_22_04.bzl index bffd19e44ec009..89cab7a0330ee3 100644 --- a/third_party/gpus/rocm/rocm_redist_ubuntu_22_04.bzl +++ b/third_party/gpus/rocm/rocm_redist_ubuntu_22_04.bzl @@ -125,6 +125,10 @@ rocm_redist_ubuntu_22_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocprim-dev6.2.0/rocprim-dev6.2.0_3.2.0.60200-66~22.04_amd64.deb", sha256 = "3d859bb735ff8bf1962ce680e9257dcc574ab36224f50069f833fa19c6d7e69d", ), + struct( + url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocm-smi-lib6.2.0/rocm-smi-lib6.2.0_7.3.0.60200-66~22.04_amd64.deb", + sha256 = "ffd4e064e8a1d52b9e72114e8a1d51c78004a960f1d923448af8ed07a1b6f30b", + ), struct( url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocprofiler-register6.2.0/rocprofiler-register6.2.0_0.4.0.60200-66~22.04_amd64.deb", sha256 = "66df78d8c5e2d1a0ae43cd4a5e41cf75ec120c870a0bbd7da18a2ba4dec42f9c", @@ -149,6 +153,22 @@ rocm_redist_ubuntu_22_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocsolver-dev6.2.0/rocsolver-dev6.2.0_3.26.0.60200-66~22.04_amd64.deb", sha256 = "4573f99191fbe3a2afab84fdf5a05e024bd230ca7866d7eba71a5f2560a3a0bf", ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm2_2.4.110-1ubuntu1_amd64.deb", + sha256 = "e5ea68db36b31aab442c790e1c78ecdf53646c16b0cd83db15966632ba04152c", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm-amdgpu1_2.4.110-1ubuntu1_amd64.deb", + sha256 = "ae1f0d77668d7275d085ba820206ba91e90833dd1a02b8e251af0c73aa119ba3", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libelf1_0.186-1build1_amd64.deb", + sha256 = "8effc4d7a0cc341bcf6cb11af0134f3defa6292376ecfdfc697a9b228606345c", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libnuma1_2.0.14-3ubuntu2_amd64.deb", + sha256 = "0721c89001fbbd1ada23e89da5d60e762763c1a7b3dc814a2e9a518480a8043d", + ), ], "rocm_root": "opt/rocm-6.2.0", }, diff --git a/third_party/gpus/rocm/rocm_redist_ubuntu_24_04.bzl b/third_party/gpus/rocm/rocm_redist_ubuntu_24_04.bzl index fc2bf916855c67..ca1c750788ead6 100644 --- a/third_party/gpus/rocm/rocm_redist_ubuntu_24_04.bzl +++ b/third_party/gpus/rocm/rocm_redist_ubuntu_24_04.bzl @@ -125,6 +125,10 @@ rocm_redist_ubuntu_24_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocm-llvm-dev/rocm-llvm-dev_18.0.0.24292.60200-66~24.04_amd64.deb", sha256 = "3e85a859c5dafa82a9a57dda096d566b821217bacfac995f7cc45ed460b68999", ), + struct( + url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocm-smi-lib6.2.0/rocm-smi-lib6.2.0_7.3.0.60200-66~24.04_amd64.deb", + sha256 = "c094e3022c73fca2aa6c8bb435f93550109531de37fe8de5fbf6cfe1f047b645", + ), struct( url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocprim-dev6.2.0/rocprim-dev6.2.0_3.2.0.60200-66~24.04_amd64.deb", sha256 = "6c832e2feb0885fbe481245825c76a466921b294f530eb0d0da70a44cfe6e608", @@ -153,6 +157,22 @@ rocm_redist_ubuntu_24_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocsolver-dev6.2.0/rocsolver-dev6.2.0_3.26.0.60200-66~24.04_amd64.deb", sha256 = "5bb6ae92a25f33488f2ee5f123ac4f67ad130e18e4949161715451509be3b89d", ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm2_2.4.120-2build1_amd64.deb", + sha256 = "f5fb4e7ce17921cc466fb7911abf91495ffb181b36772f68e2e82cb621703112", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm-amdgpu1_2.4.120-2build1_amd64.deb", + sha256 = "e149d4daea33f58853b8013fd6c24888429ce7716a4b26d1a1f45181b5a4e73e", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libelf1t64_0.190-1.1build4_amd64.deb", + sha256 = "b277e52769302778bd052376ac6687b52954b6605dd5f781bff8631e3504d58f", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libnuma1_2.0.18-1build1_amd64.deb", + sha256 = "508daa855e99959acaa945e6a89d218e0be6b5727fd28773580942ff37cf5805", + ), ], "rocm_root": "opt/rocm-6.2.0", }, diff --git a/third_party/gpus/rocm_configure.bzl b/third_party/gpus/rocm_configure.bzl index 3477f225b28702..51e2292c650b20 100644 --- a/third_party/gpus/rocm_configure.bzl +++ b/third_party/gpus/rocm_configure.bzl @@ -8,13 +8,12 @@ * `TF_SYSROOT`: The sysroot to use when compiling. * `CLANG_COMPILER_PATH`: The clang compiler path that will be used for host code compilation if TF_ROCM_CLANG is 1. - * `GCC_HOST_COMPILER_PATH`: The GCC host compiler path * `ROCM_PATH`: The path to the ROCm toolkit. Default is `/opt/rocm`. * `TF_ROCM_AMDGPU_TARGETS`: The AMDGPU targets. """ load( - "//third_party/gpus/rocm:rocm_redist.bzl", + "@tsl//third_party/gpus/rocm:rocm_redist.bzl", "rocm_redist", ) load( @@ -57,9 +56,6 @@ _ROCM_VERSION = "ROCM_VERSION" _DEFAULT_ROCM_TOOLKIT_PATH = "/opt/rocm" -def _is_clang_enabled(repository_ctx): - return get_host_environ(repository_ctx, "TF_ROCM_CLANG") == "1" - def verify_build_defines(params): """Verify all variables that crosstool/BUILD.rocm.tpl expects are substituted. @@ -145,6 +141,7 @@ def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp, tf_sysroot): if index1 == -1: return [] index2 = stderr.rfind("\n ") + if index2 == -1 or index2 < index1: return [] index2 = stderr.find("\n", index2 + 1) @@ -228,7 +225,6 @@ def _rocm_include_path(repository_ctx, rocm_config, bash_bin): inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/17/include") inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/18/include") if int(rocm_config.rocm_version_number) >= 60200: - inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/17/include") inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/18/include") inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/19/include") inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/20/include") @@ -290,6 +286,7 @@ def _hipcc_env(repository_ctx): 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): @@ -307,6 +304,7 @@ 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. @@ -319,7 +317,6 @@ def _lib_name(lib, version = "", static = False): else: if version: version = ".%s" % version - return "lib%s.so%s" % (lib, version) def _rocm_lib_paths(repository_ctx, lib, basedir): @@ -336,7 +333,6 @@ def _batch_files_exist(repository_ctx, libs_paths, bash_bin): all_paths = [] for row in libs_paths: lib_paths = row[1] - for lib_path in lib_paths: all_paths.append(lib_path) return files_exist(repository_ctx, all_paths, bash_bin) @@ -410,7 +406,7 @@ def find_rocm_config(repository_ctx, rocm_path): # Parse the dict from stdout. return dict([tuple(x.split(": ")) for x in exec_result.stdout.splitlines()]) -def _get_rocm_config(repository_ctx, bash_bin, rocm_path): +def _get_rocm_config(repository_ctx, bash_bin, rocm_path, install_path): """Detects and returns information about the ROCm installation on the system. Args: @@ -436,6 +432,7 @@ def _get_rocm_config(repository_ctx, bash_bin, rocm_path): rocm_version_number = rocm_version_number, miopen_version_number = miopen_version_number, hipruntime_version_number = hipruntime_version_number, + install_path = install_path, ) def _tpl_path(repository_ctx, labelname): @@ -614,11 +611,12 @@ def _setup_rocm_distro_dir(repository_ctx): repository_ctx.file("rocm/.index") for archive in redist["archives"]: _download_package(repository_ctx, archive) - return _get_rocm_config(repository_ctx, bash_bin, _DISTRIBUTION_PATH + "/" + redist["rocm_root"]) + return _get_rocm_config(repository_ctx, bash_bin, "{}/{}".format(_DISTRIBUTION_PATH, redist["rocm_root"]), "/{}".format(redist["rocm_root"])) else: rocm_path = repository_ctx.os.environ.get(_ROCM_TOOLKIT_PATH, _DEFAULT_ROCM_TOOLKIT_PATH) + repository_ctx.report_progress("Using local rocm installation {}".format(rocm_path)) # buildifier: disable=print repository_ctx.symlink(rocm_path, _DISTRIBUTION_PATH) - return _get_rocm_config(repository_ctx, bash_bin, _DISTRIBUTION_PATH) + return _get_rocm_config(repository_ctx, bash_bin, _DISTRIBUTION_PATH, _DEFAULT_ROCM_TOOLKIT_PATH) def _create_local_rocm_repository(repository_ctx): """Creates the repository containing files set up to build with ROCm.""" @@ -676,6 +674,7 @@ def _create_local_rocm_repository(repository_ctx): repository_dict = { "%{rocm_root}": rocm_toolkit_path, + "%{rocm_toolkit_path}": str(repository_ctx.path(rocm_config.rocm_toolkit_path)), } is_rocm_clang = _use_rocm_clang(repository_ctx) @@ -695,7 +694,6 @@ def _create_local_rocm_repository(repository_ctx): ) # Set up crosstool/ - cc = find_cc(repository_ctx, is_rocm_clang) host_compiler_includes = get_cxx_inc_directories( repository_ctx, @@ -734,10 +732,6 @@ def _create_local_rocm_repository(repository_ctx): "-DUSE_ROCM", ]) - rocm_defines["%{link_flags}"] = to_list_of_strings([ - "-fuse-ld={}".format("lld" if _is_clang_enabled(repository_ctx) else "gold"), - ]) - rocm_defines["%{host_compiler_path}"] = "clang/bin/crosstool_wrapper_driver_is_not_gcc" rocm_defines["%{cxx_builtin_include_directories}"] = to_list_of_strings( @@ -766,6 +760,7 @@ def _create_local_rocm_repository(repository_ctx): tpl_paths["crosstool:clang/bin/crosstool_wrapper_driver_rocm"], { "%{cpu_compiler}": str(cc), + "%{compiler}": rocm_defines["%{compiler}"], "%{hipcc_path}": str(repository_ctx.path(rocm_config.rocm_toolkit_path + "/bin/hipcc")), "%{hipcc_env}": _hipcc_env(repository_ctx), "%{rocm_path}": str(repository_ctx.path(rocm_config.rocm_toolkit_path)), @@ -783,6 +778,25 @@ def _create_local_rocm_repository(repository_ctx): repository_ctx.template( "rocm/rocm_config/rocm_config.h", tpl_paths["rocm:rocm_config.h"], + { + "%{rocm_amdgpu_targets}": ",".join( + ["\"%s\"" % c for c in rocm_config.amdgpu_targets], + ), + "%{rocm_toolkit_path}": rocm_config.install_path, + "%{rocm_version_number}": rocm_config.rocm_version_number, + "%{miopen_version_number}": rocm_config.miopen_version_number, + "%{hipruntime_version_number}": rocm_config.hipruntime_version_number, + "%{hipblaslt_flag}": have_hipblaslt, + "%{hip_soversion_number}": "6" if int(rocm_config.rocm_version_number) >= 60000 else "5", + "%{rocblas_soversion_number}": "4" if int(rocm_config.rocm_version_number) >= 60000 else "3", + }, + ) + + # Set up rocm_config.h, which is used by + # tensorflow/compiler/xla/stream_executor/dso_loader.cc. + repository_ctx.template( + "rocm/rocm_config_hermetic/rocm_config.h", + tpl_paths["rocm:rocm_config.h"], { "%{rocm_amdgpu_targets}": ",".join( ["\"%s\"" % c for c in rocm_config.amdgpu_targets], @@ -863,7 +877,6 @@ _ENVIRONS = [ "TF_NEED_CUDA", # Needed by the `if_gpu_is_configured` macro _ROCM_TOOLKIT_PATH, _TF_ROCM_AMDGPU_TARGETS, - "CLANG_COMPILER_PATH", _OS, _ROCM_VERSION, ] @@ -875,7 +888,7 @@ remote_rocm_configure = repository_rule( attrs = { "environ": attr.string_dict(), "_find_rocm_config": attr.label( - default = Label("//third_party/gpus:find_rocm_config.py"), + default = Label("@tsl//third_party/gpus:find_rocm_config.py"), ), }, ) @@ -885,7 +898,7 @@ rocm_configure = repository_rule( environ = _ENVIRONS + [_TF_ROCM_CONFIG_REPO], attrs = { "_find_rocm_config": attr.label( - default = Label("//third_party/gpus:find_rocm_config.py"), + default = Label("@tsl//third_party/gpus:find_rocm_config.py"), ), }, ) diff --git a/third_party/xla/third_party/tsl/third_party/gpus/rocm/BUILD.tpl b/third_party/xla/third_party/tsl/third_party/gpus/rocm/BUILD.tpl index 1740ae4bf54dc0..e153abcab6c25b 100644 --- a/third_party/xla/third_party/tsl/third_party/gpus/rocm/BUILD.tpl +++ b/third_party/xla/third_party/tsl/third_party/gpus/rocm/BUILD.tpl @@ -1,9 +1,22 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") load("@local_config_rocm//rocm:build_defs.bzl", "rocm_version_number", "select_threshold") licenses(["restricted"]) # MPL2, portions GPL v3, LGPL v3, BSD-like -package(default_visibility = ["//visibility:public"]) +package(default_visibility = ["//visibility:private"]) + +bool_flag( + name = "hermetic", + build_setting_default = False, +) + +config_setting( + name = "build_hermetic", + flag_values = { + ":hermetic": "True", + }, +) config_setting( name = "using_hipcc", @@ -13,13 +26,34 @@ config_setting( ) cc_library( - name = "rocm_config", + name = "config", hdrs = [ "rocm_config/rocm_config.h", ], include_prefix = "rocm", strip_include_prefix = "rocm_config", +) + +cc_library( + name = "config_hermetic", + hdrs = [ + "rocm_config_hermetic/rocm_config.h", + ], + include_prefix = "rocm", + strip_include_prefix = "rocm_config_hermetic", +) + +cc_library( + name = "rocm_config", visibility = ["//visibility:public"], + deps = select({ + ":build_hermetic": [ + ":config_hermetic", + ], + "//conditions:default": [ + "config", + ], + }), ) cc_library( @@ -73,12 +107,35 @@ cc_library( ], linkstatic = 1, strip_include_prefix = "%{rocm_root}", - visibility = ["//visibility:public"], deps = [":rocm_config"], ) +cc_library( + name = "rocm_rpath", + linkopts = select({ + ":build_hermetic": [ + "-Wl,-rpath=%{rocm_toolkit_path}/lib", + "-Wl,-rpath=%{rocm_toolkit_path}/lib", + ], + "//conditions:default": [ + "-Wl,-rpath=/opt/rocm/lib", + ], + }), + visibility = ["//visibility:public"], +) + cc_library( name = "hip", + data = glob(["%{rocm_root}/lib/**"]), + visibility = ["//visibility:public"], + deps = [ + ":rocm_hip", + ":rocm_rpath", + ], +) + +cc_library( + name = "rocm_hip", srcs = glob(["%{rocm_root}/lib/libamdhip*.so*"]), hdrs = glob(["%{rocm_root}/include/hip/**"]), data = glob(["%{rocm_root}/lib/hip/**"]), @@ -86,52 +143,20 @@ cc_library( includes = [ "%{rocm_root}/include", ], - linkstatic = 1, strip_include_prefix = "%{rocm_root}", - visibility = ["//visibility:public"], deps = [ ":amd_comgr", ":hsa_rocr", ":rocm_config", + ":rocm_smi", ":rocprofiler_register", + ":system_libs", ], ) -cc_library( - name = "rocprofiler_register", - srcs = glob([ - "%{rocm_root}/lib/librocprofiler-register.so*", - ]), - include_prefix = "rocm", - includes = [ - "%{rocm_root}/include", - ], - strip_include_prefix = "%{rocm_root}", - visibility = ["//visibility:public"], - deps = [":rocm_config"], -) - -cc_library( - name = "amd_comgr", - srcs = glob([ - "%{rocm_root}/lib/libamd_comgr.so*", - ]), - hdrs = glob(["%{rocm_root}/include/amd_comgr/**"]), - include_prefix = "rocm", - includes = [ - "%{rocm_root}/include", - ], - strip_include_prefix = "%{rocm_root}", - visibility = ["//visibility:public"], - deps = [":rocm_config"], -) - cc_library( name = "rocblas", hdrs = glob(["%{rocm_root}/include/rocblas/**"]), - # workaround to bring tensile files to the same fs layout as expected in the lib - # rocblas assumes that tensile files are located in ../roblas/libraries directory - copts = ["-rpath local_config_rocm/rocm/rocm_dis/lib"], data = glob([ "%{rocm_root}/lib/librocblas*.so*", "%{rocm_root}/lib/rocblas/**", @@ -140,6 +165,9 @@ cc_library( includes = [ "%{rocm_root}/include", ], + # workaround to bring tensile files to the same fs layout as expected in the lib + # rocblas assumes that tensile files are located in ../roblas/libraries directory + linkopts = ["-Wl,-rpath=local_config_rocm/rocm/rocm_dis/lib"], strip_include_prefix = "%{rocm_root}", visibility = ["//visibility:public"], deps = [":rocm_config"], @@ -154,6 +182,7 @@ cc_library( "%{rocm_root}/include", ], linkstatic = 1, + visibility = ["//visibility:public"], deps = [":rocm_config"], ) @@ -188,9 +217,6 @@ cc_library( cc_library( name = "miopen", hdrs = glob(["%{rocm_root}/include/rccl/**"]), - # workaround to bring miopen db files to the same fs layout as expected in the lib - # rocblas assumes that miopen db files are located in ../share/miopen/db directory - copts = ["-rpath local_config_rocm/rocm/rocm_dis/lib"], data = glob([ "%{rocm_root}/lib/libMIOpen*.so*", "%{rocm_root}/share/miopen/**", @@ -199,6 +225,9 @@ cc_library( includes = [ "%{rocm_root}/include", ], + # workaround to bring miopen db files to the same fs layout as expected in the lib + # rocblas assumes that miopen db files are located in ../share/miopen/db directory + linkopts = ["-Wl,-rpath=local_config_rocm/rocm/rocm_dis/lib"], strip_include_prefix = "%{rocm_root}", visibility = ["//visibility:public"], deps = [":rocm_config"], @@ -219,15 +248,10 @@ cc_library( deps = [":rocm_config"], ) -filegroup( - name = "rocm_bin", - srcs = glob(["%{rocm_root}/bin/**/*"]), - visibility = ["//visibility:public"], -) - bzl_library( name = "build_defs_bzl", srcs = ["build_defs.bzl"], + visibility = ["//visibility:public"], ) cc_library( @@ -325,9 +349,6 @@ cc_library( cc_library( name = "hipblaslt", hdrs = glob(["%{rocm_root}/include/hipblaslt/**"]), - # workaround to bring tensile files to the same fs layout as expected in the lib - # hibplatslt assumes that tensile files are located in ../hipblaslt/libraries directory - copts = ["-rpath local_config_rocm/rocm/rocm_dis/lib"], data = glob([ "%{rocm_root}/lib/hipblaslt/**", "%{rocm_root}/lib/libhipblaslt.so*", @@ -336,6 +357,9 @@ cc_library( includes = [ "%{rocm_root}/include/", ], + # workaround to bring tensile files to the same fs layout as expected in the lib + # hibplatslt assumes that tensile files are located in ../hipblaslt/libraries directory + linkopts = ["-Wl,-rpath=local_config_rocm/rocm/rocm_dis/lib"], strip_include_prefix = "%{rocm_root}", visibility = ["//visibility:public"], deps = [":rocm_config"], @@ -354,14 +378,74 @@ cc_library( deps = [":rocm_config"], ) +cc_library( + name = "rocprofiler_register", + srcs = glob([ + "%{rocm_root}/lib/librocprofiler-register.so*", + ]), + include_prefix = "rocm", + includes = [ + "%{rocm_root}/include", + ], + strip_include_prefix = "%{rocm_root}", + deps = [":rocm_config"], +) + +cc_library( + name = "amd_comgr", + srcs = glob([ + "%{rocm_root}/lib/libamd_comgr.so*", + ]), + hdrs = glob(["%{rocm_root}/include/amd_comgr/**"]), + include_prefix = "rocm", + includes = [ + "%{rocm_root}/include", + ], + strip_include_prefix = "%{rocm_root}", + deps = [":rocm_config"], +) + +cc_library( + name = "rocm_smi", + srcs = glob([ + "%{rocm_root}/lib/librocm_smi64.so*", + "%{rocm_root}/lib/libroam.so*", + ]), + hdrs = glob([ + "%{rocm_root}/include/oam/**", + "%{rocm_root}/include/rocm_smi/**", + ]), + include_prefix = "rocm", + includes = [ + "%{rocm_root}/include", + ], + strip_include_prefix = "%{rocm_root}", + deps = [":rocm_config"], +) + +cc_library( + name = "system_libs", + srcs = glob([ + "rocm_dist/usr/lib/**/libelf.so*", + "rocm_dist/usr/lib/**/libdrm.so*", + "rocm_dist/usr/lib/**/libnuma.so*", + "rocm_dist/usr/lib/**/libdrm_amdgpu.so*", + ]), + data = glob([ + "rocm_dist/usr/**", + ]), +) + filegroup( name = "rocm_root", srcs = [ "%{rocm_root}/bin/clang-offload-bundler", ], + visibility = ["//visibility:public"], ) filegroup( name = "all_files", srcs = glob(["%{rocm_root}/**"]), + visibility = ["//visibility:public"], ) diff --git a/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_20_04.bzl b/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_20_04.bzl index 3970c29de1cdcb..fd2d8c363aebb3 100644 --- a/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_20_04.bzl +++ b/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_20_04.bzl @@ -121,6 +121,10 @@ rocm_redist_ubuntu_20_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocm-llvm-dev/rocm-llvm-dev_18.0.0.24292.60200-66~20.04_amd64.deb", sha256 = "322ca8425c3a8f2ec17c551bad606b96d957b0c1eea07196dd66ac9f15460ed5", ), + struct( + url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocm-smi-lib6.2.0/rocm-smi-lib6.2.0_7.3.0.60200-66~20.04_amd64.deb", + sha256 = "1bbdb32d21dbc12bf9a736f6ca8726df9673e4401465d2b9b537c47b358b67f1", + ), struct( url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocprim-dev6.2.0/rocprim-dev6.2.0_3.2.0.60200-66~20.04_amd64.deb", sha256 = "e74e1907eb90a692344626e881cb88eeed5565ac3b487eb94ad4ac02ffd838ed", @@ -149,6 +153,22 @@ rocm_redist_ubuntu_20_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocsolver-dev6.2.0/rocsolver-dev6.2.0_3.26.0.60200-66~20.04_amd64.deb", sha256 = "21e4aa1957e7bc5d293a418a983d9b3c3917fb78eb79d3d4d55a253b9bae7743", ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm2_2.4.101-2_amd64.deb", + sha256 = "4cd2e10f9486456a2782487f8bfd39f330f35a4d5bd6d693412b9e4ca2a6acbd", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm-amdgpu1_2.4.101-2_amd64.deb", + sha256 = "d4567a30f7d68b4dcf794f8677b96e89083693c94e88279fecf577ceba8b9774", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libelf1_0.176-1.1build1_amd64.deb", + sha256 = "78a8761227efc04a1e37527f2f33ba608c6fb5d6c911616346ada5d7b9b72ee3", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libnuma1_2.0.12-1_amd64.deb", + sha256 = "0b1edf08cf9befecd21fe94e298ac25e476f87fd876ddd4adf42ef713449e637", + ), ], "rocm_root": "opt/rocm-6.2.0", }, diff --git a/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_22_04.bzl b/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_22_04.bzl index bffd19e44ec009..89cab7a0330ee3 100644 --- a/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_22_04.bzl +++ b/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_22_04.bzl @@ -125,6 +125,10 @@ rocm_redist_ubuntu_22_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocprim-dev6.2.0/rocprim-dev6.2.0_3.2.0.60200-66~22.04_amd64.deb", sha256 = "3d859bb735ff8bf1962ce680e9257dcc574ab36224f50069f833fa19c6d7e69d", ), + struct( + url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocm-smi-lib6.2.0/rocm-smi-lib6.2.0_7.3.0.60200-66~22.04_amd64.deb", + sha256 = "ffd4e064e8a1d52b9e72114e8a1d51c78004a960f1d923448af8ed07a1b6f30b", + ), struct( url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocprofiler-register6.2.0/rocprofiler-register6.2.0_0.4.0.60200-66~22.04_amd64.deb", sha256 = "66df78d8c5e2d1a0ae43cd4a5e41cf75ec120c870a0bbd7da18a2ba4dec42f9c", @@ -149,6 +153,22 @@ rocm_redist_ubuntu_22_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocsolver-dev6.2.0/rocsolver-dev6.2.0_3.26.0.60200-66~22.04_amd64.deb", sha256 = "4573f99191fbe3a2afab84fdf5a05e024bd230ca7866d7eba71a5f2560a3a0bf", ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm2_2.4.110-1ubuntu1_amd64.deb", + sha256 = "e5ea68db36b31aab442c790e1c78ecdf53646c16b0cd83db15966632ba04152c", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm-amdgpu1_2.4.110-1ubuntu1_amd64.deb", + sha256 = "ae1f0d77668d7275d085ba820206ba91e90833dd1a02b8e251af0c73aa119ba3", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libelf1_0.186-1build1_amd64.deb", + sha256 = "8effc4d7a0cc341bcf6cb11af0134f3defa6292376ecfdfc697a9b228606345c", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libnuma1_2.0.14-3ubuntu2_amd64.deb", + sha256 = "0721c89001fbbd1ada23e89da5d60e762763c1a7b3dc814a2e9a518480a8043d", + ), ], "rocm_root": "opt/rocm-6.2.0", }, diff --git a/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_24_04.bzl b/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_24_04.bzl index fc2bf916855c67..ca1c750788ead6 100644 --- a/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_24_04.bzl +++ b/third_party/xla/third_party/tsl/third_party/gpus/rocm/rocm_redist_ubuntu_24_04.bzl @@ -125,6 +125,10 @@ rocm_redist_ubuntu_24_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocm-llvm-dev/rocm-llvm-dev_18.0.0.24292.60200-66~24.04_amd64.deb", sha256 = "3e85a859c5dafa82a9a57dda096d566b821217bacfac995f7cc45ed460b68999", ), + struct( + url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocm-smi-lib6.2.0/rocm-smi-lib6.2.0_7.3.0.60200-66~24.04_amd64.deb", + sha256 = "c094e3022c73fca2aa6c8bb435f93550109531de37fe8de5fbf6cfe1f047b645", + ), struct( url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocprim-dev6.2.0/rocprim-dev6.2.0_3.2.0.60200-66~24.04_amd64.deb", sha256 = "6c832e2feb0885fbe481245825c76a466921b294f530eb0d0da70a44cfe6e608", @@ -153,6 +157,22 @@ rocm_redist_ubuntu_24_04 = { url = "https://repo.radeon.com/rocm/apt/6.2/pool/main/r/rocsolver-dev6.2.0/rocsolver-dev6.2.0_3.26.0.60200-66~24.04_amd64.deb", sha256 = "5bb6ae92a25f33488f2ee5f123ac4f67ad130e18e4949161715451509be3b89d", ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm2_2.4.120-2build1_amd64.deb", + sha256 = "f5fb4e7ce17921cc466fb7911abf91495ffb181b36772f68e2e82cb621703112", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libdrm-amdgpu1_2.4.120-2build1_amd64.deb", + sha256 = "e149d4daea33f58853b8013fd6c24888429ce7716a4b26d1a1f45181b5a4e73e", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libelf1t64_0.190-1.1build4_amd64.deb", + sha256 = "b277e52769302778bd052376ac6687b52954b6605dd5f781bff8631e3504d58f", + ), + struct( + url = "https://mirror.bazel.build/github.com/alekstheod/rocm-deps/releases/download/rocm-6.2.0/libnuma1_2.0.18-1build1_amd64.deb", + sha256 = "508daa855e99959acaa945e6a89d218e0be6b5727fd28773580942ff37cf5805", + ), ], "rocm_root": "opt/rocm-6.2.0", }, diff --git a/third_party/xla/third_party/tsl/third_party/gpus/rocm_configure.bzl b/third_party/xla/third_party/tsl/third_party/gpus/rocm_configure.bzl index e9fa73a8191c76..51e2292c650b20 100644 --- a/third_party/xla/third_party/tsl/third_party/gpus/rocm_configure.bzl +++ b/third_party/xla/third_party/tsl/third_party/gpus/rocm_configure.bzl @@ -3,7 +3,11 @@ `rocm_configure` depends on the following environment variables: * `TF_NEED_ROCM`: Whether to enable building with ROCm. - * `GCC_HOST_COMPILER_PATH`: The GCC host compiler path + * `GCC_HOST_COMPILER_PATH`: The GCC host compiler path. + * `TF_ROCM_CLANG`: Whether to use clang for C++ and HIPCC for ROCm compilation. + * `TF_SYSROOT`: The sysroot to use when compiling. + * `CLANG_COMPILER_PATH`: The clang compiler path that will be used for + host code compilation if TF_ROCM_CLANG is 1. * `ROCM_PATH`: The path to the ROCm toolkit. Default is `/opt/rocm`. * `TF_ROCM_AMDGPU_TARGETS`: The AMDGPU targets. """ @@ -26,10 +30,13 @@ load( "realpath", "which", ) +load( + ":compiler_common_tools.bzl", + "to_list_of_strings", +) load( ":cuda_configure.bzl", "enable_cuda", - "to_list_of_strings", ) load( ":sycl_configure.bzl", @@ -38,6 +45,8 @@ load( _GCC_HOST_COMPILER_PATH = "GCC_HOST_COMPILER_PATH" _GCC_HOST_COMPILER_PREFIX = "GCC_HOST_COMPILER_PREFIX" +_CLANG_COMPILER_PATH = "CLANG_COMPILER_PATH" +_TF_SYSROOT = "TF_SYSROOT" _ROCM_TOOLKIT_PATH = "ROCM_PATH" _TF_ROCM_AMDGPU_TARGETS = "TF_ROCM_AMDGPU_TARGETS" _TF_ROCM_CONFIG_REPO = "TF_ROCM_CONFIG_REPO" @@ -53,7 +62,6 @@ def verify_build_defines(params): Args: params: dict of variables that will be passed to the BUILD.tpl template. """ - missing = [] for param in [ "cxx_builtin_include_directories", @@ -75,12 +83,15 @@ def verify_build_defines(params): ".", ) -def find_cc(repository_ctx): +def find_cc(repository_ctx, use_rocm_clang): """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 + if use_rocm_clang: + target_cc_name = "clang" + cc_path_envvar = _CLANG_COMPILER_PATH + else: + 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) @@ -102,24 +113,26 @@ def _cxx_inc_convert(path): path = path.strip() return path -def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp): +def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp, tf_sysroot): """Compute the list of default C or C++ include directories.""" if lang_is_cpp: lang = "c++" else: lang = "c" + sysroot = [] + if tf_sysroot: + sysroot += ["--sysroot", tf_sysroot] # TODO: We pass -no-canonical-prefixes here to match the compiler flags, # but in rocm_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", - ]) + ] + sysroot) stderr = err_out(result) index1 = stderr.find(_INC_DIR_MARKER_BEGIN) if index1 == -1: @@ -128,6 +141,7 @@ def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp): if index1 == -1: return [] index2 = stderr.rfind("\n ") + if index2 == -1 or index2 < index1: return [] index2 = stderr.find("\n", index2 + 1) @@ -141,14 +155,24 @@ def _get_cxx_inc_directories_impl(repository_ctx, cc, lang_is_cpp): for p in inc_dirs.split("\n") ] -def get_cxx_inc_directories(repository_ctx, cc): +def get_cxx_inc_directories(repository_ctx, cc, tf_sysroot): """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 = _get_cxx_inc_directories_impl( + repository_ctx, + cc, + True, + tf_sysroot, + ) + includes_c = _get_cxx_inc_directories_impl( + repository_ctx, + cc, + False, + tf_sysroot, + ) includes_cpp_set = depset(includes_cpp) return includes_cpp + [ @@ -179,7 +203,6 @@ def _rocm_include_path(repository_ctx, rocm_config, bash_bin): rocm_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. @@ -199,9 +222,12 @@ def _rocm_include_path(repository_ctx, rocm_config, bash_bin): inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/16.0.0/include") inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/17.0.0/include") inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/17/include") + inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/17/include") inc_dirs.append(rocm_toolkit_path + "/llvm/lib/clang/18/include") if int(rocm_config.rocm_version_number) >= 60200: inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/18/include") + inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/19/include") + inc_dirs.append(rocm_toolkit_path + "/lib/llvm/lib/clang/20/include") # Support hcc based off clang 10.0.0 (for ROCm 3.3) inc_dirs.append(rocm_toolkit_path + "/hcc/compiler/lib/clang/10.0.0/include/") @@ -260,6 +286,7 @@ def _hipcc_env(repository_ctx): 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): @@ -277,6 +304,7 @@ 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. @@ -378,7 +406,7 @@ def find_rocm_config(repository_ctx, rocm_path): # Parse the dict from stdout. return dict([tuple(x.split(": ")) for x in exec_result.stdout.splitlines()]) -def _get_rocm_config(repository_ctx, bash_bin, rocm_path): +def _get_rocm_config(repository_ctx, bash_bin, rocm_path, install_path): """Detects and returns information about the ROCm installation on the system. Args: @@ -404,6 +432,7 @@ def _get_rocm_config(repository_ctx, bash_bin, rocm_path): rocm_version_number = rocm_version_number, miopen_version_number = miopen_version_number, hipruntime_version_number = hipruntime_version_number, + install_path = install_path, ) def _tpl_path(repository_ctx, labelname): @@ -527,8 +556,18 @@ def _genrule(src_dir, genrule_name, command, outs): ")\n" ) +def _flag_enabled(repository_ctx, flag_name): + return get_host_environ(repository_ctx, flag_name) == "1" + +def _use_rocm_clang(repository_ctx): + # Returns the flag if we need to use clang for the host. + return _flag_enabled(repository_ctx, "TF_ROCM_CLANG") + +def _tf_sysroot(repository_ctx): + return get_host_environ(repository_ctx, _TF_SYSROOT, "") + def _compute_rocm_extra_copts(repository_ctx, amdgpu_targets): - amdgpu_target_flags = ["--amdgpu-target=" + + amdgpu_target_flags = ["--offload-arch=" + amdgpu_target for amdgpu_target in amdgpu_targets] return str(amdgpu_target_flags) @@ -572,12 +611,12 @@ def _setup_rocm_distro_dir(repository_ctx): repository_ctx.file("rocm/.index") for archive in redist["archives"]: _download_package(repository_ctx, archive) - return _get_rocm_config(repository_ctx, bash_bin, _DISTRIBUTION_PATH + "/" + redist["rocm_root"]) + return _get_rocm_config(repository_ctx, bash_bin, "{}/{}".format(_DISTRIBUTION_PATH, redist["rocm_root"]), "/{}".format(redist["rocm_root"])) else: rocm_path = repository_ctx.os.environ.get(_ROCM_TOOLKIT_PATH, _DEFAULT_ROCM_TOOLKIT_PATH) repository_ctx.report_progress("Using local rocm installation {}".format(rocm_path)) # buildifier: disable=print repository_ctx.symlink(rocm_path, _DISTRIBUTION_PATH) - return _get_rocm_config(repository_ctx, bash_bin, _DISTRIBUTION_PATH) + return _get_rocm_config(repository_ctx, bash_bin, _DISTRIBUTION_PATH, _DEFAULT_ROCM_TOOLKIT_PATH) def _create_local_rocm_repository(repository_ctx): """Creates the repository containing files set up to build with ROCm.""" @@ -635,7 +674,12 @@ def _create_local_rocm_repository(repository_ctx): repository_dict = { "%{rocm_root}": rocm_toolkit_path, + "%{rocm_toolkit_path}": str(repository_ctx.path(rocm_config.rocm_toolkit_path)), } + + is_rocm_clang = _use_rocm_clang(repository_ctx) + tf_sysroot = _tf_sysroot(repository_ctx) + if rocm_libs["hipblaslt"] != None: repository_dict["%{hipblaslt_lib}"] = rocm_libs["hipblaslt"].file_name @@ -650,25 +694,36 @@ def _create_local_rocm_repository(repository_ctx): ) # Set up crosstool/ + cc = find_cc(repository_ctx, is_rocm_clang) + host_compiler_includes = get_cxx_inc_directories( + repository_ctx, + cc, + tf_sysroot, + ) - 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") + # host_compiler_includes = get_cxx_inc_directories(repository_ctx, cc) rocm_defines = {} - + rocm_defines["%{builtin_sysroot}"] = tf_sysroot + rocm_defines["%{compiler}"] = "unknown" + if is_rocm_clang: + rocm_defines["%{compiler}"] = "clang" + host_compiler_prefix = get_host_environ(repository_ctx, _GCC_HOST_COMPILER_PREFIX, "/usr/bin") rocm_defines["%{host_compiler_prefix}"] = host_compiler_prefix + rocm_defines["%{linker_bin_path}"] = rocm_config.rocm_toolkit_path + host_compiler_prefix + rocm_defines["%{extra_no_canonical_prefixes_flags}"] = "" + rocm_defines["%{unfiltered_compile_flags}"] = "" + rocm_defines["%{rocm_hipcc_files}"] = "[]" - rocm_defines["%{linker_bin_path}"] = rocm_config.rocm_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. - rocm_defines["%{extra_no_canonical_prefixes_flags}"] = "\"-fno-canonical-system-headers\"" + if is_rocm_clang: + rocm_defines["%{extra_no_canonical_prefixes_flags}"] = "\"-no-canonical-prefixes\"" + else: + # 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. + rocm_defines["%{extra_no_canonical_prefixes_flags}"] = "\"-fno-canonical-system-headers\"" rocm_defines["%{unfiltered_compile_flags}"] = to_list_of_strings([ "-DTENSORFLOW_USE_ROCM=1", @@ -705,6 +760,7 @@ def _create_local_rocm_repository(repository_ctx): tpl_paths["crosstool:clang/bin/crosstool_wrapper_driver_rocm"], { "%{cpu_compiler}": str(cc), + "%{compiler}": rocm_defines["%{compiler}"], "%{hipcc_path}": str(repository_ctx.path(rocm_config.rocm_toolkit_path + "/bin/hipcc")), "%{hipcc_env}": _hipcc_env(repository_ctx), "%{rocm_path}": str(repository_ctx.path(rocm_config.rocm_toolkit_path)), @@ -722,6 +778,25 @@ def _create_local_rocm_repository(repository_ctx): repository_ctx.template( "rocm/rocm_config/rocm_config.h", tpl_paths["rocm:rocm_config.h"], + { + "%{rocm_amdgpu_targets}": ",".join( + ["\"%s\"" % c for c in rocm_config.amdgpu_targets], + ), + "%{rocm_toolkit_path}": rocm_config.install_path, + "%{rocm_version_number}": rocm_config.rocm_version_number, + "%{miopen_version_number}": rocm_config.miopen_version_number, + "%{hipruntime_version_number}": rocm_config.hipruntime_version_number, + "%{hipblaslt_flag}": have_hipblaslt, + "%{hip_soversion_number}": "6" if int(rocm_config.rocm_version_number) >= 60000 else "5", + "%{rocblas_soversion_number}": "4" if int(rocm_config.rocm_version_number) >= 60000 else "3", + }, + ) + + # Set up rocm_config.h, which is used by + # tensorflow/compiler/xla/stream_executor/dso_loader.cc. + repository_ctx.template( + "rocm/rocm_config_hermetic/rocm_config.h", + tpl_paths["rocm:rocm_config.h"], { "%{rocm_amdgpu_targets}": ",".join( ["\"%s\"" % c for c in rocm_config.amdgpu_targets], @@ -798,6 +873,7 @@ _ENVIRONS = [ _GCC_HOST_COMPILER_PATH, _GCC_HOST_COMPILER_PREFIX, "TF_NEED_ROCM", + "TF_ROCM_CLANG", "TF_NEED_CUDA", # Needed by the `if_gpu_is_configured` macro _ROCM_TOOLKIT_PATH, _TF_ROCM_AMDGPU_TARGETS, diff --git a/third_party/xla/third_party/tsl/tsl/platform/default/BUILD b/third_party/xla/third_party/tsl/tsl/platform/default/BUILD index e95f87fe1678c2..2dc1c676473cdf 100644 --- a/third_party/xla/third_party/tsl/tsl/platform/default/BUILD +++ b/third_party/xla/third_party/tsl/tsl/platform/default/BUILD @@ -1,5 +1,6 @@ # Tensorflow default + linux implementations of tensorflow/core/platform libraries. load("@bazel_skylib//:bzl_library.bzl", "bzl_library") +load("@local_config_rocm//rocm:build_defs.bzl", "if_rocm_is_configured") load("@local_tsl//tsl/platform:rules_cc.bzl", "cc_library") load( "@local_xla//xla/tsl:tsl.bzl", @@ -101,8 +102,8 @@ cc_library( "@com_google_absl//absl/strings", "@com_google_absl//absl/synchronization", "@local_config_cuda//cuda:cuda_headers", - "@local_config_rocm//rocm:rocm_headers", "@local_config_rocm//rocm:rocm_config", + "@local_config_rocm//rocm:rocm_headers", "@local_config_tensorrt//:tensorrt_headers", ] + if_oss(["@local_config_nccl//:nccl_config"]), ) @@ -253,6 +254,7 @@ cc_library( name = "load_library", srcs = ["load_library.cc"], hdrs = ["//tsl/platform:load_library.h"], + linkstatic = True, tags = [ "manual", "no_oss", @@ -260,7 +262,9 @@ cc_library( ], deps = [ "@com_google_absl//absl/status", - ], + ] + if_rocm_is_configured([ + "@local_config_rocm//rocm:rocm_path", + ]), ) cc_library( @@ -406,8 +410,8 @@ cc_library( "//tsl/platform:logging", "//tsl/platform:path", "//tsl/platform:types", - "@local_config_rocm//rocm:rocm_headers", "@local_config_rocm//rocm:rocm_config", + "@local_config_rocm//rocm:rocm_headers", ], ) diff --git a/third_party/xla/xla/stream_executor/rocm/BUILD b/third_party/xla/xla/stream_executor/rocm/BUILD index f8a839d07f57d2..24fcd86167a703 100644 --- a/third_party/xla/xla/stream_executor/rocm/BUILD +++ b/third_party/xla/xla/stream_executor/rocm/BUILD @@ -931,15 +931,6 @@ cc_library( alwayslink = 1, ) -cc_library( - name = "rocm_rpath", - linkopts = select({ - "//conditions:default": [ - "-Wl,-rpath,../local_config_rocm/rocm/rocm/lib", - ], - }), -) - cc_library( name = "stream_executor_rocm", tags = [ @@ -951,12 +942,12 @@ cc_library( ]), deps = [ ":rocm_platform_id", - ":rocm_rpath", "//xla/stream_executor:dnn", "//xla/stream_executor:platform_manager", "//xla/stream_executor:scratch_allocator", "//xla/stream_executor/cuda:cuda_platform_id", "//xla/stream_executor/host:host_platform_id", + "@local_config_rocm//rocm:rocm_rpath", ] + if_static( [":all_runtime"], ),