Home | History | Annotate | Download | only in kernels
      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>
     41 static void SliceHelper(int iters, int size) {
     42   testing::StopTiming();
     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   testing::BytesProcessed(static_cast<int64>(iters) * kDim * size * sizeof(T));
     74   testing::StartTiming();
     75   test::Benchmark("cpu", g).Run(iters);
     76   testing::UseRealTime();
     77 }
     78 
     79 static void BM_SliceFloat(int iters, int dim2) {
     80   SliceHelper<float>(iters, dim2);
     81 }
     82 
     83 BENCHMARK(BM_SliceFloat)->Arg(100)->Arg(1000)->Arg(10000);
     84 
     85 static void BM_SliceComplex64(int iters, int dim2) {
     86   SliceHelper<std::complex<float>>(iters, dim2);
     87 }
     88 
     89 BENCHMARK(BM_SliceComplex64)->Arg(100)->Arg(1000)->Arg(10000);
     90 
     91 static void BM_SliceBFloat16(int iters, int dim2) {
     92   SliceHelper<bfloat16>(iters, dim2);
     93 }
     94 
     95 BENCHMARK(BM_SliceBFloat16)->Arg(100)->Arg(1000)->Arg(10000);
     96 
     97 static void BM_ValidateStridedSliceOp(int iters) {
     98   testing::StopTiming();
     99   int kDim = 100;
    100   int kMaxSize = 15000;
    101   int size = 100;
    102   Tensor begin = test::AsTensor<int32>({10, 10});
    103   Tensor end = test::AsTensor<int32>({10 + kDim, 10 + size});
    104   Tensor strides = test::AsTensor<int32>({1, 1});
    105   TensorShape input_shape({2 * kDim, kMaxSize});
    106 
    107   testing::StartTiming();
    108   for (int i = 0; i < iters; ++i) {
    109     TensorShape processing_shape, final_shape;
    110     bool is_identity = true, slice_dim0 = true, is_simple_slice = true;
    111     gtl::InlinedVector<int64, 4> begin_out, end_out, strides_out;
    112     const int32 begin_mask = 0;
    113     const int32 end_mask = 0;
    114     const int32 ellipsis_mask = 0;
    115     const int32 new_axis_mask = 0;
    116     const int32 shrink_axis_mask = 0;
    117 
    118     TF_CHECK_OK(ValidateStridedSliceOp(
    119         &begin, &end, strides, input_shape, begin_mask, end_mask, ellipsis_mask,
    120         new_axis_mask, shrink_axis_mask, &processing_shape, &final_shape,
    121         &is_identity, &is_simple_slice, &slice_dim0, &begin_out, &end_out,
    122         &strides_out));
    123   }
    124 }
    125 
    126 BENCHMARK(BM_ValidateStridedSliceOp);
    127 
    128 }  // namespace
    129 }  // namespace tensorflow
    130