nccl_op_test.cu.cc 9.1 KB
Newer Older
D
Dong Zhihong 已提交
1 2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

3 4 5
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
D
Dong Zhihong 已提交
6

7
    http://www.apache.org/licenses/LICENSE-2.0
D
Dong Zhihong 已提交
8

9 10 11 12 13
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. */
D
Dong Zhihong 已提交
14

15 16
#include <glog/logging.h>
#include <gtest/gtest.h>
D
Dong Zhihong 已提交
17
#include <algorithm>
18
#include <memory>
19 20 21
#include <mutex>
#include <thread>
#include <utility>
22
#include <vector>
D
Dong Zhihong 已提交
23

D
Dong Zhihong 已提交
24
#include "paddle/framework/block_desc.h"
25
#include "paddle/framework/init.h"
D
Dong Zhihong 已提交
26
#include "paddle/framework/op_desc.h"
27
#include "paddle/framework/op_registry.h"
D
Dong Zhihong 已提交
28 29
#include "paddle/framework/program_desc.h"
#include "paddle/framework/var_desc.h"
30
#include "paddle/operators/nccl/nccl_gpu_common.h"
D
Dong Zhihong 已提交
31 32 33
#include "paddle/platform/device_context.h"
#include "paddle/platform/enforce.h"
#include "paddle/platform/gpu_info.h"
34
#include "paddle/platform/place.h"
D
Dong Zhihong 已提交
35

36
USE_NO_KERNEL_OP(ncclInit);
Q
QI JUN 已提交
37 38 39
USE_CUDA_ONLY_OP(ncclAllReduce);
USE_CUDA_ONLY_OP(ncclReduce);
USE_CUDA_ONLY_OP(ncclBcast);
D
Dong Zhihong 已提交
40

41 42 43
namespace f = paddle::framework;
namespace p = paddle::platform;

D
Dong Zhihong 已提交
44
static std::vector<int> gpu_list;
45 46 47

// test data amount
const f::DDim kDims = {100, 100};
D
Dong Zhihong 已提交
48

49 50 51 52
// nccl op common tester, init communicator.
class NCCLTester : public ::testing::Test {
 public:
  virtual void SetUp() override {
53
    paddle::platform::CPUPlace cpu_place;
54
    for (size_t i = 0; i < gpu_list.size(); ++i) {
55
      p::CUDAPlace place(i);
56 57 58 59 60
      dev_ctxs.emplace_back(new p::CUDADeviceContext(place));
    }

    NCCLInitOp();
  }
61

62 63 64 65 66
  virtual void TearDown() override {
    for (auto &device_context : dev_ctxs) {
      delete device_context;
    }
  }
67

68
  void NCCLInitOp() {
69
    paddle::platform::CPUPlace cpu_place;
70
    std::unique_ptr<f::OpDesc> op1(new f::OpDesc);
D
Dong Zhihong 已提交
71

72 73 74
    op1->SetType("ncclInit");
    op1->SetOutput("Communicator", {"comm"});
    op1->SetAttr("gpus", {gpu_list});
D
Dong Zhihong 已提交
75

76 77
    auto *var = g_scope.Var("comm");
    var->GetMutable<p::Communicator>();
78

79 80
    auto op = f::OpRegistry::CreateOp(*op1);
    VLOG(1) << "invoke NCCLInitOp.";
81
    op->Run(g_scope, cpu_place);
82 83 84 85
    VLOG(1) << "NCCLInitOp finished.";
  }

  template <class T>
86
  void PerThreadProgram(int gpu_id, const f::OpDesc &op_desc, f::Scope *scope) {
87
    std::unique_lock<std::mutex> lk(mu);
88
    const f::OpDesc *op1 = &op_desc;
89

90
    p::CUDAPlace place(gpu_id);
91 92 93 94 95
    auto &ctx = dev_ctxs.at(gpu_id);

    auto *send_tensor = scope->Var("st")->GetMutable<f::LoDTensor>();
    auto *recv_tensor = scope->Var("rt")->GetMutable<f::LoDTensor>();

96 97 98 99 100
    if (!send_tensor->numel()) {
      send_tensor->Resize(kDims);
      send_tensor->mutable_data<T>(kDims, place);

      std::vector<T> send_vector(f::product(kDims), gpu_id);
D
dzhwinter 已提交
101
      paddle::framework::CopyFromVector<T>(send_vector, *ctx, send_tensor);
102 103 104 105
      ctx->Wait();
      VLOG(1) << "Send Tensor filled with elements " << send_tensor->numel();
    }

106
    lk.unlock();
107

108 109 110 111
    PADDLE_ENFORCE(send_tensor->numel() == f::product(kDims),
                   "Tensor numel not match!");

    auto op = f::OpRegistry::CreateOp(*op1);
112

113
    VLOG(1) << "Device : " << gpu_id << " invoke " << op_desc.Type();
114 115
    VLOG(1) << " send_tensor : " << send_tensor->numel()
            << " recv_tensor : " << recv_tensor->numel();
116
    op->Run(*scope, place);
117 118
    VLOG(1) << "Device : " << gpu_id << " finished " << op_desc.Type();
  }
119

120 121 122 123 124 125
 public:
  std::vector<p::DeviceContext *> dev_ctxs;
  f::Scope g_scope;
  std::mutex mu;
};

