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