Skip to content

Introduce 7-bit quantization for Llama in torchchat. #1139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <torchao/experimental/kernels/cpu/aarch64/bitpacking/uint4.h>
#include <torchao/experimental/kernels/cpu/aarch64/bitpacking/uint5.h>
#include <torchao/experimental/kernels/cpu/aarch64/bitpacking/uint6.h>
#include <torchao/experimental/kernels/cpu/aarch64/bitpacking/uint7.h>
#include <torchao/experimental/kernels/cpu/aarch64/tests/test_utils.h>
#include <cassert>

Expand Down Expand Up @@ -601,6 +602,49 @@ void unpack_uint_values<6>(
}
}

// Benchmark utility to compare variants of uint7 packing.
template <>
void pack_uint_values<7>(
uint8_t* packed,
uint8_t* unpacked,
int packed_size,
int unpacked_size,
int variant) {
constexpr int nbit = 7;
pack_uint_odd_bit_values(
torchao::bitpacking::internal::pack_8_uint7_values,
torchao::bitpacking::internal::vec_pack_64_uint7_values,
torchao::bitpacking::internal::vec_pack_128_uint7_values,
nbit,
packed,
unpacked,
packed_size,
unpacked_size,
variant);
}

// Benchmark utility to compare variants of uint7 unpacking.
template <>
void unpack_uint_values<7>(
uint8_t* unpacked,
uint8_t* packed,
int unpacked_size,
int packed_size,
int variant) {
constexpr int nbit = 7;
unpack_uint_odd_bit_values(
torchao::bitpacking::internal::unpack_8_uint7_values,
torchao::bitpacking::internal::vec_unpack_64_uint7_values,
torchao::bitpacking::internal::vec_unpack_128_uint7_values,
nbit,
unpacked,
packed,
unpacked_size,
packed_size,
variant);
}


} // namespace

template <int nbit>
Expand Down Expand Up @@ -653,6 +697,8 @@ BENCHMARK(benchmark_pack_uint_values<5>)->ArgsProduct({{128}, {8, 64, 128}});
BENCHMARK(benchmark_unpack_uint_values<5>)->ArgsProduct({{128}, {8, 64, 128}});
BENCHMARK(benchmark_pack_uint_values<6>)->ArgsProduct({{128}, {8, 64, 128}});
BENCHMARK(benchmark_unpack_uint_values<6>)->ArgsProduct({{128}, {4, 32, 64}});
BENCHMARK(benchmark_pack_uint_values<7>)->ArgsProduct({{128}, {8, 64, 128}});
BENCHMARK(benchmark_unpack_uint_values<7>)->ArgsProduct({{128}, {8, 64, 128}});

// Run the benchmark
BENCHMARK_MAIN();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x1x32_F32_NEONDOT
5);
BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x1x32_F32_NEONDOT(
6);
BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x1x32_F32_NEONDOT(
7);
BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x4x16_F32_NEONDOT(
1);
BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x4x16_F32_NEONDOT(
Expand All @@ -255,6 +257,8 @@ BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x4x16_F32_NEONDOT
5);
BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x4x16_F32_NEONDOT(
6);
BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x4x16_F32_NEONDOT(
7);
BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x4x16_F32_NEONDOT(
1);
BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x8x16_F32_NEONDOT(
Expand All @@ -267,6 +271,8 @@ BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x8x16_F32_NEONDOT
5);
BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x8x16_F32_NEONDOT(
6);
BENCHMARK_CHANNELWISE_8BIT_ACTIVATION_GROUPWISE_LOWBIT_WEIGHT_1x8x16_F32_NEONDOT(
7);

// Run the benchmark
BENCHMARK_MAIN();
79 changes: 55 additions & 24 deletions torchao/experimental/kernels/cpu/aarch64/bitpacking/bitpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <torchao/experimental/kernels/cpu/aarch64/bitpacking/uint4.h>
#include <torchao/experimental/kernels/cpu/aarch64/bitpacking/uint5.h>
#include <torchao/experimental/kernels/cpu/aarch64/bitpacking/uint6.h>
#include <torchao/experimental/kernels/cpu/aarch64/bitpacking/uint7.h>
#include <torchao/experimental/kernels/cpu/aarch64/macro.h>
#include <cassert>