126 127
// ncclInitOp with desc
TEST(NCCL, ncclInitOp) {
128
  std::unique_ptr<f::OpDesc> op_desc(new f::OpDesc);
129 130 131 132 133 134

  op_desc->SetType("ncclInit");
  op_desc->SetOutput("Communicator", {"x1"});
  op_desc->SetAttr("gpus", {gpu_list});

  f::Scope g_scope;
135
  paddle::platform::CPUPlace cpu_place;
136 137 138

  auto *var = g_scope.Var("x1");
  var->GetMutable<p::Communicator>();
D
Dong Zhihong 已提交
139

140 141
  auto op = f::OpRegistry::CreateOp(*op_desc);
  VLOG(1) << "invoke NCCLInitOp.";
142
  op->Run(g_scope, cpu_place);
143 144 145 146 147
  VLOG(1) << "NCCLInitOp finished.";
}

// ncclAllReduceOp with desc
TEST_F(NCCLTester, ncclAllReduceOp) {
148
  std::unique_ptr<f::OpDesc> op2(new f::OpDesc);
149 150
  op2->SetType("ncclAllReduce");
  op2->SetInput("X", {"st"});
D
Dong Zhihong 已提交
151
  op2->SetInput("Communicator", {"comm"});
152
  op2->SetOutput("Out", {"rt"});
153 154

  std::vector<f::Scope *> dev_scopes;
D
Dong Zhihong 已提交
155 156

  std::vector<std::thread> ths;
157 158 159 160 161 162 163

  for (size_t i = 0; i < gpu_list.size(); ++i) {
    dev_scopes.emplace_back(&g_scope.NewScope());
    std::thread th(&NCCLTester::PerThreadProgram<float>, this, gpu_list[i],
                   *op2.get(), dev_scopes[i]);
    ths.emplace_back(std::move(th));
  }
164

165 166 167
  for (size_t i = 0; i < gpu_list.size(); ++i) {
    ths[i].join();
  }
168

169 170 171 172 173
  // check results
  float result = std::accumulate(gpu_list.begin(), gpu_list.end(), 0);

  for (size_t i = 0; i < dev_scopes.size(); ++i) {
    p::CPUPlace cpu_place;
174
    p::CUDAPlace gpu_place(gpu_list[i]);
175 176 177 178 179 180 181 182

    auto &recv_tensor = dev_scopes[i]->FindVar("rt")->Get<f::LoDTensor>();
    auto *rt = recv_tensor.data<float>();
    auto *result_tensor = dev_scopes[i]->Var("ct")->GetMutable<f::LoDTensor>();
    result_tensor->Resize(kDims);
    auto *ct = result_tensor->mutable_data<float>(cpu_place);

    paddle::memory::Copy(
183
        cpu_place, ct, p::CUDAPlace(gpu_list[i]), rt,
184 185 186
        recv_tensor.numel() * sizeof(float),
        static_cast<p::CUDADeviceContext *>(dev_ctxs[i])->stream());

D
dangqingqing 已提交
187
    for (int64_t j = 0; j < f::product(kDims); ++j) {
188 189 190 191
      ASSERT_NEAR(ct[j], result, 1e-5);
    }
  }
}
192

