rpc_server_test.cc 5.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
T
tangwei12 已提交
2

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
T
tangwei12 已提交
6

7
    http://www.apache.org/licenses/LICENSE-2.0
T
tangwei12 已提交
8

9 10 11 12 13 14
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. */

15
#include <stdlib.h>
16
#include <unistd.h>
17
#include <memory>
18
#include <string>
Y
Yancey1989 已提交
19
#include <thread>  // NOLINT
20
#include <unordered_map>
21 22

#include "gtest/gtest.h"
Y
Yancey1989 已提交
23
#include "paddle/fluid/framework/block_desc.h"
Y
Yancey1989 已提交
24 25 26
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"

W
Wu Yi 已提交
27
#include "paddle/fluid/operators/distributed/distributed.h"
28
#include "paddle/fluid/operators/distributed/heart_beat_monitor.h"
29 30 31
#include "paddle/fluid/operators/distributed/request_handler_impl.h"
#include "paddle/fluid/operators/distributed/rpc_client.h"
#include "paddle/fluid/operators/distributed/rpc_server.h"
32

33 34
namespace framework = paddle::framework;
namespace platform = paddle::platform;
35
namespace distributed = paddle::operators::distributed;
36

37
USE_NO_KERNEL_OP(lookup_sparse_table_read);
Y
Yancey1989 已提交
38

39 40
std::unique_ptr<distributed::RPCServer> g_rpc_service;
std::unique_ptr<distributed::RequestHandler> g_req_handler;
41

Y
Yancey1989 已提交
42 43 44
framework::BlockDesc* AppendPrefetchBlcok(framework::ProgramDesc* program) {
  auto root_block = program->MutableBlock(0);
  auto* block = program->AppendBlock(*root_block);
Y
Yancey1989 已提交
45 46 47 48

  framework::VariableNameMap input({{"W", {"w"}}, {"Ids", {"ids"}}});
  framework::VariableNameMap output({{"Output", {"out"}}});
  auto op = block->AppendOp();
49
  op->SetType("lookup_sparse_table_read");
Y
Yancey1989 已提交
50 51 52
  op->SetInput("W", {"w"});
  op->SetInput("Ids", {"ids"});
  op->SetOutput("Out", {"out"});
53 54
  op->SetAttr("tablename", {"w"});
  op->SetAttr("value_names", {"Param"});
Y
Yancey1989 已提交
55 56

  auto& out = *root_block->Var("out");
57
  out.SetType(framework::proto::VarType::LOD_TENSOR);
Y
Yancey1989 已提交
58 59
  out.SetShape({10, 10});

Y
Yancey1989 已提交
60 61 62
  return block;
}

Y
Yancey1989 已提交
63 64
void CreateVarsOnScope(framework::Scope* scope, platform::CPUPlace* place) {
  auto w_var = scope->Var("w");
Y
Yancey1989 已提交
65
  w_var->GetMutable<framework::SelectedRows>();
Y
Yancey1989 已提交
66

Y
Yancey1989 已提交
67
  auto out_var = scope->Var("out");
68
  out_var->GetMutable<framework::LoDTensor>();
Y
Yancey1989 已提交
69

Y
Yancey1989 已提交
70
  auto ids_var = scope->Var("ids");
71
  ids_var->GetMutable<framework::LoDTensor>();
Y
Yancey1989 已提交
72 73
}

Y
Yancey1989 已提交
74 75
void InitTensorsOnClient(framework::Scope* scope, platform::CPUPlace* place,
                         int64_t rows_numel) {
Y
Yancey1989 已提交
76
  CreateVarsOnScope(scope, place);
77 78 79 80
  auto ids_var = scope->Var("ids")->GetMutable<framework::LoDTensor>();
  int64_t* ids_ptr =
      ids_var->mutable_data<int64_t>(framework::DDim({rows_numel, 1}), *place);
  for (int64_t i = 0; i < rows_numel; ++i) ids_ptr[i] = i * 2;
Y
Yancey1989 已提交
81 82
}

