• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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 <initializer_list>
17 #include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h"
18 #include "tensorflow/core/framework/bfloat16.h"
19 #include "tensorflow/core/framework/tensor.h"
20 #include "tensorflow/core/framework/tensor_testutil.h"
21 #include "tensorflow/core/framework/types.pb.h"
22 #include "tensorflow/core/graph/node_builder.h"
23 #include "tensorflow/core/lib/strings/stringprintf.h"
24 #include "tensorflow/core/platform/test.h"
25 #include "tensorflow/core/platform/test_benchmark.h"
26 
27 namespace tensorflow {
28 
MakeGraph(int split_dim,int num_split,std::initializer_list<int64> chunk_size)29 static Graph* MakeGraph(int split_dim, int num_split,
30                         std::initializer_list<int64> chunk_size) {
31   Graph* g = new Graph(OpRegistry::Global());
32   TensorShape in_shape(chunk_size);
33   in_shape.set_dim(split_dim, in_shape.dim_size(split_dim) * num_split);
34   Tensor in(DataTypeToEnum<float>::value, in_shape);
35   in.flat<float>().setRandom();
36   Tensor split_dim_tensor = test::AsScalar<int32>(split_dim);
37   Node* split;
38   TF_CHECK_OK(NodeBuilder(g->NewName("split"), "Split")
39                   .Input(test::graph::Constant(g, split_dim_tensor))
40                   .Input(test::graph::Constant(g, in))
41                   .Attr("num_split", num_split)
42                   .Finalize(g, &split));
43   return g;
44 }
45 
46 #define BM_SPLIT_1D(num_split, chunk_size)                                  \
47   static void BM_Split_1d_##num_split##_##chunk_size(                       \
48       ::testing::benchmark::State& state) {                                 \
49     auto label =                                                            \
50         strings::Printf("1-D %d chunks of %d each", num_split, chunk_size); \
51     state.SetLabel(label);                                                  \
52     auto g = MakeGraph(/* split_dim = */ 0, num_split, {chunk_size});       \
53     test::Benchmark("cpu", g, /*old_benchmark_api*/ false).Run(state);      \
54     state.SetItemsProcessed(static_cast<int64>(state.iterations()) *        \
55                             num_split * chunk_size);                        \
56   }                                                                         \
57   BENCHMARK(BM_Split_1d_##num_split##_##chunk_size)->UseRealTime();
58 
59 #define BM_SPLIT_2D(split_dim, num_split, chunk_size0, chunk_size1)          \
60   static void                                                                \
61       BM_Split_2d_##split_dim##_##num_split##_##chunk_size0##_##chunk_size1( \
62           ::testing::benchmark::State& state) {                              \
63     auto label =                                                             \
64         strings::Printf("2-D %d chunks in dim %d of (%d * %d) each",         \
65                         num_split, split_dim, chunk_size0, chunk_size1);     \
66     state.SetLabel(label);                                                   \
67     auto g = MakeGraph(split_dim, num_split, {chunk_size0, chunk_size1});    \
68     test::Benchmark("cpu", g, /*old_benchmark_api*/ false).Run(state);       \
69     state.SetItemsProcessed(static_cast<int64>(state.iterations()) *         \
70                             num_split * chunk_size0 * chunk_size1);          \
71   }                                                                          \
72   BENCHMARK(                                                                 \
73       BM_Split_2d_##split_dim##_##num_split##_##chunk_size0##_##chunk_size1) \
74       ->UseRealTime();
75 
76 BM_SPLIT_1D(5, 1);
77 BM_SPLIT_1D(262144, 1);
78 BM_SPLIT_1D(1, 100000);
79 BM_SPLIT_1D(5, 100000);
80 BM_SPLIT_1D(10, 4194304);
81 BM_SPLIT_1D(2, 4194304);
82 BM_SPLIT_1D(100, 1024);
83 BM_SPLIT_1D(32768, 1024);
84 
85 BM_SPLIT_2D(0, 1024, 1, 10);
86 BM_SPLIT_2D(0, 1024, 10, 10);
87 BM_SPLIT_2D(0, 512, 1024, 256);
88 BM_SPLIT_2D(0, 20, 100000, 5);
89 BM_SPLIT_2D(0, 2, 3, 524288);
90 BM_SPLIT_2D(0, 100, 4096, 512);
91 
92 BM_SPLIT_2D(1, 1024, 1, 10);
93 BM_SPLIT_2D(1, 1024, 10, 10);
94 BM_SPLIT_2D(1, 512, 1024, 256);
95 BM_SPLIT_2D(1, 20, 100000, 5);
96 BM_SPLIT_2D(1, 2, 3, 524288);
97 BM_SPLIT_2D(1, 100, 4096, 512);
98 
99 }  // namespace tensorflow
100