// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "mlas.h" #include "benchmark/benchmark.h" #include "bench_util.h" #include "core/common/float16.h" using namespace onnxruntime; template void RunRoPEBenchmark(size_t rotary_emb_dim, bool interleaved, benchmark::State& state) { std::vector input(rotary_emb_dim); size_t table_len = interleaved ? rotary_emb_dim / 2 : rotary_emb_dim; std::vector sin_data(table_len); std::vector cos_data(table_len); std::vector output_ref(rotary_emb_dim), output_impl(rotary_emb_dim); for (size_t i = 0; i < rotary_emb_dim; ++i) { input[i] = static_cast(i + 1.0f); } for (size_t i = 0; i < table_len; ++i) { // https://arxiv.org/pdf/2104.09864 section 3.4.3 float theta_i = static_cast(pow(10000, -2.0f * i / rotary_emb_dim)); sin_data[i] = static_cast(std::sin(theta_i)); cos_data[i] = static_cast(std::cos(theta_i)); } // warm up run MlasRotaryEmbedOneRow(&input[0], &sin_data[0], &cos_data[0], rotary_emb_dim, interleaved, &output_impl[0]); for (auto _ : state) { MlasRotaryEmbedOneRow(&input[0], &sin_data[0], &cos_data[0], rotary_emb_dim, interleaved, &output_impl[0]); } } template void RoPE(benchmark::State& state) { using onnxruntime::narrow; const auto rotary_emb_dim = narrow(state.range(0)); const auto interleaved = narrow(state.range(1)); RunRoPEBenchmark(rotary_emb_dim, interleaved, state); } template static void RoPEArgs(benchmark::Benchmark* b) { b->ArgNames({"rotary_emb_dim", "interleaved"}); b->ArgsProduct({ {128, 256, 512, 1024}, // rotary_emb_dim {int64_t{false}, int64_t{true}}, // interleaved }); } BENCHMARK(RoPE)->Apply(RoPEArgs)->UseRealTime(); BENCHMARK(RoPE)->Apply(RoPEArgs)->UseRealTime();