load("@local_config_cuda//cuda:build_defs.bzl", "if_cuda")
load("@local_config_rocm//rocm:build_defs.bzl", "if_rocm")
load("@local_config_sycl//sycl:build_defs.bzl", "if_sycl")
load("//xla:xla.default.bzl", "xla_cc_test")
load("//xla/pjrt/gpu:package_groups.bzl", "xla_gpu_internal_packages")
load("//xla/stream_executor:build_defs.bzl", "if_cuda_or_rocm")
load("//xla/tests:build_defs.bzl", "xla_test")
load("//xla/tsl:tsl.bzl", "if_google", "internal_visibility")
load("//xla/tsl/platform:rules_cc.bzl", "cc_library")

# Integrate with PJRT rather than the GPU client directly.
package(
    # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
    default_visibility = [":__subpackages__"],
    licenses = ["notice"],
)

xla_gpu_internal_packages()

cc_library(
    name = "gpu_helpers",
    srcs = ["gpu_helpers.cc"],
    hdrs = ["gpu_helpers.h"],
    visibility = internal_visibility(["//xla/pjrt:friends"]),
    deps = [
        "//xla:types",
        "//xla:util",
        "//xla/client:client_library",
        "//xla/client:local_client",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_allocator_config",
        "//xla/service:platform_util",
        "//xla/stream_executor:platform",
        "//xla/stream_executor:stream_executor_h",
        "//xla/stream_executor/integrations:device_mem_allocator",
        "//xla/stream_executor/integrations:stream_executor_allocator",
        "//xla/tsl/framework:allocator",
        "//xla/tsl/framework:bfc_allocator",
        "//xla/tsl/framework:device_id_impl",
        "//xla/tsl/platform:errors",
        "//xla/tsl/platform:status_macros",
        "//xla/tsl/platform:statusor",
        "//xla/tsl/util:env_var",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_absl//absl/types:span",
        "@tsl//tsl/platform:numbers",
    ],
)

