broadcast_op_handle_test.h 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//   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.

#pragma once

17
#include <memory>
18
#include <string>
19
#include <unordered_map>
20 21 22 23 24 25 26 27 28 29
#include <vector>

#include "gtest/gtest.h"
#include "paddle/fluid/framework/details/broadcast_op_handle.h"
#include "paddle/fluid/platform/device_context.h"

namespace paddle {
namespace framework {
namespace details {

W
wanghuancoder 已提交
30 31 32
struct DummyVarHandle;
struct VarHandle;

33 34 35
namespace f = paddle::framework;
namespace p = paddle::platform;

36
using DeviceType = paddle::platform::DeviceType;
37

38 39 40 41 42 43 44 45
// test data amount
const f::DDim kDims = {20, 20};

struct TestBroadcastOpHandle {
  std::vector<std::unique_ptr<p::DeviceContext>> ctxs_;
  std::vector<Scope*> local_scopes_;
  std::vector<Scope*> param_scopes_;
  Scope g_scope_;
X
Xin Pan 已提交
46 47 48
  OpHandleBase* op_handle_;
  std::vector<VarHandleBase*> vars_;
  std::vector<std::unique_ptr<ir::Node>> nodes_;
49
  std::vector<p::Place> place_list_;
50
  DeviceType use_device_;
51
#if defined(PADDLE_WITH_NCCL)
52 53 54
  std::unique_ptr<platform::NCCLContextMap> nccl_ctxs_;
#endif

55 56 57 58
#if defined(PADDLE_WITH_XPU_BKCL)
  std::unique_ptr<platform::BKCLContextMap> bkcl_ctxs_;
#endif

59 60 61 62
  void WaitAll() {
    for (size_t j = 0; j < ctxs_.size(); ++j) {
      ctxs_[j]->Wait();
    }
63
#if defined(PADDLE_WITH_NCCL)
64 65 66
    if (nccl_ctxs_) {
      nccl_ctxs_->WaitAll();
    }
67 68 69 70 71
#endif
#if defined(PADDLE_WITH_XPU_BKCL)
    if (bkcl_ctxs_) {
      bkcl_ctxs_->WaitAll();
    }
72 73 74
#endif
  }

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  void InitCtxOnDevice(DeviceType use_device) {
    use_device_ = use_device;
    if (use_device_ == p::kXPU) {
#if defined(PADDLE_WITH_XPU_BKCL)
      int count = p::GetXPUDeviceCount();
      if (count <= 1) {
        LOG(WARNING) << "Cannot test multi-xpu Broadcast, because the XPU "
                        "device count is "
                     << count;
        exit(0);
      }
      for (int i = 0; i < count; ++i) {
        auto p = p::XPUPlace(i);
        place_list_.push_back(p);
        ctxs_.emplace_back(new p::XPUDeviceContext(p));
      }
      bkcl_ctxs_.reset(new platform::BKCLContextMap(place_list_));
#else
      PADDLE_THROW(
          platform::errors::PreconditionNotMet("Not compiled with BKCL."));
#endif
    } else if (use_device_ == p::kCUDA) {
97
#if defined(PADDLE_WITH_NCCL)
98 99 100 101 102 103 104 105 106 107 108 109 110 111
      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);
        place_list_.push_back(p);
        ctxs_.emplace_back(new p::CUDADeviceContext(p));
      }
      nccl_ctxs_.reset(new platform::NCCLContextMap(place_list_));
#else
112 113
      PADDLE_THROW(
          platform::errors::PreconditionNotMet("Not compiled with NCLL."));
114 115 116 117 118 119 120 121
#endif
    } else {
      int count = 8;
      for (int i = 0; i < count; ++i) {
        auto p = p::CPUPlace();
        place_list_.push_back(p);
        ctxs_.emplace_back(new p::CPUDeviceContext(p));
      }
122 123 124
#if defined(PADDLE_WITH_XPU_BKCL)
      bkcl_ctxs_.reset(nullptr);
#endif
125
#if defined(PADDLE_WITH_NCCL)
126 127 128 129 130 131
      nccl_ctxs_.reset(nullptr);
#endif
    }
  }

