nccl_op_test.cu 11.0 KB
Newer Older
D
Dong Zhihong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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. */

D
Dong Zhihong 已提交
15 16
#define EIGEN_USE_GPU

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

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

D
Dong Zhihong 已提交
38
USE_NO_KERNEL_OP(ncclInit);
D
Dong Zhihong 已提交
39 40
USE_GPU_ONLY_OP(ncclAllReduce);
USE_GPU_ONLY_OP(ncclReduce);
D
Dong Zhihong 已提交
41
USE_GPU_ONLY_OP(ncclBcast);
D
Dong Zhihong 已提交
42

D
Dong Zhihong 已提交
43 44 45
namespace f = paddle::framework;
namespace p = paddle::platform;

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

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

D
Dong Zhihong 已提交
51 52 53 54 55 56 57 58 59 60 61 62
// nccl op common tester, init communicator.
class NCCLTester : public ::testing::Test {
 public:
  virtual void SetUp() override {
    cpu_ctx = new p::CPUDeviceContext(p::CPUPlace());
    for (size_t i = 0; i < gpu_list.size(); ++i) {
      p::GPUPlace place(i);
      dev_ctxs.emplace_back(new p::CUDADeviceContext(place));
    }

    NCCLInitOp();
  }
D
Dong Zhihong 已提交
63

D
Dong Zhihong 已提交
64 65 66 67 68
  virtual void TearDown() override {
    for (auto &device_context : dev_ctxs) {
      delete device_context;
    }
  }
D
Dong Zhihong 已提交
69

D
Dong Zhihong 已提交
70 71
  void NCCLInitOp() {
    std::unique_ptr<f::OpDescBind> op1(new f::OpDescBind);
D
Dong Zhihong 已提交
72

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

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

D
Dong Zhihong 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    auto op = f::OpRegistry::CreateOp(*op1);
    VLOG(1) << "invoke NCCLInitOp.";
    op->Run(g_scope, *cpu_ctx);
    VLOG(1) << "NCCLInitOp finished.";
  }

  template <class T>
  void PerThreadProgram(int gpu_id, const f::OpDescBind &op_desc,
                        f::Scope *scope) {
    std::unique_lock<std::mutex> lk(mu);
    f::ProgramDescBind program;
    f::BlockDescBind *block = program.Block(0);
    f::OpDescBind *op1 = block->AppendOp();
    *op1 = op_desc;

    p::GPUPlace place(gpu_id);
    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>();
    send_tensor->Resize(kDims);
    send_tensor->mutable_data<T>(kDims, place);

    std::vector<T> send_vector(f::product(kDims), gpu_id);
    send_tensor->CopyFromVector<T>(send_vector, *ctx);
    lk.unlock();
    PADDLE_ENFORCE(send_tensor->numel() == f::product(kDims),
                   "Tensor numel not match!");
    ctx->Wait();

    VLOG(1) << "Send Tensor filled with elements " << send_tensor->numel();

    auto op = f::OpRegistry::CreateOp(*op1);
    VLOG(1) << "Device : " << gpu_id << " invoke " << op_desc.Type();
    op->Run(*scope, *ctx);
    VLOG(1) << "Device : " << gpu_id << " finished " << op_desc.Type();
  }
117

D
Dong Zhihong 已提交
118 119 120 121 122 123 124 125
 public:
  std::vector<p::DeviceContext *> dev_ctxs;
  p::DeviceContext *cpu_ctx;
  f::Scope g_scope;
  std::mutex mu;
};

// ncclInitOp with desc
D
Dong Zhihong 已提交
126 127
TEST(NCCL, ncclInitOp) {
  std::unique_ptr<f::OpDescBind> op_desc(new f::OpDescBind);
D
Dong Zhihong 已提交
128

D
Dong Zhihong 已提交
129 130 131
  op_desc->SetType("ncclInit");
  op_desc->SetOutput("Communicator", {"x1"});
  op_desc->SetAttr("gpus", {gpu_list});
D
Dong Zhihong 已提交
132

D
Dong Zhihong 已提交
133 134
  f::Scope g_scope;
  std::unique_ptr<p::DeviceContext> ctx(new p::CPUDeviceContext(p::CPUPlace()));
135

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

D
Dong Zhihong 已提交
139 140 141 142 143
  auto op = f::OpRegistry::CreateOp(*op_desc);
  VLOG(1) << "invoke NCCLInitOp.";
  op->Run(g_scope, *ctx.get());
  VLOG(1) << "NCCLInitOp finished.";
}
D
Dong Zhihong 已提交
144

