recv_op.cc 5.7 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
#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"
W
Wu Yi 已提交
22
#include "paddle/fluid/operators/distributed/distributed.h"
Q
Qiao Longfei 已提交
23
#include "paddle/fluid/operators/distributed/parameter_recv.h"
Y
Yancey1989 已提交
24
#include "paddle/fluid/platform/profiler.h"
T
typhoonzero 已提交
25

武毅 已提交
26 27 28 29 30
namespace paddle {
namespace operators {

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

36 37
  void RunImpl(const framework::Scope &scope,
               const platform::Place &place) const override {
T
typhoonzero 已提交
38
    std::vector<std::string> epmap = Attr<std::vector<std::string>>("epmap");
39 40
    std::vector<std::string> varnames =
        Attr<std::vector<std::string>>("varnames");
Y
Yancey1989 已提交
41
    int sync_mode = Attr<int>("sync_mode");
42 43
    auto outs = Outputs("Out");
    bool with_barrier = Attr<bool>("with_barrier");
Y
Yancey1989 已提交
44

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

48
    distributed::RPCClient *rpc_client =
W
Wu Yi 已提交
49 50
        distributed::RPCClient::GetInstance<RPCCLIENT_T>(
            Attr<int>("trainer_id"));
T
typhoonzero 已提交
51

Q
Qiao Longfei 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    std::vector<std::string> recv_varnames =
        Attr<std::vector<std::string>>("recv_varnames");

    if (recv_varnames.size() > 0) {
      framework::RuntimeContext ctx(Inputs(), Outputs(), scope);
      platform::DeviceContextPool &pool =
          platform::DeviceContextPool::Instance();
      auto *dev_ctx = pool.Get(place);
      auto exe_ctx = framework::ExecutionContext(*this, scope, *dev_ctx, ctx);
      auto recv_functor = distributed::ParameterRecv<float>();
      recv_functor(outs[0], recv_varnames, epmap, exe_ctx, scope);
    } else {
      if (with_barrier) {
        std::vector<distributed::VarHandlePtr> rets;
        for (size_t i = 0; i < outs.size(); i++) {
          std::string varname = varnames.size() == 0 ? outs[i] : varnames[i];
          VLOG(4) << "recv " << outs[i] << " from " << epmap[i] << " with "
                  << varname << " and with AsyncGetVar";
          rets.push_back(
              rpc_client->AsyncGetVar(epmap[i], ctx, scope, varname, outs[i]));
        }
        if (sync_mode) {
          for (size_t i = 0; i < rets.size(); i++) {
            PADDLE_ENFORCE(rets[i]->Wait(), "internal error in RPCClient");
          }
        }
      } else {
        std::vector<distributed::VarHandlePtr> rets;
        for (size_t i = 0; i < outs.size(); i++) {
          std::string varname = varnames.size() == 0 ? outs[i] : varnames[i];
          VLOG(4) << "recv " << outs[i] << " from " << epmap[i] << " with "
                  << varname << " and with AsyncGetVarNoBarrier";
          rets.push_back(rpc_client->AsyncGetVarNoBarrier(epmap[i], ctx, scope,
                                                          varname, outs[i]));
        }
87 88 89 90
        for (size_t i = 0; i < rets.size(); i++) {
          PADDLE_ENFORCE(rets[i]->Wait(), "internal error in RPCClient");
        }
      }
Y
Yancey1989 已提交
91
    }
武毅 已提交
92 93 94 95 96
  }
};

class RecvOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
97
  void Make() {
98 99
    AddInput("X", "(Any) Dummy inputs, used for control dependency")
        .AsDuplicable();
T
typhoonzero 已提交
100
    AddOutput("Out", "(Tensor) Variables to get from server.").AsDuplicable();
武毅 已提交
101 102 103
    AddComment(R"DOC(
Recv operator

104
This operator can get variables from server side.
武毅 已提交
105
)DOC");
T
typhoonzero 已提交
106 107 108 109
    AddAttr<std::vector<std::string>>("epmap",
                                      "(string vector, default 127.0.0.1:6164)"
                                      "Server endpoints in the order of input "
                                      "variables for mapping")
Y
Yancey1989 已提交
110
        .SetDefault({});
W
Wu Yi 已提交
111
    AddAttr<int>("trainer_id", "trainer id from 0 ~ worker_num.").SetDefault(0);
Y
Yancey1989 已提交
112
    AddAttr<int>("sync_mode",
Y
Yancey1989 已提交
113 114 115
                 "(int, default 0)"
                 "sync recv or async recv.")
        .SetDefault(0);
116 117 118 119 120 121 122 123 124 125 126
    AddAttr<bool>("with_barrier",
                  "(bool, default True) if with_barrier=False, will use "
                  "AsyncGetVarNoBarrier get variable from pserver immediately")
        .SetDefault(true);
    AddAttr<std::vector<std::string>>(
        "varnames",
        "(string vector, default {}) "
        "sometimes we need to put received var in another name "
        "for example: we need var named 'moment_1@127.0.0.1:1001', "
        "and it real name on parameter server is 'moment_1'. ")
        .SetDefault({});
Q
Qiao Longfei 已提交
127 128 129 130 131
    AddAttr<std::vector<std::string>>(
        "recv_varnames",
        "(vector<string>) "
        "the splited parameter varnames to be recved from pserver")
        .SetDefault(std::vector<std::string>{});
武毅 已提交
132 133 134
  }
};

135 136
class RecvOpShapeInference : public framework::InferShapeBase {
 public:
137
  void operator()(framework::InferShapeContext *ctx) const override {}
138 139
};

武毅 已提交
140 141 142 143 144
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

145 146
REGISTER_OPERATOR(recv, ops::RecvOp, paddle::framework::EmptyGradOpMaker,
                  ops::RecvOpMaker, ops::RecvOpShapeInference);