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