nccl_op_test.cu 6.8 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 <memory>
D
Dong Zhihong 已提交
20 21 22
#include <mutex>
#include <thread>
#include <utility>
D
Dong Zhihong 已提交
23
#include <vector>
D
Dong Zhihong 已提交
24

D
Dong Zhihong 已提交
25 26
#include "paddle/framework/block_desc.h"
#include "paddle/framework/op_desc.h"
D
Dong Zhihong 已提交
27
#include "paddle/framework/op_registry.h"
D
Dong Zhihong 已提交
28 29
#include "paddle/framework/program_desc.h"
#include "paddle/framework/var_desc.h"
D
Dong Zhihong 已提交
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"
D
Dong Zhihong 已提交
34
#include "paddle/platform/place.h"
D
Dong Zhihong 已提交
35

D
Dong Zhihong 已提交
36
USE_NO_KERNEL_OP(ncclInit);
D
Dong Zhihong 已提交
37 38 39 40
USE_GPU_ONLY_OP(ncclAllReduce);
USE_GPU_ONLY_OP(ncclReduce);
USE_GPU_ONLY_OP(ncclBcastSend);
USE_GPU_ONLY_OP(ncclBcastRecv);
D
Dong Zhihong 已提交
41

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

D
Dong Zhihong 已提交
45
static std::vector<int> gpu_list;
D
Dong Zhihong 已提交
46 47 48 49 50
static std::vector<std::unique_ptr<p::DeviceContext>> dev_ctxs;
std::mutex mu;

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

D
Dong Zhihong 已提交
52
// ncclInitOp with desc
D
Dong Zhihong 已提交
53 54
TEST(NCCL, ncclInitOp) {
  std::unique_ptr<f::OpDescBind> op_desc(new f::OpDescBind);
D
Dong Zhihong 已提交
55

D
Dong Zhihong 已提交
56 57 58 59 60
  op_desc->SetType("ncclInit");
  op_desc->SetOutput("Communicator", {"x1"});
  op_desc->SetAttr("gpus", {gpu_list});
  f::Scope g_scope;
  p::DeviceContext *ctx = new p::CPUDeviceContext(p::CPUPlace());
D
Dong Zhihong 已提交
61

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

D
Dong Zhihong 已提交
65 66 67 68
  auto op = f::OpRegistry::CreateOp(*op_desc);
  VLOG(1) << "invoke NCCLInitOp.";
  op->Run(g_scope, *ctx);
  VLOG(1) << "NCCLInitOp finished.";
D
Dong Zhihong 已提交
69 70
}

D
Dong Zhihong 已提交
71 72 73
template <class T>
void DeviceProgram(int gpu_id, const f::OpDescBind &op_desc, f::Scope *scope) {
  std::unique_lock<std::mutex> lk(mu);
D
Dong Zhihong 已提交
74 75
  f::ProgramDescBind program;
  f::BlockDescBind *block = program.Block(0);
D
Dong Zhihong 已提交
76 77 78 79
  f::OpDescBind *op1 = block->AppendOp();
  *op1 = op_desc;

  p::GPUPlace place(gpu_id);
D
Dong Zhihong 已提交
80
  auto ctx = dev_ctxs.at(gpu_id);
D
Dong Zhihong 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93

  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();

D
Dong Zhihong 已提交
94
  VLOG(1) << "Send Tensor filled with elements " << send_tensor->numel();
D
Dong Zhihong 已提交
95 96 97 98 99

  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();
100 101 102
}

// ncclAllReduceOp with desc
D
Dong Zhihong 已提交
103
TEST(NCCL, ncclAllReduceOp) {
D
Dong Zhihong 已提交
104 105
  std::unique_ptr<p::DeviceContext> ctx(new p::CPUDeviceContext(p::CPUPlace()));
  std::unique_ptr<f::Scope> g_scope(new Scope);
106

D
Dong Zhihong 已提交
107
  std::unique_ptr<f::OpDescBind> op1(new f::OpDescBind);
D
Dong Zhihong 已提交
108 109 110
  op1->SetType("ncclInit");
  op1->SetOutput("Communicator", {"comm"});
  op1->SetAttr("gpus", {gpu_list});
111

D
Dong Zhihong 已提交
112 113 114 115
  auto *var = g_scope.Var("comm");
  var->GetMutable<p::Communicator>();

  auto op = f::OpRegistry::CreateOp(*op1);
D
Dong Zhihong 已提交
116 117 118
  VLOG(1) << "invoke NCCLInitOp.";
  op->Run(g_scope, *ctx);
  VLOG(1) << "NCCLInitOp finished.";
D
Dong Zhihong 已提交
119 120
  delete ctx;

D
Dong Zhihong 已提交
121
  std::unique_ptr<f::OpDescBind> op2(new f::OpDescBind);
D
Dong Zhihong 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  op2->SetType("ncclAllReduce");
  op2->SetInput("X", {"st"});
  op2->SetInput("Communicator", {"comm"});
  op2->SetOutput("Out", {"rt"});

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

  for (size_t i = 0; i < gpu_list.size(); ++i) {
    ths[i].join();
  }
D
Dong Zhihong 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  g_scope->reset(nullptr);
}

