nccl_op_test.cu 3.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 17 18 19
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <thrust/device_vector.h>
#include <memory>
#include <vector>
D
Dong Zhihong 已提交
20

D
Dong Zhihong 已提交
21 22 23 24 25
#include "paddle/framework/block_desc.h"
#include "paddle/framework/op_desc.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/program_desc.h"
#include "paddle/framework/var_desc.h"
D
Dong Zhihong 已提交
26 27 28
#include "paddle/platform/device_context.h"
#include "paddle/platform/enforce.h"
#include "paddle/platform/gpu_info.h"
D
Dong Zhihong 已提交
29
#include "paddle/platform/place.h"
D
Dong Zhihong 已提交
30

D
Dong Zhihong 已提交
31 32 33 34 35
USE_CPU_ONLY_OP(ncclInit);
USE_GPU_ONLY_OP(ncclAllReduce);
USE_GPU_ONLY_OP(ncclReduce);
USE_GPU_ONLY_OP(ncclBcastSend);
USE_GPU_ONLY_OP(ncclBcastRecv);
D
Dong Zhihong 已提交
36 37 38

static std::vector<int> gpu_list;

D
Dong Zhihong 已提交
39 40
namespace f = paddle::framework;
namespace ops = paddle::operators;
D
Dong Zhihong 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

void AddOp(const std::string &type, const f::VariableNameMap &inputs,
           const f::VariableNameMap &outputs, f::AttributeMap attrs,
           paddle::framework::BlockDescBind *block) {
  for (auto kv : outputs) {
    for (auto v : kv.second) {
      auto var = block->Var(v);
      var->SetDataType(paddle::framework::DataType::FP32);
    }
  }

  auto op = block->AppendOp();
  op->SetType(type);
  for (auto &kv : inputs) {
    op->SetInput(kv.first, kv.second);
  }
  for (auto &kv : outputs) {
    op->SetOutput(kv.first, kv.second);
  }
  op->SetAttrMap(attrs);
}

D
Dong Zhihong 已提交
63 64
// ncclInitOp with desc
TEST(NCCL, ncclInitOp) {
D
Dong Zhihong 已提交
65 66
  f::ProgramDescBind program;
  f::BlockDescBind *block = program.Block(0);
67
  f::OpDescBind *op_desc = block->AppendOp();
D
Dong Zhihong 已提交
68

69 70 71
  op_desc->SetType("ncclInit");
  op_desc->SetOutput("Communicator", {"x1"});
  op_desc->SetAttr("gpus", {gpu_list});
D
Dong Zhihong 已提交
72 73 74 75 76 77 78
  f::Scope g_scope;
  paddle::platform::DeviceContext *ctx =
      new paddle::platform::CPUDeviceContext(paddle::platform::CPUPlace());

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

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  auto op = f::OpRegistry::CreateOp(*op_desc);
  VLOG(1) << "invoke NCCLInitOp.";
  op->Run(g_scope, *ctx);
  VLOG(1) << "NCCLInitOp finished.";
}

// ncclAllReduceOp with desc
TEST(NCCL, ncclInitOp) {
  f::ProgramDescBind program;
  f::BlockDescBind *block = program.Block(0);
  f::OpDescBind *op_desc = block->AppendOp();

  op_desc->SetType("ncclAllReduce");

  op_desc->SetOutput("Communicator", {"x1"});
  op_desc->SetAttr("gpus", {gpu_list});
  f::Scope g_scope;
  paddle::platform::DeviceContext *ctx =
      new paddle::platform::CPUDeviceContext(paddle::platform::CPUPlace());

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

  auto op = f::OpRegistry::CreateOp(*op_desc);
D
Dong Zhihong 已提交
103 104 105
  VLOG(1) << "invoke NCCLInitOp.";
  op->Run(g_scope, *ctx);
  VLOG(1) << "NCCLInitOp finished.";
D
Dong Zhihong 已提交
106 107 108
}

int main(int argc, char **argv) {
D
Dong Zhihong 已提交
109
  static int dev_count = paddle::platform::GetCUDADeviceCount();
D
Dong Zhihong 已提交
110 111 112 113 114 115
  if (dev_count <= 1) {
    LOG(WARNING)
        << "Cannot test multi-gpu nccl, because the CUDA device count is "
        << dev_count;
    return 0;
  }
D
Dong Zhihong 已提交
116 117 118 119

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