Expand Down Expand Up @@ -79,10 +80,6 @@ TORCHAO_ALWAYS_INLINE inline void vec_pack_32_lowbit_values(
static_assert(nbit < 8);
static_assert(nbit >= 1);

// Currently supported values
static_assert(nbit >= 1);
static_assert(nbit <= 6);

// Shift unpacked values to nonnegative range
int8x16_t shift = vdupq_n_s8(1 << (nbit - 1));
uint8x16_t shifted0 = vreinterpretq_u8_s8(vaddq_s8(unpacked0, shift));
Expand Down Expand Up @@ -144,6 +141,16 @@ TORCHAO_ALWAYS_INLINE inline void vec_pack_32_lowbit_values(
torchao::bitpacking::internal::vec_pack_32_uint6_values(
packed, shifted0, shifted1);
break;
case 7:
uint8_t buffer7[32];
vst1q_u8(buffer7, shifted0);
vst1q_u8(buffer7 + 16, shifted1);

torchao::bitpacking::internal::pack_8_uint7_values(packed, buffer7);
torchao::bitpacking::internal::pack_8_uint7_values(packed + 7, buffer7 + 8);
torchao::bitpacking::internal::pack_8_uint7_values(packed + 14, buffer7 + 16);
torchao::bitpacking::internal::pack_8_uint7_values(packed + 21, buffer7 + 24);
break;
default:
assert(false);
}
Expand All @@ -157,10 +164,6 @@ TORCHAO_ALWAYS_INLINE inline void vec_unpack_32_lowbit_values(
static_assert(nbit < 8);
static_assert(nbit >= 1);

// Currently supported values
static_assert(nbit >= 1);
static_assert(nbit <= 6);

uint8x16_t shifted0;
uint8x16_t shifted1;

Expand Down Expand Up @@ -219,6 +222,18 @@ TORCHAO_ALWAYS_INLINE inline void vec_unpack_32_lowbit_values(
torchao::bitpacking::internal::vec_unpack_32_uint6_values(
shifted0, shifted1, packed);
break;
case 7:
uint8_t buffer7[32];
torchao::bitpacking::internal::unpack_8_uint7_values(buffer7, packed);
torchao::bitpacking::internal::unpack_8_uint7_values(
buffer7 + 8, packed + 7);
torchao::bitpacking::internal::unpack_8_uint7_values(
buffer7 + 16, packed + 14);
torchao::bitpacking::internal::unpack_8_uint7_values(
buffer7 + 24, packed + 21);
shifted0 = vld1q_u8(buffer7);
shifted1 = vld1q_u8(buffer7 + 16);
break;
default:
assert(false);
}
Expand All @@ -239,10 +254,6 @@ TORCHAO_ALWAYS_INLINE inline void vec_pack_64_lowbit_values(
static_assert(nbit < 8);
static_assert(nbit >= 1);

// Currently supported values
static_assert(nbit >= 1);
static_assert(nbit <= 6);

// Shift unpacked values to nonnegative range
int8x16_t shift = vdupq_n_s8(1 << (nbit - 1));
uint8x16_t shifted0 = vreinterpretq_u8_s8(vaddq_s8(unpacked0, shift));
Expand Down Expand Up @@ -277,6 +288,10 @@ TORCHAO_ALWAYS_INLINE inline void vec_pack_64_lowbit_values(
torchao::bitpacking::internal::vec_pack_64_uint6_values(
packed, shifted0, shifted1, shifted2, shifted3);
break;
case 7:
torchao::bitpacking::internal::vec_pack_64_uint7_values(
packed, shifted0, shifted1, shifted2, shifted3);
break;
default:
assert(false);
}
Expand All @@ -292,10 +307,6 @@ TORCHAO_ALWAYS_INLINE inline void vec_unpack_64_lowbit_values(
static_assert(nbit < 8);
static_assert(nbit >= 1);

// Currently supported values
static_assert(nbit >= 1);
static_assert(nbit <= 6);

uint8x16_t shifted0;
uint8x16_t shifted1;
uint8x16_t shifted2;
Expand Down Expand Up @@ -328,6 +339,10 @@ TORCHAO_ALWAYS_INLINE inline void vec_unpack_64_lowbit_values(
torchao::bitpacking::internal::vec_unpack_64_uint6_values(
shifted0, shifted1, shifted2, shifted3, packed);
break;
case 7:
torchao::bitpacking::internal::vec_unpack_64_uint7_values(
shifted0, shifted1, shifted2, shifted3, packed);
break;
default:
assert(false);
}
Expand All @@ -354,10 +369,6 @@ TORCHAO_ALWAYS_INLINE inline void vec_pack_128_lowbit_values(
static_assert(nbit < 8);
static_assert(nbit >= 1);

// Currently supported values
static_assert(nbit >= 1);
static_assert(nbit <= 6);

// Shift unpacked values to nonnegative range
int8x16_t shift = vdupq_n_s8(1 << (nbit - 1));
uint8x16_t shifted0 = vreinterpretq_u8_s8(vaddq_s8(unpacked0, shift));
Expand Down Expand Up @@ -428,6 +439,18 @@ TORCHAO_ALWAYS_INLINE inline void vec_pack_128_lowbit_values(
torchao::bitpacking::internal::vec_pack_64_uint6_values(
packed + 48, shifted4, shifted5, shifted6, shifted7);
break;
case 7:
torchao::bitpacking::internal::vec_pack_128_uint7_values(
packed,
shifted0,
shifted1,
shifted2,
shifted3,
shifted4,
shifted5,
shifted6,
shifted7);
break;
default:
assert(false);
}
Expand All @@ -447,10 +470,6 @@ TORCHAO_ALWAYS_INLINE inline void vec_unpack_128_lowbit_values(
static_assert(nbit < 8);
static_assert(nbit >= 1);

// Currently supported values
static_assert(nbit >= 1);
static_assert(nbit <= 6);

uint8x16_t shifted0;
uint8x16_t shifted1;
uint8x16_t shifted2;
Expand Down Expand Up @@ -519,6 +538,18 @@ TORCHAO_ALWAYS_INLINE inline void vec_unpack_128_lowbit_values(
torchao::bitpacking::internal::vec_unpack_64_uint6_values(
shifted4, shifted5, shifted6, shifted7, packed + 48);
break;
case 7:
torchao::bitpacking::internal::vec_unpack_128_uint7_values(
shifted0,
shifted1,
shifted2,
shifted3,
shifted4,
shifted5,
shifted6,
shifted7,
packed);
break;
default:
assert(false);
}
Expand Down
Loading
Loading