Extend support for Packet16b:

* Add ptranspose<*,4> to support matmul and add unit test for Matrix<bool> * Matrix<bool>
* work around a bug in slicing of Tensor<bool>.
* Add tensor tests

This speeds up matmul for boolean matrices by about 10x

name                            old time/op             new time/op             delta
BM_MatMul<bool>/8                267ns ± 0%              479ns ± 0%  +79.25%          (p=0.008 n=5+5)
BM_MatMul<bool>/32              6.42µs ± 0%             0.87µs ± 0%  -86.50%          (p=0.008 n=5+5)
BM_MatMul<bool>/64              43.3µs ± 0%              5.9µs ± 0%  -86.42%          (p=0.008 n=5+5)
BM_MatMul<bool>/128              315µs ± 0%               44µs ± 0%  -85.98%          (p=0.008 n=5+5)
BM_MatMul<bool>/256             2.41ms ± 0%             0.34ms ± 0%  -85.68%          (p=0.008 n=5+5)
BM_MatMul<bool>/512             18.8ms ± 0%              2.7ms ± 0%  -85.53%          (p=0.008 n=5+5)
BM_MatMul<bool>/1k               149ms ± 0%               22ms ± 0%  -85.40%          (p=0.008 n=5+5)
This commit is contained in:
Rasmus Munk Larsen
2020-04-24 17:29:25 -07:00
parent b47c777993
commit ab773c7e91
10 changed files with 267 additions and 162 deletions

View File