cc_library(
    name = "se_gpu_pjrt_client",
    srcs = ["se_gpu_pjrt_client.cc"],
    hdrs = ["se_gpu_pjrt_client.h"],
    defines = if_cuda(["GOOGLE_CUDA=1"]) + if_rocm(["TENSORFLOW_USE_ROCM=1"]) + if_sycl([
        "TENSORFLOW_USE_SYCL=1",
    ]),
    visibility = internal_visibility(["//xla/pjrt/gpu:legacy_gpu_client_users"]),
    deps = [
        ":gpu_helpers",
        ":gpu_metrics",
        ":se_gpu_pjrt_runtime_abi_version",
        ":se_gpu_topology_description",
        "//xla:executable_run_options",
        "//xla:future",
        "//xla:literal",
        "//xla:shape_tree",
        "//xla:shape_util",
        "//xla:status_macros",
        "//xla:util",
        "//xla:xla_data_proto_cc",
        "//xla:xla_proto_cc",
        "//xla/backends/cpu:target_machine_options",
        "//xla/backends/gpu/collectives:allocator_memory_registration",
        "//xla/backends/gpu/collectives:gpu_clique",
        "//xla/backends/gpu/collectives:gpu_clique_key",
        "//xla/backends/gpu/collectives:gpu_cliques",
        "//xla/backends/gpu/collectives:gpu_collectives",
        "//xla/backends/gpu/collectives:gpu_communicator",
        "//xla/backends/gpu/target_config",
        "//xla/client:client_library",
        "//xla/client:local_client",
        "//xla/core/collectives",
        "//xla/core/collectives:clique_id",
        "//xla/core/collectives:collectives_registry",
        "//xla/core/collectives:communicator",
        "//xla/core/collectives:rank_id",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/ir:hlo",
        "//xla/pjrt:async_work_runner",
        "//xla/pjrt:common_pjrt_client",
        "//xla/pjrt:device_event",
        "//xla/pjrt:host_memory_allocator",
        "//xla/pjrt:host_memory_spaces",
        "//xla/pjrt:maybe_owning_mlir_module",
        "//xla/pjrt:mlir_to_hlo",
        "//xla/pjrt:pjrt_abi_version",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_common",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_device_description",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt:raw_buffer",
        "//xla/pjrt:utils",
        "//xla/pjrt:worker_thread",
        "//xla/pjrt/distributed:client",
        "//xla/pjrt/distributed:in_memory_key_value_store",
        "//xla/pjrt/distributed:key_value_store_interface",
        "//xla/pjrt/distributed:protocol_proto_cc",
        "//xla/pjrt/distributed:topology_util",
        "//xla/pjrt/distributed/coordination:coordination_service_proto_cc",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_allocator_config",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_client_options",
        "//xla/pjrt/proto:compile_options_proto_cc",
        "//xla/pjrt/se:event_pool",
        "//xla/pjrt/se:local_device_state",
        "//xla/pjrt/se:pjrt_stream_executor_client",
        "//xla/pjrt/se:stream_executor_executable",
        "//xla/pjrt/se:stream_executor_executable_proto_cc",
        "//xla/pjrt/se:stream_executor_pjrt_abi_version",
        "//xla/runtime:device_id",
        "//xla/runtime:hang_watchdog",
        "//xla/runtime:process_id",
        "//xla/service:buffer_assignment",
        "//xla/service:compiler",
        "//xla/service:computation_placer_hdr",
        "//xla/service:executable",
        "//xla/service:gpu_topology",
        "//xla/service:gpu_topology_proto_cc",
        "//xla/service:pjrt_gpu_utils",
        "//xla/service:platform_util",
        "//xla/service:shaped_buffer",
        "//xla/service:transfer_manager",
        "//xla/service/gpu:buffer_allocations",
        "//xla/service/gpu:gpu_constants",
        "//xla/service/gpu:gpu_executable_run_options",
        "//xla/service/gpu:gpu_memory_space_assignment",
        "//xla/stream_executor:device_address",
        "//xla/stream_executor:device_address_allocator",
        "//xla/stream_executor:device_address_vmm_allocator",
        "//xla/stream_executor:device_description",
        "//xla/stream_executor:device_description_proto_cc",
        "//xla/stream_executor:device_interconnect_resource",
        "//xla/stream_executor:memory_space",
        "//xla/stream_executor:platform",
        "//xla/stream_executor:stream",
        "//xla/stream_executor:stream_executor_address_allocator",
        "//xla/stream_executor:stream_executor_h",
        "//xla/stream_executor/integrations:device_mem_allocator",
        "//xla/stream_executor/integrations:tf_allocator_adapter",
        "//xla/tsl/concurrency:async_value",
        "//xla/tsl/concurrency:ref_count",
        "//xla/tsl/distributed_runtime/coordination:coordination_service",
        "//xla/tsl/framework:allocator",
        "//xla/tsl/framework:bfc_allocator",
        "//xla/tsl/framework:device_id",
        "//xla/tsl/framework:device_id_impl",
        "//xla/tsl/framework:scoped_allocation_trace",
        "//xla/tsl/lib/strings:proto_serialization",
        "//xla/tsl/platform:env",
        "//xla/tsl/platform:errors",
        "//xla/tsl/platform:status",
        "//xla/tsl/platform:status_macros",
        "//xla/tsl/platform:statusor",
        "//xla/tsl/protobuf:coordination_service_proto_cc",
        "//xla/tsl/util:env_var",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:inlined_vector",
        "@com_google_absl//absl/functional:any_invocable",
        "@com_google_absl//absl/functional:bind_front",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
        "@llvm-project//mlir:IR",
        "@tsl//tsl/platform:casts",
        "@tsl//tsl/platform:env",
        "@tsl//tsl/platform:errors",
        "@tsl//tsl/platform:fingerprint",
        "@tsl//tsl/platform:platform_port",
        "@tsl//tsl/platform:protobuf",
        "@tsl//tsl/platform:status",
        "@tsl//tsl/platform:statusor",
        "@tsl//tsl/profiler/lib:connected_traceme",
        "@tsl//tsl/profiler/lib:nvtx_utils",
        "@tsl//tsl/profiler/lib:traceme",
    ] + if_cuda_or_rocm([
        # keep sorted
        "//xla:debug_options_flags",
        "//xla/service/gpu:gpu_compiler",
        "//xla/service/gpu:gpu_executable",
        "//xla/service/gpu:gpu_executable_buffer_allocator",
        "//xla/service/gpu:stream_executor_util",
    ]) + if_cuda([
        # keep sorted
        "//xla/stream_executor/cuda:cuda_compute_capability",
        "//xla/stream_executor/cuda:cuda_device_address_vmm_allocator",
        "//xla/stream_executor/gpu:gpu_cudamallocasync_allocator",
        "@local_config_cuda//cuda:cuda_headers",
    ]) + if_rocm([
        # keep sorted
        "//xla/stream_executor/rocm:rocm_device_address_vmm_allocator",
        "@local_config_rocm//rocm:rocm_headers",
    ]) + if_sycl([
        # keep sorted
        "//xla:debug_options_flags",
        "//xla/service/gpu:gpu_compiler",
        "//xla/service/gpu:gpu_executable",
        "//xla/service/gpu:gpu_executable_buffer_allocator",
        "//xla/service/gpu:stream_executor_util",
        "@local_config_sycl//sycl:sycl_headers",
    ]),
)

