reduce.h 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2021 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.

#pragma once

17
// CUDA and HIP use same api
18 19
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) || \
    defined(PADDLE_WITH_XPU_KP)
20

21
#include "paddle/phi/core/visit_type.h"
22
#include "paddle/phi/kernels/funcs/reduce_function.h"
23

24
namespace phi {
25 26

template <typename T,
27 28 29 30
          template <typename>
          class ReduceOp,
          template <typename, typename>
          class TransformOp>
N
niuliling123 已提交
31
void Reduce(const KPDevice& dev_ctx,
32 33 34 35 36
            const DenseTensor& x,
            bool reduce_all,
            const std::vector<int64_t>& dims,
            bool keep_dim,
            DataType out_dtype,
37 38
            DenseTensor* out,
            bool is_mean = false) {
39
  std::vector<int> reduce_dims =
40
      phi::funcs::details::GetReduceDim(dims, x.dims().size(), reduce_all);
41 42 43 44 45

  int reduce_num = 1;
  for (auto i : reduce_dims) {
    reduce_num *= (x.dims())[i];
  }
46
#ifndef PADDLE_WITH_XPU_KP
47 48
  if (out_dtype != phi::DataType::UNDEFINED && out_dtype != x.dtype()) {
    auto tmp_tensor = phi::Cast<T>(dev_ctx, x, out_dtype);
49
    PD_VISIT_BOOL_AND_FLOATING_AND_COMPLEX_AND_3_TYPES(
50 51 52
        phi::DataType::INT32,
        phi::DataType::INT64,
        phi::DataType::FLOAT16,
53
        out_dtype,
54
        "ReduceKernel",
55 56
        ([&] {
          using MPType = typename kps::details::MPTypeTrait<data_t>::Type;
57 58 59 60
          phi::funcs::ReduceKernel<data_t,
                                   data_t,
                                   ReduceOp,
                                   TransformOp<data_t, MPType>>(
W
Wilber 已提交
61
              dev_ctx,
62
              tmp_tensor,
W
Wilber 已提交
63
              out,
64
              TransformOp<data_t, MPType>(reduce_num),
65 66
              reduce_dims,
              is_mean);
67 68 69
        }));
  } else {
    using MPType = typename kps::details::MPTypeTrait<T>::Type;
70
    phi::funcs::ReduceKernel<T, T, ReduceOp, TransformOp<T, MPType>>(
71 72 73 74 75 76
        dev_ctx,
        x,
        out,
        TransformOp<T, MPType>(reduce_num),
        reduce_dims,
        is_mean);
77
  }
78 79 80 81 82 83 84 85 86 87
#else
  using MPType = typename kps::details::MPTypeTrait<T>::Type;
  phi::funcs::ReduceKernel<T, T, ReduceOp, TransformOp<T, MPType>>(
      dev_ctx,
      x,
      out,
      TransformOp<T, MPType>(reduce_num),
      reduce_dims,
      is_mean);
#endif
88
}
89
}  // namespace phi
90 91

#endif