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/kernels/xent_op.h"
     21 #include "tensorflow/core/platform/test.h"
     22 #include "tensorflow/core/platform/test_benchmark.h"
     23 
     24 namespace tensorflow {
     25 
     26 static Graph* SparseXent(int batch_size, int num_classes) {
     27   Graph* g = new Graph(OpRegistry::Global());
     28   Tensor logits(DT_FLOAT, TensorShape({batch_size, num_classes}));
     29   logits.flat<float>().setRandom();
     30   Tensor labels(DT_INT64, TensorShape({batch_size}));
     31   std::random_device rd;
     32   std::mt19937 gen(rd());
     33   std::uniform_int_distribution<> dist(0, num_classes - 1);
     34   auto labels_t = labels.flat<int64>();
     35   for (int i = 0; i < batch_size; ++i) {
     36     labels_t(i) = dist(gen);
     37   }
     38   test::graph::Binary(g, "SparseSoftmaxCrossEntropyWithLogits",
     39                       test::graph::Constant(g, logits),
     40                       test::graph::Constant(g, labels));
     41   return g;
     42 }
     43 
     44 #define BM_SparseXentDev(BATCH, CLASS, DEVICE)                          \
     45   static void BM_SparseXent##_##BATCH##_##CLASS##_##DEVICE(int iters) { \
     46     testing::ItemsProcessed(static_cast<int64>(iters) * BATCH * CLASS); \
     47     test::Benchmark(#DEVICE, SparseXent(BATCH, CLASS)).Run(iters);      \
     48   }                                                                     \
     49   BENCHMARK(BM_SparseXent##_##BATCH##_##CLASS##_##DEVICE);
     50 
     51 /// The representative tests for ptb_word on GPU
     52 BM_SparseXentDev(8, 1000000, gpu);
     53 
     54 BM_SparseXentDev(16, 10000, gpu);
     55 BM_SparseXentDev(16, 30000, gpu);
     56 BM_SparseXentDev(16, 100000, gpu);
     57 
     58 BM_SparseXentDev(32, 10000, gpu);
     59 BM_SparseXentDev(32, 30000, gpu);
     60 BM_SparseXentDev(32, 100000, gpu);
     61 
     62 BM_SparseXentDev(64, 10000, gpu);
     63 BM_SparseXentDev(64, 30000, gpu);
     64 BM_SparseXentDev(64, 100000, gpu);
     65 
     66 // CPU
     67 BM_SparseXentDev(8, 1000000, cpu);
     68 
     69 BM_SparseXentDev(16, 10000, cpu);
     70 BM_SparseXentDev(16, 100000, cpu);
     71 
     72 BM_SparseXentDev(32, 10000, cpu);
     73 BM_SparseXentDev(32, 100000, cpu);
     74 
     75 BM_SparseXentDev(64, 10000, cpu);
     76 BM_SparseXentDev(64, 100000, cpu);
     77 
     78 }  // end namespace tensorflow
     79