reduce_op_handle_test.cc 10.8 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//   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.

#include "paddle/fluid/framework/details/reduce_op_handle.h"
16

17
#include <unordered_map>
18

C
chengduoZH 已提交
19 20 21 22 23 24 25 26 27
#include "gtest/gtest.h"
#include "paddle/fluid/platform/device_context.h"

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

28
using DeviceType = paddle::platform::DeviceType;
29

C
chengduoZH 已提交
30 31 32 33 34 35 36
// test data amount
const f::DDim kDims = {20, 20};

struct TestReduceOpHandle {
  bool use_gpu_;
  Scope g_scope_;
  std::vector<Scope *> local_scopes_;
C
chengduoZH 已提交
37
  std::vector<Scope *> param_scopes_;
X
Xin Pan 已提交
38 39
  OpHandleBase *op_handle_;
  std::vector<VarHandleBase *> vars_;
C
chengduoZH 已提交
40 41 42
  std::vector<p::Place> gpu_list_;
  std::vector<std::unique_ptr<p::DeviceContext>> ctxs_;

43
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
C
chengduoZH 已提交
44 45 46 47 48 49 50
  std::unique_ptr<platform::NCCLContextMap> nccl_ctxs_;
#endif

  void WaitAll() {
    for (size_t j = 0; j < ctxs_.size(); ++j) {
      ctxs_[j]->Wait();
    }
51
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
C
chengduoZH 已提交
52 53 54
    if (nccl_ctxs_) {
      nccl_ctxs_->WaitAll();
    }
C
chengduoZH 已提交
55 56 57 58 59 60
#endif
  }

  void InitCtxOnGpu(bool use_gpu) {
    use_gpu_ = use_gpu;
    if (use_gpu) {
61
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
62
      int count = p::GetGPUDeviceCount();
C
chengduoZH 已提交
63 64 65 66 67 68 69 70 71
      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);
L
Leo Chen 已提交
72
        ctxs_.emplace_back(new phi::GPUContext(p));
C
chengduoZH 已提交
73
      }
C
chengduoZH 已提交
74
      nccl_ctxs_.reset(new platform::NCCLContextMap(gpu_list_));
C
chengduoZH 已提交
75
#else
76 77
      PADDLE_THROW(
          platform::errors::PreconditionNotMet("Not compiled with NCLL."));
C
chengduoZH 已提交
78 79 80 81 82 83
#endif
    } else {
      int count = 8;
      for (int i = 0; i < count; ++i) {
        auto p = p::CPUPlace();
        gpu_list_.push_back(p);
L
Leo Chen 已提交
84
        ctxs_.emplace_back(new phi::CPUContext(p));
C
chengduoZH 已提交
85
      }
86
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
C
chengduoZH 已提交
87
      nccl_ctxs_.reset(nullptr);
C
chengduoZH 已提交
88
#endif
C
chengduoZH 已提交
89
    }
C
chengduoZH 已提交
90 91
  }

