broadcast_op_handle_test.cc 6.8 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

C
chengduoZH 已提交
15
#include "paddle/fluid/framework/details/broadcast_op_handle.h"
C
chengduoZH 已提交
16 17 18 19 20 21 22 23 24 25
#include "gtest/gtest.h"

#include "paddle/fluid/platform/device_context.h"

namespace f = paddle::framework;
namespace p = paddle::platform;

// test data amount
const f::DDim kDims = {20, 20};

C
chengduoZH 已提交
26
class BroadcastTester : public ::testing::Test {
C
chengduoZH 已提交
27
 public:
C
chengduoZH 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  void InitCtx(bool use_gpu) {
    if (use_gpu) {
#ifdef PADDLE_WITH_CUDA
      int count = p::GetCUDADeviceCount();
      if (count <= 1) {
        LOG(WARNING) << "Cannot test multi-gpu Broadcast, because the CUDA "
                        "device count is "
                     << count;
        exit(0);
      }
      for (int i = 0; i < count; ++i) {
        auto p = p::CUDAPlace(i);
        gpu_list_.push_back(p);
        ctxs_.emplace_back(new p::CUDADeviceContext(p));
      }
#else
      PADDLE_THROW("CUDA is not support.");
#endif
    } else {
      int count = 8;
      for (int i = 0; i < count; ++i) {
        auto p = p::CPUPlace();
        gpu_list_.push_back(p);
        ctxs_.emplace_back(new p::CPUDeviceContext(p));
      }
C
chengduoZH 已提交
53 54 55 56
    }
  }

