send_op.cc 5.9 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>

18
#include "paddle/fluid/framework/blocking_queue.h"
Y
Yi Wang 已提交
19 20 21
#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
Q
can run  
Qiao Longfei 已提交
22
#include "paddle/fluid/operators/distributed/communicator.h"
W
Wu Yi 已提交
23
#include "paddle/fluid/operators/distributed/distributed.h"
Q
Qiao Longfei 已提交
24
#include "paddle/fluid/operators/distributed/parameter_send.h"
25
#include "paddle/fluid/operators/distributed/rpc_common.h"
W
Wu Yi 已提交
26
#include "paddle/fluid/operators/distributed_ops/send_recv_util.h"
27
#include "paddle/fluid/platform/profiler.h"
武毅 已提交
28 29 30 31 32 33

namespace paddle {
namespace operators {

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

T
typhoonzero 已提交
39 40
  void RunImpl(const framework::Scope& scope,
               const platform::Place& place) const override {
T
typhoonzero 已提交
41
    auto ins = Inputs("X");
G
gongweibao 已提交
42

Q
Qiao Longfei 已提交
43
    auto epmap = Attr<std::vector<std::string>>("epmap");
Q
Qiao Longfei 已提交
44
    auto trainer_id = Attr<int>("trainer_id");
Q
qiaolongfei 已提交
45

Q
Qiao Longfei 已提交
46
    auto send_varnames = Attr<std::vector<std::string>>("send_varnames");
Q
Qiao Longfei 已提交
47
    auto height_sections = Attr<std::vector<int64_t>>("sections");
1
123malin 已提交
48
    auto use_send_handler = Attr<bool>("use_send_handler");
T
typhoonzero 已提交
49

Q
Qiao Longfei 已提交
50
    if (send_varnames.size() > 0) {
51 52 53 54 55 56
      if (ins.size() > 1) {
        distributed::Communicator::GetInstance()->Send(ins, send_varnames,
                                                       scope);
      } else {
        distributed::Communicator::GetInstance()->Send(ins[0], scope);
      }
Q
Qiao Longfei 已提交
57 58 59 60
    } else {
      platform::DeviceContextPool& pool =
          platform::DeviceContextPool::Instance();
      auto& ctx = *pool.Get(place);
T
typhoonzero 已提交
61

Q
Qiao Longfei 已提交
62
      distributed::RPCClient* rpc_client =
Q
Qiao Longfei 已提交
63
          distributed::RPCClient::GetInstance<RPCCLIENT_T>(trainer_id);
Q
Qiao Longfei 已提交
64 65

      std::vector<distributed::VarHandlePtr> rets;
1
123malin 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      if (use_send_handler) {
        for (size_t i = 0; i < ins.size(); i++) {
          if (NeedSend(scope, ins[i])) {
            VLOG(3) << "sending " << ins[i] << " to " << epmap[i];
            rets.push_back(
                rpc_client->AsyncSendVar(epmap[i], ctx, scope, ins[i]));
          } else {
            VLOG(3) << "don't send no-initialied variable: " << ins[i];
          }
        }
      } else {
        for (size_t i = 0; i < ins.size(); i++) {
          for (size_t j = 0; j < epmap.size(); j++) {
            if (NeedSend(scope, ins[i])) {
              VLOG(3) << "sending " << ins[i] << " to " << epmap[j];
              rets.push_back(rpc_client->AsyncDistributeNotify(epmap[j], ctx,
                                                               scope, ins[i]));
            } else {
              VLOG(3) << "don't send no-initialied variable: " << ins[i];
            }
          }
Q
Qiao Longfei 已提交
87
        }
88
      }
89 90 91 92
      for (size_t i = 0; i < rets.size(); i++) {
        VLOG(7) << "before sync_send " << ins[i] << "from " << epmap[i];
        PADDLE_ENFORCE_NE(rets[i]->Wait(), 0U, "internal error in RPCClient");
        VLOG(7) << "after sync_send " << ins[i] << "from " << epmap[i];
93
      }
武毅 已提交
94 95 96 97 98 99
    }
  }
};

class SendOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
100
  void Make() {
101
    AddInput("X", "(Tensor, SelectedRows) Input variables to be sent")
T
typhoonzero 已提交
102
        .AsDuplicable();
103 104
    AddOutput("Out", "(Any) Dummy outputs, used for control dependency")
        .AsDuplicable();
武毅 已提交
105
    AddComment(R"DOC(
A
Abhinav Arora 已提交
106
Send operator
武毅 已提交
107

108
This operator will send variables to listen_and_serve op at the parameter server.
武毅 已提交
109
)DOC");
W
Wu Yi 已提交
110
    AddAttr<int>("trainer_id", "trainer id from 0 ~ worker_num.").SetDefault(0);
T
typhoonzero 已提交
111 112 113
    AddAttr<std::vector<std::string>>("epmap",
                                      "(string vector, default 127.0.0.1:6164)"
                                      "Server endpoints in the order of input "
T
typhoonzero 已提交
114
                                      "variables for mapping")
115
        .SetDefault({"127.0.0.1:6164"});
Q
Qiao Longfei 已提交
116 117 118 119 120
    AddAttr<std::vector<int64_t>>("sections",
                                  "(vector<int>) "
                                  "the length of each output along the "
                                  "specified axis.")
        .SetDefault(std::vector<int64_t>{});
Q
Qiao Longfei 已提交
121 122 123 124 125 126 127 128 129 130
    AddAttr<std::vector<std::string>>(
        "send_varnames",
        "(vector<string>) "
        "the splited output varnames to send to pserver")
        .SetDefault(std::vector<std::string>{});
    AddAttr<int>("num",
                 "(int, default 0)"
                 "Number of sub-tensors. This must evenly divide "
                 "Input.dims()[axis]")
        .SetDefault(0);
1
123malin 已提交
131 132 133 134 135 136 137 138 139
    AddAttr<bool>("merge_add",
                  "(bool, default 0)"
                  "merge method, true represent add, false represent average")
        .SetDefault(false);
    AddAttr<bool>(
        "use_send_handler",
        "(bool, default 1)"
        "if it's true, use send handler, other wise, use notify handler")
        .SetDefault(true);
武毅 已提交
140 141 142
  }
};

T
typhoonzero 已提交
143 144 145 146 147
class SendOpShapeInference : public framework::InferShapeBase {
 public:
  void operator()(framework::InferShapeContext* ctx) const override {}
};

武毅 已提交
148 149 150 151 152
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

H
hong 已提交
153 154 155 156 157
REGISTER_OPERATOR(
    send, ops::SendOp,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    ops::SendOpMaker, ops::SendOpShapeInference);