Caffe2 - C++ API
A deep learning, cross platform ML framework
net_dag_utils.h
1 #ifndef CAFFE2_CORE_NET_DAG_UTILS_H_
2 #define CAFFE2_CORE_NET_DAG_UTILS_H_
3 
4 #include <atomic>
5 #include <climits>
6 #include <cstddef>
7 #include <thread> // NOLINT
8 #include <typeinfo>
9 #include <unordered_map>
10 #include <unordered_set>
11 #include <vector>
12 
13 #include "c10/util/Registry.h"
14 #include "caffe2/core/blob.h"
15 #include "caffe2/core/common.h"
16 #include "caffe2/core/logging.h"
17 #include "caffe2/core/net.h"
18 #include "caffe2/core/observer.h"
19 #include "caffe2/core/operator_schema.h"
20 #include "caffe2/core/tensor.h"
21 #include "caffe2/core/workspace.h"
22 #include "caffe2/proto/caffe2_pb.h"
23 #include "caffe2/utils/simple_queue.h"
24 
25 namespace caffe2 {
26 namespace dag_utils {
27 
28 struct OperatorNode {
29  unique_ptr<OperatorBase> operator_;
30  vector<int> children_;
31  vector<int> parents_;
32  std::atomic<int> runtime_parent_count_;
33  bool is_chain_start_ = false;
34  std::atomic_flag scheduled_ = ATOMIC_FLAG_INIT;
35 };
36 
37 struct OpGraphNode {
38  vector<int> children_;
39  vector<int> parents_;
40  int visited_inputs = 0;
41  int num_orig_parents;
42 };
43 
44 using ExecutionChains = std::unordered_map<int, std::vector<int>>;
45 
46 C10_EXPORT ExecutionChains computeChains(std::vector<OperatorNode>& orig_nodes);
47 
48 // Instead of breaking down the DAG into chains, we partition it into clusters
49 // of sync ops and individual async op. This is useful for disturbuted inference
50 // case where we have sync and async cpu ops. Note that we have go sync each
51 // aysnc op instead of put them into the chain and sync its tail like GPU op,
52 // because CPU async ops are typically rpc calls and are not guaranteed to be
53 // linearized at remote site.
54 C10_EXPORT ExecutionChains computeGroups(std::vector<OperatorNode>& orig_nodes);
55 
56 C10_EXPORT ExecutionChains singleChains(std::vector<OperatorNode>& nodes);
57 
58 C10_EXPORT std::vector<OperatorNode> prepareOperatorNodes(
59  const std::shared_ptr<const NetDef>& net_def,
60  Workspace* ws);
61 
62 std::vector<OpGraphNode> prepareChainGraphNodes(
63  const std::vector<dag_utils::OperatorNode>& operator_nodes,
64  const std::vector<std::vector<int>>& execution_chains);
65 
66 } // namespace dag_utils
67 } // namespace caffe2
68 
69 #endif // CAFFE2_CORE_NET_DAG_UTILS_H_
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13