Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 1 | #include <algorithm> |
| 2 | #include <cfloat> |
Marat Dukhan | 4fa0fbe | 2019-10-31 10:23:46 -0700 | [diff] [blame] | 3 | #include <chrono> |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 4 | #include <cmath> |
| 5 | #include <functional> |
| 6 | #include <random> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "bench/utils.h" |
| 10 | #include <xnnpack/common.h> |
| 11 | #include <xnnpack/params.h> |
| 12 | #include <xnnpack/raddexpminusmax.h> |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 13 | #include <xnnpack/raddextexp.h> |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 14 | #include <xnnpack/raddstoreexpminusmax.h> |
| 15 | #include <xnnpack/rmax.h> |
Marat Dukhan | 05ac8e3 | 2019-10-21 15:39:33 -0700 | [diff] [blame] | 16 | #include <xnnpack/vscale.h> |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 17 | #include <xnnpack/vscaleexpminusmax.h> |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 18 | #include <xnnpack/vscaleextexp.h> |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 19 | |
| 20 | #include <benchmark/benchmark.h> |
Marat Dukhan | 8d3c693 | 2020-03-06 20:27:27 -0800 | [diff] [blame] | 21 | #ifdef BENCHMARK_INTEL_DNNL |
| 22 | #include <dnnl.h> |
| 23 | #endif // BENCHMARK_INTEL_DNNL |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 24 | |
| 25 | |
Marat Dukhan | 8d3c693 | 2020-03-06 20:27:27 -0800 | [diff] [blame] | 26 | #ifdef BENCHMARK_INTEL_DNNL |
| 27 | static void DNNLSoftArgMax( |
| 28 | benchmark::State& state) |
| 29 | { |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 30 | const size_t elements = state.range(0); |
Marat Dukhan | 8d3c693 | 2020-03-06 20:27:27 -0800 | [diff] [blame] | 31 | const size_t cache_line_size_max = 128; |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 32 | const size_t packed_elements = benchmark::utils::RoundUp(elements, cache_line_size_max / sizeof(float)); |
Marat Dukhan | 8d3c693 | 2020-03-06 20:27:27 -0800 | [diff] [blame] | 33 | |
| 34 | std::random_device random_device; |
| 35 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 36 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-1000.0f, 1000.0f), std::ref(rng)); |
Marat Dukhan | 8d3c693 | 2020-03-06 20:27:27 -0800 | [diff] [blame] | 37 | |
| 38 | const size_t num_buffers = 1 + |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 39 | benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(), packed_elements * sizeof(float)); |
| 40 | std::vector<float> x(elements); |
| 41 | std::vector<float> y(packed_elements * num_buffers); |
Marat Dukhan | 8d3c693 | 2020-03-06 20:27:27 -0800 | [diff] [blame] | 42 | |
| 43 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 44 | |
| 45 | dnnl_engine_t engine; |
| 46 | if (dnnl_engine_create(&engine, dnnl_cpu, 0) != dnnl_success) { |
| 47 | state.SkipWithError("failed to create CPU engine"); |
| 48 | return; |
| 49 | } |
| 50 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 51 | dnnl_dim_t input_output_shape[1] = { static_cast<int>(elements) }; |
Marat Dukhan | 8d3c693 | 2020-03-06 20:27:27 -0800 | [diff] [blame] | 52 | |
| 53 | dnnl_memory_desc_t memory_descriptor = { 0 }; |
| 54 | if (dnnl_memory_desc_init_by_tag( |
| 55 | &memory_descriptor, 1, input_output_shape, dnnl_f32, dnnl_x) != dnnl_success) |
| 56 | { |
| 57 | state.SkipWithError("failed to create input memory descriptor"); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | dnnl_memory_t input_memory = nullptr; |
| 62 | if (dnnl_memory_create( |
| 63 | &input_memory, &memory_descriptor, engine, x.data()) != dnnl_success) |
| 64 | { |
| 65 | state.SkipWithError("failed to create input memory"); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | dnnl_memory_t output_memory = nullptr; |
| 70 | if (dnnl_memory_create( |
| 71 | &output_memory, &memory_descriptor, engine, y.data()) != dnnl_success) |
| 72 | { |
| 73 | state.SkipWithError("failed to create output memory"); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | dnnl_softmax_desc_t softmax_forward_descriptor = {}; |
| 78 | if (dnnl_softmax_forward_desc_init( |
| 79 | &softmax_forward_descriptor, dnnl_forward_inference, |
| 80 | &memory_descriptor, 0) != dnnl_success) |
| 81 | { |
| 82 | state.SkipWithError("failed to create SoftMax forward descriptor"); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | dnnl_primitive_desc_t softmax_primitive_descriptor = nullptr; |
| 87 | if (dnnl_primitive_desc_create( |
| 88 | &softmax_primitive_descriptor, &softmax_forward_descriptor, |
| 89 | nullptr /* primitive attributes */, engine, nullptr /* hint */) != dnnl_success) |
| 90 | { |
| 91 | state.SkipWithError("failed to create SoftMax primitive descriptor"); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | dnnl_primitive_t softmax_primitive = nullptr; |
| 96 | if (dnnl_primitive_create( |
| 97 | &softmax_primitive, softmax_primitive_descriptor) != dnnl_success) |
| 98 | { |
| 99 | state.SkipWithError("failed to create SoftMax primitive"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | dnnl_exec_arg_t softmax_args[2] = { |
| 104 | {DNNL_ARG_SRC, input_memory}, |
| 105 | {DNNL_ARG_DST, output_memory}, |
| 106 | }; |
| 107 | |
| 108 | dnnl_stream_t stream = nullptr; |
| 109 | if (dnnl_stream_create(&stream, engine, dnnl_stream_default_flags) != dnnl_success) { |
| 110 | state.SkipWithError("failed to create stream"); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | size_t buffer_index = 0; |
| 115 | for (auto _ : state) { |
| 116 | benchmark::utils::PrefetchToL1(x.data(), x.size() * sizeof(float)); |
| 117 | if (++buffer_index == num_buffers) { |
| 118 | buffer_index = 0; |
| 119 | } |
| 120 | |
| 121 | const auto start = std::chrono::high_resolution_clock::now(); |
| 122 | if (dnnl_primitive_execute( |
| 123 | softmax_primitive, stream, 2, softmax_args) != dnnl_success) |
| 124 | { |
| 125 | state.SkipWithError("failed to execute SoftMax"); |
| 126 | return; |
| 127 | } |
| 128 | const auto end = std::chrono::high_resolution_clock::now(); |
| 129 | |
| 130 | const auto elapsed_seconds = |
| 131 | std::chrono::duration_cast<std::chrono::duration<double>>(end - start); |
| 132 | state.SetIterationTime(elapsed_seconds.count()); |
| 133 | } |
| 134 | |
| 135 | if (dnnl_stream_destroy(stream) != dnnl_success) { |
| 136 | state.SkipWithError("failed to destroy stream"); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | if (dnnl_primitive_desc_destroy(softmax_primitive_descriptor) != dnnl_success) { |
| 141 | state.SkipWithError("failed to destroy SoftMax primitive descriptor"); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | if (dnnl_primitive_destroy(softmax_primitive) != dnnl_success) { |
| 146 | state.SkipWithError("failed to destroy SoftMax primitive"); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | if (dnnl_memory_destroy(input_memory) != dnnl_success) { |
| 151 | state.SkipWithError("failed to destroy input memory"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | if (dnnl_memory_destroy(output_memory) != dnnl_success) { |
| 156 | state.SkipWithError("failed to destroy output memory"); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (dnnl_engine_destroy(engine) != dnnl_success) { |
| 161 | state.SkipWithError("failed to destroy engine"); |
| 162 | return; |
| 163 | } |
| 164 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 165 | const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 166 | if (cpu_frequency != 0) { |
| 167 | state.counters["cpufreq"] = cpu_frequency; |
| 168 | } |
| 169 | |
| 170 | const size_t elements_per_iteration = elements; |
Marat Dukhan | 8d3c693 | 2020-03-06 20:27:27 -0800 | [diff] [blame] | 171 | state.counters["elements"] = |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 172 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 173 | |
| 174 | const size_t bytes_per_iteration = 2 * elements * sizeof(float); |
Marat Dukhan | 8d3c693 | 2020-03-06 20:27:27 -0800 | [diff] [blame] | 175 | state.counters["bytes"] = |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 176 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
Marat Dukhan | 8d3c693 | 2020-03-06 20:27:27 -0800 | [diff] [blame] | 177 | } |
| 178 | #endif // BENCHMARK_INTEL_DNNL |
| 179 | |
Marat Dukhan | fd8e689 | 2020-01-27 15:25:25 -0800 | [diff] [blame] | 180 | static void ThreePassSoftMaxWithRecomputing( |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 181 | benchmark::State& state, |
| 182 | xnn_f32_rmax_ukernel_function rmax, |
| 183 | xnn_f32_raddexpminusmax_ukernel_function raddexpminusmax, |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 184 | xnn_f32_vscaleexpminusmax_ukernel_function vscaleexpminusmax, |
| 185 | benchmark::utils::IsaCheckFunction isa_check = nullptr) |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 186 | { |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 187 | if (isa_check && !isa_check(state)) { |
| 188 | return; |
| 189 | } |
| 190 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 191 | const size_t elements = state.range(0); |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 192 | const size_t cache_line_size_max = 128; |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 193 | const size_t packed_elements = benchmark::utils::RoundUp(elements, cache_line_size_max / sizeof(float)); |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 194 | |
| 195 | std::random_device random_device; |
| 196 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 197 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-1000.0f, 1000.0f), std::ref(rng)); |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 198 | |
| 199 | const size_t num_buffers = 1 + |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 200 | benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(), packed_elements * sizeof(float)); |
| 201 | std::vector<float> x(elements); |
| 202 | std::vector<float> y(packed_elements * num_buffers); |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 203 | |
| 204 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 205 | |
| 206 | benchmark::utils::DisableDenormals(); |
| 207 | |
| 208 | size_t buffer_index = 0; |
| 209 | for (auto _ : state) { |
Marat Dukhan | 4232323 | 2019-10-23 02:09:02 -0700 | [diff] [blame] | 210 | benchmark::utils::PrefetchToL1(x.data(), x.size() * sizeof(float)); |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 211 | if (++buffer_index == num_buffers) { |
| 212 | buffer_index = 0; |
| 213 | } |
| 214 | |
| 215 | const auto start = std::chrono::high_resolution_clock::now(); |
| 216 | float x_max = nanf(""); |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 217 | rmax(elements * sizeof(float), x.data(), &x_max); |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 218 | float y_sum = nanf(""); |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 219 | raddexpminusmax(elements * sizeof(float), x.data(), &y_sum, x_max); |
| 220 | vscaleexpminusmax(elements * sizeof(float), x.data(), y.data() + packed_elements * buffer_index, x_max, 1.0f / y_sum); |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 221 | const auto end = std::chrono::high_resolution_clock::now(); |
| 222 | |
| 223 | const auto elapsed_seconds = |
| 224 | std::chrono::duration_cast<std::chrono::duration<double>>(end - start); |
| 225 | state.SetIterationTime(elapsed_seconds.count()); |
| 226 | } |
| 227 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 228 | const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 229 | if (cpu_frequency != 0) { |
| 230 | state.counters["cpufreq"] = cpu_frequency; |
| 231 | } |
| 232 | |
| 233 | const size_t elements_per_iteration = elements; |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 234 | state.counters["elements"] = |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 235 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 236 | |
| 237 | const size_t bytes_per_iteration = 2 * elements * sizeof(float); |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 238 | state.counters["bytes"] = |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 239 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Marat Dukhan | fd8e689 | 2020-01-27 15:25:25 -0800 | [diff] [blame] | 242 | static void ThreePassSoftMaxWithReloading( |
Marat Dukhan | 05ac8e3 | 2019-10-21 15:39:33 -0700 | [diff] [blame] | 243 | benchmark::State& state, |
| 244 | xnn_f32_rmax_ukernel_function rmax, |
| 245 | xnn_f32_raddstoreexpminusmax_ukernel_function raddstoreexpminusmax, |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 246 | xnn_f32_vscale_ukernel_function vscale, |
| 247 | benchmark::utils::IsaCheckFunction isa_check = nullptr) |
Marat Dukhan | 05ac8e3 | 2019-10-21 15:39:33 -0700 | [diff] [blame] | 248 | { |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 249 | if (isa_check && !isa_check(state)) { |
| 250 | return; |
| 251 | } |
| 252 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 253 | const size_t elements = state.range(0); |
Marat Dukhan | 05ac8e3 | 2019-10-21 15:39:33 -0700 | [diff] [blame] | 254 | const size_t cache_line_size_max = 128; |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 255 | const size_t packed_elements = benchmark::utils::RoundUp(elements, cache_line_size_max / sizeof(float)); |
Marat Dukhan | 05ac8e3 | 2019-10-21 15:39:33 -0700 | [diff] [blame] | 256 | |
| 257 | std::random_device random_device; |
| 258 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 259 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-1000.0f, 1000.0f), std::ref(rng)); |
Marat Dukhan | 05ac8e3 | 2019-10-21 15:39:33 -0700 | [diff] [blame] | 260 | |
| 261 | const size_t num_buffers = 1 + |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 262 | benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(), packed_elements * sizeof(float)); |
| 263 | std::vector<float> x(elements); |
| 264 | std::vector<float> y(packed_elements * num_buffers); |
Marat Dukhan | 05ac8e3 | 2019-10-21 15:39:33 -0700 | [diff] [blame] | 265 | |
| 266 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 267 | |
| 268 | benchmark::utils::DisableDenormals(); |
| 269 | |
| 270 | size_t buffer_index = 0; |
| 271 | for (auto _ : state) { |
Marat Dukhan | 4232323 | 2019-10-23 02:09:02 -0700 | [diff] [blame] | 272 | benchmark::utils::PrefetchToL1(x.data(), x.size() * sizeof(float)); |
Marat Dukhan | 05ac8e3 | 2019-10-21 15:39:33 -0700 | [diff] [blame] | 273 | if (++buffer_index == num_buffers) { |
| 274 | buffer_index = 0; |
| 275 | } |
| 276 | |
| 277 | const auto start = std::chrono::high_resolution_clock::now(); |
| 278 | float x_max = nanf(""); |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 279 | rmax(elements * sizeof(float), x.data(), &x_max); |
Marat Dukhan | 05ac8e3 | 2019-10-21 15:39:33 -0700 | [diff] [blame] | 280 | float y_sum = nanf(""); |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 281 | raddstoreexpminusmax(elements * sizeof(float), x.data(), y.data() + packed_elements * buffer_index, &y_sum, x_max); |
| 282 | vscale(elements * sizeof(float), y.data() + packed_elements * buffer_index, y.data() + packed_elements * buffer_index, 1.0f / y_sum); |
Marat Dukhan | 05ac8e3 | 2019-10-21 15:39:33 -0700 | [diff] [blame] | 283 | const auto end = std::chrono::high_resolution_clock::now(); |
| 284 | |
| 285 | const auto elapsed_seconds = |
| 286 | std::chrono::duration_cast<std::chrono::duration<double>>(end - start); |
| 287 | state.SetIterationTime(elapsed_seconds.count()); |
| 288 | } |
| 289 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 290 | const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 291 | if (cpu_frequency != 0) { |
| 292 | state.counters["cpufreq"] = cpu_frequency; |
| 293 | } |
| 294 | |
| 295 | const size_t elements_per_iteration = elements; |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 296 | state.counters["elements"] = |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 297 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 298 | |
| 299 | const size_t bytes_per_iteration = 2 * elements * sizeof(float); |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 300 | state.counters["bytes"] = |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 301 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Marat Dukhan | fd8e689 | 2020-01-27 15:25:25 -0800 | [diff] [blame] | 304 | static void TwoPassSoftMax( |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 305 | benchmark::State& state, |
| 306 | xnn_f32_raddextexp_ukernel_function raddextexp, |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 307 | xnn_f32_vscaleextexp_ukernel_function vscaleextexp, |
| 308 | benchmark::utils::IsaCheckFunction isa_check = nullptr) |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 309 | { |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 310 | if (isa_check && !isa_check(state)) { |
| 311 | return; |
| 312 | } |
| 313 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 314 | const size_t elements = state.range(0); |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 315 | const size_t cache_line_size_max = 128; |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 316 | const size_t packed_elements = benchmark::utils::RoundUp(elements, cache_line_size_max / sizeof(float)); |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 317 | |
| 318 | std::random_device random_device; |
| 319 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 320 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-1000.0f, 1000.0f), std::ref(rng)); |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 321 | |
| 322 | const size_t num_buffers = 1 + |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 323 | benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(), packed_elements * sizeof(float)); |
| 324 | std::vector<float> x(elements); |
| 325 | std::vector<float> y(packed_elements * num_buffers); |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 326 | |
| 327 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 328 | |
| 329 | benchmark::utils::DisableDenormals(); |
| 330 | |
| 331 | size_t buffer_index = 0; |
| 332 | for (auto _ : state) { |
| 333 | benchmark::utils::PrefetchToL1(x.data(), x.size() * sizeof(float)); |
| 334 | if (++buffer_index == num_buffers) { |
| 335 | buffer_index = 0; |
| 336 | } |
| 337 | |
| 338 | const auto start = std::chrono::high_resolution_clock::now(); |
| 339 | float scale[2]; |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 340 | raddextexp(elements * sizeof(float), x.data(), scale); |
| 341 | vscaleextexp(elements * sizeof(float), x.data(), y.data() + packed_elements * buffer_index, 1.0f / scale[0], -scale[1]); |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 342 | const auto end = std::chrono::high_resolution_clock::now(); |
| 343 | |
| 344 | const auto elapsed_seconds = |
| 345 | std::chrono::duration_cast<std::chrono::duration<double>>(end - start); |
| 346 | state.SetIterationTime(elapsed_seconds.count()); |
| 347 | } |
| 348 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 349 | const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 350 | if (cpu_frequency != 0) { |
| 351 | state.counters["cpufreq"] = cpu_frequency; |
| 352 | } |
| 353 | |
| 354 | const size_t elements_per_iteration = elements; |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 355 | state.counters["elements"] = |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 356 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 357 | |
| 358 | const size_t bytes_per_iteration = 2 * elements * sizeof(float); |
Marat Dukhan | 4a2bbc6 | 2019-10-25 17:36:32 -0700 | [diff] [blame] | 359 | state.counters["bytes"] = |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 360 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
Marat Dukhan | 05ac8e3 | 2019-10-21 15:39:33 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 363 | static void CharacteristicArguments(benchmark::internal::Benchmark* b) { |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 364 | for (int32_t n = 1000; n <= 100000000; n *= 10) { |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 365 | b->Arg(n); |
| 366 | b->Arg(3 * n); |
| 367 | } |
| 368 | } |
| 369 | |
Marat Dukhan | 8d3c693 | 2020-03-06 20:27:27 -0800 | [diff] [blame] | 370 | #ifdef BENCHMARK_INTEL_DNNL |
| 371 | BENCHMARK(DNNLSoftArgMax)->Apply(CharacteristicArguments)->UseManualTime(); |
| 372 | #endif |
| 373 | |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 374 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
Marat Dukhan | b6862d1 | 2020-03-08 15:26:49 -0700 | [diff] [blame] | 375 | BENCHMARK_CAPTURE(TwoPassSoftMax, avx2_p5, |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 376 | xnn_f32_raddextexp_ukernel__avx2_p5_x96, |
| 377 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x40, |
| 378 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseManualTime(); |
Marat Dukhan | b6862d1 | 2020-03-08 15:26:49 -0700 | [diff] [blame] | 379 | BENCHMARK_CAPTURE(ThreePassSoftMaxWithRecomputing, avx2_p5, |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 380 | xnn_f32_rmax_ukernel__avx, |
| 381 | xnn_f32_raddexpminusmax_ukernel__avx2_p5_x96, |
| 382 | xnn_f32_vscaleexpminusmax_ukernel__avx2_p5_x24, |
| 383 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseManualTime(); |
Marat Dukhan | b6862d1 | 2020-03-08 15:26:49 -0700 | [diff] [blame] | 384 | BENCHMARK_CAPTURE(ThreePassSoftMaxWithReloading, avx2_p5, |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 385 | xnn_f32_rmax_ukernel__avx, |
| 386 | xnn_f32_raddstoreexpminusmax_ukernel__avx2_p5_x64_acc2, |
Frank Barchard | beca652 | 2020-10-30 22:34:35 -0700 | [diff] [blame] | 387 | xnn_f32_vscale_ukernel__avx_x32, |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 388 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseManualTime(); |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 389 | |
Marat Dukhan | b6862d1 | 2020-03-08 15:26:49 -0700 | [diff] [blame] | 390 | BENCHMARK_CAPTURE(TwoPassSoftMax, avx512f_p5_scalef, |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 391 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x144_acc3, |
| 392 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x16, |
| 393 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseManualTime(); |
Marat Dukhan | b6862d1 | 2020-03-08 15:26:49 -0700 | [diff] [blame] | 394 | BENCHMARK_CAPTURE(ThreePassSoftMaxWithRecomputing, avx512f_p5_scalef, |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 395 | xnn_f32_rmax_ukernel__avx512f, |
| 396 | xnn_f32_raddexpminusmax_ukernel__avx512f_p5_scalef_x128_acc4, |
| 397 | xnn_f32_vscaleexpminusmax_ukernel__avx512f_p5_scalef_x16, |
| 398 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseManualTime(); |
Marat Dukhan | b6862d1 | 2020-03-08 15:26:49 -0700 | [diff] [blame] | 399 | BENCHMARK_CAPTURE(ThreePassSoftMaxWithReloading, avx512f_p5_scalef, |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 400 | xnn_f32_rmax_ukernel__avx512f, |
| 401 | xnn_f32_raddstoreexpminusmax_ukernel__avx512f_p5_scalef_x128_acc2, |
Frank Barchard | beca652 | 2020-10-30 22:34:35 -0700 | [diff] [blame] | 402 | xnn_f32_vscale_ukernel__avx512f_x64, |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 403 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseManualTime(); |
Marat Dukhan | 4a4a7fa | 2019-10-21 13:46:14 -0700 | [diff] [blame] | 404 | #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 405 | |
| 406 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 407 | BENCHMARK_MAIN(); |
| 408 | #endif |