1 /* Copyright 2018 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/service/tuple_util.h"
17
18 #include <memory>
19
20 #include "absl/memory/memory.h"
21 #include "tensorflow/compiler/xla/service/hlo_matchers.h"
22 #include "tensorflow/compiler/xla/service/hlo_module_config.h"
23 #include "tensorflow/compiler/xla/service/hlo_parser.h"
24 #include "tensorflow/compiler/xla/shape_util.h"
25 #include "tensorflow/compiler/xla/test.h"
26 #include "tensorflow/compiler/xla/tests/verified_hlo_module.h"
27
28 namespace xla {
29 namespace {
30
31 namespace op = ::xla::testing::opcode_matchers;
32
GetParsedModule(HloComputation ** entry_computation,HloInstruction ** param0,HloInstruction ** param1)33 StatusOr<std::unique_ptr<VerifiedHloModule>> GetParsedModule(
34 HloComputation** entry_computation, HloInstruction** param0,
35 HloInstruction** param1) {
36 const char* const hlo_string = R"(
37 HloModule Module
38
39 ENTRY entry {
40 p0 = (f32[32,32]{1,0},f32[32,32]{1,0},f32[32,32]{1,0}) parameter(0)
41 ROOT p1 = f32[32,32]{1,0} parameter(1)
42 }
43 )";
44
45 auto module = absl::make_unique<VerifiedHloModule>(
46 "TupleUtilTest", HloModuleConfig(), /*verifier_layout_sensitive=*/true,
47 /*allow_mixed_precision_in_hlo_verifier=*/false,
48 ShapeUtil::ByteSizeOfElements);
49 TF_RETURN_IF_ERROR(module->ParseHloStringAndVerifyModule(hlo_string));
50
51 *entry_computation = module->entry_computation();
52 *param0 = (*entry_computation)->parameter_instruction(0);
53 *param1 = (*entry_computation)->parameter_instruction(1);
54
55 return std::move(module);
56 }
57
TEST(TupleUtilTest,ExtractPrefix)58 TEST(TupleUtilTest, ExtractPrefix) {
59 HloInstruction *param0, *param1;
60 HloComputation* entry_computation;
61
62 TF_ASSERT_OK_AND_ASSIGN(
63 auto module, GetParsedModule(&entry_computation, ¶m0, ¶m1));
64
65 HloInstruction* prefix = TupleUtil::ExtractPrefix(param0, 2);
66
67 EXPECT_THAT(prefix, op::Tuple(op::GetTupleElement(op::Parameter(0), 0),
68 op::GetTupleElement(op::Parameter(0), 1)));
69 }
70
TEST(TupleUtilTest,AppendSuffix)71 TEST(TupleUtilTest, AppendSuffix) {
72 HloInstruction *param0, *param1;
73 HloComputation* entry_computation;
74
75 TF_ASSERT_OK_AND_ASSIGN(
76 auto module, GetParsedModule(&entry_computation, ¶m0, ¶m1));
77
78 HloInstruction* with_suffix =
79 TupleUtil::AppendSuffix(param0, {param1, param1});
80
81 EXPECT_THAT(with_suffix, op::Tuple(op::GetTupleElement(op::Parameter(0), 0),
82 op::GetTupleElement(op::Parameter(0), 1),
83 op::GetTupleElement(op::Parameter(0), 2),
84 op::Parameter(1), op::Parameter(1)));
85 }
86
87 } // namespace
88 } // namespace xla
89