C
chengduoZH 已提交
92
  void InitReduceOp(size_t out_scope_idx) {
X
Xin Pan 已提交
93
    std::vector<std::unique_ptr<ir::Node>> nodes;
C
chengduoZH 已提交
94
    // init scope
95
    std::unordered_map<Scope *, Scope *> scope_map;
C
chengduoZH 已提交
96 97
    for (size_t j = 0; j < gpu_list_.size(); ++j) {
      local_scopes_.push_back(&(g_scope_.NewScope()));
C
chengduoZH 已提交
98 99 100
      Scope &local_scope = local_scopes_.back()->NewScope();
      local_scope.Var("input");
      param_scopes_.emplace_back(&local_scope);
101
      scope_map.emplace(local_scopes_.back(), param_scopes_.back());
C
chengduoZH 已提交
102
    }
C
chengduoZH 已提交
103
    param_scopes_[out_scope_idx]->Var("out");
C
chengduoZH 已提交
104

X
Xin Pan 已提交
105
    nodes.emplace_back(new ir::Node("node"));
C
chengduoZH 已提交
106
    if (use_gpu_) {
107
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
108 109
      op_handle_.reset(new ReduceOpHandle(
          nodes.back().get(), local_scopes_, gpu_list_, nccl_ctxs_.get()));
C
chengduoZH 已提交
110
#else
111 112
      PADDLE_THROW(
          platform::errors::PreconditionNotMet("Not compiled with NCLL."));
C
chengduoZH 已提交
113 114
#endif
    } else {
115
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
116 117
      op_handle_.reset(new ReduceOpHandle(
          nodes.back().get(), local_scopes_, gpu_list_, nccl_ctxs_.get()));
C
chengduoZH 已提交
118
#else
X
Xin Pan 已提交
119 120
      op_handle_.reset(
          new ReduceOpHandle(nodes.back().get(), local_scopes_, gpu_list_));
C
chengduoZH 已提交
121
#endif
C
chengduoZH 已提交
122
    }
C
chengduoZH 已提交
123

124 125
    op_handle_->SetLocalExecScopes(scope_map);

C
chengduoZH 已提交
126
    // init op handle
C
chengduoZH 已提交
127 128
    // add input
    for (size_t j = 0; j < gpu_list_.size(); ++j) {
C
chengduoZH 已提交
129
      if (!use_gpu_) {
C
chengduoZH 已提交
130
        op_handle_->SetDeviceContext(gpu_list_[j], ctxs_[j].get());
C
chengduoZH 已提交
131
      }
X
Xin Pan 已提交
132
      nodes.emplace_back(new ir::Node("node1"));
X
Xin Pan 已提交
133 134 135
      auto *in_var_handle =
          new VarHandle(nodes.back().get(), 1, j, "input", gpu_list_[j]);
      in_var_handle->ClearGeneratedOp();
136
      vars_.emplace_back(in_var_handle);
C
chengduoZH 已提交
137 138 139 140 141 142 143
      op_handle_->AddInput(in_var_handle);
    }

    // add dummy var
    vars_.emplace_back(new DummyVarHandle());
    DummyVarHandle *in_dummy_var_handle =
        static_cast<DummyVarHandle *>(vars_.back().get());
X
Xin Pan 已提交
144
    in_dummy_var_handle->ClearGeneratedOp();
C
chengduoZH 已提交
145 146 147
    op_handle_->AddInput(in_dummy_var_handle);

    // add output
X
Xin Pan 已提交
148
    nodes.emplace_back(new ir::Node("node2"));
149 150
    auto *out_var_handle = new VarHandle(
        nodes.back().get(), 2, out_scope_idx, "out", gpu_list_[out_scope_idx]);
151
    vars_.emplace_back(out_var_handle);
C
chengduoZH 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164
    op_handle_->AddOutput(out_var_handle);

    // add dummy var
    vars_.emplace_back(new DummyVarHandle());
    DummyVarHandle *dummy_var_handle =
        static_cast<DummyVarHandle *>(vars_.back().get());
    op_handle_->AddOutput(dummy_var_handle);
  }

  void TestReduceSelectedRows(size_t output_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};
165
    std::vector<float> send_vector(phi::product(kDims));
