12#ifndef EIGEN_TENSOR_TENSOR_ARG_MAX_H
13#define EIGEN_TENSOR_TENSOR_ARG_MAX_H
16#include "./InternalHeaderCheck.h"
21template <
typename XprType>
22struct traits<TensorIndexPairOp<XprType>> :
public traits<XprType> {
23 using XprTraits = traits<XprType>;
24 using StorageKind =
typename XprTraits::StorageKind;
25 using Index =
typename XprTraits::Index;
26 using Scalar = Pair<Index, typename XprTraits::Scalar>;
27 static constexpr int NumDimensions = XprTraits::NumDimensions;
28 static constexpr int Layout = XprTraits::Layout;
31template <
typename XprType>
32struct eval<TensorIndexPairOp<XprType>, Eigen::Dense> {
33 using type =
const TensorIndexPairOp<XprType> EIGEN_DEVICE_REF;
43template <
typename XprType>
44class TensorIndexPairOp :
public TensorBase<TensorIndexPairOp<XprType>, ReadOnlyAccessors> {
46 using Scalar =
typename Eigen::internal::traits<TensorIndexPairOp>::Scalar;
48 using Nested =
typename Eigen::internal::ref_selector<TensorIndexPairOp>::type;
49 using StorageKind =
typename Eigen::internal::traits<TensorIndexPairOp>::StorageKind;
50 using Index =
typename Eigen::internal::traits<TensorIndexPairOp>::Index;
51 using CoeffReturnType = Pair<Index, typename XprType::CoeffReturnType>;
53 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIndexPairOp(
const XprType& expr) : m_xpr(expr) {}
55 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename XprType::Nested>& expression()
const {
return m_xpr; }
58 typename XprType::Nested m_xpr;
62template <
typename ArgType,
typename Device>
65 using Index =
typename XprType::Index;
66 using Scalar =
typename XprType::Scalar;
67 using CoeffReturnType =
typename XprType::CoeffReturnType;
69 using Dimensions =
typename TensorEvaluator<ArgType, Device>::Dimensions;
70 static constexpr int NumDims = internal::array_size<Dimensions>::value;
71 using Storage = StorageMemory<CoeffReturnType, Device>;
72 using EvaluatorPointerType =
typename Storage::Type;
82 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
85 using TensorBlock = internal::TensorBlockNotImplemented;
88 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device) : m_impl(op.expression(), device) {}
90 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_impl.dimensions(); }
92#ifdef EIGEN_USE_THREADS
93 template <
typename EvalSubExprsCallback>
94 EIGEN_STRONG_INLINE
void evalSubExprsIfNeededAsync(EvaluatorPointerType, EvalSubExprsCallback done) {
95 m_impl.evalSubExprsIfNeededAsync(
nullptr, [done](
bool) { done(
true); });
99 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType ) {
100 m_impl.evalSubExprsIfNeeded(
nullptr);
103 EIGEN_STRONG_INLINE
void cleanup() { m_impl.cleanup(); }
105 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
106 return CoeffReturnType(index, m_impl.coeff(index));
109 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
110 return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, 1);
113 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return nullptr; }
116 TensorEvaluator<ArgType, Device> m_impl;
127template <
typename ReduceOp,
typename Dims,
typename XprType>
129 using XprTraits = traits<XprType>;
130 using StorageKind =
typename XprTraits::StorageKind;
131 using Index =
typename XprTraits::Index;
132 using Scalar = Index;
133 static constexpr int NumDimensions = XprTraits::NumDimensions - array_size<Dims>::value;
134 static constexpr int Layout = XprTraits::Layout;
137template <
typename ReduceOp,
typename Dims,
typename XprType>
144template <
typename ReduceOp,
typename Dims,
typename XprType>
145class TensorPairReducerOp :
public TensorBase<TensorPairReducerOp<ReduceOp, Dims, XprType>, ReadOnlyAccessors> {
147 using Scalar =
typename Eigen::internal::traits<TensorPairReducerOp>::Scalar;
148 using RealScalar =
typename Eigen::NumTraits<Scalar>::Real;
149 using Nested =
typename Eigen::internal::ref_selector<TensorPairReducerOp>::type;
150 using StorageKind =
typename Eigen::internal::traits<TensorPairReducerOp>::StorageKind;
151 using Index =
typename Eigen::internal::traits<TensorPairReducerOp>::Index;
152 using CoeffReturnType = Index;
154 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPairReducerOp(
const XprType& expr,
const ReduceOp& reduce_op,
155 const Index return_dim,
const Dims& reduce_dims)
156 : m_xpr(expr), m_reduce_op(reduce_op), m_return_dim(return_dim), m_reduce_dims(reduce_dims) {}
158 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename XprType::Nested>& expression()
const {
return m_xpr; }
160 EIGEN_DEVICE_FUNC
const ReduceOp& reduce_op()
const {
return m_reduce_op; }
162 EIGEN_DEVICE_FUNC
const Dims& reduce_dims()
const {
return m_reduce_dims; }
164 EIGEN_DEVICE_FUNC Index return_dim()
const {
return m_return_dim; }
167 typename XprType::Nested m_xpr;
168 const ReduceOp m_reduce_op;
169 const Index m_return_dim;
170 const Dims m_reduce_dims;
174template <
typename ReduceOp,
typename Dims,
typename ArgType,
typename Device>
175struct TensorEvaluator<const TensorPairReducerOp<ReduceOp, Dims, ArgType>, Device> {
176 using XprType = TensorPairReducerOp<ReduceOp, Dims, ArgType>;
177 using Index =
typename XprType::Index;
178 using Scalar =
typename XprType::Scalar;
179 using CoeffReturnType =
typename XprType::CoeffReturnType;
180 using PairType =
typename TensorIndexPairOp<ArgType>::CoeffReturnType;
181 using Dimensions =
typename TensorEvaluator<const TensorReductionOp<ReduceOp, Dims, const TensorIndexPairOp<ArgType>>,
183 using InputDimensions =
typename TensorEvaluator<const TensorIndexPairOp<ArgType>, Device>::Dimensions;
184 static constexpr int NumDims = internal::array_size<InputDimensions>::value;
185 using StrideDims = array<Index, NumDims>;
186 using Storage = StorageMemory<CoeffReturnType, Device>;
187 using EvaluatorPointerType =
typename Storage::Type;
191 PacketAccess =
false,
193 PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
197 static constexpr int Layout =
198 TensorEvaluator<const TensorReductionOp<ReduceOp, Dims, const TensorIndexPairOp<ArgType>>, Device>::Layout;
200 using TensorBlock = internal::TensorBlockNotImplemented;
203 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
204 : m_orig_impl(op.expression(), device),
205 m_impl(op.expression().index_pairs().reduce(op.reduce_dims(), op.reduce_op()), device),
206 m_return_dim(op.return_dim()) {
207 gen_strides(m_orig_impl.dimensions(), m_strides);
208 EIGEN_IF_CONSTEXPR (Layout ==
static_cast<int>(
ColMajor)) {
209 const Index total_size = internal::array_prod(m_orig_impl.dimensions());
210 m_stride_mod = (m_return_dim < NumDims - 1) ? m_strides[m_return_dim + 1] : total_size;
212 const Index total_size = internal::array_prod(m_orig_impl.dimensions());
213 m_stride_mod = (m_return_dim > 0) ? m_strides[m_return_dim - 1] : total_size;
217 ((m_return_dim >= 0) && (m_return_dim < static_cast<Index>(m_strides.size()))) ? m_strides[m_return_dim] : 1;
220 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_impl.dimensions(); }
222#ifdef EIGEN_USE_THREADS
223 template <
typename EvalSubExprsCallback>
224 EIGEN_STRONG_INLINE
void evalSubExprsIfNeededAsync(EvaluatorPointerType, EvalSubExprsCallback done) {
225 m_impl.evalSubExprsIfNeededAsync(
nullptr, [done](
bool) { done(
true); });
229 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType ) {
230 m_impl.evalSubExprsIfNeeded(
nullptr);
233 EIGEN_STRONG_INLINE
void cleanup() { m_impl.cleanup(); }
235 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
236 const PairType v = m_impl.coeff(index);
237 return (m_return_dim < 0) ? v.first : (v.first % m_stride_mod) / m_stride_div;
240 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return nullptr; }
242 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
243 const double compute_cost =
244 1.0 + (m_return_dim < 0 ? 0.0 : (TensorOpCost::ModCost<Index>() + TensorOpCost::DivCost<Index>()));
245 return m_orig_impl.costPerCoeff(vectorized) + m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, compute_cost);
249 EIGEN_DEVICE_FUNC
void gen_strides(
const InputDimensions& dims, StrideDims& strides) {
250 if (m_return_dim < 0) {
253 eigen_assert(m_return_dim < NumDims &&
"Asking to convert index to a dimension outside of the rank");
257 EIGEN_IF_CONSTEXPR (Layout ==
static_cast<int>(
ColMajor)) {
259 for (
int i = 1; i < NumDims; ++i) {
260 strides[i] = strides[i - 1] * dims[i - 1];
263 strides[NumDims - 1] = 1;
264 for (
int i = NumDims - 2; i >= 0; --i) {
265 strides[i] = strides[i + 1] * dims[i + 1];
271 TensorEvaluator<const TensorIndexPairOp<ArgType>, Device> m_orig_impl;
272 TensorEvaluator<const TensorReductionOp<ReduceOp, Dims, const TensorIndexPairOp<ArgType>>, Device> m_impl;
273 const Index m_return_dim;
274 StrideDims m_strides;
The tensor base class.
Definition TensorForwardDeclarations.h:69
Tensor + Index Pair class.
Definition TensorArgMax.h:44
Converts to Tensor<Pair<Index, Scalar> > and reduces to Tensor<Index>.
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47