rpc_server_test.cc 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
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
    http://www.apache.org/licenses/LICENSE-2.0
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. */

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

#include "gtest/gtest.h"
Y
Yancey1989 已提交
20
#include "paddle/fluid/framework/block_desc.h"
Y
Yancey1989 已提交
21 22 23
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"

T
tangwei12 已提交
24
#include "paddle/fluid/operators/distributed/barrier_monitor.h"
W
Wu Yi 已提交
25
#include "paddle/fluid/operators/distributed/distributed.h"
26
#include "paddle/fluid/operators/distributed/heart_beat_monitor.h"
27 28 29
#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"
30

31 32
namespace framework = paddle::framework;
namespace platform = paddle::platform;
33
namespace distributed = paddle::operators::distributed;
34

35
USE_NO_KERNEL_OP(lookup_sparse_table);
Y
Yancey1989 已提交
36

37 38
std::unique_ptr<distributed::RPCServer> g_rpc_service;
std::unique_ptr<distributed::RequestHandler> g_req_handler;
39

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

  framework::VariableNameMap input({{"W", {"w"}}, {"Ids", {"ids"}}});
  framework::VariableNameMap output({{"Output", {"out"}}});
  auto op = block->AppendOp();
47
  op->SetType("lookup_sparse_table");
Y
Yancey1989 已提交
48 49 50
  op->SetInput("W", {"w"});
  op->SetInput("Ids", {"ids"});
  op->SetOutput("Out", {"out"});
Y
Yancey1989 已提交
51 52

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

Y
Yancey1989 已提交
56 57 58
  return block;
}

Y
Yancey1989 已提交
59 60
void CreateVarsOnScope(framework::Scope* scope, platform::CPUPlace* place) {
  auto w_var = scope->Var("w");
Y
Yancey1989 已提交
61
  w_var->GetMutable<framework::SelectedRows>();
Y
Yancey1989 已提交
62

Y
Yancey1989 已提交
63
  auto out_var = scope->Var("out");
64
  out_var->GetMutable<framework::LoDTensor>();
Y
Yancey1989 已提交
65

Y
Yancey1989 已提交
66
  auto ids_var = scope->Var("ids");
67
  ids_var->GetMutable<framework::LoDTensor>();
Y
Yancey1989 已提交
68 69
}

Y
Yancey1989 已提交
70 71
void InitTensorsOnClient(framework::Scope* scope, platform::CPUPlace* place,
                         int64_t rows_numel) {
Y
Yancey1989 已提交
72
  CreateVarsOnScope(scope, place);
73 74 75 76
  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 已提交
77 78
}

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

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

  for (int64_t i = 0; i < w_value->numel(); ++i) {
Y
Yancey1989 已提交
90 91 92
    ptr[i] = static_cast<float>(i / 10);
  }
}
Y
Yancey1989 已提交
93

Y
Yancey1989 已提交
94
void StartServer(const std::string& rpc_name) {
Y
Yancey1989 已提交
95 96 97 98 99
  framework::ProgramDesc program;
  framework::Scope scope;
  platform::CPUPlace place;
  framework::Executor exe(place);
  platform::CPUDeviceContext ctx(place);
Y
Yancey1989 已提交
100
  auto* block = AppendPrefetchBlcok(&program);
101 102 103
  std::string in_var_name("ids");
  std::vector<int> prefetch_block_ids{block->ID()};
  auto prepared = exe.Prepare(program, prefetch_block_ids);
Y
Yancey1989 已提交
104
  InitTensorsOnServer(&scope, &place, 10);
Y
Yancey1989 已提交
105

106 107 108 109
  std::unordered_map<std::string,
                     std::shared_ptr<framework::ExecutorPrepareContext>>
      prefetch_var_name_to_prepared;
  prefetch_var_name_to_prepared[in_var_name] = prepared[0];
Y
Yancey1989 已提交
110

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

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

  distributed::HeartBeatMonitor::Init(2, true, "w@grad");
T
tangwei12 已提交
120
  distributed::BarrierMonitor::Init(2);
121

122 123 124
  g_req_handler->SetRPCServer(g_rpc_service.get());

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

127
  server_thread.join();
128 129
}

130
TEST(PREFETCH, CPU) {
131 132
  setenv("http_proxy", "", 1);
  setenv("https_proxy", "", 1);
1
123malin 已提交
133 134
  g_req_handler.reset(new distributed::RequestPrefetchHandler(
      distributed::DistributedMode::kSync));
G
gongweibao 已提交
135
  g_rpc_service.reset(new RPCSERVER_T("127.0.0.1:0", 1));
136
  distributed::RPCClient* client =
W
Wu Yi 已提交
137
      distributed::RPCClient::GetInstance<RPCCLIENT_T>(0);
138

Y
Yancey1989 已提交
139
  std::thread server_thread(StartServer, distributed::kRequestPrefetch);
140 141 142 143 144
  g_rpc_service->WaitServerReady();

  int port = g_rpc_service->GetSelectedPort();
  std::string ep = paddle::string::Sprintf("127.0.0.1:%d", port);

145 146 147
  framework::Scope scope;
  platform::CPUPlace place;
  platform::CPUDeviceContext ctx(place);
148 149 150 151 152 153 154
  {
    // create var on local scope
    int64_t rows_numel = 5;
    InitTensorsOnClient(&scope, &place, rows_numel);
    std::string in_var_name("ids");
    std::string out_var_name("out");

G
gongweibao 已提交
155
    client->AsyncPrefetchVar(ep, ctx, scope, in_var_name, out_var_name);
W
Wu Yi 已提交
156
    client->Wait();
157
    auto var = scope.Var(out_var_name);
158 159
    auto value = var->GetMutable<framework::LoDTensor>();
    auto ptr = value->mutable_data<float>(place);
160 161

    for (int64_t i = 0; i < rows_numel; ++i) {
162
      EXPECT_EQ(ptr[0 + i * value->dims()[1]], static_cast<float>(i * 2));
163
    }
Y
Yancey1989 已提交
164
  }
165

T
tangwei12 已提交
166 167 168
  auto* barrier = distributed::BarrierMonitor::GetInstance();
  barrier->Stop();

W
Wu Yi 已提交
169
  g_rpc_service->ShutDown();
170 171 172 173
  server_thread.join();
  LOG(INFO) << "begin reset";
  g_rpc_service.reset(nullptr);
  g_req_handler.reset(nullptr);
174
}
Y
Yancey1989 已提交
175 176

TEST(COMPLETE, CPU) {
177 178
  setenv("http_proxy", "", 1);
  setenv("https_proxy", "", 1);
T
tangwei12 已提交
179 180
  g_req_handler.reset(new distributed::RequestNotifyHandler(
      distributed::DistributedMode::kSync, -1));
Y
Yancey1989 已提交
181
  g_rpc_service.reset(new RPCSERVER_T("127.0.0.1:0", 2));
T
tangwei12 已提交
182

Y
Yancey1989 已提交
183
  distributed::RPCClient* client =
W
Wu Yi 已提交
184
      distributed::RPCClient::GetInstance<RPCCLIENT_T>(0);
Y
Yancey1989 已提交
185
  PADDLE_ENFORCE(client != nullptr);
T
tangwei12 已提交
186
  std::thread server_thread(StartServer, distributed::kRequestNotify);
Y
Yancey1989 已提交
187 188 189 190 191 192
  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 已提交
193 194 195 196
  auto* barrier = distributed::BarrierMonitor::GetInstance();
  EXPECT_EQ(barrier->GetWorkerNum(), 1);

  barrier->Stop();
Y
Yancey1989 已提交
197 198 199 200 201 202

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