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 48 49 50

  auto& out = *root_block->Var("out");
  out.SetType(framework::proto::VarType::LOD_TENSOR);
  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 57
  auto w = w_var->GetMutable<framework::LoDTensor>();
  w->Resize({10, 10});
Y
Yancey1989 已提交
58
  w->mutable_data<float>(*place);
Y
Yancey1989 已提交
59

Y
Yancey1989 已提交
60
  auto out_var = scope->Var("out");
Y
Yancey1989 已提交
61 62
  auto out = out_var->GetMutable<framework::LoDTensor>();
  out->Resize({5, 10});
Y
Yancey1989 已提交
63
  out->mutable_data<float>(*place);
Y
Yancey1989 已提交
64

Y
Yancey1989 已提交
65
  auto ids_var = scope->Var("ids");
Y
Yancey1989 已提交
66 67
  auto ids = ids_var->GetMutable<framework::LoDTensor>();
  ids->Resize({5, 1});
Y
Yancey1989 已提交
68 69 70 71 72 73
}

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

Y
Yancey1989 已提交
79 80 81 82 83 84 85 86 87
void InitTensorsOnServer(framework::Scope* scope, platform::CPUPlace* place) {
  CreateVarsOnScope(scope, place);
  auto w_var = scope->Var("w");
  auto w = w_var->GetMutable<framework::LoDTensor>();
  auto ptr = w->mutable_data<float>(*place);
  for (int64_t i = 0; i < w->numel(); ++i) {
    ptr[i] = static_cast<float>(i / 10);
  }
}
Y
Yancey1989 已提交
88

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

  rpc_service_->SetProgram(&program);
  rpc_service_->SetPrefetchBlkdId(block->ID());
  rpc_service_->SetDevCtx(&ctx);
  rpc_service_->SetScope(&scope);
  rpc_service_->SetExecutor(&exe);

Q
qiaolongfei 已提交
105
  rpc_service_->RunSyncUpdate();
106 107 108 109 110 111 112
}

TEST(PREFETCH, CPU) {
  // start up a server instance backend
  // TODO(Yancey1989): Need to start a server with optimize blocks and
  // prefetch blocks.
  std::thread server_thread(StartServer, "127.0.0.1:8889");
Y
Yancey1989 已提交
113
  sleep(2);
114 115 116 117
  framework::Scope scope;
  platform::CPUPlace place;
  platform::CPUDeviceContext ctx(place);
  // create var on local scope
Y
Yancey1989 已提交
118
  InitTensorsOnClient(&scope, &place);
Y
Yancey1989 已提交
119
  std::string in_var_name("ids");
Q
qiaolongfei 已提交
120
  std::string out_var_name("out");
121 122

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

Y
Yancey1989 已提交
127 128
  auto out_var = scope.Var(out_var_name);
  auto out = out_var->Get<framework::LoDTensor>();
Y
Yancey1989 已提交
129

Y
Yancey1989 已提交
130
  auto out_ptr = out.data<float>();
Q
qiaolongfei 已提交
131
  rpc_service_->ShutDown();
132 133
  server_thread.join();
  rpc_service_.reset(nullptr);
Y
Yancey1989 已提交
134 135 136 137 138 139 140

  EXPECT_EQ(out.dims().size(), 2);
  EXPECT_EQ(out_ptr[0], static_cast<float>(0));
  EXPECT_EQ(out_ptr[0 + 1 * out.dims()[1]], static_cast<float>(2));
  EXPECT_EQ(out_ptr[0 + 2 * out.dims()[1]], static_cast<float>(4));
  EXPECT_EQ(out_ptr[0 + 3 * out.dims()[1]], static_cast<float>(6));
  EXPECT_EQ(out_ptr[0 + 4 * out.dims()[1]], static_cast<float>(8));
141
}