reduce_sum_op.cu 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

W
Wu Yi 已提交
15 16
#include "paddle/fluid/operators/reduce_ops/cub_reduce.h"
#include "paddle/fluid/operators/reduce_ops/reduce_sum_op.h"
17

18 19 20
namespace paddle {
namespace operators {

21
template <typename Tout>
22 23 24
struct IdentityFunctor {
  HOSTDEVICE explicit inline IdentityFunctor() {}

25 26 27
  template <typename U>
  HOSTDEVICE inline Tout operator()(const U& x) const {
    return static_cast<Tout>(x);
28
  }
29 30 31 32 33 34 35 36 37
};

template <typename T>
class ReduceSumKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    bool reduce_all = context.Attr<bool>("reduce_all");
    auto* input = context.Input<Tensor>("X");
    auto* output = context.Output<Tensor>("Out");
38
    auto out_dtype = context.Attr<int>("out_dtype");
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

    auto dims = context.Attr<std::vector<int>>("dim");
    bool keep_dim = context.Attr<bool>("keep_dim");

    std::vector<int> reduce_dims;
    if (reduce_all) {
      reduce_dims.resize(input->dims().size());
      for (int i = 0; i < reduce_dims.size(); ++i) reduce_dims[i] = i;
    } else {
      for (auto e : dims) {
        reduce_dims.push_back(e >= 0 ? e : e + input->dims().size());
      }
    }

    int reduce_num = 1;
    for (int i = 0; i < reduce_dims.size(); ++i) {
      reduce_num *= input->dims()[reduce_dims[i]];
    }

    auto stream = context.cuda_device_context().stream();
59 60 61
    if (out_dtype >= 0) {
      framework::VisitDataTypeSmall(
          static_cast<framework::proto::VarType::Type>(out_dtype),
62
          TensorReduceFunctor<T, cub::Sum, IdentityFunctor>(
63
              *input, output, reduce_dims, static_cast<double>(0.0), cub::Sum(),
64
              stream));
65
    } else {
66
      TensorReduce<T, T, cub::Sum, IdentityFunctor<T>>(
67
          *input, output, reduce_dims, static_cast<T>(0), cub::Sum(),
68
          IdentityFunctor<T>(), stream);
69
    }
70 71 72 73 74 75
  }
};

}  // namespace operators
}  // namespace paddle

76 77
REGISTER_OP_CUDA_KERNEL(
    reduce_sum, ops::ReduceSumKernel<bool>, ops::ReduceSumKernel<float>,
78 79
    ops::ReduceSumKernel<double>,
    ops::ReduceSumKernel<paddle::platform::float16>, ops::ReduceSumKernel<int>,
80 81 82
    ops::ReduceSumKernel<int64_t>,
    ops::ReduceSumKernel<paddle::platform::complex<float>>,
    ops::ReduceSumKernel<paddle::platform::complex<double>>);