1/* Copyright 2016 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 <map> 17#include <string> 18#include <unordered_map> 19#include <vector> 20 21#include "tensorflow/core/common_runtime/graph_runner.h" 22 23#include "tensorflow/cc/framework/scope.h" 24#include "tensorflow/cc/ops/standard_ops.h" 25#include "tensorflow/core/common_runtime/device_factory.h" 26#include "tensorflow/core/common_runtime/device_mgr.h" 27#include "tensorflow/core/framework/function_testlib.h" 28#include "tensorflow/core/framework/node_def_util.h" 29#include "tensorflow/core/framework/tensor.h" 30#include "tensorflow/core/framework/tensor_shape.h" 31#include "tensorflow/core/framework/tensor_testutil.h" 32#include "tensorflow/core/framework/types.h" 33#include "tensorflow/core/graph/node_builder.h" 34#include "tensorflow/core/graph/testlib.h" 35#include "tensorflow/core/lib/core/status_test_util.h" 36#include "tensorflow/core/lib/core/threadpool.h" 37#include "tensorflow/core/lib/strings/strcat.h" 38#include "tensorflow/core/platform/test.h" 39#include "tensorflow/core/public/session_options.h" 40 41namespace tensorflow { 42namespace { 43 44using test::internal::ExpectEqual; 45 46TEST(GraphRunnerTest, SingleConst) { 47 Scope root = Scope::NewRootScope(); 48 auto c = ops::Const(root, 42.0f); 49 GraphRunner graph_runner(Env::Default()); 50 std::vector<Tensor> outputs; 51 Status s = graph_runner.Run(root.graph(), nullptr, {}, {c.name()}, &outputs); 52 TF_ASSERT_OK(s); 53 ExpectEqual(42.0f, outputs[0].scalar<float>()()); 54} 55 56// If not using DeepCopy, and the allocator is deleted with the cpu-device, 57// this test will seg-fault. 58TEST(GraphRunnerTest, DeepCopy) { 59 Scope root = Scope::NewRootScope(); 60 auto p1 = ops::Placeholder(root.WithOpName("p1"), DT_FLOAT); 61 auto p2 = ops::Placeholder(root.WithOpName("p2"), DT_FLOAT); 62 auto add = ops::Add(root.WithOpName("add"), p1, p2); 63 64 Tensor p1_data(DT_FLOAT, TensorShape({})); 65 Tensor p2_data(DT_FLOAT, TensorShape({})); 66 p1_data.scalar<float>()() = 1.0f; 67 p2_data.scalar<float>()() = 2.0f; 68 std::vector<std::pair<string, Tensor>> inputs = {{"p1:0", p1_data}, 69 {"p2:0", p2_data}}; 70 71 // Create and destroy the GraphRunner, and ensure that the outputs are 72 // consumable beyond the lifetime of GraphRunner. 73 std::vector<Tensor> outputs; 74 { 75 GraphRunner graph_runner(Env::Default()); 76 Status s = 77 graph_runner.Run(root.graph(), nullptr, inputs, {"add:0"}, &outputs); 78 TF_ASSERT_OK(s); 79 } 80 ExpectEqual(3.0f, outputs[0].scalar<float>()()); 81} 82 83TEST(GraphRunnerTest, MultiFetchConst) { 84 Scope root = Scope::NewRootScope(); 85 auto c = ops::Const(root, 42.0f); 86 auto pi = ops::Const(root, 3.14f); 87 GraphRunner graph_runner(Env::Default()); 88 std::vector<Tensor> outputs; 89 Status s = graph_runner.Run(root.graph(), nullptr, {}, {c.name(), pi.name()}, 90 &outputs); 91 TF_ASSERT_OK(s); 92 ExpectEqual(42.0f, outputs[0].scalar<float>()()); 93 ExpectEqual(3.14f, outputs[1].scalar<float>()()); 94} 95 96TEST(GraphRunnerTest, FeedAndFetch) { 97 Scope root = Scope::NewRootScope(); 98 auto p1 = ops::Placeholder(root.WithOpName("p1"), DT_FLOAT); 99 auto p2 = ops::Placeholder(root.WithOpName("p2"), DT_FLOAT); 100 auto add = ops::Add(root.WithOpName("add"), p1, p2); 101 102 Tensor p1_data(DT_FLOAT, TensorShape({})); 103 Tensor p2_data(DT_FLOAT, TensorShape({})); 104 p1_data.scalar<float>()() = 1.0f; 105 p2_data.scalar<float>()() = 2.0f; 106 std::vector<std::pair<string, Tensor>> inputs = {{"p1:0", p1_data}, 107 {"p2:0", p2_data}}; 108 109 GraphRunner graph_runner(Env::Default()); 110 std::vector<Tensor> outputs; 111 Status s = 112 graph_runner.Run(root.graph(), nullptr, inputs, {"add:0"}, &outputs); 113 TF_ASSERT_OK(s); 114 ExpectEqual(3.0f, outputs[0].scalar<float>()()); 115} 116 117} // namespace 118} // namespace tensorflow 119