cc_library(
    name = "se_gpu_pjrt_client_test_lib",
    testonly = 1,
    srcs = ["se_gpu_pjrt_client_test_helper.cc"],
    hdrs = ["se_gpu_pjrt_client_test_helper.h"],
    deps = [
        "//xla:future",
        "//xla:literal",
        "//xla:shape_util",
        "//xla:status_macros",
        "//xla:util",
        "//xla:xla_data_proto_cc",
        "//xla:xla_proto_cc",
        "//xla/backends/gpu:ffi",
        "//xla/ffi",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/parser:hlo_parser",
        "//xla/hlo/testlib:test",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt:raw_buffer",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_client_options",
        "//xla/pjrt/proto:compile_options_proto_cc",
        "//xla/service:gpu_topology_proto_cc",
        "//xla/service:platform_util",
        "//xla/stream_executor:device_address",
        "//xla/stream_executor:stream",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_absl//absl/types:span",
        "@com_google_googletest//:gtest_for_library",
    ],
    alwayslink = 1,
)

xla_test(
    name = "pjrt_client_test_gpu",
    srcs = ["pjrt_client_test_gpu.cc"],
    backends = ["gpu"],
    tags = [
        "multi_gpu",
        "no_oss",
    ],
    deps = [
        ":se_gpu_pjrt_client",
        ":se_gpu_pjrt_client_test_lib",
        "//xla/pjrt:pjrt_client_test_common",
        "//xla/tsl/platform:test_main",
    ],
)

