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 "tensorflow/core/kernels/xent_op.h"
     17 #include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h"
     18 #include "tensorflow/core/framework/tensor.h"
     19 #include "tensorflow/core/platform/test.h"
     20 #include "tensorflow/core/platform/test_benchmark.h"
     21 
     22 namespace tensorflow {
     23 
     24 static Graph* Xent(int batch_size, int num_classes) {
     25   Graph* g = new Graph(OpRegistry::Global());
     26   Tensor logits(DT_FLOAT, TensorShape({batch_size, num_classes}));
     27   logits.flat<float>().setRandom();
     28   Tensor labels(DT_FLOAT, TensorShape({batch_size, num_classes}));
     29   labels.flat<float>().setRandom();
     30   test::graph::Binary(g, "SoftmaxCrossEntropyWithLogits",
     31                       test::graph::Constant(g, logits),
     32                       test::graph::Constant(g, labels));
     33   return g;
     34 }
     35 
     36 #define BM_XentDev(BATCH, CLASS, DEVICE)                                \
     37   static void BM_Xent##_##BATCH##_##CLASS##_##DEVICE(int iters) {       \
     38     testing::ItemsProcessed(static_cast<int64>(iters) * BATCH * CLASS); \
     39     test::Benchmark(#DEVICE, Xent(BATCH, CLASS)).Run(iters);            \
     40   }                                                                     \
     41   BENCHMARK(BM_Xent##_##BATCH##_##CLASS##_##DEVICE);
     42 
     43 /// The representative tests for ptb_word on GPU
     44 BM_XentDev(16, 10000, gpu);
     45 BM_XentDev(16, 30000, gpu);
     46 BM_XentDev(16, 100000, gpu);
     47 
     48 BM_XentDev(32, 10000, gpu);
     49 BM_XentDev(32, 30000, gpu);
     50 BM_XentDev(32, 100000, gpu);
     51 
     52 BM_XentDev(64, 10000, gpu);
     53 BM_XentDev(64, 30000, gpu);
     54 BM_XentDev(64, 100000, gpu);
     55 
     56 /// Only the smaller tests for CPU. Otherwise, it's too slow
     57 BM_XentDev(16, 10000, cpu);
     58 BM_XentDev(32, 10000, cpu);
     59 BM_XentDev(64, 10000, cpu);
     60 
     61 }  // end namespace tensorflow
     62