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#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
27namespace tsl {
28class Env;
29} // namespace tsl
30namespace tensorflow {
31using Env = tsl::Env;
32
33class Device;
34class 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.
45class 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