reduce_op.h 7.2 KB
Newer Older
G
guosheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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

D
Dong Zhihong 已提交
17
#include "glog/logging.h"
G
guosheng 已提交
18 19 20 21 22 23 24 25 26 27 28 29
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using DDim = framework::DDim;
template <typename T, size_t D, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;

D
Dong Zhihong 已提交
30 31 32 33
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenScalar = framework::EigenScalar<T, MajorType, IndexType>;

G
guosheng 已提交
34
struct SumFunctor {
Q
QI JUN 已提交
35 36
  template <typename DeviceContext, typename X, typename Y, typename Dim>
  void operator()(const DeviceContext& place, X& x, Y& y, const Dim& dim) {
G
guosheng 已提交
37
    y.device(place) = x.sum(dim);
G
guosheng 已提交
38 39 40 41
  }
};

struct SumGradFunctor {
Q
QI JUN 已提交
42 43 44
  template <typename DeviceContext, typename X, typename Y, typename DX,
            typename DY, typename Dim>
  void operator()(const DeviceContext& place, X& x, Y& y, DX& dx, DY& dy,
G
guosheng 已提交
45 46
                  const Dim& dim, int size) {
    dx.device(place) = dy.broadcast(dim);
G
guosheng 已提交
47 48 49 50
  }
};

struct MeanFunctor {
Q
QI JUN 已提交
51 52
  template <typename DeviceContext, typename X, typename Y, typename Dim>
  void operator()(const DeviceContext& place, X& x, Y& y, const Dim& dim) {
G
guosheng 已提交
53
    y.device(place) = x.mean(dim);
G
guosheng 已提交
54 55 56 57
  }
};

struct MeanGradFunctor {
Q
QI JUN 已提交
58 59 60
  template <typename DeviceContext, typename X, typename Y, typename DX,
            typename DY, typename Dim>
  void operator()(const DeviceContext& place, X& x, Y& y, DX& dx, DY& dy,
G
guosheng 已提交
61 62
                  const Dim& dim, int size) {
    dx.device(place) = dy.broadcast(dim) / dx.constant(size);
G
guosheng 已提交
63 64 65 66
  }
};

struct MaxFunctor {
Q
QI JUN 已提交
67 68
  template <typename DeviceContext, typename X, typename Y, typename Dim>
  void operator()(const DeviceContext& place, X& x, Y& y, const Dim& dim) {
G
guosheng 已提交
69
    y.device(place) = x.maximum(dim);
G
guosheng 已提交
70 71 72 73
  }
};

struct MinFunctor {
Q
QI JUN 已提交
74 75
  template <typename DeviceContext, typename X, typename Y, typename Dim>
  void operator()(const DeviceContext& place, X& x, Y& y, const Dim& dim) {
G
guosheng 已提交
76
    y.device(place) = x.minimum(dim);
G
guosheng 已提交
77 78 79 80
  }
};

struct MaxOrMinGradFunctor {
Q
QI JUN 已提交
81 82 83
  template <typename DeviceContext, typename X, typename Y, typename DX,
            typename DY, typename Dim>
  void operator()(const DeviceContext& place, X& x, Y& y, DX& dx, DY& dy,
G
guosheng 已提交
84 85 86 87
                  const Dim& dim, int size) {
    auto equals = x == y.broadcast(dim);
    auto ones = dx.constant(1);
    auto zeros = dx.constant(0);
88 89
    // If there are multiple minimum or maximum elements, the subgradient of
    // each is the set [0, 1], and we pass gradient to all of them here.
G
guosheng 已提交
90
    dx.device(place) = dy.broadcast(dim) * equals.select(ones, zeros);
G
guosheng 已提交
91 92 93
  }
};

