gather_op_handle_test.cc 7.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/gather_op_handle.h"
16

17 18
#include <memory>
#include <unordered_map>
19

C
chengduoZH 已提交
20 21
#include "gtest/gtest.h"

C
chengduoZH 已提交
22 23 24
namespace paddle {
namespace framework {
namespace details {
W
wanghuancoder 已提交
25 26
struct DummyVarHandle;

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

30 31
using DeviceType = paddle::platform::DeviceType;

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

35 36 37
struct TestGatherOpHandle {
  std::vector<std::unique_ptr<p::DeviceContext>> ctxs_;
  std::vector<Scope*> local_scopes_;
C
chengduoZH 已提交
38
  std::vector<Scope*> param_scopes_;
39
  Scope g_scope_;
X
clean1  
Xin Pan 已提交
40 41
  OpHandleBase* op_handle_;
  std::vector<VarHandleBase*> vars_;
42
  std::vector<p::Place> gpu_list_;
X
Xin Pan 已提交
43
  std::vector<std::unique_ptr<ir::Node>> nodes_;
44 45 46 47 48 49 50

  void WaitAll() {
    for (size_t j = 0; j < ctxs_.size(); ++j) {
      ctxs_[j]->Wait();
    }
  }

C
chengduoZH 已提交
51
  void InitCtxOnGpu(bool use_gpu) {
C
chengduoZH 已提交
52 53 54 55
    if (use_gpu) {
#ifdef PADDLE_WITH_CUDA
      int count = p::GetCUDADeviceCount();
      if (count <= 1) {
56
        LOG(WARNING) << "Cannot test multi-gpu Broadcast, because the CUDA "
C
chengduoZH 已提交
57 58 59 60 61 62 63 64 65 66
                        "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
67 68
      PADDLE_THROW(
          platform::errors::PreconditionNotMet("Not compiled with CUDA."));
C
chengduoZH 已提交
69 70 71 72 73 74 75 76 77 78 79
#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));
      }
    }
  }

80
  void InitGatherOp(size_t input_scope_idx) {
X
Xin Pan 已提交
81
    nodes_.clear();
82
    std::unordered_map<Scope*, Scope*> scope_map;
C
chengduoZH 已提交
83
    for (size_t j = 0; j < gpu_list_.size(); ++j) {
84
      local_scopes_.push_back(&(g_scope_.NewScope()));
C
chengduoZH 已提交
85 86 87
      Scope& local_scope = local_scopes_.back()->NewScope();
      local_scope.Var("input");
      param_scopes_.emplace_back(&local_scope);
88
      scope_map.emplace(local_scopes_.back(), param_scopes_.back());
C
chengduoZH 已提交
89
    }
C
chengduoZH 已提交
90
    param_scopes_[input_scope_idx]->Var("out");
C
chengduoZH 已提交
91

X
Xin Pan 已提交
92
    nodes_.emplace_back(
X
Xin Pan 已提交
93
        ir::CreateNodeForTest("node", ir::Node::Type::kOperation).release());
X
clean1  
Xin Pan 已提交
94
    op_handle_ =
X
Xin Pan 已提交
95
        new GatherOpHandle(nodes_.back().get(), local_scopes_, gpu_list_);
96 97 98

    op_handle_->SetLocalExecScopes(scope_map);

99
    // add input
C
chengduoZH 已提交
100
    for (size_t j = 0; j < gpu_list_.size(); ++j) {
X
Xin Pan 已提交
101
      op_handle_->SetDeviceContext(gpu_list_[j], ctxs_[j].get());
X
Xin Pan 已提交
102
      nodes_.emplace_back(
X
Xin Pan 已提交
103
          ir::CreateNodeForTest("node1", ir::Node::Type::kVariable).release());
X
Xin Pan 已提交
104
      auto* in_var_handle =
X
Xin Pan 已提交
105
          new VarHandle(nodes_.back().get(), 1, j, "input", gpu_list_[j]);
Y
Yu Yang 已提交
106
      vars_.emplace_back(in_var_handle);
107
      op_handle_->AddInput(in_var_handle);
C
chengduoZH 已提交
108 109
    }

110
    // add dummy var
X
Xin Pan 已提交
111
    nodes_.emplace_back(
X
Xin Pan 已提交
112
        ir::CreateNodeForTest("node2", ir::Node::Type::kVariable).release());
X
Xin Pan 已提交
113
    vars_.emplace_back(new DummyVarHandle(nodes_.back().get()));
114
    DummyVarHandle* in_dummy_var_handle =
X
clean1  
Xin Pan 已提交
115
        static_cast<DummyVarHandle*>(vars_.back());
X
Xin Pan 已提交
116
    in_dummy_var_handle->ClearGeneratedOp();
117 118 119
    op_handle_->AddInput(in_dummy_var_handle);

    // add output
X
Xin Pan 已提交
120
    nodes_.emplace_back(
X
Xin Pan 已提交
121
        ir::CreateNodeForTest("node3", ir::Node::Type::kVariable).release());
X
Xin Pan 已提交
122 123 124
    auto* out_var_handle =
        new VarHandle(nodes_.back().get(), 2, input_scope_idx, "out",
                      gpu_list_[input_scope_idx]);
Y
Yu Yang 已提交
125
    vars_.emplace_back(out_var_handle);
126
    op_handle_->AddOutput(out_var_handle);
C
chengduoZH 已提交
127

128
    // add dummy var
X
Xin Pan 已提交
129
    nodes_.emplace_back(
X
Xin Pan 已提交
130
        ir::CreateNodeForTest("node4", ir::Node::Type::kVariable).release());
X
Xin Pan 已提交
131
    vars_.emplace_back(new DummyVarHandle(nodes_.back().get()));
132
    DummyVarHandle* dummy_var_handle =
X
clean1  
Xin Pan 已提交
133
        static_cast<DummyVarHandle*>(vars_.back());
134 135
    op_handle_->AddOutput(dummy_var_handle);
  }
C
chengduoZH 已提交
136

137
  void TestGatherSelectedRows(size_t output_scope_idx) {
C
chengduoZH 已提交
138 139 140 141 142 143 144 145 146 147
    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};
    std::vector<float> send_vector(f::product(kDims));
    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 已提交
148
      auto in_var = param_scopes_.at(input_scope_idx)->FindVar("input");
149 150 151
      PADDLE_ENFORCE_NOT_NULL(
          in_var, platform::errors::NotFound(
                      "The variable '%s' is not found in the scope.", "input"));
C
chengduoZH 已提交
152 153 154 155 156 157 158 159 160 161 162 163
      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]);

      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 已提交
164
    auto out_var = param_scopes_.at(output_scope_idx)->FindVar("out");
165 166 167
    PADDLE_ENFORCE_NOT_NULL(
        out_var, platform::errors::NotFound(
                     "The variable '%s' is not found in the scope.", "out"));
168 169
    auto out_selected_rows = out_var->GetMutable<f::SelectedRows>();

C
chengduoZH 已提交
170
    auto in_var = param_scopes_.at(output_scope_idx)->FindVar("input");
171 172 173 174 175
    auto in_selected_rows = in_var->GetMutable<f::SelectedRows>();

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

176 177
    DeviceType use_device = p::kCPU;
    op_handle_->Run(use_device);
C
chengduoZH 已提交
178 179 180 181 182 183 184 185

