grpc_server_test.cc 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#include <unistd.h>
#include <string>
Y
Yancey1989 已提交
17
#include <thread>  // NOLINT
18 19 20 21

#include "gtest/gtest.h"
#include "paddle/fluid/operators/detail/grpc_client.h"
#include "paddle/fluid/operators/detail/grpc_server.h"
G
gongweibao 已提交
22
#include "paddle/fluid/operators/detail/rpc_client.h"
23

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

28 29
#include "paddle/fluid/operators/detail/request_handler_impl.h"

30 31 32 33
namespace framework = paddle::framework;
namespace platform = paddle::platform;
namespace detail = paddle::operators::detail;

Y
Yancey1989 已提交
34 35
USE_OP(lookup_table);

36 37
std::unique_ptr<detail::AsyncGRPCServer> g_rpc_service;
std::unique_ptr<detail::RequestHandler> g_req_handler;
38

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

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

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

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

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

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

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

Y
Yancey1989 已提交
69 70
void InitTensorsOnClient(framework::Scope* scope, platform::CPUPlace* place,
                         int64_t rows_numel) {
Y
Yancey1989 已提交
71
  CreateVarsOnScope(scope, place);
Y
Yancey1989 已提交
72 73 74 75 76
  auto ids_var = scope->Var("ids")->GetMutable<framework::SelectedRows>();
  auto rows = ids_var->mutable_rows();
  for (int64_t i = 0; i < rows_numel; ++i) rows->push_back(i * 2);
  ids_var->mutable_value()->Resize({rows_numel, 1});
  ids_var->mutable_value()->mutable_data<float>(*place);
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 85 86 87 88 89 90
  auto w = scope->Var("w")->GetMutable<framework::SelectedRows>();
  auto rows = w->mutable_rows();
  for (int64_t i = 0; i < rows_numel; ++i) rows->push_back(i);
  auto w_value = w->mutable_value();
  w_value->Resize({rows_numel, 10});

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

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

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

107 108 109 110
  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];
111
  g_req_handler->SetProgram(&program);
112
  g_req_handler->SetPrefetchPreparedCtx(&prefetch_var_name_to_prepared);
113 114 115 116 117 118 119 120 121
  g_req_handler->SetDevCtx(&ctx);
  g_req_handler->SetScope(&scope);
  g_req_handler->SetExecutor(&exe);

  g_rpc_service->RegisterRPC(detail::kRequestPrefetch, g_req_handler.get());
  g_req_handler->SetRPCServer(g_rpc_service.get());

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

123
  server_thread.join();
124 125
}

126 127 128 129 130 131 132
TEST(PREFETCH, CPU) {
  g_req_handler.reset(new detail::RequestPrefetchHandler(true));
  g_rpc_service.reset(new detail::AsyncGRPCServer("127.0.0.1:0", 1));

  std::thread server_thread(StartServer);
  g_rpc_service->WaitServerReady();

G
gongweibao 已提交
133 134
  detail::RPCClient* client =
      detail::RPCClient::GetInstance<detail::GRPCClient>();
135 136 137
  int port = g_rpc_service->GetSelectedPort();
  std::string ep = paddle::string::Sprintf("127.0.0.1:%d", port);

138 139 140
  framework::Scope scope;
  platform::CPUPlace place;
  platform::CPUDeviceContext ctx(place);
141 142 143 144 145 146 147
  {
    // 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 已提交
148
    client->AsyncPrefetchVar(ep, ctx, scope, in_var_name, out_var_name);
W
Wu Yi 已提交
149
    client->Wait();
150 151 152 153 154 155 156
    auto var = scope.Var(out_var_name);
    auto value = var->GetMutable<framework::SelectedRows>()->value();
    auto ptr = value.mutable_data<float>(place);

    for (int64_t i = 0; i < rows_numel; ++i) {
      EXPECT_EQ(ptr[0 + i * value.dims()[1]], static_cast<float>(i * 2));
    }
Y
Yancey1989 已提交
157
  }
158

W
Wu Yi 已提交
159
  g_rpc_service->ShutDown();
160 161 162 163
  server_thread.join();
  LOG(INFO) << "begin reset";
  g_rpc_service.reset(nullptr);
  g_req_handler.reset(nullptr);
164
}