Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include <random>
     17 
     18 #include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h"
     19 #include "tensorflow/core/framework/tensor.h"
     20 #include "tensorflow/core/graph/node_builder.h"
     21 #include "tensorflow/core/platform/test.h"
     22 #include "tensorflow/core/platform/test_benchmark.h"
     23 
     24 namespace tensorflow {
     25 
     26 Node* SparseTensorDenseMatMulNode(Graph* g, Node* a_indices, Node* a_values,
     27                                   Node* a_shape, Node* b, bool adjoint_a,
     28                                   bool adjoint_b) {
     29   Node* ret;
     30   TF_CHECK_OK(NodeBuilder(g->NewName("n"), "SparseTensorDenseMatMul")
     31                   .Input(a_indices)
     32                   .Input(a_values)
     33                   .Input(a_shape)
     34                   .Input(b)
     35                   .Attr("T", DT_FLOAT)
     36                   .Attr("adjoint_a", adjoint_a)
     37                   .Attr("adjoint_b", adjoint_b)
     38                   .Finalize(g, &ret));
     39   return ret;
     40 }
     41 
     42 static Graph* SparseTensorDenseMatmul(int nnz, int m, int k, int n,
     43                                       bool adjoint_a, bool adjoint_b) {
     44   Graph* g = new Graph(OpRegistry::Global());
     45   Tensor a_values(DT_FLOAT, TensorShape({nnz}));
     46   Tensor a_indices(DT_INT64, TensorShape({nnz, 2}));
     47   Tensor a_shape(DT_INT64, TensorShape({2}));
     48   auto a_shape_t = a_shape.vec<int64>();
     49   a_shape_t(0) = adjoint_a ? k : m;
     50   a_shape_t(1) = adjoint_a ? m : k;
     51   a_values.flat<float>().setRandom();
     52   auto a_indices_t = a_indices.matrix<int64>();
     53   std::random_device rd;
     54   std::mt19937 gen(rd());
     55   std::uniform_int_distribution<> a_lhs_dist(0, a_shape_t(0) - 1);
     56   std::uniform_int_distribution<> a_rhs_dist(0, a_shape_t(1) - 1);
     57   for (int32 i = 0; i < nnz; ++i) {
     58     a_indices_t(i, 0) = a_lhs_dist(gen);
     59     a_indices_t(i, 1) = a_rhs_dist(gen);
     60   }
     61   Tensor b(DT_FLOAT, adjoint_b ? TensorShape({n, k}) : TensorShape({k, n}));
     62   b.flat<float>().setRandom();
     63 
     64   SparseTensorDenseMatMulNode(
     65       g, test::graph::Constant(g, a_indices),
     66       test::graph::Constant(g, a_values), test::graph::HostConstant(g, a_shape),
     67       test::graph::Constant(g, b), adjoint_a, adjoint_b);
     68   return g;
     69 }
     70 
     71 #define BM_SparseTensorDenseMatmulDev(NNZ, M, K, N, TA, TB, DEVICE)                  \
     72   static void                                                                        \
     73       BM_SparseTensorDenseMatmul##_##NNZ##_##M##_##K##_##N##_##TA##_##TB##_##DEVICE( \
     74           int iters) {                                                               \
     75     int64 items_per_iter = (static_cast<int64>(NNZ) * (TB ? K : N));                 \
     76     testing::ItemsProcessed(static_cast<int64>(iters) * items_per_iter);             \
     77     testing::BytesProcessed(static_cast<int64>(iters) * items_per_iter *             \
     78                             sizeof(float));                                          \
     79     test::Benchmark(#DEVICE, SparseTensorDenseMatmul(NNZ, M, K, N, TA, TB))          \
     80         .Run(iters);                                                                 \
     81   }                                                                                  \
     82   BENCHMARK(                                                                         \
     83       BM_SparseTensorDenseMatmul##_##NNZ##_##M##_##K##_##N##_##TA##_##TB##_##DEVICE);
     84 
     85 #define BM_SparseTensorDenseMatmul(NNZ, M, K, N, TA, TB)    \
     86   BM_SparseTensorDenseMatmulDev(NNZ, M, K, N, TA, TB, cpu); \
     87   BM_SparseTensorDenseMatmulDev(NNZ, M, K, N, TA, TB, gpu);
     88 
     89 BM_SparseTensorDenseMatmul(128, 8, 512, 1, false, false);
     90 BM_SparseTensorDenseMatmul(128, 16, 512, 1, false, false);
     91 BM_SparseTensorDenseMatmul(128, 128, 512, 1, false, false);
     92 
     93 BM_SparseTensorDenseMatmul(128, 4096, 4096, 1, false, false);
     94 BM_SparseTensorDenseMatmul(1024, 4096, 4096, 1, false, false);
     95 BM_SparseTensorDenseMatmul(16384, 4096, 4096, 1, false, false);
     96 
     97 BM_SparseTensorDenseMatmul(128, 8, 1024, 16, false, false);
     98 BM_SparseTensorDenseMatmul(128, 16, 1024, 16, false, false);
     99 BM_SparseTensorDenseMatmul(128, 128, 1024, 16, false, false);
    100 BM_SparseTensorDenseMatmul(128, 4096, 4096, 128, false, false);
    101 BM_SparseTensorDenseMatmul(128, 4096, 4096, 1024, false, false);
    102 
    103 BM_SparseTensorDenseMatmul(1024, 8, 1024, 16, false, false);
    104 BM_SparseTensorDenseMatmul(1024, 16, 1024, 16, false, false);
    105 BM_SparseTensorDenseMatmul(1024, 128, 1024, 16, false, false);
    106 BM_SparseTensorDenseMatmul(1024, 4096, 4096, 128, false, false);
    107 BM_SparseTensorDenseMatmul(1024, 4096, 4096, 1024, false, false);
    108 
    109 BM_SparseTensorDenseMatmul(16384, 8, 1024, 16, false, false);
    110 BM_SparseTensorDenseMatmul(16384, 16, 1024, 16, false, false);
    111 BM_SparseTensorDenseMatmul(16384, 128, 1024, 16, false, false);
    112 BM_SparseTensorDenseMatmul(16384, 4096, 4096, 128, false, false);
    113 BM_SparseTensorDenseMatmul(16384, 4096, 4096, 1024, false, false);
    114 
    115 BM_SparseTensorDenseMatmul(16384, 4096, 4096, 4096, false, false);
    116 BM_SparseTensorDenseMatmul(16384, 4096, 4096, 4096, false, true);
    117 BM_SparseTensorDenseMatmul(16384, 4096, 4096, 4096, true, false);
    118 BM_SparseTensorDenseMatmul(16384, 4096, 4096, 4096, true, true);
    119 
    120 }  // end namespace tensorflow
    121