xla_test(
    name = "se_gpu_pjrt_client_test",
    srcs = ["se_gpu_pjrt_client_test.cc"],
    backends = ["gpu"],
    shard_count = 3,
    tags = ["nofixdeps"],
    use_legacy_runtime = True,
    deps = [
        ":se_gpu_pjrt_client",
        ":se_gpu_pjrt_client_test_lib",
        ":se_gpu_topology_description",
        "//xla:debug_options_flags",
        "//xla:future",
        "//xla:literal",
        "//xla:literal_util",
        "//xla:parse_flags_from_env",
        "//xla:shape_util",
        "//xla:types",
        "//xla:util",
        "//xla:xla_data_proto_cc",
        "//xla:xla_proto_cc",
        "//xla/ffi",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/parser:hlo_parser",
        "//xla/hlo/testlib:test",
        "//xla/pjrt:device_event",
        "//xla/pjrt:host_memory_spaces",
        "//xla/pjrt:maybe_owning_mlir_module",
        "//xla/pjrt:mlir_to_hlo",
        "//xla/pjrt:pjrt_abi_version",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_device_description",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt:raw_buffer",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_allocator_config",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_client_options",
        "//xla/pjrt/profiling:device_time_measurement",
        "//xla/pjrt/proto:compile_options_proto_cc",
        "//xla/pjrt/se:local_device_state",
        "//xla/pjrt/se:pjrt_stream_executor_client",
        "//xla/runtime:device_id",
        "//xla/service:gpu_topology",
        "//xla/service:gpu_topology_proto_cc",
        "//xla/service:platform_util",
        "//xla/stream_executor:stream_executor_address_allocator",
        "//xla/stream_executor/integrations:tf_allocator_adapter",
        "//xla/tests:literal_test_util",
        "//xla/tsl/concurrency:async_value",
        "//xla/tsl/concurrency:ref_count",
        "//xla/tsl/lib/core:status_test_util",
        "//xla/tsl/platform:env",
        "//xla/tsl/platform:errors",
        "//xla/tsl/platform:statusor",
        "//xla/util/split_proto:split_executable_and_options_writer",
        "//xla/util/split_proto:split_proto_reader",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base:log_severity",
        "@com_google_absl//absl/cleanup",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/log:globals",
        "@com_google_absl//absl/log:scoped_mock_log",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:status_matchers",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
        "@com_google_googletest//:gtest_main",
        "@com_google_protobuf//:protobuf",
        "@llvm-project//mlir:IR",
        "@riegeli//riegeli/bytes:string_reader",
        "@riegeli//riegeli/bytes:string_writer",
        "@tsl//tsl/platform",
        "@tsl//tsl/platform:casts",
        "@tsl//tsl/platform:platform_port",
    ] + if_cuda([
        "//xla/stream_executor/cuda:cuda_compute_capability",
        "//xla/stream_executor/cuda:cuda_device_address_vmm_allocator",
    ]) + if_rocm([
        "//xla/stream_executor/rocm:rocm_device_address_vmm_allocator",
    ]) + if_google([
        # In OSS this dependency is added automatically for xla_test targets. See
        # third_party/tensorflow/compiler/xla/xla.default.bzl.
        "//xla/tsl/framework:allocator",
    ]),
)

