1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/compiler/xla/pjrt/tracked_device_buffer.h"
17
18 #include <memory>
19
20 #include "tensorflow/compiler/xla/client/client_library.h"
21 #include "tensorflow/compiler/xla/literal_util.h"
22 #include "tensorflow/compiler/xla/shape_util.h"
23 #include "tensorflow/compiler/xla/status_macros.h"
24 #include "tensorflow/compiler/xla/test.h"
25 #include "tensorflow/stream_executor/device_memory_allocator.h"
26
27 namespace xla {
28 namespace {
29
MakeArray(const Shape & shape,LocalClient * client)30 StatusOr<std::shared_ptr<TrackedDeviceBuffer>> MakeArray(const Shape& shape,
31 LocalClient* client) {
32 std::vector<stream_executor::DeviceMemoryBase> device_buffers;
33 TF_RETURN_IF_ERROR(ShapeUtil::ForEachSubshapeWithStatus(
34 client->backend().transfer_manager()->HostShapeToDeviceShape(shape),
35 [&](const Shape& subshape, const ShapeIndex&) -> Status {
36 TF_ASSIGN_OR_RETURN(
37 se::OwningDeviceMemory device_memory,
38 client->backend().memory_allocator()->Allocate(
39 /*device_ordinal=*/0,
40 client->backend().transfer_manager()->GetByteSizeRequirement(
41 subshape)));
42 device_buffers.push_back(device_memory.Release());
43 return OkStatus();
44 }));
45 return std::make_shared<TrackedDeviceBuffer>(
46 client->backend().memory_allocator(), /*device_ordinal=*/0,
47 device_buffers,
48 absl::Span<const std::shared_ptr<BufferSequencingEvent>>(), nullptr);
49 }
50
TEST(TrackedDeviceBufferTest,AsShapedBuffer)51 TEST(TrackedDeviceBufferTest, AsShapedBuffer) {
52 LocalClient* client = ClientLibrary::LocalClientOrDie();
53
54 Shape a_shape = ShapeUtil::MakeShape(F32, {3, 101, 4});
55 Shape b_shape = ShapeUtil::MakeShape(S8, {77});
56 Shape c_shape = ShapeUtil::MakeShape(S64, {});
57 TF_ASSERT_OK_AND_ASSIGN(auto a_buffer, MakeArray(a_shape, client));
58 TF_ASSERT_OK_AND_ASSIGN(auto b_buffer, MakeArray(b_shape, client));
59 TF_ASSERT_OK_AND_ASSIGN(auto c_buffer, MakeArray(c_shape, client));
60
61 ASSERT_EQ(a_buffer->device_memory().size(), 1);
62 ASSERT_EQ(b_buffer->device_memory().size(), 1);
63 ASSERT_EQ(c_buffer->device_memory().size(), 1);
64 std::vector<se::DeviceMemoryBase> expected_buffer_sequence = {
65 a_buffer->device_memory()[0], b_buffer->device_memory()[0],
66 c_buffer->device_memory()[0]};
67 ShapedBuffer shaped_a = a_buffer->AsShapedBuffer(
68 client->backend().transfer_manager()->HostShapeToDeviceShape(a_shape));
69 ShapedBuffer shaped_b = b_buffer->AsShapedBuffer(
70 client->backend().transfer_manager()->HostShapeToDeviceShape(b_shape));
71 ShapedBuffer shaped_c = c_buffer->AsShapedBuffer(
72 client->backend().transfer_manager()->HostShapeToDeviceShape(c_shape));
73 auto expected_it = expected_buffer_sequence.begin();
74 for (auto it = shaped_a.buffers().begin(); it != shaped_a.buffers().end();
75 ++it) {
76 ASSERT_TRUE(expected_it != expected_buffer_sequence.end());
77 EXPECT_TRUE(expected_it->IsSameAs(it->second));
78 ++expected_it;
79 }
80 for (auto it = shaped_b.buffers().begin(); it != shaped_b.buffers().end();
81 ++it) {
82 ASSERT_TRUE(expected_it != expected_buffer_sequence.end());
83 EXPECT_TRUE(expected_it->IsSameAs(it->second));
84 ++expected_it;
85 }
86 for (auto it = shaped_c.buffers().begin(); it != shaped_c.buffers().end();
87 ++it) {
88 ASSERT_TRUE(expected_it != expected_buffer_sequence.end());
89 EXPECT_TRUE(expected_it->IsSameAs(it->second));
90 ++expected_it;
91 }
92 EXPECT_TRUE(expected_it == expected_buffer_sequence.end());
93 }
94
TEST(TrackedDeviceBufferTest,FromScopedShapedBuffer)95 TEST(TrackedDeviceBufferTest, FromScopedShapedBuffer) {
96 LocalClient* client = ClientLibrary::LocalClientOrDie();
97
98 Literal literal = LiteralUtil::MakeTupleOwned(
99 LiteralUtil::CreateFullWithDescendingLayout<float>({10, 3, 7}, 33.4f),
100 LiteralUtil::One(S64));
101
102 TF_ASSERT_OK_AND_ASSIGN(
103 ScopedShapedBuffer shaped_buffer,
104 client->LiteralToShapedBuffer(literal, /*device_ordinal=*/0));
105 std::shared_ptr<TrackedDeviceBuffer> device_buffer =
106 TrackedDeviceBuffer::FromScopedShapedBuffer(&shaped_buffer, {});
107
108 EXPECT_EQ(device_buffer->device_memory().size(),
109 ShapeUtil::SubshapeCount(
110 client->backend().transfer_manager()->HostShapeToDeviceShape(
111 literal.shape())));
112 }
113
114 } // namespace
115 } // namespace xla
116