  void InitBroadcastOp(size_t input_scope_idx) {
X
Xin Pan 已提交
132
    nodes_.clear();
133
    std::unordered_map<Scope*, Scope*> scope_map;
134 135 136 137 138
    for (size_t j = 0; j < place_list_.size(); ++j) {
      local_scopes_.push_back(&(g_scope_.NewScope()));
      Scope& local_scope = local_scopes_.back()->NewScope();
      local_scope.Var("out");
      param_scopes_.emplace_back(&local_scope);
139
      scope_map.emplace(local_scopes_.back(), param_scopes_.back());
140 141 142
    }
    param_scopes_[input_scope_idx]->Var("input");

X
Xin Pan 已提交
143 144
    nodes_.emplace_back(
        ir::CreateNodeForTest("node0", ir::Node::Type::kOperation));
145
    if (use_device_ == p::kCUDA) {
146
#if defined(PADDLE_WITH_NCCL)
X
Xin Pan 已提交
147 148
      op_handle_ = new BroadcastOpHandle(nodes_.back().get(), local_scopes_,
                                         place_list_, nccl_ctxs_.get());
149
#else
150
      PADDLE_THROW(
151
          platform::errors::PreconditionNotMet("Not compiled with NCCL."));
152
#endif
153 154
    } else if (use_device_ == p::kXPU) {
#if defined(PADDLE_WITH_XPU_BKCL)
X
Xin Pan 已提交
155
      op_handle_ = new BroadcastOpHandle(nodes_.back().get(), local_scopes_,
156
                                         place_list_, bkcl_ctxs_.get());
157
#else
158 159 160 161
      PADDLE_THROW(
          platform::errors::PreconditionNotMet("Not compiled with BKCL."));
#endif
    } else {
X
Xin Pan 已提交
162 163
      op_handle_ = new BroadcastOpHandle(nodes_.back().get(), local_scopes_,
                                         place_list_);
164 165
    }

166 167
    op_handle_->SetLocalExecScopes(scope_map);

X
Xin Pan 已提交
168 169 170 171
    nodes_.emplace_back(
        ir::CreateNodeForTest("node1", ir::Node::Type::kVariable));
    auto* in_var_handle = new VarHandle(nodes_.back().get(), 1, input_scope_idx,
                                        "input", place_list_[input_scope_idx]);
172 173 174 175 176
    vars_.emplace_back(in_var_handle);
    op_handle_->AddInput(in_var_handle);

    // add dummy var

X
Xin Pan 已提交
177 178 179
    nodes_.emplace_back(
        ir::CreateNodeForTest("node2", ir::Node::Type::kVariable));
    vars_.emplace_back(new DummyVarHandle(nodes_.back().get()));
180
    DummyVarHandle* dummy_var_handle =
X
Xin Pan 已提交
181
        static_cast<DummyVarHandle*>(vars_.back());
182 183 184 185
    dummy_var_handle->ClearGeneratedOp();
    op_handle_->AddInput(dummy_var_handle);

    for (size_t j = 0; j < place_list_.size(); ++j) {
186
      if (use_device_ != p::kCUDA) {
187 188
        op_handle_->SetDeviceContext(place_list_[j], ctxs_[j].get());
      }
X
Xin Pan 已提交
189 190
      nodes_.emplace_back(
          ir::CreateNodeForTest("node3", ir::Node::Type::kVariable));
191
      VarHandle* out_var_handle =
X
Xin Pan 已提交
192
          new VarHandle(nodes_.back().get(), 2, j, "out", place_list_[j]);
193 194 195 196 197
      vars_.emplace_back(out_var_handle);
      op_handle_->AddOutput(out_var_handle);
    }

    // add dummy var
X
Xin Pan 已提交
198 199 200
    nodes_.emplace_back(
        ir::CreateNodeForTest("node4", ir::Node::Type::kVariable));
    vars_.emplace_back(new DummyVarHandle(nodes_.back().get()));
201
    DummyVarHandle* out_dummy_var_handle =
X
Xin Pan 已提交
202
        static_cast<DummyVarHandle*>(vars_.back());
203 204 205 206 207 208 209 210 211
    out_dummy_var_handle->ClearGeneratedOp();
    op_handle_->AddOutput(out_dummy_var_handle);
  }

  std::vector<float> InitLoDTensor(const std::string& varname,
                                   size_t input_scope_idx, const f::LoD& lod,
                                   float val_scalar = 0.0) {
    auto var = param_scopes_[input_scope_idx]->FindVar(varname);

212 213 214
    PADDLE_ENFORCE_NOT_NULL(
        var, platform::errors::NotFound("Variable %s is not found in scope.",
                                        varname));
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    auto lod_tensor = var->GetMutable<f::LoDTensor>();
    std::vector<float> send_vector(static_cast<size_t>(f::product(kDims)));
    for (size_t k = 0; k < send_vector.size(); ++k) {
      send_vector[k] = k + val_scalar;
    }
    paddle::framework::TensorFromVector<float>(
        send_vector, *(ctxs_[input_scope_idx]), lod_tensor);
    lod_tensor->set_lod(lod);
    lod_tensor->Resize(kDims);
    return send_vector;
  }

