1 /* Copyright 2017 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 <stdlib.h> 17 #include <initializer_list> 18 #include <iterator> 19 #include <vector> 20 #include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h" 21 #include "tensorflow/core/framework/bfloat16.h" 22 #include "tensorflow/core/framework/tensor.h" 23 #include "tensorflow/core/framework/tensor_testutil.h" 24 #include "tensorflow/core/framework/types.pb.h" 25 #include "tensorflow/core/graph/node_builder.h" 26 #include "tensorflow/core/lib/strings/stringprintf.h" 27 #include "tensorflow/core/platform/test.h" 28 #include "tensorflow/core/platform/test_benchmark.h" 29 30 namespace tensorflow { 31 32 // Generate "count" random positive integers (not including zero) with sum 33 // "sum". Technique based on one from https://math.stackexchange.com/a/1276225 34 // but simplified (especially for zero-based indexing). 35 static std::vector<int64> GenerateRandomIntsWithSum(int64 sum, int count) { 36 CHECK_GE(count, 1); 37 CHECK_GE(sum, count); 38 std::vector<int64> temp(count); 39 for (int i = 0; i + 1 < count; ++i) { 40 temp[i] = lrand48() % (sum - count); 41 } 42 temp[count - 1] = sum - count; 43 std::sort(temp.begin(), std::prev(temp.end())); 44 std::vector<int64> result(count); 45 std::adjacent_difference(temp.begin(), temp.end(), result.begin()); 46 for (int i = 0; i < count; ++i) { 47 ++result[i]; 48 } 49 CHECK(std::all_of(result.begin(), result.end(), 50 [sum](int64 x) { return x >= 1 && x <= sum; })); 51 CHECK_EQ(std::accumulate(result.begin(), result.end(), static_cast<int64>(0)), 52 sum); 53 CHECK_EQ(result.size(), count); 54 return result; 55 } 56 57 static Graph* MakeGraph(int split_dim, const std::vector<int64>& size_splits, 58 std::initializer_list<int64> total_size) { 59 Graph* g = new Graph(OpRegistry::Global()); 60 TensorShape in_shape(total_size); 61 Tensor in(DataTypeToEnum<float>::value, in_shape); 62 in.flat<float>().setRandom(); 63 Tensor split_dim_tensor = test::AsScalar<int32>(split_dim); 64 Tensor size_splits_tensor = test::AsTensor<int64>(size_splits); 65 Node* splitv; 66 TF_CHECK_OK(NodeBuilder(g->NewName("splitv"), "SplitV") 67 .Input(test::graph::Constant(g, in)) 68 .Input(test::graph::Constant(g, size_splits_tensor)) 69 .Input(test::graph::Constant(g, split_dim_tensor)) 70 .Attr("num_split", static_cast<int64>(size_splits.size())) 71 .Finalize(g, &splitv)); 72 return g; 73 } 74 75 #define BM_SPLITV_1D(num_split, total_size) \ 76 static void BM_SplitV_1d_##num_split##_##total_size(int iters) { \ 77 testing::StopTiming(); \ 78 testing::ItemsProcessed(static_cast<int64>(iters) * total_size); \ 79 auto label = \ 80 strings::Printf("1-D %d chunks totaling %d", num_split, total_size); \ 81 testing::SetLabel(label); \ 82 testing::UseRealTime(); \ 83 auto g = MakeGraph(/* split_dim = */ 0, \ 84 GenerateRandomIntsWithSum(total_size, num_split), \ 85 {total_size}); \ 86 testing::StartTiming(); \ 87 test::Benchmark("cpu", g).Run(iters); \ 88 } \ 89 BENCHMARK(BM_SplitV_1d_##num_split##_##total_size); 90 91 #define BM_SPLITV_2D(split_dim, num_split, total_size0, total_size1) \ 92 static void \ 93 BM_SplitV_2d_##split_dim##_##num_split##_##total_size0##_##total_size1( \ 94 int iters) { \ 95 testing::StopTiming(); \ 96 std::vector<int64> total_size_vec{total_size0, total_size1}; \ 97 testing::ItemsProcessed(static_cast<int64>(iters) * total_size0 * \ 98 total_size1); \ 99 auto label = \ 100 strings::Printf("2-D %d chunks in dim %d totaling (%d * %d)", \ 101 num_split, split_dim, total_size0, total_size1); \ 102 testing::SetLabel(label); \ 103 testing::UseRealTime(); \ 104 auto g = MakeGraph( \ 105 split_dim, \ 106 GenerateRandomIntsWithSum(total_size_vec[split_dim], num_split), \ 107 {total_size0, total_size1}); \ 108 testing::StartTiming(); \ 109 test::Benchmark("cpu", g).Run(iters); \ 110 } \ 111 BENCHMARK( \ 112 BM_SplitV_2d_##split_dim##_##num_split##_##total_size0##_##total_size1); 113 114 BM_SPLITV_1D(5, 20); 115 BM_SPLITV_1D(262144, 1000000); 116 BM_SPLITV_1D(1, 100000); 117 BM_SPLITV_1D(5, 100000); 118 BM_SPLITV_1D(5, 250000); 119 BM_SPLITV_1D(5, 500000); 120 BM_SPLITV_1D(5, 1000000); 121 BM_SPLITV_1D(10, 4194304); 122 BM_SPLITV_1D(2, 4194304); 123 BM_SPLITV_1D(100, 10240); 124 BM_SPLITV_1D(32768, 1048576); 125 126 BM_SPLITV_2D(0, 1024, 10247, 10); 127 BM_SPLITV_2D(0, 1024, 100000, 10); 128 BM_SPLITV_2D(0, 512, 1024, 256); 129 BM_SPLITV_2D(0, 20, 100000, 5); 130 BM_SPLITV_2D(0, 2, 7, 524288); 131 BM_SPLITV_2D(0, 100, 4096, 512); 132 133 BM_SPLITV_2D(1, 1024, 15, 10240); 134 BM_SPLITV_2D(1, 1024, 10, 100000); 135 BM_SPLITV_2D(1, 512, 1024, 2563); 136 BM_SPLITV_2D(1, 20, 100000, 52); 137 BM_SPLITV_2D(1, 2, 3, 524288); 138 BM_SPLITV_2D(1, 100, 4096, 512); 139 140 } // namespace tensorflow 141