193
// ncclReduceOp with desc
194
TEST_F(NCCLTester, ncclReduceOp) {
195
  std::unique_ptr<f::OpDesc> op2(new f::OpDesc);
196 197 198 199 200
  const int kRoot = 0;
  op2->SetType("ncclReduce");
  op2->SetInput("X", {"st"});
  op2->SetInput("Communicator", {"comm"});
  op2->SetOutput("Out", {"rt"});
201
  op2->SetAttr("root", kRoot);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

  std::vector<f::Scope *> dev_scopes;

  std::vector<std::thread> ths;

  for (size_t i = 0; i < gpu_list.size(); ++i) {
    dev_scopes.emplace_back(&g_scope.NewScope());
    std::thread th(&NCCLTester::PerThreadProgram<float>, this, gpu_list[i],
                   *op2.get(), dev_scopes[i]);
    ths.emplace_back(std::move(th));
  }

  for (size_t i = 0; i < gpu_list.size(); ++i) {
    ths[i].join();
  }

  // check results on
  float result = std::accumulate(gpu_list.begin(), gpu_list.end(), 0);

  p::CPUPlace cpu_place;
222
  p::CUDAPlace gpu_place(gpu_list[kRoot]);
223 224 225 226 227 228 229 230 231

  auto &recv_tensor = dev_scopes[kRoot]->FindVar("rt")->Get<f::LoDTensor>();
  auto *rt = recv_tensor.data<float>();
  auto *result_tensor =
      dev_scopes[kRoot]->Var("ct")->GetMutable<f::LoDTensor>();
  result_tensor->Resize(kDims);
  auto *ct = result_tensor->mutable_data<float>(cpu_place);

  paddle::memory::Copy(
232
      cpu_place, ct, p::CUDAPlace(gpu_list[kRoot]), rt,
233 234 235
      recv_tensor.numel() * sizeof(float),
      static_cast<p::CUDADeviceContext *>(dev_ctxs[kRoot])->stream());

D
dangqingqing 已提交
236
  for (int64_t j = 0; j < f::product(kDims); ++j) {
237 238 239 240
    ASSERT_NEAR(ct[j], result, 1e-5);
  }
}

241
// ncclBcastOp with desc
242
TEST_F(NCCLTester, ncclBcastOp) {
243
  std::unique_ptr<f::OpDesc> op2(new f::OpDesc);
244 245 246 247 248
  const int kRoot = 5;
  op2->SetType("ncclBcast");
  op2->SetInput("X", {"st"});
  op2->SetInput("Communicator", {"comm"});
  op2->SetOutput("Out", {"rt"});
249
  op2->SetAttr("root", kRoot);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

  std::vector<f::Scope *> dev_scopes;

  std::vector<std::thread> ths;

  for (size_t i = 0; i < gpu_list.size(); ++i) {
    dev_scopes.emplace_back(&g_scope.NewScope());
    std::thread th(&NCCLTester::PerThreadProgram<float>, this, gpu_list[i],
                   *op2.get(), dev_scopes[i]);
    ths.emplace_back(std::move(th));
  }

  for (size_t i = 0; i < gpu_list.size(); ++i) {
    ths[i].join();
  }

  const int idx = 1;
  // check results on
  float result = kRoot;

  p::CPUPlace cpu_place;
271
  p::CUDAPlace gpu_place(gpu_list[idx]);
272 273 274 275 276 277 278 279

  auto &recv_tensor = dev_scopes[idx]->FindVar("rt")->Get<f::LoDTensor>();
  auto *rt = recv_tensor.data<float>();
  auto *result_tensor = dev_scopes[idx]->Var("ct")->GetMutable<f::LoDTensor>();
  result_tensor->Resize(kDims);
  auto *ct = result_tensor->mutable_data<float>(cpu_place);

  paddle::memory::Copy(
280
      cpu_place, ct, p::CUDAPlace(gpu_list[idx]), rt,
281 282 283
      recv_tensor.numel() * sizeof(float),
      static_cast<p::CUDADeviceContext *>(dev_ctxs[idx])->stream());

D
dangqingqing 已提交
284
  for (int64_t j = 0; j < f::product(kDims); ++j) {
285 286 287 288
    ASSERT_NEAR(ct[j], result, 1e-5);
  }
}

D
Dong Zhihong 已提交
289
int main(int argc, char **argv) {
290
  const int dev_count = p::GetCUDADeviceCount();
D
Dong Zhihong 已提交
291 292 293 294 295 296
  if (dev_count <= 1) {
    LOG(WARNING)
        << "Cannot test multi-gpu nccl, because the CUDA device count is "
        << dev_count;
    return 0;
  }
D
Dong Zhihong 已提交
297

298 299 300 301 302
  std::vector<paddle::platform::Place> places;

  places.emplace_back(paddle::platform::CPUPlace());
  int count = paddle::platform::GetCUDADeviceCount();
  for (int i = 0; i < count; ++i) {
303
    places.emplace_back(paddle::platform::CUDAPlace(i));
D
Dong Zhihong 已提交
304 305
    gpu_list.emplace_back(i);
  }
306 307 308 309

  VLOG(0) << " DeviceCount " << count;
  paddle::platform::DeviceContextPool::Create(places);

D
Dong Zhihong 已提交
310
  testing::InitGoogleTest(&argc, argv);
311 312 313

  // device context should be release before scope.
  // otherwise driver will down.
D
Dong Zhihong 已提交
314 315
  return RUN_ALL_TESTS();
}
新手
引导
客服 返回
顶部