Home | History | Annotate | Download | only in kernels
      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/core/kernels/remote_fused_graph_execute_op_test_utils.h"
     17 
     18 #include "tensorflow/cc/ops/array_ops.h"
     19 #include "tensorflow/cc/ops/const_op.h"
     20 #include "tensorflow/cc/ops/math_ops.h"
     21 #include "tensorflow/core/framework/tensor_testutil.h"
     22 #include "tensorflow/core/kernels/remote_fused_graph_execute_utils.h"
     23 #include "tensorflow/core/lib/core/status.h"
     24 #include "tensorflow/core/platform/logging.h"
     25 
     26 namespace tensorflow {
     27 /* static */ Output RemoteFusedGraphExecuteOpTestUtils::BuildAddOp(
     28     const Scope& scope, const Input& x, const Input& y) {
     29   CHECK(scope.ok());
     30   auto _x = ops::AsNodeOut(scope, x);
     31   CHECK(scope.ok());
     32   auto _y = ops::AsNodeOut(scope, y);
     33   CHECK(scope.ok());
     34   Node* ret;
     35   const auto unique_name = scope.GetUniqueNameForOp("Add");
     36   auto builder = NodeBuilder(unique_name, "Add").Input(_x).Input(_y);
     37   scope.UpdateBuilder(&builder);
     38   scope.UpdateStatus(builder.Finalize(scope.graph(), &ret));
     39   CHECK(scope.ok()) << scope.status();
     40   return Output(ret, 0);
     41 }
     42 
     43 /* static */ Status RemoteFusedGraphExecuteOpTestUtils::BuildAddGraph(
     44     const string& name0, const float val0, const string& name1,
     45     const float val1, const string& name_out, GraphDef* graph_def) {
     46   Scope root = Scope::NewRootScope();
     47   Output node0 = ops::Const(root.WithOpName(name0), val0);
     48   Output node1 = ops::Const(root.WithOpName(name1), val1);
     49   RemoteFusedGraphExecuteOpTestUtils::BuildAddOp(root.WithOpName(name_out),
     50                                                  node0, node1);
     51   TF_RETURN_IF_ERROR(root.ToGraphDef(graph_def));
     52   return Status::OK();
     53 }
     54 
     55 /* static */ Status RemoteFusedGraphExecuteOpTestUtils::BuildMultipleAddGraph(
     56     GraphDef* graph_def) {
     57   Scope root = tensorflow::Scope::NewRootScope();
     58 
     59   Tensor a_data(DT_FLOAT, TensorShape({1, 1, 1, 1}));
     60   test::FillIota<float>(&a_data, 1.0f);
     61   Output a_const = ops::Const(root.WithOpName("A"), Input::Initializer(a_data));
     62 
     63   Tensor b_data(DT_FLOAT, TensorShape({1, 1, 1, 1}));
     64   test::FillIota<float>(&b_data, 1.0f);
     65   Output b_const = ops::Const(root.WithOpName("B"), Input::Initializer(b_data));
     66 
     67   Tensor c_data(DT_FLOAT, TensorShape({1, 1, 1, 1}));
     68   test::FillIota<float>(&c_data, 1.0f);
     69   Output c_const = ops::Const(root.WithOpName("C"), Input::Initializer(c_data));
     70 
     71   Tensor d_data(DT_FLOAT, TensorShape({1, 1, 1, 1}));
     72   test::FillIota<float>(&d_data, 1.0f);
     73   Output d_const = ops::Const(root.WithOpName("D"), Input::Initializer(d_data));
     74 
     75   Tensor e_data(DT_FLOAT, TensorShape({1, 1, 1, 1}));
     76   test::FillIota<float>(&e_data, 1.0f);
     77   Output e_const = ops::Const(root.WithOpName("E"), Input::Initializer(e_data));
     78 
     79   Output f_add = ops::Add(root.WithOpName("F"), a_const, b_const);
     80 
     81   Output g_add = ops::Add(root.WithOpName("G"), d_const, e_const);
     82 
     83   Output h_add = ops::Add(root.WithOpName("H"), f_add, c_const);
     84 
     85   Output i_add = ops::Add(root.WithOpName("I"), c_const, g_add);
     86 
     87   Output j_add = ops::Add(root.WithOpName("J"), h_add, i_add);
     88 
     89   Output k_add = ops::Add(root.WithOpName("K"), j_add, g_add);
     90 
     91   TF_RETURN_IF_ERROR(root.ToGraphDef(graph_def));
     92 
     93   return Status::OK();
     94 }
     95 
     96 TestRemoteFusedGraphExecutor::TestRemoteFusedGraphExecutor(
     97     const std::unordered_set<string>& fused_op_types,
     98     const string& executor_name)
     99     : fused_op_types_(fused_op_types), executor_name_(executor_name) {}
    100 
    101 int TestRemoteFusedGraphExecutor::GetVersion() { return 0; }
    102 bool TestRemoteFusedGraphExecutor::Init(const RemoteFusedGraphExecuteInfo&) {
    103   return true;
    104 }
    105 bool TestRemoteFusedGraphExecutor::Finalize() { return true; }
    106 bool TestRemoteFusedGraphExecutor::SetupGraph() { return true; }
    107 bool TestRemoteFusedGraphExecutor::ExecuteGraph() { return true; }
    108 bool TestRemoteFusedGraphExecutor::TeardownGraph() { return true; }
    109 bool TestRemoteFusedGraphExecutor::FillInputNode(const string&, const Tensor&) {
    110   return true;
    111 }
    112 bool TestRemoteFusedGraphExecutor::ReadOutputNode(const string&,
    113                                                   TensorAllocatorFunc) {
    114   return true;
    115 }
    116 Status TestRemoteFusedGraphExecutor::FuseRemoteGraph(
    117     const GraphDef& original_graph_def, const std::vector<string>& inputs,
    118     const std::vector<string>& outputs, GraphDef* fused_graph_def) {
    119   return RemoteFusedGraphExecuteUtils::FuseRemoteGraphByOpTypes(
    120       original_graph_def, inputs, outputs, "remote_fused_graph_node_names",
    121       fused_op_types_, executor_name_,
    122       /*require_shape_type=*/false, fused_graph_def);
    123   return Status::OK();
    124 }
    125 
    126 bool TestRemoteFusedGraphExecutor::IsEnabled() const { return true; }
    127 
    128 }  // namespace tensorflow
    129