2018-07-21 08:37:20 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2018 Andy Davis <andydavis@google.com>
|
|
|
|
// Copyright (C) 2018 Eugene Zhulenev <ezhulenev@google.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/.
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
2018-08-15 05:26:59 +08:00
|
|
|
#include <algorithm>
|
2018-07-21 08:37:20 +08:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include <Eigen/CXX11/Tensor>
|
|
|
|
|
|
|
|
using Eigen::Tensor;
|
|
|
|
using Eigen::Index;
|
|
|
|
using Eigen::RowMajor;
|
|
|
|
using Eigen::ColMajor;
|
2019-12-11 07:40:23 +08:00
|
|
|
using Eigen::internal::TensorBlockShapeType;
|
2018-07-21 08:37:20 +08:00
|
|
|
|
2019-12-19 04:07:00 +08:00
|
|
|
static TensorOpCost zeroCost() { return {0, 0, 0}; }
|
2018-07-24 06:50:55 +08:00
|
|
|
|
2018-07-21 08:37:20 +08:00
|
|
|
template<typename T>
|
|
|
|
static const T& choose(int layout, const T& col, const T& row) {
|
|
|
|
return layout == ColMajor ? col : row;
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:40:23 +08:00
|
|
|
static TensorBlockShapeType RandomShape() {
|
2018-07-24 06:50:55 +08:00
|
|
|
return internal::random<bool>()
|
2019-12-11 07:40:23 +08:00
|
|
|
? TensorBlockShapeType::kUniformAllDims
|
|
|
|
: TensorBlockShapeType::kSkewedInnerDims;
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <int NumDims>
|
2019-12-11 03:58:30 +08:00
|
|
|
static size_t RandomTargetSize(const DSizes<Index, NumDims>& dims) {
|
|
|
|
return internal::random<size_t>(1, dims.TotalSize());
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:17 +08:00
|
|
|
template <int NumDims>
|
|
|
|
static DSizes<Index, NumDims> RandomDims() {
|
|
|
|
array<Index, NumDims> dims;
|
|
|
|
for (int i = 0; i < NumDims; ++i) {
|
|
|
|
dims[i] = internal::random<int>(1, 20);
|
|
|
|
}
|
|
|
|
return DSizes<Index, NumDims>(dims);
|
2018-08-15 05:26:59 +08:00
|
|
|
}
|
2018-07-28 03:45:17 +08:00
|
|
|
|
2018-07-24 06:50:55 +08:00
|
|
|
template <typename T>
|
|
|
|
static T* GenerateRandomData(const Index& size) {
|
|
|
|
T* data = new T[size];
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
data[i] = internal::random<T>();
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:17 +08:00
|
|
|
template <int NumDims>
|
|
|
|
static void Debug(DSizes<Index, NumDims> dims) {
|
|
|
|
for (int i = 0; i < NumDims; ++i) {
|
|
|
|
std::cout << dims[i] << "; ";
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
|
2018-07-21 08:37:20 +08:00
|
|
|
template <int Layout>
|
|
|
|
static void test_block_mapper_sanity()
|
|
|
|
{
|
2019-12-11 07:40:23 +08:00
|
|
|
typedef internal::TensorBlockMapper<2, Layout> TensorBlockMapper;
|
2018-07-21 08:37:20 +08:00
|
|
|
|
|
|
|
DSizes<Index, 2> tensor_dims(100, 100);
|
|
|
|
|
|
|
|
// Test uniform blocks.
|
|
|
|
TensorBlockMapper uniform_block_mapper(
|
2019-12-19 04:07:00 +08:00
|
|
|
tensor_dims, {TensorBlockShapeType::kUniformAllDims, 100, zeroCost()});
|
2018-07-21 08:37:20 +08:00
|
|
|
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(uniform_block_mapper.blockCount(), 100);
|
|
|
|
VERIFY_IS_EQUAL(uniform_block_mapper.blockTotalSize(), 100);
|
2018-07-21 08:37:20 +08:00
|
|
|
|
|
|
|
// 10x10 blocks
|
2019-12-11 03:58:30 +08:00
|
|
|
auto uniform_b0 = uniform_block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(uniform_b0.dimensions().at(0), 10);
|
|
|
|
VERIFY_IS_EQUAL(uniform_b0.dimensions().at(1), 10);
|
2018-07-21 08:37:20 +08:00
|
|
|
|
|
|
|
// Test skewed to inner dims blocks.
|
|
|
|
TensorBlockMapper skewed_block_mapper(
|
2019-12-19 04:07:00 +08:00
|
|
|
tensor_dims, {TensorBlockShapeType::kSkewedInnerDims, 100, zeroCost()});
|
2018-07-21 08:37:20 +08:00
|
|
|
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(skewed_block_mapper.blockCount(), 100);
|
|
|
|
VERIFY_IS_EQUAL(skewed_block_mapper.blockTotalSize(), 100);
|
2018-07-21 08:37:20 +08:00
|
|
|
|
|
|
|
// 1x100 (100x1) rows/cols depending on a tensor layout.
|
2019-12-11 03:58:30 +08:00
|
|
|
auto skewed_b0 = skewed_block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(skewed_b0.dimensions().at(0), choose(Layout, 100, 1));
|
|
|
|
VERIFY_IS_EQUAL(skewed_b0.dimensions().at(1), choose(Layout, 1, 100));
|
2018-07-21 08:37:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given a TensorBlock "visit" every element accessible though it, and a keep an
|
|
|
|
// index in the visited set. Verify that every coeff accessed only once.
|
2019-12-11 03:58:30 +08:00
|
|
|
template<int NumDims, int Layout>
|
2018-07-21 08:37:20 +08:00
|
|
|
static void UpdateCoeffSet(
|
2019-12-11 03:58:30 +08:00
|
|
|
const DSizes<Index, NumDims>& tensor_strides,
|
|
|
|
const internal::TensorBlockDescriptor<NumDims>& block,
|
2018-07-24 06:50:55 +08:00
|
|
|
Index first_coeff_index, int dim_index, std::set<Index>* visited_coeffs) {
|
2019-12-11 03:58:30 +08:00
|
|
|
const DSizes<Index, NumDims>& block_sizes = block.dimensions();
|
2018-07-21 08:37:20 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < block_sizes[dim_index]; ++i) {
|
|
|
|
if (tensor_strides[dim_index] == 1) {
|
2018-08-18 06:15:52 +08:00
|
|
|
typedef std::pair<std::set<Index>::iterator, bool> ReturnType;
|
|
|
|
ReturnType inserted = visited_coeffs->insert(first_coeff_index + i);
|
2018-07-21 08:37:20 +08:00
|
|
|
VERIFY_IS_EQUAL(inserted.second, true);
|
|
|
|
} else {
|
|
|
|
int next_dim_index = dim_index + choose(Layout, -1, 1);
|
2019-12-11 03:58:30 +08:00
|
|
|
UpdateCoeffSet<NumDims, Layout>(tensor_strides, block, first_coeff_index,
|
2018-07-21 08:37:20 +08:00
|
|
|
next_dim_index, visited_coeffs);
|
|
|
|
first_coeff_index += tensor_strides[dim_index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:17 +08:00
|
|
|
template <typename T, int NumDims, int Layout>
|
|
|
|
static void test_block_mapper_maps_every_element() {
|
2019-12-11 07:40:23 +08:00
|
|
|
typedef internal::TensorBlockMapper<NumDims, Layout> TensorBlockMapper;
|
2018-07-21 08:37:20 +08:00
|
|
|
|
2018-07-28 03:45:17 +08:00
|
|
|
DSizes<Index, NumDims> dims = RandomDims<NumDims>();
|
2019-12-11 03:58:30 +08:00
|
|
|
DSizes<Index, NumDims> strides = internal::strides<Layout>(dims);
|
2018-07-21 08:37:20 +08:00
|
|
|
|
|
|
|
// Keep track of elements indices available via block access.
|
|
|
|
std::set<Index> coeff_set;
|
|
|
|
|
|
|
|
// Try different combinations of block types and sizes.
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims, {RandomShape(), RandomTargetSize(dims), zeroCost()});
|
2018-07-21 08:37:20 +08:00
|
|
|
|
2019-12-11 03:58:30 +08:00
|
|
|
for (int i = 0; i < block_mapper.blockCount(); ++i) {
|
|
|
|
auto block = block_mapper.blockDescriptor(i);
|
|
|
|
UpdateCoeffSet<NumDims, Layout>(strides, block, block.offset(),
|
|
|
|
choose(Layout, NumDims - 1, 0),
|
|
|
|
&coeff_set);
|
2018-07-21 08:37:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that every coefficient in the original Tensor is accessible through
|
|
|
|
// TensorBlock only once.
|
2018-07-28 03:45:17 +08:00
|
|
|
Index total_coeffs = dims.TotalSize();
|
2018-08-25 04:58:55 +08:00
|
|
|
VERIFY_IS_EQUAL(Index(coeff_set.size()), total_coeffs);
|
2018-07-28 03:45:17 +08:00
|
|
|
VERIFY_IS_EQUAL(*coeff_set.begin(), 0);
|
|
|
|
VERIFY_IS_EQUAL(*coeff_set.rbegin(), total_coeffs - 1);
|
2018-07-21 08:37:20 +08:00
|
|
|
}
|
|
|
|
|
2018-07-24 06:50:55 +08:00
|
|
|
template <int Layout, int NumDims>
|
2018-08-25 04:58:55 +08:00
|
|
|
static Index GetInputIndex(Index output_index,
|
2018-07-24 06:50:55 +08:00
|
|
|
const array<Index, NumDims>& output_to_input_dim_map,
|
|
|
|
const array<Index, NumDims>& input_strides,
|
|
|
|
const array<Index, NumDims>& output_strides) {
|
|
|
|
int input_index = 0;
|
|
|
|
if (Layout == ColMajor) {
|
|
|
|
for (int i = NumDims - 1; i > 0; --i) {
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index idx = output_index / output_strides[i];
|
2018-07-24 06:50:55 +08:00
|
|
|
input_index += idx * input_strides[output_to_input_dim_map[i]];
|
|
|
|
output_index -= idx * output_strides[i];
|
|
|
|
}
|
|
|
|
return input_index +
|
|
|
|
output_index * input_strides[output_to_input_dim_map[0]];
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < NumDims - 1; ++i) {
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index idx = output_index / output_strides[i];
|
2018-07-24 06:50:55 +08:00
|
|
|
input_index += idx * input_strides[output_to_input_dim_map[i]];
|
|
|
|
output_index -= idx * output_strides[i];
|
|
|
|
}
|
|
|
|
return input_index +
|
|
|
|
output_index * input_strides[output_to_input_dim_map[NumDims - 1]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int Layout, int NumDims>
|
|
|
|
static array<Index, NumDims> ComputeStrides(
|
|
|
|
const array<Index, NumDims>& sizes) {
|
|
|
|
array<Index, NumDims> strides;
|
|
|
|
if (Layout == ColMajor) {
|
|
|
|
strides[0] = 1;
|
|
|
|
for (int i = 1; i < NumDims; ++i) {
|
|
|
|
strides[i] = strides[i - 1] * sizes[i - 1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
strides[NumDims - 1] = 1;
|
|
|
|
for (int i = NumDims - 2; i >= 0; --i) {
|
|
|
|
strides[i] = strides[i + 1] * sizes[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strides;
|
|
|
|
}
|
|
|
|
|
2018-08-25 17:53:28 +08:00
|
|
|
template<typename Scalar, typename StorageIndex, int Dim>
|
|
|
|
class EqualityChecker
|
|
|
|
{
|
|
|
|
const Scalar* input_data;
|
|
|
|
const DSizes<StorageIndex, Dim> &input_dims, &input_strides, &output_dims, &output_strides;
|
|
|
|
void check_recursive(const Scalar* input, const Scalar* output, int depth=0) const
|
|
|
|
{
|
|
|
|
if(depth==Dim)
|
|
|
|
{
|
|
|
|
VERIFY_IS_EQUAL(*input, *output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i=0; i<output_dims[depth]; ++i)
|
|
|
|
{
|
|
|
|
check_recursive(input + i % input_dims[depth] * input_strides[depth], output + i*output_strides[depth], depth+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
EqualityChecker(const Scalar* input_data_,
|
|
|
|
const DSizes<StorageIndex, Dim> &input_dims_, const DSizes<StorageIndex, Dim> &input_strides_,
|
|
|
|
const DSizes<StorageIndex, Dim> &output_dims_, const DSizes<StorageIndex, Dim> &output_strides_)
|
|
|
|
: input_data(input_data_)
|
|
|
|
, input_dims(input_dims_), input_strides(input_strides_)
|
|
|
|
, output_dims(output_dims_), output_strides(output_strides_)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void operator()(const Scalar* output_data) const
|
|
|
|
{
|
|
|
|
check_recursive(input_data, output_data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-24 06:50:55 +08:00
|
|
|
template <int Layout>
|
|
|
|
static void test_uniform_block_shape()
|
|
|
|
{
|
2019-12-11 03:58:30 +08:00
|
|
|
typedef internal::TensorBlockDescriptor<5> TensorBlock;
|
2019-12-11 07:40:23 +08:00
|
|
|
typedef internal::TensorBlockMapper<5, Layout> TensorBlockMapper;
|
2018-07-24 06:50:55 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
// Test shape 'UniformAllDims' with uniform 'max_coeff count'.
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 5 * 5 * 5 * 5 * 5;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
|
|
|
|
max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 0; i < 5; ++i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test shape 'UniformAllDims' with larger 'max_coeff count' which spills
|
|
|
|
// partially into first inner-most dimension.
|
|
|
|
if (Layout == ColMajor) {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 7 * 5 * 5 * 5 * 5;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
|
|
|
|
max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[0]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 1; i < 5; ++i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
} else {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 5 * 5 * 5 * 5 * 6;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
|
|
|
|
max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(6, block.dimensions()[4]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 3; i >= 0; --i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test shape 'UniformAllDims' with larger 'max_coeff count' which spills
|
|
|
|
// fully into first inner-most dimension.
|
|
|
|
if (Layout == ColMajor) {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 11 * 5 * 5 * 5 * 5;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
|
|
|
|
max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(11, block.dimensions()[0]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 1; i < 5; ++i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
} else {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 5 * 5 * 5 * 5 * 7;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
|
|
|
|
max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[4]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 3; i >= 0; --i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test shape 'UniformAllDims' with larger 'max_coeff count' which spills
|
|
|
|
// fully into first few inner-most dimensions.
|
|
|
|
if (Layout == ColMajor) {
|
|
|
|
DSizes<Index, 5> dims(7, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 7 * 5 * 6 * 7 * 5;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
|
|
|
|
max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[0]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[1]);
|
|
|
|
VERIFY_IS_EQUAL(6, block.dimensions()[2]);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[3]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[4]);
|
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
} else {
|
|
|
|
DSizes<Index, 5> dims(7, 5, 6, 9, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 5 * 5 * 5 * 6 * 7;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
|
|
|
|
max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[4]);
|
|
|
|
VERIFY_IS_EQUAL(6, block.dimensions()[3]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[2]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[1]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[0]);
|
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test shape 'UniformAllDims' with full allocation to all dims.
|
|
|
|
if (Layout == ColMajor) {
|
|
|
|
DSizes<Index, 5> dims(7, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 7 * 5 * 6 * 17 * 7;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
|
|
|
|
max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[0]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[1]);
|
|
|
|
VERIFY_IS_EQUAL(6, block.dimensions()[2]);
|
|
|
|
VERIFY_IS_EQUAL(17, block.dimensions()[3]);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[4]);
|
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
} else {
|
|
|
|
DSizes<Index, 5> dims(7, 5, 6, 9, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 7 * 5 * 6 * 9 * 7;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
|
|
|
|
max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[4]);
|
|
|
|
VERIFY_IS_EQUAL(9, block.dimensions()[3]);
|
|
|
|
VERIFY_IS_EQUAL(6, block.dimensions()[2]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[1]);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[0]);
|
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int Layout>
|
|
|
|
static void test_skewed_inner_dim_block_shape()
|
|
|
|
{
|
2019-12-11 03:58:30 +08:00
|
|
|
typedef internal::TensorBlockDescriptor<5> TensorBlock;
|
2019-12-11 07:40:23 +08:00
|
|
|
typedef internal::TensorBlockMapper<5, Layout> TensorBlockMapper;
|
2018-07-24 06:50:55 +08:00
|
|
|
|
|
|
|
// Test shape 'SkewedInnerDims' with partial allocation to inner-most dim.
|
|
|
|
if (Layout == ColMajor) {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 10 * 1 * 1 * 1 * 1;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims,
|
|
|
|
{TensorBlockShapeType::kSkewedInnerDims, max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(10, block.dimensions()[0]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 1; i < 5; ++i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(1, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
} else {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 1 * 1 * 1 * 1 * 6;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims,
|
|
|
|
{TensorBlockShapeType::kSkewedInnerDims, max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(6, block.dimensions()[4]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 3; i >= 0; --i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(1, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test shape 'SkewedInnerDims' with full allocation to inner-most dim.
|
|
|
|
if (Layout == ColMajor) {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 11 * 1 * 1 * 1 * 1;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims,
|
|
|
|
{TensorBlockShapeType::kSkewedInnerDims, max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(11, block.dimensions()[0]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 1; i < 5; ++i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(1, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
} else {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 1 * 1 * 1 * 1 * 7;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims,
|
|
|
|
{TensorBlockShapeType::kSkewedInnerDims, max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[4]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 3; i >= 0; --i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(1, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test shape 'SkewedInnerDims' with full allocation to inner-most dim,
|
|
|
|
// and partial allocation to second inner-dim.
|
|
|
|
if (Layout == ColMajor) {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 11 * 3 * 1 * 1 * 1;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims,
|
|
|
|
{TensorBlockShapeType::kSkewedInnerDims, max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(11, block.dimensions()[0]);
|
|
|
|
VERIFY_IS_EQUAL(3, block.dimensions()[1]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 2; i < 5; ++i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(1, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
} else {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 1 * 1 * 1 * 15 * 7;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims,
|
|
|
|
{TensorBlockShapeType::kSkewedInnerDims, max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[4]);
|
|
|
|
VERIFY_IS_EQUAL(15, block.dimensions()[3]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 2; i >= 0; --i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(1, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test shape 'SkewedInnerDims' with full allocation to inner-most dim,
|
|
|
|
// and partial allocation to third inner-dim.
|
|
|
|
if (Layout == ColMajor) {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 11 * 5 * 5 * 1 * 1;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims,
|
|
|
|
{TensorBlockShapeType::kSkewedInnerDims, max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(11, block.dimensions()[0]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[1]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[2]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 3; i < 5; ++i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(1, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
} else {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 1 * 1 * 5 * 17 * 7;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims,
|
|
|
|
{TensorBlockShapeType::kSkewedInnerDims, max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[4]);
|
|
|
|
VERIFY_IS_EQUAL(17, block.dimensions()[3]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[2]);
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int i = 1; i >= 0; --i) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(1, block.dimensions()[i]);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test shape 'SkewedInnerDims' with full allocation to all dims.
|
|
|
|
if (Layout == ColMajor) {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 11 * 5 * 6 * 17 * 7;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims,
|
|
|
|
{TensorBlockShapeType::kSkewedInnerDims, max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(11, block.dimensions()[0]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[1]);
|
|
|
|
VERIFY_IS_EQUAL(6, block.dimensions()[2]);
|
|
|
|
VERIFY_IS_EQUAL(17, block.dimensions()[3]);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[4]);
|
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
} else {
|
|
|
|
DSizes<Index, 5> dims(11, 5, 6, 17, 7);
|
2018-08-25 04:58:55 +08:00
|
|
|
const Index max_coeff_count = 11 * 5 * 6 * 17 * 7;
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims,
|
|
|
|
{TensorBlockShapeType::kSkewedInnerDims, max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
TensorBlock block = block_mapper.blockDescriptor(0);
|
|
|
|
VERIFY_IS_EQUAL(7, block.dimensions()[4]);
|
|
|
|
VERIFY_IS_EQUAL(17, block.dimensions()[3]);
|
|
|
|
VERIFY_IS_EQUAL(6, block.dimensions()[2]);
|
|
|
|
VERIFY_IS_EQUAL(5, block.dimensions()[1]);
|
|
|
|
VERIFY_IS_EQUAL(11, block.dimensions()[0]);
|
|
|
|
VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int Layout>
|
2019-12-11 07:40:23 +08:00
|
|
|
static void test_empty_dims(const internal::TensorBlockShapeType block_shape)
|
2018-07-24 06:50:55 +08:00
|
|
|
{
|
|
|
|
// Test blocking of tensors with zero dimensions:
|
|
|
|
// - we must not crash on asserts and divisions by zero
|
|
|
|
// - we must not return block with zero dimensions
|
|
|
|
// (recipe for overflows/underflows, divisions by zero and NaNs later)
|
|
|
|
// - total block count must be zero
|
|
|
|
{
|
2019-12-11 07:40:23 +08:00
|
|
|
typedef internal::TensorBlockMapper<1, Layout> TensorBlockMapper;
|
2019-12-11 03:58:30 +08:00
|
|
|
|
2018-07-24 06:50:55 +08:00
|
|
|
DSizes<Index, 1> dims(0);
|
2019-12-11 03:58:30 +08:00
|
|
|
for (size_t max_coeff_count = 0; max_coeff_count < 2; ++max_coeff_count) {
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims, {block_shape, max_coeff_count, zeroCost()});
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(block_mapper.blockCount(), 0);
|
|
|
|
VERIFY(block_mapper.blockTotalSize() >= 1);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-12-11 07:40:23 +08:00
|
|
|
typedef internal::TensorBlockMapper<2, Layout> TensorBlockMapper;
|
2019-12-11 03:58:30 +08:00
|
|
|
|
2018-07-24 06:50:55 +08:00
|
|
|
for (int dim1 = 0; dim1 < 3; ++dim1) {
|
|
|
|
for (int dim2 = 0; dim2 < 3; ++dim2) {
|
|
|
|
DSizes<Index, 2> dims(dim1, dim2);
|
2019-12-11 03:58:30 +08:00
|
|
|
for (size_t max_coeff_count = 0; max_coeff_count < 2; ++max_coeff_count) {
|
2019-12-19 04:07:00 +08:00
|
|
|
TensorBlockMapper block_mapper(
|
|
|
|
dims, {block_shape, max_coeff_count, zeroCost()});
|
2018-07-24 06:50:55 +08:00
|
|
|
if (dim1 * dim2 == 0) {
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY_IS_EQUAL(block_mapper.blockCount(), 0);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
2019-12-11 03:58:30 +08:00
|
|
|
VERIFY(block_mapper.blockTotalSize() >= 1);
|
2018-07-24 06:50:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:17 +08:00
|
|
|
#define TEST_LAYOUTS(NAME) \
|
2018-07-24 06:50:55 +08:00
|
|
|
CALL_SUBTEST(NAME<ColMajor>()); \
|
|
|
|
CALL_SUBTEST(NAME<RowMajor>())
|
|
|
|
|
2018-07-28 03:45:17 +08:00
|
|
|
#define TEST_LAYOUTS_AND_DIMS(TYPE, NAME) \
|
|
|
|
CALL_SUBTEST((NAME<TYPE, 1, ColMajor>())); \
|
|
|
|
CALL_SUBTEST((NAME<TYPE, 1, RowMajor>())); \
|
|
|
|
CALL_SUBTEST((NAME<TYPE, 2, ColMajor>())); \
|
|
|
|
CALL_SUBTEST((NAME<TYPE, 2, RowMajor>())); \
|
|
|
|
CALL_SUBTEST((NAME<TYPE, 3, ColMajor>())); \
|
|
|
|
CALL_SUBTEST((NAME<TYPE, 3, RowMajor>())); \
|
|
|
|
CALL_SUBTEST((NAME<TYPE, 4, ColMajor>())); \
|
|
|
|
CALL_SUBTEST((NAME<TYPE, 4, RowMajor>())); \
|
|
|
|
CALL_SUBTEST((NAME<TYPE, 5, ColMajor>())); \
|
|
|
|
CALL_SUBTEST((NAME<TYPE, 5, RowMajor>()))
|
|
|
|
|
|
|
|
#define TEST_LAYOUTS_WITH_ARG(NAME, ARG) \
|
2018-07-24 06:50:55 +08:00
|
|
|
CALL_SUBTEST(NAME<ColMajor>(ARG)); \
|
|
|
|
CALL_SUBTEST(NAME<RowMajor>(ARG))
|
|
|
|
|
2018-07-26 04:51:10 +08:00
|
|
|
EIGEN_DECLARE_TEST(cxx11_tensor_block_access) {
|
2018-07-28 03:45:17 +08:00
|
|
|
TEST_LAYOUTS(test_block_mapper_sanity);
|
|
|
|
TEST_LAYOUTS_AND_DIMS(float, test_block_mapper_maps_every_element);
|
|
|
|
TEST_LAYOUTS(test_uniform_block_shape);
|
|
|
|
TEST_LAYOUTS(test_skewed_inner_dim_block_shape);
|
2019-12-11 07:40:23 +08:00
|
|
|
TEST_LAYOUTS_WITH_ARG(test_empty_dims, TensorBlockShapeType::kUniformAllDims);
|
|
|
|
TEST_LAYOUTS_WITH_ARG(test_empty_dims, TensorBlockShapeType::kSkewedInnerDims);
|
2018-07-21 08:37:20 +08:00
|
|
|
}
|
2018-07-24 06:50:55 +08:00
|
|
|
|
2018-07-28 03:45:17 +08:00
|
|
|
#undef TEST_LAYOUTS
|
2018-08-15 05:26:59 +08:00
|
|
|
#undef TEST_LAYOUTS_WITH_ARG
|