Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions xla/pjrt/gpu/se_gpu_pjrt_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ limitations under the License.

namespace xla {

template <typename MemorySpaceKind>
static bool IsMemorySpaceKind(const PjRtMemorySpace* memory_space) {
return memory_space->kind_id() == MemorySpaceKind::kKindId;
}

absl::Status RunCallbackOnStream(
se::Stream* stream, AsyncWorkRunner* async_work_runner,
absl::AnyInvocable<void() &&> callback,
Expand Down Expand Up @@ -1344,6 +1349,19 @@ absl::StatusOr<Layout> StreamExecutorGpuClient::GetDefaultLayout(
return topology_->GetDefaultLayout(element_type, dims);
}

absl::StatusOr<xla::Shape> StreamExecutorGpuClient::GetCopyDestinationShape(
const xla::Shape& shape, PjRtMemorySpace* src_memory_space,
PjRtMemorySpace* dst_memory_space) {
if (this != dst_memory_space->client() ||
IsMemorySpaceKind<UnpinnedHostMemorySpace>(src_memory_space) !=
IsMemorySpaceKind<UnpinnedHostMemorySpace>(dst_memory_space)) {
return CommonPjRtClient::GetCopyDestinationShape(shape, src_memory_space,
dst_memory_space);
}
return MakeDefaultShapeForMemorySpace(
dst_memory_space, shape, shape.has_layout() ? &shape.layout() : nullptr);
}

absl::StatusOr<std::unique_ptr<PjRtLoadedExecutable>>
StreamExecutorGpuClient::CompileAndLoad(MaybeOwningMlirModule module,
CompileOptions options) {
Expand Down
4 changes: 4 additions & 0 deletions xla/pjrt/gpu/se_gpu_pjrt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ class StreamExecutorGpuClient : public xla::PjRtStreamExecutorClient {
absl::StatusOr<Layout> GetDefaultLayout(
PrimitiveType element_type, absl::Span<const int64_t> dims) override;

absl::StatusOr<xla::Shape> GetCopyDestinationShape(
const xla::Shape& shape, PjRtMemorySpace* src_memory_space,
PjRtMemorySpace* dst_memory_space) override;

absl::StatusOr<std::unique_ptr<PjRtLoadedExecutable>> LoadSerialized(
absl::string_view serialized, std::optional<CompileOptions> options,
const LoadOptions& load_options);
Expand Down
26 changes: 22 additions & 4 deletions xla/pjrt/gpu/se_gpu_pjrt_client_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ limitations under the License.
#include "xla/hlo/parser/hlo_parser.h"
#include "xla/hlo/testlib/test.h"
#include "xla/layout.h"
#include "xla/layout_util.h"
#include "xla/literal.h"
#include "xla/literal_util.h"
#include "xla/pjrt/device_event.h"
Expand Down Expand Up @@ -1520,16 +1521,33 @@ TEST(StreamExecutorGpuClientTest, GetTopologyDescriptionWithGlobalDevicesTest) {
TEST(PjRtCpuClientTest, CopyToMemorySpace) {
TF_ASSERT_OK_AND_ASSIGN(auto client,
GetStreamExecutorGpuClient(DefaultOptions()));
xla::Shape shape = xla::ShapeUtil::MakeShape(S32, {128, 256});
TF_ASSERT_OK_AND_ASSIGN(auto literal, xla::MakeFakeLiteral(shape));
for (auto* memory_space : client->memory_spaces()) {
xla::Shape shape = xla::ShapeUtil::MakeShape(S32, {128, 256});
TF_ASSERT_OK_AND_ASSIGN(auto literal, xla::MakeFakeLiteral(shape));
TF_ASSERT_OK_AND_ASSIGN(
auto buffer, client->BufferFromHostLiteral(literal, memory_space));
TF_ASSERT_OK_AND_ASSIGN(buffer,
buffer->CopyToMemorySpace(buffer->memory_space()));
TF_ASSERT_OK_AND_ASSIGN(auto received_literal, buffer->ToLiteral().Await());
EXPECT_THAT(received_literal->data<int32_t>(),
ElementsAreArray(literal.data<int32_t>()));
EXPECT_EQ(*received_literal, literal);
}
}

TEST(PjRtCpuClientTest, CopyToMemorySpaceWithCustomLayout) {
TF_ASSERT_OK_AND_ASSIGN(auto client,
GetStreamExecutorGpuClient(DefaultOptions()));
xla::Shape shape = xla::ShapeUtil::MakeShape(S32, {128, 256});
TF_ASSERT_OK_AND_ASSIGN(auto literal, xla::MakeFakeLiteral(shape));
Layout device_layout = LayoutUtil::MakeAscendingLayout(2);
for (auto* memory_space : client->memory_spaces()) {
TF_ASSERT_OK_AND_ASSIGN(
auto buffer,
client->BufferFromHostLiteral(literal, memory_space, &device_layout));
TF_ASSERT_OK_AND_ASSIGN(buffer,
buffer->CopyToMemorySpace(buffer->memory_space()));
EXPECT_EQ(buffer->layout()->xla_layout(), device_layout);
TF_ASSERT_OK_AND_ASSIGN(auto received_literal, buffer->ToLiteral().Await());
EXPECT_EQ(*received_literal, literal);
}
}

Expand Down
Loading