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 <functional>
17 #include <memory>
18
19 #include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h"
20 #include "tensorflow/core/framework/allocator.h"
21 #include "tensorflow/core/framework/op_kernel.h"
22 #include "tensorflow/core/framework/tensor.h"
23 #include "tensorflow/core/framework/types.h"
24 #include "tensorflow/core/framework/types.pb.h"
25 #include "tensorflow/core/graph/algorithm.h"
26 #include "tensorflow/core/graph/node_builder.h"
27 #include "tensorflow/core/graph/testlib.h"
28 #include "tensorflow/core/kernels/ops_testutil.h"
29 #include "tensorflow/core/kernels/ops_util.h"
30 #include "tensorflow/core/lib/core/status_test_util.h"
31 #include "tensorflow/core/platform/test.h"
32 #include "tensorflow/core/platform/test_benchmark.h"
33
34 namespace tensorflow {
35 namespace {
36
37 // For the benchmark, we set up two 2-dimensional tensors, each kDim1 x 'dim'
38 // in size, and concat them together along "concat_dimension"
39 template <typename T>
SliceHelper(::testing::benchmark::State & state)40 static void SliceHelper(::testing::benchmark::State& state) {
41 const int size = state.range(0);
42 Graph* g = new Graph(OpRegistry::Global());
43 DataType dt = DataTypeToEnum<T>::v();
44 int kDim = 100;
45 int kMaxSize = 15000;
46 CHECK_LT(size, kMaxSize);
47
48 Tensor begin(DT_INT32, TensorShape({2}));
49 begin.flat<int32>()(0) = 10;
50 begin.flat<int32>()(1) = 10;
51
52 Tensor sizes(DT_INT32, TensorShape({2}));
53 sizes.flat<int32>()(0) = kDim;
54 sizes.flat<int32>()(1) = size;
55
56 Tensor input(dt, TensorShape({2 * kDim, kMaxSize}));
57 input.flat<T>().setRandom();
58
59 Node* node;
60 TF_CHECK_OK(NodeBuilder(g->NewName("n"), "Slice")
61 .Input(test::graph::Constant(g, input))
62 .Input(test::graph::Constant(g, begin))
63 .Input(test::graph::Constant(g, sizes))
64 .Attr("T", dt)
65 .Finalize(g, &node));
66 FixupSourceAndSinkEdges(g);
67
68 test::Benchmark("cpu", g, nullptr, nullptr, nullptr,
69 "SINGLE_THREADED_EXECUTOR", /*old_benchmark_api*/ false)
70 .Run(state);
71 state.SetBytesProcessed(static_cast<int64>(state.iterations()) * kDim * size *
72 sizeof(T));
73 }
74
BM_SliceFloat(::testing::benchmark::State & state)75 void BM_SliceFloat(::testing::benchmark::State& state) {
76 SliceHelper<float>(state);
77 }
78
79 BENCHMARK(BM_SliceFloat)->UseRealTime()->Arg(100)->Arg(1000)->Arg(10000);
80
BM_SliceBFloat16(::testing::benchmark::State & state)81 void BM_SliceBFloat16(::testing::benchmark::State& state) {
82 SliceHelper<bfloat16>(state);
83 }
84
85 BENCHMARK(BM_SliceBFloat16)->UseRealTime()->Arg(100)->Arg(1000)->Arg(10000);
86
87 } // namespace
88 } // namespace tensorflow
89