@@ -64,7 +64,7 @@ static void test_static_reshape() {
#endif
}
template<typename>
template <typename>
static void test_reshape_in_expr() {
MatrixXf m1(2,3*5*7*11);
MatrixXf m2(3*5*7*11,13);
@@ -113,19 +113,19 @@ static void test_reshape_as_lvalue()
}
}
template<int DataLayout>
template<typename T, int DataLayout>
static void test_simple_slice()
{
Tensor<float, 5, DataLayout> tensor(2,3,5,7,11);
Tensor<T, 5, DataLayout> tensor(2,3,5,7,11);
tensor.setRandom();
Tensor<float, 5, DataLayout> slice1(1,1,1,1,1);
Tensor<T, 5, DataLayout> slice1(1,1,1,1,1);
Eigen::DSizes<ptrdiff_t, 5> indices(1,2,3,4,5);
Eigen::DSizes<ptrdiff_t, 5> sizes(1,1,1,1,1);
slice1 = tensor.slice(indices, sizes);
VERIFY_IS_EQUAL(slice1(0,0,0,0,0), tensor(1,2,3,4,5));
Tensor<float, 5, DataLayout> slice2(1,1,2,2,3);
Tensor<T, 5, DataLayout> slice2(1,1,2,2,3);
Eigen::DSizes<ptrdiff_t, 5> indices2(1,1,3,4,5);
Eigen::DSizes<ptrdiff_t, 5> sizes2(1,1,2,2,3);
slice2 = tensor.slice(indices2, sizes2);
@@ -138,20 +138,20 @@ static void test_simple_slice()
}
}
template<typename=void>
template<typename T>
static void test_const_slice()
{
const float b[1] = {42};
TensorMap<Tensor<const float, 1> > m(b, 1);
const T b[1] = {42};
TensorMap<Tensor<const T, 1> > m(b, 1);
DSizes<DenseIndex, 1> offsets;
offsets[0] = 0;
TensorRef<Tensor<const float, 1> > slice_ref(m.slice(offsets, m.dimensions()));
TensorRef<Tensor<const T, 1> > slice_ref(m.slice(offsets, m.dimensions()));
VERIFY_IS_EQUAL(slice_ref(0), 42);
}
template<int DataLayout>
template<typename T, int DataLayout>
static void test_slice_in_expr() {
typedef Matrix<float, Dynamic, Dynamic, DataLayout> Mtx;
typedef Matrix<T, Dynamic, Dynamic, DataLayout> Mtx;
Mtx m1(7,7);
Mtx m2(3,3);
m1.setRandom();
@@ -159,10 +159,10 @@ static void test_slice_in_expr() {
Mtx m3 = m1.block(1, 2, 3, 3) * m2.block(0, 2, 3, 1);
TensorMap<Tensor<float, 2, DataLayout>> tensor1(m1.data(), 7, 7);
TensorMap<Tensor<float, 2, DataLayout>> tensor2(m2.data(), 3, 3);
Tensor<float, 2, DataLayout> tensor3(3,1);
typedef Tensor<float, 1>::DimensionPair DimPair;
TensorMap<Tensor<T, 2, DataLayout>> tensor1(m1.data(), 7, 7);
TensorMap<Tensor<T, 2, DataLayout>> tensor2(m2.data(), 3, 3);
Tensor<T, 2, DataLayout> tensor3(3,1);
typedef typename Tensor<T, 1>::DimensionPair DimPair;
array<DimPair, 1> contract_along{{DimPair(1, 0)}};
Eigen::DSizes<ptrdiff_t, 2> indices1(1,2);
@@ -179,28 +179,28 @@ static void test_slice_in_expr() {
}
// Take an arbitrary slice of an arbitrarily sized tensor.
TensorMap<Tensor<const float, 2, DataLayout>> tensor4(m1.data(), 7, 7);
Tensor<float, 1, DataLayout> tensor6 = tensor4.reshape(DSizes<ptrdiff_t, 1>(7*7)).exp().slice(DSizes<ptrdiff_t, 1>(0), DSizes<ptrdiff_t, 1>(35));
TensorMap<Tensor<const T, 2, DataLayout>> tensor4(m1.data(), 7, 7);
Tensor<T, 1, DataLayout> tensor6 = tensor4.reshape(DSizes<ptrdiff_t, 1>(7*7)).exp().slice(DSizes<ptrdiff_t, 1>(0), DSizes<ptrdiff_t, 1>(35));
for (int i = 0; i < 35; ++i) {
VERIFY_IS_APPROX(tensor6(i), expf(tensor4.data()[i]));
}
}
template<int DataLayout>
template<typename T, int DataLayout>
static void test_slice_as_lvalue()
{
Tensor<float, 3, DataLayout> tensor1(2,2,7);
Tensor<T, 3, DataLayout> tensor1(2,2,7);
tensor1.setRandom();
Tensor<float, 3, DataLayout> tensor2(2,2,7);
Tensor<T, 3, DataLayout> tensor2(2,2,7);
tensor2.setRandom();
Tensor<float, 3, DataLayout> tensor3(4,3,5);
Tensor<T, 3, DataLayout> tensor3(4,3,5);
tensor3.setRandom();
Tensor<float, 3, DataLayout> tensor4(4,3,2);
Tensor<T, 3, DataLayout> tensor4(4,3,2);
tensor4.setRandom();
Tensor<float, 3, DataLayout> tensor5(10,13,12);
Tensor<T, 3, DataLayout> tensor5(10,13,12);
tensor5.setRandom();
Tensor<float, 3, DataLayout> result(4,5,7);
Tensor<T, 3, DataLayout> result(4,5,7);
Eigen::DSizes<ptrdiff_t, 3> sizes12(2,2,7);
Eigen::DSizes<ptrdiff_t, 3> first_slice(0,0,0);
result.slice(first_slice, sizes12) = tensor1;
@@ -246,10 +246,10 @@ static void test_slice_as_lvalue()
}
}
template<int DataLayout>
template<typename T, int DataLayout>
static void test_slice_raw_data()
{
Tensor<float, 4, DataLayout> tensor(3,5,7,11);
Tensor<T, 4, DataLayout> tensor(3,5,7,11);
tensor.setRandom();
Eigen::DSizes<ptrdiff_t, 4> offsets(1,2,3,4);
@@ -276,7 +276,7 @@ static void test_slice_raw_data()
extents = Eigen::DSizes<ptrdiff_t, 4>(1,2,1,1);
auto slice3 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice());
VERIFY_IS_EQUAL(slice3.dimensions().TotalSize(), 2);
VERIFY_IS_EQUAL(slice3.data(), static_cast<float*>(0));
VERIFY_IS_EQUAL(slice3.data(), static_cast<T*>(0));
if (DataLayout == ColMajor) {
offsets = Eigen::DSizes<ptrdiff_t, 4>(0,2,3,4);
@@ -341,15 +341,15 @@ static void test_slice_raw_data()
}
template<int DataLayout>
template<typename T, int DataLayout>
static void test_strided_slice()
{
typedef Tensor<float, 5, DataLayout> Tensor5f;
typedef Tensor<T, 5, DataLayout> Tensor5f;
typedef Eigen::DSizes<Eigen::DenseIndex, 5> Index5;
typedef Tensor<float, 2, DataLayout> Tensor2f;
typedef Tensor<T, 2, DataLayout> Tensor2f;
typedef Eigen::DSizes<Eigen::DenseIndex, 2> Index2;
Tensor<float, 5, DataLayout> tensor(2,3,5,7,11);
Tensor<float, 2, DataLayout> tensor2(7,11);
Tensor<T, 5, DataLayout> tensor(2,3,5,7,11);
Tensor<T, 2, DataLayout> tensor2(7,11);
tensor.setRandom();
tensor2.setRandom();
@@ -435,13 +435,13 @@ static void test_strided_slice()
}
}
template<int DataLayout>
template<typename T, int DataLayout>
static void test_strided_slice_write()
{
typedef Tensor<float, 2, DataLayout> Tensor2f;
typedef Tensor<T, 2, DataLayout> Tensor2f;
typedef Eigen::DSizes<Eigen::DenseIndex, 2> Index2;
Tensor<float, 2, DataLayout> tensor(7,11),tensor2(7,11);
Tensor<T, 2, DataLayout> tensor(7,11),tensor2(7,11);
tensor.setRandom();
tensor2=tensor;
Tensor2f slice(2,3);
@@ -461,15 +461,14 @@ static void test_strided_slice_write()
}
}
template<int DataLayout>
template<typename T, int DataLayout>
static void test_composition()
{
Eigen::Tensor<float, 2, DataLayout> matrix(7, 11);
Eigen::Tensor<T, 2, DataLayout> matrix(7, 11);
matrix.setRandom();
const DSizes<ptrdiff_t, 3> newDims(1, 1, 11);
Eigen::Tensor<float, 3, DataLayout> tensor =
Eigen::Tensor<T, 3, DataLayout> tensor =
matrix.slice(DSizes<ptrdiff_t, 2>(2, 0), DSizes<ptrdiff_t, 2>(1, 11)).reshape(newDims);
VERIFY_IS_EQUAL(tensor.dimensions().TotalSize(), 11);
@@ -481,29 +480,27 @@ static void test_composition()
}
}
#define CALL_SUBTEST_PART(PART) \
CALL_SUBTEST_##PART
#define CALL_SUBTESTS_TYPES_LAYOUTS(PART, NAME) \
CALL_SUBTEST_PART(PART)((NAME<float, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, RowMajor>()))
EIGEN_DECLARE_TEST(cxx11_tensor_morphing)
{
CALL_SUBTEST_1(test_simple_reshape<void>());
CALL_SUBTEST_1(test_static_reshape<void>());
CALL_SUBTEST_1(test_reshape_in_expr<void>());
CALL_SUBTEST_1(test_reshape_as_lvalue<void>());
CALL_SUBTEST_1(test_reshape_in_expr<void>());
CALL_SUBTEST_1(test_const_slice<float>());
CALL_SUBTEST_1(test_simple_slice<ColMajor>());
CALL_SUBTEST_1(test_simple_slice<RowMajor>());
CALL_SUBTEST_1(test_const_slice());
CALL_SUBTEST_2(test_slice_in_expr<ColMajor>());
CALL_SUBTEST_3(test_slice_in_expr<RowMajor>());
CALL_SUBTEST_4(test_slice_as_lvalue<ColMajor>());
CALL_SUBTEST_4(test_slice_as_lvalue<RowMajor>());
CALL_SUBTEST_5(test_slice_raw_data<ColMajor>());
CALL_SUBTEST_5(test_slice_raw_data<RowMajor>());
CALL_SUBTEST_6(test_strided_slice_write<ColMajor>());
CALL_SUBTEST_6(test_strided_slice<ColMajor>());
CALL_SUBTEST_6(test_strided_slice_write<RowMajor>());
CALL_SUBTEST_6(test_strided_slice<RowMajor>());
CALL_SUBTEST_7(test_composition<ColMajor>());
CALL_SUBTEST_7(test_composition<RowMajor>());
CALL_SUBTESTS_TYPES_LAYOUTS(2, test_simple_slice);
CALL_SUBTESTS_TYPES_LAYOUTS(3, test_slice_as_lvalue);
CALL_SUBTESTS_TYPES_LAYOUTS(4, test_slice_raw_data);
CALL_SUBTESTS_TYPES_LAYOUTS(5, test_strided_slice_write);
CALL_SUBTESTS_TYPES_LAYOUTS(6, test_strided_slice);
CALL_SUBTESTS_TYPES_LAYOUTS(7, test_composition);
}