Caffe2 - C++ API
A deep learning, cross platform ML framework
communicator_op.cc
1 #include "caffe2/core/operator.h"
2 #include "caffe2/operators/no_default_engine_op.h"
3 
4 namespace caffe2 {
5 
6 OPERATOR_SCHEMA(CreateCommonWorld)
7  .NumInputs(0, 1)
8  .NumOutputs(1)
9  .SetDoc(R"DOC(
10 Creates a common world for communication operators.
11 )DOC")
12  .Input(0, "kv_handler", "Key/value handler for rendezvous (optional).")
13  .Output(0, "comm_world", "A common world for collective operations.")
14  .Arg("size", "(int) size of the common world.")
15  .Arg("rank", "(int) rank of this node in the common world.");
16 
17 OPERATOR_SCHEMA(CloneCommonWorld)
18  .NumInputs(1)
19  .NumOutputs(1)
20  .SetDoc(R"DOC(
21 Clones existing common world.
22 )DOC")
23  .Input(0, "existing_comm_world", "Existing common world to clone.")
24  .Output(0, "comm_world", "A common world for collective operations.");
25 
26 OPERATOR_SCHEMA(DestroyCommonWorld)
27  .NumInputs(1)
28  .NumOutputs(1)
29  .EnforceInplace({{0, 0}})
30  .SetDoc("Closes all connections managed by a common world.")
31  .Input(0, "common_world", "The common world to be destroyed.");
32 
33 OPERATOR_SCHEMA(Broadcast)
34  .NumInputsOutputs([](int in, int out) {
35  return in >= 2 && out == (in - 1);
36  })
37  .EnforceInplace([](int in, int out) { return (in - 1) == out; })
38  .InputsCanCrossDevices()
39  .IdenticalTypeAndShapeOfInput(0)
40  .SetDoc(R"DOC(
41 Does a broadcast operation from the root node to every other node. The tensor
42 on each node should have been pre-created with the same shape and data type.
43 )DOC")
44  .Input(0, "comm_world", "The common world.")
45  .Input(1, "X", "A tensor to be broadcasted.")
46  .Output(0, "X", "In-place as input 1.")
47  .Arg("root", "(int, default 0) the root to run broadcast from.");
48 
49 OPERATOR_SCHEMA(Reduce)
50  .NumInputs(2)
51  .NumOutputs(1)
52  .InputsCanCrossDevices()
53  .IdenticalTypeAndShapeOfInput(0)
54  .SetDoc(R"DOC(
55 Does a reduce operation from every node to the root node. Currently only
56 Sum is supported.
57 )DOC")
58  .Input(0, "comm_world", "The common world.")
59  .Input(1, "X", "A tensor to be reduced.")
60  .Output(0, "Y", "The reduced result on root, not set for other nodes.")
61  .Arg("root", "(int, default 0) the root to run reduce into.");
62 
63 OPERATOR_SCHEMA(Allreduce)
64  .NumInputsOutputs([](int in, int out) {
65  return in >= 2 && out == (in - 1);
66  })
67  .EnforceInplace([](int in, int out) { return (in - 1) == out; })
68  .IdenticalTypeAndShapeOfInput(0)
69  .InputsCanCrossDevices()
70  .SetDoc(R"DOC(
71 Does an allreduce operation among the nodes. Currently only Sum is supported.
72 )DOC")
73  .Input(0, "comm_world", "The common world.")
74  .Input(1, "X", "A tensor to be allreduced.")
75  .Output(0, "Y", "The allreduced tensor, same on all nodes.");
76 
77 OPERATOR_SCHEMA(ReduceScatter)
78  .NumInputsOutputs([](int in, int out) {
79  return in >= 2 && out == (in - 1);
80  })
81  .EnforceInplace([](int in, int out) { return (in - 1) == out; })
82  .IdenticalTypeAndShapeOfInput(0)
83  .InputsCanCrossDevices()
84  .SetDoc(R"DOC(
85 Does reduce-scatter operation among the nodes. Currently only Sum is supported.
86 )DOC")
87  .Input(0, "comm_world", "The common world.")
88  .Input(1, "X", "A tensor to be reduce-scattered.")
89  .Output(0, "Y", "The reduced tensor, scattered on all nodes.");
90 
91 OPERATOR_SCHEMA(Allgather)
92  .NumInputs(2, INT_MAX)
93  .NumOutputs(1)
94  .InputsCanCrossDevices()
95  .SetDoc(R"DOC(
96 Does an allgather operation among the nodes.
97 )DOC")
98  .Input(0, "comm_world", "The common world.")
99  .Input(1, "X", "A tensor to be allgathered.")
100  .Output(0, "Y", "The allgathered tensor, same on all nodes.");
101 
102 OPERATOR_SCHEMA(Barrier)
103  .NumInputs(1)
104  .SetDoc(R"DOC(
105 Does a barrier operation among the nodes.
106 )DOC")
107  .Input(0, "comm_world", "The common world.");
108 
109 OPERATOR_SCHEMA(SendTensor)
110  .NumInputs({2, 4})
111  .NumOutputs(0)
112  .SetDoc(R"DOC(
113 Sends the tensor to another node.
114 )DOC")
115  .Input(0, "comm_world", "The common world.")
116  .Input(1, "X", "A tensor to be allgathered.")
117  .Input(
118  2,
119  "dst",
120  "An int CPUtensor of size 1 specifying the rank. If "
121  "given, this overrides the 'to' argument of the op.")
122  .Input(
123  3,
124  "tag",
125  "An int CPUtensor of size 1 specifying the tag to "
126  "send the tensor with. This overrides the 'tag' "
127  "argument of the op.")
128  .Arg("dst", "The rank to send the tensor to.")
129  .Arg("tag", "(int) a tag to send the tensor with.")
130  .Arg(
131  "raw_buffer",
132  "(bool) if set, only send the content and assume that the receiver "
133  "has already known the tensor's shape and information.");
134 
135 OPERATOR_SCHEMA(ReceiveTensor)
136  .NumInputs({2, 4})
137  .NumOutputs(3)
138  .EnforceInplace({{1, 0}})
139  .AllowInplace({{2, 1}, {3, 2}})
140  .SetDoc(R"DOC(
141 Receives the tensor from another node.
142 )DOC")
143  .Input(0, "comm_world", "The common world.")
144  .Input(
145  1,
146  "Y",
147  "In-place output. If raw_buffer is specified, "
148  "Y should have pre-allocated data and type..")
149  .Input(
150  2,
151  "src",
152  "An int CPUtensor of size 1 specifying the rank. If "
153  "given, this overrides the 'from' argument of the op.")
154  .Input(
155  3,
156  "tag",
157  "An int CPUtensor of size 1 specifying the tag to "
158  "send the tensor with. This overrides the 'tag' "
159  "argument of the op.")
160  .Output(0, "Y", "The received tensor.")
161  .Output(
162  1,
163  "src",
164  "The sender that sent the message as a CPUTensor "
165  "of size 1 and of type int.")
166  .Output(
167  2,
168  "tag",
169  "The tag that the message is sent with as a CPUTensor "
170  "of size 1 and of type int.")
171  .Arg("src", "(int) he rank to receive the tensor from.")
172  .Arg("tag", "(int) a tag to receive the tensor with.")
173  .Arg(
174  "raw_buffer",
175  "(bool) if set, only send the content and assume that the receiver "
176  "has already known the tensor's shape and information.");
177 
178 SHOULD_NOT_DO_GRADIENT(CreateCommonWorld);
179 SHOULD_NOT_DO_GRADIENT(CloneCommonWorld);
180 SHOULD_NOT_DO_GRADIENT(DestroyCommonWorld);
181 SHOULD_NOT_DO_GRADIENT(Broadcast);
182 SHOULD_NOT_DO_GRADIENT(Reduce);
183 SHOULD_NOT_DO_GRADIENT(Allgather);
184 SHOULD_NOT_DO_GRADIENT(Allreduce);
185 SHOULD_NOT_DO_GRADIENT(ReduceScatter);
186 SHOULD_NOT_DO_GRADIENT(Barrier);
187 SHOULD_NOT_DO_GRADIENT(SendTensor);
188 SHOULD_NOT_DO_GRADIENT(ReceiveTensor);
189 
190 // Communication operators do not have default engines.
191 REGISTER_CPU_OPERATOR(CreateCommonWorld, NoDefaultEngineOp<CPUContext>);
192 REGISTER_CPU_OPERATOR(CloneCommonWorld, NoDefaultEngineOp<CPUContext>);
193 REGISTER_CPU_OPERATOR(DestroyCommonWorld, NoDefaultEngineOp<CPUContext>);
194 REGISTER_CPU_OPERATOR(Broadcast, NoDefaultEngineOp<CPUContext>);
195 REGISTER_CPU_OPERATOR(Reduce, NoDefaultEngineOp<CPUContext>);
196 REGISTER_CPU_OPERATOR(Allgather, NoDefaultEngineOp<CPUContext>);
197 REGISTER_CPU_OPERATOR(Allreduce, NoDefaultEngineOp<CPUContext>);
198 REGISTER_CPU_OPERATOR(ReduceScatter, NoDefaultEngineOp<CPUContext>);
199 REGISTER_CPU_OPERATOR(Barrier, NoDefaultEngineOp<CPUContext>);
200 REGISTER_CPU_OPERATOR(SendTensor, NoDefaultEngineOp<CPUContext>);
201 REGISTER_CPU_OPERATOR(ReceiveTensor, NoDefaultEngineOp<CPUContext>);
202 
203 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13