broadcast_op_handle_test.cc 8.9 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
#include "gtest/gtest.h"

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

C
chengduoZH 已提交
20 21 22 23
namespace paddle {
namespace framework {
namespace details {

C
chengduoZH 已提交
24 25 26 27 28 29
namespace f = paddle::framework;
namespace p = paddle::platform;

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

30 31 32
struct TestBroadcastOpHandle {
  std::vector<std::unique_ptr<p::DeviceContext>> ctxs_;
  std::vector<Scope*> local_scopes_;
C
chengduoZH 已提交
33
  std::vector<Scope*> param_scopes_;
34 35 36 37
  Scope g_scope_;
  std::unique_ptr<OpHandleBase> op_handle_;
  std::vector<std::unique_ptr<VarHandleBase>> vars_;
  std::vector<p::Place> gpu_list_;
C
chengduoZH 已提交
38 39 40 41
  bool use_gpu_;
#ifdef PADDLE_WITH_CUDA
  std::unique_ptr<platform::NCCLContextMap> nccl_ctxs_;
#endif
42 43 44 45 46

  void WaitAll() {
    for (size_t j = 0; j < ctxs_.size(); ++j) {
      ctxs_[j]->Wait();
    }
C
chengduoZH 已提交
47 48 49 50 51
#ifdef PADDLE_WITH_CUDA
    if (nccl_ctxs_) {
      nccl_ctxs_->WaitAll();
    }
#endif
52 53
  }

C
chengduoZH 已提交
54
  void InitCtxOnGpu(bool use_gpu) {
C
chengduoZH 已提交
55 56
    use_gpu_ = use_gpu;
    if (use_gpu_) {
C
chengduoZH 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69
#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));
      }
C
chengduoZH 已提交
70
      nccl_ctxs_.reset(new platform::NCCLContextMap(gpu_list_));
C
chengduoZH 已提交
71 72 73 74 75 76 77 78 79 80
#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 已提交
81 82 83
#ifdef PADDLE_WITH_CUDA
      nccl_ctxs_.reset(nullptr);
#endif
C
chengduoZH 已提交
84 85 86
    }
  }

87
  void InitBroadcastOp(size_t input_scope_idx) {
C
chengduoZH 已提交
88
    for (size_t j = 0; j < gpu_list_.size(); ++j) {
89
      local_scopes_.push_back(&(g_scope_.NewScope()));
C
chengduoZH 已提交
90 91 92 93 94 95
      Scope& local_scope = local_scopes_.back()->NewScope();
      *local_scopes_.back()
           ->Var(details::kLocalExecScopeName)
           ->GetMutable<Scope*>() = &local_scope;
      local_scope.Var("out");
      param_scopes_.emplace_back(&local_scope);
C
chengduoZH 已提交
96
    }
C
chengduoZH 已提交
97
    param_scopes_[input_scope_idx]->Var("input");
C
chengduoZH 已提交
98

X
Xin Pan 已提交
99
    std::unique_ptr<ir::Node> n(new ir::Node(ir::Node::Type::kOperation));
C
chengduoZH 已提交
100 101
    if (use_gpu_) {
#ifdef PADDLE_WITH_CUDA
X
Xin Pan 已提交
102 103
      op_handle_.reset(new BroadcastOpHandle(n.get(), local_scopes_, gpu_list_,
                                             nccl_ctxs_.get()));
C
chengduoZH 已提交
104 105 106 107 108
#else
      PADDLE_THROW("CUDA is not support.");
#endif
    } else {
#ifdef PADDLE_WITH_CUDA
X
Xin Pan 已提交
109 110
      op_handle_.reset(new BroadcastOpHandle(n.get(), local_scopes_, gpu_list_,
                                             nccl_ctxs_.get()));
C
chengduoZH 已提交
111
#else
X
Xin Pan 已提交
112 113
      op_handle_.reset(
          new BroadcastOpHandle(n.get(), local_scopes_, gpu_list_));
C
chengduoZH 已提交
114 115
#endif
    }
C
chengduoZH 已提交
116

X
Xin Pan 已提交
117 118 119
    std::unique_ptr<ir::Node> v(new ir::Node(ir::Node::Type::kVariable));
    auto* in_var_handle = new VarHandle(v.get(), 1, input_scope_idx, "input",
                                        gpu_list_[input_scope_idx]);
Y
Yu Yang 已提交
120
    vars_.emplace_back(in_var_handle);
121 122 123
    op_handle_->AddInput(in_var_handle);

    // add dummy var
X
Xin Pan 已提交
124 125 126

    std::unique_ptr<ir::Node> v2(new ir::Node(ir::Node::Type::kVariable));
    vars_.emplace_back(new DummyVarHandle(v2.get()));
127 128
    DummyVarHandle* dummy_var_handle =
        static_cast<DummyVarHandle*>(vars_.back().get());
X
Xin Pan 已提交
129
    dummy_var_handle->ClearGeneratedOp();
130
    op_handle_->AddInput(dummy_var_handle);
C
chengduoZH 已提交
131 132

    for (size_t j = 0; j < gpu_list_.size(); ++j) {
C
chengduoZH 已提交
133 134 135
      if (!use_gpu_) {
        op_handle_->SetDeviceContext(gpu_list_[j], ctxs_[j].get());
      }
X
Xin Pan 已提交
136 137 138
      std::unique_ptr<ir::Node> v3(new ir::Node(ir::Node::Type::kVariable));
      VarHandle* out_var_handle =
          new VarHandle(v3.get(), 2, j, "out", gpu_list_[j]);
Y
Yu Yang 已提交
139
      vars_.emplace_back(out_var_handle);
140
      op_handle_->AddOutput(out_var_handle);
C
chengduoZH 已提交
141 142
    }

143
    // add dummy var
X
Xin Pan 已提交
144 145
    std::unique_ptr<ir::Node> v4(new ir::Node(ir::Node::Type::kVariable));
    vars_.emplace_back(new DummyVarHandle(v4.get()));
146 147
    DummyVarHandle* out_dummy_var_handle =
        static_cast<DummyVarHandle*>(vars_.back().get());
X
Xin Pan 已提交
148
    out_dummy_var_handle->ClearGeneratedOp();
149
    op_handle_->AddOutput(out_dummy_var_handle);
C
chengduoZH 已提交
150
  }
C
chengduoZH 已提交
151

152
  void TestBroadcastLodTensor(size_t input_scope_idx) {
C
chengduoZH 已提交
153 154
    auto in_var = param_scopes_[input_scope_idx]->FindVar("input");
    PADDLE_ENFORCE_NOT_NULL(in_var);
C
chengduoZH 已提交
155 156
    auto in_lod_tensor = in_var->GetMutable<f::LoDTensor>();
    in_lod_tensor->mutable_data<float>(kDims, gpu_list_[input_scope_idx]);
C
chengduoZH 已提交
157

158
    std::vector<float> send_vector(static_cast<size_t>(f::product(kDims)));
C
chengduoZH 已提交
159 160
    for (size_t k = 0; k < send_vector.size(); ++k) {
      send_vector[k] = k;
C
chengduoZH 已提交
161
    }
C
chengduoZH 已提交
162 163 164 165
    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 已提交
166
    in_lod_tensor->Resize(kDims);
C
chengduoZH 已提交
167

168
    op_handle_->Run(false);
C
chengduoZH 已提交
169

C
chengduoZH 已提交
170 171 172 173
    WaitAll();

    p::CPUPlace cpu_place;
    for (size_t j = 0; j < gpu_list_.size(); ++j) {
C
chengduoZH 已提交
174 175
      auto out_var = param_scopes_[j]->FindVar("out");
      PADDLE_ENFORCE_NOT_NULL(out_var);
C
chengduoZH 已提交
176 177
      auto out_tensor = out_var->Get<f::LoDTensor>();
      PADDLE_ENFORCE_EQ(out_tensor.lod(), lod, "lod is not equal.");
C
chengduoZH 已提交
178

C
chengduoZH 已提交
179
      f::Tensor result_tensor;
F
fengjiayi 已提交
180
      f::TensorCopySync(out_tensor, cpu_place, &result_tensor);
C
chengduoZH 已提交
181
      float* ct = result_tensor.mutable_data<float>(cpu_place);
C
chengduoZH 已提交
182

183 184
      for (int64_t i = 0; i < f::product(kDims); ++i) {
        ASSERT_NEAR(ct[i], send_vector[i], 1e-5);
C
chengduoZH 已提交
185 186 187
      }
    }
  }
C
chengduoZH 已提交
188

189
  void TestBroadcastSelectedRows(size_t input_scope_idx) {
C
chengduoZH 已提交
190 191
    auto in_var = param_scopes_[input_scope_idx]->FindVar("input");
    PADDLE_ENFORCE_NOT_NULL(in_var);
C
chengduoZH 已提交
192 193 194
    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]);
195
    int height = static_cast<int>(kDims[0]) * 2;
C
chengduoZH 已提交
196 197 198 199 200
    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);

201
    std::vector<float> send_vector(static_cast<size_t>(f::product(kDims)));
C
chengduoZH 已提交
202 203
    for (size_t k = 0; k < send_vector.size(); ++k) {
      send_vector[k] = k;
C
chengduoZH 已提交
204
    }
C
chengduoZH 已提交
205 206 207
    paddle::framework::TensorFromVector<float>(
        send_vector, *(ctxs_[input_scope_idx]), value);

208
    op_handle_->Run(false);
C
chengduoZH 已提交
209

C
chengduoZH 已提交
210
    WaitAll();
C
chengduoZH 已提交
211

C
chengduoZH 已提交
212 213
    p::CPUPlace cpu_place;
    for (size_t j = 0; j < gpu_list_.size(); ++j) {
C
chengduoZH 已提交
214 215
      auto out_var = param_scopes_[j]->FindVar("out");
      PADDLE_ENFORCE_NOT_NULL(out_var);
C
chengduoZH 已提交
216 217 218 219 220 221 222 223 224 225
      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
fengjiayi 已提交
226
      f::TensorCopySync(rt, cpu_place, &result_tensor);
C
chengduoZH 已提交
227 228
      float* ct = result_tensor.data<float>();

229 230
      for (int64_t i = 0; i < f::product(kDims); ++i) {
        ASSERT_NEAR(ct[i], send_vector[i], 1e-5);
C
chengduoZH 已提交
231
      }
C
chengduoZH 已提交
232 233
    }
  }
