• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/tensor_testutil.h"
24 #include "tensorflow/core/framework/types.h"
25 #include "tensorflow/core/framework/types.pb.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 #include "tensorflow/core/util/strided_slice_op.h"
34 
35 namespace tensorflow {
36 namespace {
37 
38 // For the benchmark, we set up two 2-dimensional tensors, each kDim1 x 'dim'
39 // in size, and concat them together along "concat_dimension"
40 template <typename T>
SliceHelper(::testing::benchmark::State & state)41 static void SliceHelper(::testing::benchmark::State& state) {
42   const int size = state.range(0);
43   Graph* g = new Graph(OpRegistry::Global());
44   DataType dt = DataTypeToEnum<T>::v();
45   int kDim = 100;
46   int kMaxSize = 15000;
47   CHECK_LT(size, kMaxSize);
48 
49   Tensor begin(DT_INT32, TensorShape({2}));
50   begin.flat<int32>()(0) = 10;
51   begin.flat<int32>()(1) = 10;
52 
53   Tensor end(DT_INT32, TensorShape({2}));
54   end.flat<int32>()(0) = 10 + kDim;
55   end.flat<int32>()(1) = 10 + size;
56 
57   Tensor strides(DT_INT32, TensorShape({2}));
58   strides.flat<int32>()(0) = 1;
59   strides.flat<int32>()(1) = 1;
60 
61   Tensor input(dt, TensorShape({2 * kDim, kMaxSize}));
62   input.flat<T>().setRandom();
63 
64   Node* node;
65   TF_CHECK_OK(NodeBuilder(g->NewName("n"), "StridedSlice")
66                   .Input(test::graph::Constant(g, input))
67                   .Input(test::graph::Constant(g, begin))
68                   .Input(test::graph::Constant(g, end))
69                   .Input(test::graph::Constant(g, strides))
70                   .Attr("T", dt)
71                   .Finalize(g, &node));
72 
73   test::Benchmark("cpu", g, /*old_benchmark_api*/ false).Run(state);
74   state.SetBytesProcessed(static_cast<int64>(state.iterations()) * kDim * size *
75                           sizeof(T));
76 }
77 
BM_SliceFloat(::testing::benchmark::State & state)78 void BM_SliceFloat(::testing::benchmark::State& state) {
79   SliceHelper<float>(state);
80 }
81 
82 BENCHMARK(BM_SliceFloat)->UseRealTime()->Arg(100)->Arg(1000)->Arg(10000);
83 
BM_SliceComplex64(::testing::benchmark::State & state)84 void BM_SliceComplex64(::testing::benchmark::State& state) {
85   SliceHelper<std::complex<float>>(state);
86 }
87 
88 BENCHMARK(BM_SliceComplex64)->UseRealTime()->Arg(100)->Arg(1000)->Arg(10000);
89 
BM_SliceBFloat16(::testing::benchmark::State & state)90 void BM_SliceBFloat16(::testing::benchmark::State& state) {
91   SliceHelper<bfloat16>(state);
92 }
93 
94 BENCHMARK(BM_SliceBFloat16)->UseRealTime()->Arg(100)->Arg(1000)->Arg(10000);
95 
BM_ValidateStridedSliceOp(::testing::benchmark::State & state)96 void BM_ValidateStridedSliceOp(::testing::benchmark::State& state) {
97   int kDim = 100;
98   int kMaxSize = 15000;
99   int size = 100;
100   Tensor begin = test::AsTensor<int32>({10, 10});
101   Tensor end = test::AsTensor<int32>({10 + kDim, 10 + size});
102   Tensor strides = test::AsTensor<int32>({1, 1});
103   TensorShape input_shape({2 * kDim, kMaxSize});
104 
105   for (auto s : state) {
106     TensorShape processing_shape, final_shape;
107     bool is_identity = true, slice_dim0 = true, is_simple_slice = true;
108     gtl::InlinedVector<int64, 4> begin_out, end_out, strides_out;
109     const int32_t begin_mask = 0;
110     const int32_t end_mask = 0;
111     const int32_t ellipsis_mask = 0;
112     const int32_t new_axis_mask = 0;
113     const int32_t shrink_axis_mask = 0;
114 
115     TF_CHECK_OK(ValidateStridedSliceOp(
116         &begin, &end, strides, input_shape, begin_mask, end_mask, ellipsis_mask,
117         new_axis_mask, shrink_axis_mask, &processing_shape, &final_shape,
118         &is_identity, &is_simple_slice, &slice_dim0, &begin_out, &end_out,
119         &strides_out));
120   }
121 }
122 
123 BENCHMARK(BM_ValidateStridedSliceOp);
124 
125 }  // namespace
126 }  // namespace tensorflow
127