xla_test(
    name = "se_gpu_pjrt_client_multi_gpu_test",
    srcs = ["se_gpu_pjrt_client_multi_gpu_test.cc"],
    backends = ["gpu"],
    shard_count = 3,
    tags = [
        "multi_gpu",
        # Takes to long to run and doesn't require 8x GPUs setup.
        "no_oss",
        "nofixdeps",
    ],
    use_legacy_runtime = True,
    deps = [
        ":se_gpu_pjrt_client",
        ":se_gpu_pjrt_client_test_lib",
        ":se_gpu_topology_description",
        "//xla:debug_options_flags",
        "//xla:future",
        "//xla:literal",
        "//xla:literal_util",
        "//xla:shape_util",
        "//xla:status_macros",
        "//xla:types",
        "//xla:util",
        "//xla:xla_data_proto_cc",
        "//xla:xla_proto_cc",
        "//xla/backends/gpu:ffi",
        "//xla/ffi",
        "//xla/ffi:ffi_api",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/parser:hlo_parser",
        "//xla/hlo/testlib:test",
        "//xla/pjrt:common_pjrt_client",
        "//xla/pjrt:device_event",
        "//xla/pjrt:host_memory_spaces",
        "//xla/pjrt:maybe_owning_mlir_module",
        "//xla/pjrt:mlir_to_hlo",
        "//xla/pjrt:pjrt_abi_version",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_common",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_device_description",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt:raw_buffer",
        "//xla/pjrt/distributed",
        "//xla/pjrt/distributed:client",
        "//xla/pjrt/distributed:in_memory_key_value_store",
        "//xla/pjrt/distributed:service",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_client_options",
        "//xla/pjrt/profiling:device_time_measurement",
        "//xla/pjrt/proto:compile_options_proto_cc",
        "//xla/pjrt/se:local_device_state",
        "//xla/pjrt/se:pjrt_stream_executor_client",
        "//xla/runtime:device_id",
        "//xla/service:gpu_topology",
        "//xla/service:gpu_topology_proto_cc",
        "//xla/service:platform_util",
        "//xla/service/gpu:gpu_memory_space_assignment",
        "//xla/stream_executor:device_address",
        "//xla/stream_executor:stream",
        "//xla/tests:literal_test_util",
        "//xla/tsl/concurrency:async_value",
        "//xla/tsl/concurrency:ref_count",
        "//xla/tsl/lib/core:status_test_util",
        "//xla/tsl/platform:env",
        "//xla/tsl/platform:errors",
        "//xla/tsl/platform:status",
        "//xla/tsl/platform:status_macros",
        "//xla/tsl/platform:statusor",
        "//xla/tsl/platform:subprocess",
        "//xla/tsl/util:command_line_flags",
        "//xla/util/split_proto:split_executable_and_options_writer",
        "//xla/util/split_proto:split_proto_reader",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/base:log_severity",
        "@com_google_absl//absl/cleanup",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/log:globals",
        "@com_google_absl//absl/log:scoped_mock_log",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:status_matchers",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
        "@com_google_googletest//:gtest",
        "@com_google_protobuf//:protobuf",
        "@llvm-project//mlir:IR",
        "@riegeli//riegeli/bytes:reader",
        "@riegeli//riegeli/bytes:string_reader",
        "@riegeli//riegeli/bytes:string_writer",
        "@tsl//tsl/platform",
        "@tsl//tsl/platform:casts",
        "@tsl//tsl/platform:platform_port",
    ] + if_cuda([
        "//xla/stream_executor/cuda:cuda_compute_capability",
        "//xla/stream_executor/cuda:cuda_device_address_vmm_allocator",
    ]) + if_google([
        # In OSS this dependency is added automatically for xla_test targets. See
        # third_party/tensorflow/compiler/xla/xla.default.bzl.
        "//xla/tsl/framework:allocator",
    ]),
)

xla_test(
    name = "se_gpu_pjrt_client_benchmark_test",
    srcs = ["se_gpu_pjrt_client_benchmark_test.cc"],
    backends = ["gpu"],
    deps = [
        ":se_gpu_pjrt_client",
        "//xla:xla_data_proto_cc",
        "//xla/pjrt:maybe_owning_mlir_module",
        "//xla/pjrt:mlir_to_hlo",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_allocator_config",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_client_options",
        "//xla/tsl/platform:test",
        "//xla/tsl/platform:test_benchmark",
        "//xla/tsl/platform:test_main",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/strings:string_view",
        "@llvm-project//mlir:IR",
    ],
)

