Home | History | Annotate | Download | only in tf2xla
      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 // Tests for the backward const analysis.
     17 
     18 #include "tensorflow/compiler/tf2xla/const_analysis.h"
     19 
     20 #include "tensorflow/cc/framework/ops.h"
     21 #include "tensorflow/cc/ops/function_ops.h"
     22 #include "tensorflow/cc/ops/standard_ops.h"
     23 #include "tensorflow/core/lib/core/status_test_util.h"
     24 #include "tensorflow/core/platform/test.h"
     25 
     26 namespace tensorflow {
     27 namespace {
     28 
     29 TEST(ConstAnalysisTest, Basics) {
     30   Scope root = Scope::NewRootScope();
     31 
     32   auto arg0 = ops::_Arg(root.WithOpName("Arg0"), DT_INT32, 0);
     33   auto arg1 = ops::_Arg(root.WithOpName("Arg1"), DT_INT32, 1);
     34   auto arg2 = ops::_Arg(root.WithOpName("Arg2"), DT_INT32, 2);
     35   auto arg3 = ops::_Arg(root.WithOpName("Arg3"), DT_INT32, 3);
     36   auto a = ops::Shape(root, arg0);
     37   auto b = ops::Add(root, a, arg1);
     38   auto c = ops::Reshape(root, arg2, b);
     39   auto d = ops::Mul(root, c, ops::Sum(root, arg3, arg3));
     40 
     41   Graph graph(OpRegistry::Global());
     42   TF_ASSERT_OK(root.ToGraph(&graph));
     43 
     44   std::vector<bool> const_args(4, false);
     45   TF_ASSERT_OK(BackwardsConstAnalysis(graph, &const_args));
     46 
     47   // Arg 0 doesn't need to be constant since the graph only uses its shape.
     48   // Arg 1 must be constant because it flows to the shape argument of a Reshape.
     49   // Arg 2 is used only as the value input to a Reshape and need not be const.
     50   // Arg 3 is used as the reduction-indices argument to Sum and must be const.
     51   EXPECT_EQ(const_args, std::vector<bool>({false, true, false, true}));
     52 }
     53 
     54 // Regression test for a case where the backward const analysis did
     55 // not visit nodes in topological order.
     56 TEST(ConstAnalysisTest, TopologicalOrder) {
     57   for (bool order : {false, true}) {
     58     Scope root = Scope::NewRootScope();
     59 
     60     auto arg0 = ops::_Arg(root.WithOpName("Arg0"), DT_INT32, 0);
     61     auto arg1 = ops::_Arg(root.WithOpName("Arg1"), DT_INT32, 1);
     62     auto arg2 = ops::_Arg(root.WithOpName("Arg2"), DT_INT32, 2);
     63     auto a = ops::Reshape(root, arg0, arg1);
     64     auto b = ops::Reshape(root, arg2, a);
     65     if (order) {
     66       // Consider both orders for arguments to the Sum so we aren't sensitive
     67       // to the DFS traversal order.
     68       std::swap(a, b);
     69     }
     70     auto c = ops::Add(root, a, b);
     71 
     72     Graph graph(OpRegistry::Global());
     73     TF_ASSERT_OK(root.ToGraph(&graph));
     74 
     75     std::vector<bool> const_args(3, false);
     76     TF_ASSERT_OK(BackwardsConstAnalysis(graph, &const_args));
     77 
     78     EXPECT_EQ(const_args, std::vector<bool>({true, true, false}));
     79   }
     80 }
     81 
     82 }  // namespace
     83 }  // namespace tensorflow
     84