recv_op.cc 3.2 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"
武毅 已提交
22

Y
Yi Wang 已提交
23
#include "paddle/fluid/operators/detail/grpc_client.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
  RecvOp(const std::string& type, const framework::VariableNameMap& inputs,
T
typhoonzero 已提交
32 33 34 35
         const framework::VariableNameMap& outputs,
         const framework::AttributeMap& attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}

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

    platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
    auto& ctx = *pool.Get(place);
    // For profiling
    platform::RecordEvent record_event(Type(), &ctx);

Y
Yancey1989 已提交
48 49 50 51 52
    PADDLE_ENFORCE_NOT_NULL(scope.FindVar(client_var_name),
                            "Can not find variable '%s' in the scope.",
                            client_var_name);
    auto* client_var = scope.FindVar(client_var_name);
    detail::RPCClient* rpc_client = client_var->GetMutable<detail::RPCClient>();
T
typhoonzero 已提交
53 54

    for (size_t i = 0; i < outs.size(); i++) {
Y
Yancey1989 已提交
55 56
      VLOG(3) << "getting " << outs[i] << " from " << epmap[i];
      rpc_client->AsyncGetVariable(epmap[i], ctx, scope, outs[i]);
武毅 已提交
57
    }
Y
Yancey1989 已提交
58
    if (sync_mode) {
Y
Yancey1989 已提交
59 60
      PADDLE_ENFORCE(rpc_client->Wait());
    }
武毅 已提交
61 62 63 64 65
  }
};

class RecvOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
66
  void Make() {
T
typhoonzero 已提交
67
    AddOutput("Out", "(Tensor) Variables to get from server.").AsDuplicable();
Y
Yancey1989 已提交
68 69 70
    AddOutput("RPCClient",
              "(RPCClient) The RPC client object which is"
              "initialized at most once.");
武毅 已提交
71 72 73
    AddComment(R"DOC(
Recv operator

74
This operator can get variables from server side.
武毅 已提交
75
)DOC");
T
typhoonzero 已提交
76 77 78 79
    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 已提交
80
        .SetDefault({});
Y
Yancey1989 已提交
81
    AddAttr<int>("sync_mode",
Y
Yancey1989 已提交
82 83 84
                 "(int, default 0)"
                 "sync recv or async recv.")
        .SetDefault(0);
武毅 已提交
85 86 87 88 89 90 91 92 93
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

REGISTER_OPERATOR(recv, ops::RecvOp, ops::RecvOpMaker);