D
Dong Zhihong 已提交
145
// ncclAllReduceOp with desc
D
Dong Zhihong 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
TEST_F(NCCLTester, ncclAllReduceOp) {
  std::unique_ptr<f::OpDescBind> op2(new f::OpDescBind);
  op2->SetType("ncclAllReduce");
  op2->SetInput("X", {"st"});
  op2->SetInput("Communicator", {"comm"});
  op2->SetOutput("Out", {"rt"});

  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
  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;
    p::GPUPlace gpu_place(gpu_list[i]);

    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(
        cpu_place, ct, p::GPUPlace(gpu_list[i]), rt,
        recv_tensor.numel() * sizeof(float),
        static_cast<p::CUDADeviceContext *>(dev_ctxs[i])->stream());

    for (size_t j = 0; j < f::product(kDims); ++j) {
      ASSERT_NEAR(ct[j], result, 1e-5);
    }
  }
}
D
Dong Zhihong 已提交
191 192 193

// ncclAReduceOp with desc
TEST_F(NCCLTester, ncclReduceOp) {
D
Dong Zhihong 已提交
194
  std::unique_ptr<f::OpDescBind> op2(new f::OpDescBind);
D
Dong Zhihong 已提交
195 196
  const int kRoot = 0;
  op2->SetType("ncclReduce");
D
Dong Zhihong 已提交
197 198 199
  op2->SetInput("X", {"st"});
  op2->SetInput("Communicator", {"comm"});
  op2->SetOutput("Out", {"rt"});
D
Dong Zhihong 已提交
200
  op2->SetAttr("root", {kRoot});
D
Dong Zhihong 已提交
201

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

D
Dong Zhihong 已提交
204
  std::vector<std::thread> ths;
D
Dong Zhihong 已提交
205

D
Dong Zhihong 已提交
206
  for (size_t i = 0; i < gpu_list.size(); ++i) {
D
Dong Zhihong 已提交
207
    dev_scopes.emplace_back(&g_scope.NewScope());
D
Dong Zhihong 已提交
208
    std::thread th(&NCCLTester::PerThreadProgram<float>, this, gpu_list[i],
D
Dong Zhihong 已提交
209
                   *op2.get(), dev_scopes[i]);
D
Dong Zhihong 已提交
210 211 212 213 214 215
    ths.emplace_back(std::move(th));
  }

  for (size_t i = 0; i < gpu_list.size(); ++i) {
    ths[i].join();
  }
D
Dong Zhihong 已提交
216

D
Dong Zhihong 已提交
217 218
  // check results on
  float result = std::accumulate(gpu_list.begin(), gpu_list.end(), 0);
D
Dong Zhihong 已提交
219

D
Dong Zhihong 已提交
220 221
  p::CPUPlace cpu_place;
  p::GPUPlace gpu_place(gpu_list[kRoot]);
D
Dong Zhihong 已提交
222

D
Dong Zhihong 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236
  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(
      cpu_place, ct, p::GPUPlace(gpu_list[kRoot]), rt,
      recv_tensor.numel() * sizeof(float),
      static_cast<p::CUDADeviceContext *>(dev_ctxs[kRoot])->stream());

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

D
Dong Zhihong 已提交
240 241
// // ncclBcastOp with desc
TEST_F(NCCLTester, ncclBcastOp) {
D
Dong Zhihong 已提交
242
  std::unique_ptr<f::OpDescBind> op2(new f::OpDescBind);
D
Dong Zhihong 已提交
243
  const int kRoot = 5;
D
Dong Zhihong 已提交
244
  op2->SetType("ncclBcast");
D
Dong Zhihong 已提交
245 246 247
  op2->SetInput("X", {"st"});
  op2->SetInput("Communicator", {"comm"});
  op2->SetOutput("Out", {"rt"});
D
Dong Zhihong 已提交
248
  op2->SetAttr("root", {kRoot});
D
Dong Zhihong 已提交
249

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

D
Dong Zhihong 已提交
252
  std::vector<std::thread> ths;
D
Dong Zhihong 已提交
253

D
Dong Zhihong 已提交
254
  for (size_t i = 0; i < gpu_list.size(); ++i) {
D
Dong Zhihong 已提交
255
    dev_scopes.emplace_back(&g_scope.NewScope());
D
Dong Zhihong 已提交
256
    std::thread th(&NCCLTester::PerThreadProgram<float>, this, gpu_list[i],
D
Dong Zhihong 已提交
257
                   *op2.get(), dev_scopes[i]);
D
Dong Zhihong 已提交
258 259 260 261 262 263
    ths.emplace_back(std::move(th));
  }

  for (size_t i = 0; i < gpu_list.size(); ++i) {
    ths[i].join();
  }
D
Dong Zhihong 已提交
264

D
Dong Zhihong 已提交
265 266
  const int idx = 1;
  // check results on
D
Dong Zhihong 已提交
267
  float result = kRoot;
D
Dong Zhihong 已提交
268

D
Dong Zhihong 已提交
269 270
  p::CPUPlace cpu_place;
  p::GPUPlace gpu_place(gpu_list[idx]);
D
Dong Zhihong 已提交
271

D
Dong Zhihong 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284
  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(
      cpu_place, ct, p::GPUPlace(gpu_list[idx]), rt,
      recv_tensor.numel() * sizeof(float),
      static_cast<p::CUDADeviceContext *>(dev_ctxs[idx])->stream());

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

D
Dong Zhihong 已提交
288 289 290
// joint ncclBcastOp and ncclReduceOp
TEST_F(NCCLTester, MultipleOp) {
  const int kRoot = 0;
D
Dong Zhihong 已提交
291
  std::unique_ptr<f::OpDescBind> op1(new f::OpDescBind);
D
Dong Zhihong 已提交
292
  op1->SetType("ncclReduce");
D
Dong Zhihong 已提交
293
  op1->SetInput("X", {"st"});
D
Dong Zhihong 已提交
294
  op1->SetInput("Communicator", {"comm"});
D
Dong Zhihong 已提交
295
  op1->SetOutput("Out", {"rt"});
D
Dong Zhihong 已提交
296
  op1->SetAttr("root", {kRoot});
D
Dong Zhihong 已提交
297 298

  std::unique_ptr<f::OpDescBind> op2(new f::OpDescBind);
D
Dong Zhihong 已提交
299
  op2->SetType("ncclBcast");
D
Dong Zhihong 已提交
300
  op2->SetInput("X", {"rt"});
D
Dong Zhihong 已提交
301 302
  op2->SetInput("Communicator", {"comm"});
  op2->SetOutput("Out", {"rt"});
D
Dong Zhihong 已提交
303 304 305
  op2->SetAttr("root", {kRoot});

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

  std::vector<std::thread> ths;
D
Dong Zhihong 已提交
308 309 310 311

  // run Bcast
  for (size_t i = 0; i < gpu_list.size(); ++i) {
    dev_scopes.emplace_back(&g_scope.NewScope());
D
Dong Zhihong 已提交
312
    std::thread th(&NCCLTester::PerThreadProgram<float>, this, gpu_list[i],
D
Dong Zhihong 已提交
313
                   *op1.get(), dev_scopes[i]);
D
Dong Zhihong 已提交
314 315 316 317 318 319 320
    ths.emplace_back(std::move(th));
  }

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

D
Dong Zhihong 已提交
321
  ths.clear();
D
Dong Zhihong 已提交
322

D
Dong Zhihong 已提交
323 324 325 326 327 328 329
  // run Reduce
  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));
  }
