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/tensor.h" 21 #include "tensorflow/core/framework/tensor_shape.pb.h" 22 #include "tensorflow/core/framework/types.h" 23 #include "tensorflow/core/framework/types.pb.h" 24 #include "tensorflow/core/graph/node_builder.h" 25 #include "tensorflow/core/graph/testlib.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 namespace { 35 36 const int kMaxStrLen = 40; 37 38 TensorProto GetRandomInt32TensorProto(int dim, int max_int) { 39 TensorProto tensor_proto; 40 tensor_proto.set_dtype(DT_INT32); 41 tensor_proto.mutable_tensor_shape()->add_dim()->set_size(dim); 42 tensor_proto.mutable_tensor_shape()->set_unknown_rank(false); 43 for (int i = 0; i < dim; ++i) { 44 const int int_val = std::rand() % max_int; 45 tensor_proto.add_int_val(int_val); 46 } 47 return tensor_proto; 48 } 49 50 TensorProto GetRandomInt32TensorProtoWithRepeat(int dim, int repeat, 51 int max_int) { 52 TensorProto tensor_proto; 53 tensor_proto.set_dtype(DT_INT32); 54 tensor_proto.mutable_tensor_shape()->add_dim()->set_size(dim); 55 tensor_proto.mutable_tensor_shape()->set_unknown_rank(false); 56 for (int i = 0; i < dim; ++i) { 57 const int int_val = std::rand() % max_int; 58 for (int j = 0; j < repeat; ++j) { 59 tensor_proto.add_int_val(int_val); 60 } 61 } 62 return tensor_proto; 63 } 64 65 static void BM_Unique_INT32(int iters, int dim, int max_int) { 66 testing::StopTiming(); 67 Graph* g = new Graph(OpRegistry::Global()); 68 69 Tensor input(DT_INT32, TensorShape({dim})); 70 CHECK(input.FromProto(GetRandomInt32TensorProto(dim, max_int))); 71 72 Node* node; 73 TF_CHECK_OK(NodeBuilder(g->NewName("n"), "Unique") 74 .Input(test::graph::Constant(g, input)) 75 .Attr("T", DT_INT32) 76 .Finalize(g, &node)); 77 78 testing::BytesProcessed(static_cast<int64>(iters) * dim * sizeof(int32)); 79 testing::UseRealTime(); 80 testing::StartTiming(); 81 test::Benchmark("cpu", g).Run(iters); 82 } 83 84 static void BM_Unique_INT32_Repeat(int iters, int dim, int max_int) { 85 testing::StopTiming(); 86 Graph* g = new Graph(OpRegistry::Global()); 87 88 Tensor input(DT_INT32, TensorShape({dim * 200})); 89 CHECK( 90 input.FromProto(GetRandomInt32TensorProtoWithRepeat(dim, 200, max_int))); 91 92 Node* node; 93 TF_CHECK_OK(NodeBuilder(g->NewName("n"), "Unique") 94 .Input(test::graph::Constant(g, input)) 95 .Attr("T", DT_INT32) 96 .Finalize(g, &node)); 97 98 testing::BytesProcessed(static_cast<int64>(iters) * dim * 200 * 99 sizeof(int32)); 100 testing::UseRealTime(); 101 testing::StartTiming(); 102 test::Benchmark("cpu", g).Run(iters); 103 } 104 105 TensorProto GetRandomStringsTensorProto(int dim, int max_str_len) { 106 TensorProto tensor_proto; 107 tensor_proto.set_dtype(DT_STRING); 108 tensor_proto.mutable_tensor_shape()->add_dim()->set_size(dim); 109 tensor_proto.mutable_tensor_shape()->set_unknown_rank(false); 110 for (int i = 0; i < dim; ++i) { 111 const int len = std::rand() % max_str_len + 1; 112 string rand_str; 113 rand_str.resize(len); 114 for (int j = 0; j < len; ++j) { 115 rand_str[j] = static_cast<char>(j % 256); 116 } 117 tensor_proto.add_string_val(rand_str); 118 } 119 return tensor_proto; 120 } 121 122 static void BM_Unique_STRING(int iters, int dim) { 123 testing::StopTiming(); 124 Graph* g = new Graph(OpRegistry::Global()); 125 126 Tensor input(DT_STRING, TensorShape({dim})); 127 CHECK(input.FromProto(GetRandomStringsTensorProto(dim, kMaxStrLen))); 128 129 Node* node; 130 TF_CHECK_OK(NodeBuilder(g->NewName("n"), "Unique") 131 .Input(test::graph::Constant(g, input)) 132 .Attr("T", DT_STRING) 133 .Finalize(g, &node)); 134 135 testing::BytesProcessed(static_cast<int64>(iters) * dim * sizeof(string)); 136 testing::UseRealTime(); 137 testing::StartTiming(); 138 test::Benchmark("cpu", g).Run(iters); 139 } 140 141 BENCHMARK(BM_Unique_INT32) 142 ->ArgPair(32, 1024 * 1024) 143 ->ArgPair(256, 1024 * 1024) 144 ->ArgPair(1024, 1024 * 1024) 145 ->ArgPair(4 * 1024, 1024 * 1024) 146 ->ArgPair(16 * 1024, 1024 * 1024) 147 ->ArgPair(64 * 1024, 1024 * 1024) 148 ->ArgPair(1024 * 1024, 1024 * 1024) 149 ->ArgPair(4 * 1024 * 1024, 1024 * 1024) 150 ->ArgPair(32, 64 * 1024 * 1024) 151 ->ArgPair(256, 64 * 1024 * 1024) 152 ->ArgPair(1024, 64 * 1024 * 1024) 153 ->ArgPair(4 * 1024, 64 * 1024 * 1024) 154 ->ArgPair(16 * 1024, 64 * 1024 * 1024) 155 ->ArgPair(64 * 1024, 64 * 1024 * 1024) 156 ->ArgPair(1024 * 1024, 64 * 1024 * 1024) 157 ->ArgPair(4 * 1024 * 1024, 64 * 1024 * 1024); 158 159 BENCHMARK(BM_Unique_INT32_Repeat) 160 ->ArgPair(32, 1024 * 1024) 161 ->ArgPair(256, 1024 * 1024) 162 ->ArgPair(1024, 1024 * 1024) 163 ->ArgPair(4 * 1024, 1024 * 1024) 164 ->ArgPair(16 * 1024, 1024 * 1024) 165 ->ArgPair(64 * 1024, 1024 * 1024) 166 ->ArgPair(1024 * 1024, 1024 * 1024) 167 ->ArgPair(4 * 1024 * 1024, 1024 * 1024) 168 ->ArgPair(32, 32 * 1024 * 1024) 169 ->ArgPair(256, 32 * 1024 * 1024) 170 ->ArgPair(1024, 32 * 1024 * 1024) 171 ->ArgPair(4 * 1024, 32 * 1024 * 1024) 172 ->ArgPair(16 * 1024, 32 * 1024 * 1024) 173 ->ArgPair(64 * 1024, 32 * 1024 * 1024) 174 ->ArgPair(1024 * 1024, 32 * 1024 * 1024) 175 ->ArgPair(32, 64 * 1024 * 1024) 176 ->ArgPair(256, 64 * 1024 * 1024) 177 ->ArgPair(1024, 64 * 1024 * 1024) 178 ->ArgPair(4 * 1024, 64 * 1024 * 1024) 179 ->ArgPair(16 * 1024, 64 * 1024 * 1024) 180 ->ArgPair(64 * 1024, 64 * 1024 * 1024) 181 ->ArgPair(1024 * 1024, 64 * 1024 * 1024); 182 183 BENCHMARK(BM_Unique_STRING) 184 ->Arg(32) 185 ->Arg(256) 186 ->Arg(1024) 187 ->Arg(4 * 1024) 188 ->Arg(16 * 1024) 189 ->Arg(64 * 1024) 190 ->Arg(256 * 1024); 191 192 } // namespace 193 } // namespace tensorflow 194