Y
Yancey1989 已提交
83 84
void InitTensorsOnServer(framework::Scope* scope, platform::CPUPlace* place,
                         int64_t rows_numel) {
Y
Yancey1989 已提交
85
  CreateVarsOnScope(scope, place);
Y
Yancey1989 已提交
86 87 88
  auto w = scope->Var("w")->GetMutable<framework::SelectedRows>();
  auto w_value = w->mutable_value();
  w_value->Resize({rows_numel, 10});
89
  for (int64_t i = 0; i < rows_numel; ++i) w->AutoGrownIndex(i, true);
Y
Yancey1989 已提交
90 91 92 93

  auto ptr = w_value->mutable_data<float>(*place);

  for (int64_t i = 0; i < w_value->numel(); ++i) {
Y
Yancey1989 已提交
94 95 96
    ptr[i] = static_cast<float>(i / 10);
  }
}
Y
Yancey1989 已提交
97

Y
Yancey1989 已提交
98
void StartServer(const std::string& rpc_name) {
Y
Yancey1989 已提交
99 100 101 102 103 104
  framework::ProgramDesc program;
  framework::Scope scope;
  platform::CPUPlace place;
  framework::Executor exe(place);
  platform::CPUDeviceContext ctx(place);

105 106 107
  std::unordered_map<std::string,
                     std::shared_ptr<framework::ExecutorPrepareContext>>
      prefetch_var_name_to_prepared;
Y
Yancey1989 已提交
108

109
  g_req_handler->SetProgram(&program);
110
  g_req_handler->SetPrefetchPreparedCtx(&prefetch_var_name_to_prepared);
111 112 113 114
  g_req_handler->SetDevCtx(&ctx);
  g_req_handler->SetScope(&scope);
  g_req_handler->SetExecutor(&exe);

Y
Yancey1989 已提交
115
  g_rpc_service->RegisterRPC(rpc_name, g_req_handler.get());
116 117 118

  distributed::HeartBeatMonitor::Init(2, true, "w@grad");

119 120 121
  g_req_handler->SetRPCServer(g_rpc_service.get());

  std::thread server_thread(
122
      std::bind(&distributed::RPCServer::StartServer, g_rpc_service.get()));
Y
Yancey1989 已提交
123

124
  server_thread.join();
125 126
}

Y
Yancey1989 已提交
127
TEST(COMPLETE, CPU) {
128 129
  setenv("http_proxy", "", 1);
  setenv("https_proxy", "", 1);
T
tangwei12 已提交
130 131
  g_req_handler.reset(
      new distributed::RequestSendHandler(distributed::DistributedMode::kSync));
Y
Yancey1989 已提交
132 133
  g_rpc_service.reset(new RPCSERVER_T("127.0.0.1:0", 2));
  distributed::RPCClient* client =
W
Wu Yi 已提交
134
      distributed::RPCClient::GetInstance<RPCCLIENT_T>(0);
Y
Yancey1989 已提交
135
  PADDLE_ENFORCE(client != nullptr);
T
tangwei12 已提交
136
  std::thread server_thread(StartServer, distributed::kRequestSend);
Y
Yancey1989 已提交
137 138 139 140 141 142
  g_rpc_service->WaitServerReady();
  int port = g_rpc_service->GetSelectedPort();
  std::string ep = paddle::string::Sprintf("127.0.0.1:%d", port);
  client->AsyncSendComplete(ep);
  client->Wait();

T
tangwei12 已提交
143
  EXPECT_EQ(g_rpc_service->GetClientNum(), 1);
Y
Yancey1989 已提交
144 145 146 147 148 149

  g_rpc_service->ShutDown();
  server_thread.join();
  g_rpc_service.reset(nullptr);
  g_req_handler.reset(nullptr);
}