grpc_server_test.cc 4.6 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 22

#include "gtest/gtest.h"
#include "paddle/fluid/operators/detail/grpc_client.h"
#include "paddle/fluid/operators/detail/grpc_server.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"

27 28 29 30
namespace framework = paddle::framework;
namespace platform = paddle::platform;
namespace detail = paddle::operators::detail;

Y
Yancey1989 已提交
31 32
USE_OP(lookup_table);

33 34
std::unique_ptr<detail::AsyncGRPCServer> rpc_service_;

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

  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 已提交
46 47

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

Y
Yancey1989 已提交
51 52 53
  return block;
}

Y
Yancey1989 已提交
54 55
void CreateVarsOnScope(framework::Scope* scope, platform::CPUPlace* place) {
  auto w_var = scope->Var("w");
Y
Yancey1989 已提交
56
  w_var->GetMutable<framework::SelectedRows>();
Y
Yancey1989 已提交
57

Y
Yancey1989 已提交
58
  auto out_var = scope->Var("out");
Y
Yancey1989 已提交
59
  out_var->GetMutable<framework::SelectedRows>();
Y
Yancey1989 已提交
60

Y
Yancey1989 已提交
61
  auto ids_var = scope->Var("ids");
Y
Yancey1989 已提交
62
  ids_var->GetMutable<framework::SelectedRows>();
Y
Yancey1989 已提交
63 64
}

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

Y
Yancey1989 已提交
75 76
void InitTensorsOnServer(framework::Scope* scope, platform::CPUPlace* place,
                         int64_t rows_numel) {
Y
Yancey1989 已提交
77
  CreateVarsOnScope(scope, place);
Y
Yancey1989 已提交
78 79 80 81 82 83 84 85 86
  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 已提交
87 88 89
    ptr[i] = static_cast<float>(i / 10);
  }
}
Y
Yancey1989 已提交
90

91
void StartServer(const std::string& endpoint) {
Q
qiaolongfei 已提交
92
  rpc_service_.reset(new detail::AsyncGRPCServer(endpoint, true));
Y
Yancey1989 已提交
93 94 95 96 97
  framework::ProgramDesc program;
  framework::Scope scope;
  platform::CPUPlace place;
  framework::Executor exe(place);
  platform::CPUDeviceContext ctx(place);
Y
Yancey1989 已提交
98
  auto* block = AppendPrefetchBlcok(&program);
Y
Yancey1989 已提交
99
  auto prepared = exe.Prepare(program, block->ID());
Y
Yancey1989 已提交
100
  InitTensorsOnServer(&scope, &place, 10);
Y
Yancey1989 已提交
101 102

  rpc_service_->SetProgram(&program);
X
Xin Pan 已提交
103
  rpc_service_->SetPrefetchPreparedCtx(std::move(prepared));
Y
Yancey1989 已提交
104 105 106 107
  rpc_service_->SetDevCtx(&ctx);
  rpc_service_->SetScope(&scope);
  rpc_service_->SetExecutor(&exe);

Q
qiaolongfei 已提交
108
  rpc_service_->RunSyncUpdate();
109 110
}

Y
yuyang18 已提交
111
TEST(PREFETCH, CPU) {
112 113
  // start up a server instance backend
  std::thread server_thread(StartServer, "127.0.0.1:8889");
Y
Yancey1989 已提交
114
  sleep(2);
115 116 117 118
  framework::Scope scope;
  platform::CPUPlace place;
  platform::CPUDeviceContext ctx(place);
  // create var on local scope
Y
Yancey1989 已提交
119 120
  int64_t rows_numel = 5;
  InitTensorsOnClient(&scope, &place, rows_numel);
Y
Yancey1989 已提交
121
  std::string in_var_name("ids");
Q
qiaolongfei 已提交
122
  std::string out_var_name("out");
123 124

  detail::RPCClient client;
Q
qiaolongfei 已提交
125 126 127 128
  client.AsyncPrefetchVariable("127.0.0.1:8889", ctx, scope, in_var_name,
                               out_var_name);
  client.Wait();

Y
Yancey1989 已提交
129 130 131
  auto var = scope.Var(out_var_name);
  auto value = var->GetMutable<framework::SelectedRows>()->value();
  auto ptr = value.mutable_data<float>(place);
Y
Yancey1989 已提交
132

Q
qiaolongfei 已提交
133
  rpc_service_->ShutDown();
134 135
  server_thread.join();
  rpc_service_.reset(nullptr);
Y
Yancey1989 已提交
136

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