C
chengduoZH 已提交
166 167 168 169 170 171
    for (size_t k = 0; k < send_vector.size(); ++k) {
      send_vector[k] = k;
    }

    for (size_t input_scope_idx = 0; input_scope_idx < gpu_list_.size();
         ++input_scope_idx) {
C
chengduoZH 已提交
172
      auto in_var = param_scopes_[input_scope_idx]->FindVar("input");
173 174

      PADDLE_ENFORCE_NOT_NULL(
175 176 177
          in_var,
          platform::errors::NotFound("Variable %s is not found in scope.",
                                     "input"));
178
      auto in_selected_rows = in_var->GetMutable<phi::SelectedRows>();
C
chengduoZH 已提交
179 180 181 182 183 184 185 186 187 188 189
      auto value = in_selected_rows->mutable_value();
      value->mutable_data<float>(kDims, gpu_list_[input_scope_idx]);

      in_selected_rows->set_height(height);
      in_selected_rows->set_rows(rows);

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

C
chengduoZH 已提交
190
    auto out_var = param_scopes_[output_scope_idx]->FindVar("out");
191 192 193
    PADDLE_ENFORCE_NOT_NULL(out_var,
                            platform::errors::NotFound(
                                "Variable %s is not found in scope.", "out"));
194
    auto out_selected_rows = out_var->GetMutable<phi::SelectedRows>();
C
chengduoZH 已提交
195

C
chengduoZH 已提交
196
    auto in_var = param_scopes_[output_scope_idx]->FindVar("input");
197
    auto in_selected_rows = in_var->GetMutable<phi::SelectedRows>();
C
chengduoZH 已提交
198 199 200 201

    out_selected_rows->mutable_value()->ShareDataWith(
        in_selected_rows->value());

202
    DeviceType use_device = p::kCPU;
203
    op_handle_->Run(use_device);
C
chengduoZH 已提交
204 205 206 207 208

    WaitAll();

    p::CPUPlace cpu_place;

209
    auto &out_select_rows = out_var->Get<phi::SelectedRows>();
C
chengduoZH 已提交
210 211
    auto rt = out_select_rows.value();

212 213
    PADDLE_ENFORCE_EQ(out_select_rows.height(),
                      height,
214 215 216
                      platform::errors::InvalidArgument(
                          "The height of SelectedRows is not equal to "
                          "the expected, expect %d, but got %d.",
217 218
                          height,
                          out_select_rows.height()));
C
chengduoZH 已提交
219
    for (size_t k = 0; k < out_select_rows.rows().size(); ++k) {
220
      PADDLE_ENFORCE_EQ(
221 222
          out_select_rows.rows()[k],
          rows[k % rows.size()],
223 224 225
          platform::errors::InvalidArgument(
              "The item at position %d of rows of SelectedRows is not equal to "
              "the expected, expect %d, but got %d.",
226 227 228
              k,
              rows[k % rows.size()],
              out_select_rows.rows()[k]));
C
chengduoZH 已提交
229 230 231
    }

    f::Tensor result_tensor;
F
fengjiayi 已提交
232
    f::TensorCopySync(rt, cpu_place, &result_tensor);
C
chengduoZH 已提交
233 234
    float *ct = result_tensor.data<float>();

235
    for (int64_t j = 0; j < phi::product(result_tensor.dims()); ++j) {
C
chengduoZH 已提交
236 237
      ASSERT_NEAR(ct[j], send_vector[j % send_vector.size()], 1e-5);
    }
238
  }  // namespace details