C
chengduoZH 已提交
234 235
};

236 237 238 239 240 241
TEST(BroadcastTester, TestCPUBroadcastTestLodTensor) {
  TestBroadcastOpHandle test_op;
  size_t input_scope_idx = 0;
  test_op.InitCtxOnGpu(false);
  test_op.InitBroadcastOp(input_scope_idx);
  test_op.TestBroadcastLodTensor(input_scope_idx);
C
chengduoZH 已提交
242 243
}

244 245 246 247 248 249
TEST(BroadcastTester, TestCPUBroadcastTestSelectedRows) {
  TestBroadcastOpHandle test_op;
  size_t input_scope_idx = 0;
  test_op.InitCtxOnGpu(false);
  test_op.InitBroadcastOp(input_scope_idx);
  test_op.TestBroadcastSelectedRows(input_scope_idx);
C
chengduoZH 已提交
250 251 252
}

#ifdef PADDLE_WITH_CUDA
253 254 255 256 257 258
TEST(BroadcastTester, TestGPUBroadcastTestLodTensor) {
  TestBroadcastOpHandle test_op;
  size_t input_scope_idx = 0;
  test_op.InitCtxOnGpu(true);
  test_op.InitBroadcastOp(input_scope_idx);
  test_op.TestBroadcastLodTensor(input_scope_idx);
C
chengduoZH 已提交
259 260
}

261 262 263 264 265 266
TEST(BroadcastTester, TestGPUBroadcastTestSelectedRows) {
  TestBroadcastOpHandle test_op;
  size_t input_scope_idx = 0;
  test_op.InitCtxOnGpu(true);
  test_op.InitBroadcastOp(input_scope_idx);
  test_op.TestBroadcastSelectedRows(input_scope_idx);
C
chengduoZH 已提交
267
}
C
chengduoZH 已提交
268
#endif
C
chengduoZH 已提交
269 270 271 272

}  // namespace details
}  // namespace framework
}  // namespace paddle