  std::vector<float> InitSelectedRows(const std::string& varname,
                                      size_t input_scope_idx,
                                      const std::vector<int64_t>& rows,
                                      int height, float value_scalar = 0.0) {
    std::vector<float> send_vector(static_cast<size_t>(f::product(kDims)));
    for (size_t k = 0; k < send_vector.size(); ++k) {
      send_vector[k] = k + value_scalar;
    }

    auto var = param_scopes_[input_scope_idx]->FindVar(varname);
237 238 239
    PADDLE_ENFORCE_NOT_NULL(
        var, platform::errors::NotFound("Variable %s is not found in scope.",
                                        varname));
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    auto selected_rows = var->GetMutable<f::SelectedRows>();
    auto value = selected_rows->mutable_value();
    value->mutable_data<float>(kDims, place_list_[input_scope_idx]);
    selected_rows->set_height(height);
    selected_rows->set_rows(rows);

    paddle::framework::TensorFromVector<float>(
        send_vector, *(ctxs_[input_scope_idx]), value);

    return send_vector;
  }

  void SelectedRowsEqual(const std::string& varname, int input_scope_idx,
                         const std::vector<float>& send_vector,
                         const std::vector<int64_t>& rows, int height) {
    auto var = param_scopes_[input_scope_idx]->FindVar(varname);
256 257 258
    PADDLE_ENFORCE_NOT_NULL(
        var, platform::errors::NotFound("Variable %s is not found in scope.",
                                        varname));
259 260
    auto& selected_rows = var->Get<f::SelectedRows>();
    auto rt = selected_rows.value();
261 262 263 264 265
    PADDLE_ENFORCE_EQ(selected_rows.height(), height,
                      platform::errors::InvalidArgument(
                          "The height of SelectedRows is not equal to "
                          "the expected, expect %d, but got %ld.",
                          height, selected_rows.height()));
266 267

    for (size_t k = 0; k < selected_rows.rows().size(); ++k) {
268 269 270 271 272 273
      PADDLE_ENFORCE_EQ(
          selected_rows.rows()[k], rows[k],
          platform::errors::InvalidArgument(
              "The item at position %zu of rows of SelectedRows "
              "is not equal to the expected, expect %ld, but got %ld.",
              k, rows[k], selected_rows.rows()[k]));
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    }

    p::CPUPlace cpu_place;
    f::Tensor result_tensor;
    f::TensorCopySync(rt, cpu_place, &result_tensor);
    float* ct = result_tensor.data<float>();

    for (int64_t i = 0; i < f::product(kDims); ++i) {
      ASSERT_NEAR(ct[i], send_vector[i], 1e-5);
    }
  }

  void LoDTensorEqual(const std::string& varname,
                      const std::vector<float>& send_vec, const f::LoD& lod,
                      framework::Scope* scope) {
    p::CPUPlace cpu_place;
    auto var = scope->FindVar(varname);
291 292 293
    PADDLE_ENFORCE_NOT_NULL(
        var, platform::errors::NotFound("Variable %s is not found in scope.",
                                        varname));
294
    auto tensor = var->Get<f::LoDTensor>();
295 296 297 298 299
    PADDLE_ENFORCE_EQ(tensor.lod(), lod,
                      platform::errors::InvalidArgument(
                          "The LoD of tensor is not equal to "
                          "the expected, expect %s, but got %s.",
                          lod, tensor.lod()));
300 301 302 303 304 305 306 307 308 309 310 311
    f::Tensor result_tensor;
    f::TensorCopySync(tensor, cpu_place, &result_tensor);
    float* ct = result_tensor.mutable_data<float>(cpu_place);
    for (int64_t k = 0; k < f::product(kDims); ++k) {
      ASSERT_NEAR(ct[k], send_vec[k], 1e-5);
    }
  }

  void TestBroadcastLodTensor(size_t input_scope_idx) {
    f::LoD lod{{0, 10, 20}};
    auto send_vector = InitLoDTensor("input", input_scope_idx, lod);

312
    DeviceType use_device = p::kCPU;
313
    op_handle_->Run(use_device);
314 315 316 317 318 319 320 321 322 323 324 325 326

    WaitAll();
    for (size_t j = 0; j < place_list_.size(); ++j) {
      LoDTensorEqual("out", send_vector, lod, param_scopes_[j]);
    }
  }

  void TestBroadcastSelectedRows(size_t input_scope_idx) {
    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};
    int height = static_cast<int>(kDims[0] * 2);
    auto send_vector = InitSelectedRows("input", input_scope_idx, rows, height);

327
    DeviceType use_device = p::kCPU;
328
    op_handle_->Run(use_device);
329 330 331 332 333 334 335 336 337 338 339

    WaitAll();
    for (size_t j = 0; j < place_list_.size(); ++j) {
      SelectedRowsEqual("out", input_scope_idx, send_vector, rows, height);
    }
  }
};

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