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