Home | History | Annotate | Download | only in service
      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/hlo_reachability.h"
     17 
     18 #include <set>
     19 
     20 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
     21 #include "tensorflow/compiler/xla/test.h"
     22 #include "tensorflow/compiler/xla/test_helpers.h"
     23 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
     24 
     25 namespace xla {
     26 
     27 namespace {
     28 
     29 class HloReachabilityTest : public HloTestBase {};
     30 
     31 TEST_F(HloReachabilityTest, Reachability) {
     32   // Construct and test a reachability graph of the following form:
     33   /*
     34        a
     35       / \
     36      b   c
     37       \ / \
     38        d   e
     39   */
     40   auto builder = HloComputation::Builder(TestName());
     41   auto a = builder.AddInstruction(
     42       HloInstruction::CreateConstant(Literal::CreateR0<float>(0.0f)));
     43   auto b = builder.AddInstruction(
     44       HloInstruction::CreateConstant(Literal::CreateR0<float>(0.0f)));
     45   auto c = builder.AddInstruction(
     46       HloInstruction::CreateConstant(Literal::CreateR0<float>(0.0f)));
     47   auto d = builder.AddInstruction(
     48       HloInstruction::CreateConstant(Literal::CreateR0<float>(0.0f)));
     49   auto e = builder.AddInstruction(
     50       HloInstruction::CreateConstant(Literal::CreateR0<float>(0.0f)));
     51   builder.Build();
     52 
     53   HloReachabilityMap reachability({a, b, c, d, e});
     54   reachability.SetReachable(a, a);
     55   EXPECT_TRUE(reachability.SetReachabilityToUnion({a}, b));
     56   EXPECT_TRUE(reachability.SetReachabilityToUnion({a}, c));
     57   EXPECT_TRUE(reachability.SetReachabilityToUnion({b, c}, d));
     58   EXPECT_TRUE(reachability.SetReachabilityToUnion({c}, e));
     59 
     60   EXPECT_TRUE(reachability.IsReachable(a, a));
     61   EXPECT_TRUE(reachability.IsReachable(a, b));
     62   EXPECT_TRUE(reachability.IsReachable(a, c));
     63   EXPECT_TRUE(reachability.IsReachable(a, d));
     64   EXPECT_TRUE(reachability.IsReachable(a, e));
     65 
     66   EXPECT_FALSE(reachability.IsReachable(b, a));
     67   EXPECT_TRUE(reachability.IsReachable(b, b));
     68   EXPECT_FALSE(reachability.IsReachable(b, c));
     69   EXPECT_TRUE(reachability.IsReachable(b, d));
     70   EXPECT_FALSE(reachability.IsReachable(b, e));
     71 
     72   EXPECT_FALSE(reachability.IsReachable(e, a));
     73   EXPECT_FALSE(reachability.IsReachable(e, b));
     74   EXPECT_FALSE(reachability.IsReachable(e, c));
     75   EXPECT_FALSE(reachability.IsReachable(e, d));
     76   EXPECT_TRUE(reachability.IsReachable(e, e));
     77 
     78   // Recomputing the same reachability for a previously computed instruction
     79   // should return false (no change).
     80   EXPECT_FALSE(reachability.SetReachabilityToUnion({a}, b));
     81   EXPECT_FALSE(reachability.SetReachabilityToUnion({b, c}, d));
     82 }
     83 
     84 }  // namespace
     85 
     86 }  // namespace xla
     87