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