Home | History | Annotate | Download | only in kernels
      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 
     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(int iters) {           \
     48     testing::StopTiming();                                                  \
     49     testing::ItemsProcessed(static_cast<int64>(iters) * num_split *         \
     50                             chunk_size);                                    \
     51     auto label =                                                            \
     52         strings::Printf("1-D %d chunks of %d each", num_split, chunk_size); \
     53     testing::SetLabel(label);                                               \
     54     testing::UseRealTime();                                                 \
     55     auto g = MakeGraph(/* split_dim = */ 0, num_split, {chunk_size});       \
     56     testing::StartTiming();                                                 \
     57     test::Benchmark("cpu", g).Run(iters);                                   \
     58   }                                                                         \
     59   BENCHMARK(BM_Split_1d_##num_split##_##chunk_size);
     60 
     61 #define BM_SPLIT_2D(split_dim, num_split, chunk_size0, chunk_size1)          \
     62   static void                                                                \
     63       BM_Split_2d_##split_dim##_##num_split##_##chunk_size0##_##chunk_size1( \
     64           int iters) {                                                       \
     65     testing::StopTiming();                                                   \
     66     testing::ItemsProcessed(static_cast<int64>(iters) * num_split *          \
     67                             chunk_size0 * chunk_size1);                      \
     68     auto label =                                                             \
     69         strings::Printf("2-D %d chunks in dim %d of (%d * %d) each",         \
     70                         num_split, split_dim, chunk_size0, chunk_size1);     \
     71     testing::SetLabel(label);                                                \
     72     testing::UseRealTime();                                                  \
     73     auto g = MakeGraph(split_dim, num_split, {chunk_size0, chunk_size1});    \
     74     testing::StartTiming();                                                  \
     75     test::Benchmark("cpu", g).Run(iters);                                    \
     76   }                                                                          \
     77   BENCHMARK(                                                                 \
     78       BM_Split_2d_##split_dim##_##num_split##_##chunk_size0##_##chunk_size1);
     79 
     80 BM_SPLIT_1D(5, 1);
     81 BM_SPLIT_1D(262144, 1);
     82 BM_SPLIT_1D(1, 100000);
     83 BM_SPLIT_1D(5, 100000);
     84 BM_SPLIT_1D(10, 4194304);
     85 BM_SPLIT_1D(2, 4194304);
     86 BM_SPLIT_1D(100, 1024);
     87 BM_SPLIT_1D(32768, 1024);
     88 
     89 BM_SPLIT_2D(0, 1024, 1, 10);
     90 BM_SPLIT_2D(0, 1024, 10, 10);
     91 BM_SPLIT_2D(0, 512, 1024, 256);
     92 BM_SPLIT_2D(0, 20, 100000, 5);
     93 BM_SPLIT_2D(0, 2, 3, 524288);
     94 BM_SPLIT_2D(0, 100, 4096, 512);
     95 
     96 BM_SPLIT_2D(1, 1024, 1, 10);
     97 BM_SPLIT_2D(1, 1024, 10, 10);
     98 BM_SPLIT_2D(1, 512, 1024, 256);
     99 BM_SPLIT_2D(1, 20, 100000, 5);
    100 BM_SPLIT_2D(1, 2, 3, 524288);
    101 BM_SPLIT_2D(1, 100, 4096, 512);
    102 
    103 }  // namespace tensorflow
    104