cc_library(
    name = "se_gpu_pjrt_compiler_impl",
    srcs = ["se_gpu_pjrt_compiler.cc"],
    hdrs = ["se_gpu_pjrt_compiler.h"],
    deps = [
        ":se_gpu_pjrt_client",
        ":se_gpu_pjrt_runtime_abi_version",
        ":se_gpu_topology_description",
        "//xla:shape_util",
        "//xla:status_macros",
        "//xla/backends/gpu/target_config",
        "//xla/client:executable_build_options",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/ir:hlo",
        "//xla/mlir_hlo:mhlo_passes",
        "//xla/pjrt:layout_mode",
        "//xla/pjrt:maybe_owning_mlir_module",
        "//xla/pjrt:mlir_to_hlo",
        "//xla/pjrt:pjrt_abi_version",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_common",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt:utils",
        "//xla/pjrt/se:stream_executor_executable",
        "//xla/service:compiled_module",
        "//xla/service:compiler",
        "//xla/service:dump",
        "//xla/service:gpu_topology",
        "//xla/service:hlo_module_config",
        "//xla/service:hlo_module_util",
        "//xla/service:hlo_proto_cc",
        "//xla/service:local_service_utils",
        "//xla/service:platform_util",
        "//xla/stream_executor:platform",
        "//xla/stream_executor:platform_id",
        "//xla/stream_executor:platform_manager",
        "//xla/stream_executor/abi:runtime_abi_version",
        "//xla/tsl/platform:errors",
        "//xla/tsl/platform:status_macros",
        "//xla/tsl/platform:statusor",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_absl//absl/synchronization",
    ],
)

cc_library(
    name = "se_gpu_pjrt_compiler_cuda_registration",
    srcs = ["se_gpu_pjrt_compiler_cuda_registration.cc"],
    compatible_with = [],
    tags = [
        "cuda-only",
        "gpu",
    ],
    deps = [
        ":se_gpu_pjrt_compiler_impl",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt/se:stream_executor_platform_id_mapping",
        "//xla/service/gpu:nvptx_compiler",  # buildcleaner: keep
        "//xla/stream_executor/cuda:cuda_platform_id",
        "//xla/stream_executor/platform:initialize",
        "@com_google_absl//absl/log:check",
    ],
    alwayslink = 1,
)

cc_library(
    name = "se_gpu_pjrt_compiler_rocm_registration",
    srcs = ["se_gpu_pjrt_compiler_rocm_registration.cc"],
    compatible_with = [],
    tags = [
        "gpu",
        "rocm-only",
    ],
    deps = [
        ":se_gpu_pjrt_compiler_impl",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt/se:stream_executor_platform_id_mapping",
        "//xla/service/gpu:amdgpu_compiler",  # buildcleaner: keep
        "//xla/stream_executor/platform:initialize",
        "//xla/stream_executor/rocm:rocm_platform_id",
        "@com_google_absl//absl/log:check",
    ],
    alwayslink = 1,
)

cc_library(
    name = "se_gpu_pjrt_compiler_sycl_registration",
    srcs = ["se_gpu_pjrt_compiler_sycl_registration.cc"],
    compatible_with = [],
    tags = [
        "gpu",
        "oneapi-only",
    ] + if_google([
        # TODO(b/456585142): Currently we don't support building the SYCL backend.
        "notap",
        "nobuilder",
        "manual",
    ]),
    deps = [
        ":se_gpu_pjrt_compiler_impl",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt/se:stream_executor_platform_id_mapping",
        "//xla/service/gpu:intel_gpu_compiler",
        "//xla/stream_executor/platform:initialize",
        "//xla/stream_executor/sycl:sycl_platform_id",
        "@com_google_absl//absl/log:check",
    ],
    alwayslink = 1,
)

cc_library(
    name = "se_gpu_pjrt_compiler",
    hdrs = ["se_gpu_pjrt_compiler.h"],
    # TODO(b/382070262): Migrate users off this target - They should go though the PjRt ABI compatibility layer.
    visibility = internal_visibility(["//xla/pjrt/gpu:legacy_se_gpu_pjrt_compiler_users"]),
    deps = [
        ":se_gpu_pjrt_compiler_impl",
        "//xla:shape_util",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/ir:hlo",
        "//xla/pjrt:maybe_owning_mlir_module",
        "//xla/pjrt:pjrt_abi_version",
        "//xla/pjrt:pjrt_common",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_executable",
        "//xla/service:compiler",
        "//xla/stream_executor:platform",
        "//xla/stream_executor:platform_id",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/synchronization",
        "@llvm-project//mlir:IR",
    ] + if_cuda([
        ":se_gpu_pjrt_compiler_cuda_registration",
    ]) + if_rocm([
        ":se_gpu_pjrt_compiler_rocm_registration",
    ]) + if_sycl([
        ":se_gpu_pjrt_compiler_sycl_registration",
    ]),
)

