send_op.cc 4.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
武毅 已提交
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
#include <future>  // NOLINT
武毅 已提交
16 17
#include <ostream>

Y
Yi Wang 已提交
18 19 20 21 22
#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/detail/grpc_client.h"
Q
Qiao Longfei 已提交
23
#include "paddle/fluid/operators/send_recv_util.h"
24
#include "paddle/fluid/platform/profiler.h"
武毅 已提交
25 26 27 28 29 30

namespace paddle {
namespace operators {

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

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

Q
qiaolongfei 已提交
44 45
    bool sync_mode = Attr<bool>("sync_mode");

Y
Yancey1989 已提交
46 47
    platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
    auto& ctx = *pool.Get(place);
T
typhoonzero 已提交
48

49 50 51
    // For profiling
    platform::RecordEvent record_event(Type(), &ctx);

Y
Yancey1989 已提交
52
    auto rpc_client = detail::RPCClient::GetInstance();
T
typhoonzero 已提交
53

G
gongweibao 已提交
54
    for (size_t i = 0; i < ins.size(); i++) {
55
      if (NeedSend(scope, ins[i])) {
T
typhoonzero 已提交
56
        VLOG(3) << "sending " << ins[i] << " to " << epmap[i];
57 58 59 60
        rpc_client->AsyncSendVariable(epmap[i], ctx, scope, ins[i]);
      } else {
        VLOG(3) << "don't send no-initialied variable: " << ins[i];
      }
武毅 已提交
61
    }
W
Wu Yi 已提交
62
    rpc_client->Wait();
G
gongweibao 已提交
63

Q
qiaolongfei 已提交
64 65 66 67 68
    if (sync_mode) {
      for (auto& ep : endpoints) {
        VLOG(3) << "batch barrier, ep: " << ep;
        rpc_client->AsyncSendBatchBarrier(ep);
      }
W
Wu Yi 已提交
69
      rpc_client->Wait();
Y
Yancey 已提交
70 71
    }

T
typhoonzero 已提交
72
    if (outs.size() > 0) {
T
typhoonzero 已提交
73
      for (size_t i = 0; i < outs.size(); i++) {
T
typhoonzero 已提交
74
        VLOG(2) << "getting " << outs[i] << " from " << epmap[i];
Y
Yancey1989 已提交
75
        rpc_client->AsyncGetVariable(epmap[i], ctx, scope, outs[i]);
T
typhoonzero 已提交
76
      }
W
Wu Yi 已提交
77
      rpc_client->Wait();
78 79
      // tell pservers that current trainer have called fetch
      for (auto& ep : endpoints) {
T
typhoonzero 已提交
80
        VLOG(2) << "send fetch barrier, ep: " << ep;
81 82
        rpc_client->AsyncSendFetchBarrier(ep);
      }
W
Wu Yi 已提交
83
      rpc_client->Wait();
武毅 已提交
84 85 86 87 88 89
    }
  }
};

class SendOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
90
  void Make() {
91 92
    AddInput("X", "(Tensor) Input tensor to be sent").AsDuplicable();
    AddOutput("Out", "(Tensor) Output tensor to be received from server")
T
typhoonzero 已提交
93
        .AsDuplicable();
武毅 已提交
94
    AddComment(R"DOC(
A
Abhinav Arora 已提交
95
Send operator
武毅 已提交
96

97
This operator will send tensor to recv_op at the parameter server.
武毅 已提交
98
)DOC");
T
typhoonzero 已提交
99 100
    // TODO(typhoonzero): remove this attr generate de-duplicated vector from
    // epmap when initializing.
T
typhoonzero 已提交
101 102
    AddAttr<std::vector<std::string>>("endpoints",
                                      "(string vector, default 127.0.0.1:6164)"
T
typhoonzero 已提交
103 104
                                      "Server endpoints to send variables to.")
        .SetDefault({});
T
typhoonzero 已提交
105 106 107
    AddAttr<std::vector<std::string>>("epmap",
                                      "(string vector, default 127.0.0.1:6164)"
                                      "Server endpoints in the order of input "
T
typhoonzero 已提交
108 109
                                      "variables for mapping")
        .SetDefault({});
Q
qiaolongfei 已提交
110
    AddAttr<bool>("sync_mode", "work in sync_mode or not").SetDefault(true);
武毅 已提交
111 112 113
  }
};

T
typhoonzero 已提交
114 115 116 117 118
class SendOpShapeInference : public framework::InferShapeBase {
 public:
  void operator()(framework::InferShapeContext* ctx) const override {}
};

武毅 已提交
119 120 121 122 123
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

T
typhoonzero 已提交
124
REGISTER_OPERATOR(send, ops::SendOp, paddle::framework::EmptyGradOpMaker,
Y
Yancey1989 已提交
125
                  ops::SendOpMaker, ops::SendOpShapeInference);