10#ifndef EIGEN_CXX11_TENSOR_TENSOR_CONTRACTION_H
11#define EIGEN_CXX11_TENSOR_TENSOR_CONTRACTION_H
14#include "./InternalHeaderCheck.h"
20template <
typename Dimensions,
typename LhsXprType,
typename RhsXprType,
typename OutputKernelType>
21struct traits<TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType>> {
23 typedef typename gebp_traits<std::remove_const_t<typename LhsXprType::Scalar>,
24 std::remove_const_t<typename RhsXprType::Scalar>>::ResScalar Scalar;
26 typedef typename promote_storage_type<typename traits<LhsXprType>::StorageKind,
27 typename traits<RhsXprType>::StorageKind>::ret StorageKind;
29 typename promote_index_type<typename traits<LhsXprType>::Index,
typename traits<RhsXprType>::Index>::type
Index;
30 typedef typename LhsXprType::Nested LhsNested;
31 typedef typename RhsXprType::Nested RhsNested;
32 typedef std::remove_reference_t<LhsNested> LhsNested_;
33 typedef std::remove_reference_t<RhsNested> RhsNested_;
36 static constexpr int NumDimensions =
37 traits<LhsXprType>::NumDimensions + traits<RhsXprType>::NumDimensions - 2 * array_size<Dimensions>::value;
38 static constexpr int Layout = traits<LhsXprType>::Layout;
39 typedef std::conditional_t<Pointer_type_promotion<typename LhsXprType::Scalar, Scalar>::val,
40 typename traits<LhsXprType>::PointerType,
typename traits<RhsXprType>::PointerType>
46template <
typename Dimensions,
typename LhsXprType,
typename RhsXprType,
typename OutputKernelType>
47struct eval<TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType>, Eigen::Dense> {
48 typedef const TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType>& type;
51template <
typename Dimensions,
typename LhsXprType,
typename RhsXprType,
typename OutputKernelType>
52struct nested<TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType>, 1,
53 typename eval<TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType>>::type> {
54 typedef TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType> type;
57template <
typename Indices_,
typename LeftArgType_,
typename RightArgType_,
typename OutputKernelType_,
60 TensorEvaluator<const TensorContractionOp<Indices_, LeftArgType_, RightArgType_, OutputKernelType_>, Device_>> {
61 typedef Indices_ Indices;
62 typedef LeftArgType_ LeftArgType;
63 typedef RightArgType_ RightArgType;
64 typedef OutputKernelType_ OutputKernelType;
65 typedef Device_ Device;
68 static constexpr int NumDimensions =
69 traits<LeftArgType_>::NumDimensions + traits<RightArgType_>::NumDimensions - 2 * array_size<Indices_>::value;
73template <
typename LhsScalar,
typename RhsScalar>
74struct TensorContractionBlockMemAllocator {
75 typedef void* BlockMemHandle;
77 template <
typename Device>
78 EIGEN_DEVICE_FUNC
static BlockMemHandle allocate(Device& d,
const Index bm,
const Index bk,
const Index bn,
79 LhsScalar** lhs_block, RhsScalar** rhs_block) {
80 eigen_assert(lhs_block);
81 eigen_assert(rhs_block);
82 BlockSizes sz = ComputeLhsRhsBlockSizes(bm, bk, bn);
83 char* block_mem =
static_cast<char*
>(d.allocate(sz.lhs_size + sz.rhs_size));
84 *lhs_block =
static_cast<LhsScalar*
>(
static_cast<void*
>(block_mem));
85 *rhs_block =
static_cast<RhsScalar*
>(
static_cast<void*
>(block_mem + sz.lhs_size));
89 template <
typename Device>
90 EIGEN_DEVICE_FUNC
static BlockMemHandle allocateSlices(Device& d,
const Index bm,
const Index bk,
const Index bn,
92 const Index num_slices, std::vector<LhsScalar*>* lhs_blocks,
93 std::vector<RhsScalar*>* rhs_blocks) {
94 eigen_assert(num_slices > 0);
95 eigen_assert(num_lhs >= 0 && num_rhs >= 0);
96 eigen_assert(num_lhs == 0 || lhs_blocks);
97 eigen_assert(num_rhs == 0 || rhs_blocks);
98 BlockSizes sz = ComputeLhsRhsBlockSizes(bm, bk, bn);
99 void* block_mem = d.allocate((num_lhs * sz.lhs_size + num_rhs * sz.rhs_size) * num_slices);
100 eigen_assert(block_mem);
101 char* mem =
static_cast<char*
>(block_mem);
103 for (
Index x = 0; x < num_slices; x++) {
104 if (num_lhs > 0) lhs_blocks[x].resize(num_lhs);
105 for (
Index m = 0; m < num_lhs; m++) {
106 lhs_blocks[x][m] =
static_cast<LhsScalar*
>(
static_cast<void*
>(mem));
109 if (num_rhs > 0) rhs_blocks[x].resize(num_rhs);
110 for (
Index n = 0; n < num_rhs; n++) {
111 rhs_blocks[x][n] =
static_cast<RhsScalar*
>(
static_cast<void*
>(mem));
119 template <
typename Device>
120 EIGEN_DEVICE_FUNC
static void deallocate(Device& d, BlockMemHandle handle) {
121 d.deallocate(handle);
129 EIGEN_DEVICE_FUNC
static BlockSizes ComputeLhsRhsBlockSizes(
const Index bm,
const Index bk,
const Index bn) {
130 Index align = numext::maxi(EIGEN_MAX_ALIGN_BYTES, 1);
132 sz.lhs_size = numext::div_ceil<Index>(bm * bk *
sizeof(LhsScalar), align) * align;
133 sz.rhs_size = numext::div_ceil<Index>(bn * bk *
sizeof(RhsScalar), align) * align;
166template <
typename ResScalar,
typename LhsScalar,
typename RhsScalar,
typename StorageIndex,
typename OutputMapper,
167 typename LhsMapper,
typename RhsMapper>
168struct TensorContractionKernel {
171 enum { HasBeta =
false };
173 EIGEN_DEVICE_FUNC TensorContractionKernel(StorageIndex m_, StorageIndex k_, StorageIndex n_, StorageIndex bm_,
174 StorageIndex bk_, StorageIndex bn_)
175 : m(m_), k(k_), n(n_), bm(bm_), bk(bk_), bn(bn_) {}
178 typedef LhsScalar* LhsBlock;
179 typedef RhsScalar* RhsBlock;
182 typedef TensorContractionBlockMemAllocator<LhsScalar, RhsScalar> BlockMemAllocator;
183 typedef typename BlockMemAllocator::BlockMemHandle BlockMemHandle;
185 typedef typename internal::gebp_traits<LhsScalar, RhsScalar> Traits;
187 typedef internal::gemm_pack_lhs<LhsScalar, StorageIndex,
typename LhsMapper::SubMapper, Traits::mr,
188 Traits::LhsProgress,
typename Traits::LhsPacket4Packing,
ColMajor>
191 typedef internal::gemm_pack_rhs<RhsScalar, StorageIndex, typename RhsMapper::SubMapper, Traits::nr, ColMajor>
194 typedef internal::gebp_kernel<LhsScalar, RhsScalar, StorageIndex, OutputMapper, Traits::mr, Traits::nr,
198 template <
typename Device>
199 EIGEN_DEVICE_FUNC BlockMemHandle allocate(Device& d, LhsBlock* lhs_block, RhsBlock* rhs_block) {
200 return BlockMemAllocator::allocate(d, bm, bk, bn, lhs_block, rhs_block);
203 template <
typename Device>
204 EIGEN_DEVICE_FUNC BlockMemHandle allocateSlices(Device& d,
const StorageIndex num_lhs,
const StorageIndex num_rhs,
205 const StorageIndex num_slices, std::vector<LhsBlock>* lhs_blocks,
206 std::vector<RhsBlock>* rhs_blocks) {
207 return BlockMemAllocator::allocateSlices(d, bm, bk, bn, num_lhs, num_rhs, num_slices, lhs_blocks, rhs_blocks);
210 template <
typename Device>
211 EIGEN_DEVICE_FUNC
static void deallocate(Device& d, BlockMemHandle handle) {
212 BlockMemAllocator::deallocate(d, handle);
215 EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE
void packLhs(LhsBlock* lhsBlock,
const typename LhsMapper::SubMapper& data_mapper,
216 const StorageIndex depth,
const StorageIndex rows) {
217 LhsPacker()(*lhsBlock, data_mapper, depth, rows, 0,
221 EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE
void packRhs(RhsBlock* rhsBlock,
const typename RhsMapper::SubMapper& data_mapper,
222 const StorageIndex depth,
const StorageIndex cols) {
223 RhsPacker()(*rhsBlock, data_mapper, depth, cols);
226 EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE
void invoke(
const OutputMapper& output_mapper,
const LhsBlock& lhsBlock,
227 const RhsBlock& rhsBlock,
const StorageIndex rows,
228 const StorageIndex depth,
const StorageIndex cols,
229 const ResScalar alpha,
const ResScalar beta) {
231 eigen_assert(beta == ResScalar(1));
232 static const int kComputeStrideFromBlockDimensions = -1;
233 GebpKernel()(output_mapper, lhsBlock, rhsBlock, rows, depth, cols, alpha,
234 kComputeStrideFromBlockDimensions,
235 kComputeStrideFromBlockDimensions,
243 const StorageIndex m;
244 const StorageIndex k;
245 const StorageIndex n;
246 const StorageIndex bm;
247 const StorageIndex bk;
248 const StorageIndex bn;
255struct TensorContractionParams {
258 bool swapped_arguments;
268struct NoOpOutputKernel {
284 template <
typename Index,
typename Scalar>
285 EIGEN_ALWAYS_INLINE
void operator()(
const internal::blas_data_mapper<Scalar, Index, ColMajor>& output_mapper,
286 const TensorContractionParams& params,
Index i,
Index j,
Index num_rows,
287 Index num_cols)
const {
288 EIGEN_UNUSED_VARIABLE(output_mapper);
289 EIGEN_UNUSED_VARIABLE(params);
290 EIGEN_UNUSED_VARIABLE(i);
291 EIGEN_UNUSED_VARIABLE(j);
292 EIGEN_UNUSED_VARIABLE(num_rows);
293 EIGEN_UNUSED_VARIABLE(num_cols);
300template <
typename Indices,
typename LhsXprType,
typename RhsXprType,
301 typename OutputKernelType =
const NoOpOutputKernel>
302class TensorContractionOp
303 :
public TensorBase<TensorContractionOp<Indices, LhsXprType, RhsXprType, OutputKernelType>, ReadOnlyAccessors> {
305 typedef typename Eigen::internal::traits<TensorContractionOp>::Scalar Scalar;
306 typedef typename internal::gebp_traits<
typename LhsXprType::CoeffReturnType,
307 typename RhsXprType::CoeffReturnType>::ResScalar CoeffReturnType;
308 typedef typename Eigen::internal::nested<TensorContractionOp>::type Nested;
309 typedef typename Eigen::internal::traits<TensorContractionOp>::StorageKind StorageKind;
310 typedef typename Eigen::internal::traits<TensorContractionOp>::Index Index;
312 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorContractionOp(
const LhsXprType& lhs,
const RhsXprType& rhs,
314 const OutputKernelType& output_kernel = OutputKernelType())
315 : m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_indices(dims), m_output_kernel(output_kernel) {}
317 EIGEN_DEVICE_FUNC
const Indices& indices()
const {
return m_indices; }
320 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename LhsXprType::Nested>&
lhsExpression()
const {
324 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename RhsXprType::Nested>& rhsExpression()
const {
328 EIGEN_DEVICE_FUNC
const OutputKernelType& outputKernel()
const {
return m_output_kernel; }
331 typename LhsXprType::Nested m_lhs_xpr;
332 typename RhsXprType::Nested m_rhs_xpr;
333 const Indices m_indices;
334 const OutputKernelType m_output_kernel;
337template <
typename Derived>
338struct TensorContractionEvaluatorBase {
339 typedef typename internal::traits<Derived>::Indices Indices;
340 typedef typename internal::traits<Derived>::LeftArgType LeftArgType;
341 typedef typename internal::traits<Derived>::RightArgType RightArgType;
342 typedef typename internal::traits<Derived>::OutputKernelType OutputKernelType;
343 typedef typename internal::traits<Derived>::Device Device;
345 typedef TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType> XprType;
346 typedef std::remove_const_t<typename XprType::Scalar> Scalar;
347 typedef typename XprType::Index Index;
348 typedef typename XprType::CoeffReturnType CoeffReturnType;
349 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
350 typedef StorageMemory<Scalar, Device> Storage;
351 typedef typename Storage::Type EvaluatorPointerType;
353 static constexpr int Layout = TensorEvaluator<LeftArgType, Device>::Layout;
356 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
358 PreferBlockAccess =
false,
364 typedef internal::TensorBlockNotImplemented TensorBlock;
371 typedef std::conditional_t<static_cast<int>(Layout) ==
static_cast<int>(
ColMajor), LeftArgType, RightArgType>
373 typedef std::conditional_t<static_cast<int>(Layout) ==
static_cast<int>(
ColMajor), RightArgType, LeftArgType>
376 typedef TensorEvaluator<EvalLeftArgType, Device> LeftEvaluatorType;
377 typedef TensorEvaluator<EvalRightArgType, Device> RightEvaluatorType;
379 static constexpr int LDims =
380 internal::array_size<typename TensorEvaluator<EvalLeftArgType, Device>::Dimensions>::value;
381 static constexpr int RDims =
382 internal::array_size<typename TensorEvaluator<EvalRightArgType, Device>::Dimensions>::value;
383 static constexpr int ContractDims = internal::array_size<Indices>::value;
384 static constexpr int NumDims = LDims + RDims - 2 * ContractDims;
386 typedef array<Index, ContractDims> contract_t;
387 typedef array<Index, LDims - ContractDims> left_nocontract_t;
388 typedef array<Index, RDims - ContractDims> right_nocontract_t;
390 typedef DSizes<Index, NumDims> Dimensions;
392 EIGEN_STRONG_INLINE TensorContractionEvaluatorBase(
const XprType& op,
const Device& device)
393 : m_leftImpl(choose(Cond<static_cast<int>(Layout) == static_cast<int>(
ColMajor)>(), op.lhsExpression(),
396 m_rightImpl(choose(Cond<static_cast<int>(Layout) == static_cast<int>(
ColMajor)>(), op.rhsExpression(),
400 m_output_kernel(op.outputKernel()),
402 EIGEN_STATIC_ASSERT((
static_cast<int>(TensorEvaluator<LeftArgType, Device>::Layout) ==
403 static_cast<int>(TensorEvaluator<RightArgType, Device>::Layout)),
404 YOU_MADE_A_PROGRAMMING_MISTAKE);
406 DSizes<Index, LDims> eval_left_dims;
407 DSizes<Index, RDims> eval_right_dims;
408 array<IndexPair<Index>, ContractDims> eval_op_indices;
409 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
411 for (
int i = 0; i < LDims; i++) {
412 eval_left_dims[i] = m_leftImpl.dimensions()[i];
414 for (
int i = 0; i < RDims; i++) {
415 eval_right_dims[i] = m_rightImpl.dimensions()[i];
418 for (
int i = 0; i < ContractDims; i++) {
419 eval_op_indices[i].first = op.indices()[i].first;
420 eval_op_indices[i].second = op.indices()[i].second;
424 for (
int i = 0; i < LDims; i++) {
425 eval_left_dims[i] = m_leftImpl.dimensions()[LDims - i - 1];
427 for (
int i = 0; i < RDims; i++) {
428 eval_right_dims[i] = m_rightImpl.dimensions()[RDims - i - 1];
432 for (
int i = 0; i < ContractDims; i++) {
433 eval_op_indices[i].first = LDims - 1 - op.indices()[ContractDims - 1 - i].second;
434 eval_op_indices[i].second = RDims - 1 - op.indices()[ContractDims - 1 - i].first;
440 for (
int i = 0; i < ContractDims; i++) {
441 for (
int j = i + 1; j < ContractDims; j++) {
442 eigen_assert(eval_op_indices[j].first != eval_op_indices[i].first &&
443 eval_op_indices[j].second != eval_op_indices[i].second &&
"contraction axes should be unique");
444 if (eval_op_indices[j].first < eval_op_indices[i].first) {
445 numext::swap(eval_op_indices[j], eval_op_indices[i]);
450 array<Index, LDims> lhs_strides;
452 for (
int i = 0; i < LDims - 1; ++i) {
453 lhs_strides[i + 1] = lhs_strides[i] * eval_left_dims[i];
456 array<Index, RDims> rhs_strides;
458 for (
int i = 0; i < RDims - 1; ++i) {
459 rhs_strides[i + 1] = rhs_strides[i] * eval_right_dims[i];
462 if (m_i_strides.size() > 0) m_i_strides[0] = 1;
463 if (m_j_strides.size() > 0) m_j_strides[0] = 1;
464 if (m_k_strides.size() > 0) m_k_strides[0] = 1;
474 m_lhs_inner_dim_contiguous =
true;
476 Index nocontract_idx = 0;
478 for (
int i = 0; i < LDims; i++) {
480 bool contracting =
false;
481 for (
int j = 0; j < ContractDims; j++) {
482 if (eval_op_indices[j].first == i) {
489 m_dimensions[dim_idx] = eval_left_dims[i];
490 m_left_nocontract_strides[nocontract_idx] = lhs_strides[i];
492 m_lhs_inner_dim_contiguous =
false;
494 if (nocontract_idx + 1 < internal::array_size<left_nocontract_t>::value) {
495 m_i_strides[nocontract_idx + 1] = m_i_strides[nocontract_idx] * eval_left_dims[i];
497 m_i_size = m_i_strides[nocontract_idx] * eval_left_dims[i];
505 for (
int i = 0; i < RDims; i++) {
506 bool contracting =
false;
508 for (
int j = 0; j < ContractDims; j++) {
509 if (eval_op_indices[j].second == i) {
515 m_dimensions[dim_idx] = eval_right_dims[i];
516 if (nocontract_idx + 1 < internal::array_size<right_nocontract_t>::value) {
517 m_j_strides[nocontract_idx + 1] = m_j_strides[nocontract_idx] * eval_right_dims[i];
519 m_j_size = m_j_strides[nocontract_idx] * eval_right_dims[i];
521 m_right_nocontract_strides[nocontract_idx] = rhs_strides[i];
532 m_rhs_inner_dim_contiguous =
true;
533 m_rhs_inner_dim_reordered =
false;
534 for (
int i = 0; i < ContractDims; i++) {
535 Index left = eval_op_indices[i].first;
536 Index right = eval_op_indices[i].second;
538 Index size = eval_left_dims[left];
539 eigen_assert(size == eval_right_dims[right] &&
"Contraction axes must be same size");
541 if (i + 1 <
static_cast<int>(internal::array_size<contract_t>::value)) {
542 m_k_strides[i + 1] = m_k_strides[i] * size;
544 m_k_size = m_k_strides[i] * size;
546 m_left_contracting_strides[i] = lhs_strides[left];
547 m_right_contracting_strides[i] = rhs_strides[right];
549 if (i > 0 && right < eval_op_indices[i - 1].second) {
550 m_rhs_inner_dim_reordered =
true;
553 m_rhs_inner_dim_contiguous =
false;
558 if (
static_cast<int>(Layout) ==
static_cast<int>(
RowMajor)) {
559 for (
int i = 0, j = NumDims - 1; i < j; i++, j--) {
560 numext::swap(m_dimensions[i], m_dimensions[j]);
568 m_tensor_contraction_params.swapped_arguments =
static_cast<int>(Layout) ==
RowMajor;
571 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
573 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
574 m_leftImpl.evalSubExprsIfNeeded(NULL);
575 m_rightImpl.evalSubExprsIfNeeded(NULL);
580 m_result =
static_cast<EvaluatorPointerType
>(m_device.allocate(dimensions().TotalSize() *
sizeof(Scalar)));
586#ifdef EIGEN_USE_THREADS
587 template <
typename EvalSubExprsCallback>
588 EIGEN_STRONG_INLINE
void evalSubExprsIfNeededAsync(EvaluatorPointerType dest, EvalSubExprsCallback done) {
589 m_leftImpl.evalSubExprsIfNeededAsync(
nullptr, [
this, done, dest](
bool) {
590 m_rightImpl.evalSubExprsIfNeededAsync(
nullptr, [
this, done, dest](
bool) {
592 evalToAsync(dest, [done]() { done(
false); });
594 m_result =
static_cast<EvaluatorPointerType
>(m_device.allocate(dimensions().TotalSize() *
sizeof(Scalar)));
595 evalToAsync(m_result, [done]() { done(
true); });
602#ifndef TENSOR_CONTRACTION_DISPATCH
603#define TENSOR_CONTRACTION_DISPATCH(METHOD, ALIGNMENT, ARGS) \
604 if (this->m_lhs_inner_dim_contiguous) { \
605 if (this->m_rhs_inner_dim_contiguous) { \
606 if (this->m_rhs_inner_dim_reordered) { \
607 METHOD<true, true, true, ALIGNMENT> ARGS; \
609 METHOD<true, true, false, ALIGNMENT> ARGS; \
612 if (this->m_rhs_inner_dim_reordered) { \
613 METHOD<true, false, true, ALIGNMENT> ARGS; \
615 METHOD<true, false, false, ALIGNMENT> ARGS; \
619 if (this->m_rhs_inner_dim_contiguous) { \
620 if (this->m_rhs_inner_dim_reordered) { \
621 METHOD<false, true, true, ALIGNMENT> ARGS; \
623 METHOD<false, true, false, ALIGNMENT> ARGS; \
626 if (this->m_rhs_inner_dim_reordered) { \
627 METHOD<false, false, true, ALIGNMENT> ARGS; \
629 METHOD<false, false, false, ALIGNMENT> ARGS; \
635#ifndef TENSOR_CONTRACTION_ASYNC_DISPATCH
636#define TENSOR_CONTRACTION_ASYNC_DISPATCH(METHOD, DONE, ALIGNMENT, ARGS, FN) \
637 if (this->m_lhs_inner_dim_contiguous) { \
638 if (this->m_rhs_inner_dim_contiguous) { \
639 if (this->m_rhs_inner_dim_reordered) { \
640 (new METHOD<DONE, true, true, true, ALIGNMENT> ARGS)->FN; \
642 (new METHOD<DONE, true, true, false, ALIGNMENT> ARGS)->FN; \
645 if (this->m_rhs_inner_dim_reordered) { \
646 (new METHOD<DONE, true, false, true, ALIGNMENT> ARGS)->FN; \
648 (new METHOD<DONE, true, false, false, ALIGNMENT> ARGS)->FN; \
652 if (this->m_rhs_inner_dim_contiguous) { \
653 if (this->m_rhs_inner_dim_reordered) { \
654 (new METHOD<DONE, false, true, true, ALIGNMENT> ARGS)->FN; \
656 (new METHOD<DONE, false, true, false, ALIGNMENT> ARGS)->FN; \
659 if (this->m_rhs_inner_dim_reordered) { \
660 (new METHOD<DONE, false, false, true, ALIGNMENT> ARGS)->FN; \
662 (new METHOD<DONE, false, false, false, ALIGNMENT> ARGS)->FN; \
668 EIGEN_DEVICE_FUNC
void evalTo(Scalar* buffer)
const {
669 static_cast<const Derived*
>(
this)->
template evalProduct<Unaligned>(buffer);
672#ifdef EIGEN_USE_THREADS
673 template <
typename EvalToCallback>
674 void evalToAsync(Scalar* buffer, EvalToCallback done)
const {
675 static_cast<const Derived*
>(
this)->
template evalProductAsync<EvalToCallback, Unaligned>(buffer, std::move(done));
679 template <
bool lhs_inner_dim_contiguous,
bool rhs_inner_dim_contiguous,
bool rhs_inner_dim_reordered,
int Alignment>
680 void evalProductSequential(Scalar* buffer)
const {
681 if (this->m_j_size == 1) {
682 this->
template evalGemv<lhs_inner_dim_contiguous, rhs_inner_dim_contiguous, rhs_inner_dim_reordered, Alignment>(
685 this->
template evalGemm<lhs_inner_dim_contiguous, rhs_inner_dim_contiguous, rhs_inner_dim_reordered, Alignment>(
690 template <
bool lhs_inner_dim_contiguous,
bool rhs_inner_dim_contiguous,
bool rhs_inner_dim_reordered,
int Alignment>
691#if !defined(EIGEN_HIPCC)
695 evalGemv(Scalar* buffer)
const {
696 const Index rows = m_i_size;
697 const Index cols = m_k_size;
699 typedef std::remove_const_t<typename EvalLeftArgType::Scalar> LhsScalar;
700 typedef std::remove_const_t<typename EvalRightArgType::Scalar> RhsScalar;
701 typedef TensorEvaluator<EvalLeftArgType, Device> LeftEvaluator;
702 typedef TensorEvaluator<EvalRightArgType, Device> RightEvaluator;
703 const int lhs_packet_size = internal::unpacket_traits<typename LeftEvaluator::PacketReturnType>::size;
704 const int rhs_packet_size = internal::unpacket_traits<typename RightEvaluator::PacketReturnType>::size;
707 typedef internal::TensorContractionInputMapper<LhsScalar, Index, internal::Lhs, LeftEvaluator, left_nocontract_t,
708 contract_t, lhs_packet_size, lhs_inner_dim_contiguous,
false,
712 typedef internal::TensorContractionInputMapper<RhsScalar, Index, internal::Rhs, RightEvaluator, right_nocontract_t,
713 contract_t, rhs_packet_size, rhs_inner_dim_contiguous,
714 rhs_inner_dim_reordered, rhs_alignment>
717 LhsMapper lhs(m_leftImpl, m_left_nocontract_strides, m_i_strides, m_left_contracting_strides, m_k_strides);
718 RhsMapper rhs(m_rightImpl, m_right_nocontract_strides, m_j_strides, m_right_contracting_strides, m_k_strides);
720 const Scalar alpha(1);
721 const Index resIncr(1);
724 m_device.fill(buffer, buffer + rows, Scalar(0));
726 internal::general_matrix_vector_product<Index, LhsScalar, LhsMapper,
ColMajor,
false, RhsScalar, RhsMapper,
727 false>::run(rows, cols, lhs, rhs, buffer, resIncr, alpha);
729 typedef internal::blas_data_mapper<Scalar, Index, ColMajor> OutputMapper;
730 m_output_kernel(OutputMapper(buffer, rows), m_tensor_contraction_params,
static_cast<Index
>(0),
731 static_cast<Index
>(0), rows,
static_cast<Index
>(1));
734 template <
bool lhs_inner_dim_contiguous,
bool rhs_inner_dim_contiguous,
bool rhs_inner_dim_reordered,
int Alignment>
735#if !defined(EIGEN_HIPCC)
739 evalGemm(Scalar* buffer)
const {
741 const Index k = this->m_k_size;
742 this->
template evalGemmPartial<lhs_inner_dim_contiguous, rhs_inner_dim_contiguous, rhs_inner_dim_reordered,
743 Alignment,
true>(buffer, 0, k, 1);
746 template <
bool lhs_inner_dim_contiguous,
bool rhs_inner_dim_contiguous,
bool rhs_inner_dim_reordered,
int Alignment>
747 EIGEN_DEVICE_FUNC
void evalGemmPartialWithoutOutputKernel(Scalar* buffer, Index k_start, Index k_end,
748 int num_threads)
const {
749 evalGemmPartial<lhs_inner_dim_contiguous, rhs_inner_dim_contiguous, rhs_inner_dim_reordered, Alignment,
750 false>(buffer, k_start, k_end, num_threads);
753 template <
bool lhs_inner_dim_contiguous,
bool rhs_inner_dim_contiguous,
bool rhs_inner_dim_reordered,
int Alignment,
754 bool use_output_kernel>
755 EIGEN_DEVICE_FUNC
void evalGemmPartial(Scalar* buffer, Index k_start, Index k_end,
int num_threads)
const {
756 eigen_assert(k_end >= k_start && k_start >= 0 && k_end <= this->m_k_size);
758 const Index k_slice = k_end - k_start;
761 const Index m = this->m_i_size;
764 const Index n = this->m_j_size;
767 typedef std::remove_const_t<typename EvalLeftArgType::Scalar> LhsScalar;
768 typedef std::remove_const_t<typename EvalRightArgType::Scalar> RhsScalar;
770 typedef TensorEvaluator<EvalLeftArgType, Device> LeftEvaluator;
771 typedef TensorEvaluator<EvalRightArgType, Device> RightEvaluator;
773 const int lhs_packet_size = internal::unpacket_traits<typename LeftEvaluator::PacketReturnType>::size;
774 const int rhs_packet_size = internal::unpacket_traits<typename RightEvaluator::PacketReturnType>::size;
776 typedef internal::TensorContractionInputMapper<LhsScalar, Index, internal::Lhs, LeftEvaluator, left_nocontract_t,
777 contract_t, lhs_packet_size, lhs_inner_dim_contiguous,
false,
781 typedef internal::TensorContractionInputMapper<RhsScalar, Index, internal::Rhs, RightEvaluator, right_nocontract_t,
782 contract_t, rhs_packet_size, rhs_inner_dim_contiguous,
786 typedef internal::blas_data_mapper<Scalar, Index, ColMajor> OutputMapper;
788 typedef internal::TensorContractionKernel<Scalar, LhsScalar, RhsScalar, Index, OutputMapper, LhsMapper, RhsMapper>
789 TensorContractionKernel;
792 LhsMapper lhs(this->m_leftImpl, this->m_left_nocontract_strides, this->m_i_strides,
793 this->m_left_contracting_strides, this->m_k_strides);
795 RhsMapper rhs(this->m_rightImpl, this->m_right_nocontract_strides, this->m_j_strides,
796 this->m_right_contracting_strides, this->m_k_strides);
798 OutputMapper output(buffer, m);
801 internal::TensorContractionBlocking<Scalar, LhsScalar, RhsScalar, Index, internal::ShardByCol> blocking(
802 k_slice, m, n, num_threads);
803 const Index kc = blocking.kc();
804 const Index mc = numext::mini(m, blocking.mc());
805 const Index nc = numext::mini(n, blocking.nc());
807 typedef typename TensorContractionKernel::LhsBlock LhsBlock;
808 typedef typename TensorContractionKernel::RhsBlock RhsBlock;
813 TensorContractionKernel kernel(m, k_slice, n, mc, kc, nc);
815 typedef typename TensorContractionKernel::BlockMemHandle BlockMemHandle;
816 const BlockMemHandle packed_mem = kernel.allocate(this->m_device, &blockA, &blockB);
820 if (!TensorContractionKernel::HasBeta) {
821 this->m_device.fill(buffer, buffer + m * n, Scalar(0));
824 for (Index i2 = 0; i2 < m; i2 += mc) {
825 const Index actual_mc = numext::mini(i2 + mc, m) - i2;
826 for (Index k2 = k_start; k2 < k_end; k2 += kc) {
828 const Index actual_kc = numext::mini(k2 + kc, k_end) - k2;
829 kernel.packLhs(&blockA, lhs.getSubMapper(i2, k2), actual_kc, actual_mc);
833 const Scalar alpha = Scalar(1);
834 const Scalar beta = (TensorContractionKernel::HasBeta && k2 == k_start) ? Scalar(0) : Scalar(1);
837 for (Index j2 = 0; j2 < n; j2 += nc) {
839 const Index actual_nc = numext::mini(j2 + nc, n) - j2;
840 kernel.packRhs(&blockB, rhs.getSubMapper(k2, j2), actual_kc, actual_nc);
844 const OutputMapper output_mapper = output.getSubMapper(i2, j2);
845 kernel.invoke(output_mapper, blockA, blockB, actual_mc, actual_kc, actual_nc, alpha, beta);
848 if (use_output_kernel && k2 + kc >= k_end) {
849 m_output_kernel(output_mapper, m_tensor_contraction_params, i2, j2, actual_mc, actual_nc);
855 kernel.deallocate(this->m_device, packed_mem);
858 EIGEN_STRONG_INLINE
void cleanup() {
859 m_leftImpl.cleanup();
860 m_rightImpl.cleanup();
862 if (m_result != NULL) {
863 m_device.deallocate(m_result);
868 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
return m_result[index]; }
870 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool)
const {
871 return TensorOpCost(
sizeof(CoeffReturnType), 0, 0);
874 template <
int LoadMode>
875 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const {
876 return internal::ploadt<PacketReturnType, LoadMode>(m_result + index);
879 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EvaluatorPointerType data()
const {
return m_result; }
882 Dimensions m_dimensions;
884 contract_t m_k_strides;
885 contract_t m_left_contracting_strides;
886 contract_t m_right_contracting_strides;
888 bool m_lhs_inner_dim_contiguous;
889 bool m_rhs_inner_dim_contiguous;
890 bool m_rhs_inner_dim_reordered;
892 left_nocontract_t m_i_strides;
893 right_nocontract_t m_j_strides;
894 left_nocontract_t m_left_nocontract_strides;
895 right_nocontract_t m_right_nocontract_strides;
901 TensorContractionParams m_tensor_contraction_params;
903 TensorEvaluator<EvalLeftArgType, Device> m_leftImpl;
904 TensorEvaluator<EvalRightArgType, Device> m_rightImpl;
905 const Device EIGEN_DEVICE_REF m_device;
906 OutputKernelType m_output_kernel;
907 EvaluatorPointerType m_result;
911template <
typename Indices,
typename LeftArgType,
typename RightArgType,
typename OutputKernelType,
typename Device>
913 :
public TensorContractionEvaluatorBase<
914 TensorEvaluator<const TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType>, Device>> {
915 typedef TensorEvaluator<const TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType>, Device> Self;
916 typedef TensorContractionEvaluatorBase<Self> Base;
918 typedef TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType> XprType;
919 typedef std::remove_const_t<typename XprType::Scalar> Scalar;
920 typedef typename XprType::Index Index;
921 typedef typename XprType::CoeffReturnType CoeffReturnType;
922 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
924 static constexpr int Layout = TensorEvaluator<LeftArgType, Device>::Layout;
930 typedef std::conditional_t<Layout == static_cast<int>(
ColMajor), LeftArgType, RightArgType> EvalLeftArgType;
931 typedef std::conditional_t<Layout == static_cast<int>(
ColMajor), RightArgType, LeftArgType> EvalRightArgType;
933 static constexpr int LDims =
934 internal::array_size<typename TensorEvaluator<EvalLeftArgType, Device>::Dimensions>::value;
935 static constexpr int RDims =
936 internal::array_size<typename TensorEvaluator<EvalRightArgType, Device>::Dimensions>::value;
937 static constexpr int ContractDims = internal::array_size<Indices>::value;
939 typedef array<Index, ContractDims> contract_t;
940 typedef array<Index, LDims - ContractDims> left_nocontract_t;
941 typedef array<Index, RDims - ContractDims> right_nocontract_t;
943 static constexpr int NumDims = LDims + RDims - 2 * ContractDims;
946 typedef DSizes<Index, NumDims> Dimensions;
948 TensorEvaluator(
const XprType& op,
const Device& device) : Base(op, device) {}
950 template <
int Alignment>
951 void evalProduct(Scalar* buffer)
const {
952 TENSOR_CONTRACTION_DISPATCH(this->
template evalProductSequential, Alignment, (buffer));
The tensor base class.
Definition TensorForwardDeclarations.h:68
Definition TensorContraction.h:303
const internal::remove_all_t< typename LhsXprType::Nested > & lhsExpression() const
Definition TensorContraction.h:320
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:30