TensorConcatenation.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 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 
10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_CONCATENATION_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_CONCATENATION_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
24 namespace internal {
25 template<typename Axis, typename LhsXprType, typename RhsXprType>
26 struct traits<TensorConcatenationOp<Axis, LhsXprType, RhsXprType> >
27 {
28  // Type promotion to handle the case where the types of the lhs and the rhs are different.
29  typedef typename promote_storage_type<typename LhsXprType::Scalar,
30  typename RhsXprType::Scalar>::ret Scalar;
31  typedef typename promote_storage_type<typename traits<LhsXprType>::StorageKind,
32  typename traits<RhsXprType>::StorageKind>::ret StorageKind;
33  typedef typename promote_index_type<typename traits<LhsXprType>::Index,
34  typename traits<RhsXprType>::Index>::type Index;
35  typedef typename LhsXprType::Nested LhsNested;
36  typedef typename RhsXprType::Nested RhsNested;
37  typedef std::remove_reference_t<LhsNested> LhsNested_;
38  typedef std::remove_reference_t<RhsNested> RhsNested_;
39  static constexpr int NumDimensions = traits<LhsXprType>::NumDimensions;
40  static constexpr int Layout = traits<LhsXprType>::Layout;
41  enum { Flags = 0 };
42  typedef std::conditional_t<Pointer_type_promotion<typename LhsXprType::Scalar, Scalar>::val,
44 };
45 
46 template<typename Axis, typename LhsXprType, typename RhsXprType>
47 struct eval<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, Eigen::Dense>
48 {
49  typedef const TensorConcatenationOp<Axis, LhsXprType, RhsXprType>& type;
50 };
51 
52 template<typename Axis, typename LhsXprType, typename RhsXprType>
53 struct nested<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, 1, typename eval<TensorConcatenationOp<Axis, LhsXprType, RhsXprType> >::type>
54 {
55  typedef TensorConcatenationOp<Axis, LhsXprType, RhsXprType> type;
56 };
57 
58 } // end namespace internal
59 
60 
61 template<typename Axis, typename LhsXprType, typename RhsXprType>
62 class TensorConcatenationOp : public TensorBase<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, WriteAccessors>
63 {
64  public:
66  typedef typename internal::traits<TensorConcatenationOp>::Scalar Scalar;
67  typedef typename internal::traits<TensorConcatenationOp>::StorageKind StorageKind;
68  typedef typename internal::traits<TensorConcatenationOp>::Index Index;
69  typedef typename internal::nested<TensorConcatenationOp>::type Nested;
70  typedef typename internal::promote_storage_type<typename LhsXprType::CoeffReturnType,
71  typename RhsXprType::CoeffReturnType>::ret CoeffReturnType;
73 
74  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorConcatenationOp(const LhsXprType& lhs, const RhsXprType& rhs, Axis axis)
75  : m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_axis(axis) {}
76 
79  lhsExpression() const { return m_lhs_xpr; }
80 
83  rhsExpression() const { return m_rhs_xpr; }
84 
85  EIGEN_DEVICE_FUNC const Axis& axis() const { return m_axis; }
86 
88  protected:
89  typename LhsXprType::Nested m_lhs_xpr;
90  typename RhsXprType::Nested m_rhs_xpr;
91  const Axis m_axis;
92 };
93 
94 
95 // Eval as rvalue
96 template<typename Axis, typename LeftArgType, typename RightArgType, typename Device>
97 struct TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
98 {
100  typedef typename XprType::Index Index;
101  static constexpr int NumDims = internal::array_size<typename TensorEvaluator<LeftArgType, Device>::Dimensions>::value;
102  static constexpr int RightNumDims = internal::array_size<typename TensorEvaluator<RightArgType, Device>::Dimensions>::value;
104  typedef typename XprType::Scalar Scalar;
110  enum {
111  IsAligned = false,
114  BlockAccess = false,
117  RawAccess = false
118  };
119 
120  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
121  typedef internal::TensorBlockNotImplemented TensorBlock;
122  //===--------------------------------------------------------------------===//
123 
124  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
125  : m_leftImpl(op.lhsExpression(), device), m_rightImpl(op.rhsExpression(), device), m_axis(op.axis())
126  {
127  EIGEN_STATIC_ASSERT((static_cast<int>(TensorEvaluator<LeftArgType, Device>::Layout) == static_cast<int>(TensorEvaluator<RightArgType, Device>::Layout) || NumDims == 1), YOU_MADE_A_PROGRAMMING_MISTAKE);
128  EIGEN_STATIC_ASSERT((NumDims == RightNumDims), YOU_MADE_A_PROGRAMMING_MISTAKE);
129  EIGEN_STATIC_ASSERT((NumDims > 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
130 
131  eigen_assert(0 <= m_axis && m_axis < NumDims);
132  const Dimensions& lhs_dims = m_leftImpl.dimensions();
133  const Dimensions& rhs_dims = m_rightImpl.dimensions();
134  {
135  int i = 0;
136  for (; i < m_axis; ++i) {
137  eigen_assert(lhs_dims[i] > 0);
138  eigen_assert(lhs_dims[i] == rhs_dims[i]);
139  m_dimensions[i] = lhs_dims[i];
140  }
141  eigen_assert(lhs_dims[i] > 0); // Now i == m_axis.
142  eigen_assert(rhs_dims[i] > 0);
143  m_dimensions[i] = lhs_dims[i] + rhs_dims[i];
144  for (++i; i < NumDims; ++i) {
145  eigen_assert(lhs_dims[i] > 0);
146  eigen_assert(lhs_dims[i] == rhs_dims[i]);
147  m_dimensions[i] = lhs_dims[i];
148  }
149  }
150 
151  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
152  m_leftStrides[0] = 1;
153  m_rightStrides[0] = 1;
154  m_outputStrides[0] = 1;
155 
156  for (int j = 1; j < NumDims; ++j) {
157  m_leftStrides[j] = m_leftStrides[j-1] * lhs_dims[j-1];
158  m_rightStrides[j] = m_rightStrides[j-1] * rhs_dims[j-1];
159  m_outputStrides[j] = m_outputStrides[j-1] * m_dimensions[j-1];
160  }
161  } else {
162  m_leftStrides[NumDims - 1] = 1;
163  m_rightStrides[NumDims - 1] = 1;
164  m_outputStrides[NumDims - 1] = 1;
165 
166  for (int j = NumDims - 2; j >= 0; --j) {
167  m_leftStrides[j] = m_leftStrides[j+1] * lhs_dims[j+1];
168  m_rightStrides[j] = m_rightStrides[j+1] * rhs_dims[j+1];
169  m_outputStrides[j] = m_outputStrides[j+1] * m_dimensions[j+1];
170  }
171  }
172  }
173 
174  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
175 
176  // TODO(phli): Add short-circuit memcpy evaluation if underlying data are linear?
177  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
178  {
179  m_leftImpl.evalSubExprsIfNeeded(NULL);
180  m_rightImpl.evalSubExprsIfNeeded(NULL);
181  return true;
182  }
183 
184  EIGEN_STRONG_INLINE void cleanup()
185  {
186  m_leftImpl.cleanup();
187  m_rightImpl.cleanup();
188  }
189 
190  // TODO(phli): attempt to speed this up. The integer divisions and modulo are slow.
191  // See CL/76180724 comments for more ideas.
192  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
193  {
194  // Collect dimension-wise indices (subs).
196  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
197  for (int i = NumDims - 1; i > 0; --i) {
198  subs[i] = index / m_outputStrides[i];
199  index -= subs[i] * m_outputStrides[i];
200  }
201  subs[0] = index;
202  } else {
203  for (int i = 0; i < NumDims - 1; ++i) {
204  subs[i] = index / m_outputStrides[i];
205  index -= subs[i] * m_outputStrides[i];
206  }
207  subs[NumDims - 1] = index;
208  }
209 
210  const Dimensions& left_dims = m_leftImpl.dimensions();
211  if (subs[m_axis] < left_dims[m_axis]) {
212  Index left_index;
213  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
214  left_index = subs[0];
216  for (int i = 1; i < NumDims; ++i) {
217  left_index += (subs[i] % left_dims[i]) * m_leftStrides[i];
218  }
219  } else {
220  left_index = subs[NumDims - 1];
222  for (int i = NumDims - 2; i >= 0; --i) {
223  left_index += (subs[i] % left_dims[i]) * m_leftStrides[i];
224  }
225  }
226  return m_leftImpl.coeff(left_index);
227  } else {
228  subs[m_axis] -= left_dims[m_axis];
229  const Dimensions& right_dims = m_rightImpl.dimensions();
230  Index right_index;
231  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
232  right_index = subs[0];
234  for (int i = 1; i < NumDims; ++i) {
235  right_index += (subs[i] % right_dims[i]) * m_rightStrides[i];
236  }
237  } else {
238  right_index = subs[NumDims - 1];
240  for (int i = NumDims - 2; i >= 0; --i) {
241  right_index += (subs[i] % right_dims[i]) * m_rightStrides[i];
242  }
243  }
244  return m_rightImpl.coeff(right_index);
245  }
246  }
247 
248  // TODO(phli): Add a real vectorization.
249  template<int LoadMode>
250  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
251  {
252  const int packetSize = PacketType<CoeffReturnType, Device>::size;
253  EIGEN_STATIC_ASSERT((packetSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
254  eigen_assert(index + packetSize - 1 < dimensions().TotalSize());
255 
256  EIGEN_ALIGN_MAX CoeffReturnType values[packetSize];
258  for (int i = 0; i < packetSize; ++i) {
259  values[i] = coeff(index+i);
260  }
261  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
262  return rslt;
263  }
264 
265  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
266  costPerCoeff(bool vectorized) const {
267  const double compute_cost = NumDims * (2 * TensorOpCost::AddCost<Index>() +
268  2 * TensorOpCost::MulCost<Index>() +
269  TensorOpCost::DivCost<Index>() +
270  TensorOpCost::ModCost<Index>());
271  const double lhs_size = m_leftImpl.dimensions().TotalSize();
272  const double rhs_size = m_rightImpl.dimensions().TotalSize();
273  return (lhs_size / (lhs_size + rhs_size)) *
274  m_leftImpl.costPerCoeff(vectorized) +
275  (rhs_size / (lhs_size + rhs_size)) *
276  m_rightImpl.costPerCoeff(vectorized) +
277  TensorOpCost(0, 0, compute_cost);
278  }
279 
280  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
281 
282  protected:
289  const Axis m_axis;
290 };
291 
292 // Eval as lvalue
293 template<typename Axis, typename LeftArgType, typename RightArgType, typename Device>
294  struct TensorEvaluator<TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
295  : public TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>
296 {
299  typedef typename Base::Dimensions Dimensions;
301  enum {
302  IsAligned = false,
305  BlockAccess = false,
308  RawAccess = false
309  };
310 
311  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
312  typedef internal::TensorBlockNotImplemented TensorBlock;
313  //===--------------------------------------------------------------------===//
314 
315  EIGEN_STRONG_INLINE TensorEvaluator(XprType& op, const Device& device)
316  : Base(op, device)
317  {
318  EIGEN_STATIC_ASSERT((static_cast<int>(Layout) == static_cast<int>(ColMajor)), YOU_MADE_A_PROGRAMMING_MISTAKE);
319  }
320 
321  typedef typename XprType::Index Index;
322  typedef typename XprType::Scalar Scalar;
325 
326  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index) const
327  {
328  // Collect dimension-wise indices (subs).
330  for (int i = Base::NumDims - 1; i > 0; --i) {
331  subs[i] = index / this->m_outputStrides[i];
332  index -= subs[i] * this->m_outputStrides[i];
333  }
334  subs[0] = index;
335 
336  const Dimensions& left_dims = this->m_leftImpl.dimensions();
337  if (subs[this->m_axis] < left_dims[this->m_axis]) {
338  Index left_index = subs[0];
339  for (int i = 1; i < Base::NumDims; ++i) {
340  left_index += (subs[i] % left_dims[i]) * this->m_leftStrides[i];
341  }
342  return this->m_leftImpl.coeffRef(left_index);
343  } else {
344  subs[this->m_axis] -= left_dims[this->m_axis];
345  const Dimensions& right_dims = this->m_rightImpl.dimensions();
346  Index right_index = subs[0];
347  for (int i = 1; i < Base::NumDims; ++i) {
348  right_index += (subs[i] % right_dims[i]) * this->m_rightStrides[i];
349  }
350  return this->m_rightImpl.coeffRef(right_index);
351  }
352  }
353 
354  template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
355  void writePacket(Index index, const PacketReturnType& x) const
356  {
357  const int packetSize = PacketType<CoeffReturnType, Device>::size;
358  EIGEN_STATIC_ASSERT((packetSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
359  eigen_assert(index + packetSize - 1 < this->dimensions().TotalSize());
360 
361  EIGEN_ALIGN_MAX CoeffReturnType values[packetSize];
362  internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
363  for (int i = 0; i < packetSize; ++i) {
364  coeffRef(index+i) = values[i];
365  }
366  }
367 };
368 
369 } // end namespace Eigen
370 
371 #endif // EIGEN_CXX11_TENSOR_TENSOR_CONCATENATION_H
int i
#define EIGEN_ALIGN_MAX
#define EIGEN_UNROLL_LOOP
#define EIGEN_DEVICE_FUNC
#define eigen_assert(x)
#define EIGEN_STATIC_ASSERT(X, MSG)
#define EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: TensorMacros.h:80
The tensor base class.
Tensor concatenation class.
internal::traits< TensorConcatenationOp >::StorageKind StorageKind
const internal::remove_all_t< typename RhsXprType::Nested > & rhsExpression() const
TensorBase< TensorConcatenationOp< Axis, LhsXprType, RhsXprType >, WriteAccessors > Base
internal::nested< TensorConcatenationOp >::type Nested
internal::traits< TensorConcatenationOp >::Index Index
NumTraits< Scalar >::Real RealScalar
TensorConcatenationOp(const LhsXprType &lhs, const RhsXprType &rhs, Axis axis)
const internal::remove_all_t< typename LhsXprType::Nested > & lhsExpression() const
internal::traits< TensorConcatenationOp >::Scalar Scalar
internal::promote_storage_type< typename LhsXprType::CoeffReturnType, typename RhsXprType::CoeffReturnType >::ret CoeffReturnType
WriteAccessors
typename remove_all< T >::type remove_all_t
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
std::array< T, N > array
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
internal::packet_traits< Scalar >::type type
Definition: TensorMeta.h:55
TensorEvaluator< const TensorConcatenationOp< Axis, LeftArgType, RightArgType >, Device > Base
A cost model used to limit the number of threads used for evaluating tensor expression.
const Dimensions & dimensions() const
static constexpr int Layout
CoeffReturnType coeff(Index index) const
CoeffReturnType & coeffRef(Index index) const
std::ptrdiff_t j