2014-06-04 09:21:48 -07:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
|
|
#ifndef EIGEN_CXX11_TENSOR_TENSOR_CONTRACTION_H
|
|
|
|
|
#define EIGEN_CXX11_TENSOR_TENSOR_CONTRACTION_H
|
|
|
|
|
|
2023-08-21 16:25:22 +00:00
|
|
|
// IWYU pragma: private
|
2021-09-10 19:12:26 +00:00
|
|
|
#include "./InternalHeaderCheck.h"
|
|
|
|
|
|
2014-06-04 09:21:48 -07:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
|
|
|
|
namespace internal {
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2018-07-10 13:16:38 -07:00
|
|
|
template <typename Dimensions, typename LhsXprType, typename RhsXprType, typename OutputKernelType>
|
|
|
|
|
struct traits<TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType>> {
|
2014-06-04 09:21:48 -07:00
|
|
|
// Type promotion to handle the case where the types of the lhs and the rhs are different.
|
2026-03-04 08:59:22 -08:00
|
|
|
using Scalar = typename gebp_traits<std::remove_const_t<typename LhsXprType::Scalar>,
|
|
|
|
|
std::remove_const_t<typename RhsXprType::Scalar>>::ResScalar;
|
|
|
|
|
|
|
|
|
|
using StorageKind = typename promote_storage_type<typename traits<LhsXprType>::StorageKind,
|
|
|
|
|
typename traits<RhsXprType>::StorageKind>::ret;
|
|
|
|
|
using Index =
|
|
|
|
|
typename promote_index_type<typename traits<LhsXprType>::Index, typename traits<RhsXprType>::Index>::type;
|
|
|
|
|
using LhsNested = typename LhsXprType::Nested;
|
|
|
|
|
using RhsNested = typename RhsXprType::Nested;
|
|
|
|
|
using LhsNested_ = std::remove_reference_t<LhsNested>;
|
|
|
|
|
using RhsNested_ = std::remove_reference_t<RhsNested>;
|
2014-06-09 09:45:30 -07:00
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
// From NumDims below.
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int NumDimensions =
|
|
|
|
|
traits<LhsXprType>::NumDimensions + traits<RhsXprType>::NumDimensions - 2 * array_size<Dimensions>::value;
|
|
|
|
|
static constexpr int Layout = traits<LhsXprType>::Layout;
|
2026-03-04 08:59:22 -08:00
|
|
|
using PointerType =
|
|
|
|
|
std::conditional_t<Pointer_type_promotion<typename LhsXprType::Scalar, Scalar>::val,
|
|
|
|
|
typename traits<LhsXprType>::PointerType, typename traits<RhsXprType>::PointerType>;
|
2015-01-14 12:36:57 -08:00
|
|
|
|
2016-04-29 18:36:10 -07:00
|
|
|
enum { Flags = 0 };
|
2014-06-04 09:21:48 -07:00
|
|
|
};
|
|
|
|
|
|
2018-07-10 13:16:38 -07:00
|
|
|
template <typename Dimensions, typename LhsXprType, typename RhsXprType, typename OutputKernelType>
|
|
|
|
|
struct eval<TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType>, Eigen::Dense> {
|
2026-03-04 08:59:22 -08:00
|
|
|
using type = const TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType>&;
|
2014-06-04 09:21:48 -07:00
|
|
|
};
|
|
|
|
|
|
2018-07-10 13:16:38 -07:00
|
|
|
template <typename Dimensions, typename LhsXprType, typename RhsXprType, typename OutputKernelType>
|
|
|
|
|
struct nested<TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType>, 1,
|
|
|
|
|
typename eval<TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType>>::type> {
|
2026-03-04 08:59:22 -08:00
|
|
|
using type = TensorContractionOp<Dimensions, LhsXprType, RhsXprType, OutputKernelType>;
|
2014-06-04 09:21:48 -07:00
|
|
|
};
|
|
|
|
|
|
2018-07-10 13:16:38 -07:00
|
|
|
template <typename Indices_, typename LeftArgType_, typename RightArgType_, typename OutputKernelType_,
|
|
|
|
|
typename Device_>
|
|
|
|
|
struct traits<
|
|
|
|
|
TensorEvaluator<const TensorContractionOp<Indices_, LeftArgType_, RightArgType_, OutputKernelType_>, Device_>> {
|
2026-03-04 08:59:22 -08:00
|
|
|
using Indices = Indices_;
|
|
|
|
|
using LeftArgType = LeftArgType_;
|
|
|
|
|
using RightArgType = RightArgType_;
|
|
|
|
|
using OutputKernelType = OutputKernelType_;
|
|
|
|
|
using Device = Device_;
|
2015-01-14 12:36:57 -08:00
|
|
|
|
|
|
|
|
// From NumDims below.
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int NumDimensions =
|
|
|
|
|
traits<LeftArgType_>::NumDimensions + traits<RightArgType_>::NumDimensions - 2 * array_size<Indices_>::value;
|
2014-10-03 10:16:59 -07:00
|
|
|
};
|
|
|
|
|
|
2019-04-01 11:47:31 -07:00
|
|
|
// Helper class to allocate and deallocate temporary memory for packed buffers.
|
|
|
|
|
template <typename LhsScalar, typename RhsScalar>
|
|
|
|
|
struct TensorContractionBlockMemAllocator {
|
2026-03-04 08:59:22 -08:00
|
|
|
using BlockMemHandle = void*;
|
2019-04-01 11:47:31 -07:00
|
|
|
|
|
|
|
|
template <typename Device>
|
|
|
|
|
EIGEN_DEVICE_FUNC static BlockMemHandle allocate(Device& d, const Index bm, const Index bk, const Index bn,
|
|
|
|
|
LhsScalar** lhs_block, RhsScalar** rhs_block) {
|
|
|
|
|
eigen_assert(lhs_block);
|
|
|
|
|
eigen_assert(rhs_block);
|
|
|
|
|
BlockSizes sz = ComputeLhsRhsBlockSizes(bm, bk, bn);
|
|
|
|
|
char* block_mem = static_cast<char*>(d.allocate(sz.lhs_size + sz.rhs_size));
|
2023-07-17 20:37:27 +00:00
|
|
|
*lhs_block = static_cast<LhsScalar*>(static_cast<void*>(block_mem));
|
|
|
|
|
*rhs_block = static_cast<RhsScalar*>(static_cast<void*>(block_mem + sz.lhs_size));
|
2019-04-01 11:47:31 -07:00
|
|
|
return block_mem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Device>
|
|
|
|
|
EIGEN_DEVICE_FUNC static BlockMemHandle allocateSlices(Device& d, const Index bm, const Index bk, const Index bn,
|
|
|
|
|
const Index num_lhs, const Index num_rhs,
|
|
|
|
|
const Index num_slices, std::vector<LhsScalar*>* lhs_blocks,
|
|
|
|
|
std::vector<RhsScalar*>* rhs_blocks) {
|
|
|
|
|
eigen_assert(num_slices > 0);
|
2019-04-02 15:04:26 -07:00
|
|
|
eigen_assert(num_lhs >= 0 && num_rhs >= 0);
|
2019-04-01 11:47:31 -07:00
|
|
|
eigen_assert(num_lhs == 0 || lhs_blocks);
|
|
|
|
|
eigen_assert(num_rhs == 0 || rhs_blocks);
|
|
|
|
|
BlockSizes sz = ComputeLhsRhsBlockSizes(bm, bk, bn);
|
|
|
|
|
void* block_mem = d.allocate((num_lhs * sz.lhs_size + num_rhs * sz.rhs_size) * num_slices);
|
|
|
|
|
eigen_assert(block_mem);
|
|
|
|
|
char* mem = static_cast<char*>(block_mem);
|
|
|
|
|
|
|
|
|
|
for (Index x = 0; x < num_slices; x++) {
|
|
|
|
|
if (num_lhs > 0) lhs_blocks[x].resize(num_lhs);
|
|
|
|
|
for (Index m = 0; m < num_lhs; m++) {
|
2023-07-17 20:37:27 +00:00
|
|
|
lhs_blocks[x][m] = static_cast<LhsScalar*>(static_cast<void*>(mem));
|
2019-04-01 11:47:31 -07:00
|
|
|
mem += sz.lhs_size;
|
|
|
|
|
}
|
|
|
|
|
if (num_rhs > 0) rhs_blocks[x].resize(num_rhs);
|
|
|
|
|
for (Index n = 0; n < num_rhs; n++) {
|
2023-07-17 20:37:27 +00:00
|
|
|
rhs_blocks[x][n] = static_cast<RhsScalar*>(static_cast<void*>(mem));
|
2019-04-01 11:47:31 -07:00
|
|
|
mem += sz.rhs_size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return block_mem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Device>
|
|
|
|
|
EIGEN_DEVICE_FUNC static void deallocate(Device& d, BlockMemHandle handle) {
|
|
|
|
|
d.deallocate(handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
struct BlockSizes {
|
|
|
|
|
Index lhs_size;
|
|
|
|
|
Index rhs_size;
|
|
|
|
|
};
|
|
|
|
|
EIGEN_DEVICE_FUNC static BlockSizes ComputeLhsRhsBlockSizes(const Index bm, const Index bk, const Index bn) {
|
|
|
|
|
Index align = numext::maxi(EIGEN_MAX_ALIGN_BYTES, 1);
|
|
|
|
|
BlockSizes sz;
|
2023-10-10 17:16:59 +00:00
|
|
|
sz.lhs_size = numext::div_ceil<Index>(bm * bk * sizeof(LhsScalar), align) * align;
|
|
|
|
|
sz.rhs_size = numext::div_ceil<Index>(bn * bk * sizeof(RhsScalar), align) * align;
|
2019-04-01 11:47:31 -07:00
|
|
|
return sz;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-27 11:49:19 -07:00
|
|
|
// WARNING: In this code we assume that Lhs and Rhs tensor expressions are in
|
|
|
|
|
// ColMajor storage order. This property is guaranteed by the
|
|
|
|
|
// TensorContractionOp evaluator. TensorContractionKernel specifies how we pack
|
|
|
|
|
// blocks of Lhs and Rhs tensor expressions, and how we invoke matrix
|
|
|
|
|
// multiplication for these blocks. Default tensor contraction uses
|
|
|
|
|
// gemm_pack_rhs, gemm_pack_lhs and gebp_kernel from Eigen Core (see
|
2026-02-22 22:04:23 -08:00
|
|
|
// GeneralBlockPanelKernel.h for details).
|
2018-09-27 11:49:19 -07:00
|
|
|
//
|
|
|
|
|
// By specializing contraction kernels we can use other low level libraries to
|
|
|
|
|
// perform matrix multiplication, and still rely on Eigen contraction evaluator.
|
|
|
|
|
// This also includes full support in TensorContractionThreadPool, assuming that
|
2026-02-22 22:04:23 -08:00
|
|
|
// underlying gemm does not use its own threading.
|
2018-09-27 11:49:19 -07:00
|
|
|
//
|
|
|
|
|
// - ResScalar/LhsScalar/RhsScalar - scalar type for the result of
|
|
|
|
|
// multiplication, lhs tensor and rhs tensor respectively.
|
|
|
|
|
//
|
|
|
|
|
// - StorageIndex - index type for the tensor expressions. In practice almost
|
|
|
|
|
// always is Eigen::Index.
|
|
|
|
|
//
|
|
|
|
|
// - OutputMapper provides access to the memory of the output matrix. In
|
|
|
|
|
// practice it's always column major blas_data_mapper (it must be of ResScalar
|
|
|
|
|
// type).
|
|
|
|
|
//
|
|
|
|
|
// - LhsMapper/RhsMapper similarly to blas_data_mapper provide a two dimensional
|
|
|
|
|
// view into the Lhs/Rhs tensor expressions. In practice it's
|
|
|
|
|
// TensorContractionInputMapper, or some specialization of it based on the
|
|
|
|
|
// type of tensor expression (e.g. TensorImagePatchOp has optimized input
|
|
|
|
|
// mapper).
|
2019-04-01 11:47:31 -07:00
|
|
|
template <typename ResScalar, typename LhsScalar, typename RhsScalar, typename StorageIndex, typename OutputMapper,
|
2018-09-27 11:49:19 -07:00
|
|
|
typename LhsMapper, typename RhsMapper>
|
|
|
|
|
struct TensorContractionKernel {
|
2019-10-02 11:06:02 -07:00
|
|
|
// True if `invoke()` supports `beta` in `C <- alpha * A * B + beta * C`
|
|
|
|
|
// (otherwise beta should be always equal to 1).
|
|
|
|
|
enum { HasBeta = false };
|
|
|
|
|
|
2019-05-07 18:32:19 +02:00
|
|
|
EIGEN_DEVICE_FUNC TensorContractionKernel(StorageIndex m_, StorageIndex k_, StorageIndex n_, StorageIndex bm_,
|
|
|
|
|
StorageIndex bk_, StorageIndex bn_)
|
|
|
|
|
: m(m_), k(k_), n(n_), bm(bm_), bk(bk_), bn(bn_) {}
|
2019-04-01 11:47:31 -07:00
|
|
|
|
|
|
|
|
// Pack blocks of Lhs and Rhs into contiguous blocks in memory.
|
2026-03-04 08:59:22 -08:00
|
|
|
using LhsBlock = LhsScalar*;
|
|
|
|
|
using RhsBlock = RhsScalar*;
|
2019-04-01 11:47:31 -07:00
|
|
|
|
|
|
|
|
// Packed Lhs/Rhs block memory allocator.
|
2026-03-04 08:59:22 -08:00
|
|
|
using BlockMemAllocator = TensorContractionBlockMemAllocator<LhsScalar, RhsScalar>;
|
|
|
|
|
using BlockMemHandle = typename BlockMemAllocator::BlockMemHandle;
|
2019-04-01 11:47:31 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using Traits = typename internal::gebp_traits<LhsScalar, RhsScalar>;
|
2018-09-27 11:49:19 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using LhsPacker = internal::gemm_pack_lhs<LhsScalar, StorageIndex, typename LhsMapper::SubMapper, Traits::mr,
|
|
|
|
|
Traits::LhsProgress, typename Traits::LhsPacket4Packing, ColMajor>;
|
2018-09-27 11:49:19 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using RhsPacker =
|
|
|
|
|
internal::gemm_pack_rhs<RhsScalar, StorageIndex, typename RhsMapper::SubMapper, Traits::nr, ColMajor>;
|
2018-09-27 11:49:19 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using GebpKernel = internal::gebp_kernel<LhsScalar, RhsScalar, StorageIndex, OutputMapper, Traits::mr, Traits::nr,
|
|
|
|
|
/*ConjugateLhs*/ false, /*ConjugateRhs*/ false>;
|
2018-09-27 11:49:19 -07:00
|
|
|
|
2019-04-01 11:47:31 -07:00
|
|
|
template <typename Device>
|
|
|
|
|
EIGEN_DEVICE_FUNC BlockMemHandle allocate(Device& d, LhsBlock* lhs_block, RhsBlock* rhs_block) {
|
|
|
|
|
return BlockMemAllocator::allocate(d, bm, bk, bn, lhs_block, rhs_block);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Device>
|
|
|
|
|
EIGEN_DEVICE_FUNC BlockMemHandle allocateSlices(Device& d, const StorageIndex num_lhs, const StorageIndex num_rhs,
|
|
|
|
|
const StorageIndex num_slices, std::vector<LhsBlock>* lhs_blocks,
|
|
|
|
|
std::vector<RhsBlock>* rhs_blocks) {
|
|
|
|
|
return BlockMemAllocator::allocateSlices(d, bm, bk, bn, num_lhs, num_rhs, num_slices, lhs_blocks, rhs_blocks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Device>
|
|
|
|
|
EIGEN_DEVICE_FUNC static void deallocate(Device& d, BlockMemHandle handle) {
|
|
|
|
|
BlockMemAllocator::deallocate(d, handle);
|
2018-09-27 11:49:19 -07:00
|
|
|
}
|
|
|
|
|
|
2019-04-01 11:47:31 -07:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE void packLhs(LhsBlock* lhsBlock, const typename LhsMapper::SubMapper& data_mapper,
|
|
|
|
|
const StorageIndex depth, const StorageIndex rows) {
|
|
|
|
|
LhsPacker()(*lhsBlock, data_mapper, depth, rows, /*stride*/ 0,
|
|
|
|
|
/*offset*/ 0);
|
2018-09-27 11:49:19 -07:00
|
|
|
}
|
|
|
|
|
|
2019-04-01 11:47:31 -07:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE void packRhs(RhsBlock* rhsBlock, const typename RhsMapper::SubMapper& data_mapper,
|
|
|
|
|
const StorageIndex depth, const StorageIndex cols) {
|
|
|
|
|
RhsPacker()(*rhsBlock, data_mapper, depth, cols);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE void invoke(const OutputMapper& output_mapper, const LhsBlock& lhsBlock,
|
|
|
|
|
const RhsBlock& rhsBlock, const StorageIndex rows,
|
|
|
|
|
const StorageIndex depth, const StorageIndex cols,
|
2019-10-02 11:06:02 -07:00
|
|
|
const ResScalar alpha, const ResScalar beta) {
|
|
|
|
|
// Default GEBP kernel does not support beta.
|
2026-02-26 07:03:38 -08:00
|
|
|
EIGEN_ONLY_USED_FOR_DEBUG(beta);
|
2019-10-02 11:06:02 -07:00
|
|
|
eigen_assert(beta == ResScalar(1));
|
2019-04-01 11:47:31 -07:00
|
|
|
static const int kComputeStrideFromBlockDimensions = -1;
|
2018-09-27 11:49:19 -07:00
|
|
|
GebpKernel()(output_mapper, lhsBlock, rhsBlock, rows, depth, cols, alpha,
|
2019-04-01 11:47:31 -07:00
|
|
|
/*strideA*/ kComputeStrideFromBlockDimensions,
|
|
|
|
|
/*strideB*/ kComputeStrideFromBlockDimensions,
|
2018-09-27 11:49:19 -07:00
|
|
|
/*offsetA*/ 0, /*offsetB*/ 0);
|
|
|
|
|
}
|
2019-04-01 11:47:31 -07:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// These are dimensions of the original Tensors, and selected block sizes. The
|
|
|
|
|
// actual block sizes passed to all function above might be smaller because of
|
|
|
|
|
// the partial blocks at the end.
|
|
|
|
|
const StorageIndex m;
|
|
|
|
|
const StorageIndex k;
|
|
|
|
|
const StorageIndex n;
|
|
|
|
|
const StorageIndex bm;
|
|
|
|
|
const StorageIndex bk;
|
|
|
|
|
const StorageIndex bn;
|
2018-09-27 11:49:19 -07:00
|
|
|
};
|
|
|
|
|
|
2014-06-04 09:21:48 -07:00
|
|
|
} // end namespace internal
|
|
|
|
|
|
2018-07-10 13:16:38 -07:00
|
|
|
// Tensor contraction params that should enable to get from output matrix
|
|
|
|
|
// 2-dimensional coordinates to the output tensor dimensions.
|
|
|
|
|
struct TensorContractionParams {
|
|
|
|
|
// TensorContraction evaluator assumes that both tensors are in ColMajor
|
|
|
|
|
// layout, if tensors are in RowMajor evaluator swap lhs with rhs.
|
|
|
|
|
bool swapped_arguments;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Output kernel allows to fuse operations into the tensor contraction.
|
|
|
|
|
//
|
|
|
|
|
// Examples:
|
|
|
|
|
// 1. Elementwise Relu transformation following Conv2D.
|
|
|
|
|
// 2. AddBias to the Conv2D output channels dimension.
|
|
|
|
|
//
|
2018-08-15 09:34:47 -07:00
|
|
|
// The NoOpOutputKernel implements an output kernel that does absolutely nothing.
|
2018-07-10 13:16:38 -07:00
|
|
|
struct NoOpOutputKernel {
|
|
|
|
|
/**
|
|
|
|
|
* Tensor contraction evaluator calls this kernel after finishing each block
|
|
|
|
|
* of output matrix. Output blocks belong to the 2-dimensional output tensor.
|
|
|
|
|
*
|
|
|
|
|
* TensorContractionParams contains contraction dimensions information
|
|
|
|
|
* required to map output 2-d space into the expected output tensor space
|
|
|
|
|
* (potentially higher dimensional).
|
|
|
|
|
*
|
|
|
|
|
* \param[in] output_mapper Access to output tensor memory
|
|
|
|
|
* \param[in] params Tensor contraction parameters
|
|
|
|
|
* \param[in] i Index of a first row available through output_mapper
|
|
|
|
|
* \param[in] j Index of a first column available through output_mapper
|
|
|
|
|
* \param[in] num_rows Number of available rows
|
|
|
|
|
* \param[in] num_cols Number of available columns
|
|
|
|
|
*/
|
|
|
|
|
template <typename Index, typename Scalar>
|
2019-04-23 17:23:19 -07:00
|
|
|
EIGEN_ALWAYS_INLINE void operator()(const internal::blas_data_mapper<Scalar, Index, ColMajor>& output_mapper,
|
|
|
|
|
const TensorContractionParams& params, Index i, Index j, Index num_rows,
|
|
|
|
|
Index num_cols) const {
|
|
|
|
|
EIGEN_UNUSED_VARIABLE(output_mapper);
|
|
|
|
|
EIGEN_UNUSED_VARIABLE(params);
|
|
|
|
|
EIGEN_UNUSED_VARIABLE(i);
|
|
|
|
|
EIGEN_UNUSED_VARIABLE(j);
|
|
|
|
|
EIGEN_UNUSED_VARIABLE(num_rows);
|
|
|
|
|
EIGEN_UNUSED_VARIABLE(num_cols);
|
|
|
|
|
}
|
2018-07-10 13:16:38 -07:00
|
|
|
};
|
|
|
|
|
|
2025-02-05 17:36:00 +00:00
|
|
|
/** Tensor contraction class.
|
|
|
|
|
* \ingroup CXX11_Tensor_Module
|
|
|
|
|
*/
|
2018-07-18 14:21:01 -07:00
|
|
|
template <typename Indices, typename LhsXprType, typename RhsXprType,
|
|
|
|
|
typename OutputKernelType = const NoOpOutputKernel>
|
2018-07-10 13:16:38 -07:00
|
|
|
class TensorContractionOp
|
|
|
|
|
: public TensorBase<TensorContractionOp<Indices, LhsXprType, RhsXprType, OutputKernelType>, ReadOnlyAccessors> {
|
2014-06-04 09:21:48 -07:00
|
|
|
public:
|
2026-03-04 08:59:22 -08:00
|
|
|
using Scalar = typename Eigen::internal::traits<TensorContractionOp>::Scalar;
|
|
|
|
|
using CoeffReturnType = typename internal::gebp_traits<typename LhsXprType::CoeffReturnType,
|
|
|
|
|
typename RhsXprType::CoeffReturnType>::ResScalar;
|
|
|
|
|
using Nested = typename Eigen::internal::nested<TensorContractionOp>::type;
|
|
|
|
|
using StorageKind = typename Eigen::internal::traits<TensorContractionOp>::StorageKind;
|
|
|
|
|
using Index = typename Eigen::internal::traits<TensorContractionOp>::Index;
|
2014-06-04 09:21:48 -07:00
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorContractionOp(const LhsXprType& lhs, const RhsXprType& rhs,
|
2018-07-10 13:16:38 -07:00
|
|
|
const Indices& dims,
|
|
|
|
|
const OutputKernelType& output_kernel = OutputKernelType())
|
|
|
|
|
: m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_indices(dims), m_output_kernel(output_kernel) {}
|
2014-06-04 09:21:48 -07:00
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
EIGEN_DEVICE_FUNC const Indices& indices() const { return m_indices; }
|
2014-06-04 09:21:48 -07:00
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
/** \returns the nested expressions */
|
2022-03-16 16:43:40 +00:00
|
|
|
EIGEN_DEVICE_FUNC const internal::remove_all_t<typename LhsXprType::Nested>& lhsExpression() const {
|
2015-01-14 12:36:57 -08:00
|
|
|
return m_lhs_xpr;
|
|
|
|
|
}
|
2014-06-04 09:21:48 -07:00
|
|
|
|
2022-03-16 16:43:40 +00:00
|
|
|
EIGEN_DEVICE_FUNC const internal::remove_all_t<typename RhsXprType::Nested>& rhsExpression() const {
|
2015-01-14 12:36:57 -08:00
|
|
|
return m_rhs_xpr;
|
|
|
|
|
}
|
2014-06-04 09:21:48 -07:00
|
|
|
|
2018-07-10 13:16:38 -07:00
|
|
|
EIGEN_DEVICE_FUNC const OutputKernelType& outputKernel() const { return m_output_kernel; }
|
|
|
|
|
|
2014-06-04 09:21:48 -07:00
|
|
|
protected:
|
|
|
|
|
typename LhsXprType::Nested m_lhs_xpr;
|
|
|
|
|
typename RhsXprType::Nested m_rhs_xpr;
|
|
|
|
|
const Indices m_indices;
|
2018-07-10 13:16:38 -07:00
|
|
|
const OutputKernelType m_output_kernel;
|
2014-06-04 09:21:48 -07:00
|
|
|
};
|
|
|
|
|
|
2014-10-03 10:16:59 -07:00
|
|
|
template <typename Derived>
|
2023-01-16 07:04:08 +00:00
|
|
|
struct TensorContractionEvaluatorBase {
|
2026-03-04 08:59:22 -08:00
|
|
|
using Indices = typename internal::traits<Derived>::Indices;
|
|
|
|
|
using LeftArgType = typename internal::traits<Derived>::LeftArgType;
|
|
|
|
|
using RightArgType = typename internal::traits<Derived>::RightArgType;
|
|
|
|
|
using OutputKernelType = typename internal::traits<Derived>::OutputKernelType;
|
|
|
|
|
using Device = typename internal::traits<Derived>::Device;
|
|
|
|
|
|
|
|
|
|
using XprType = TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType>;
|
|
|
|
|
using Scalar = std::remove_const_t<typename XprType::Scalar>;
|
|
|
|
|
using Index = typename XprType::Index;
|
|
|
|
|
using CoeffReturnType = typename XprType::CoeffReturnType;
|
|
|
|
|
using PacketReturnType = typename PacketType<CoeffReturnType, Device>::type;
|
|
|
|
|
using Storage = StorageMemory<Scalar, Device>;
|
|
|
|
|
using EvaluatorPointerType = typename Storage::Type;
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2022-03-16 16:43:40 +00:00
|
|
|
static constexpr int Layout = TensorEvaluator<LeftArgType, Device>::Layout;
|
2014-06-04 09:21:48 -07:00
|
|
|
enum {
|
2019-08-30 15:13:38 -07:00
|
|
|
IsAligned = true,
|
|
|
|
|
PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
|
2019-12-10 15:40:23 -08:00
|
|
|
BlockAccess = false,
|
2018-08-10 16:53:36 -07:00
|
|
|
PreferBlockAccess = false,
|
2019-08-30 15:13:38 -07:00
|
|
|
CoordAccess = false, // to be implemented
|
|
|
|
|
RawAccess = true
|
2014-06-04 09:21:48 -07:00
|
|
|
};
|
|
|
|
|
|
2019-09-24 12:52:45 -07:00
|
|
|
//===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
|
2026-03-04 08:59:22 -08:00
|
|
|
using TensorBlock = internal::TensorBlockNotImplemented;
|
2019-09-24 12:52:45 -07:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
// Most of the code is assuming that both input tensors are ColMajor. If the
|
|
|
|
|
// inputs are RowMajor, we will "cheat" by swapping the LHS and RHS:
|
|
|
|
|
// If we want to compute A * B = C, where A is LHS and B is RHS, the code
|
|
|
|
|
// will pretend B is LHS and A is RHS.
|
2026-03-04 08:59:22 -08:00
|
|
|
using EvalLeftArgType =
|
|
|
|
|
std::conditional_t<static_cast<int>(Layout) == static_cast<int>(ColMajor), LeftArgType, RightArgType>;
|
|
|
|
|
using EvalRightArgType =
|
|
|
|
|
std::conditional_t<static_cast<int>(Layout) == static_cast<int>(ColMajor), RightArgType, LeftArgType>;
|
2019-08-30 15:13:38 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using LeftEvaluatorType = TensorEvaluator<EvalLeftArgType, Device>;
|
|
|
|
|
using RightEvaluatorType = TensorEvaluator<EvalRightArgType, Device>;
|
2015-01-14 12:36:57 -08:00
|
|
|
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int LDims =
|
2015-01-14 12:36:57 -08:00
|
|
|
internal::array_size<typename TensorEvaluator<EvalLeftArgType, Device>::Dimensions>::value;
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int RDims =
|
2015-01-14 12:36:57 -08:00
|
|
|
internal::array_size<typename TensorEvaluator<EvalRightArgType, Device>::Dimensions>::value;
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int ContractDims = internal::array_size<Indices>::value;
|
|
|
|
|
static constexpr int NumDims = LDims + RDims - 2 * ContractDims;
|
2015-01-14 12:36:57 -08:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using contract_t = array<Index, ContractDims>;
|
|
|
|
|
using left_nocontract_t = array<Index, LDims - ContractDims>;
|
|
|
|
|
using right_nocontract_t = array<Index, RDims - ContractDims>;
|
2015-01-14 12:36:57 -08:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using Dimensions = DSizes<Index, NumDims>;
|
2015-01-14 12:36:57 -08:00
|
|
|
|
|
|
|
|
EIGEN_STRONG_INLINE TensorContractionEvaluatorBase(const XprType& op, const Device& device)
|
2019-04-01 11:47:31 -07:00
|
|
|
: m_leftImpl(choose(Cond<static_cast<int>(Layout) == static_cast<int>(ColMajor)>(), op.lhsExpression(),
|
2015-01-14 12:36:57 -08:00
|
|
|
op.rhsExpression()),
|
|
|
|
|
device),
|
2019-04-01 11:47:31 -07:00
|
|
|
m_rightImpl(choose(Cond<static_cast<int>(Layout) == static_cast<int>(ColMajor)>(), op.rhsExpression(),
|
|
|
|
|
op.lhsExpression()),
|
|
|
|
|
device),
|
2015-01-14 12:36:57 -08:00
|
|
|
m_device(device),
|
2018-08-03 10:18:53 -07:00
|
|
|
m_output_kernel(op.outputKernel()),
|
2017-01-16 13:58:49 +00:00
|
|
|
m_result(NULL) {
|
2015-02-10 12:13:19 -08:00
|
|
|
EIGEN_STATIC_ASSERT((static_cast<int>(TensorEvaluator<LeftArgType, Device>::Layout) ==
|
2016-12-14 15:30:37 +00:00
|
|
|
static_cast<int>(TensorEvaluator<RightArgType, Device>::Layout)),
|
2015-01-14 12:36:57 -08:00
|
|
|
YOU_MADE_A_PROGRAMMING_MISTAKE);
|
|
|
|
|
|
|
|
|
|
DSizes<Index, LDims> eval_left_dims;
|
|
|
|
|
DSizes<Index, RDims> eval_right_dims;
|
|
|
|
|
array<IndexPair<Index>, ContractDims> eval_op_indices;
|
2015-02-10 12:20:24 -08:00
|
|
|
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
|
2015-01-14 12:36:57 -08:00
|
|
|
// For ColMajor, we keep using the existing dimensions
|
|
|
|
|
for (int i = 0; i < LDims; i++) {
|
|
|
|
|
eval_left_dims[i] = m_leftImpl.dimensions()[i];
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < RDims; i++) {
|
|
|
|
|
eval_right_dims[i] = m_rightImpl.dimensions()[i];
|
|
|
|
|
}
|
|
|
|
|
// We keep the pairs of contracting indices.
|
2015-12-07 08:12:30 -08:00
|
|
|
for (int i = 0; i < ContractDims; i++) {
|
2015-01-14 12:36:57 -08:00
|
|
|
eval_op_indices[i].first = op.indices()[i].first;
|
|
|
|
|
eval_op_indices[i].second = op.indices()[i].second;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// For RowMajor, we need to reverse the existing dimensions
|
|
|
|
|
for (int i = 0; i < LDims; i++) {
|
|
|
|
|
eval_left_dims[i] = m_leftImpl.dimensions()[LDims - i - 1];
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < RDims; i++) {
|
|
|
|
|
eval_right_dims[i] = m_rightImpl.dimensions()[RDims - i - 1];
|
|
|
|
|
}
|
|
|
|
|
// We need to flip all the pairs of contracting indices as well as
|
|
|
|
|
// reversing the dimensions.
|
2015-12-07 08:12:30 -08:00
|
|
|
for (int i = 0; i < ContractDims; i++) {
|
2016-01-20 18:12:08 -08:00
|
|
|
eval_op_indices[i].first = LDims - 1 - op.indices()[ContractDims - 1 - i].second;
|
|
|
|
|
eval_op_indices[i].second = RDims - 1 - op.indices()[ContractDims - 1 - i].first;
|
2015-01-14 12:36:57 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 15:08:02 -07:00
|
|
|
// Check for duplicate axes and make sure the first index in eval_op_indices
|
|
|
|
|
// is increasing. Using O(n^2) sorting is OK since ContractDims is small
|
|
|
|
|
for (int i = 0; i < ContractDims; i++) {
|
|
|
|
|
for (int j = i + 1; j < ContractDims; j++) {
|
|
|
|
|
eigen_assert(eval_op_indices[j].first != eval_op_indices[i].first &&
|
|
|
|
|
eval_op_indices[j].second != eval_op_indices[i].second && "contraction axes should be unique");
|
|
|
|
|
if (eval_op_indices[j].first < eval_op_indices[i].first) {
|
|
|
|
|
numext::swap(eval_op_indices[j], eval_op_indices[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
array<Index, LDims> lhs_strides;
|
2014-10-03 10:16:59 -07:00
|
|
|
lhs_strides[0] = 1;
|
2015-01-14 12:36:57 -08:00
|
|
|
for (int i = 0; i < LDims - 1; ++i) {
|
|
|
|
|
lhs_strides[i + 1] = lhs_strides[i] * eval_left_dims[i];
|
2014-10-03 10:16:59 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
array<Index, RDims> rhs_strides;
|
2014-10-03 10:16:59 -07:00
|
|
|
rhs_strides[0] = 1;
|
2015-01-14 12:36:57 -08:00
|
|
|
for (int i = 0; i < RDims - 1; ++i) {
|
|
|
|
|
rhs_strides[i + 1] = rhs_strides[i] * eval_right_dims[i];
|
2014-10-03 10:16:59 -07:00
|
|
|
}
|
2014-06-04 09:21:48 -07:00
|
|
|
|
2016-05-05 08:37:47 -07:00
|
|
|
if (m_i_strides.size() > 0) m_i_strides[0] = 1;
|
|
|
|
|
if (m_j_strides.size() > 0) m_j_strides[0] = 1;
|
|
|
|
|
if (m_k_strides.size() > 0) m_k_strides[0] = 1;
|
2014-10-03 10:16:59 -07:00
|
|
|
|
|
|
|
|
m_i_size = 1;
|
|
|
|
|
m_j_size = 1;
|
|
|
|
|
m_k_size = 1;
|
|
|
|
|
|
|
|
|
|
// To compute the dimension, we simply concatenate the non-contracting
|
|
|
|
|
// dimensions of the left and then the right tensor. Additionally, we also
|
|
|
|
|
// compute the strides corresponding to the left non-contracting
|
|
|
|
|
// dimensions and right non-contracting dimensions.
|
|
|
|
|
m_lhs_inner_dim_contiguous = true;
|
|
|
|
|
int dim_idx = 0;
|
2018-08-24 23:54:12 +02:00
|
|
|
Index nocontract_idx = 0;
|
2015-01-14 12:36:57 -08:00
|
|
|
|
|
|
|
|
for (int i = 0; i < LDims; i++) {
|
2014-10-03 10:16:59 -07:00
|
|
|
// find if we are contracting on index i of left tensor
|
|
|
|
|
bool contracting = false;
|
2015-12-07 08:12:30 -08:00
|
|
|
for (int j = 0; j < ContractDims; j++) {
|
2015-01-14 12:36:57 -08:00
|
|
|
if (eval_op_indices[j].first == i) {
|
2014-10-03 10:16:59 -07:00
|
|
|
contracting = true;
|
2014-06-04 09:21:48 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-03 10:16:59 -07:00
|
|
|
if (!contracting) {
|
|
|
|
|
// add dimension size to output dimensions
|
2015-01-14 12:36:57 -08:00
|
|
|
m_dimensions[dim_idx] = eval_left_dims[i];
|
2014-10-03 10:16:59 -07:00
|
|
|
m_left_nocontract_strides[nocontract_idx] = lhs_strides[i];
|
|
|
|
|
if (dim_idx != i) {
|
|
|
|
|
m_lhs_inner_dim_contiguous = false;
|
|
|
|
|
}
|
2026-03-19 03:08:21 +00:00
|
|
|
// Suppress false-positive GCC -Warray-bounds warning at -O3 when
|
|
|
|
|
// left_nocontract_t has size 1 (the runtime check prevents OOB access).
|
|
|
|
|
EIGEN_DIAGNOSTICS(push)
|
|
|
|
|
EIGEN_DIAGNOSTICS_OFF(disable : 4789, ignored "-Warray-bounds")
|
2014-10-03 10:16:59 -07:00
|
|
|
if (nocontract_idx + 1 < internal::array_size<left_nocontract_t>::value) {
|
2015-01-14 12:36:57 -08:00
|
|
|
m_i_strides[nocontract_idx + 1] = m_i_strides[nocontract_idx] * eval_left_dims[i];
|
2014-10-03 10:16:59 -07:00
|
|
|
} else {
|
2015-01-14 12:36:57 -08:00
|
|
|
m_i_size = m_i_strides[nocontract_idx] * eval_left_dims[i];
|
2014-10-03 10:16:59 -07:00
|
|
|
}
|
2026-03-19 03:08:21 +00:00
|
|
|
EIGEN_DIAGNOSTICS(pop)
|
2014-10-03 10:16:59 -07:00
|
|
|
dim_idx++;
|
|
|
|
|
nocontract_idx++;
|
2014-06-04 09:21:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 10:16:59 -07:00
|
|
|
nocontract_idx = 0;
|
2015-01-14 12:36:57 -08:00
|
|
|
for (int i = 0; i < RDims; i++) {
|
2014-10-03 10:16:59 -07:00
|
|
|
bool contracting = false;
|
|
|
|
|
// find if we are contracting on index i of right tensor
|
2015-12-07 08:12:30 -08:00
|
|
|
for (int j = 0; j < ContractDims; j++) {
|
2015-01-14 12:36:57 -08:00
|
|
|
if (eval_op_indices[j].second == i) {
|
2014-10-03 10:16:59 -07:00
|
|
|
contracting = true;
|
2014-06-04 09:21:48 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-03 10:16:59 -07:00
|
|
|
if (!contracting) {
|
2015-01-14 12:36:57 -08:00
|
|
|
m_dimensions[dim_idx] = eval_right_dims[i];
|
2026-03-19 03:08:21 +00:00
|
|
|
EIGEN_DIAGNOSTICS(push)
|
|
|
|
|
EIGEN_DIAGNOSTICS_OFF(disable : 4789, ignored "-Warray-bounds")
|
2014-10-03 10:16:59 -07:00
|
|
|
if (nocontract_idx + 1 < internal::array_size<right_nocontract_t>::value) {
|
2015-01-14 12:36:57 -08:00
|
|
|
m_j_strides[nocontract_idx + 1] = m_j_strides[nocontract_idx] * eval_right_dims[i];
|
2014-10-03 10:16:59 -07:00
|
|
|
} else {
|
2015-01-14 12:36:57 -08:00
|
|
|
m_j_size = m_j_strides[nocontract_idx] * eval_right_dims[i];
|
2014-10-03 10:16:59 -07:00
|
|
|
}
|
2026-03-19 03:08:21 +00:00
|
|
|
EIGEN_DIAGNOSTICS(pop)
|
2014-10-03 10:16:59 -07:00
|
|
|
m_right_nocontract_strides[nocontract_idx] = rhs_strides[i];
|
|
|
|
|
dim_idx++;
|
|
|
|
|
nocontract_idx++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now compute the strides corresponding to the contracting dimensions. We
|
|
|
|
|
// assumed above that non-contracting axes are represented in the same order
|
|
|
|
|
// in the matrix as they are in the tensor. This is not the case for
|
|
|
|
|
// contracting axes. As the contracting axes must be of the same size in
|
|
|
|
|
// each tensor, we'll only look at the first tensor here.
|
|
|
|
|
m_rhs_inner_dim_contiguous = true;
|
|
|
|
|
m_rhs_inner_dim_reordered = false;
|
2015-12-07 08:12:30 -08:00
|
|
|
for (int i = 0; i < ContractDims; i++) {
|
2015-01-14 12:36:57 -08:00
|
|
|
Index left = eval_op_indices[i].first;
|
|
|
|
|
Index right = eval_op_indices[i].second;
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
Index size = eval_left_dims[left];
|
|
|
|
|
eigen_assert(size == eval_right_dims[right] && "Contraction axes must be same size");
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2026-03-19 03:08:21 +00:00
|
|
|
EIGEN_DIAGNOSTICS(push)
|
|
|
|
|
EIGEN_DIAGNOSTICS_OFF(disable : 4789, ignored "-Warray-bounds")
|
2015-12-07 16:17:57 -08:00
|
|
|
if (i + 1 < static_cast<int>(internal::array_size<contract_t>::value)) {
|
2014-10-03 10:16:59 -07:00
|
|
|
m_k_strides[i + 1] = m_k_strides[i] * size;
|
2014-06-04 09:21:48 -07:00
|
|
|
} else {
|
2014-10-03 10:16:59 -07:00
|
|
|
m_k_size = m_k_strides[i] * size;
|
|
|
|
|
}
|
2026-03-19 03:08:21 +00:00
|
|
|
EIGEN_DIAGNOSTICS(pop)
|
2014-10-03 10:16:59 -07:00
|
|
|
m_left_contracting_strides[i] = lhs_strides[left];
|
|
|
|
|
m_right_contracting_strides[i] = rhs_strides[right];
|
|
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
if (i > 0 && right < eval_op_indices[i - 1].second) {
|
2014-10-03 10:16:59 -07:00
|
|
|
m_rhs_inner_dim_reordered = true;
|
|
|
|
|
}
|
|
|
|
|
if (right != i) {
|
|
|
|
|
m_rhs_inner_dim_contiguous = false;
|
2014-06-04 09:21:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
// If the layout is RowMajor, we need to reverse the m_dimensions
|
2015-02-10 12:13:19 -08:00
|
|
|
if (static_cast<int>(Layout) == static_cast<int>(RowMajor)) {
|
2015-01-14 12:36:57 -08:00
|
|
|
for (int i = 0, j = NumDims - 1; i < j; i++, j--) {
|
2015-07-06 17:02:29 -07:00
|
|
|
numext::swap(m_dimensions[i], m_dimensions[j]);
|
2015-01-14 12:36:57 -08:00
|
|
|
}
|
|
|
|
|
}
|
2018-07-10 13:16:38 -07:00
|
|
|
|
|
|
|
|
// A set of parameters that will allow output kernel to get from output
|
|
|
|
|
// tensor dimensions (i, j) into the original tensor dimensions.
|
|
|
|
|
// TODO(ezhulenev): Add parameters required to infer output tensor index for
|
|
|
|
|
// more complex contractions than 2x2 on internal dimension.
|
2018-07-17 14:16:48 -04:00
|
|
|
m_tensor_contraction_params.swapped_arguments = static_cast<int>(Layout) == RowMajor;
|
2014-06-04 09:21:48 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-03 10:16:59 -07:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
|
2014-06-04 09:21:48 -07:00
|
|
|
|
2021-05-11 22:47:49 +00:00
|
|
|
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
|
2014-08-13 08:33:18 -07:00
|
|
|
m_leftImpl.evalSubExprsIfNeeded(NULL);
|
|
|
|
|
m_rightImpl.evalSubExprsIfNeeded(NULL);
|
2014-10-03 10:16:59 -07:00
|
|
|
if (data) {
|
|
|
|
|
evalTo(data);
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2019-06-28 10:08:23 +01:00
|
|
|
m_result = static_cast<EvaluatorPointerType>(m_device.allocate(dimensions().TotalSize() * sizeof(Scalar)));
|
2014-10-03 10:16:59 -07:00
|
|
|
evalTo(m_result);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 15:13:38 -07:00
|
|
|
#ifdef EIGEN_USE_THREADS
|
|
|
|
|
template <typename EvalSubExprsCallback>
|
|
|
|
|
EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(EvaluatorPointerType dest, EvalSubExprsCallback done) {
|
|
|
|
|
m_leftImpl.evalSubExprsIfNeededAsync(nullptr, [this, done, dest](bool) {
|
|
|
|
|
m_rightImpl.evalSubExprsIfNeededAsync(nullptr, [this, done, dest](bool) {
|
|
|
|
|
if (dest) {
|
|
|
|
|
evalToAsync(dest, [done]() { done(false); });
|
|
|
|
|
} else {
|
|
|
|
|
m_result = static_cast<EvaluatorPointerType>(m_device.allocate(dimensions().TotalSize() * sizeof(Scalar)));
|
|
|
|
|
evalToAsync(m_result, [done]() { done(true); });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
#endif // EIGEN_USE_THREADS
|
|
|
|
|
|
2021-06-11 08:30:41 -07:00
|
|
|
#ifndef TENSOR_CONTRACTION_DISPATCH
|
2019-08-30 15:13:38 -07:00
|
|
|
#define TENSOR_CONTRACTION_DISPATCH(METHOD, ALIGNMENT, ARGS) \
|
|
|
|
|
if (this->m_lhs_inner_dim_contiguous) { \
|
|
|
|
|
if (this->m_rhs_inner_dim_contiguous) { \
|
|
|
|
|
if (this->m_rhs_inner_dim_reordered) { \
|
|
|
|
|
METHOD<true, true, true, ALIGNMENT> ARGS; \
|
|
|
|
|
} else { \
|
|
|
|
|
METHOD<true, true, false, ALIGNMENT> ARGS; \
|
|
|
|
|
} \
|
|
|
|
|
} else { \
|
|
|
|
|
if (this->m_rhs_inner_dim_reordered) { \
|
|
|
|
|
METHOD<true, false, true, ALIGNMENT> ARGS; \
|
|
|
|
|
} else { \
|
|
|
|
|
METHOD<true, false, false, ALIGNMENT> ARGS; \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
} else { \
|
|
|
|
|
if (this->m_rhs_inner_dim_contiguous) { \
|
|
|
|
|
if (this->m_rhs_inner_dim_reordered) { \
|
|
|
|
|
METHOD<false, true, true, ALIGNMENT> ARGS; \
|
|
|
|
|
} else { \
|
|
|
|
|
METHOD<false, true, false, ALIGNMENT> ARGS; \
|
|
|
|
|
} \
|
|
|
|
|
} else { \
|
|
|
|
|
if (this->m_rhs_inner_dim_reordered) { \
|
|
|
|
|
METHOD<false, false, true, ALIGNMENT> ARGS; \
|
|
|
|
|
} else { \
|
|
|
|
|
METHOD<false, false, false, ALIGNMENT> ARGS; \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
}
|
2021-06-11 08:30:41 -07:00
|
|
|
#endif
|
2019-08-30 15:13:38 -07:00
|
|
|
|
2021-06-11 08:30:41 -07:00
|
|
|
#ifndef TENSOR_CONTRACTION_ASYNC_DISPATCH
|
2019-08-30 15:13:38 -07:00
|
|
|
#define TENSOR_CONTRACTION_ASYNC_DISPATCH(METHOD, DONE, ALIGNMENT, ARGS, FN) \
|
|
|
|
|
if (this->m_lhs_inner_dim_contiguous) { \
|
|
|
|
|
if (this->m_rhs_inner_dim_contiguous) { \
|
|
|
|
|
if (this->m_rhs_inner_dim_reordered) { \
|
|
|
|
|
(new METHOD<DONE, true, true, true, ALIGNMENT> ARGS)->FN; \
|
|
|
|
|
} else { \
|
|
|
|
|
(new METHOD<DONE, true, true, false, ALIGNMENT> ARGS)->FN; \
|
|
|
|
|
} \
|
|
|
|
|
} else { \
|
|
|
|
|
if (this->m_rhs_inner_dim_reordered) { \
|
|
|
|
|
(new METHOD<DONE, true, false, true, ALIGNMENT> ARGS)->FN; \
|
|
|
|
|
} else { \
|
|
|
|
|
(new METHOD<DONE, true, false, false, ALIGNMENT> ARGS)->FN; \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
} else { \
|
|
|
|
|
if (this->m_rhs_inner_dim_contiguous) { \
|
|
|
|
|
if (this->m_rhs_inner_dim_reordered) { \
|
|
|
|
|
(new METHOD<DONE, false, true, true, ALIGNMENT> ARGS)->FN; \
|
|
|
|
|
} else { \
|
|
|
|
|
(new METHOD<DONE, false, true, false, ALIGNMENT> ARGS)->FN; \
|
|
|
|
|
} \
|
|
|
|
|
} else { \
|
|
|
|
|
if (this->m_rhs_inner_dim_reordered) { \
|
|
|
|
|
(new METHOD<DONE, false, false, true, ALIGNMENT> ARGS)->FN; \
|
|
|
|
|
} else { \
|
|
|
|
|
(new METHOD<DONE, false, false, false, ALIGNMENT> ARGS)->FN; \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
}
|
2021-06-11 08:30:41 -07:00
|
|
|
#endif
|
2018-07-27 12:36:34 -07:00
|
|
|
|
|
|
|
|
EIGEN_DEVICE_FUNC void evalTo(Scalar* buffer) const {
|
|
|
|
|
static_cast<const Derived*>(this)->template evalProduct<Unaligned>(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 15:13:38 -07:00
|
|
|
#ifdef EIGEN_USE_THREADS
|
|
|
|
|
template <typename EvalToCallback>
|
|
|
|
|
void evalToAsync(Scalar* buffer, EvalToCallback done) const {
|
|
|
|
|
static_cast<const Derived*>(this)->template evalProductAsync<EvalToCallback, Unaligned>(buffer, std::move(done));
|
|
|
|
|
}
|
|
|
|
|
#endif // EIGEN_USE_THREADS
|
|
|
|
|
|
2018-07-27 12:36:34 -07:00
|
|
|
template <bool lhs_inner_dim_contiguous, bool rhs_inner_dim_contiguous, bool rhs_inner_dim_reordered, int Alignment>
|
|
|
|
|
void evalProductSequential(Scalar* buffer) const {
|
|
|
|
|
if (this->m_j_size == 1) {
|
|
|
|
|
this->template evalGemv<lhs_inner_dim_contiguous, rhs_inner_dim_contiguous, rhs_inner_dim_reordered, Alignment>(
|
|
|
|
|
buffer);
|
|
|
|
|
} else {
|
|
|
|
|
this->template evalGemm<lhs_inner_dim_contiguous, rhs_inner_dim_contiguous, rhs_inner_dim_reordered, Alignment>(
|
|
|
|
|
buffer);
|
2014-10-03 10:16:59 -07:00
|
|
|
}
|
2014-06-13 09:56:51 -07:00
|
|
|
}
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2014-11-03 08:51:33 -08:00
|
|
|
template <bool lhs_inner_dim_contiguous, bool rhs_inner_dim_contiguous, bool rhs_inner_dim_reordered, int Alignment>
|
2018-07-18 14:21:01 -07:00
|
|
|
#if !defined(EIGEN_HIPCC)
|
2018-06-06 10:12:58 -04:00
|
|
|
EIGEN_DEVICE_FUNC
|
|
|
|
|
#endif
|
|
|
|
|
void
|
|
|
|
|
evalGemv(Scalar* buffer) const {
|
2014-11-03 08:51:33 -08:00
|
|
|
const Index rows = m_i_size;
|
|
|
|
|
const Index cols = m_k_size;
|
|
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using LhsScalar = std::remove_const_t<typename EvalLeftArgType::Scalar>;
|
|
|
|
|
using RhsScalar = std::remove_const_t<typename EvalRightArgType::Scalar>;
|
|
|
|
|
using LeftEvaluator = TensorEvaluator<EvalLeftArgType, Device>;
|
|
|
|
|
using RightEvaluator = TensorEvaluator<EvalRightArgType, Device>;
|
2025-08-25 18:26:46 +08:00
|
|
|
const int lhs_packet_size = internal::unpacket_traits<typename LeftEvaluator::PacketReturnType>::size;
|
|
|
|
|
const int rhs_packet_size = internal::unpacket_traits<typename RightEvaluator::PacketReturnType>::size;
|
2016-01-12 11:32:27 -08:00
|
|
|
const int lhs_alignment = LeftEvaluator::IsAligned ? Aligned : Unaligned;
|
|
|
|
|
const int rhs_alignment = RightEvaluator::IsAligned ? Aligned : Unaligned;
|
2026-03-04 08:59:22 -08:00
|
|
|
using LhsMapper = internal::TensorContractionInputMapper<LhsScalar, Index, internal::Lhs, LeftEvaluator,
|
|
|
|
|
left_nocontract_t, contract_t, lhs_packet_size,
|
|
|
|
|
lhs_inner_dim_contiguous, false, lhs_alignment>;
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using RhsMapper =
|
|
|
|
|
internal::TensorContractionInputMapper<RhsScalar, Index, internal::Rhs, RightEvaluator, right_nocontract_t,
|
|
|
|
|
contract_t, rhs_packet_size, rhs_inner_dim_contiguous,
|
|
|
|
|
rhs_inner_dim_reordered, rhs_alignment>;
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2014-11-03 08:51:33 -08:00
|
|
|
LhsMapper lhs(m_leftImpl, m_left_nocontract_strides, m_i_strides, m_left_contracting_strides, m_k_strides);
|
|
|
|
|
RhsMapper rhs(m_rightImpl, m_right_nocontract_strides, m_j_strides, m_right_contracting_strides, m_k_strides);
|
|
|
|
|
|
|
|
|
|
const Scalar alpha(1);
|
|
|
|
|
const Index resIncr(1);
|
|
|
|
|
|
|
|
|
|
// zero out the result buffer (which must be of size at least rows * sizeof(Scalar)
|
2021-05-11 09:52:00 -07:00
|
|
|
m_device.fill(buffer, buffer + rows, Scalar(0));
|
2014-11-03 08:51:33 -08:00
|
|
|
|
|
|
|
|
internal::general_matrix_vector_product<Index, LhsScalar, LhsMapper, ColMajor, false, RhsScalar, RhsMapper,
|
|
|
|
|
false>::run(rows, cols, lhs, rhs, buffer, resIncr, alpha);
|
2018-07-12 14:52:23 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using OutputMapper = internal::blas_data_mapper<Scalar, Index, ColMajor>;
|
2018-07-12 14:52:23 -07:00
|
|
|
m_output_kernel(OutputMapper(buffer, rows), m_tensor_contraction_params, static_cast<Index>(0),
|
|
|
|
|
static_cast<Index>(0), rows, static_cast<Index>(1));
|
2014-11-03 08:51:33 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 16:48:10 -07:00
|
|
|
template <bool lhs_inner_dim_contiguous, bool rhs_inner_dim_contiguous, bool rhs_inner_dim_reordered, int Alignment>
|
2018-07-18 14:21:01 -07:00
|
|
|
#if !defined(EIGEN_HIPCC)
|
2018-06-06 10:12:58 -04:00
|
|
|
EIGEN_DEVICE_FUNC
|
|
|
|
|
#endif
|
|
|
|
|
void
|
|
|
|
|
evalGemm(Scalar* buffer) const {
|
2016-04-15 16:48:10 -07:00
|
|
|
// columns in left side, rows in right side
|
|
|
|
|
const Index k = this->m_k_size;
|
2018-09-26 16:47:13 -07:00
|
|
|
this->template evalGemmPartial<lhs_inner_dim_contiguous, rhs_inner_dim_contiguous, rhs_inner_dim_reordered,
|
2018-10-02 18:36:30 +02:00
|
|
|
Alignment, true>(buffer, 0, k, 1);
|
2018-09-26 16:47:13 -07:00
|
|
|
}
|
|
|
|
|
|
2018-09-28 11:24:08 -07:00
|
|
|
template <bool lhs_inner_dim_contiguous, bool rhs_inner_dim_contiguous, bool rhs_inner_dim_reordered, int Alignment>
|
|
|
|
|
EIGEN_DEVICE_FUNC void evalGemmPartialWithoutOutputKernel(Scalar* buffer, Index k_start, Index k_end,
|
|
|
|
|
int num_threads) const {
|
|
|
|
|
evalGemmPartial<lhs_inner_dim_contiguous, rhs_inner_dim_contiguous, rhs_inner_dim_reordered, Alignment,
|
|
|
|
|
/*use_output_kernel*/ false>(buffer, k_start, k_end, num_threads);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 18:36:30 +02:00
|
|
|
template <bool lhs_inner_dim_contiguous, bool rhs_inner_dim_contiguous, bool rhs_inner_dim_reordered, int Alignment,
|
|
|
|
|
bool use_output_kernel>
|
2018-09-27 12:08:17 -07:00
|
|
|
EIGEN_DEVICE_FUNC void evalGemmPartial(Scalar* buffer, Index k_start, Index k_end, int num_threads) const {
|
2018-09-28 16:00:09 -07:00
|
|
|
eigen_assert(k_end >= k_start && k_start >= 0 && k_end <= this->m_k_size);
|
|
|
|
|
// columns in slice on left side, rows on right side
|
2018-09-27 12:08:17 -07:00
|
|
|
const Index k_slice = k_end - k_start;
|
2018-09-26 16:47:13 -07:00
|
|
|
|
|
|
|
|
// rows in left side
|
|
|
|
|
const Index m = this->m_i_size;
|
|
|
|
|
|
|
|
|
|
// columns in right side
|
|
|
|
|
const Index n = this->m_j_size;
|
2016-04-15 16:48:10 -07:00
|
|
|
|
2018-09-27 11:49:19 -07:00
|
|
|
// define data mappers for Lhs and Rhs
|
2026-03-04 08:59:22 -08:00
|
|
|
using LhsScalar = std::remove_const_t<typename EvalLeftArgType::Scalar>;
|
|
|
|
|
using RhsScalar = std::remove_const_t<typename EvalRightArgType::Scalar>;
|
2016-04-15 16:48:10 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using LeftEvaluator = TensorEvaluator<EvalLeftArgType, Device>;
|
|
|
|
|
using RightEvaluator = TensorEvaluator<EvalRightArgType, Device>;
|
2016-04-15 16:48:10 -07:00
|
|
|
|
2025-08-25 18:26:46 +08:00
|
|
|
const int lhs_packet_size = internal::unpacket_traits<typename LeftEvaluator::PacketReturnType>::size;
|
|
|
|
|
const int rhs_packet_size = internal::unpacket_traits<typename RightEvaluator::PacketReturnType>::size;
|
2016-04-15 16:48:10 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using LhsMapper =
|
|
|
|
|
internal::TensorContractionInputMapper<LhsScalar, Index, internal::Lhs, LeftEvaluator, left_nocontract_t,
|
|
|
|
|
contract_t, lhs_packet_size, lhs_inner_dim_contiguous, false, Unaligned>;
|
2016-04-15 16:48:10 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using RhsMapper =
|
|
|
|
|
internal::TensorContractionInputMapper<RhsScalar, Index, internal::Rhs, RightEvaluator, right_nocontract_t,
|
|
|
|
|
contract_t, rhs_packet_size, rhs_inner_dim_contiguous,
|
|
|
|
|
rhs_inner_dim_reordered, Unaligned>;
|
2016-04-15 16:48:10 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using OutputMapper = internal::blas_data_mapper<Scalar, Index, ColMajor>;
|
2016-04-15 16:48:10 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using TensorContractionKernel =
|
|
|
|
|
internal::TensorContractionKernel<Scalar, LhsScalar, RhsScalar, Index, OutputMapper, LhsMapper, RhsMapper>;
|
2016-04-15 16:48:10 -07:00
|
|
|
|
|
|
|
|
// initialize data mappers
|
|
|
|
|
LhsMapper lhs(this->m_leftImpl, this->m_left_nocontract_strides, this->m_i_strides,
|
|
|
|
|
this->m_left_contracting_strides, this->m_k_strides);
|
|
|
|
|
|
|
|
|
|
RhsMapper rhs(this->m_rightImpl, this->m_right_nocontract_strides, this->m_j_strides,
|
|
|
|
|
this->m_right_contracting_strides, this->m_k_strides);
|
|
|
|
|
|
|
|
|
|
OutputMapper output(buffer, m);
|
|
|
|
|
|
|
|
|
|
// Sizes of the blocks to load in cache. See the Goto paper for details.
|
2018-09-27 12:08:17 -07:00
|
|
|
internal::TensorContractionBlocking<Scalar, LhsScalar, RhsScalar, Index, internal::ShardByCol> blocking(
|
|
|
|
|
k_slice, m, n, num_threads);
|
2016-04-15 16:48:10 -07:00
|
|
|
const Index kc = blocking.kc();
|
|
|
|
|
const Index mc = numext::mini(m, blocking.mc());
|
|
|
|
|
const Index nc = numext::mini(n, blocking.nc());
|
|
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using LhsBlock = typename TensorContractionKernel::LhsBlock;
|
|
|
|
|
using RhsBlock = typename TensorContractionKernel::RhsBlock;
|
2019-04-01 11:47:31 -07:00
|
|
|
|
|
|
|
|
LhsBlock blockA;
|
|
|
|
|
RhsBlock blockB;
|
|
|
|
|
|
|
|
|
|
TensorContractionKernel kernel(m, k_slice, n, mc, kc, nc);
|
|
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using BlockMemHandle = typename TensorContractionKernel::BlockMemHandle;
|
2019-04-01 11:47:31 -07:00
|
|
|
const BlockMemHandle packed_mem = kernel.allocate(this->m_device, &blockA, &blockB);
|
2016-04-15 16:48:10 -07:00
|
|
|
|
2019-10-02 11:06:02 -07:00
|
|
|
// If a contraction kernel does not support beta, explicitly initialize
|
|
|
|
|
// output buffer with zeroes.
|
|
|
|
|
if (!TensorContractionKernel::HasBeta) {
|
2021-05-11 09:52:00 -07:00
|
|
|
this->m_device.fill(buffer, buffer + m * n, Scalar(0));
|
2019-10-02 11:06:02 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 16:48:10 -07:00
|
|
|
for (Index i2 = 0; i2 < m; i2 += mc) {
|
|
|
|
|
const Index actual_mc = numext::mini(i2 + mc, m) - i2;
|
2018-09-26 16:47:13 -07:00
|
|
|
for (Index k2 = k_start; k2 < k_end; k2 += kc) {
|
2016-04-15 16:48:10 -07:00
|
|
|
// make sure we don't overshoot right edge of left matrix, then pack vertical panel
|
2018-09-28 11:24:08 -07:00
|
|
|
const Index actual_kc = numext::mini(k2 + kc, k_end) - k2;
|
2019-04-01 11:47:31 -07:00
|
|
|
kernel.packLhs(&blockA, lhs.getSubMapper(i2, k2), actual_kc, actual_mc);
|
2016-04-15 16:48:10 -07:00
|
|
|
|
2019-10-02 11:06:02 -07:00
|
|
|
// If kernel supports beta, there is no need to initialize output
|
|
|
|
|
// buffer with zeroes.
|
|
|
|
|
const Scalar alpha = Scalar(1);
|
|
|
|
|
const Scalar beta = (TensorContractionKernel::HasBeta && k2 == k_start) ? Scalar(0) : Scalar(1);
|
|
|
|
|
|
2016-04-15 16:48:10 -07:00
|
|
|
// series of horizontal blocks
|
|
|
|
|
for (Index j2 = 0; j2 < n; j2 += nc) {
|
|
|
|
|
// make sure we don't overshoot right edge of right matrix, then pack block
|
|
|
|
|
const Index actual_nc = numext::mini(j2 + nc, n) - j2;
|
2019-04-01 11:47:31 -07:00
|
|
|
kernel.packRhs(&blockB, rhs.getSubMapper(k2, j2), actual_kc, actual_nc);
|
2016-04-15 16:48:10 -07:00
|
|
|
|
|
|
|
|
// call gebp (matrix kernel)
|
|
|
|
|
// The parameters here are copied from Eigen's GEMM implementation
|
2018-08-16 10:41:01 -07:00
|
|
|
const OutputMapper output_mapper = output.getSubMapper(i2, j2);
|
2019-04-01 11:47:31 -07:00
|
|
|
kernel.invoke(output_mapper, blockA, blockB, actual_mc, actual_kc, actual_nc, alpha, beta);
|
2018-07-10 13:16:38 -07:00
|
|
|
|
|
|
|
|
// We are done with this [i2, j2] output block.
|
2018-09-28 11:24:08 -07:00
|
|
|
if (use_output_kernel && k2 + kc >= k_end) {
|
2018-07-10 13:16:38 -07:00
|
|
|
m_output_kernel(output_mapper, m_tensor_contraction_params, i2, j2, actual_mc, actual_nc);
|
|
|
|
|
}
|
2016-04-15 16:48:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 11:47:31 -07:00
|
|
|
kernel.deallocate(this->m_device, packed_mem);
|
2016-04-15 16:48:10 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-11 22:47:49 +00:00
|
|
|
EIGEN_STRONG_INLINE void cleanup() {
|
2014-06-13 09:56:51 -07:00
|
|
|
m_leftImpl.cleanup();
|
|
|
|
|
m_rightImpl.cleanup();
|
2014-10-03 10:16:59 -07:00
|
|
|
|
|
|
|
|
if (m_result != NULL) {
|
|
|
|
|
m_device.deallocate(m_result);
|
|
|
|
|
m_result = NULL;
|
|
|
|
|
}
|
2014-06-13 09:56:51 -07:00
|
|
|
}
|
2014-06-04 09:21:48 -07:00
|
|
|
|
2014-10-03 10:16:59 -07:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_result[index]; }
|
2014-06-04 09:21:48 -07:00
|
|
|
|
2016-04-15 15:34:34 -07:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool) const {
|
2016-04-14 13:57:35 -07:00
|
|
|
return TensorOpCost(sizeof(CoeffReturnType), 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-04 09:21:48 -07:00
|
|
|
template <int LoadMode>
|
2016-01-20 14:51:48 -08:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
|
2016-03-08 12:07:33 -08:00
|
|
|
return internal::ploadt<PacketReturnType, LoadMode>(m_result + index);
|
2014-06-04 09:21:48 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-28 10:08:23 +01:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EvaluatorPointerType data() const { return m_result; }
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2016-12-21 12:32:06 -08:00
|
|
|
protected:
|
2014-06-04 09:21:48 -07:00
|
|
|
Dimensions m_dimensions;
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2026-03-19 03:08:21 +00:00
|
|
|
contract_t m_k_strides{};
|
|
|
|
|
contract_t m_left_contracting_strides{};
|
|
|
|
|
contract_t m_right_contracting_strides{};
|
2014-10-03 10:16:59 -07:00
|
|
|
|
|
|
|
|
bool m_lhs_inner_dim_contiguous;
|
|
|
|
|
bool m_rhs_inner_dim_contiguous;
|
|
|
|
|
bool m_rhs_inner_dim_reordered;
|
|
|
|
|
|
2026-03-19 03:08:21 +00:00
|
|
|
left_nocontract_t m_i_strides{};
|
|
|
|
|
right_nocontract_t m_j_strides{};
|
|
|
|
|
left_nocontract_t m_left_nocontract_strides{};
|
|
|
|
|
right_nocontract_t m_right_nocontract_strides{};
|
2014-10-03 10:16:59 -07:00
|
|
|
|
|
|
|
|
Index m_i_size;
|
|
|
|
|
Index m_j_size;
|
|
|
|
|
Index m_k_size;
|
|
|
|
|
|
2018-07-10 13:16:38 -07:00
|
|
|
TensorContractionParams m_tensor_contraction_params;
|
|
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
TensorEvaluator<EvalLeftArgType, Device> m_leftImpl;
|
|
|
|
|
TensorEvaluator<EvalRightArgType, Device> m_rightImpl;
|
2019-06-28 10:08:23 +01:00
|
|
|
const Device EIGEN_DEVICE_REF m_device;
|
2018-07-10 13:16:38 -07:00
|
|
|
OutputKernelType m_output_kernel;
|
2019-06-28 10:08:23 +01:00
|
|
|
EvaluatorPointerType m_result;
|
2014-06-04 09:21:48 -07:00
|
|
|
};
|
|
|
|
|
|
2015-01-14 12:36:57 -08:00
|
|
|
// evaluator for default device
|
2018-07-10 13:16:38 -07:00
|
|
|
template <typename Indices, typename LeftArgType, typename RightArgType, typename OutputKernelType, typename Device>
|
|
|
|
|
struct TensorEvaluator<const TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType>, Device>
|
2015-01-14 12:36:57 -08:00
|
|
|
: public TensorContractionEvaluatorBase<
|
2018-07-10 13:16:38 -07:00
|
|
|
TensorEvaluator<const TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType>, Device>> {
|
2026-03-04 08:59:22 -08:00
|
|
|
using Self = TensorEvaluator<const TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType>, Device>;
|
|
|
|
|
using Base = TensorContractionEvaluatorBase<Self>;
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using XprType = TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType>;
|
|
|
|
|
using Scalar = std::remove_const_t<typename XprType::Scalar>;
|
|
|
|
|
using Index = typename XprType::Index;
|
|
|
|
|
using CoeffReturnType = typename XprType::CoeffReturnType;
|
|
|
|
|
using PacketReturnType = typename PacketType<CoeffReturnType, Device>::type;
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2022-03-16 16:43:40 +00:00
|
|
|
static constexpr int Layout = TensorEvaluator<LeftArgType, Device>::Layout;
|
2015-01-14 12:36:57 -08:00
|
|
|
|
|
|
|
|
// Most of the code is assuming that both input tensors are ColMajor. If the
|
|
|
|
|
// inputs are RowMajor, we will "cheat" by swapping the LHS and RHS:
|
|
|
|
|
// If we want to compute A * B = C, where A is LHS and B is RHS, the code
|
|
|
|
|
// will pretend B is LHS and A is RHS.
|
2026-03-04 08:59:22 -08:00
|
|
|
using EvalLeftArgType = std::conditional_t<Layout == static_cast<int>(ColMajor), LeftArgType, RightArgType>;
|
|
|
|
|
using EvalRightArgType = std::conditional_t<Layout == static_cast<int>(ColMajor), RightArgType, LeftArgType>;
|
2015-01-14 12:36:57 -08:00
|
|
|
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int LDims =
|
2015-01-14 12:36:57 -08:00
|
|
|
internal::array_size<typename TensorEvaluator<EvalLeftArgType, Device>::Dimensions>::value;
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int RDims =
|
2015-01-14 12:36:57 -08:00
|
|
|
internal::array_size<typename TensorEvaluator<EvalRightArgType, Device>::Dimensions>::value;
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int ContractDims = internal::array_size<Indices>::value;
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2026-03-04 08:59:22 -08:00
|
|
|
using contract_t = array<Index, ContractDims>;
|
|
|
|
|
using left_nocontract_t = array<Index, LDims - ContractDims>;
|
|
|
|
|
using right_nocontract_t = array<Index, RDims - ContractDims>;
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2022-04-04 17:33:33 +00:00
|
|
|
static constexpr int NumDims = LDims + RDims - 2 * ContractDims;
|
2015-01-14 12:36:57 -08:00
|
|
|
|
|
|
|
|
// Could we use NumDimensions here?
|
2026-03-04 08:59:22 -08:00
|
|
|
using Dimensions = DSizes<Index, NumDims>;
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2021-05-11 22:47:49 +00:00
|
|
|
TensorEvaluator(const XprType& op, const Device& device) : Base(op, device) {}
|
2014-10-03 10:16:59 -07:00
|
|
|
|
2018-07-27 12:36:34 -07:00
|
|
|
template <int Alignment>
|
|
|
|
|
void evalProduct(Scalar* buffer) const {
|
|
|
|
|
TENSOR_CONTRACTION_DISPATCH(this->template evalProductSequential, Alignment, (buffer));
|
2014-10-03 10:16:59 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-06-04 09:21:48 -07:00
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
|
|
|
|
#endif // EIGEN_CXX11_TENSOR_TENSOR_CONTRACTION_H
|