-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new
x8-packq
microkernel that packs and per-row dynamically q…
…uantizes `fp32` to `qp8`. The microkernels themselves are just wrappers for the corresponding KleidiAI kernels. PiperOrigin-RevId: 633914713
- Loading branch information
1 parent
8d5f4e0
commit 7130ee3
Showing
21 changed files
with
952 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#include "packq-benchmark.h" | ||
|
||
#include <xnnpack/aligned-allocator.h> | ||
#include <xnnpack/common.h> | ||
#include <xnnpack/microfnptr.h> | ||
#include <xnnpack/pack.h> | ||
#include <xnnpack/packq.h> | ||
|
||
#include <algorithm> | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <random> | ||
#include <vector> | ||
|
||
#include "bench/utils.h" | ||
#include <benchmark/benchmark.h> | ||
|
||
void x8_packq(benchmark::State& state, xnn_x8_packq_f32qp8_ukernel_fn packq, | ||
size_t mr, size_t kr, size_t sr, | ||
benchmark::utils::IsaCheckFunction isa_check) { | ||
if (isa_check != nullptr && !isa_check(state)) { | ||
return; | ||
} | ||
|
||
const size_t batch = state.range(0); | ||
const size_t dim_m = state.range(2); | ||
const size_t dim_k = state.range(3); | ||
|
||
const size_t rounded_n = benchmark::utils::RoundUp(dim_m, mr); | ||
const size_t rounded_k = benchmark::utils::RoundUp(dim_k, kr); | ||
|
||
std::random_device random_device; | ||
auto rng = std::mt19937(random_device()); | ||
auto f32rng = [&]() { | ||
return std::uniform_real_distribution<float>(-10, 10)(rng); | ||
}; | ||
|
||
// Compute a num_buffers that fit cache with source weights + packed_weights. | ||
const size_t num_buffers = | ||
1 + benchmark::utils::DivideRoundUp<size_t>( | ||
benchmark::utils::GetMaxCacheSize(), | ||
sizeof(int8_t) * batch * | ||
(dim_m * dim_k + rounded_n * rounded_k + rounded_n)); | ||
|
||
std::vector<float, AlignedAllocator<float, 64>> input(num_buffers * batch * | ||
dim_m * dim_k); | ||
std::generate(input.begin(), input.end(), f32rng); | ||
const size_t packed_size = | ||
xnn_x8_packq_f32qp8_packed_size(batch * dim_m, dim_k, mr, kr); | ||
std::vector<int8_t, AlignedAllocator<int8_t, 64>> packed_weights(num_buffers * | ||
packed_size); | ||
|
||
size_t buffer_index = 0; | ||
for (auto _ : state) { | ||
if (++buffer_index == num_buffers) { | ||
buffer_index = 0; | ||
} | ||
|
||
packq(batch * dim_m, dim_k, mr, kr, sr, | ||
/*m_idx_start=*/buffer_index * dim_m, | ||
input.data() + buffer_index * batch * dim_m * dim_k, | ||
dim_k * sizeof(float), packed_weights.data()); | ||
} | ||
|
||
const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); | ||
if (cpu_frequency != 0) { | ||
state.counters["cpufreq"] = cpu_frequency; | ||
} | ||
|
||
const size_t elements_per_iteration = batch * dim_m * dim_k; | ||
state.counters["elements"] = benchmark::Counter( | ||
static_cast<uint64_t>(state.iterations()) * elements_per_iteration, | ||
benchmark::Counter::kIsRate); | ||
|
||
const size_t bytes_per_iteration = | ||
(elements_per_iteration + batch * (rounded_n * rounded_k + rounded_n)) * | ||
sizeof(int8_t); | ||
state.counters["bytes"] = benchmark::Counter( | ||
static_cast<uint64_t>(state.iterations()) * bytes_per_iteration, | ||
benchmark::Counter::kIsRate); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#ifndef __XNNPACK_BENCH_PACKQ_BENCHMARK_H | ||
#define __XNNPACK_BENCH_PACKQ_BENCHMARK_H | ||
|
||
#include <xnnpack/aligned-allocator.h> | ||
#include <xnnpack/common.h> | ||
#include <xnnpack/microfnptr.h> | ||
#include <xnnpack/pack.h> | ||
#include <xnnpack/packq.h> | ||
|
||
#include <cstddef> | ||
|
||
#include "bench/utils.h" | ||
#include <benchmark/benchmark.h> | ||
|
||
void x8_packq(benchmark::State& state, xnn_x8_packq_f32qp8_ukernel_fn packq, | ||
size_t mr, size_t kr, size_t sr, | ||
benchmark::utils::IsaCheckFunction isa_check = nullptr); | ||
|
||
#endif // __XNNPACK_TEST_PACKQ_MICROKERNEL_TESTER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
// | ||
// Auto-generated file. Do not edit! | ||
// Specification: test/x8-packq.yaml | ||
// Generator: tools/generate-packq-test.py | ||
|
||
|
||
#include <xnnpack/common.h> | ||
#include <xnnpack/packq.h> | ||
|
||
#include <benchmark/benchmark.h> | ||
#include "bench/bgemm.h" | ||
#include "bench/packq-benchmark.h" | ||
|
||
|
||
static void x8_packq_f32qp8_ukernel__scalar_u1_mr1( | ||
benchmark::State& state, const char* net) { | ||
x8_packq(state, | ||
xnn_x8_packq_f32qp8_ukernel__scalar_u1, | ||
/*mr=*/1, /*kr=*/1, /*sr=*/1); | ||
} | ||
BENCHMARK_BGEMM(x8_packq_f32qp8_ukernel__scalar_u1_mr1) | ||
|
||
static void x8_packq_f32qp8_ukernel__scalar_u1_mr2( | ||
benchmark::State& state, const char* net) { | ||
x8_packq(state, | ||
xnn_x8_packq_f32qp8_ukernel__scalar_u1, | ||
/*mr=*/2, /*kr=*/1, /*sr=*/1); | ||
} | ||
BENCHMARK_BGEMM(x8_packq_f32qp8_ukernel__scalar_u1_mr2) | ||
|
||
static void x8_packq_f32qp8_ukernel__scalar_u1_mr4( | ||
benchmark::State& state, const char* net) { | ||
x8_packq(state, | ||
xnn_x8_packq_f32qp8_ukernel__scalar_u1, | ||
/*mr=*/4, /*kr=*/1, /*sr=*/1); | ||
} | ||
BENCHMARK_BGEMM(x8_packq_f32qp8_ukernel__scalar_u1_mr4) | ||
|
||
|
||
#ifndef XNNPACK_BENCHMARK_NO_MAIN | ||
BENCHMARK_MAIN(); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.