Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2016 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 <functional>
     17 #include <memory>
     18 #include <vector>
     19 
     20 #include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h"
     21 #include "tensorflow/core/graph/node_builder.h"
     22 #include "tensorflow/core/kernels/ops_testutil.h"
     23 #include "tensorflow/core/platform/test_benchmark.h"
     24 
     25 namespace tensorflow {
     26 
     27 static Graph* Multinomial(int batch_size, int num_classes, int num_samples) {
     28   Graph* g = new Graph(OpRegistry::Global());
     29   Tensor logits_t(DT_FLOAT, TensorShape({batch_size, num_classes}));
     30   Tensor num_samples_t(DT_INT32, TensorShape());
     31   logits_t.flat<float>().setRandom();
     32   num_samples_t.scalar<int32>().setConstant(num_samples);
     33 
     34   Node* ret;
     35   TF_CHECK_OK(NodeBuilder(g->NewName("multinomial"), "Multinomial")
     36                   .Input(test::graph::Constant(g, logits_t))
     37                   .Input(test::graph::Constant(g, num_samples_t))
     38                   .Attr("T", DT_FLOAT)
     39                   .Finalize(g, &ret));
     40   return g;
     41 }
     42 
     43 #define BM_MultinomialDev(DEVICE, B, C, S)                           \
     44   static void BM_Multinomial_##DEVICE##_##B##_##C##_##S(int iters) { \
     45     test::Benchmark(#DEVICE, Multinomial(B, C, S)).Run(iters);       \
     46     testing::ItemsProcessed(static_cast<int64>(B) * C * S * iters);  \
     47   }                                                                  \
     48   BENCHMARK(BM_Multinomial_##DEVICE##_##B##_##C##_##S);
     49 
     50 #define BM_MultinomialBCS(B, C, S) \
     51   BM_MultinomialDev(cpu, B, C, S); \
     52   BM_MultinomialDev(gpu, B, C, S);
     53 
     54 // NOTE(zongheng): some more expensive cases are commented out; left here since
     55 // they could still be useful in covering realistic cases.
     56 
     57 BM_MultinomialBCS(1, 10000, 4);
     58 BM_MultinomialBCS(1, 10000, 128);
     59 BM_MultinomialBCS(1, 10000, 10000);
     60 BM_MultinomialBCS(1, 100000, 4);
     61 // BM_MultinomialBCS(1, 100000, 128);
     62 
     63 BM_MultinomialBCS(32, 10000, 4);
     64 BM_MultinomialBCS(32, 10000, 128);
     65 BM_MultinomialBCS(32, 100000, 4);
     66 // BM_MultinomialBCS(32, 100000, 128);
     67 
     68 BM_MultinomialBCS(128, 100000, 1);
     69 // BM_MultinomialBCS(128, 100000, 128);
     70 
     71 }  // namespace tensorflow
     72