nccl_op_test.cu 6.6 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 19
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <thrust/device_vector.h>
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 27 28
#include "paddle/framework/block_desc.h"
#include "paddle/framework/op_desc.h"
#include "paddle/framework/program_desc.h"
#include "paddle/framework/var_desc.h"
D
Dong Zhihong 已提交
29
#include "paddle/operators/nccl/nccl_gpu_common.h"
D
Dong Zhihong 已提交
30 31 32
#include "paddle/platform/device_context.h"
#include "paddle/platform/enforce.h"
#include "paddle/platform/gpu_info.h"
D
Dong Zhihong 已提交
33
#include "paddle/platform/place.h"
D
Dong Zhihong 已提交
34

D
Dong Zhihong 已提交
35 36 37
#include "paddle/framework/op_registry.h"

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

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

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

D
Dong Zhihong 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
// ncclInitOp with desc
// TEST(NCCL, ncclInitOp) {
//   f::ProgramDescBind program;
//   f::BlockDescBind *block = program.Block(0);
//   f::OpDescBind *op_desc = block->AppendOp();

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

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

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

// test data amount
static const f::DDim kDims = {100, 100};
static std::vector<p::DeviceContext *> dev_ctxs;

void CreateContext() {
  for (size_t i = 0; i < gpu_list.size(); ++i) {
    p::GPUPlace place(i);
    VLOG(1) << "create devicecontext : " << i;
    dev_ctxs.emplace_back(new p::CUDADeviceContext(place));
D
Dong Zhihong 已提交
79
  }
D
Dong Zhihong 已提交
80
}
D
Dong Zhihong 已提交
81

D
Dong Zhihong 已提交
82 83 84
void DestroyContext() {
  for (size_t i = 0; i < gpu_list.size(); ++i) {
    delete dev_ctxs[i];
D
Dong Zhihong 已提交
85 86 87
  }
}

D
Dong Zhihong 已提交
88 89 90 91 92 93 94
// global scope
static f::Scope g_scope;
std::mutex mu;

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 已提交
95 96
  f::ProgramDescBind program;
  f::BlockDescBind *block = program.Block(0);
D
Dong Zhihong 已提交
97 98 99 100 101 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
  f::OpDescBind *op1 = block->AppendOp();
  *op1 = op_desc;

  p::GPUPlace place(gpu_id);
  // p::DeviceContext *ctx =
  //     new p::CUDADeviceContext(place);
  p::DeviceContext *ctx = dev_ctxs.at(gpu_id);
  VLOG(1) << "device context : " << dev_ctxs.size() << " gpu_id " << gpu_id;

  // f::Scope &local_scope = g_scope.NewScope();

  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);
  // recv_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->numel() << " element in send tensor";

  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();
127 128 129
}

// ncclAllReduceOp with desc
D
Dong Zhihong 已提交
130
TEST(NCCL, ncclAllReduceOp) {
131 132
  f::ProgramDescBind program;
  f::BlockDescBind *block = program.Block(0);
D
Dong Zhihong 已提交
133
  f::OpDescBind *op1 = block->AppendOp();
134

D
Dong Zhihong 已提交
135
  p::DeviceContext *ctx = new p::CPUDeviceContext(p::CPUPlace());
136

D
Dong Zhihong 已提交
137
  CreateContext();
138

D
Dong Zhihong 已提交
139 140 141
  op1->SetType("ncclInit");
  op1->SetOutput("Communicator", {"comm"});
  op1->SetAttr("gpus", {gpu_list});
142

D
Dong Zhihong 已提交
143 144 145 146
  auto *var = g_scope.Var("comm");
  var->GetMutable<p::Communicator>();

  auto op = f::OpRegistry::CreateOp(*op1);
D
Dong Zhihong 已提交
147 148 149
  VLOG(1) << "invoke NCCLInitOp.";
  op->Run(g_scope, *ctx);
  VLOG(1) << "NCCLInitOp finished.";
D
Dong Zhihong 已提交
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
  delete ctx;

  f::OpDescBind *op2 = new f::OpDescBind;
  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());
    // std::thread th([=](){
    //     VLOG(1) << "thread id created : " << i;
    //     return 1;});
    ths.emplace_back(std::move(th));
  }

  for (size_t i = 0; i < gpu_list.size(); ++i) {
    VLOG(1) << " thread joined! " << i;
    ths[i].join();
  }
  VLOG(1) << " main thread joined!";

  delete op2;
  g_scope.~Scope();
  DestroyContext();
  VLOG(1) << " destory contexts";
D
Dong Zhihong 已提交
178 179
}

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

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