Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move StreamExecutorMemoryAllocator to its own header & implementation file. #68475

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions third_party/xla/xla/service/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ cc_library(
"//xla/stream_executor",
"//xla/stream_executor:device_memory_allocator",
"//xla/stream_executor:stream_executor_interface",
"//xla/stream_executor:stream_executor_memory_allocator",
"//xla/stream_executor/host:host_platform_id",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
Expand Down Expand Up @@ -1457,6 +1458,7 @@ xla_cc_test(
"//xla:test",
"//xla/stream_executor:device_memory_allocator",
"//xla/stream_executor:stream_executor_interface",
"//xla/stream_executor:stream_executor_memory_allocator",
"//xla/tests:xla_internal_test_main",
"@local_tsl//tsl/platform:test_benchmark",
],
Expand Down Expand Up @@ -4095,6 +4097,7 @@ xla_cc_test(
"//xla:types",
"//xla/stream_executor",
"//xla/stream_executor:platform_manager",
"//xla/stream_executor:stream_executor_memory_allocator",
"//xla/stream_executor/host:host_platform",
"//xla/stream_executor/host:host_platform_id",
"//xla/tests:literal_test_util",
Expand Down Expand Up @@ -5753,6 +5756,7 @@ cc_library(
"//xla/hlo/ir:hlo",
"//xla/hlo/ir:hlo_module_group",
"//xla/stream_executor",
"//xla/stream_executor:stream_executor_memory_allocator",
"@com_google_absl//absl/types:span",
"@eigen_archive//:eigen3",
"@local_tsl//tsl/platform:blocking_counter",
Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/service/backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ limitations under the License.
#include "xla/stream_executor/host/host_platform_id.h"
#include "xla/stream_executor/stream_executor.h"
#include "xla/stream_executor/stream_executor_interface.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "xla/util.h"
#include "tsl/platform/cpu_info.h"
#include "tsl/platform/env.h"
Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/service/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ limitations under the License.
#include "xla/statusor.h"
#include "xla/stream_executor/device_memory_allocator.h"
#include "xla/stream_executor/stream_executor.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"

namespace Eigen {
struct ThreadPoolDevice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ limitations under the License.
#include "xla/stream_executor/host/host_platform_id.h"
#include "xla/stream_executor/platform_manager.h"
#include "xla/stream_executor/stream_executor.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "xla/tests/literal_test_util.h"
#include "xla/types.h"
#include "tsl/lib/core/status_test_util.h"
Expand Down
7 changes: 5 additions & 2 deletions third_party/xla/xla/service/gpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,10 @@ cc_library(
"@local_tsl//tsl/profiler/lib:scoped_annotation",
"//xla/tsl/util/proto:proto_utils",
"//xla/service/gpu:hlo_traversal",
]) + ["@local_tsl//tsl/platform:path"],
]) + [
"//xla/stream_executor:stream_executor_memory_allocator",
"@local_tsl//tsl/platform:path",
],
)

xla_test(
Expand Down Expand Up @@ -1610,7 +1613,7 @@ cc_library(
"@local_tsl//tsl/platform:path",
"@local_tsl//tsl/platform:protobuf",
"@local_tsl//tsl/platform:statusor",
]),
]) + ["//xla/stream_executor:stream_executor_memory_allocator"],
)

# We need a separate target, as runtime executable cannot depend on compilation
Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/service/gpu/autotuner_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ limitations under the License.
#include "xla/stream_executor/device_memory_allocator.h"
#include "xla/stream_executor/gpu/redzone_allocator.h"
#include "xla/stream_executor/stream_executor.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "xla/xla.pb.h"

