1 /* Copyright 2022 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/host_callback.h"
17
18 #include <cstring>
19 #include <utility>
20
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include "tensorflow/compiler/xla/tests/literal_test_util.h"
24 #include "tensorflow/core/lib/core/status_test_util.h"
25
26 namespace xla {
27 namespace {
28
29 class TestPjRtHostMemoryForDeviceManager
30 : public PjRtHostMemoryForDeviceManager {
31 public:
32 ~TestPjRtHostMemoryForDeviceManager() override = default;
33
ToDeviceLayout(const void * src_data,size_t src_size,const Shape & host_shape,const Shape & device_shape)34 StatusOr<PjRtChunk> ToDeviceLayout(const void* src_data, size_t src_size,
35 const Shape& host_shape,
36 const Shape& device_shape) override {
37 auto chunk = PjRtChunk::AllocateDefault(src_size);
38 std::memcpy(chunk.data(), src_data, src_size);
39 return chunk;
40 }
41
ToHostLayout(const void * src_data,size_t src_size,const Shape & src_shape,void * dst_data,size_t dst_size,const Shape & dst_shape)42 Status ToHostLayout(const void* src_data, size_t src_size,
43 const Shape& src_shape, void* dst_data, size_t dst_size,
44 const Shape& dst_shape) override {
45 CHECK_EQ(src_size, dst_size);
46 std::memcpy(dst_data, src_data, src_size);
47 return Status::OK();
48 }
49 };
50
TEST(HostCallbackTest,Basic)51 TEST(HostCallbackTest, Basic) {
52 HostCallback host_callback;
53
54 Shape shape = ShapeUtil::MakeShape(F32, {2, 2});
55 size_t byte_size = ShapeUtil::ByteSizeOf(shape);
56
57 host_callback.operands = {HostCallbackArgInfo{/*channel_id=*/1, shape}};
58 host_callback.results = {HostCallbackArgInfo{/*channel_id=*/2, shape}};
59 host_callback.callback = [byte_size](void** outputs, void** inputs) {
60 std::memcpy(outputs[0], inputs[0], byte_size);
61 return Status::OK();
62 };
63
64 HostCallbackStates states;
65
66 auto& send_callbacks = states.send_callbacks.emplace_back();
67 auto& recv_callbacks = states.recv_callbacks.emplace_back();
68
69 TestPjRtHostMemoryForDeviceManager test_host_memory_for_device_manager;
70
71 auto context = CreateHostCallbackStateAndAppendSendRecvCallbacks(
72 std::move(host_callback), &test_host_memory_for_device_manager,
73 send_callbacks, recv_callbacks);
74
75 PjRtTransferMetadata metadata;
76 metadata.device_shape = shape;
77
78 auto literal = LiteralUtil::CreateR2({{1.0f, 2.0f}, {3.0f, 4.0f}});
79 auto chunk = PjRtChunk::AllocateDefault(/*size=*/byte_size);
80 ASSERT_EQ(chunk.size(), literal.size_bytes());
81 std::memcpy(chunk.data(), literal.untyped_data(), literal.size_bytes());
82
83 TF_ASSERT_OK(context->OnSend(/*arg_num=*/0, metadata, std::move(chunk)));
84
85 CopyToDeviceStream stream(/*total_bytes=*/byte_size, /*granule_bytes=*/8);
86 context->Receive(/*res_num=*/0, metadata, stream);
87
88 auto received_chunk = stream.ConsumeNextChunk().value();
89 ASSERT_FALSE(stream.ConsumeNextChunk());
90
91 BorrowingLiteral borrowing_literal(
92 reinterpret_cast<const char*>(received_chunk.data()), shape);
93
94 EXPECT_TRUE(LiteralTestUtil::Equal(literal, borrowing_literal));
95 }
96
97 } // namespace
98 } // namespace xla
99