1 /* Copyright 2017 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_GRAPPLER_UTILS_FRAME_H_
17 #define TENSORFLOW_CORE_GRAPPLER_UTILS_FRAME_H_
18 
19 #include "absl/container/flat_hash_map.h"
20 #include "tensorflow/core/framework/graph.pb.h"
21 #include "tensorflow/core/grappler/utils/graph_view.h"
22 #include "tensorflow/core/lib/core/status.h"
23 
24 namespace tensorflow {
25 namespace grappler {
26 
27 // FrameView is a helper class that allows to find in what execution frames (if
28 // any) the given node can be running in. It's constructed from an immutable
29 // GraphView, and any modification of the underlying graph might invalidate it.
30 //
31 // All execution frames assigned a unique integer id, but they do not have any
32 // meaning whatsoever, it's just a sequence number.
33 //
34 // See the paper "Dynamic Control Flow in Large-Scale Machine Learning" for
35 // detailed explanation of execution frames (https://arxiv.org/abs/1805.01772).
36 class FrameView {
37  public:
FrameView()38   FrameView() : is_inferred_(false), num_frames_(0) {}
39 
40   // Infers nodes execution frames from the GraphView. Returns an error if
41   // called multiple times.
42   Status InferFromGraphView(const utils::GraphView& graph_view);
43   // Infers nodes execution frames from the MutableGraphView. Returns an error
44   // if called multiple times.
45   Status InferFromGraphView(const utils::MutableGraphView& graph_view);
46   // Infers nodes execution by constructing temporary GraphView and passing it
47   // to InferFromGraphView.
48   Status InferFromGraph(const GraphDef& graph);
49 
50   // Returns all frames of the given node (denoted by their frame ids) in
51   // outermost-to-innermost order.
52   const std::vector<int>& Frames(const NodeDef& node) const;
53 
54   // Returns true iff the node is at least in one execution frame.
55   bool IsInFrame(const NodeDef& node) const;
56 
num_frames()57   int num_frames() const { return num_frames_; }
is_inferred()58   bool is_inferred() const { return is_inferred_; }
59 
60  private:
61   template <typename GraphViewT>
62   inline Status InferFromGraphViewT(const GraphViewT& graph_view);
63 
64   bool is_inferred_;  // true if it was inferred from the graph
65   int num_frames_;    // number of frames present in a graph
66   absl::flat_hash_map<const NodeDef*, std::vector<int>> node_to_frames_;
67 
68   // We return a reference to this vector if node has no frames.
69   const std::vector<int> node_has_no_frames_;
70 };
71 
72 }  // namespace grappler
73 }  // namespace tensorflow
74 
75 #endif  // TENSORFLOW_CORE_GRAPPLER_UTILS_FRAME_H_
76