Q
QI JUN 已提交
94
template <typename DeviceContext, typename T, typename Functor>
Y
Yu Yang 已提交
95
class ReduceKernel : public framework::OpKernel<T> {
G
guosheng 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    int rank = context.Input<Tensor>("X")->dims().size();
    switch (rank) {
      case 1:
        ReduceCompute<1>(context);
        break;
      case 2:
        ReduceCompute<2>(context);
        break;
      case 3:
        ReduceCompute<3>(context);
        break;
      case 4:
        ReduceCompute<4>(context);
        break;
      case 5:
        ReduceCompute<5>(context);
        break;
      case 6:
        ReduceCompute<6>(context);
        break;
    }
  }

 private:
  template <size_t D>
  void ReduceCompute(const framework::ExecutionContext& context) const {
    auto* input = context.Input<Tensor>("X");
    auto* output = context.Output<Tensor>("Out");
    output->mutable_data<T>(context.GetPlace());

    auto x = EigenTensor<T, D>::From(*input);
    auto x_rank = static_cast<int>(x.dimensions().size());
    int dim = static_cast<int>(context.Attr<int>("dim"));
    if (dim < 0) dim = x_rank + dim;
    auto reduce_dim = Eigen::array<int, 1>({{dim}});
    // construct the squeezed output tensor
G
guosheng 已提交
134
    bool keep_dim = context.Attr<bool>("keep_dim");
G
guosheng 已提交
135 136 137 138 139 140
    DDim dims = output->dims();
    auto dims_vector = vectorize(dims);
    if (keep_dim && x_rank > 1) {
      dims_vector.erase(dims_vector.begin() + dim);
      dims = framework::make_ddim(dims_vector);
    }
D
Dong Zhihong 已提交
141

Q
QI JUN 已提交
142 143
    auto& place =
        *context.template device_context<DeviceContext>().eigen_device();
G
guosheng 已提交
144
    Functor functor;
D
Dong Zhihong 已提交
145 146 147 148 149 150 151 152

    if (D == 1) {
      auto out = EigenScalar<T>::From(*output);
      functor(place, x, out, reduce_dim);
    } else {
      auto out = EigenTensor<T, (D - 1)>::From(*output, dims);
      functor(place, x, out, reduce_dim);
    }
G
guosheng 已提交
153 154 155
  }
};

Q
QI JUN 已提交
156
template <typename DeviceContext, typename T, typename Functor>
Y
Yu Yang 已提交
157
class ReduceGradKernel : public framework::OpKernel<T> {
G
guosheng 已提交
158 159 160 161 162
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    int rank = context.Input<Tensor>("X")->dims().size();
    switch (rank) {
      case 1:
163
        ReduceGradCompute<1>(context);
G
guosheng 已提交
164 165
        break;
      case 2:
166
        ReduceGradCompute<2>(context);
G
guosheng 已提交
167 168
        break;
      case 3:
169
        ReduceGradCompute<3>(context);
G
guosheng 已提交
170 171
        break;
      case 4:
172
        ReduceGradCompute<4>(context);
G
guosheng 已提交
173 174
        break;
      case 5:
175
        ReduceGradCompute<5>(context);
G
guosheng 已提交
176 177
        break;
      case 6:
178
        ReduceGradCompute<6>(context);
G
guosheng 已提交
179 180 181 182 183 184
        break;
    }
  }

 private:
  template <size_t D>
185
  void ReduceGradCompute(const framework::ExecutionContext& context) const {
G
guosheng 已提交
186 187 188 189 190
    auto* input0 = context.Input<Tensor>("X");
    auto* input1 = context.Input<Tensor>("Out");
    auto* input2 = context.Input<Tensor>(framework::GradVarName("Out"));
    auto* output = context.Output<Tensor>(framework::GradVarName("X"));

191 192 193 194 195 196 197 198 199 200 201
    output->mutable_data<T>(context.GetPlace());
    auto x = EigenTensor<T, D>::From(*input0);
    auto x_grad = EigenTensor<T, D>::From(*output);
    auto x_rank = static_cast<int>(x.dimensions().size());
    int dim = static_cast<int>(context.Attr<int>("dim"));
    if (dim < 0) dim = x_rank + dim;
    DDim dims = input0->dims();
    dims[dim] = 1;
    auto x_reduce = EigenTensor<T, D>::From(*input1, dims);
    auto x_reduce_grad = EigenTensor<T, D>::From(*input2, dims);

D
Dong Zhihong 已提交
202 203 204
    Eigen::array<int, D> broadcast_dim;
    for (size_t i = 0; i < D; ++i) broadcast_dim[i] = 1;
    broadcast_dim[dim] = input0->dims()[dim];
Q
QI JUN 已提交
205 206
    auto& place =
        *context.template device_context<DeviceContext>().eigen_device();
207
    Functor functor;
D
Dong Zhihong 已提交
208 209
    functor(place, x, x_reduce, x_grad, x_reduce_grad, broadcast_dim,
            broadcast_dim[dim]);
G
guosheng 已提交
210 211 212 213 214
  }
};

}  // namespace operators
}  // namespace paddle
215 216 217 218 219 220

#define FOR_EACH_KERNEL_FUNCTOR(__macro)                \
  __macro(reduce_sum, SumFunctor, SumGradFunctor);      \
  __macro(reduce_mean, MeanFunctor, MeanGradFunctor);   \
  __macro(reduce_max, MaxFunctor, MaxOrMinGradFunctor); \
  __macro(reduce_min, MinFunctor, MaxOrMinGradFunctor);