• 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 "tensorflow/core/common_runtime/kernel_benchmark_testlib.h"
17 #include "tensorflow/core/framework/tensor.h"
18 #include "tensorflow/core/graph/node_builder.h"
19 #include "tensorflow/core/platform/test.h"
20 #include "tensorflow/core/platform/test_benchmark.h"
21 
22 namespace tensorflow {
23 
Resize(const char * algorithm,int batches,int input_height,int input_width,int channels,int output_height,int output_width)24 static Graph* Resize(const char* algorithm, int batches, int input_height,
25                      int input_width, int channels, int output_height,
26                      int output_width) {
27   Graph* g = new Graph(OpRegistry::Global());
28   Tensor in(DT_FLOAT,
29             TensorShape({batches, input_height, input_width, channels}));
30   in.flat<float>().setRandom();
31 
32   Tensor out_size(DT_INT32, TensorShape({2}));
33   auto out_size_flat = out_size.flat<int32>();
34   out_size_flat(0) = output_height;
35   out_size_flat(1) = output_width;
36 
37   Node* ret;
38   Status s = NodeBuilder(g->NewName("n"), algorithm)
39                  .Input(test::graph::Constant(g, in))
40                  .Input(test::graph::Constant(g, out_size))
41                  .Finalize(g, &ret);
42   assert(s.ok());
43   return g;
44 }
45 
46 #define BM_ResizeDev(DEVICE, ALGORITHM, B, W, H, C, OW, OH)                   \
47   static void                                                                 \
48       BM_Resize_##ALGORITHM##_##DEVICE##_##B##_##W##_##H##_##C##_##OW##_##OH( \
49           ::testing::benchmark::State& state) {                               \
50     test::Benchmark(#DEVICE, Resize(#ALGORITHM, B, W, H, C, OW, OH),          \
51                     /*old_benchmark_api*/ false)                              \
52         .Run(state);                                                          \
53     state.SetItemsProcessed(state.iterations() * B * W * H * C);              \
54   }                                                                           \
55   BENCHMARK(                                                                  \
56       BM_Resize_##ALGORITHM##_##DEVICE##_##B##_##W##_##H##_##C##_##OW##_##OH)
57 
58 BM_ResizeDev(cpu, ResizeNearestNeighbor, 10, 499, 499, 1, 250, 250);
59 BM_ResizeDev(cpu, ResizeNearestNeighbor, 10, 499, 499, 3, 250, 250);
60 BM_ResizeDev(cpu, ResizeNearestNeighbor, 10, 499, 499, 1, 998, 998);
61 BM_ResizeDev(cpu, ResizeNearestNeighbor, 10, 499, 499, 3, 998, 998);
62 BM_ResizeDev(cpu, ResizeBilinear, 10, 499, 499, 1, 250, 250);
63 BM_ResizeDev(cpu, ResizeBilinear, 10, 499, 499, 3, 250, 250);
64 BM_ResizeDev(cpu, ResizeBilinear, 10, 499, 499, 1, 998, 998);
65 BM_ResizeDev(cpu, ResizeBilinear, 10, 499, 499, 3, 998, 998);
66 
67 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
68 BM_ResizeDev(gpu, ResizeNearestNeighbor, 10, 499, 499, 3, 998, 998);
69 BM_ResizeDev(gpu, ResizeBilinear, 10, 499, 499, 3, 998, 998);
70 #endif
71 
72 }  // namespace tensorflow
73