• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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 #ifndef TENSORFLOW_CORE_TFRT_UTILS_TFRT_GRAPH_EXECUTION_STATE_H_
16 #define TENSORFLOW_CORE_TFRT_UTILS_TFRT_GRAPH_EXECUTION_STATE_H_
17 
18 #include <functional>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "absl/time/time.h"
24 #include "tensorflow/compiler/mlir/tensorflow/translate/mlir_roundtrip_flags.h"
25 #include "tensorflow/core/common_runtime/graph_execution_state.h"
26 #include "tensorflow/core/framework/graph.pb.h"
27 #include "tensorflow/core/graph/graph.h"
28 #include "tensorflow/core/platform/status.h"
29 #include "tensorflow/core/protobuf/config.pb.h"
30 #include "tensorflow/core/tfrt/fallback/fallback_state.h"
31 #include "tensorflow/core/tfrt/utils/statusor.h"
32 
33 namespace tensorflow {
34 namespace tfrt_stub {
35 
36 // This is a TFRT variant of `tensorflow::GraphExecutionState`. It wraps
37 // `tensorflow::GraphExecutionState` and adds TFRT-specific adjustments.
38 //
39 // Responsible for generating an executable `Graph` from the original `GraphDef`
40 // that specifies the complete graph and from `GraphImportConfig` that specifies
41 // input/output nodes.
42 //
43 // Thread-safe.
44 class TfrtGraphExecutionState {
45  public:
46   struct OptimizationResult {
47     std::unique_ptr<tensorflow::Graph> graph;
48     absl::Duration functionalization_duration;
49     absl::Duration grappler_duration;
50   };
51 
52   // Creates a `GraphExecutionState` given `graph_def` and `fallback_state`.
53   static StatusOr<std::unique_ptr<TfrtGraphExecutionState>> Create(
54       tensorflow::GraphDef graph_def, const FallbackState& fallback_state);
55 
56   // Ctor. Do not use directly. Public only for `std::make_unique<>()`.
TfrtGraphExecutionState(std::unique_ptr<tensorflow::GraphExecutionState> graph_execution_state)57   explicit TfrtGraphExecutionState(
58       std::unique_ptr<tensorflow::GraphExecutionState> graph_execution_state)
59       : graph_execution_state_(std::move(graph_execution_state)) {}
60 
61   // Creates an optimized graph by pruning with `graph_import_config` and
62   // best-effort Grappler run.
63   StatusOr<OptimizationResult> CreateOptimizedGraph(
64       const tensorflow::GraphImportConfig& graph_import_config);
65 
66  private:
67   // Return the preprocessed full graph. Note that it does not contain the
68   // function library in the original graph.
graph()69   const tensorflow::Graph& graph() const {
70     DCHECK(graph_execution_state_->full_graph());
71     return *graph_execution_state_->full_graph();
72   }
73 
74   // Return the function library in the original graph.
flib_def()75   const FunctionLibraryDefinition& flib_def() const {
76     return graph_execution_state_->flib_def();
77   }
78 
79   Status OptimizeGraph(
80       std::unique_ptr<tensorflow::Graph>& graph,
81       const tensorflow::BuildGraphOptions& build_graph_options);
82 
83   std::unique_ptr<tensorflow::GraphExecutionState> graph_execution_state_;
84 };
85 
86 // Prunes the `graph_def` using the feed/fetch nodes specified in
87 // `callable_options`. It is a TFRT-specific version that it performs more
88 // pruning (e.g., prunes the input edges to the feed nodes) than
89 // `ComputeTransitiveFanin()` so that the graph can be functionalized properly
90 // later.
91 Status PruneGraphDef(GraphDef& graph_def,
92                      const CallableOptions& callable_options);
93 
94 // Eliminates ref variables in V1 control flow, which is required for
95 // functionalization. Current strategy is to insert an identity node between
96 // each ref node and its ref input and in-place update the ref node to its
97 // non-ref counterpart.
98 Status EliminateRefVariablesFromV1ControlFlow(GraphDef& graph_def);
99 
100 }  // namespace tfrt_stub
101 }  // namespace tensorflow
102 
103 #endif  // TENSORFLOW_CORE_TFRT_UTILS_TFRT_GRAPH_EXECUTION_STATE_H_
104