1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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/platform/test.h"
21#include "tensorflow/core/platform/test_benchmark.h"
22
23namespace tensorflow {
24namespace {
25
26Tensor VecShape(int64 v) {
27  if (v >= std::numeric_limits<int32>::max()) {
28    Tensor shape(DT_INT64, TensorShape({1}));
29    shape.vec<int64>()(0) = v;
30    return shape;
31  } else {
32    Tensor shape(DT_INT32, TensorShape({1}));
33    shape.vec<int32>()(0) = v;
34    return shape;
35  }
36}
37
38Tensor VecLam32(int64 n, int magnitude) {
39  std::mt19937 gen(0x12345);
40  std::uniform_real_distribution<float> dist(0.0, 1.0);
41  Tensor lams(DT_FLOAT, TensorShape({n}));
42  for (int i = 0; i < n; i++) {
43    // Generate in range (magnitude, 2 * magnitude)
44    lams.vec<float>()(i) = magnitude * (1 + dist(gen));
45  }
46  return lams;
47}
48
49Tensor VecLam64(int64 n, int magnitude) {
50  std::mt19937 gen(0x12345);
51  std::uniform_real_distribution<double> dist(0.0, 1.0);
52  Tensor lams(DT_DOUBLE, TensorShape({n}));
53  for (int i = 0; i < n; i++) {
54    // Generate in range (magnitude, 2 * magnitude)
55    lams.vec<double>()(i) = magnitude * (1 + dist(gen));
56  }
57  return lams;
58}
59
60#define BM_Poisson(DEVICE, BITS, MAGNITUDE)                            \
61  static void BM_##DEVICE##_RandomPoisson_lam_##MAGNITUDE##_##BITS(    \
62      int iters, int nsamp, int nlam) {                                \
63    testing::ItemsProcessed(static_cast<int64>(iters) * nsamp * nlam); \
64    Graph* g = new Graph(OpRegistry::Global());                        \
65    test::graph::RandomPoisson(                                        \
66        g, test::graph::Constant(g, VecShape(nsamp)),                  \
67        test::graph::Constant(g, VecLam##BITS(nlam, MAGNITUDE)));      \
68    test::Benchmark(#DEVICE, g).Run(iters);                            \
69  }                                                                    \
70  BENCHMARK(BM_##DEVICE##_RandomPoisson_lam_##MAGNITUDE##_##BITS)      \
71      ->RangePair(1, 64, 2, 50);
72
73BM_Poisson(cpu, 32, 1);
74BM_Poisson(cpu, 32, 8);
75BM_Poisson(cpu, 32, 32);
76
77BM_Poisson(cpu, 64, 1);
78BM_Poisson(cpu, 64, 8);
79BM_Poisson(cpu, 64, 32);
80
81}  // namespace
82}  // namespace tensorflow
83