C
chengduoZH 已提交
239 240

  void TestReduceLodTensors(size_t output_scope_idx) {
241
    std::vector<float> send_vector(static_cast<size_t>(phi::product(kDims)));
C
chengduoZH 已提交
242 243 244 245 246 247 248
    for (size_t k = 0; k < send_vector.size(); ++k) {
      send_vector[k] = k;
    }
    f::LoD lod{{0, 10, 20}};

    for (size_t input_scope_idx = 0; input_scope_idx < gpu_list_.size();
         ++input_scope_idx) {
C
chengduoZH 已提交
249
      auto in_var = param_scopes_[input_scope_idx]->FindVar("input");
250
      PADDLE_ENFORCE_NOT_NULL(
251 252 253
          in_var,
          platform::errors::NotFound("Variable %s is not found in scope.",
                                     "input"));
C
chengduoZH 已提交
254 255 256 257 258 259 260 261
      auto in_lod_tensor = in_var->GetMutable<f::LoDTensor>();
      in_lod_tensor->mutable_data<float>(kDims, gpu_list_[input_scope_idx]);
      in_lod_tensor->set_lod(lod);

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

C
chengduoZH 已提交
262
    auto out_var = param_scopes_[output_scope_idx]->FindVar("out");
263 264 265
    PADDLE_ENFORCE_NOT_NULL(out_var,
                            platform::errors::NotFound(
                                "Variable %s is not found in scope.", "out"));
C
chengduoZH 已提交
266 267
    auto out_lodtensor = out_var->GetMutable<f::LoDTensor>();

C
chengduoZH 已提交
268
    auto in_var = param_scopes_[output_scope_idx]->FindVar("input");
C
chengduoZH 已提交
269 270 271 272
    auto in_lodtensor = in_var->Get<f::LoDTensor>();

    out_lodtensor->ShareDataWith(in_lodtensor);

273
    DeviceType use_device = p::kCPU;
274
    op_handle_->Run(use_device);
C
chengduoZH 已提交
275 276 277 278 279 280 281 282

    WaitAll();

    p::CPUPlace cpu_place;

    auto &rt = out_var->Get<f::LoDTensor>();

    f::Tensor result_tensor;
F
fengjiayi 已提交
283
    f::TensorCopySync(rt, cpu_place, &result_tensor);
C
chengduoZH 已提交
284 285
    float *ct = result_tensor.data<float>();

286
    for (int64_t j = 0; j < phi::product(result_tensor.dims()); ++j) {
C
chengduoZH 已提交
287 288 289
      ASSERT_NEAR(ct[j], send_vector[j] * gpu_list_.size(), 1e-5);
    }
  }
290
};  // namespace details
C
chengduoZH 已提交
291 292 293

TEST(ReduceTester, TestCPUReduceTestSelectedRows) {
  TestReduceOpHandle test_op;
C
chengduoZH 已提交
294
  size_t out_scope_idx = 0;
C
chengduoZH 已提交
295
  test_op.InitCtxOnGpu(false);
C
chengduoZH 已提交
296 297
  test_op.InitReduceOp(out_scope_idx);
  test_op.TestReduceSelectedRows(out_scope_idx);
C
chengduoZH 已提交
298
}
C
chengduoZH 已提交
299 300
TEST(ReduceTester, TestCPUReduceTestLodTensor) {
  TestReduceOpHandle test_op;
C
chengduoZH 已提交
301
  size_t out_scope_idx = 0;
C
chengduoZH 已提交
302
  test_op.InitCtxOnGpu(false);
C
chengduoZH 已提交
303 304
  test_op.InitReduceOp(out_scope_idx);
  test_op.TestReduceLodTensors(out_scope_idx);
C
chengduoZH 已提交
305
}
306
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
C
chengduoZH 已提交
307

C
chengduoZH 已提交
308 309
TEST(ReduceTester, TestGPUReduceTestSelectedRows) {
  TestReduceOpHandle test_op;
C
chengduoZH 已提交
310
  size_t out_scope_idx = 0;
C
chengduoZH 已提交
311
  test_op.InitCtxOnGpu(true);
C
chengduoZH 已提交
312 313
  test_op.InitReduceOp(out_scope_idx);
  test_op.TestReduceSelectedRows(out_scope_idx);
C
chengduoZH 已提交
314 315 316 317
}

TEST(ReduceTester, TestGPUReduceTestLodTensor) {
  TestReduceOpHandle test_op;
C
chengduoZH 已提交
318
  size_t out_scope_idx = 0;
C
chengduoZH 已提交
319
  test_op.InitCtxOnGpu(true);
C
chengduoZH 已提交
320 321
  test_op.InitReduceOp(out_scope_idx);
  test_op.TestReduceLodTensors(out_scope_idx);
C
chengduoZH 已提交
322 323
}
#endif
C
chengduoZH 已提交
324 325 326 327

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