  template <class T>
C
chengduoZH 已提交
57
  void BroadcastInitOp(int input_scope_idx) {
C
chengduoZH 已提交
58 59 60 61 62
    for (size_t j = 0; j < gpu_list_.size(); ++j) {
      local_scope_.push_back(&g_scope_.NewScope());
      auto* out_var = local_scope_[j]->Var("out");
      out_var->GetMutable<T>();
    }
C
chengduoZH 已提交
63
    auto* in_var = local_scope_[input_scope_idx]->Var("input");
C
chengduoZH 已提交
64 65
    in_var->GetMutable<T>();

C
chengduoZH 已提交
66
    bc_op_handle_ = new f::details::BroadcastOpHandle(local_scope_, gpu_list_);
C
chengduoZH 已提交
67 68

    f::details::VarHandle* in_var_handle = new f::details::VarHandle();
C
chengduoZH 已提交
69
    in_var_handle->place_ = gpu_list_[input_scope_idx];
C
chengduoZH 已提交
70 71
    in_var_handle->name_ = "input";
    in_var_handle->version_ = 1;
C
chengduoZH 已提交
72
    in_var_handle->scope_idx_ = input_scope_idx;
C
chengduoZH 已提交
73 74 75 76
    in_var_handle->generated_op_ = nullptr;
    bc_op_handle_->AddInput(in_var_handle);

    for (size_t j = 0; j < gpu_list_.size(); ++j) {
C
chengduoZH 已提交
77
      bc_op_handle_->dev_ctxes_[gpu_list_[j]] = ctxs_[j];
C
chengduoZH 已提交
78 79 80 81
      f::details::VarHandle* out_var_handle = new f::details::VarHandle();
      out_var_handle->place_ = gpu_list_[j];
      out_var_handle->name_ = "out";
      out_var_handle->version_ = 2;
C
chengduoZH 已提交
82
      out_var_handle->scope_idx_ = j;
C
chengduoZH 已提交
83 84 85 86
      out_var_handle->generated_op_ = bc_op_handle_;
      bc_op_handle_->AddOutput(out_var_handle);
    }
  }
C
chengduoZH 已提交
87
  void BroadcastOpDestroy() {
C
chengduoZH 已提交
88 89 90 91 92 93 94
    for (auto in : bc_op_handle_->inputs_) {
      delete in;
    }
    for (auto out : bc_op_handle_->outputs_) {
      delete out;
    }
    delete bc_op_handle_;
C
chengduoZH 已提交
95 96 97
    for (size_t j = 0; j < ctxs_.size(); ++j) {
      delete ctxs_[j];
    }
C
chengduoZH 已提交
98 99
  }

C
chengduoZH 已提交
100 101 102 103 104
  void WaitAll() {
    for (size_t j = 0; j < ctxs_.size(); ++j) {
      ctxs_[j]->Wait();
    }
  }
C
chengduoZH 已提交
105

C
chengduoZH 已提交
106 107 108
  void TestBroadcastLodTensor() {
    int input_scope_idx = 0;
    BroadcastInitOp<f::LoDTensor>(input_scope_idx);
C
chengduoZH 已提交
109

C
chengduoZH 已提交
110 111 112
    auto in_var = local_scope_[input_scope_idx]->Var("input");
    auto in_lod_tensor = in_var->GetMutable<f::LoDTensor>();
    in_lod_tensor->mutable_data<float>(kDims, gpu_list_[input_scope_idx]);
C
chengduoZH 已提交
113

C
chengduoZH 已提交
114 115 116
    std::vector<float> send_vector(f::product(kDims), input_scope_idx + 12);
    for (size_t k = 0; k < send_vector.size(); ++k) {
      send_vector[k] = k;
C
chengduoZH 已提交
117
    }
C
chengduoZH 已提交
118 119 120 121
    f::LoD lod{{0, 10, 20}};
    paddle::framework::TensorFromVector<float>(
        send_vector, *(ctxs_[input_scope_idx]), in_lod_tensor);
    in_lod_tensor->set_lod(lod);
C
chengduoZH 已提交
122

C
chengduoZH 已提交
123
    bc_op_handle_->Run(false);
C
chengduoZH 已提交
124

C
chengduoZH 已提交
125 126 127 128 129 130 131
    WaitAll();

    p::CPUPlace cpu_place;
    for (size_t j = 0; j < gpu_list_.size(); ++j) {
      auto out_var = local_scope_[j]->Var("out");
      auto out_tensor = out_var->Get<f::LoDTensor>();
      PADDLE_ENFORCE_EQ(out_tensor.lod(), lod, "lod is not equal.");
C
chengduoZH 已提交
132

C
chengduoZH 已提交
133 134 135
      f::Tensor result_tensor;
      f::TensorCopy(out_tensor, cpu_place, *(ctxs_[j]), &result_tensor);
      float* ct = result_tensor.mutable_data<float>(cpu_place);
C
chengduoZH 已提交
136

C
chengduoZH 已提交
137 138 139 140
      for (int64_t j = 0; j < f::product(kDims); ++j) {
        ASSERT_NEAR(ct[j], send_vector[j], 1e-5);
      }
    }
C
chengduoZH 已提交
141

C
chengduoZH 已提交
142
    BroadcastOpDestroy();
C
chengduoZH 已提交
143
  }
C
chengduoZH 已提交
144

C
chengduoZH 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  void TestBroadcastSelectedRows() {
    int input_scope_idx = 0;
    BroadcastInitOp<f::SelectedRows>(input_scope_idx);

    auto in_var = local_scope_[input_scope_idx]->Var("input");
    auto in_selected_rows = in_var->GetMutable<f::SelectedRows>();
    auto value = in_selected_rows->mutable_value();
    value->mutable_data<float>(kDims, gpu_list_[input_scope_idx]);
    int height = kDims[0] * 2;
    std::vector<int64_t> rows{0, 1, 2, 3, 3, 0, 14, 7, 3, 1,
                              2, 4, 6, 3, 1, 1, 1,  1, 3, 7};
    in_selected_rows->set_height(height);
    in_selected_rows->set_rows(rows);

    std::vector<float> send_vector(f::product(kDims));
    for (size_t k = 0; k < send_vector.size(); ++k) {
      send_vector[k] = k;
C
chengduoZH 已提交
162
    }
C
chengduoZH 已提交
163 164 165 166
    paddle::framework::TensorFromVector<float>(
        send_vector, *(ctxs_[input_scope_idx]), value);

    bc_op_handle_->Run(false);
C
chengduoZH 已提交
167

C
chengduoZH 已提交
168
    WaitAll();
C
chengduoZH 已提交
169

C
chengduoZH 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    p::CPUPlace cpu_place;
    for (size_t j = 0; j < gpu_list_.size(); ++j) {
      auto out_var = local_scope_[j]->Var("out");
      auto& out_select_rows = out_var->Get<f::SelectedRows>();
      auto rt = out_select_rows.value();

      PADDLE_ENFORCE_EQ(out_select_rows.height(), height,
                        "height is not equal.");
      for (size_t k = 0; k < out_select_rows.rows().size(); ++k) {
        PADDLE_ENFORCE_EQ(out_select_rows.rows()[k], rows[k]);
      }

      f::Tensor result_tensor;
      f::TensorCopy(rt, cpu_place, *(ctxs_[j]), &result_tensor);
      float* ct = result_tensor.data<float>();

      for (int64_t j = 0; j < f::product(kDims); ++j) {
        ASSERT_NEAR(ct[j], send_vector[j], 1e-5);
      }
C
chengduoZH 已提交
189
    }
C
chengduoZH 已提交
190

C
chengduoZH 已提交
191
    BroadcastOpDestroy();
C
chengduoZH 已提交
192 193
  }

C
chengduoZH 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
 public:
  f::Scope g_scope_;
  std::vector<p::DeviceContext*> ctxs_;
  std::vector<f::Scope*> local_scope_;
  std::vector<p::Place> gpu_list_;
  f::details::BroadcastOpHandle* bc_op_handle_;
};

TEST_F(BroadcastTester, TestCPUBroadcastTestLodTensor) {
  InitCtx(false);
  TestBroadcastLodTensor();
}

TEST_F(BroadcastTester, TestCPUBroadcastTestSelectedRows) {
  InitCtx(false);
  TestBroadcastSelectedRows();
}

#ifdef PADDLE_WITH_CUDA
TEST_F(BroadcastTester, TestGPUBroadcastTestLodTensor) {
  InitCtx(true);
  TestBroadcastLodTensor();
}

TEST_F(BroadcastTester, TestGPUBroadcastTestSelectedRows) {
  InitCtx(true);
  TestBroadcastSelectedRows();
C
chengduoZH 已提交
221
}
C
chengduoZH 已提交
222
#endif