[CPU] Migrate unquantized MoE to the modular-kernel experts structure (#50133)
Signed-off-by: jiang1.li <jiang1.li@intel.com> Signed-off-by: Fadi Arafeh <fadi.arafeh@arm.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Fadi Arafeh <fadi.arafeh@arm.com>
This commit is contained in:
co-authored by
Claude Opus 5
Fadi Arafeh
parent
b3f97dae24
commit
0a6446005d
@@ -11,6 +11,13 @@
|
||||
#include "cpu/cpu_arch_macros.h"
|
||||
#include "cpu/utils.hpp"
|
||||
|
||||
#if defined(DEFINE_FAST_EXP)
|
||||
#define DEFINE_CPU_FUSED_MOE_EXP DEFINE_FAST_EXP
|
||||
#else
|
||||
#define DEFINE_CPU_FUSED_MOE_EXP \
|
||||
auto fast_exp = [](const vec_op::FP32Vec16& vec) { return vec.exp(); };
|
||||
#endif
|
||||
|
||||
namespace cpu_fused_moe_utils {
|
||||
enum class FusedMOEAct {
|
||||
SiluAndMul,
|
||||
@@ -39,7 +46,7 @@ void swigluoai_and_mul(float* __restrict__ input, scalar_t* __restrict__ output,
|
||||
const int32_t input_stride,
|
||||
const int32_t output_stride) {
|
||||
using scalar_vec_t = typename cpu_utils::VecTypeTrait<scalar_t>::vec_t;
|
||||
#if !defined(__aarch64__)
|
||||
#if defined(__AVX512F__)
|
||||
// For GPT-OSS interleaved gate-up weights
|
||||
alignas(64) static int32_t index[16] = {0, 2, 4, 6, 8, 10, 12, 14,
|
||||
16, 18, 20, 22, 24, 26, 28, 30};
|
||||
@@ -50,7 +57,7 @@ void swigluoai_and_mul(float* __restrict__ input, scalar_t* __restrict__ output,
|
||||
vec_op::FP32Vec16 alpha_vec(1.702);
|
||||
vec_op::FP32Vec16 one_vec(1.0);
|
||||
|
||||
DEFINE_FAST_EXP
|
||||
DEFINE_CPU_FUSED_MOE_EXP
|
||||
|
||||
for (int32_t m = 0; m < m_size; ++m) {
|
||||
for (int32_t n = 0; n < n_size; n += 32) {
|
||||
@@ -59,9 +66,19 @@ void swigluoai_and_mul(float* __restrict__ input, scalar_t* __restrict__ output,
|
||||
vec_op::FP32Vec16 gate_vec(vec_op::uninit);
|
||||
vec_op::FP32Vec16 up_vec(vec_op::uninit);
|
||||
vec_op::FP32Vec16::load_even_odd(input + n, gate_vec, up_vec);
|
||||
#else
|
||||
#elif defined(__AVX512F__)
|
||||
vec_op::FP32Vec16 gate_vec(input + n, index_vec);
|
||||
vec_op::FP32Vec16 up_vec(input + n + 1, index_vec);
|
||||
#else
|
||||
alignas(64) float gate_values[16];
|
||||
alignas(64) float up_values[16];
|
||||
const float* interleaved = input + n;
|
||||
for (int32_t i = 0; i < 16; ++i) {
|
||||
gate_values[i] = interleaved[2 * i];
|
||||
up_values[i] = interleaved[2 * i + 1];
|
||||
}
|
||||
vec_op::FP32Vec16 gate_vec(gate_values);
|
||||
vec_op::FP32Vec16 up_vec(up_values);
|
||||
#endif
|
||||
gate_vec = gate_vec.min(gate_up_max_vec);
|
||||
up_vec = up_vec.clamp(up_min_vec, gate_up_max_vec);
|
||||
@@ -86,7 +103,7 @@ void silu_and_mul(float* __restrict__ input, scalar_t* __restrict__ output,
|
||||
float* __restrict__ up = input + dim;
|
||||
vec_op::FP32Vec16 one_vec(1.0);
|
||||
|
||||
DEFINE_FAST_EXP
|
||||
DEFINE_CPU_FUSED_MOE_EXP
|
||||
|
||||
for (int32_t m = 0; m < m_size; ++m) {
|
||||
for (int32_t n = 0; n < dim; n += 16) {
|
||||
@@ -115,21 +132,13 @@ void gelu_and_mul(float* __restrict__ input, scalar_t* __restrict__ output,
|
||||
vec_op::FP32Vec16 one_vec(1.0);
|
||||
vec_op::FP32Vec16 w1_vec(M_SQRT1_2);
|
||||
vec_op::FP32Vec16 w2_vec(0.5);
|
||||
alignas(64) float temp[16];
|
||||
|
||||
DEFINE_FAST_EXP
|
||||
|
||||
for (int32_t m = 0; m < m_size; ++m) {
|
||||
for (int32_t n = 0; n < dim; n += 16) {
|
||||
vec_op::FP32Vec16 gate_vec(gate + n);
|
||||
vec_op::FP32Vec16 up_vec(up + n);
|
||||
auto er_input_vec = gate_vec * w1_vec;
|
||||
|
||||
er_input_vec.save(temp);
|
||||
for (int32_t i = 0; i < 16; ++i) {
|
||||
temp[i] = std::erf(temp[i]);
|
||||
}
|
||||
vec_op::FP32Vec16 er_vec(temp);
|
||||
auto er_vec = er_input_vec.er();
|
||||
auto gelu = gate_vec * w2_vec * (one_vec + er_vec);
|
||||
auto gated_output_fp32 = up_vec * gelu;
|
||||
scalar_vec_t gated_output = scalar_vec_t(gated_output_fp32);
|
||||
@@ -201,4 +210,6 @@ FORCE_INLINE void apply_gated_act(const FusedMOEAct act,
|
||||
}
|
||||
} // namespace cpu_fused_moe_utils
|
||||
|
||||
#undef DEFINE_CPU_FUSED_MOE_EXP
|
||||
|
||||
#endif
|
||||
|
||||
@@ -678,6 +678,9 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
return FP32Vec16(
|
||||
RVVI(__riscv_vfsub_vv_f32, LMUL_512)(reg, b.reg, VEC_ELEM_NUM));
|
||||
}
|
||||
FP32Vec16 operator-() const {
|
||||
return FP32Vec16(RVVI(__riscv_vfneg_v_f32, LMUL_512)(reg, VEC_ELEM_NUM));
|
||||
}
|
||||
FP32Vec16 operator*(const FP32Vec16& b) const {
|
||||
return FP32Vec16(
|
||||
RVVI(__riscv_vfmul_vv_f32, LMUL_512)(reg, b.reg, VEC_ELEM_NUM));
|
||||
@@ -898,6 +901,25 @@ struct INT8Vec16 : public Vec<INT8Vec16> {
|
||||
}
|
||||
};
|
||||
|
||||
// Reference implementation for vector operations missing from some backends.
|
||||
struct INT8Vec64 {
|
||||
constexpr static int VEC_ELEM_NUM = 64;
|
||||
|
||||
explicit INT8Vec64(const int8_t* ptr) {
|
||||
std::memcpy(data_, ptr, sizeof(data_));
|
||||
}
|
||||
|
||||
void save(int8_t* ptr) const { std::memcpy(ptr, data_, sizeof(data_)); }
|
||||
|
||||
void save(int8_t* ptr, const int elem_num) const {
|
||||
TORCH_CHECK(elem_num > 0 && elem_num <= VEC_ELEM_NUM);
|
||||
std::memcpy(ptr, data_, elem_num);
|
||||
}
|
||||
|
||||
private:
|
||||
int8_t data_[VEC_ELEM_NUM];
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Type Traits & Global Helpers
|
||||
// ============================================================================
|
||||
|
||||
@@ -333,6 +333,13 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
return FP32Vec16(ret);
|
||||
}
|
||||
|
||||
FP32Vec16 operator-() const {
|
||||
f32x16_t ret;
|
||||
unroll_loop<int, VEC_ELEM_NUM>(
|
||||
[&ret, this](int i) { ret.val[i] = -reg.val[i]; });
|
||||
return FP32Vec16(ret);
|
||||
}
|
||||
|
||||
FP32Vec16 operator/(const FP32Vec16& b) const {
|
||||
f32x16_t ret;
|
||||
unroll_loop<int, VEC_ELEM_NUM>(
|
||||
@@ -356,6 +363,10 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
return FP32Vec16(ret);
|
||||
}
|
||||
|
||||
FP32Vec16 clamp(const FP32Vec16& min_v, const FP32Vec16& max_v) const {
|
||||
return this->max(min_v).min(max_v);
|
||||
}
|
||||
|
||||
FP32Vec16 abs() const {
|
||||
f32x16_t ret;
|
||||
unroll_loop<int, VEC_ELEM_NUM>(
|
||||
@@ -363,6 +374,20 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
return FP32Vec16(ret);
|
||||
}
|
||||
|
||||
FP32Vec16 exp() const {
|
||||
f32x16_t ret;
|
||||
unroll_loop<int, VEC_ELEM_NUM>(
|
||||
[&ret, this](int i) { ret.val[i] = std::exp(reg.val[i]); });
|
||||
return FP32Vec16(ret);
|
||||
}
|
||||
|
||||
FP32Vec16 er() const {
|
||||
f32x16_t ret;
|
||||
unroll_loop<int, VEC_ELEM_NUM>(
|
||||
[&ret, this](int i) { ret.val[i] = std::erf(reg.val[i]); });
|
||||
return FP32Vec16(ret);
|
||||
}
|
||||
|
||||
FP32Vec16 tanh() const {
|
||||
f32x16_t ret;
|
||||
unroll_loop<int, VEC_ELEM_NUM>(
|
||||
@@ -404,6 +429,25 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
void save(void* ptr) const { *reinterpret_cast<f32x16_t*>(ptr) = reg; }
|
||||
};
|
||||
|
||||
// Reference implementation for vector operations missing from some backends.
|
||||
struct INT8Vec64 {
|
||||
constexpr static int VEC_ELEM_NUM = 64;
|
||||
|
||||
explicit INT8Vec64(const int8_t* ptr) {
|
||||
std::memcpy(data_, ptr, sizeof(data_));
|
||||
}
|
||||
|
||||
void save(int8_t* ptr) const { std::memcpy(ptr, data_, sizeof(data_)); }
|
||||
|
||||
void save(int8_t* ptr, const int elem_num) const {
|
||||
TORCH_CHECK(elem_num > 0 && elem_num <= VEC_ELEM_NUM);
|
||||
std::memcpy(ptr, data_, elem_num);
|
||||
}
|
||||
|
||||
private:
|
||||
int8_t data_[VEC_ELEM_NUM];
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct VecType {
|
||||
using vec_type = void;
|
||||
|
||||
@@ -655,6 +655,11 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
vec_sub(reg.val[3], b.reg.val[3])}));
|
||||
}
|
||||
|
||||
FP32Vec16 operator-() const {
|
||||
return FP32Vec16(
|
||||
f32x4x4_t({-reg.val[0], -reg.val[1], -reg.val[2], -reg.val[3]}));
|
||||
}
|
||||
|
||||
FP32Vec16 operator/(const FP32Vec16& b) const {
|
||||
return FP32Vec16(f32x4x4_t({vec_div(reg.val[0], b.reg.val[0]),
|
||||
vec_div(reg.val[1], b.reg.val[1]),
|
||||
@@ -756,6 +761,24 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
hi_e.reg.val[0], hi_e.reg.val[1]});
|
||||
}
|
||||
|
||||
FP32Vec16 tanh() const {
|
||||
FP32Vec8 lo(f32x4x2_t{reg.val[0], reg.val[1]});
|
||||
FP32Vec8 hi(f32x4x2_t{reg.val[2], reg.val[3]});
|
||||
auto lo_tanh = lo.tanh();
|
||||
auto hi_tanh = hi.tanh();
|
||||
return FP32Vec16(f32x4x4_t{lo_tanh.reg.val[0], lo_tanh.reg.val[1],
|
||||
hi_tanh.reg.val[0], hi_tanh.reg.val[1]});
|
||||
}
|
||||
|
||||
FP32Vec16 er() const {
|
||||
FP32Vec8 lo(f32x4x2_t{reg.val[0], reg.val[1]});
|
||||
FP32Vec8 hi(f32x4x2_t{reg.val[2], reg.val[3]});
|
||||
auto lo_er = lo.er();
|
||||
auto hi_er = hi.er();
|
||||
return FP32Vec16(f32x4x4_t{lo_er.reg.val[0], lo_er.reg.val[1],
|
||||
hi_er.reg.val[0], hi_er.reg.val[1]});
|
||||
}
|
||||
|
||||
float reduce_max() {
|
||||
__vector float max01 = vec_max(reg.val[0], reg.val[1]);
|
||||
__vector float max23 = vec_max(reg.val[2], reg.val[3]);
|
||||
|
||||
@@ -6,8 +6,10 @@
|
||||
#include <bit>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <torch/all.h>
|
||||
|
||||
namespace vec_op {
|
||||
|
||||
struct fp8_e4m3_tag {};
|
||||
@@ -727,6 +729,11 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
vec_sub(reg.val[3], b.reg.val[3])}));
|
||||
}
|
||||
|
||||
FP32Vec16 operator-() const {
|
||||
return FP32Vec16(f32x4x4_t({vec_neg(reg.val[0]), vec_neg(reg.val[1]),
|
||||
vec_neg(reg.val[2]), vec_neg(reg.val[3])}));
|
||||
}
|
||||
|
||||
FP32Vec16 operator/(const FP32Vec16& b) const {
|
||||
return FP32Vec16(f32x4x4_t({vec_div(reg.val[0], b.reg.val[0]),
|
||||
vec_div(reg.val[1], b.reg.val[1]),
|
||||
@@ -734,6 +741,33 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
vec_div(reg.val[3], b.reg.val[3])}));
|
||||
}
|
||||
|
||||
FP32Vec16 exp() const {
|
||||
FP32Vec8 lo(f32x4x2_t{reg.val[0], reg.val[1]});
|
||||
FP32Vec8 hi(f32x4x2_t{reg.val[2], reg.val[3]});
|
||||
auto lo_exp = lo.exp();
|
||||
auto hi_exp = hi.exp();
|
||||
return FP32Vec16(f32x4x4_t{lo_exp.reg.val[0], lo_exp.reg.val[1],
|
||||
hi_exp.reg.val[0], hi_exp.reg.val[1]});
|
||||
}
|
||||
|
||||
FP32Vec16 tanh() const {
|
||||
FP32Vec8 lo(f32x4x2_t{reg.val[0], reg.val[1]});
|
||||
FP32Vec8 hi(f32x4x2_t{reg.val[2], reg.val[3]});
|
||||
auto lo_tanh = lo.tanh();
|
||||
auto hi_tanh = hi.tanh();
|
||||
return FP32Vec16(f32x4x4_t{lo_tanh.reg.val[0], lo_tanh.reg.val[1],
|
||||
hi_tanh.reg.val[0], hi_tanh.reg.val[1]});
|
||||
}
|
||||
|
||||
FP32Vec16 er() const {
|
||||
FP32Vec8 lo(f32x4x2_t{reg.val[0], reg.val[1]});
|
||||
FP32Vec8 hi(f32x4x2_t{reg.val[2], reg.val[3]});
|
||||
auto lo_er = lo.er();
|
||||
auto hi_er = hi.er();
|
||||
return FP32Vec16(f32x4x4_t{lo_er.reg.val[0], lo_er.reg.val[1],
|
||||
hi_er.reg.val[0], hi_er.reg.val[1]});
|
||||
}
|
||||
|
||||
float reduce_sum() const {
|
||||
__vector float sum = vec_add(vec_add(reg.val[0], reg.val[1]),
|
||||
vec_add(reg.val[2], reg.val[3]));
|
||||
@@ -765,6 +799,17 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
vec_max(reg.val[3], b.reg.val[3])}));
|
||||
}
|
||||
|
||||
FP32Vec16 min(const FP32Vec16& b) const {
|
||||
return FP32Vec16(f32x4x4_t({vec_min(reg.val[0], b.reg.val[0]),
|
||||
vec_min(reg.val[1], b.reg.val[1]),
|
||||
vec_min(reg.val[2], b.reg.val[2]),
|
||||
vec_min(reg.val[3], b.reg.val[3])}));
|
||||
}
|
||||
|
||||
FP32Vec16 clamp(const FP32Vec16& min_v, const FP32Vec16& max_v) const {
|
||||
return this->max(min_v).min(max_v);
|
||||
}
|
||||
|
||||
float reduce_max() const {
|
||||
__vector float m = vec_max(vec_max(reg.val[0], reg.val[1]),
|
||||
vec_max(reg.val[2], reg.val[3]));
|
||||
@@ -899,6 +944,25 @@ struct INT8Vec16 : public Vec<INT8Vec16> {
|
||||
}
|
||||
};
|
||||
|
||||
// Reference implementation for vector operations missing from some backends.
|
||||
struct INT8Vec64 {
|
||||
constexpr static int VEC_ELEM_NUM = 64;
|
||||
|
||||
explicit INT8Vec64(const int8_t* ptr) {
|
||||
std::memcpy(data_, ptr, sizeof(data_));
|
||||
}
|
||||
|
||||
void save(int8_t* ptr) const { std::memcpy(ptr, data_, sizeof(data_)); }
|
||||
|
||||
void save(int8_t* ptr, const int elem_num) const {
|
||||
TORCH_CHECK(elem_num > 0 && elem_num <= VEC_ELEM_NUM);
|
||||
std::memcpy(ptr, data_, elem_num);
|
||||
}
|
||||
|
||||
private:
|
||||
int8_t data_[VEC_ELEM_NUM];
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct VecType {
|
||||
using vec_type = void;
|
||||
@@ -1325,4 +1389,4 @@ FORCE_INLINE void prefetch(const void* addr) {
|
||||
|
||||
}; // namespace vec_op
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#ifndef CPU_TYPES_X86_HPP
|
||||
#define CPU_TYPES_X86_HPP
|
||||
|
||||
#include <cstring>
|
||||
#include <immintrin.h>
|
||||
#include <sleef.h>
|
||||
#include <torch/all.h>
|
||||
@@ -595,6 +596,8 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
|
||||
FP32Vec16 tanh() const { return FP32Vec16(Sleef_tanhf16_u10(reg)); }
|
||||
|
||||
FP32Vec16 er() const { return FP32Vec16(Sleef_erff16_u10(reg)); }
|
||||
|
||||
float reduce_sum() const { return _mm512_reduce_add_ps(reg); }
|
||||
|
||||
float reduce_max() const { return _mm512_reduce_max_ps(reg); }
|
||||
@@ -798,6 +801,14 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
|
||||
return FP32Vec16(low.tanh().reg, high.tanh().reg);
|
||||
}
|
||||
|
||||
FP32Vec16 exp() const {
|
||||
return FP32Vec16(Sleef_expf8_u10(reg_low), Sleef_expf8_u10(reg_high));
|
||||
}
|
||||
|
||||
FP32Vec16 er() const {
|
||||
return FP32Vec16(Sleef_erff8_u10(reg_low), Sleef_erff8_u10(reg_high));
|
||||
}
|
||||
|
||||
FP32Vec16 min(const FP32Vec16& b) const {
|
||||
return FP32Vec16(_mm256_min_ps(reg_low, b.reg_low),
|
||||
_mm256_min_ps(reg_high, b.reg_high));
|
||||
@@ -927,6 +938,30 @@ struct INT8Vec16 : public Vec<INT8Vec16> {
|
||||
for (int i = 0; i < elem_num; ++i) ptr[i] = ar.values[i];
|
||||
}
|
||||
};
|
||||
|
||||
struct INT8Vec64 : public Vec<INT8Vec64> {
|
||||
constexpr static int VEC_ELEM_NUM = 64;
|
||||
|
||||
__m256i reg_low;
|
||||
__m256i reg_high;
|
||||
|
||||
explicit INT8Vec64(const void* ptr)
|
||||
: reg_low(_mm256_loadu_si256(reinterpret_cast<const __m256i*>(ptr))),
|
||||
reg_high(
|
||||
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(ptr) + 1)) {}
|
||||
|
||||
void save(void* ptr) const {
|
||||
_mm256_storeu_si256(reinterpret_cast<__m256i*>(ptr), reg_low);
|
||||
_mm256_storeu_si256(reinterpret_cast<__m256i*>(ptr) + 1, reg_high);
|
||||
}
|
||||
|
||||
void save(int8_t* ptr, const int elem_num) const {
|
||||
TORCH_CHECK(elem_num > 0 && elem_num <= VEC_ELEM_NUM);
|
||||
int8_t values[VEC_ELEM_NUM];
|
||||
save(values);
|
||||
std::memcpy(ptr, values, elem_num);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
|
||||
@@ -71,8 +71,7 @@ class TileGemm82 {
|
||||
scalar_t* __restrict__ curr_m_a = curr_a;
|
||||
vec_op::unroll_loop<int32_t, M>([&](int32_t i) {
|
||||
scalar_t v = *curr_m_a;
|
||||
load_vec_t a_reg_original(v);
|
||||
vec_op::FP32Vec16 a_reg(a_reg_original);
|
||||
vec_op::FP32Vec16 a_reg(static_cast<float>(v));
|
||||
c_regs[i * 2] = c_regs[i * 2] + a_reg * fp32_b_0_reg;
|
||||
c_regs[i * 2 + 1] = c_regs[i * 2 + 1] + a_reg * fp32_b_1_reg;
|
||||
|
||||
|
||||
@@ -614,7 +614,6 @@ TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
|
||||
#endif
|
||||
|
||||
// fused moe
|
||||
#if defined(__AVX512F__) || (defined(ARM_BF16_SUPPORT) && !defined(__APPLE__))
|
||||
ops.def(
|
||||
"prepack_moe_weight(Tensor weight, Tensor(a1!) packed_weight, str isa) "
|
||||
"-> ()");
|
||||
@@ -625,8 +624,6 @@ TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
|
||||
"bool skip_weighted, "
|
||||
"str act, str isa) -> ()");
|
||||
ops.impl("cpu_fused_moe", torch::kCPU, &cpu_fused_moe);
|
||||
#endif // #if defined(__AVX512F__) || (defined(ARM_BF16_SUPPORT) &&
|
||||
// !defined(__APPLE__))
|
||||
#if defined(ARM_I8MM_SUPPORT) && defined(ARM_BF16_SUPPORT) && \
|
||||
!defined(__APPLE__)
|
||||
ops.def(
|
||||
|
||||
@@ -40,12 +40,10 @@ struct VecTypeTrait<c10::BFloat16> {
|
||||
using vec_t = vec_op::BF16Vec16;
|
||||
};
|
||||
|
||||
#if !defined(__powerpc__)
|
||||
template <>
|
||||
struct VecTypeTrait<c10::Half> {
|
||||
using vec_t = vec_op::FP16Vec16;
|
||||
};
|
||||
#endif
|
||||
|
||||
struct Counter {
|
||||
std::atomic<int64_t> counter;
|
||||
|
||||
Reference in New Issue
Block a user