send_op.cc 3.6 KB
Newer Older
L
Luo Tao 已提交
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
武毅 已提交
2

L
Luo Tao 已提交
3 4 5
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
武毅 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
武毅 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
武毅 已提交
14 15 16 17 18 19 20 21

#include <ostream>

#include "paddle/framework/data_type.h"
#include "paddle/framework/framework.pb.h"
#include "paddle/framework/lod_tensor.h"
#include "paddle/framework/op_registry.h"

G
gongweibao 已提交
22 23
#include <future>
#include "paddle/operators/detail/grpc_client.h"
武毅 已提交
24 25 26 27 28 29

namespace paddle {
namespace operators {

class SendOp : public framework::OperatorBase {
 public:
G
gongweibao 已提交
30 31 32 33
  SendOp(const std::string& type, const framework::VariableNameMap& inputs,
         const framework::VariableNameMap& outputs,
         const framework::AttributeMap& attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}
T
typhoonzero 已提交
34

G
gongweibao 已提交
35
  void Run(const framework::Scope& scope,
Y
Yancey1989 已提交
36
           const platform::Place& place) const override {
T
typhoonzero 已提交
37
    auto ins = Inputs("X");
T
typhoonzero 已提交
38
    auto outs = Outputs("Out");
T
typhoonzero 已提交
39
    std::vector<std::string> epmap = Attr<std::vector<std::string>>("epmap");
T
typhoonzero 已提交
40
    bool do_get = Attr<bool>("DoGet");
Y
Yancey 已提交
41 42
    std::vector<std::string> endpoints =
        Attr<std::vector<std::string>>("endpoints");
G
gongweibao 已提交
43

Y
Yancey1989 已提交
44 45
    platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
    auto& ctx = *pool.Get(place);
G
gongweibao 已提交
46
    for (size_t i = 0; i < ins.size(); i++) {
Y
Yancey 已提交
47
      VLOG(3) << "sending " << ins[i] << " to " << epmap[i];
G
gongweibao 已提交
48
      client_.AsyncSendVariable(epmap[i], ctx, scope, ins[i]);
武毅 已提交
49
    }
50
    PADDLE_ENFORCE(client_.Wait());
G
gongweibao 已提交
51

Y
Yancey 已提交
52 53 54 55 56 57
    for (auto& ep : endpoints) {
      VLOG(3) << "batch barrier, ep: " << ep;
      client_.AsyncSendBatchBarrier(ep);
    }
    PADDLE_ENFORCE(client_.Wait());

T
typhoonzero 已提交
58 59
    if (do_get) {
      for (size_t i = 0; i < outs.size(); i++) {
60
        VLOG(3) << "getting " << outs[i] << " from " << epmap[i];
T
typhoonzero 已提交
61 62
        client_.AsyncGetVariable(epmap[i], ctx, scope, outs[i]);
      }
63
      PADDLE_ENFORCE(client_.Wait());
武毅 已提交
64 65 66
    }
  }

G
gongweibao 已提交
67
 private:
68
  // TODO(typhoonzero): put RPCClient in a Variable.
G
gongweibao 已提交
69
  mutable detail::RPCClient client_;
武毅 已提交
70 71 72 73
};

class SendOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
G
gongweibao 已提交
74
  SendOpMaker(OpProto* proto, OpAttrChecker* op_checker)
武毅 已提交
75
      : OpProtoAndCheckerMaker(proto, op_checker) {
76 77
    AddInput("X", "(Tensor) Input tensor to be sent").AsDuplicable();
    AddOutput("Out", "(Tensor) Output tensor to be received from server")
T
typhoonzero 已提交
78
        .AsDuplicable();
武毅 已提交
79
    AddComment(R"DOC(
A
Abhinav Arora 已提交
80
Send operator
武毅 已提交
81

82
This operator will send tensor to recv_op at the parameter server.
武毅 已提交
83
)DOC");
T
typhoonzero 已提交
84 85
    AddAttr<std::vector<std::string>>("endpoints",
                                      "(string vector, default 127.0.0.1:6164)"
T
typhoonzero 已提交
86 87
                                      "Server endpoints to send variables to.")
        .SetDefault({});
T
typhoonzero 已提交
88 89 90
    AddAttr<std::vector<std::string>>("epmap",
                                      "(string vector, default 127.0.0.1:6164)"
                                      "Server endpoints in the order of input "
T
typhoonzero 已提交
91 92
                                      "variables for mapping")
        .SetDefault({});
T
typhoonzero 已提交
93 94 95 96
    AddAttr<bool>("DoGet",
                  "(bool, default true)"
                  "Whether do GetVariable call after send")
        .SetDefault(true);
武毅 已提交
97 98 99 100 101 102 103 104 105
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

REGISTER_OPERATOR(send, ops::SendOp, ops::SendOpMaker);