Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
TensorConversion.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_CONVERSION_H
12#define EIGEN_TENSOR_TENSOR_CONVERSION_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20template <typename TargetType, typename XprType>
21struct traits<TensorConversionOp<TargetType, XprType> > {
22 // Type promotion to handle the case where the types of the lhs and the rhs are different.
23 typedef TargetType Scalar;
24 typedef typename traits<XprType>::StorageKind StorageKind;
25 typedef typename traits<XprType>::Index Index;
26 static constexpr int NumDimensions = traits<XprType>::NumDimensions;
27 static constexpr int Layout = traits<XprType>::Layout;
28 enum { Flags = 0 };
29 typedef typename TypeConversion<Scalar, typename traits<XprType>::PointerType>::type PointerType;
30};
31
32template <typename TargetType, typename XprType>
33struct eval<TensorConversionOp<TargetType, XprType>, Eigen::Dense> {
34 typedef const TensorConversionOp<TargetType, XprType>& type;
35};
36
37} // end namespace internal
38
39template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket, int SrcCoeffRatio, int TgtCoeffRatio>
40struct PacketConverter;
41
42template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket>
43struct PacketConverter<TensorEvaluator, SrcPacket, TgtPacket, 1, 1> {
44 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketConverter(const TensorEvaluator& impl) : m_impl(impl) {}
45
46 template <int LoadMode, typename Index>
47 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TgtPacket packet(Index index) const {
48 return internal::pcast<SrcPacket, TgtPacket>(m_impl.template packet<LoadMode>(index));
49 }
50
51 private:
52 const TensorEvaluator& m_impl;
53};
54
55template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket>
56struct PacketConverter<TensorEvaluator, SrcPacket, TgtPacket, 2, 1> {
57 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketConverter(const TensorEvaluator& impl) : m_impl(impl) {}
58
59 template <int LoadMode, typename Index>
60 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TgtPacket packet(Index index) const {
61 const int SrcPacketSize = internal::unpacket_traits<SrcPacket>::size;
62
63 SrcPacket src1 = m_impl.template packet<LoadMode>(index);
64 SrcPacket src2 = m_impl.template packet<LoadMode>(index + SrcPacketSize);
65 TgtPacket result = internal::pcast<SrcPacket, TgtPacket>(src1, src2);
66 return result;
67 }
68
69 private:
70 const TensorEvaluator& m_impl;
71};
72
73template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket>
74struct PacketConverter<TensorEvaluator, SrcPacket, TgtPacket, 4, 1> {
75 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketConverter(const TensorEvaluator& impl) : m_impl(impl) {}
76
77 template <int LoadMode, typename Index>
78 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TgtPacket packet(Index index) const {
79 const int SrcPacketSize = internal::unpacket_traits<SrcPacket>::size;
80
81 SrcPacket src1 = m_impl.template packet<LoadMode>(index);
82 SrcPacket src2 = m_impl.template packet<LoadMode>(index + SrcPacketSize);
83 SrcPacket src3 = m_impl.template packet<LoadMode>(index + 2 * SrcPacketSize);
84 SrcPacket src4 = m_impl.template packet<LoadMode>(index + 3 * SrcPacketSize);
85 TgtPacket result = internal::pcast<SrcPacket, TgtPacket>(src1, src2, src3, src4);
86 return result;
87 }
88
89 private:
90 const TensorEvaluator& m_impl;
91};
92
93template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket>
94struct PacketConverter<TensorEvaluator, SrcPacket, TgtPacket, 8, 1> {
95 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketConverter(const TensorEvaluator& impl) : m_impl(impl) {}
96
97 template <int LoadMode, typename Index>
98 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TgtPacket packet(Index index) const {
99 const int SrcPacketSize = internal::unpacket_traits<SrcPacket>::size;
100
101 SrcPacket src1 = m_impl.template packet<LoadMode>(index);
102 SrcPacket src2 = m_impl.template packet<LoadMode>(index + 1 * SrcPacketSize);
103 SrcPacket src3 = m_impl.template packet<LoadMode>(index + 2 * SrcPacketSize);
104 SrcPacket src4 = m_impl.template packet<LoadMode>(index + 3 * SrcPacketSize);
105 SrcPacket src5 = m_impl.template packet<LoadMode>(index + 4 * SrcPacketSize);
106 SrcPacket src6 = m_impl.template packet<LoadMode>(index + 5 * SrcPacketSize);
107 SrcPacket src7 = m_impl.template packet<LoadMode>(index + 6 * SrcPacketSize);
108 SrcPacket src8 = m_impl.template packet<LoadMode>(index + 7 * SrcPacketSize);
109 TgtPacket result = internal::pcast<SrcPacket, TgtPacket>(src1, src2, src3, src4, src5, src6, src7, src8);
110 return result;
111 }
112
113 private:
114 const TensorEvaluator& m_impl;
115};
116
117template <typename TensorEvaluator, typename SrcPacket, typename TgtPacket, int TgtCoeffRatio>
118struct PacketConverter<TensorEvaluator, SrcPacket, TgtPacket, 1, TgtCoeffRatio> {
119 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketConverter(const TensorEvaluator& impl)
120 : m_impl(impl), m_maxIndex(impl.dimensions().TotalSize()) {}
121
122 template <int LoadMode, typename Index>
123 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TgtPacket packet(Index index) const {
124 const int SrcPacketSize = internal::unpacket_traits<SrcPacket>::size;
125 // Only call m_impl.packet() when we have direct access to the underlying data. This
126 // ensures that we don't compute the subexpression twice. We may however load some
127 // coefficients twice, but in practice this doesn't negatively impact performance.
128 if (m_impl.data() && (index + SrcPacketSize < m_maxIndex)) {
129 // Force unaligned memory loads since we can't ensure alignment anymore
130 return internal::pcast<SrcPacket, TgtPacket>(m_impl.template packet<Unaligned>(index));
131 } else {
132 const int TgtPacketSize = internal::unpacket_traits<TgtPacket>::size;
133 typedef typename internal::unpacket_traits<SrcPacket>::type SrcType;
134 typedef typename internal::unpacket_traits<TgtPacket>::type TgtType;
135 internal::scalar_cast_op<SrcType, TgtType> converter;
136 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<TgtPacket>::alignment)
137 typename internal::unpacket_traits<TgtPacket>::type values[TgtPacketSize];
138 EIGEN_UNROLL_LOOP
139 for (int i = 0; i < TgtPacketSize; ++i) {
140 values[i] = converter(m_impl.coeff(index + i));
141 }
142 TgtPacket rslt = internal::pload<TgtPacket>(values);
143 return rslt;
144 }
145 }
146
147 private:
148 const TensorEvaluator& m_impl;
149 const typename TensorEvaluator::Index m_maxIndex;
150};
151
159template <typename TargetType, typename XprType>
160class TensorConversionOp : public TensorBase<TensorConversionOp<TargetType, XprType>, ReadOnlyAccessors> {
161 public:
162 typedef typename internal::traits<TensorConversionOp>::Scalar Scalar;
163 typedef typename internal::traits<TensorConversionOp>::StorageKind StorageKind;
164 typedef typename internal::traits<TensorConversionOp>::Index Index;
165 typedef typename internal::ref_selector<TensorConversionOp>::type Nested;
166 typedef Scalar CoeffReturnType;
167 typedef typename NumTraits<Scalar>::Real RealScalar;
168
169 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorConversionOp(const XprType& xpr) : m_xpr(xpr) {}
170
171 EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
172
173 protected:
174 typename XprType::Nested m_xpr;
175};
176
177template <bool SameType, typename Eval, typename EvalPointerType>
178struct ConversionSubExprEval {
179 static EIGEN_STRONG_INLINE bool run(Eval& impl, EvalPointerType) {
180 impl.evalSubExprsIfNeeded(nullptr);
181 return true;
182 }
183};
184
185template <typename Eval, typename EvalPointerType>
186struct ConversionSubExprEval<true, Eval, EvalPointerType> {
187 static EIGEN_STRONG_INLINE bool run(Eval& impl, EvalPointerType data) { return impl.evalSubExprsIfNeeded(data); }
188};
189
190#ifdef EIGEN_USE_THREADS
191template <bool SameType, typename Eval, typename EvalPointerType, typename EvalSubExprsCallback>
192struct ConversionSubExprEvalAsync {
193 static EIGEN_STRONG_INLINE void run(Eval& impl, EvalPointerType, EvalSubExprsCallback done) {
194 impl.evalSubExprsIfNeededAsync(nullptr, std::move(done));
195 }
196};
197
198template <typename Eval, typename EvalPointerType, typename EvalSubExprsCallback>
199struct ConversionSubExprEvalAsync<true, Eval, EvalPointerType, EvalSubExprsCallback> {
200 static EIGEN_STRONG_INLINE void run(Eval& impl, EvalPointerType data, EvalSubExprsCallback done) {
201 impl.evalSubExprsIfNeededAsync(data, std::move(done));
202 }
203};
204#endif
205
206namespace internal {
207
208template <typename SrcType, typename TargetType, bool IsSameT>
209struct CoeffConv {
210 template <typename ArgType, typename Device>
211 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetType run(const TensorEvaluator<ArgType, Device>& impl,
212 Index index) {
213 internal::scalar_cast_op<SrcType, TargetType> converter;
214 return converter(impl.coeff(index));
215 }
216};
217
218template <typename SrcType, typename TargetType>
219struct CoeffConv<SrcType, TargetType, true> {
220 template <typename ArgType, typename Device>
221 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetType run(const TensorEvaluator<ArgType, Device>& impl,
222 Index index) {
223 return impl.coeff(index);
224 }
225};
226
227template <typename SrcPacket, typename TargetPacket, int LoadMode, bool ActuallyVectorize, bool IsSameT>
228struct PacketConv {
229 typedef typename internal::unpacket_traits<SrcPacket>::type SrcType;
230 typedef typename internal::unpacket_traits<TargetPacket>::type TargetType;
231
232 static constexpr int PacketSize = internal::unpacket_traits<TargetPacket>::size;
233
234 template <typename ArgType, typename Device>
235 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetPacket run(const TensorEvaluator<ArgType, Device>& impl,
236 Index index) {
237 internal::scalar_cast_op<SrcType, TargetType> converter;
238 EIGEN_ALIGN_TO_BOUNDARY(unpacket_traits<TargetPacket>::alignment)
239 std::remove_const_t<TargetType> values[PacketSize];
240 EIGEN_UNROLL_LOOP
241 for (int i = 0; i < PacketSize; ++i) {
242 values[i] = converter(impl.coeff(index + i));
243 }
244 TargetPacket rslt = internal::pload<TargetPacket>(values);
245 return rslt;
246 }
247};
248
249template <typename SrcPacket, typename TargetPacket, int LoadMode, bool IsSameT>
250struct PacketConv<SrcPacket, TargetPacket, LoadMode, true, IsSameT> {
251 typedef typename internal::unpacket_traits<SrcPacket>::type SrcType;
252 typedef typename internal::unpacket_traits<TargetPacket>::type TargetType;
253
254 template <typename ArgType, typename Device>
255 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetPacket run(const TensorEvaluator<ArgType, Device>& impl,
256 Index index) {
257 const int SrcCoeffRatio = internal::type_casting_traits<SrcType, TargetType>::SrcCoeffRatio;
258 const int TgtCoeffRatio = internal::type_casting_traits<SrcType, TargetType>::TgtCoeffRatio;
259 PacketConverter<TensorEvaluator<ArgType, Device>, SrcPacket, TargetPacket, SrcCoeffRatio, TgtCoeffRatio> converter(
260 impl);
261 return converter.template packet<LoadMode>(index);
262 }
263};
264
265template <typename SrcPacket, typename TargetPacket, int LoadMode>
266struct PacketConv<SrcPacket, TargetPacket, LoadMode, /*ActuallyVectorize=*/false, /*IsSameT=*/true> {
267 typedef typename internal::unpacket_traits<TargetPacket>::type TargetType;
268 static constexpr int PacketSize = internal::unpacket_traits<TargetPacket>::size;
269
270 template <typename ArgType, typename Device>
271 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetPacket run(const TensorEvaluator<ArgType, Device>& impl,
272 Index index) {
273 EIGEN_ALIGN_TO_BOUNDARY(unpacket_traits<TargetPacket>::alignment)
274 std::remove_const_t<TargetType> values[PacketSize];
275 for (int i = 0; i < PacketSize; ++i) values[i] = impl.coeff(index + i);
276 return internal::pload<TargetPacket>(values);
277 }
278};
279
280template <typename SrcPacket, typename TargetPacket, int LoadMode>
281struct PacketConv<SrcPacket, TargetPacket, LoadMode, /*ActuallyVectorize=*/true, /*IsSameT=*/true> {
282 template <typename ArgType, typename Device>
283 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TargetPacket run(const TensorEvaluator<ArgType, Device>& impl,
284 Index index) {
285 return impl.template packet<LoadMode>(index);
286 }
287};
288
289} // namespace internal
290
291// Eval as rvalue
292template <typename TargetType, typename ArgType, typename Device>
293struct TensorEvaluator<const TensorConversionOp<TargetType, ArgType>, Device> {
294 typedef TensorConversionOp<TargetType, ArgType> XprType;
295 typedef typename XprType::Index Index;
296 typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
297 typedef TargetType Scalar;
298 typedef TargetType CoeffReturnType;
299 typedef internal::remove_all_t<typename internal::traits<ArgType>::Scalar> SrcType;
300 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
301 typedef typename PacketType<SrcType, Device>::type PacketSourceType;
302 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
303 static constexpr bool IsSameType = std::is_same<TargetType, SrcType>::value;
304 typedef StorageMemory<CoeffReturnType, Device> Storage;
305 typedef typename Storage::Type EvaluatorPointerType;
306
307 enum {
308 IsAligned = false,
309 PacketAccess =
310#ifndef EIGEN_USE_SYCL
311 true,
312#else
313 TensorEvaluator<ArgType, Device>::PacketAccess &
314 internal::type_casting_traits<SrcType, TargetType>::VectorizedCast,
315#endif
316 BlockAccess = TensorEvaluator<ArgType, Device>::BlockAccess,
317 PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
318 RawAccess = false
319 };
320
321 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
322 static constexpr int NumDims = internal::array_size<Dimensions>::value;
323
324 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
325 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
326 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
327
328 typedef typename TensorEvaluator<const ArgType, Device>::TensorBlock ArgTensorBlock;
329
330 struct TensorConversionOpBlockFactory {
331 template <typename ArgXprType>
332 struct XprType {
333 typedef TensorConversionOp<TargetType, const ArgXprType> type;
334 };
335
336 template <typename ArgXprType>
337 typename XprType<ArgXprType>::type expr(const ArgXprType& expr) const {
338 return typename XprType<ArgXprType>::type(expr);
339 }
340 };
341
342 typedef internal::TensorUnaryExprBlock<TensorConversionOpBlockFactory, ArgTensorBlock> TensorBlock;
343 //===--------------------------------------------------------------------===//
344
345 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) : m_impl(op.expression(), device) {}
346
347 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_impl.dimensions(); }
348
349 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
350 return ConversionSubExprEval<IsSameType, TensorEvaluator<ArgType, Device>, EvaluatorPointerType>::run(m_impl, data);
351 }
352
353#ifdef EIGEN_USE_THREADS
354 template <typename EvalSubExprsCallback>
355 EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(EvaluatorPointerType data, EvalSubExprsCallback done) {
356 ConversionSubExprEvalAsync<IsSameType, TensorEvaluator<ArgType, Device>, EvaluatorPointerType,
357 EvalSubExprsCallback>::run(m_impl, data, std::move(done));
358 }
359#endif
360
361 EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
362
363 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
364 return internal::CoeffConv<SrcType, TargetType, IsSameType>::run(m_impl, index);
365 }
366
367 template <int LoadMode>
368 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
369 // If we are not going to do the cast, we just need to check that base
370 // TensorEvaluator has packet access. Otherwise we also need to make sure,
371 // that we have an implementation of vectorized cast.
372 constexpr bool Vectorizable = IsSameType
373 ? TensorEvaluator<ArgType, Device>::PacketAccess
374 : int(TensorEvaluator<ArgType, Device>::PacketAccess) &
375 int(internal::type_casting_traits<SrcType, TargetType>::VectorizedCast);
376
377 return internal::PacketConv<PacketSourceType, PacketReturnType, LoadMode, Vectorizable, IsSameType>::run(m_impl,
378 index);
379 }
380
381 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
382 const double cast_cost = TensorOpCost::CastCost<SrcType, TargetType>();
383 if (vectorized) {
384 const double SrcCoeffRatio = internal::type_casting_traits<SrcType, TargetType>::SrcCoeffRatio;
385 const double TgtCoeffRatio = internal::type_casting_traits<SrcType, TargetType>::TgtCoeffRatio;
386 return m_impl.costPerCoeff(vectorized) * (SrcCoeffRatio / PacketSize) +
387 TensorOpCost(0, 0, TgtCoeffRatio * (cast_cost / PacketSize));
388 } else {
389 return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, cast_cost);
390 }
391 }
392
393 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const {
394 return m_impl.getResourceRequirements();
395 }
396
397 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
398 bool /*root_of_expr_ast*/ = false) const {
399 // The forwarded destination buffer is sized for TargetType (the assign
400 // LHS); the child block evaluator below us writes SrcType. When the cast
401 // is non-degenerate the buffer would be misinterpreted by any
402 // block-materializing child's prepareStorage (assert in debug, corruption
403 // in release). Drop the buffer; the child falls back to scratch and
404 // writeBlock still lands the cast values in the LHS.
405 EIGEN_IF_CONSTEXPR (!IsSameType) {
406 desc.DropDestinationBuffer();
407 }
408 return TensorBlock(m_impl.block(desc, scratch), TensorConversionOpBlockFactory());
409 }
410
411 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return nullptr; }
412
414 const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
415
416 protected:
417 TensorEvaluator<ArgType, Device> m_impl;
418};
419
420} // end namespace Eigen
421
422#endif // EIGEN_TENSOR_TENSOR_CONVERSION_H
The tensor base class.
Definition TensorForwardDeclarations.h:69
Tensor conversion class. This class makes it possible to vectorize type casting operations when the n...
Definition TensorConversion.h:160
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47