Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
TensorArgMax.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2015 Eugene Brevdo <ebrevdo@gmail.com>
5// Benoit Steiner <benoit.steiner.goog@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_TENSOR_TENSOR_ARG_MAX_H
13#define EIGEN_TENSOR_TENSOR_ARG_MAX_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19namespace internal {
20
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;
29};
30
31template <typename XprType>
32struct eval<TensorIndexPairOp<XprType>, Eigen::Dense> {
33 using type = const TensorIndexPairOp<XprType> EIGEN_DEVICE_REF;
34};
35
36} // end namespace internal
37
43template <typename XprType>
44class TensorIndexPairOp : public TensorBase<TensorIndexPairOp<XprType>, ReadOnlyAccessors> {
45 public:
46 using Scalar = typename Eigen::internal::traits<TensorIndexPairOp>::Scalar;
47 using RealScalar = typename Eigen::NumTraits<Scalar>::Real;
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>;
52
53 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIndexPairOp(const XprType& expr) : m_xpr(expr) {}
54
55 EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
56
57 protected:
58 typename XprType::Nested m_xpr;
59};
60
61// Eval as rvalue
62template <typename ArgType, typename Device>
63struct TensorEvaluator<const TensorIndexPairOp<ArgType>, Device> {
64 using XprType = TensorIndexPairOp<ArgType>;
65 using Index = typename XprType::Index;
66 using Scalar = typename XprType::Scalar;
67 using CoeffReturnType = typename XprType::CoeffReturnType;
68
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;
73
74 enum {
75 IsAligned = false,
76 PacketAccess = false,
77 BlockAccess = false,
79 CoordAccess = false, // to be implemented
80 RawAccess = false
81 };
82 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
83
84 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
85 using TensorBlock = internal::TensorBlockNotImplemented;
86 //===--------------------------------------------------------------------===//
87
88 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) : m_impl(op.expression(), device) {}
89
90 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_impl.dimensions(); }
91
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); });
96 }
97#endif // EIGEN_USE_THREADS
98
99 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
100 m_impl.evalSubExprsIfNeeded(nullptr);
101 return true;
102 }
103 EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
104
105 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
106 return CoeffReturnType(index, m_impl.coeff(index));
107 }
108
109 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
110 return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, 1);
111 }
112
113 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return nullptr; }
114
115 protected:
116 TensorEvaluator<ArgType, Device> m_impl;
117};
118
119namespace internal {
120
127template <typename ReduceOp, typename Dims, typename XprType>
128struct traits<TensorPairReducerOp<ReduceOp, Dims, XprType>> : public traits<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;
135};
136
137template <typename ReduceOp, typename Dims, typename XprType>
138struct eval<TensorPairReducerOp<ReduceOp, Dims, XprType>, Eigen::Dense> {
139 using type = const TensorPairReducerOp<ReduceOp, Dims, XprType> EIGEN_DEVICE_REF;
140};
141
142} // end namespace internal
143
144template <typename ReduceOp, typename Dims, typename XprType>
145class TensorPairReducerOp : public TensorBase<TensorPairReducerOp<ReduceOp, Dims, XprType>, ReadOnlyAccessors> {
146 public:
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;
153
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) {}
157
158 EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
159
160 EIGEN_DEVICE_FUNC const ReduceOp& reduce_op() const { return m_reduce_op; }
161
162 EIGEN_DEVICE_FUNC const Dims& reduce_dims() const { return m_reduce_dims; }
163
164 EIGEN_DEVICE_FUNC Index return_dim() const { return m_return_dim; }
165
166 protected:
167 typename XprType::Nested m_xpr;
168 const ReduceOp m_reduce_op;
169 const Index m_return_dim;
170 const Dims m_reduce_dims;
171};
172
173// Eval as rvalue
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>>,
182 Device>::Dimensions;
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;
188
189 enum {
190 IsAligned = false,
191 PacketAccess = false,
192 BlockAccess = false,
193 PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
194 CoordAccess = false, // to be implemented
195 RawAccess = false
196 };
197 static constexpr int Layout =
198 TensorEvaluator<const TensorReductionOp<ReduceOp, Dims, const TensorIndexPairOp<ArgType>>, Device>::Layout;
199 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
200 using TensorBlock = internal::TensorBlockNotImplemented;
201 //===--------------------------------------------------------------------===//
202
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;
211 } else {
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;
214 }
215 // If m_return_dim is not a valid index, returns 1 or this can crash on Windows.
216 m_stride_div =
217 ((m_return_dim >= 0) && (m_return_dim < static_cast<Index>(m_strides.size()))) ? m_strides[m_return_dim] : 1;
218 }
219
220 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_impl.dimensions(); }
221
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); });
226 }
227#endif // EIGEN_USE_THREADS
228
229 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
230 m_impl.evalSubExprsIfNeeded(nullptr);
231 return true;
232 }
233 EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
234
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;
238 }
239
240 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return nullptr; }
241
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);
246 }
247
248 private:
249 EIGEN_DEVICE_FUNC void gen_strides(const InputDimensions& dims, StrideDims& strides) {
250 if (m_return_dim < 0) {
251 return; // Won't be using the strides.
252 }
253 eigen_assert(m_return_dim < NumDims && "Asking to convert index to a dimension outside of the rank");
254
255 // Calculate m_stride_div and m_stride_mod, which are used to
256 // calculate the value of an index w.r.t. the m_return_dim.
257 EIGEN_IF_CONSTEXPR (Layout == static_cast<int>(ColMajor)) {
258 strides[0] = 1;
259 for (int i = 1; i < NumDims; ++i) {
260 strides[i] = strides[i - 1] * dims[i - 1];
261 }
262 } else {
263 strides[NumDims - 1] = 1;
264 for (int i = NumDims - 2; i >= 0; --i) {
265 strides[i] = strides[i + 1] * dims[i + 1];
266 }
267 }
268 }
269
270 protected:
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;
275 Index m_stride_mod;
276 Index m_stride_div;
277};
278
279} // end namespace Eigen
280
281#endif // EIGEN_TENSOR_TENSOR_ARG_MAX_H
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