    WaitAll();

    p::CPUPlace cpu_place;

    auto& out_select_rows = out_var->Get<f::SelectedRows>();
    auto rt = out_select_rows.value();

186 187 188 189 190 191
    PADDLE_ENFORCE_EQ(out_select_rows.height(), height,
                      platform::errors::InvalidArgument(
                          "The height of SelectedRows is not equal to "
                          "the expected, expect %d, but got %d.",
                          height, out_select_rows.height()));

C
chengduoZH 已提交
192
    for (size_t k = 0; k < out_select_rows.rows().size(); ++k) {
193 194 195 196 197 198
      PADDLE_ENFORCE_EQ(
          out_select_rows.rows()[k], rows[k % rows.size()],
          platform::errors::InvalidArgument(
              "The item at position %d of rows of SelectedRows is not equal to "
              "the expected, expect %d, but got %d.",
              k, rows[k % rows.size()], out_select_rows.rows()[k]));
C
chengduoZH 已提交
199 200 201 202 203 204
    }

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

C
chengduoZH 已提交
205 206
    for (int64_t j = 0;
         j < f::product(kDims) * static_cast<int64_t>(gpu_list_.size()); ++j) {
C
chengduoZH 已提交
207 208 209 210 211
      ASSERT_NEAR(ct[j], send_vector[j % send_vector.size()], 1e-5);
    }
  }
};

212 213 214 215 216 217
TEST(GatherTester, TestCPUGatherTestSelectedRows) {
  TestGatherOpHandle test_op;
  size_t input_scope_idx = 0;
  test_op.InitCtxOnGpu(false);
  test_op.InitGatherOp(input_scope_idx);
  test_op.TestGatherSelectedRows(input_scope_idx);
C
chengduoZH 已提交
218 219 220 221
}

#ifdef PADDLE_WITH_CUDA

222 223 224 225 226 227
TEST(GatherTester, TestGPUGatherTestSelectedRows) {
  TestGatherOpHandle test_op;
  size_t input_scope_idx = 0;
  test_op.InitCtxOnGpu(false);
  test_op.InitGatherOp(input_scope_idx);
  test_op.TestGatherSelectedRows(input_scope_idx);
C
chengduoZH 已提交
228 229
}
#endif
230

C
chengduoZH 已提交
231 232 233
}  // namespace details
}  // namespace framework
}  // namespace paddle