1/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/compiler/xla/service/tuple_util.h"
17
18#include "tensorflow/compiler/xla/service/hlo_matchers.h"
19#include "tensorflow/compiler/xla/test.h"
20#include "tensorflow/compiler/xla/tools/parser/hlo_parser.h"
21
22namespace xla {
23namespace {
24
25namespace op = ::xla::testing::opcode_matchers;
26
27StatusOr<std::unique_ptr<HloModule>> GetParsedModule(
28    HloComputation** entry_computation, HloInstruction** param0,
29    HloInstruction** param1) {
30  const char* const hlo_string = R"(
31HloModule Module
32
33ENTRY entry {
34  p0 = (f32[32,32]{1,0},f32[32,32]{1,0},f32[32,32]{1,0}) parameter(0)
35  ROOT p1 = f32[32,32]{1,0} parameter(1)
36}
37)";
38
39  TF_ASSIGN_OR_RETURN(std::unique_ptr<HloModule> module,
40                      tools::Parse(hlo_string));
41
42  *entry_computation = module->entry_computation();
43  *param0 = (*entry_computation)->parameter_instruction(0);
44  *param1 = (*entry_computation)->parameter_instruction(1);
45
46  return std::move(module);
47}
48
49TEST(TupleUtilTest, ExtractPrefix) {
50  HloInstruction *param0, *param1;
51  HloComputation* entry_computation;
52
53  TF_ASSERT_OK_AND_ASSIGN(
54      std::unique_ptr<HloModule> module,
55      GetParsedModule(&entry_computation, &param0, &param1));
56
57  HloInstruction* prefix = TupleUtil::ExtractPrefix(param0, 2);
58
59  EXPECT_THAT(prefix, op::Tuple(op::GetTupleElement(op::Parameter(0), 0),
60                                op::GetTupleElement(op::Parameter(0), 1)));
61}
62
63TEST(TupleUtilTest, AppendSuffix) {
64  HloInstruction *param0, *param1;
65  HloComputation* entry_computation;
66
67  TF_ASSERT_OK_AND_ASSIGN(
68      std::unique_ptr<HloModule> module,
69      GetParsedModule(&entry_computation, &param0, &param1));
70
71  HloInstruction* with_suffix =
72      TupleUtil::AppendSuffix(param0, {param1, param1});
73
74  EXPECT_THAT(with_suffix, op::Tuple(op::GetTupleElement(op::Parameter(0), 0),
75                                     op::GetTupleElement(op::Parameter(0), 1),
76                                     op::GetTupleElement(op::Parameter(0), 2),
77                                     op::Parameter(1), op::Parameter(1)));
78}
79
80}  // namespace
81}  // namespace xla
82