Misc improvements and cleanups

This commit is contained in:
Benoit Steiner
2014-10-13 17:02:09 -07:00
parent 4c70b0a762
commit 99d75235a9
29 changed files with 779 additions and 140 deletions

View File

@@ -110,6 +110,7 @@ if(EIGEN_TEST_CXX11)
# ei_add_test(cxx11_tensor_fixed_size "-std=c++0x")
ei_add_test(cxx11_tensor_const "-std=c++0x")
ei_add_test(cxx11_tensor_of_const_values "-std=c++0x")
ei_add_test(cxx11_tensor_of_complex "-std=c++0x")
ei_add_test(cxx11_tensor_of_strings "-std=c++0x")
ei_add_test(cxx11_tensor_intdiv "-std=c++0x")
ei_add_test(cxx11_tensor_lvalue "-std=c++0x")

View File

@@ -253,6 +253,39 @@ static void test_auto_resize()
}
static void test_compound_assign()
{
Tensor<int, 1> start_tensor(10);
Tensor<int, 1> offset_tensor(10);
start_tensor.setRandom();
offset_tensor.setRandom();
Tensor<int, 1> tensor = start_tensor;
tensor += offset_tensor;
for (int i = 0; i < 10; ++i) {
VERIFY_IS_EQUAL(tensor(i), start_tensor(i) + offset_tensor(i));
}
tensor = start_tensor;
tensor -= offset_tensor;
for (int i = 0; i < 10; ++i) {
VERIFY_IS_EQUAL(tensor(i), start_tensor(i) - offset_tensor(i));
}
tensor = start_tensor;
tensor *= offset_tensor;
for (int i = 0; i < 10; ++i) {
VERIFY_IS_EQUAL(tensor(i), start_tensor(i) * offset_tensor(i));
}
tensor = start_tensor;
tensor /= offset_tensor;
for (int i = 0; i < 10; ++i) {
VERIFY_IS_EQUAL(tensor(i), start_tensor(i) / offset_tensor(i));
}
}
void test_cxx11_tensor_assign()
{
CALL_SUBTEST(test_1d());
@@ -260,5 +293,5 @@ void test_cxx11_tensor_assign()
CALL_SUBTEST(test_3d());
CALL_SUBTEST(test_same_type());
CALL_SUBTEST(test_auto_resize());
CALL_SUBTEST(test_compound_assign());
}

View File

@@ -64,8 +64,78 @@ static void test_expr()
}
static void test_modes() {
Tensor<float, 1> input(3);
Tensor<float, 1> kernel(3);
input(0) = 1.0f;
input(1) = 2.0f;
input(2) = 3.0f;
kernel(0) = 0.5f;
kernel(1) = 1.0f;
kernel(2) = 0.0f;
const Eigen::array<ptrdiff_t, 1> dims{{0}};
Eigen::array<std::pair<ptrdiff_t, ptrdiff_t>, 1> padding;
// Emulate VALID mode (as defined in
// http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html).
padding[0] = std::make_pair(0, 0);
Tensor<float, 1> valid(1);
valid = input.pad(padding).convolve(kernel, dims);
VERIFY_IS_EQUAL(valid.dimension(0), 1);
VERIFY_IS_APPROX(valid(0), 2.5f);
// Emulate SAME mode (as defined in
// http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html).
padding[0] = std::make_pair(1, 1);
Tensor<float, 1> same(3);
same = input.pad(padding).convolve(kernel, dims);
VERIFY_IS_EQUAL(same.dimension(0), 3);
VERIFY_IS_APPROX(same(0), 1.0f);
VERIFY_IS_APPROX(same(1), 2.5f);
VERIFY_IS_APPROX(same(2), 4.0f);
// Emulate FULL mode (as defined in
// http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html).
padding[0] = std::make_pair(2, 2);
Tensor<float, 1> full(5);
full = input.pad(padding).convolve(kernel, dims);
VERIFY_IS_EQUAL(full.dimension(0), 5);
VERIFY_IS_APPROX(full(0), 0.0f);
VERIFY_IS_APPROX(full(1), 1.0f);
VERIFY_IS_APPROX(full(2), 2.5f);
VERIFY_IS_APPROX(full(3), 4.0f);
VERIFY_IS_APPROX(full(4), 1.5f);
}
static void test_strides() {
Tensor<float, 1> input(13);
Tensor<float, 1> kernel(3);
input.setRandom();
kernel.setRandom();
const Eigen::array<ptrdiff_t, 1> dims{{0}};
const Eigen::array<ptrdiff_t, 1> stride_of_3{{3}};
const Eigen::array<ptrdiff_t, 1> stride_of_2{{2}};
Tensor<float, 1> result;
result = input.stride(stride_of_3).convolve(kernel, dims).stride(stride_of_2);
VERIFY_IS_EQUAL(result.dimension(0), 2);
VERIFY_IS_APPROX(result(0), (input(0)*kernel(0) + input(3)*kernel(1) +
input(6)*kernel(2)));
VERIFY_IS_APPROX(result(1), (input(6)*kernel(0) + input(9)*kernel(1) +
input(12)*kernel(2)));
}
void test_cxx11_tensor_convolution()
{
CALL_SUBTEST(test_evals());
CALL_SUBTEST(test_expr());
CALL_SUBTEST(test_modes());
CALL_SUBTEST(test_strides());
}

