• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 
12 #include <Eigen/CXX11/Tensor>
13 
14 using Eigen::Tensor;
15 using Eigen::DefaultDevice;
16 
17 template <int DataLayout>
test_evals()18 static void test_evals()
19 {
20   Tensor<float, 2, DataLayout> input(3, 3);
21   Tensor<float, 1, DataLayout> kernel(2);
22 
23   input.setRandom();
24   kernel.setRandom();
25 
26   Tensor<float, 2, DataLayout> result(2,3);
27   result.setZero();
28   Eigen::array<Tensor<float, 2>::Index, 1> dims3{{0}};
29 
30   typedef TensorEvaluator<decltype(input.convolve(kernel, dims3)), DefaultDevice> Evaluator;
31   Evaluator eval(input.convolve(kernel, dims3), DefaultDevice());
32   eval.evalTo(result.data());
33   EIGEN_STATIC_ASSERT(Evaluator::NumDims==2ul, YOU_MADE_A_PROGRAMMING_MISTAKE);
34   VERIFY_IS_EQUAL(eval.dimensions()[0], 2);
35   VERIFY_IS_EQUAL(eval.dimensions()[1], 3);
36 
37   VERIFY_IS_APPROX(result(0,0), input(0,0)*kernel(0) + input(1,0)*kernel(1));  // index 0
38   VERIFY_IS_APPROX(result(0,1), input(0,1)*kernel(0) + input(1,1)*kernel(1));  // index 2
39   VERIFY_IS_APPROX(result(0,2), input(0,2)*kernel(0) + input(1,2)*kernel(1));  // index 4
40   VERIFY_IS_APPROX(result(1,0), input(1,0)*kernel(0) + input(2,0)*kernel(1));  // index 1
41   VERIFY_IS_APPROX(result(1,1), input(1,1)*kernel(0) + input(2,1)*kernel(1));  // index 3
42   VERIFY_IS_APPROX(result(1,2), input(1,2)*kernel(0) + input(2,2)*kernel(1));  // index 5
43 }
44 
45 template <int DataLayout>
test_expr()46 static void test_expr()
47 {
48   Tensor<float, 2, DataLayout> input(3, 3);
49   Tensor<float, 2, DataLayout> kernel(2, 2);
50   input.setRandom();
51   kernel.setRandom();
52 
53   Tensor<float, 2, DataLayout> result(2,2);
54   Eigen::array<ptrdiff_t, 2> dims;
55   dims[0] = 0;
56   dims[1] = 1;
57   result = input.convolve(kernel, dims);
58 
59   VERIFY_IS_APPROX(result(0,0), input(0,0)*kernel(0,0) + input(0,1)*kernel(0,1) +
60                                 input(1,0)*kernel(1,0) + input(1,1)*kernel(1,1));
61   VERIFY_IS_APPROX(result(0,1), input(0,1)*kernel(0,0) + input(0,2)*kernel(0,1) +
62                                 input(1,1)*kernel(1,0) + input(1,2)*kernel(1,1));
63   VERIFY_IS_APPROX(result(1,0), input(1,0)*kernel(0,0) + input(1,1)*kernel(0,1) +
64                                 input(2,0)*kernel(1,0) + input(2,1)*kernel(1,1));
65   VERIFY_IS_APPROX(result(1,1), input(1,1)*kernel(0,0) + input(1,2)*kernel(0,1) +
66                                 input(2,1)*kernel(1,0) + input(2,2)*kernel(1,1));
67 }
68 
69 template <int DataLayout>
test_modes()70 static void test_modes() {
71   Tensor<float, 1, DataLayout> input(3);
72   Tensor<float, 1, DataLayout> kernel(3);
73   input(0) = 1.0f;
74   input(1) = 2.0f;
75   input(2) = 3.0f;
76   kernel(0) = 0.5f;
77   kernel(1) = 1.0f;
78   kernel(2) = 0.0f;
79 
80   Eigen::array<ptrdiff_t, 1> dims;
81   dims[0] = 0;
82   Eigen::array<std::pair<ptrdiff_t, ptrdiff_t>, 1> padding;
83 
84   // Emulate VALID mode (as defined in
85   // http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html).
86   padding[0] = std::make_pair(0, 0);
87   Tensor<float, 1, DataLayout> valid(1);
88   valid = input.pad(padding).convolve(kernel, dims);
89   VERIFY_IS_EQUAL(valid.dimension(0), 1);
90   VERIFY_IS_APPROX(valid(0), 2.5f);
91 
92   // Emulate SAME mode (as defined in
93   // http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html).
94   padding[0] = std::make_pair(1, 1);
95   Tensor<float, 1, DataLayout> same(3);
96   same = input.pad(padding).convolve(kernel, dims);
97   VERIFY_IS_EQUAL(same.dimension(0), 3);
98   VERIFY_IS_APPROX(same(0), 1.0f);
99   VERIFY_IS_APPROX(same(1), 2.5f);
100   VERIFY_IS_APPROX(same(2), 4.0f);
101 
102   // Emulate FULL mode (as defined in
103   // http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html).
104   padding[0] = std::make_pair(2, 2);
105   Tensor<float, 1, DataLayout> full(5);
106   full = input.pad(padding).convolve(kernel, dims);
107   VERIFY_IS_EQUAL(full.dimension(0), 5);
108   VERIFY_IS_APPROX(full(0), 0.0f);
109   VERIFY_IS_APPROX(full(1), 1.0f);
110   VERIFY_IS_APPROX(full(2), 2.5f);
111   VERIFY_IS_APPROX(full(3), 4.0f);
112   VERIFY_IS_APPROX(full(4), 1.5f);
113 }
114 
115 template <int DataLayout>
test_strides()116 static void test_strides() {
117   Tensor<float, 1, DataLayout> input(13);
118   Tensor<float, 1, DataLayout> kernel(3);
119   input.setRandom();
120   kernel.setRandom();
121 
122   Eigen::array<ptrdiff_t, 1> dims;
123   dims[0] = 0;
124   Eigen::array<ptrdiff_t, 1> stride_of_3;
125   stride_of_3[0] = 3;
126   Eigen::array<ptrdiff_t, 1> stride_of_2;
127   stride_of_2[0] = 2;
128 
129   Tensor<float, 1, DataLayout> result;
130   result = input.stride(stride_of_3).convolve(kernel, dims).stride(stride_of_2);
131 
132   VERIFY_IS_EQUAL(result.dimension(0), 2);
133   VERIFY_IS_APPROX(result(0), (input(0)*kernel(0) + input(3)*kernel(1) +
134                                input(6)*kernel(2)));
135   VERIFY_IS_APPROX(result(1), (input(6)*kernel(0) + input(9)*kernel(1) +
136                                input(12)*kernel(2)));
137 }
138 
test_cxx11_tensor_convolution()139 void test_cxx11_tensor_convolution()
140 {
141   CALL_SUBTEST(test_evals<ColMajor>());
142   CALL_SUBTEST(test_evals<RowMajor>());
143   CALL_SUBTEST(test_expr<ColMajor>());
144   CALL_SUBTEST(test_expr<RowMajor>());
145   CALL_SUBTEST(test_modes<ColMajor>());
146   CALL_SUBTEST(test_modes<RowMajor>());
147   CALL_SUBTEST(test_strides<ColMajor>());
148   CALL_SUBTEST(test_strides<RowMajor>());
149 }
150