1 /* Copyright 2016 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 #ifndef TENSORFLOW_CORE_COMMON_RUNTIME_GRAPH_RUNNER_H_ 17 #define TENSORFLOW_CORE_COMMON_RUNTIME_GRAPH_RUNNER_H_ 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "tensorflow/core/framework/function.h" 24 #include "tensorflow/core/framework/tensor.h" 25 #include "tensorflow/core/lib/core/status.h" 26 27 namespace tensorflow { 28 29 class Device; 30 class Env; 31 class Graph; 32 33 // GraphRunner takes a Graph, some inputs to feed, and some outputs 34 // to fetch and executes the graph required to feed and fetch the 35 // inputs and outputs. 36 // 37 // This class is only meant for internal use where one needs to 38 // partially evaluate inexpensive nodes in a graph, such as for shape 39 // inference or for constant folding. Because of its limited, simple 40 // use-cases, it executes all computation on the given device (CPU by default) 41 // and is not meant to be particularly lightweight, fast, or efficient. 42 class GraphRunner { 43 public: 44 // REQUIRES: `env` is not nullptr. 45 GraphRunner(Env* env); 46 // REQUIRES: 'device' is not nullptr. Not owned. 47 GraphRunner(Device* device); 48 ~GraphRunner(); 49 50 // Function semantics for `inputs`, `output_names` and `outputs` 51 // matches those from Session::Run(). 52 // 53 // NOTE: The output tensors share lifetime with the GraphRunner, and could 54 // be destroyed once the GraphRunner is destroyed. 55 // 56 // REQUIRES: `graph`, `env`, and `outputs` are not nullptr. 57 // `function_library` may be nullptr. 58 typedef std::vector<std::pair<string, Tensor>> NamedTensorList; 59 Status Run(Graph* graph, FunctionLibraryRuntime* function_library, 60 const NamedTensorList& inputs, 61 const std::vector<string>& output_names, 62 std::vector<Tensor>* outputs); 63 64 private: 65 std::unique_ptr<Device> device_deleter_; 66 Device* const device_; 67 }; 68 69 } // namespace tensorflow 70 71 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_GRAPH_RUNNER_H_ 72