1 /* Copyright 2017 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/dfs_hlo_visitor_with_default.h"
17
18 #include "tensorflow/compiler/xla/service/hlo_computation.h"
19 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
20 #include "tensorflow/compiler/xla/service/hlo_module.h"
21 #include "tensorflow/compiler/xla/service/hlo_opcode.h"
22 #include "tensorflow/compiler/xla/service/hlo_runner.h"
23 #include "tensorflow/compiler/xla/shape_util.h"
24 #include "tensorflow/compiler/xla/test.h"
25 #include "tensorflow/compiler/xla/test_helpers.h"
26 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
27 #include "tensorflow/core/lib/core/status_test_util.h"
28
29 namespace xla {
30 namespace {
31
32 class DfsHloVisitorWithDefaultTest : public HloTestBase {};
33
TEST_F(DfsHloVisitorWithDefaultTest,DefaultElementwiseTest)34 TEST_F(DfsHloVisitorWithDefaultTest, DefaultElementwiseTest) {
35 // Verify that HandleElementwiseBinary and HandleElementwiseUnary are called
36 // on the appropriate HLO ops (elementwise binary/unary ops).
37
38 class ElementwiseTestVisitor : public DfsHloVisitorWithDefault {
39 public:
40 Status DefaultAction(HloInstruction* hlo) override {
41 // The HLO should be neither an elementwise unary nor binary op. These
42 // cases are handled in HandleElementwiseBinary/Unary.
43 TF_RET_CHECK(!(hlo->IsElementwise() && hlo->operand_count() == 2))
44 << hlo->ToString();
45 TF_RET_CHECK(!(hlo->IsElementwise() && hlo->operand_count() == 1))
46 << hlo->ToString();
47 return Status::OK();
48 }
49
50 Status HandleElementwiseBinary(HloInstruction* hlo) override {
51 // HLO should be elementwise binary.
52 TF_RET_CHECK(hlo->IsElementwise() && hlo->operand_count() == 2)
53 << hlo->ToString();
54 return Status::OK();
55 }
56 Status HandleElementwiseUnary(HloInstruction* hlo) override {
57 // HLO should be elementwise unary.
58 TF_RET_CHECK(hlo->IsElementwise() && hlo->operand_count() == 1)
59 << hlo->ToString();
60 return Status::OK();
61 }
62 };
63
64 // HLO module contains are arbitrary mix of elementwise and non-elementwise
65 // operations.
66 const string& hlo_string = R"(
67 HloModule TestModule
68
69 ENTRY TestComputation {
70 arg = f32[] parameter(0)
71 tuple = (f32[]) tuple(arg)
72 gte = f32[] get-tuple-element(tuple), index=0
73 abs = f32[] abs(arg)
74 add = f32[] add(arg, gte)
75 broadcast = f32[42] broadcast(add), dimensions={}
76 slice = f32[1] slice(broadcast), slice={[1:2]}
77 copy = f32[] copy(arg)
78 eq = pred[] compare(arg, gte), direction=EQ
79 neg = f32[] negate(arg)
80 ROOT convert = f64[] convert(f32[] arg)
81 })";
82 std::unique_ptr<HloModule> module =
83 ParseAndReturnVerifiedModule(hlo_string).ConsumeValueOrDie();
84 ElementwiseTestVisitor visitor;
85 TF_EXPECT_OK(module->entry_computation()->Accept(&visitor));
86 }
87
88 } // namespace
89 } // namespace xla
90