1 /* Copyright 2019 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/kernels/bias_op.h"
17
18 #include <random>
19
20 #include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h"
21 #include "tensorflow/core/framework/tensor.h"
22 #include "tensorflow/core/platform/test.h"
23 #include "tensorflow/core/platform/test_benchmark.h"
24
25 namespace tensorflow {
26
BiasAdd(int d0,int d1,int d2,int d3)27 static Graph* BiasAdd(int d0, int d1, int d2, int d3) {
28 auto* g = new Graph(OpRegistry::Global());
29 Tensor input(DT_FLOAT, TensorShape({d0, d1, d2, d3}));
30 Tensor bias(DT_FLOAT, TensorShape({d3}));
31 input.flat<float>().setRandom();
32 bias.flat<float>().setRandom();
33 test::graph::Binary(g, "BiasAdd", test::graph::Constant(g, input),
34 test::graph::Constant(g, bias));
35 return g;
36 }
37
BiasAddGrad(int d0,int d1,int d2,int d3)38 static Graph* BiasAddGrad(int d0, int d1, int d2, int d3) {
39 auto* g = new Graph(OpRegistry::Global());
40 Tensor out_backprop(DT_FLOAT, TensorShape({d0, d1, d2, d3}));
41 out_backprop.flat<float>().setRandom();
42 test::graph::Unary(g, "BiasAddGrad", test::graph::Constant(g, out_backprop));
43 return g;
44 }
45
46 #define BM_BiasAddNHWC(N, W, H, C, DEVICE) \
47 static void BM_BiasAddNHWC##_##N##_##H##_##W##_##C##_##DEVICE( \
48 ::testing::benchmark::State& state) { \
49 test::Benchmark(#DEVICE, BiasAdd(N, H, W, C), /*old_benchmark_api=*/false) \
50 .Run(state); \
51 state.SetItemsProcessed(static_cast<int64>(state.iterations()) * N * H * \
52 W * C); \
53 } \
54 BENCHMARK(BM_BiasAddNHWC##_##N##_##H##_##W##_##C##_##DEVICE)->UseRealTime();
55
56 #define BM_BiasAddGradNHWC(N, W, H, C, DEVICE) \
57 static void BM_BiasAddGradNHWC##_##N##_##H##_##W##_##C##_##DEVICE( \
58 ::testing::benchmark::State& state) { \
59 test::Benchmark(#DEVICE, BiasAddGrad(N, H, W, C), \
60 /*old_benchmark_api=*/false) \
61 .Run(state); \
62 state.SetItemsProcessed(static_cast<int64>(state.iterations()) * N * H * \
63 W * C); \
64 } \
65 BENCHMARK(BM_BiasAddGradNHWC##_##N##_##H##_##W##_##C##_##DEVICE) \
66 ->UseRealTime();
67
68 // CPU
69 BM_BiasAddNHWC(32, 32, 32, 128, cpu);
70 BM_BiasAddNHWC(32, 32, 32, 256, cpu);
71 BM_BiasAddNHWC(32, 32, 32, 512, cpu);
72 BM_BiasAddNHWC(32, 32, 32, 1024, cpu);
73
74 BM_BiasAddNHWC(32, 64, 64, 128, cpu);
75 BM_BiasAddNHWC(32, 64, 64, 256, cpu);
76 BM_BiasAddNHWC(32, 64, 64, 512, cpu);
77 BM_BiasAddNHWC(32, 64, 64, 1024, cpu);
78
79 BM_BiasAddGradNHWC(32, 32, 32, 128, cpu);
80 BM_BiasAddGradNHWC(32, 32, 32, 256, cpu);
81 BM_BiasAddGradNHWC(32, 32, 32, 512, cpu);
82 BM_BiasAddGradNHWC(32, 32, 32, 1024, cpu);
83
84 BM_BiasAddGradNHWC(32, 64, 64, 128, cpu);
85 BM_BiasAddGradNHWC(32, 64, 64, 256, cpu);
86 BM_BiasAddGradNHWC(32, 64, 64, 512, cpu);
87 BM_BiasAddGradNHWC(32, 64, 64, 1024, cpu);
88
89 #ifdef GOOGLE_CUDA
90 BM_BiasAddGradNHWC(32, 32, 32, 128, gpu);
91 BM_BiasAddGradNHWC(32, 32, 32, 256, gpu);
92 BM_BiasAddGradNHWC(32, 32, 32, 512, gpu);
93 BM_BiasAddGradNHWC(32, 32, 32, 1024, gpu);
94
95 BM_BiasAddGradNHWC(32, 64, 64, 128, gpu);
96 BM_BiasAddGradNHWC(32, 64, 64, 256, gpu);
97 BM_BiasAddGradNHWC(32, 64, 64, 512, gpu);
98 BM_BiasAddGradNHWC(32, 64, 64, 1024, gpu);
99 #endif // GOOGLE_CUDA
100
101 } // end namespace tensorflow
102