cc_library(
    name = "gpu_metrics",
    srcs = ["gpu_metrics.cc"],
    hdrs = ["gpu_metrics.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//xla/tsl/lib/monitoring:gauge",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:string_view",
    ],
)

xla_test(
    name = "se_gpu_pjrt_compiler_test",
    srcs = ["se_gpu_pjrt_compiler_test.cc"],
    backends = ["gpu"],
    deps = [
        ":se_gpu_pjrt_client",
        ":se_gpu_pjrt_compiler_impl",
        ":se_gpu_topology_description",
        "//xla:literal",
        "//xla:literal_util",
        "//xla:shape_layout",
        "//xla/backends/cpu:target_machine_options",
        "//xla/backends/gpu/target_config",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/ir:hlo",
        "//xla/hlo/parser:hlo_parser",
        "//xla/hlo/testlib:test",
        "//xla/mlir_hlo",
        "//xla/pjrt:maybe_owning_mlir_module",
        "//xla/pjrt:mock_pjrt_client",
        "//xla/pjrt:pjrt_abi_version",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_common",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_client_options",
        "//xla/service:compiled_module",
        "//xla/service:compiler",
        "//xla/service:gpu_topology",
        "//xla/service:mock_compiler",
        "//xla/service:platform_util",
        "//xla/stream_executor:stream_executor_h",
        "//xla/stream_executor/cuda:cuda_platform_id",
        "//xla/tests:literal_test_util",
        "//xla/tsl/platform:status_macros",
        "//xla/tsl/platform:statusor",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:status_matchers",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:Parser",
        "@stablehlo//:stablehlo_ops",
    ],
)

xla_test(
    name = "se_gpu_pjrt_compiler_aot_test",
    srcs = ["se_gpu_pjrt_compiler_aot_test.cc"],
    backends = ["gpu"],
    deps = [
        ":se_gpu_pjrt_client",
        ":se_gpu_pjrt_compiler_impl",
        "//xla:literal",
        "//xla:literal_util",
        "//xla:shape_util",
        "//xla:xla_data_proto_cc",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/ir:hlo",
        "//xla/hlo/parser:hlo_parser",
        "//xla/mlir_hlo",
        "//xla/pjrt:compiled_memory_stats",
        "//xla/pjrt:maybe_owning_mlir_module",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_client_options",
        "//xla/service:compiler",
        "//xla/tests:literal_test_util",
        "//xla/tsl/platform:status_macros",
        "//xla/tsl/platform:statusor",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status:status_matchers",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:Parser",
        "@tsl//tsl/platform:casts",
    ],
)

cc_library(
    name = "se_gpu_topology_description",
    srcs = ["se_gpu_topology_description.cc"],
    hdrs = ["se_gpu_topology_description.h"],
    visibility = internal_visibility([
        "//xla/pjrt/gpu:legacy_gpu_topology_users",
        ":__subpackages__",
    ]),
    deps = [
        "//xla:shape_util",
        "//xla:util",
        "//xla:xla_data_proto_cc",
        "//xla/pjrt:pjrt_common",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_device_description",
        "//xla/pjrt:pjrt_device_dimensions",
        "//xla/pjrt/proto:topology_description_proto_cc",
        "//xla/pjrt/se:pjrt_stream_executor_device_description",
        "//xla/runtime:device_id",
        "//xla/service:gpu_topology",
        "//xla/service:gpu_topology_proto_cc",
        "//xla/stream_executor:device_description_proto_cc",
        "//xla/tsl/lib/strings:proto_serialization",
        "//xla/tsl/platform:status_macros",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_absl//absl/types:span",
        "@tsl//tsl/platform:fingerprint",
    ],
)

