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
AdjustContrast(int batches,int width,int height)24 static Graph* AdjustContrast(int batches, int width, int height) {
25 Graph* g = new Graph(OpRegistry::Global());
26 Tensor in(DT_FLOAT, TensorShape({batches, width, height, 3}));
27 in.flat<float>().setRandom();
28 Tensor factor(DT_FLOAT, TensorShape({}));
29 factor.flat<float>().setConstant(1.2);
30
31 Node* ret;
32 TF_CHECK_OK(NodeBuilder(g->NewName("n"), "AdjustContrastv2")
33 .Input(test::graph::Constant(g, in))
34 .Input(test::graph::Constant(g, factor))
35 .Finalize(g, &ret));
36 return g;
37 }
38
39 #define BM_AdjustContrastDev(DEVICE, B, W, H) \
40 static void BM_AdjustContrast_##DEVICE##_##B##_##W##_##H( \
41 ::testing::benchmark::State& state) { \
42 test::Benchmark(#DEVICE, AdjustContrast(B, W, H), \
43 /*old_benchmark_api*/ false) \
44 .Run(state); \
45 state.SetItemsProcessed(state.iterations() * B * W * H * 3); \
46 } \
47 BENCHMARK(BM_AdjustContrast_##DEVICE##_##B##_##W##_##H)
48
49 // Benchmark results as of cl/106323955
50 // BM_AdjustContrast_cpu_1_299_299 3416770 22008951 100 11.6M items/s
51 // BM_AdjustContrast_gpu_32_299_299 37117844 45512374 100 179.8M items/s
52 // Benchmark results as of cl/109478777
53 // (note that the definition has changed to perform no min/max or clamping,
54 // so a comparison to cl/106323955 is inherently unfair)
55 // The GPU test ran with -c opt --config=cuda --copt=-mavx, CPU ran without
56 // --config=cuda because for some reason that killed throughput measurement.
57 // CPU: Intel Haswell with HyperThreading (6 cores) dL1:32KB dL2:256KB dL3:15MB
58 // GPU: Tesla K40m
59 // BM_AdjustContrast_cpu_1_299_299 179084 340186 2181 751.9M items/s
60 // BM_AdjustContrast_gpu_32_299_299 85276 123665 4189 2.9G items/s
61 BM_AdjustContrastDev(cpu, 1, 299, 299);
62 #if (defined(GOOGLE_CUDA) && GOOGLE_CUDA) || \
63 (defined(TENSORFLOW_USE_ROCM) && TENSORFLOW_USE_ROCM)
64 BM_AdjustContrastDev(gpu, 32, 299, 299);
65 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
66
67 } // namespace tensorflow
68