You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
11 KiB

  1. #ifndef THIRD_PARTY_EIGEN3_TENSOR_BENCHMARKS_H_
  2. #define THIRD_PARTY_EIGEN3_TENSOR_BENCHMARKS_H_
  3. typedef int TensorIndex;
  4. #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int
  5. #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
  6. #include "testing/base/public/benchmark.h"
  7. using StormEigen::Tensor;
  8. using StormEigen::TensorMap;
  9. // TODO(bsteiner): also templatize on the input type since we have users
  10. // for int8 as well as floats.
  11. template <typename Device> class BenchmarkSuite {
  12. public:
  13. BenchmarkSuite(const Device& device, size_t m, size_t k, size_t n)
  14. : m_(m), k_(k), n_(n), device_(device) {
  15. initialize();
  16. }
  17. BenchmarkSuite(const Device& device, size_t m)
  18. : m_(m), k_(m), n_(m), device_(device) {
  19. initialize();
  20. }
  21. ~BenchmarkSuite() {
  22. device_.deallocate(a_);
  23. device_.deallocate(b_);
  24. device_.deallocate(c_);
  25. }
  26. void memcpy(int num_iters) {
  27. eigen_assert(m_ == k_ && k_ == n_);
  28. StartBenchmarkTiming();
  29. for (int iter = 0; iter < num_iters; ++iter) {
  30. device_.memcpy(c_, a_, m_ * m_ * sizeof(float));
  31. }
  32. // Record the number of values copied per second
  33. finalizeBenchmark(m_ * m_ * num_iters);
  34. }
  35. void random(int num_iters) {
  36. eigen_assert(m_ == k_ && k_ == n_);
  37. const StormEigen::array<TensorIndex, 2> sizes(m_, m_);
  38. TensorMap<Tensor<float, 2>, StormEigen::Aligned> C(c_, sizes);
  39. StartBenchmarkTiming();
  40. for (int iter = 0; iter < num_iters; ++iter) {
  41. C.device(device_) = C.random();
  42. }
  43. // Record the number of random numbers generated per second
  44. finalizeBenchmark(m_ * m_ * num_iters);
  45. }
  46. void slicing(int num_iters) {
  47. eigen_assert(m_ == k_ && k_ == n_);
  48. const StormEigen::array<TensorIndex, 2> sizes(m_, m_);
  49. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> A(a_, sizes);
  50. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> B(b_, sizes);
  51. TensorMap<Tensor<float, 2>, StormEigen::Aligned> C(c_, sizes);
  52. const StormEigen::DSizes<TensorIndex, 2> quarter_sizes(StormEigen::array<TensorIndex, 2>(m_/2, m_/2));
  53. const StormEigen::DSizes<TensorIndex, 2> first_quadrant(StormEigen::array<TensorIndex, 2>(0, 0));
  54. const StormEigen::DSizes<TensorIndex, 2> second_quadrant(StormEigen::array<TensorIndex, 2>(0, m_/2));
  55. const StormEigen::DSizes<TensorIndex, 2> third_quadrant(StormEigen::array<TensorIndex, 2>(m_/2, 0));
  56. const StormEigen::DSizes<TensorIndex, 2> fourth_quadrant(StormEigen::array<TensorIndex, 2>(m_/2, m_/2));
  57. StartBenchmarkTiming();
  58. for (int iter = 0; iter < num_iters; ++iter) {
  59. C.slice(first_quadrant, quarter_sizes).device(device_) =
  60. A.slice(first_quadrant, quarter_sizes);
  61. C.slice(second_quadrant, quarter_sizes).device(device_) =
  62. B.slice(second_quadrant, quarter_sizes);
  63. C.slice(third_quadrant, quarter_sizes).device(device_) =
  64. A.slice(third_quadrant, quarter_sizes);
  65. C.slice(fourth_quadrant, quarter_sizes).device(device_) =
  66. B.slice(fourth_quadrant, quarter_sizes);
  67. }
  68. // Record the number of values copied from the rhs slice to the lhs slice
  69. // each second
  70. finalizeBenchmark(m_ * m_ * num_iters);
  71. }
  72. void shuffling(int num_iters) {
  73. eigen_assert(m_ == n_);
  74. const StormEigen::array<TensorIndex, 2> size_a(m_, k_);
  75. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> A(a_, size_a);
  76. const StormEigen::array<TensorIndex, 2> size_b(k_, m_);
  77. TensorMap<Tensor<float, 2>, StormEigen::Aligned> B(b_, size_b);
  78. const StormEigen::array<int, 2> shuffle(1, 0);
  79. StartBenchmarkTiming();
  80. for (int iter = 0; iter < num_iters; ++iter) {
  81. B.device(device_) = A.shuffle(shuffle);
  82. }
  83. // Record the number of values shuffled from A and copied to B each second
  84. finalizeBenchmark(m_ * k_ * num_iters);
  85. }
  86. void padding(int num_iters) {
  87. eigen_assert(m_ == k_);
  88. const StormEigen::array<TensorIndex, 2> size_a(m_, k_-3);
  89. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> A(a_, size_a);
  90. const StormEigen::array<TensorIndex, 2> size_b(k_, m_);
  91. TensorMap<Tensor<float, 2>, StormEigen::Aligned> B(b_, size_b);
  92. StormEigen::array<StormEigen::IndexPair<TensorIndex>, 2> paddings;
  93. paddings[0] = StormEigen::IndexPair<TensorIndex>(0, 0);
  94. paddings[1] = StormEigen::IndexPair<TensorIndex>(2, 1);
  95. StartBenchmarkTiming();
  96. for (int iter = 0; iter < num_iters; ++iter) {
  97. B.device(device_) = A.pad(paddings);
  98. }
  99. // Record the number of values copied from the padded tensor A each second
  100. finalizeBenchmark(m_ * k_ * num_iters);
  101. }
  102. void striding(int num_iters) {
  103. eigen_assert(m_ == k_);
  104. const StormEigen::array<TensorIndex, 2> size_a(m_, k_);
  105. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> A(a_, size_a);
  106. const StormEigen::array<TensorIndex, 2> size_b(m_, k_ / 2);
  107. TensorMap<Tensor<float, 2>, StormEigen::Aligned> B(b_, size_b);
  108. const StormEigen::array<TensorIndex, 2> strides(1, 2);
  109. StartBenchmarkTiming();
  110. for (int iter = 0; iter < num_iters; ++iter) {
  111. B.device(device_) = A.stride(strides);
  112. }
  113. // Record the number of values copied from the padded tensor A each second
  114. finalizeBenchmark(m_ * k_ * num_iters);
  115. }
  116. void broadcasting(int num_iters) {
  117. const StormEigen::array<TensorIndex, 2> size_a(m_, 1);
  118. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> A(a_, size_a);
  119. const StormEigen::array<TensorIndex, 2> size_c(m_, n_);
  120. TensorMap<Tensor<float, 2>, StormEigen::Aligned> C(c_, size_c);
  121. #if defined(__CUDACC__)
  122. // nvcc doesn't support cxx11
  123. const StormEigen::array<int, 2> broadcast(1, n_);
  124. #else
  125. // Take advantage of cxx11 to give the compiler information it can use to
  126. // optimize the code.
  127. StormEigen::IndexList<StormEigen::type2index<1>, int> broadcast;
  128. broadcast.set(1, n_);
  129. #endif
  130. StartBenchmarkTiming();
  131. for (int iter = 0; iter < num_iters; ++iter) {
  132. C.device(device_) = A.broadcast(broadcast);
  133. }
  134. // Record the number of values broadcasted from A and copied to C each second
  135. finalizeBenchmark(m_ * n_ * num_iters);
  136. }
  137. void coeffWiseOp(int num_iters) {
  138. eigen_assert(m_ == k_ && k_ == n_);
  139. const StormEigen::array<TensorIndex, 2> sizes(m_, m_);
  140. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> A(a_, sizes);
  141. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> B(b_, sizes);
  142. TensorMap<Tensor<float, 2>, StormEigen::Aligned> C(c_, sizes);
  143. StartBenchmarkTiming();
  144. for (int iter = 0; iter < num_iters; ++iter) {
  145. C.device(device_) = A * A.constant(3.14) + B * B.constant(2.7);
  146. }
  147. // Record the number of FLOP executed per second (2 multiplications and
  148. // 1 addition per value)
  149. finalizeBenchmark(3 * m_ * m_ * num_iters);
  150. }
  151. void algebraicFunc(int num_iters) {
  152. eigen_assert(m_ == k_ && k_ == n_);
  153. const StormEigen::array<TensorIndex, 2> sizes(m_, m_);
  154. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> A(a_, sizes);
  155. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> B(b_, sizes);
  156. TensorMap<Tensor<float, 2>, StormEigen::Aligned> C(c_, sizes);
  157. StartBenchmarkTiming();
  158. for (int iter = 0; iter < num_iters; ++iter) {
  159. C.device(device_) = A.rsqrt() + B.sqrt() * B.square();
  160. }
  161. // Record the number of FLOP executed per second (assuming one operation
  162. // per value)
  163. finalizeBenchmark(m_ * m_ * num_iters);
  164. }
  165. void transcendentalFunc(int num_iters) {
  166. eigen_assert(m_ == k_ && k_ == n_);
  167. const StormEigen::array<TensorIndex, 2> sizes(m_, m_);
  168. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> A(a_, sizes);
  169. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> B(b_, sizes);
  170. TensorMap<Tensor<float, 2>, StormEigen::Aligned> C(c_, sizes);
  171. StartBenchmarkTiming();
  172. for (int iter = 0; iter < num_iters; ++iter) {
  173. C.device(device_) = A.exp() + B.log();
  174. }
  175. // Record the number of FLOP executed per second (assuming one operation
  176. // per value)
  177. finalizeBenchmark(m_ * m_ * num_iters);
  178. }
  179. // Simple reduction
  180. void reduction(int num_iters) {
  181. const StormEigen::array<TensorIndex, 2> input_size(k_, n_);
  182. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> B(b_, input_size);
  183. const StormEigen::array<TensorIndex, 1> output_size(n_);
  184. TensorMap<Tensor<float, 1>, StormEigen::Aligned> C(c_, output_size);
  185. const StormEigen::array<TensorIndex, 1> sum_along_dim(0);
  186. StartBenchmarkTiming();
  187. for (int iter = 0; iter < num_iters; ++iter) {
  188. C.device(device_) = B.sum(sum_along_dim);
  189. }
  190. // Record the number of FLOP executed per second (assuming one operation
  191. // per value)
  192. finalizeBenchmark(m_ * m_ * num_iters);
  193. }
  194. // do a contraction which is equivalent to a matrix multiplication
  195. void contraction(int num_iters) {
  196. const StormEigen::array<TensorIndex, 2> sizeA(m_, k_);
  197. const StormEigen::array<TensorIndex, 2> sizeB(k_, n_);
  198. const StormEigen::array<TensorIndex, 2> sizeC(m_, n_);
  199. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> A(a_, sizeA);
  200. const TensorMap<Tensor<float, 2>, StormEigen::Aligned> B(b_, sizeB);
  201. TensorMap<Tensor<float, 2>, StormEigen::Aligned> C(c_, sizeC);
  202. typedef typename Tensor<float, 2>::DimensionPair DimPair;
  203. const StormEigen::array<DimPair, 1> dims(DimPair(1, 0));
  204. StartBenchmarkTiming();
  205. for (int iter = 0; iter < num_iters; ++iter) {
  206. C.device(device_) = A.contract(B, dims);
  207. }
  208. // Record the number of FLOP executed per second (size_ multiplications and
  209. // additions for each value in the resulting tensor)
  210. finalizeBenchmark(static_cast<int64>(2) * m_ * n_ * k_ * num_iters);
  211. }
  212. void convolution(int num_iters, int kernel_x, int kernel_y) {
  213. const StormEigen::array<TensorIndex, 2> input_sizes(m_, n_);
  214. TensorMap<Tensor<float, 2>, StormEigen::Aligned> A(a_, input_sizes);
  215. const StormEigen::array<TensorIndex, 2> kernel_sizes(kernel_x, kernel_y);
  216. TensorMap<Tensor<float, 2>, StormEigen::Aligned> B(b_, kernel_sizes);
  217. const StormEigen::array<TensorIndex, 2> result_sizes(
  218. m_ - kernel_x + 1, n_ - kernel_y + 1);
  219. TensorMap<Tensor<float, 2>, StormEigen::Aligned> C(c_, result_sizes);
  220. StormEigen::array<Tensor<float, 2>::Index, 2> dims(0, 1);
  221. StartBenchmarkTiming();
  222. for (int iter = 0; iter < num_iters; ++iter) {
  223. C.device(device_) = A.convolve(B, dims);
  224. }
  225. // Record the number of FLOP executed per second (kernel_size
  226. // multiplications and additions for each value in the resulting tensor)
  227. finalizeBenchmark(
  228. (m_ - kernel_x + 1) * (n_ - kernel_y + 1) * kernel_x * kernel_y * 2 * num_iters);
  229. }
  230. private:
  231. void initialize() {
  232. a_ = (float *) device_.allocate(m_ * k_ * sizeof(float));
  233. b_ = (float *) device_.allocate(k_ * n_ * sizeof(float));
  234. c_ = (float *) device_.allocate(m_ * n_ * sizeof(float));
  235. // Initialize the content of the memory pools to prevent asan from
  236. // complaining.
  237. device_.memset(a_, 12, m_ * k_ * sizeof(float));
  238. device_.memset(b_, 23, k_ * n_ * sizeof(float));
  239. device_.memset(c_, 31, m_ * n_ * sizeof(float));
  240. BenchmarkUseRealTime();
  241. }
  242. inline void finalizeBenchmark(int64 num_items) {
  243. #if defined(EIGEN_USE_GPU) && defined(__CUDACC__)
  244. if (StormEigen::internal::is_same<Device, StormEigen::GpuDevice>::value) {
  245. device_.synchronize();
  246. }
  247. #endif
  248. StopBenchmarkTiming();
  249. SetBenchmarkItemsProcessed(num_items);
  250. }
  251. size_t m_;
  252. size_t k_;
  253. size_t n_;
  254. float* a_;
  255. float* b_;
  256. float* c_;
  257. Device device_;
  258. };
  259. #endif // THIRD_PARTY_EIGEN3_TENSOR_BENCHMARKS_H_