// ncclReduceOp with desc
TEST(NCCL, ncclReduceOp) {
  std::unique_ptr<p::DeviceContext> ctx(new p::CPUDeviceContext(p::CPUPlace()));
  std::unique_ptr<f::Scope> g_scope(new Scope);

  std::unique_ptr<f::OpDescBind> op1(new f::OpDescBind);
  op1->SetType("ncclInit");
  op1->SetOutput("Communicator", {"comm"});
  op1->SetAttr("gpus", {gpu_list});

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

  auto op = f::OpRegistry::CreateOp(*op1);
  VLOG(1) << "invoke NCCLInitOp.";
  op->Run(g_scope, *ctx);
  VLOG(1) << "NCCLInitOp finished.";
  delete ctx;

  std::unique_ptr<f::OpDescBind> op2(new f::OpDescBind);
  op2->SetType("ncclReduce");
  op2->SetInput("X", {"st"});
  op2->SetInput("Communicator", {"comm"});
  op2->SetOutput("Out", {"rt"});
D
Dong Zhihong 已提交
164

D
Dong Zhihong 已提交
165 166 167 168 169 170 171 172 173 174 175
  std::vector<std::thread> ths;
  for (size_t i = 0; i < gpu_list.size(); ++i) {
    std::thread th(DeviceProgram<float>, gpu_list[i], *op2,
                   &g_scope.NewScope());
    ths.emplace_back(std::move(th));
  }

  for (size_t i = 0; i < gpu_list.size(); ++i) {
    ths[i].join();
  }
  g_scope->reset(nullptr);
D
Dong Zhihong 已提交
176 177
}

D
Dong Zhihong 已提交
178
// ncclBcastOp with desc
D
Dong Zhihong 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
TEST(NCCL, ncclBcastOp) {
  f::ProgramDescBind program;
  f::BlockDescBind *block = program.Block(0);
  f::OpDescBind *op1 = block->AppendOp();

  p::DeviceContext *ctx = new p::CPUDeviceContext(p::CPUPlace());

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

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

  auto op = f::OpRegistry::CreateOp(*op1);
  VLOG(1) << "invoke NCCLInitOp.";
  op->Run(g_scope, *ctx);
  VLOG(1) << "NCCLInitOp finished.";

  f::OpDescBind *op2 = new f::OpDescBind;
  op2->SetType("ncclBcastSend");
  op2->SetInput("X", {"st"});
  op2->SetInput("Communicator", {"comm"});
  op2->SetOutput("Out", {"rt"});

  std::vector<std::thread> ths;
  for (size_t i = 0; i < gpu_list.size(); ++i) {
    std::thread th(DeviceProgram<float>, gpu_list[i], *op2);
    ths.emplace_back(std::move(th));
  }

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

D
Dong Zhihong 已提交
215
int main(int argc, char **argv) {
D
Dong Zhihong 已提交
216
  const int dev_count = p::GetCUDADeviceCount();
D
Dong Zhihong 已提交
217 218 219 220 221 222
  if (dev_count <= 1) {
    LOG(WARNING)
        << "Cannot test multi-gpu nccl, because the CUDA device count is "
        << dev_count;
    return 0;
  }
D
Dong Zhihong 已提交
223 224 225 226

  for (int i = 0; i < dev_count; ++i) {
    gpu_list.emplace_back(i);
  }
D
Dong Zhihong 已提交
227
  testing::InitGoogleTest(&argc, argv);
D
Dong Zhihong 已提交
228 229 230 231 232 233 234

  // device context should be release before scope.
  // otherwise driver will down.
  for (size_t i = 0; i < gpu_list.size(); ++i) {
    p::GPUPlace place(i);
    dev_ctxs.emplace_back(new p::CUDADeviceContext(place));
  }
D
Dong Zhihong 已提交
235 236
  return RUN_ALL_TESTS();
}