D
Dong Zhihong 已提交
330

D
Dong Zhihong 已提交
331 332 333
  for (size_t i = 0; i < gpu_list.size(); ++i) {
    ths[i].join();
  }
D
Dong Zhihong 已提交
334

D
Dong Zhihong 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
  // 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;
    p::GPUPlace gpu_place(gpu_list[i]);

    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(
        cpu_place, ct, p::GPUPlace(gpu_list[i]), rt,
        recv_tensor.numel() * sizeof(float),
        static_cast<p::CUDADeviceContext *>(dev_ctxs[i])->stream());

    for (int j = 0; j < f::product(kDims); ++j) {
      ASSERT_NEAR(ct[j], result, 1e-5);
    }
  }
}
D
Dong Zhihong 已提交
358

D
Dong Zhihong 已提交
359
int main(int argc, char **argv) {
D
Dong Zhihong 已提交
360
  const int dev_count = p::GetCUDADeviceCount();
D
Dong Zhihong 已提交
361 362 363 364 365 366
  if (dev_count <= 1) {
    LOG(WARNING)
        << "Cannot test multi-gpu nccl, because the CUDA device count is "
        << dev_count;
    return 0;
  }
D
Dong Zhihong 已提交
367 368 369 370

  for (int i = 0; i < dev_count; ++i) {
    gpu_list.emplace_back(i);
  }
D
Dong Zhihong 已提交
371
  testing::InitGoogleTest(&argc, argv);
D
Dong Zhihong 已提交
372 373 374

  // device context should be release before scope.
  // otherwise driver will down.
D
Dong Zhihong 已提交
375 376
  return RUN_ALL_TESTS();
}