View File

@@ -123,6 +123,14 @@ static void test_forced_contextual_eval(Context* context)
context->out().device(context->device()) = (context->in1() + context->in2()).eval() * 3.14f + context->in1().constant(2.718f);
}
template <typename Context>
static void test_compound_assignment(Context* context)
{
context->out().device(context->device()) = context->in1().constant(2.718f);
context->out().device(context->device()) += context->in1() + context->in2() * 3.14f;
}
template <typename Context>
static void test_contraction(Context* context)
{
@@ -197,6 +205,15 @@ static void test_cpu() {
}
}
test_compound_assignment(&context);
for (int i = 0; i < 40; ++i) {
for (int j = 0; j < 50; ++j) {
for (int k = 0; k < 70; ++k) {
VERIFY_IS_APPROX(out(Eigen::array<int, 3>(i,j,k)), in1(Eigen::array<int, 3>(i,j,k)) + in2(Eigen::array<int, 3>(i,j,k)) * 3.14f + 2.718f);
}
}
}
test_contraction(&context);
for (int i = 0; i < 40; ++i) {
for (int j = 0; j < 40; ++j) {
@@ -299,6 +316,16 @@ static void test_gpu() {
}
}
test_compound_assignment(&context);
assert(cudaMemcpy(out.data(), d_out, out_bytes, cudaMemcpyDeviceToHost) == cudaSuccess);
for (int i = 0; i < 40; ++i) {
for (int j = 0; j < 50; ++j) {
for (int k = 0; k < 70; ++k) {
VERIFY_IS_APPROX(out(Eigen::array<int, 3>(i,j,k)), in1(Eigen::array<int, 3>(i,j,k)) + in2(Eigen::array<int, 3>(i,j,k)) * 3.14f + 2.718f);
}
}
}
test_contraction(&context);
assert(cudaMemcpy(out.data(), d_out, out_bytes, cudaMemcpyDeviceToHost) == cudaSuccess);
for (int i = 0; i < 40; ++i) {

View File

@@ -12,6 +12,7 @@
#include <Eigen/CXX11/Tensor>
using Eigen::Tensor;
using Eigen::IndexPair;
static void test_simple_reshape()
{
@@ -52,7 +53,7 @@ static void test_reshape_in_expr() {
TensorMap<Tensor<float, 5>> tensor2(m2.data(), 3,5,7,11,13);
Tensor<float, 2>::Dimensions newDims1{{2,3*5*7*11}};
Tensor<float, 2>::Dimensions newDims2{{3*5*7*11,13}};
array<Tensor<float, 1>::DimensionPair, 1> contract_along{{std::make_pair(1, 0)}};
Eigen::array<IndexPair<DenseIndex>, 1> contract_along{{IndexPair<DenseIndex>(1, 0)}};
Tensor<float, 2> tensor3(2,13);
tensor3 = tensor1.reshape(newDims1).contract(tensor2.reshape(newDims2), contract_along);
@@ -125,7 +126,7 @@ static void test_slice_in_expr() {
TensorMap<Tensor<float, 2>> tensor1(m1.data(), 7, 7);
TensorMap<Tensor<float, 2>> tensor2(m2.data(), 3, 3);
Tensor<float, 2> tensor3(3,1);
array<Tensor<float, 1>::DimensionPair, 1> contract_along{{std::make_pair(1, 0)}};
array<IndexPair<DenseIndex>, 1> contract_along{{IndexPair<DenseIndex>(1, 0)}};
Eigen::DSizes<ptrdiff_t, 2> indices1(1,2);
Eigen::DSizes<ptrdiff_t, 2> sizes1(3,3);

View File

@@ -0,0 +1,64 @@
// 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/.
#include "main.h"
#include <Eigen/CXX11/Tensor>
using Eigen::Tensor;
using Eigen::TensorMap;
static void test_additions()
{
Tensor<std::complex<float>, 1> data1(3);
Tensor<std::complex<float>, 1> data2(3);
for (int i = 0; i < 3; ++i) {
data1(i) = std::complex<float>(i, -i);
data2(i) = std::complex<float>(i, 7 * i);
}
Tensor<std::complex<float>, 1> sum = data1 + data2;
for (int i = 0; i < 3; ++i) {
VERIFY_IS_EQUAL(sum(i), std::complex<float>(2*i, 6*i));
}
}
static void test_contractions()
{
Tensor<std::complex<float>, 4> t_left(30, 50, 8, 31);
Tensor<std::complex<float>, 5> t_right(8, 31, 7, 20, 10);
Tensor<std::complex<float>, 5> t_result(30, 50, 7, 20, 10);
t_left.setRandom();
t_right.setRandom();
typedef Map<Matrix<std::complex<float>, Dynamic, Dynamic>> MapXcf;
MapXcf m_left(t_left.data(), 1500, 248);
MapXcf m_right(t_right.data(), 248, 1400);
Matrix<std::complex<float>, Dynamic, Dynamic> m_result(1500, 1400);
// This contraction should be equivalent to a regular matrix multiplication
typedef Tensor<float, 1>::DimensionPair DimPair;
Eigen::array<DimPair, 2> dims({{DimPair(2, 0), DimPair(3, 1)}});
t_result = t_left.contract(t_right, dims);
m_result = m_left * m_right;
for (size_t i = 0; i < t_result.dimensions().TotalSize(); i++) {
VERIFY_IS_APPROX(t_result.data()[i], m_result.data()[i]);
}
}
void test_cxx11_tensor_of_complex()
{
CALL_SUBTEST(test_additions());
CALL_SUBTEST(test_contractions());
}

View File

@@ -9,22 +9,23 @@
#define EIGEN_USE_THREADS
#include <iostream>
#include "main.h"
#include <Eigen/CXX11/Tensor>
using Eigen::Tensor;
void test_cxx11_tensor_thread_pool()
static void test_multithread_elementwise()
{
Eigen::Tensor<float, 3> in1(2,3,7);
Eigen::Tensor<float, 3> in2(2,3,7);
Eigen::Tensor<float, 3> out(2,3,7);
Tensor<float, 3> in1(2,3,7);
Tensor<float, 3> in2(2,3,7);
Tensor<float, 3> out(2,3,7);
in1.setRandom();
in2.setRandom();
Eigen::ThreadPoolDevice thread_pool_device(3);
Eigen::ThreadPoolDevice thread_pool_device(internal::random<int>(3, 11));
out.device(thread_pool_device) = in1 + in2 * 3.14f;
for (int i = 0; i < 2; ++i) {
@@ -35,3 +36,222 @@ void test_cxx11_tensor_thread_pool()
}
}
}
static void test_multithread_compound_assignment()
{
Tensor<float, 3> in1(2,3,7);
Tensor<float, 3> in2(2,3,7);
Tensor<float, 3> out(2,3,7);
in1.setRandom();
in2.setRandom();
Eigen::ThreadPoolDevice thread_pool_device(internal::random<int>(3, 11));
out.device(thread_pool_device) = in1;
out.device(thread_pool_device) += in2 * 3.14f;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 7; ++k) {
VERIFY_IS_APPROX(out(i,j,k), in1(i,j,k) + in2(i,j,k) * 3.14f);
}
}
}
}
static void test_multithread_contraction()
{
Tensor<float, 4> t_left(30, 50, 37, 31);
Tensor<float, 5> t_right(37, 31, 70, 2, 10);
Tensor<float, 5> t_result(30, 50, 70, 2, 10);
t_left.setRandom();
t_right.setRandom();
// this contraction should be equivalent to a single matrix multiplication
typedef Tensor<float, 1>::DimensionPair DimPair;
Eigen::array<DimPair, 2> dims({{DimPair(2, 0), DimPair(3, 1)}});
typedef Map<MatrixXf> MapXf;
MapXf m_left(t_left.data(), 1500, 1147);
MapXf m_right(t_right.data(), 1147, 1400);
MatrixXf m_result(1500, 1400);
Eigen::ThreadPoolDevice thread_pool_device(4);
// compute results by separate methods
t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
m_result = m_left * m_right;
for (ptrdiff_t i = 0; i < t_result.size(); i++) {
VERIFY(&t_result.data()[i] != &m_result.data()[i]);
if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
std::cout << "mismatch detected: " << t_result.data()[i] << " vs " << m_result.data()[i] << std::endl;
assert(false);
}
}
}
static void test_contraction_corner_cases()
{
Tensor<float, 2> t_left(32, 500);
Tensor<float, 2> t_right(32, 28*28);
Tensor<float, 2> t_result(500, 28*28);
t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
t_right = (t_right.constant(-0.6f) + t_right.random()) * 2.0f;
t_result = t_result.constant(NAN);
// this contraction should be equivalent to a single matrix multiplication
typedef Tensor<float, 1>::DimensionPair DimPair;
Eigen::array<DimPair, 1> dims{{DimPair(0, 0)}};
typedef Map<MatrixXf> MapXf;
MapXf m_left(t_left.data(), 32, 500);
MapXf m_right(t_right.data(), 32, 28*28);
MatrixXf m_result(500, 28*28);
Eigen::ThreadPoolDevice thread_pool_device(12);
// compute results by separate methods
t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
m_result = m_left.transpose() * m_right;
for (ptrdiff_t i = 0; i < t_result.size(); i++) {
assert(!isnan(t_result.data()[i]));
if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
std::cout << "mismatch detected at index " << i << " : " << t_result.data()[i] << " vs " << m_result.data()[i] << std::endl;
assert(false);
}
}
t_left.resize(32, 1);
t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
t_result.resize (1, 28*28);
t_result = t_result.constant(NAN);
t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
new(&m_left) MapXf(t_left.data(), 32, 1);
m_result = m_left.transpose() * m_right;
for (ptrdiff_t i = 0; i < t_result.size(); i++) {
assert(!isnan(t_result.data()[i]));
if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
std::cout << "mismatch detected: " << t_result.data()[i] << " vs " << m_result.data()[i] << std::endl;
assert(false);
}
}
t_left.resize(32, 500);
t_right.resize(32, 4);
t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
t_right = (t_right.constant(-0.6f) + t_right.random()) * 2.0f;
t_result.resize (500, 4);
t_result = t_result.constant(NAN);
t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
new(&m_left) MapXf(t_left.data(), 32, 500);
new(&m_right) MapXf(t_right.data(), 32, 4);
m_result = m_left.transpose() * m_right;
for (ptrdiff_t i = 0; i < t_result.size(); i++) {
assert(!isnan(t_result.data()[i]));
if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
std::cout << "mismatch detected: " << t_result.data()[i] << " vs " << m_result.data()[i] << std::endl;
assert(false);
}
}
t_left.resize(32, 1);
t_right.resize(32, 4);
t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
t_right = (t_right.constant(-0.6f) + t_right.random()) * 2.0f;
t_result.resize (1, 4);
t_result = t_result.constant(NAN);
t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
new(&m_left) MapXf(t_left.data(), 32, 1);
new(&m_right) MapXf(t_right.data(), 32, 4);
m_result = m_left.transpose() * m_right;
for (ptrdiff_t i = 0; i < t_result.size(); i++) {
assert(!isnan(t_result.data()[i]));
if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
std::cout << "mismatch detected: " << t_result.data()[i] << " vs " << m_result.data()[i] << std::endl;
assert(false);
}
}
}
static void test_multithread_contraction_agrees_with_singlethread() {
int contract_size = internal::random<int>(1, 5000);
Tensor<float, 3> left(internal::random<int>(1, 80),
contract_size,
internal::random<int>(1, 100));
Tensor<float, 4> right(internal::random<int>(1, 25),
internal::random<int>(1, 37),
contract_size,
internal::random<int>(1, 51));
left.setRandom();
right.setRandom();
// add constants to shift values away from 0 for more precision
left += left.constant(1.5f);
right += right.constant(1.5f);
typedef Tensor<float, 1>::DimensionPair DimPair;
Eigen::array<DimPair, 1> dims({{DimPair(1, 2)}});
Eigen::ThreadPoolDevice thread_pool_device(internal::random<int>(2, 11));
Tensor<float, 5> st_result;
st_result = left.contract(right, dims);
Tensor<float, 5> tp_result(st_result.dimensions());
tp_result.device(thread_pool_device) = left.contract(right, dims);
VERIFY(internal::dimensions_match(st_result.dimensions(), tp_result.dimensions()));
for (ptrdiff_t i = 0; i < st_result.size(); i++) {
// if both of the values are very small, then do nothing (because the test will fail
// due to numerical precision issues when values are small)
if (fabs(st_result.data()[i] - tp_result.data()[i]) >= 1e-4) {
VERIFY_IS_APPROX(st_result.data()[i], tp_result.data()[i]);
}
}
}
static void test_memcpy() {
for (int i = 0; i < 5; ++i) {
const int num_threads = internal::random<int>(3, 11);
Eigen::ThreadPoolDevice thread_pool_device(num_threads);
const int size = internal::random<int>(13, 7632);
Tensor<float, 1> t1(size);
t1.setRandom();
std::vector<float> result(size);
thread_pool_device.memcpy(&result[0], t1.data(), size*sizeof(float));
for (int i = 0; i < size; i++) {
VERIFY_IS_EQUAL(t1(i), result[i]);
}
}
}
void test_cxx11_tensor_thread_pool()
{
CALL_SUBTEST(test_multithread_elementwise());
CALL_SUBTEST(test_multithread_compound_assignment());
CALL_SUBTEST(test_multithread_contraction());
CALL_SUBTEST(test_multithread_contraction_agrees_with_singlethread());
// Exercise various cases that have been problematic in the past.
CALL_SUBTEST(test_contraction_corner_cases());
CALL_SUBTEST(test_memcpy());
}