namespace xla {
Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/service/gpu/fusions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ xla_test(
"//xla/service/gpu/runtime:thunk",
"//xla/service/gpu/tests:gpu_codegen_test",
"//xla/stream_executor:stream_executor_headers",
"//xla/stream_executor:stream_executor_memory_allocator",
"//xla/tests:filecheck",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/service/gpu/fusions/cudnn_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ limitations under the License.
#include "xla/service/pattern_matcher.h"
#include "xla/service/pattern_matcher_gmock.h"
#include "xla/stream_executor/dnn.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "xla/stream_executor/stream_executor_pimpl.h"
#include "xla/tests/filecheck.h"
#include "xla/xla.pb.h"
Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/service/gpu/gemm_fusion_autotuner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ limitations under the License.
#include "xla/stream_executor/device_memory_allocator.h"
#include "xla/stream_executor/gpu/redzone_allocator.h"
#include "xla/stream_executor/stream.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "xla/tools/hlo_decomposer.h"
#include "xla/tsl/util/proto/proto_utils.h"
#include "xla/util.h"
Expand Down
9 changes: 6 additions & 3 deletions third_party/xla/xla/service/gpu/runtime/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,18 @@ xla_test(
local_defines = if_cuda_is_configured(["GOOGLE_CUDA=1"]) + if_rocm_is_configured(["TENSORFLOW_USE_ROCM=1"]),
deps = [
":command_buffer_cmd",
":thunk",
"//xla:status",
"//xla:types",
"//xla/service:buffer_assignment",
"//xla/service:executable",
"//xla/service:platform_util",
"//xla/service/gpu:buffer_allocations",
"//xla/service/gpu:launch_dimensions",
"//xla/service/gpu/runtime:thunk",
"//xla/stream_executor",
"//xla/stream_executor:platform",
"//xla/stream_executor:platform_manager",
"//xla/stream_executor:stream_executor_memory_allocator",
"//xla/stream_executor/gpu:gpu_test_kernels",
"@com_google_absl//absl/functional:function_ref",
"@com_google_absl//absl/status",
Expand Down Expand Up @@ -356,6 +357,7 @@ xla_test(
":address_computation_thunk",
":custom_call_thunk",
":gemm_thunk",
":thunk",
"//xla:shape_util",
"//xla:types",
"//xla/ffi",
Expand All @@ -366,10 +368,10 @@ xla_test(
"//xla/service/gpu:buffer_allocations",
"//xla/service/gpu:launch_dimensions",
"//xla/service/gpu:matmul_utils",
"//xla/service/gpu/runtime:thunk",
"//xla/stream_executor",
"//xla/stream_executor:platform",
"//xla/stream_executor:platform_manager",
"//xla/stream_executor:stream_executor_memory_allocator",
"//xla/stream_executor/gpu:gpu_test_kernels",
"//xla/stream_executor/gpu:gpu_types_header",
"@com_google_absl//absl/algorithm:container",
Expand Down Expand Up @@ -454,6 +456,7 @@ xla_test(
deps = [
":command_buffer_cmd",
":command_buffer_thunk",
":thunk",
"//xla:shape_util",
"//xla:types",
"//xla:xla_data_proto_cc",
Expand All @@ -463,10 +466,10 @@ xla_test(
"//xla/service/gpu:buffer_allocations",
"//xla/service/gpu:launch_dimensions",
"//xla/service/gpu:matmul_utils",
"//xla/service/gpu/runtime:thunk",
"//xla/stream_executor",
"//xla/stream_executor:platform",
"//xla/stream_executor:platform_manager",
"//xla/stream_executor:stream_executor_memory_allocator",
"//xla/stream_executor/gpu:gpu_test_kernels",
"//xla/stream_executor/gpu:gpu_types_header",
"@com_google_absl//absl/status:statusor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ limitations under the License.
#include "xla/stream_executor/platform_manager.h"
#include "xla/stream_executor/stream.h"
#include "xla/stream_executor/stream_executor.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "xla/types.h" // IWYU pragma: keep
#include "tsl/lib/core/status_test_util.h"
#include "tsl/platform/test.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ limitations under the License.
#include "xla/stream_executor/platform.h"
#include "xla/stream_executor/platform_manager.h"
#include "xla/stream_executor/stream_executor.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "xla/types.h" // IWYU pragma: keep
#include "tsl/lib/core/status_test_util.h"
#include "tsl/platform/status.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ limitations under the License.
#include "xla/stream_executor/platform.h"
#include "xla/stream_executor/platform_manager.h"
#include "xla/stream_executor/stream_executor.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "xla/types.h" // IWYU pragma: keep
#include "xla/xla_data.pb.h"
#include "tsl/lib/core/status_test_util.h"
Expand Down
2 changes: 2 additions & 0 deletions third_party/xla/xla/service/gpu/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ xla_test(
"//xla/service/gpu:gpu_executable",
"//xla/stream_executor:device_description",
"//xla/stream_executor:device_memory_allocator",
"//xla/stream_executor:stream_executor_memory_allocator",
"//xla/tests:filecheck",
"//xla/tests:verified_hlo_module",
"@com_google_absl//absl/container:flat_hash_map",
Expand Down Expand Up @@ -230,6 +231,7 @@ xla_cc_test(
":gpu_codegen_test",
"//xla/hlo/ir:hlo",
"//xla/service:executable",
"//xla/stream_executor:stream_executor_memory_allocator",
"@com_google_absl//absl/status:statusor",
"@local_tsl//tsl/platform:statusor",
"@local_tsl//tsl/platform:test",
Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/service/gpu/tests/gemm_rewrite_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ limitations under the License.
#include "xla/service/pattern_matcher_gmock.h"
#include "xla/stream_executor/device_description.h"
#include "xla/stream_executor/device_memory_allocator.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "xla/test.h"
#include "xla/tests/filecheck.h"
#include "xla/tests/verified_hlo_module.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#include "xla/hlo/ir/hlo_module.h"
#include "xla/service/executable.h"
#include "xla/service/gpu/tests/gpu_codegen_test.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "tsl/platform/statusor.h"
#include "tsl/platform/test.h"

Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/service/hlo_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ limitations under the License.
#include "xla/shape.h"
#include "xla/shape_util.h"
#include "xla/stream_executor/device_memory_allocator.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "tsl/platform/blocking_counter.h"
#include "tsl/platform/logging.h"
#include "tsl/platform/statusor.h"
Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/service/shaped_buffer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include "xla/shape_util.h"
#include "xla/stream_executor/device_memory_allocator.h"
#include "xla/stream_executor/stream_executor_interface.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "xla/test.h"
#include "tsl/platform/test_benchmark.h"

Expand Down
24 changes: 22 additions & 2 deletions third_party/xla/xla/stream_executor/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,10 @@ cc_library(

cc_library(
name = "device_memory_allocator",
srcs = ["device_memory_allocator.cc"],
hdrs = ["device_memory_allocator.h"],
deps = [
":device_memory",
":platform",
":stream_executor_headers",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
Expand All @@ -269,6 +267,28 @@ cc_library(
],
)

cc_library(
name = "stream_executor_memory_allocator",
srcs = ["stream_executor_memory_allocator.cc"],
hdrs = ["stream_executor_memory_allocator.h"],
deps = [
":device_memory",
":device_memory_allocator",
":platform",
":stream_executor_headers",
":stream_executor_interface",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/types:span",
"@local_tsl//tsl/platform:logging",
"@local_tsl//tsl/platform:numbers",
"@local_tsl//tsl/platform:statusor",
],
)

cc_library(
name = "host_memory_allocation",
srcs = ["host_memory_allocation.cc"],
Expand Down
55 changes: 1 addition & 54 deletions third_party/xla/xla/stream_executor/device_memory_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@ limitations under the License.

#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <map>
#include <vector>

#include "absl/base/thread_annotations.h"
#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/synchronization/mutex.h"
#include "absl/types/span.h"
#include "xla/stream_executor/device_memory.h"
#include "xla/stream_executor/platform.h"
#include "xla/stream_executor/stream_executor_interface.h"
#include "tsl/platform/errors.h"
#include "tsl/platform/status.h"

Expand Down Expand Up @@ -79,8 +72,7 @@ class ScopedDeviceMemory {
device_ordinal_(other.device_ordinal_),
allocator_(other.allocator_) {}

// Releases the memory that was provided in the constructor, through the
// "parent" StreamExecutor.
// Releases the memory that was provided in the constructor.
~ScopedDeviceMemory() { TF_CHECK_OK(Free()); }

// Moves ownership of the memory from other to this object.
Expand Down Expand Up @@ -223,51 +215,6 @@ class DeviceMemoryAllocator {
const Platform *platform_;
};

// Default memory allocator for a platform which uses
// StreamExecutor::Allocate/Deallocate.
class StreamExecutorMemoryAllocator : public DeviceMemoryAllocator {
public:
// Create an allocator supporting a single device, corresponding to the passed
// executor.
explicit StreamExecutorMemoryAllocator(StreamExecutorInterface *executor);

// Create an allocator supporting multiple stream executors.
//
// Precondition: all stream_executors have different device ordinals.
StreamExecutorMemoryAllocator(
const Platform *platform,
absl::Span<StreamExecutorInterface *const> stream_executors);

absl::StatusOr<OwningDeviceMemory> Allocate(int device_ordinal, uint64_t size,
bool retry_on_failure,
int64_t memory_space) override;

// Pull in two-arg overload that sets retry_on_failure to true.
using DeviceMemoryAllocator::Allocate;

absl::Status Deallocate(int device_ordinal, DeviceMemoryBase mem) override;

bool AllowsAsynchronousDeallocation() const override;

// Gets-or-creates a stream for a given `device_ordinal` from an appropriate
// stream executor.
absl::StatusOr<Stream *> GetStream(int device_ordinal) override;

// Gets the stream executor for given device ordinal.
absl::StatusOr<StreamExecutorInterface *> GetStreamExecutor(
int device_ordinal) const;

private:
// Available stream executors. Each stream executor has a different device
// ordinal.
std::vector<StreamExecutorInterface *> stream_executors_;

absl::Mutex mutex_;

// Cache of streams for GetStream.
std::map<int, std::unique_ptr<Stream>> streams_ ABSL_GUARDED_BY(mutex_);
};

template <typename ElemT>
absl::Status ScopedDeviceMemory<ElemT>::Free() {
if (!wrapped_.is_null()) {
Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/stream_executor/gpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ xla_cc_test(
":redzone_allocator",
"//xla/stream_executor",
"//xla/stream_executor:device_memory_allocator",
"//xla/stream_executor:stream_executor_memory_allocator",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ limitations under the License.
#include "xla/stream_executor/gpu/gpu_init.h"
#include "xla/stream_executor/platform.h"
#include "xla/stream_executor/platform_manager.h"
#include "xla/stream_executor/stream_executor_memory_allocator.h"
#include "tsl/lib/core/status_test_util.h"
#include "tsl/platform/statusor.h"
#include "tsl/platform/test.h"
Expand Down