add_n_kernel.cc 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2022 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.

Y
YuanRisheng 已提交
15
#include "paddle/phi/kernels/impl/add_n_kernel_impl.h"
16

17 18
#include "glog/logging.h"

19 20 21 22
namespace phi {

template <typename T, typename Context>
void AddNKernel(const Context& dev_ctx,
Y
YuanRisheng 已提交
23
                const std::vector<const TensorBase*>& x,
24 25
                DenseTensor* out) {
  size_t in_num = x.size();
Y
YuanRisheng 已提交
26 27 28 29 30 31
  dev_ctx.template Alloc<T>(out);

  bool in_place = false;
  if (x.size() > 0 && x[0]->initialized() && DenseTensor::classof(x[0])) {
    if ((static_cast<const DenseTensor*>(x[0]))->Holder() == out->Holder()) {
      in_place = true;
32 33 34 35 36 37 38
    }
  }

  auto result = EigenVector<T>::Flatten(*out);
  auto& place = *dev_ctx.eigen_device();
  int start = in_place ? 1 : 0;
  if (!in_place) {
Y
YuanRisheng 已提交
39 40 41 42 43
    if ((in_num >= 2) && DenseTensor::classof(x[0]) &&
        DenseTensor::classof(x[1]) && x[0]->initialized() &&
        x[1]->initialized()) {
      auto& in_0 = *(static_cast<const DenseTensor*>(x[0]));
      auto& in_1 = *(static_cast<const DenseTensor*>(x[1]));
44 45 46 47 48 49 50 51 52
      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) {
      VLOG(10) << "Fill with constant = 0 in sum kernel.";
Y
YuanRisheng 已提交
53
      phi::funcs::SetConstant<Context, T> constant_functor;
54 55 56 57
      constant_functor(dev_ctx, out, static_cast<T>(0));
    }
  }

58
  phi::funcs::SelectedRowsAddToTensor<Context, T> functor;
59 60
  // If in_place, just skip the first tensor
  for (size_t i = start; i < in_num; i++) {
Y
YuanRisheng 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    if (DenseTensor::classof(x[i])) {
      auto& in_t = *(static_cast<const DenseTensor*>(x[i]));
      if (!in_t.initialized() || in_t.numel() == 0) {
        continue;
      }
      auto in = EigenVector<T>::Flatten(in_t);
      result.device(place) = result + in;
    } else if (SelectedRows::classof(x[i])) {
      auto& in_t = *(static_cast<const SelectedRows*>(x[i]));
      functor(dev_ctx, in_t, out);
    } else {
      PADDLE_THROW(phi::errors::InvalidArgument(
          "Expected type of Input(X) of %d-th must be Tensor, "
          "SelectedRows. But got "
          "unsupport type: %s.",
          x[i]->type_info().name()));
77 78
    }
  }
Y
YuanRisheng 已提交
79
  VLOG(10) << "end add_n kernel";
80 81 82 83 84 85 86 87 88 89 90 91 92
}

}  // namespace phi

PD_REGISTER_KERNEL(add_n,
                   CPU,
                   ALL_LAYOUT,
                   phi::AddNKernel,
                   float,
                   double,
                   int,
                   phi::dtype::bfloat16,
                   int64_t) {}
Y
YuanRisheng 已提交
93 94 95 96 97 98 99 100 101 102

PD_REGISTER_KERNEL(add_n_array,
                   CPU,
                   ALL_LAYOUT,
                   phi::AddNArrayKernel,
                   float,
                   double,
                   int,
                   phi::dtype::bfloat16,
                   int64_t) {}