sum_op.h 7.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12
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
13
#include <vector>
Y
Yi Wang 已提交
14 15 16 17 18
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/lod_tensor_array.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/operators/math/selected_rows_functor.h"
19 20 21 22 23

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
Q
QI JUN 已提交
24 25
using SelectedRows = framework::SelectedRows;
using LoDTensor = framework::LoDTensor;
26 27 28 29
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;

Q
QI JUN 已提交
30
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
31
class SumKernel : public framework::OpKernel<T> {
32
 public:
33
  void Compute(const framework::ExecutionContext &context) const override {
Y
Yu Yang 已提交
34
    auto in_vars = context.MultiInputVar("X");
35
    size_t in_num = in_vars.size();
Q
QI JUN 已提交
36 37
    auto out_var = context.OutputVar("Out");

Y
Yu Yang 已提交
38 39
    bool in_place = out_var == in_vars[0];

Q
QI JUN 已提交
40
    if (out_var->IsType<framework::LoDTensor>()) {
Y
Update  
Yang Yu 已提交
41
      auto *out = context.Output<LoDTensor>("Out");
Y
Yu Yang 已提交
42
      if (!in_place) {
Y
Refine  
Yang Yu 已提交
43
        out->mutable_data<T>(context.GetPlace());
Y
Update  
Yang Yu 已提交
44 45
      }
      auto result = EigenVector<T>::Flatten(*out);
46 47 48
      auto &place =
          *context.template device_context<DeviceContext>().eigen_device();
      int start = in_place ? 1 : 0;
Y
Update  
Yang Yu 已提交
49
      if (!in_place) {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        if ((in_num >= 2) && in_vars[0]->IsType<framework::LoDTensor>() &&
            in_vars[1]->IsType<framework::LoDTensor>()) {
          auto &in_0 = in_vars[0]->Get<framework::LoDTensor>();
          auto &in_1 = in_vars[1]->Get<framework::LoDTensor>();
          if (in_0.numel() && in_1.numel()) {
            auto in_0_e = EigenVector<T>::Flatten(in_0);
            auto in_1_e = EigenVector<T>::Flatten(in_1);
            result.device(place) = in_0_e + in_1_e;
            start = 2;
          }
        }
        if (start != 2) {
          math::SetConstant<DeviceContext, T> constant_functor;
          constant_functor(context.template device_context<DeviceContext>(),
                           out, 0.0);
        }
Y
Yu Yang 已提交
66
      }
Q
QI JUN 已提交
67

Q
QI JUN 已提交
68
      math::SelectedRowsAddToTensor<DeviceContext, T> functor;
Y
Yu Yang 已提交
69
      // If in_place, just skip the first tensor
70
      for (size_t i = start; i < in_num; i++) {
Q
QI JUN 已提交
71
        if (in_vars[i]->IsType<framework::LoDTensor>()) {
72
          auto &in_t = in_vars[i]->Get<framework::LoDTensor>();
73 74 75
          if (in_t.numel() == 0) {
            continue;
          }
Q
QI JUN 已提交
76 77 78
          auto in = EigenVector<T>::Flatten(in_t);
          result.device(place) = result + in;
        } else if (in_vars[i]->IsType<framework::SelectedRows>()) {
79
          auto &in_t = in_vars[i]->Get<framework::SelectedRows>();
Q
QI JUN 已提交
80
          functor(context.template device_context<DeviceContext>(), in_t, out);
Q
QI JUN 已提交
81 82 83 84 85
        } else {
          PADDLE_THROW("Variable type must be LoDTensor/SelectedRows.");
        }
      }
    } else if (out_var->IsType<framework::SelectedRows>()) {
Y
Yang Yu 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
      std::unique_ptr<framework::SelectedRows> in0;
      if (in_place) {
        // If is in_place, we store the input[0] to in0
        auto &in_sel0 = in_vars[0]->Get<SelectedRows>();
        auto &rows = in_sel0.rows();
#ifdef PADDLE_WITH_CUDA
        std::vector<int64_t> rows_in_cpu;
        rows_in_cpu.reserve(rows.size());
        for (auto item : rows) {
          rows_in_cpu.push_back(item);
        }
        in0.reset(new framework::SelectedRows(rows_in_cpu, in_sel0.height()));
#else
        in0.reset(new framework::SelectedRows(rows, in_sel0.height()));
#endif
        in0->mutable_value()->ShareDataWith(in_sel0.value());
      }

      auto get_selected_row = [&](size_t i) -> const SelectedRows & {
        if (i == 0 && in0) {
          return *in0.get();
        } else {
          return in_vars[i]->Get<SelectedRows>();
        }
      };

112
      auto *out = context.Output<SelectedRows>("Out");
Y
Yancey 已提交
113
      out->mutable_rows()->clear();
114
      auto *out_value = out->mutable_value();
Q
QI JUN 已提交
115 116 117

      // Runtime InferShape
      size_t first_dim = 0;
118
      for (size_t i = 0; i < in_num; i++) {
Y
Yang Yu 已提交
119 120
        auto &sel_row = get_selected_row(i);
        first_dim += sel_row.rows().size();
Q
QI JUN 已提交
121
      }
T
tangwei12 已提交
122 123

      std::vector<int64_t> in_dim;
124
      for (size_t i = 0; i < in_num; i++) {
T
tangwei12 已提交
125 126 127 128 129 130
        auto &sel_row = get_selected_row(i);
        if (sel_row.rows().size() > 0) {
          in_dim = framework::vectorize(sel_row.value().dims());
          break;
        }
      }
T
tangwei12 已提交
131
      if (in_dim.empty()) {
T
tangwei12 已提交
132
        VLOG(3) << "WARNING: all the inputs are empty";
133 134
        in_dim =
            framework::vectorize(get_selected_row(in_num - 1).value().dims());
T
tangwei12 已提交
135 136
      } else {
        in_dim[0] = static_cast<int64_t>(first_dim);
T
tangwei12 已提交
137 138
      }

Y
Yang Yu 已提交
139
      out_value->Resize(framework::make_ddim(in_dim));
T
tangwei12 已提交
140
      out_value->mutable_data<T>(context.GetPlace());
141 142 143 144 145
      // if all the input sparse vars are empty, no need to
      // merge these vars.
      if (first_dim == 0UL) {
        return;
      }
Q
QI JUN 已提交
146

Q
QI JUN 已提交
147
      math::SelectedRowsAddTo<DeviceContext, T> functor;
Q
QI JUN 已提交
148 149

      int64_t offset = 0;
150
      for (size_t i = 0; i < in_num; i++) {
Y
Yang Yu 已提交
151
        auto &sel_row = get_selected_row(i);
152
        if (sel_row.rows().size() == 0) {
153 154
          continue;
        }
Y
Yang Yu 已提交
155 156 157 158
        PADDLE_ENFORCE_EQ(out->height(), sel_row.height());
        functor(context.template device_context<DeviceContext>(), sel_row,
                offset, out);
        offset += sel_row.value().numel();
Q
QI JUN 已提交
159
      }
160 161 162 163 164 165 166 167 168 169 170 171 172
    } else if (out_var->IsType<framework::LoDTensorArray>()) {
      auto &out_array = *out_var->GetMutable<framework::LoDTensorArray>();
      for (size_t i = in_place ? 1 : 0; i < in_vars.size(); ++i) {
        PADDLE_ENFORCE(in_vars[i]->IsType<framework::LoDTensorArray>(),
                       "Only support all inputs are TensorArray");
        auto &in_array = in_vars[i]->Get<framework::LoDTensorArray>();

        for (size_t i = 0; i < in_array.size(); ++i) {
          if (in_array[i].numel() != 0) {
            if (i >= out_array.size()) {
              out_array.resize(i + 1);
            }
            if (out_array[i].numel() == 0) {
Y
Yi Wang 已提交
173 174
              framework::TensorCopy(in_array[i], in_array[i].place(),
                                    context.device_context(), &out_array[i]);
175 176 177 178 179
              out_array[i].set_lod(in_array[i].lod());
            } else {
              PADDLE_ENFORCE(out_array[i].lod() == in_array[i].lod());
              auto in = EigenVector<T>::Flatten(in_array[i]);
              auto result = EigenVector<T>::Flatten(out_array[i]);
Q
QI JUN 已提交
180 181
              result.device(*context.template device_context<DeviceContext>()
                                 .eigen_device()) = result + in;
182 183 184 185 186 187 188
            }
          }
        }
      }
    } else {
      PADDLE_THROW("Unexpected branch, output variable type is %s",
                   out_var->Type().name());
189 190 191 192 193
    }
  }
};
}  // namespace operators
}  // namespace paddle