|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <ATen/Parallel.h> |
| 4 | +#include <ATen/NumericUtils.h> |
| 5 | +#include <ATen/cpu/vec/vec.h> |
| 6 | +#include <ATen/cpu/vec/functional.h> |
| 7 | +#include <ATen/native/ReductionType.h> |
| 8 | +#include <c10/util/irange.h> |
| 9 | + |
| 10 | +namespace at::native { |
| 11 | +inline namespace CPU_CAPABILITY { |
| 12 | + |
| 13 | +using namespace vec; |
| 14 | + |
| 15 | +#define AT_DISPATCH_REDUCTION_TYPES(op, ...) \ |
| 16 | + [&] { \ |
| 17 | + switch (op) { \ |
| 18 | + case SUM: { \ |
| 19 | + static constexpr ReductionType reduce = SUM; \ |
| 20 | + return __VA_ARGS__(); \ |
| 21 | + } \ |
| 22 | + case MEAN: { \ |
| 23 | + static constexpr ReductionType reduce = MEAN; \ |
| 24 | + return __VA_ARGS__(); \ |
| 25 | + } \ |
| 26 | + case MIN: { \ |
| 27 | + static constexpr ReductionType reduce = MIN; \ |
| 28 | + return __VA_ARGS__(); \ |
| 29 | + } \ |
| 30 | + case MAX: { \ |
| 31 | + static constexpr ReductionType reduce = MAX; \ |
| 32 | + return __VA_ARGS__(); \ |
| 33 | + } \ |
| 34 | + case PROD: { \ |
| 35 | + static constexpr ReductionType reduce = PROD; \ |
| 36 | + return __VA_ARGS__(); \ |
| 37 | + } \ |
| 38 | + } \ |
| 39 | + }() |
| 40 | + |
| 41 | +template <typename scalar_t, ReductionType reduce> |
| 42 | +inline vec_scalar_t<scalar_t> init_value() { |
| 43 | + using acc_t = vec_scalar_t<scalar_t>; |
| 44 | + acc_t val; |
| 45 | + if (reduce == ReductionType::SUM || |
| 46 | + reduce == ReductionType::MEAN) { |
| 47 | + val = static_cast<acc_t>(0); |
| 48 | + } else if (reduce == ReductionType::PROD) { |
| 49 | + val = static_cast<acc_t>(1); |
| 50 | + } else if (reduce == ReductionType::MAX) { |
| 51 | + val = -std::numeric_limits<acc_t>::infinity(); |
| 52 | + } else { |
| 53 | + TORCH_INTERNAL_ASSERT(reduce == ReductionType::MIN); |
| 54 | + val = std::numeric_limits<acc_t>::infinity(); |
| 55 | + } |
| 56 | + return val; |
| 57 | +} |
| 58 | + |
| 59 | +template <typename scalar_t, ReductionType reduce> |
| 60 | +inline vec_scalar_t<scalar_t> init_value(const c10::optional<Scalar>& initial) { |
| 61 | + using acc_t = vec_scalar_t<scalar_t>; |
| 62 | + if (initial.has_value()) { |
| 63 | + return initial.value().to<acc_t>(); |
| 64 | + } else { |
| 65 | + return init_value<scalar_t, reduce>(); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +template <typename scalar_t> |
| 70 | +inline void init(scalar_t* out, int64_t size, const vec_scalar_t<scalar_t>& val) { |
| 71 | + using Vec = Vectorized<vec_scalar_t<scalar_t>>; |
| 72 | + map<scalar_t>( |
| 73 | + [val](Vec x) { return Vec(val); }, |
| 74 | + out, |
| 75 | + out, |
| 76 | + size); |
| 77 | +} |
| 78 | + |
| 79 | +template <typename scalar_t, ReductionType reduce> |
| 80 | +inline void init(scalar_t* out, int64_t size, const c10::optional<Scalar>& initial) { |
| 81 | + using acc_t = vec_scalar_t<scalar_t>; |
| 82 | + acc_t val = init_value<scalar_t, reduce>(initial); |
| 83 | + init(out, size, val); |
| 84 | +} |
| 85 | + |
| 86 | +// overload with `include_self`, used by scatter_reduce |
| 87 | +template <typename scalar_t, ReductionType reduce> |
| 88 | +inline void init(scalar_t* out, int64_t size, bool include_self = false) { |
| 89 | + using acc_t = vec_scalar_t<scalar_t>; |
| 90 | + if (!include_self) { |
| 91 | + acc_t val = init_value<scalar_t, reduce>(); |
| 92 | + init(out, size, val); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +template <typename scalar_t> |
| 97 | +inline scalar_t _max(const scalar_t& x, const scalar_t& y) { |
| 98 | + return at::_isnan(y) ? y : std::max(x, y); |
| 99 | +} |
| 100 | + |
| 101 | +template <typename scalar_t> |
| 102 | +inline Vectorized<scalar_t> _max(const Vectorized<scalar_t>& x, const Vectorized<scalar_t>& y) { |
| 103 | + // vec::maximum propagates NaN |
| 104 | + return vec::maximum(x, y); |
| 105 | +} |
| 106 | + |
| 107 | +template <typename scalar_t> |
| 108 | +inline scalar_t _min(const scalar_t& x, const scalar_t& y) { |
| 109 | + return at::_isnan(y) ? y : std::min(x, y); |
| 110 | +} |
| 111 | + |
| 112 | +template <typename scalar_t> |
| 113 | +inline Vectorized<scalar_t> _min(const Vectorized<scalar_t>& x, const Vectorized<scalar_t>& y) { |
| 114 | + // vec::minimum propagates NaN |
| 115 | + return vec::minimum(x, y); |
| 116 | +} |
| 117 | + |
| 118 | +// for Max and Min, propagate NaN: |
| 119 | +template <typename T, ReductionType reduce> |
| 120 | +inline T update(const T& x, const T& y) { |
| 121 | + if (reduce == ReductionType::SUM || |
| 122 | + reduce == ReductionType::MEAN) { |
| 123 | + return x + y; |
| 124 | + } else if (reduce == ReductionType::PROD) { |
| 125 | + return x * y; |
| 126 | + } else if (reduce == ReductionType::MAX) { |
| 127 | + return _max(x, y); |
| 128 | + } else { |
| 129 | + TORCH_INTERNAL_ASSERT(reduce == ReductionType::MIN); |
| 130 | + return _min(x, y); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +template <typename scalar_t, ReductionType reduce> |
| 135 | +inline void update(scalar_t* out, scalar_t* data, int64_t K) { |
| 136 | + using Vec = vec::Vectorized<vec_scalar_t<scalar_t>>; |
| 137 | + map2<scalar_t>( |
| 138 | + [](Vec x, Vec y) { return update<Vec, reduce>(x, y); }, |
| 139 | + out, |
| 140 | + out, |
| 141 | + data, |
| 142 | + K); |
| 143 | +} |
| 144 | + |
| 145 | +template <typename scalar_t, ReductionType reduce> |
| 146 | +inline void write(scalar_t* out, int64_t count, int64_t K) { |
| 147 | + using Vec = vec::Vectorized<vec_scalar_t<scalar_t>>; |
| 148 | + if (reduce == ReductionType::MEAN) { |
| 149 | + if (count > 0) { |
| 150 | + vec::map<scalar_t>( |
| 151 | + [count](Vec x) { return x / Vec(count); }, |
| 152 | + out, |
| 153 | + out, |
| 154 | + K); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +} // namespace CPU_CAPABILITY |
| 160 | +} // namespace at::native |
0 commit comments