diff options
author | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2016-02-23 05:28:02 +0000 |
---|---|---|
committer | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2016-02-23 05:28:02 +0000 |
commit | 8cb9bfab870c1f55ea9c69233a832e92c8de189d (patch) | |
tree | a06ca43a0b4e4404b45a41f6d92cef2e03153f60 /bench/tensors/tensor_benchmarks.h | |
parent | f442a5a5b34ede4ab4e8fe36d1c8237315ad3f04 (diff) |
Extended the tensor benchmark suite to support types other than floats
Diffstat (limited to 'bench/tensors/tensor_benchmarks.h')
-rw-r--r-- | bench/tensors/tensor_benchmarks.h | 100 |
1 files changed, 50 insertions, 50 deletions
diff --git a/bench/tensors/tensor_benchmarks.h b/bench/tensors/tensor_benchmarks.h index 688f558d0..b208a401a 100644 --- a/bench/tensors/tensor_benchmarks.h +++ b/bench/tensors/tensor_benchmarks.h @@ -15,7 +15,7 @@ using Eigen::TensorMap; // TODO(bsteiner): also templatize on the input type since we have users // for int8 as well as floats. -template <typename Device> class BenchmarkSuite { +template <typename Device, typename T> class BenchmarkSuite { public: BenchmarkSuite(const Device& device, size_t m, size_t k, size_t n) : m_(m), k_(k), n_(n), device_(device) { @@ -37,7 +37,7 @@ template <typename Device> class BenchmarkSuite { eigen_assert(m_ == k_ && k_ == n_); StartBenchmarkTiming(); for (int iter = 0; iter < num_iters; ++iter) { - device_.memcpy(c_, a_, m_ * m_ * sizeof(float)); + device_.memcpy(c_, a_, m_ * m_ * sizeof(T)); } // Record the number of values copied per second finalizeBenchmark(static_cast<int64_t>(m_) * m_ * num_iters); @@ -48,12 +48,12 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> sizes; sizes[0] = m_; sizes[1] = k_; - const TensorMap<Tensor<float, 2, 0, TensorIndex>, Eigen::Aligned> A(a_, sizes); + const TensorMap<Tensor<T, 2, 0, TensorIndex>, Eigen::Aligned> A(a_, sizes); TensorMap<Tensor<int, 2, 0, TensorIndex>, Eigen::Aligned> B((int*)b_, sizes); StartBenchmarkTiming(); for (int iter = 0; iter < num_iters; ++iter) { - B.device(device_) = A.cast<int>(); + B.device(device_) = A.template cast<int>(); } // Record the number of values copied per second finalizeBenchmark(static_cast<int64_t>(m_) * k_ * num_iters); @@ -64,7 +64,7 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> sizes; sizes[0] = m_; sizes[1] = m_; - TensorMap<Tensor<float, 2>, Eigen::Aligned> C(c_, sizes); + TensorMap<Tensor<T, 2>, Eigen::Aligned> C(c_, sizes); StartBenchmarkTiming(); for (int iter = 0; iter < num_iters; ++iter) { @@ -79,9 +79,9 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> sizes; sizes[0] = m_; sizes[1] = m_; - const TensorMap<Tensor<float, 2>, Eigen::Aligned> A(a_, sizes); - const TensorMap<Tensor<float, 2>, Eigen::Aligned> B(b_, sizes); - TensorMap<Tensor<float, 2>, Eigen::Aligned> C(c_, sizes); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> A(a_, sizes); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> B(b_, sizes); + TensorMap<Tensor<T, 2>, Eigen::Aligned> C(c_, sizes); const Eigen::DSizes<TensorIndex, 2> quarter_sizes(m_/2, m_/2); const Eigen::DSizes<TensorIndex, 2> first_quadrant(0, 0); @@ -109,10 +109,10 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> input_size; input_size[0] = k_; input_size[1] = n_; - const TensorMap<Tensor<float, 2, 0, TensorIndex>, Eigen::Aligned> B(b_, input_size); + const TensorMap<Tensor<T, 2, 0, TensorIndex>, Eigen::Aligned> B(b_, input_size); Eigen::array<TensorIndex, 1> output_size; output_size[0] = n_; - TensorMap<Tensor<float, 1, 0, TensorIndex>, Eigen::Aligned> C(c_, output_size); + TensorMap<Tensor<T, 1, 0, TensorIndex>, Eigen::Aligned> C(c_, output_size); StartBenchmarkTiming(); for (int iter = 0; iter < num_iters; ++iter) { @@ -126,10 +126,10 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> input_size; input_size[0] = k_; input_size[1] = n_; - const TensorMap<Tensor<float, 2, 0, TensorIndex>, Eigen::Aligned> B(b_, input_size); + const TensorMap<Tensor<T, 2, 0, TensorIndex>, Eigen::Aligned> B(b_, input_size); Eigen::array<TensorIndex, 1> output_size; output_size[0] = n_; - TensorMap<Tensor<float, 1, 0, TensorIndex>, Eigen::Aligned> C(c_, output_size); + TensorMap<Tensor<T, 1, 0, TensorIndex>, Eigen::Aligned> C(c_, output_size); StartBenchmarkTiming(); for (int iter = 0; iter < num_iters; ++iter) { @@ -144,11 +144,11 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> size_a; size_a[0] = m_; size_a[1] = k_; - const TensorMap<Tensor<float, 2>, Eigen::Aligned> A(a_, size_a); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> A(a_, size_a); Eigen::array<TensorIndex, 2> size_b; size_b[0] = k_; size_b[1] = m_; - TensorMap<Tensor<float, 2>, Eigen::Aligned> B(b_, size_b); + TensorMap<Tensor<T, 2>, Eigen::Aligned> B(b_, size_b); Eigen::array<int, 2> shuffle; shuffle[0] = 1; @@ -167,11 +167,11 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> size_a; size_a[0] = m_; size_a[1] = k_-3; - const TensorMap<Tensor<float, 2>, Eigen::Aligned> A(a_, size_a); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> A(a_, size_a); Eigen::array<TensorIndex, 2> size_b; size_b[0] = k_; size_b[1] = m_; - TensorMap<Tensor<float, 2>, Eigen::Aligned> B(b_, size_b); + TensorMap<Tensor<T, 2>, Eigen::Aligned> B(b_, size_b); Eigen::array<Eigen::IndexPair<TensorIndex>, 2> paddings; paddings[0] = Eigen::IndexPair<TensorIndex>(0, 0); @@ -190,11 +190,11 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> size_a; size_a[0] = m_; size_a[1] = k_; - const TensorMap<Tensor<float, 2>, Eigen::Aligned> A(a_, size_a); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> A(a_, size_a); Eigen::array<TensorIndex, 2> size_b; size_b[0] = m_; size_b[1] = k_/2; - TensorMap<Tensor<float, 2>, Eigen::Aligned> B(b_, size_b); + TensorMap<Tensor<T, 2>, Eigen::Aligned> B(b_, size_b); Eigen::array<TensorIndex, 2> strides; strides[0] = 1; @@ -212,11 +212,11 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> size_a; size_a[0] = m_; size_a[1] = 1; - const TensorMap<Tensor<float, 2>, Eigen::Aligned> A(a_, size_a); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> A(a_, size_a); Eigen::array<TensorIndex, 2> size_c; size_c[0] = m_; size_c[1] = n_; - TensorMap<Tensor<float, 2>, Eigen::Aligned> C(c_, size_c); + TensorMap<Tensor<T, 2>, Eigen::Aligned> C(c_, size_c); #ifndef EIGEN_HAS_INDEX_LIST Eigen::array<int, 2> broadcast; @@ -242,9 +242,9 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> sizes; sizes[0] = m_; sizes[1] = m_; - const TensorMap<Tensor<float, 2>, Eigen::Aligned> A(a_, sizes); - const TensorMap<Tensor<float, 2>, Eigen::Aligned> B(b_, sizes); - TensorMap<Tensor<float, 2>, Eigen::Aligned> C(c_, sizes); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> A(a_, sizes); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> B(b_, sizes); + TensorMap<Tensor<T, 2>, Eigen::Aligned> C(c_, sizes); StartBenchmarkTiming(); for (int iter = 0; iter < num_iters; ++iter) { @@ -260,9 +260,9 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> sizes; sizes[0] = m_; sizes[1] = m_; - const TensorMap<Tensor<float, 2>, Eigen::Aligned> A(a_, sizes); - const TensorMap<Tensor<float, 2>, Eigen::Aligned> B(b_, sizes); - TensorMap<Tensor<float, 2>, Eigen::Aligned> C(c_, sizes); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> A(a_, sizes); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> B(b_, sizes); + TensorMap<Tensor<T, 2>, Eigen::Aligned> C(c_, sizes); StartBenchmarkTiming(); for (int iter = 0; iter < num_iters; ++iter) { @@ -278,9 +278,9 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> sizes; sizes[0] = m_; sizes[1] = m_; - const TensorMap<Tensor<float, 2>, Eigen::Aligned> A(a_, sizes); - const TensorMap<Tensor<float, 2>, Eigen::Aligned> B(b_, sizes); - TensorMap<Tensor<float, 2>, Eigen::Aligned> C(c_, sizes); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> A(a_, sizes); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> B(b_, sizes); + TensorMap<Tensor<T, 2>, Eigen::Aligned> C(c_, sizes); StartBenchmarkTiming(); for (int iter = 0; iter < num_iters; ++iter) { @@ -296,9 +296,9 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> input_size; input_size[0] = k_; input_size[1] = n_; - const TensorMap<Tensor<float, 2, 0, TensorIndex>, Eigen::Aligned> B(b_, input_size); + const TensorMap<Tensor<T, 2, 0, TensorIndex>, Eigen::Aligned> B(b_, input_size); const Eigen::array<TensorIndex, 1> output_size = {{n_}}; - TensorMap<Tensor<float, 1, 0, TensorIndex>, Eigen::Aligned> C(c_, output_size); + TensorMap<Tensor<T, 1, 0, TensorIndex>, Eigen::Aligned> C(c_, output_size); #ifndef EIGEN_HAS_INDEX_LIST Eigen::array<TensorIndex, 1> sum_along_dim; @@ -323,10 +323,10 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> input_size; input_size[0] = k_; input_size[1] = n_; - const TensorMap<Tensor<float, 2, 0, TensorIndex>, Eigen::Aligned> B( + const TensorMap<Tensor<T, 2, 0, TensorIndex>, Eigen::Aligned> B( b_, input_size); const Eigen::array<TensorIndex, 1> output_size = {{k_}}; - TensorMap<Tensor<float, 1, 0, TensorIndex>, Eigen::Aligned> C( + TensorMap<Tensor<T, 1, 0, TensorIndex>, Eigen::Aligned> C( c_, output_size); #ifndef EIGEN_HAS_INDEX_LIST @@ -359,11 +359,11 @@ template <typename Device> class BenchmarkSuite { sizeC[0] = m_; sizeC[1] = n_; - const TensorMap<Tensor<float, 2>, Eigen::Aligned> A(a_, sizeA); - const TensorMap<Tensor<float, 2>, Eigen::Aligned> B(b_, sizeB); - TensorMap<Tensor<float, 2>, Eigen::Aligned> C(c_, sizeC); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> A(a_, sizeA); + const TensorMap<Tensor<T, 2>, Eigen::Aligned> B(b_, sizeB); + TensorMap<Tensor<T, 2>, Eigen::Aligned> C(c_, sizeC); - typedef typename Tensor<float, 2>::DimensionPair DimPair; + typedef typename Tensor<T, 2>::DimensionPair DimPair; Eigen::array<DimPair, 1> dims; dims[0] = DimPair(1, 0); @@ -380,16 +380,16 @@ template <typename Device> class BenchmarkSuite { Eigen::array<TensorIndex, 2> input_sizes; input_sizes[0] = m_; input_sizes[1] = n_; - TensorMap<Tensor<float, 2>, Eigen::Aligned> A(a_, input_sizes); + TensorMap<Tensor<T, 2>, Eigen::Aligned> A(a_, input_sizes); Eigen::array<TensorIndex, 2> kernel_sizes; kernel_sizes[0] = kernel_x; kernel_sizes[1] = kernel_y; - TensorMap<Tensor<float, 2>, Eigen::Aligned> B(b_, kernel_sizes); + TensorMap<Tensor<T, 2>, Eigen::Aligned> B(b_, kernel_sizes); Eigen::array<TensorIndex, 2> result_sizes; result_sizes[0] = m_ - kernel_x + 1; result_sizes[1] = n_ - kernel_y + 1; - TensorMap<Tensor<float, 2>, Eigen::Aligned> C(c_, result_sizes); - Eigen::array<Tensor<float, 2>::Index, 2> dims; + TensorMap<Tensor<T, 2>, Eigen::Aligned> C(c_, result_sizes); + Eigen::array<TensorIndex, 2> dims; dims[0] = 0; dims[1] = 1; @@ -405,15 +405,15 @@ template <typename Device> class BenchmarkSuite { private: void initialize() { - a_ = (float *) device_.allocate(m_ * k_ * sizeof(float)); - b_ = (float *) device_.allocate(k_ * n_ * sizeof(float)); - c_ = (float *) device_.allocate(m_ * n_ * sizeof(float)); + a_ = (T *) device_.allocate(m_ * k_ * sizeof(T)); + b_ = (T *) device_.allocate(k_ * n_ * sizeof(T)); + c_ = (T *) device_.allocate(m_ * n_ * sizeof(T)); // Initialize the content of the memory pools to prevent asan from // complaining. - device_.memset(a_, 12, m_ * k_ * sizeof(float)); - device_.memset(b_, 23, k_ * n_ * sizeof(float)); - device_.memset(c_, 31, m_ * n_ * sizeof(float)); + device_.memset(a_, 12, m_ * k_ * sizeof(T)); + device_.memset(b_, 23, k_ * n_ * sizeof(T)); + device_.memset(c_, 31, m_ * n_ * sizeof(T)); //BenchmarkUseRealTime(); } @@ -432,9 +432,9 @@ template <typename Device> class BenchmarkSuite { TensorIndex m_; TensorIndex k_; TensorIndex n_; - float* a_; - float* b_; - float* c_; + T* a_; + T* b_; + T* c_; Device device_; }; #endif // THIRD_PARTY_EIGEN3_TENSOR_BENCHMARKS_H_ |