xla_cc_test(
    name = "se_gpu_topology_description_test",
    srcs = ["se_gpu_topology_description_test.cc"],
    deps = [
        ":se_gpu_topology_description",
        "//xla/pjrt:pjrt_common",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_device_description",
        "//xla/pjrt:pjrt_device_dimensions",
        "//xla/pjrt/se:pjrt_stream_executor_device_description",
        "//xla/service:gpu_topology",
        "//xla/tsl/platform:statusor",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "se_gpu_pjrt_runtime_abi_version",
    srcs = ["se_gpu_pjrt_runtime_abi_version.cc"],
    hdrs = ["se_gpu_pjrt_runtime_abi_version.h"],
    visibility = internal_visibility(["//xla:internal"]),
    deps = [
        "//xla/pjrt:pjrt_abi_version",
        "//xla/pjrt:pjrt_common",
        "//xla/pjrt/proto:pjrt_abi_version_proto_cc",
        "//xla/pjrt/se:stream_executor_pjrt_abi_version",
        "//xla/stream_executor/abi:runtime_abi_version",
        "//xla/stream_executor/abi:runtime_abi_version_proto_cc",
        "//xla/stream_executor/abi:runtime_abi_version_resolver",
        "//xla/tsl/platform:status_macros",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
    ],
)

xla_cc_test(
    name = "se_gpu_pjrt_runtime_abi_version_test",
    srcs = ["se_gpu_pjrt_runtime_abi_version_test.cc"],
    deps = [
        ":se_gpu_pjrt_runtime_abi_version",
        "//xla/pjrt:pjrt_common",
        "//xla/pjrt/proto:pjrt_abi_version_proto_cc",
        "//xla/pjrt/se:stream_executor_pjrt_abi_version",
        "//xla/stream_executor/abi:executable_abi_version",
        "//xla/stream_executor/abi:executable_abi_version_proto_cc",
        "//xla/stream_executor/abi:mock_runtime_abi_version",
        "//xla/stream_executor/abi:runtime_abi_version_proto_cc",
        "//xla/tsl/util/proto:proto_matchers",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:status_matchers",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "abi_helpers",
    srcs = ["abi_helpers.cc"],
    hdrs = ["abi_helpers.h"],
    visibility = internal_visibility(["//xla/python/pjrt_ifrt:__subpackages__"]),
    deps = [
        "//xla/pjrt:pjrt_abi_version",
        "//xla/pjrt:pjrt_api",
        "//xla/pjrt/c:pjrt_c_api_abi_version_helpers",
        "//xla/pjrt/c:pjrt_c_api_hdrs",
        "//xla/pjrt/plugin:plugin_names",
        "//xla/pjrt/proto:pjrt_abi_version_proto_cc",
        "//xla/tsl/platform:status_macros",
        "@com_google_absl//absl/status:statusor",
    ],
)

xla_test(
    name = "gpu_client_stream_error_test",
    srcs = ["gpu_client_stream_error_test.cc"],
    backends = [
        "nvgpu_any",
    ],
    # Each test case is a separate process since cuda stream errors are sticky.
    shard_count = 4,
    tags = [
        "gpu",
    ],
    deps = [
        "//xla/backends/gpu:ffi",
        "//xla/ffi",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/ir:hlo",
        "//xla/hlo/parser:hlo_parser",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_client_options",
        "//xla/pjrt/plugin/xla_gpu:xla_gpu_pjrt_client",
        "//xla/service:hlo_module_config",
        "//xla/service/gpu:launch_dimensions",
        "//xla/service/gpu:stream_executor_util",
        "//xla/stream_executor:kernel",
        "//xla/stream_executor:stream",
        "//xla/tsl/lib/core:status_test_util",
        "//xla/tsl/platform:status_macros",
        "//xla/tsl/platform:statusor",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_googletest//:gtest_main",
    ],
)
