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

15 16
#include <stdlib.h>
#include <memory>
17 18 19 20 21
#include <string>

#include "gtest/gtest.h"
#include "paddle/fluid/operators/distributed/collective_client.h"
#include "paddle/fluid/operators/distributed/collective_server.h"
W
wanghuancoder 已提交
22 23 24 25 26 27

namespace paddle {
namespace framework {
class Variable;
}  // namespace framework
}  // namespace paddle
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

namespace framework = paddle::framework;
namespace platform = paddle::platform;
namespace distributed = paddle::operators::distributed;

std::unique_ptr<distributed::CollectiveServer> StartServer(
    const std::string& ep, int fan_in, framework::Scope* scope,
    platform::DeviceContext* dev_ctx) {
  distributed::CollectiveServer* server =
      distributed::CollectiveServer::GetInstance(ep, fan_in);

  auto rpc_server = server->GetRPCServer();
  rpc_server->RegisterVar("var1", distributed::kRequestGetMonomerVariable,
                          scope, dev_ctx);

  std::cout << "StartServer return" << std::endl;
  return std::unique_ptr<distributed::CollectiveServer>(server);
}

std::unique_ptr<framework::Scope> GenerateVars(platform::Place place) {
  platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
  auto& ctx = *pool.Get(place);

  framework::Scope* scope = new framework::Scope();
  framework::Variable* var = scope->Var("var1");
  auto* slr = var->GetMutable<framework::SelectedRows>();
54
  slr->set_height(20000);
55 56 57 58

  auto* tensor = slr->mutable_value();
  auto* rows = slr->mutable_rows();

59
  tensor->Resize(framework::make_ddim({3, 1024}));
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  tensor->mutable_data<float>(place);

  paddle::operators::math::set_constant(ctx, tensor, 32.7);
  for (int i = 0; i < 3; ++i) rows->push_back(i);

  std::cout << "src:" << distributed::GetSelectedRowsInfo(*slr);

  return std::unique_ptr<framework::Scope>(scope);
}

void Gather(const std::vector<distributed::RemoteVar>& vars,
            platform::DeviceContext* dev_ctx) {
  distributed::CollectiveClient* client =
      distributed::CollectiveClient::GetInstance();

  framework::Scope* scope = new framework::Scope();
  framework::Variable* var = scope->Var("var1");
  var->GetMutable<framework::SelectedRows>();

  std::vector<const framework::SelectedRows*> dst;
  client->Gather(vars, &dst, *dev_ctx, scope);
  std::cout << "dst:" << distributed::GetSelectedRowsInfo(*dst[0]);
82 83 84 85 86 87 88 89 90 91 92 93 94 95
  dev_ctx->Wait();

  ASSERT_EQ(dst[0]->value().dims(), framework::make_ddim({3, 1024}));
  ASSERT_EQ(dst[0]->height(), 20000);
  ASSERT_EQ(dst[0]->rows().size(), static_cast<size_t>(3));
  for (int i = 0; i < 3; i++) {
    ASSERT_EQ(dst[0]->rows()[i], i);
  }

  std::vector<float> vec;
  TensorToVector(dst[0]->value(), *dev_ctx, &vec);
  for (size_t i = 0; i < 3 * 1024; i++) {
    ASSERT_FLOAT_EQ(vec[i], 32.7);
  }
96 97
}

G
gongweibao 已提交
98
TEST(CollectiveServer, GPU) {
99 100 101
  setenv("http_proxy", "", 1);
  setenv("https_proxy", "", 1);

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  platform::CUDAPlace place;
  platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
  auto& ctx = *pool.Get(place);

  std::string ep = "127.0.0.1:7164";
  auto scope = GenerateVars(place);

  auto* v1 = scope->FindVar("var1");
  std::cout << "var1:" << v1 << std::endl;

  auto server = StartServer(ep, 2, scope.get(), &ctx);
  auto rpc_server = server->GetRPCServer();

  distributed::RemoteVar var;
  var.ep_ = ep;
  var.var_name_ = "var1";
  var.trainer_id_ = 0;

  std::vector<distributed::RemoteVar> vars{var};
  Gather(vars, &ctx);
  Gather(vars, &ctx);

  std::cout << "begin WaitVarBarrier" << std::endl;
  rpc_server->WaitVarBarrier("var1");
  rpc_server->ClearRegisteredVars();
  server->Stop();

  scope.release();
  server.release();
}