1 /* Copyright 2016 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/allocator.h"
18 #include "tensorflow/core/framework/fake_input.h"
19 #include "tensorflow/core/framework/node_def_builder.h"
20 #include "tensorflow/core/framework/op_kernel.h"
21 #include "tensorflow/core/framework/tensor.h"
22 #include "tensorflow/core/framework/tensor_testutil.h"
23 #include "tensorflow/core/framework/types.h"
24 #include "tensorflow/core/framework/types.pb.h"
25 #include "tensorflow/core/graph/node_builder.h"
26 #include "tensorflow/core/kernels/ops_testutil.h"
27 #include "tensorflow/core/kernels/ops_util.h"
28 #include "tensorflow/core/lib/core/status_test_util.h"
29 #include "tensorflow/core/platform/test.h"
30 #include "tensorflow/core/platform/test_benchmark.h"
31
32 namespace tensorflow {
33
34 // Test data from the TensorFlow README.md.
35 const char* lines[] = {
36 "**TensorFlow** is an open source software library for numerical "
37 "computation using data flow graphs.",
38 "The graph nodes represent mathematical operations, while the graph edges "
39 "represent the multidimensional data arrays (tensors) that flow between "
40 "them.",
41 "This flexible architecture enables you to deploy computation to one or "
42 "more CPUs or GPUs in a desktop, server, or mobile device without "
43 "rewriting code.",
44 "TensorFlow also includes "
45 "[TensorBoard](https://www.tensorflow.org/guide/"
46 "summaries_and_tensorboard), a data visualization toolkit.",
47 "TensorFlow was originally developed by researchers and engineers working "
48 "on the Google Brain team within Google's Machine Intelligence Research "
49 "organization for the purposes of conducting machine learning and deep "
50 "neural networks research.",
51 "The system is general enough to be applicable in a wide variety of other "
52 "domains, as well.",
53 "TensorFlow provides stable Python API and C APIs as well as without API "
54 "backwards compatibility guarantee like C++, Go, Java, JavaScript and "
55 "Swift."};
56
57 const char kRegExPattern[] = "\\p{P}";
58 const char kRewrite[] = " ";
59
GetTestTensor(int batch)60 Tensor GetTestTensor(int batch) {
61 const int sz = TF_ARRAYSIZE(lines);
62 Tensor t(DT_STRING, {batch});
63 auto s = t.flat<tstring>();
64 for (int i = 0; i < batch; ++i) {
65 s(i) = lines[i % sz];
66 }
67 return t;
68 }
69
SetupRegexReplaceGraph(const Tensor & input,const string & input_pattern,const string & input_rewrite)70 Graph* SetupRegexReplaceGraph(const Tensor& input, const string& input_pattern,
71 const string& input_rewrite) {
72 Graph* g = new Graph(OpRegistry::Global());
73 Tensor pattern(DT_STRING, TensorShape({}));
74 pattern.flat<tstring>().setConstant(input_pattern);
75 Tensor rewrite(DT_STRING, TensorShape({}));
76 rewrite.flat<tstring>().setConstant(input_rewrite);
77
78 TF_CHECK_OK(NodeBuilder("regex_replace_op", "RegexReplace")
79 .Input(test::graph::Constant(g, input))
80 .Input(test::graph::Constant(g, pattern))
81 .Input(test::graph::Constant(g, rewrite))
82 .Attr("replace_global", true)
83 .Finalize(g, nullptr /* node */));
84 return g;
85 }
86
BM_RegexReplace(::testing::benchmark::State & state)87 static void BM_RegexReplace(::testing::benchmark::State& state) {
88 const int batch_size = state.range(0);
89
90 Tensor input = GetTestTensor(batch_size);
91 Graph* g = SetupRegexReplaceGraph(input, kRegExPattern, kRewrite);
92 test::Benchmark("cpu", g, /*old_benchmark_api*/ false).Run(state);
93 state.SetItemsProcessed(static_cast<int64>(state.iterations()));
94 }
95
96 BENCHMARK(BM_RegexReplace)
97 ->UseRealTime()
98 ->Arg(1)
99 ->Arg(8)
100 ->Arg(16)
101 ->Arg(32)
102 ->Arg(64)
103 ->Arg(128)
104 ->Arg(256);
105
SetupStaticGraph(const Tensor & input,const string & input_pattern,const string & rewrite)106 Graph* SetupStaticGraph(const Tensor& input, const string& input_pattern,
107 const string& rewrite) {
108 Graph* g = new Graph(OpRegistry::Global());
109
110 TF_CHECK_OK(NodeBuilder("static_regex_replace_op", "StaticRegexReplace")
111 .Attr("pattern", input_pattern)
112 .Attr("rewrite", rewrite)
113 .Input(test::graph::Constant(g, input))
114 .Attr("replace_global", true)
115 .Finalize(g, nullptr /* node */));
116 return g;
117 }
BM_StaticRegexReplace(::testing::benchmark::State & state)118 static void BM_StaticRegexReplace(::testing::benchmark::State& state) {
119 const int batch_size = state.range(0);
120
121 Tensor input = GetTestTensor(batch_size);
122 Graph* g = SetupStaticGraph(input, kRegExPattern, kRewrite);
123 test::Benchmark("cpu", g, /*old_benchmark_api*/ false).Run(state);
124 state.SetItemsProcessed(static_cast<int64>(state.iterations()));
125 }
126
127 BENCHMARK(BM_StaticRegexReplace)
128 ->UseRealTime()
129 ->Arg(1)
130 ->Arg(8)
131 ->Arg(16)
132 ->Arg(32)
133 ->Arg(64)
134 ->Arg(128)
135 ->Arg(256);
136
137 } // end namespace tensorflow
138