PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
// Generated by CMake. Do not edit directly.
#if defined(ARK_XPU) && defined(ARK_SYCL_TLA)
#if @MOE_SOURCE_MODE@ == 1
#include "sycl_tla_common.hpp"
namespace ark {
void moe_gemm(sycl::queue* q, void* activations, void* weights, void* scales, void* outputs, BTLA_DTYPE dtype,
int N, int K, int* num_tokens_per_expert, int num_experts) {
switch (dtype) {
case BTLA_DTYPE::F16:
sycl_tla_moe_gemm_f16(q, activations, weights, scales, outputs, N, K, num_tokens_per_expert, num_experts);
return;
case BTLA_DTYPE::BF16:
sycl_tla_moe_gemm_bf16(q, activations, weights, scales, outputs, N, K, num_tokens_per_expert, num_experts);
return;
default:
throw std::runtime_error("moe_gemm: unsupported dtype, only BF16/FP16 supported");
}
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 2
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_gemm_helpers.hpp"
namespace ark {
// Host-side tile-policy dispatcher. The three work-group tile policies live in
// their own translation units (`sycl_tla_moe_@MOE_DISPATCH_DTYPE@_n{64,128,256}.cpp`)
// so that no cutlass grouped-GEMM template is instantiated here; see
// `sycl_tla_moe_gemm_helpers.hpp` for why.
void sycl_tla_moe_gemm_@MOE_DISPATCH_DTYPE@(sycl::queue* q, void* activations, void* weights, void* scales,
void* outputs, int N, int K, int* num_tokens_per_expert,
int num_experts) {
switch (moe_gemm_detail::select_tile_policy(N)) {
case moe_gemm_detail::TilePolicy::kN64:
moe_gemm_detail::dispatch_@MOE_DISPATCH_DTYPE@_n64(q, activations, weights, scales, outputs, N, K,
num_tokens_per_expert, num_experts);
return;
case moe_gemm_detail::TilePolicy::kN256:
moe_gemm_detail::dispatch_@MOE_DISPATCH_DTYPE@_n256(q, activations, weights, scales, outputs, N, K,
num_tokens_per_expert, num_experts);
return;
default:
moe_gemm_detail::dispatch_@MOE_DISPATCH_DTYPE@_n128(q, activations, weights, scales, outputs, N, K,
num_tokens_per_expert, num_experts);
return;
}
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 3
#include "sycl_tla_common.hpp"
namespace ark {
void sycl_tla_moe_decode_fill_expert_id(sycl::queue* q, int* expert_id_per_token,
const int* num_tokens_per_expert, int num_experts, int total_tokens) {
if (total_tokens == 0) return;
q->parallel_for(sycl::range<1>(static_cast<size_t>(total_tokens)), [=](sycl::id<1> idx) {
const int token = static_cast<int>(idx[0]);
int offset = 0;
int expert = num_experts - 1;
for (int expert_id = 0; expert_id < num_experts; ++expert_id) {
const int token_count = num_tokens_per_expert[expert_id];
if (token < offset + token_count) {
expert = expert_id;
break;
}
offset += token_count;
}
expert_id_per_token[token] = expert;
});
}
void moe_gemm_decode(sycl::queue* q, void* activations, void* weights, void* scales, void* zeros, void* outputs,
int* expert_id_per_token_buf, BTLA_DTYPE act_dtype, BTLA_DTYPE weight_dtype, int N, int K,
int group_size, int* num_tokens_per_expert, int num_experts, int total_tokens, bool asym) {
MoeDecodeParams params{q, activations, weights, scales, zeros, outputs, expert_id_per_token_buf, act_dtype,
weight_dtype, N, K, group_size, num_tokens_per_expert, num_experts, total_tokens, asym};
// The int4-sym and FP8 DPAS fast paths consume `num_tokens_per_expert`
// directly and never read `expert_id_per_token_buf`. Skipping the fill on
// those paths removes an extra device-timeline kernel launch from the decode
// hot path; every other path (fp, int8, int2, and the scalar int4 / fp8
// fallbacks) still needs the per-token expert mapping.
const bool dpas_fastpath =
(weight_dtype == BTLA_DTYPE::S4_CLIP && sycl_tla_moe_decode_int4_dpas_applicable(params)) ||
((weight_dtype == BTLA_DTYPE::F8_E4M3 || weight_dtype == BTLA_DTYPE::F8_E5M2) &&
sycl_tla_moe_decode_fp8_dpas_applicable(params));
if (!dpas_fastpath) {
sycl_tla_moe_decode_fill_expert_id(q, expert_id_per_token_buf, num_tokens_per_expert, num_experts, total_tokens);
}
if (weight_dtype == BTLA_DTYPE::F16 || weight_dtype == BTLA_DTYPE::BF16) {
if (weight_dtype != act_dtype) {
throw std::invalid_argument("moe_gemm_decode: unquantized weight_dtype must match act_dtype");
}
sycl_tla_moe_decode_fp(params);
return;
}
if (weight_dtype == BTLA_DTYPE::S4_CLIP) {
sycl_tla_moe_decode_int4(params);
return;
}
if (weight_dtype == BTLA_DTYPE::S8) {
sycl_tla_moe_decode_int8(params);
return;
}
if (weight_dtype == BTLA_DTYPE::S2_CLIP) {
sycl_tla_moe_decode_int2(params);
return;
}
if (weight_dtype == BTLA_DTYPE::F8_E4M3 || weight_dtype == BTLA_DTYPE::F8_E5M2) {
if (asym) {
throw std::invalid_argument("moe_gemm_decode(fp8): asym mode is not supported");
}
sycl_tla_moe_decode_fp8(params);
return;
}
throw std::invalid_argument(
"moe_gemm_decode: unsupported weight_dtype (supported: F16, BF16, S8, S4_CLIP, S2_CLIP, F8_E4M3, F8_E5M2)");
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 4
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_decode.hpp"
namespace ark {
void sycl_tla_moe_decode_fp(const MoeDecodeParams& params) {
if (params.act_dtype == BTLA_DTYPE::F16) {
moe_decode_detail::launch_fp<sycl::half>(
params.q, static_cast<const sycl::half*>(params.activations), static_cast<const sycl::half*>(params.weights),
static_cast<sycl::half*>(params.outputs), params.expert_id_per_token, params.total_tokens, params.N,
params.K);
return;
}
if (params.act_dtype == BTLA_DTYPE::BF16) {
using BF = sycl::ext::oneapi::bfloat16;
moe_decode_detail::launch_fp<BF>(
params.q, static_cast<const BF*>(params.activations), static_cast<const BF*>(params.weights),
static_cast<BF*>(params.outputs), params.expert_id_per_token, params.total_tokens, params.N, params.K);
return;
}
throw std::invalid_argument("moe_gemm_decode(fp): act_dtype must be FP16 or BF16");
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 5
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_decode.hpp"
// FP8 per-group DPAS grouped GEMM. Like the S4 decode path, the tile-policy
// instantiations live in the generated `sycl_tla_moe_prefill_fp8_*` translation
// units; this TU only needs the `dispatch_*` declarations and the env-flag /
// shape gates. Instantiating the ladder here instead would pull four cutlass
// grouped-GEMM policies x four group sizes into a single TU and push peak
// compiler RSS into the tens of GiB.
#include "sycl_tla_moe_decode_fp8_helpers.hpp"
#include "sycl_tla_moe_prefill_fp8_helpers.hpp"
namespace ark {
bool sycl_tla_moe_decode_fp8_dpas_applicable(const MoeDecodeParams& params) {
return (params.weight_dtype == BTLA_DTYPE::F8_E4M3 || params.weight_dtype == BTLA_DTYPE::F8_E5M2) && !params.asym &&
(params.act_dtype == BTLA_DTYPE::F16 || params.act_dtype == BTLA_DTYPE::BF16) &&
moe_decode_dpas_fp8_enabled() &&
moe_decode_dpas_fp8_occupancy_ok(params.total_tokens, params.num_experts) &&
moe_prefill_fp8_detail::shape_ok(params.N, params.K, params.group_size);
}
void sycl_tla_moe_decode_fp8(const MoeDecodeParams& params) {
if (params.act_dtype != BTLA_DTYPE::F16 && params.act_dtype != BTLA_DTYPE::BF16) {
throw std::invalid_argument("moe_gemm_decode(fp8): act_dtype must be FP16 or BF16");
}
const bool is_e4m3 = params.weight_dtype == BTLA_DTYPE::F8_E4M3;
// Fast path: FP8 through the decode-phase per-group DPAS grouped GEMM.
// Falls back to the scalar GEMV when the env flag is off, when the batch is
// too small to fill the DPAS M tile (the usual decode case), or when the
// shape gate rejects the tile geometry (e.g. N%64!=0, K%32!=0, unsupported
// group_size). Shares `sycl_tla_moe_decode_fp8_dpas_applicable` with
// `moe_gemm_decode` (which uses it to skip the `fill_expert_id` launch) so
// the two decisions cannot diverge.
if (sycl_tla_moe_decode_fp8_dpas_applicable(params)) {
const MoePrefillParams dpas_params{params.q,
params.activations,
params.weights,
params.scales,
params.zeros,
params.outputs,
nullptr,
params.act_dtype,
params.weight_dtype,
params.N,
params.K,
params.group_size,
params.num_tokens_per_expert,
params.num_experts,
params.total_tokens,
params.asym};
// Decode `A_avg_M` ladder (`_m_8` -> `_m_16` -> `_m_32` -> wide), matching
// the S4 decode ladder rather than the FP8 prefill one, whose
// `<= 512 -> m_32` rung is tuned for prefill-sized batches. The 8-row rung
// matters because decode `A_avg_M` is far below 16, so without it half of
// every M tile is padding and the bandwidth-bound FP8 weights are streamed
// for rows that contribute nothing.
const int average_tokens = params.num_experts > 0 ? params.total_tokens / params.num_experts : 0;
if (params.act_dtype == BTLA_DTYPE::F16) {
if (is_e4m3) {
if (average_tokens <= 4) {
moe_prefill_fp8_detail::dispatch_f16_e4m3_tiny(dpas_params);
} else if (average_tokens <= 8) {
moe_prefill_fp8_detail::dispatch_f16_e4m3_small(dpas_params);
} else if (average_tokens <= 128) {
moe_prefill_fp8_detail::dispatch_f16_e4m3_mid(dpas_params);
} else {
moe_prefill_fp8_detail::dispatch_f16_e4m3_large(dpas_params);
}
} else if (average_tokens <= 4) {
moe_prefill_fp8_detail::dispatch_f16_e5m2_tiny(dpas_params);
} else if (average_tokens <= 8) {
moe_prefill_fp8_detail::dispatch_f16_e5m2_small(dpas_params);
} else if (average_tokens <= 128) {
moe_prefill_fp8_detail::dispatch_f16_e5m2_mid(dpas_params);
} else {
moe_prefill_fp8_detail::dispatch_f16_e5m2_large(dpas_params);
}
} else if (is_e4m3) {
if (average_tokens <= 4) {
moe_prefill_fp8_detail::dispatch_bf16_e4m3_tiny(dpas_params);
} else if (average_tokens <= 8) {
moe_prefill_fp8_detail::dispatch_bf16_e4m3_small(dpas_params);
} else if (average_tokens <= 128) {
moe_prefill_fp8_detail::dispatch_bf16_e4m3_mid(dpas_params);
} else {
moe_prefill_fp8_detail::dispatch_bf16_e4m3_large(dpas_params);
}
} else if (average_tokens <= 4) {
moe_prefill_fp8_detail::dispatch_bf16_e5m2_tiny(dpas_params);
} else if (average_tokens <= 8) {
moe_prefill_fp8_detail::dispatch_bf16_e5m2_small(dpas_params);
} else if (average_tokens <= 128) {
moe_prefill_fp8_detail::dispatch_bf16_e5m2_mid(dpas_params);
} else {
moe_prefill_fp8_detail::dispatch_bf16_e5m2_large(dpas_params);
}
return;
}
// Scalar GEMV fallback. The kernels are not instantiated here: each
// (dtype, format, decode-mode) triple lives in its own generated
// `sycl_tla_moe_decode_fp8_*` translation unit, so this TU instantiates no
// SYCL kernels at all. Expanding them inline means 2 dtypes x 2 formats x 3
// modes x 4 kernels = 48 kernels in one TU, which measured ~14.4 GiB of peak
// compiler RSS. `fp8_decode_mode()` (`ARK_FP8_DECODE_MODE`) picks the
// byte-decode mode on the host exactly as `launch_fp8_by_mode` used to; the
// K-split vs legacy choice still happens inside the selected TU. All
// variants are numerically equivalent.
const moe_dequant::Fp8DecodeMode decode_mode = moe_dequant::fp8_decode_mode();
if (params.act_dtype == BTLA_DTYPE::F16) {
if (is_e4m3) {
switch (decode_mode) {
case moe_dequant::Fp8DecodeMode::kLut:
moe_decode_fp8_detail::dispatch_f16_e4m3_lut(params);
return;
case moe_dequant::Fp8DecodeMode::kBits:
moe_decode_fp8_detail::dispatch_f16_e4m3_bits(params);
return;
default:
moe_decode_fp8_detail::dispatch_f16_e4m3_word(params);
return;
}
}
switch (decode_mode) {
case moe_dequant::Fp8DecodeMode::kLut:
moe_decode_fp8_detail::dispatch_f16_e5m2_lut(params);
return;
case moe_dequant::Fp8DecodeMode::kBits:
moe_decode_fp8_detail::dispatch_f16_e5m2_bits(params);
return;
default:
moe_decode_fp8_detail::dispatch_f16_e5m2_word(params);
return;
}
}
if (is_e4m3) {
switch (decode_mode) {
case moe_dequant::Fp8DecodeMode::kLut:
moe_decode_fp8_detail::dispatch_bf16_e4m3_lut(params);
return;
case moe_dequant::Fp8DecodeMode::kBits:
moe_decode_fp8_detail::dispatch_bf16_e4m3_bits(params);
return;
default:
moe_decode_fp8_detail::dispatch_bf16_e4m3_word(params);
return;
}
}
switch (decode_mode) {
case moe_dequant::Fp8DecodeMode::kLut:
moe_decode_fp8_detail::dispatch_bf16_e5m2_lut(params);
return;
case moe_dequant::Fp8DecodeMode::kBits:
moe_decode_fp8_detail::dispatch_bf16_e5m2_bits(params);
return;
default:
moe_decode_fp8_detail::dispatch_bf16_e5m2_word(params);
return;
}
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 6
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_decode.hpp"
namespace ark {
void sycl_tla_moe_decode_@DECODE_INT_KIND@(const MoeDecodeParams& params) {
if (params.act_dtype == BTLA_DTYPE::F16) {
if (params.asym) {
moe_decode_detail::launch_@DECODE_INT_KIND@<sycl::half, true>(
params.q, static_cast<const sycl::half*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const sycl::half*>(params.scales), static_cast<const sycl::half*>(params.zeros),
static_cast<sycl::half*>(params.outputs), params.expert_id_per_token, params.total_tokens, params.N,
params.K, params.group_size);
} else {
moe_decode_detail::launch_@DECODE_INT_KIND@<sycl::half, false>(
params.q, static_cast<const sycl::half*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const sycl::half*>(params.scales), static_cast<const sycl::half*>(params.zeros),
static_cast<sycl::half*>(params.outputs), params.expert_id_per_token, params.total_tokens, params.N,
params.K, params.group_size);
}
return;
}
if (params.act_dtype == BTLA_DTYPE::BF16) {
using BF = sycl::ext::oneapi::bfloat16;
if (params.asym) {
moe_decode_detail::launch_@DECODE_INT_KIND@<BF, true>(
params.q, static_cast<const BF*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const BF*>(params.scales), static_cast<const BF*>(params.zeros), static_cast<BF*>(params.outputs),
params.expert_id_per_token, params.total_tokens, params.N, params.K, params.group_size);
} else {
moe_decode_detail::launch_@DECODE_INT_KIND@<BF, false>(
params.q, static_cast<const BF*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const BF*>(params.scales), static_cast<const BF*>(params.zeros), static_cast<BF*>(params.outputs),
params.expert_id_per_token, params.total_tokens, params.N, params.K, params.group_size);
}
return;
}
throw std::invalid_argument("moe_gemm_decode(@DECODE_INT_KIND@): act_dtype must be FP16 or BF16");
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 7
#include "sycl_tla_common.hpp"
namespace ark {
void moe_gemm_prefill(sycl::queue* q, void* activations, void* weights, void* scales, void* zeros, void* outputs,
void* dequant_workspace, BTLA_DTYPE act_dtype, BTLA_DTYPE weight_dtype, int N, int K,
int group_size, int* num_tokens_per_expert, int num_experts, int total_tokens, bool asym) {
if (total_tokens == 0) return;
if (weight_dtype == act_dtype && weight_dtype == BTLA_DTYPE::F16) {
sycl_tla_moe_gemm_f16(q, activations, weights, scales, outputs, N, K, num_tokens_per_expert, num_experts);
return;
}
if (weight_dtype == act_dtype && weight_dtype == BTLA_DTYPE::BF16) {
sycl_tla_moe_gemm_bf16(q, activations, weights, scales, outputs, N, K, num_tokens_per_expert, num_experts);
return;
}
MoePrefillParams params{q, activations, weights, scales, zeros, outputs, dequant_workspace, act_dtype, weight_dtype,
N, K, group_size, num_tokens_per_expert, num_experts, total_tokens, asym};
if (sycl_tla_moe_prefill_int8_dpas(params)) return;
if (sycl_tla_moe_prefill_s4_dpas(params)) return;
if (sycl_tla_moe_prefill_lowbit_int8_dpas(params)) return;
if (sycl_tla_moe_prefill_fp8_dpas(params)) return;
if (sycl_tla_moe_prefill_native_fp8(params)) return;
if (dequant_workspace == nullptr) {
throw std::invalid_argument("moe_gemm_prefill: dequant_workspace must be non-null for quantized paths");
}
if (act_dtype == BTLA_DTYPE::F16) {
sycl_tla_moe_prefill_dequant_f16(params);
return;
}
if (act_dtype == BTLA_DTYPE::BF16) {
sycl_tla_moe_prefill_dequant_bf16(params);
return;
}
throw std::invalid_argument("moe_gemm_prefill: act_dtype must be F16 or BF16");
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 8
#include "@MOE_HELPER_HEADER@"
#include "@MOE_DPAS_HEADER@"
namespace ark {
namespace @MOE_NAMESPACE@ {
#if @MOE_DEFINE_FP8_HELPERS@
bool enabled() { return moe_dpas_fp8::moe_prefill_dpas_fp8_enabled(); }
bool shape_ok(int N, int K, int group_size) {
return moe_dpas_fp8::moe_prefill_dpas_fp8_pergroup_shape_ok(N, K, group_size);
}
#endif
#if @MOE_DEFINE_INT8_HELPERS@
bool enabled() { return moe_dpas_int::moe_prefill_dpas_int_enabled(); }
bool shape_ok(int N, int K, int group_size) {
return moe_dpas_int::moe_prefill_dpas_int_pergroup_shape_ok(N, K, group_size);
}
#endif
#if @MOE_DEFINE_S4_HELPERS@
bool enabled() { return moe_dpas_s4::moe_prefill_dpas_s4_enabled(); }
bool shape_ok(int N, int K, int group_size) {
return moe_dpas_s4::moe_prefill_dpas_s4_pergroup_shape_ok(N, K, group_size);
}
#endif
void @MOE_FUNCTION_NAME@(const MoePrefillParams& params) {
using ScalarT = @MOE_SCALAR_TYPE@;
@MOE_DPAS_NAMESPACE@::@MOE_DISPATCH_FUNCTION@<ScalarT, @MOE_POLICY_ARGS@>(
params.q, static_cast<const ScalarT*>(params.activations),
static_cast<const @MOE_WEIGHT_TYPE@*>(params.weights), static_cast<const ScalarT*>(params.scales),
static_cast<ScalarT*>(params.outputs), params.num_tokens_per_expert, params.num_experts, params.N, params.K,
params.group_size, params.total_tokens);
}
} // namespace @MOE_NAMESPACE@
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 9
#include "@MOE_HELPER_HEADER@"
#include "@MOE_DPAS_HEADER@"
namespace ark {
namespace @MOE_NAMESPACE@ {
void @MOE_FUNCTION_NAME@(const MoePrefillParams& params) {
using ScalarT = @MOE_SCALAR_TYPE@;
@MOE_DPAS_NAMESPACE@::@MOE_DISPATCH_FUNCTION@<ScalarT, @MOE_POLICY_ARGS@>(
params.q, static_cast<const ScalarT*>(params.activations),
static_cast<const @MOE_WEIGHT_TYPE@*>(params.weights), static_cast<const @MOE_SCALE_TYPE@*>(params.scales),
static_cast<ScalarT*>(params.outputs), params.num_tokens_per_expert, params.num_experts, params.N, params.K,
params.total_tokens);
}
} // namespace @MOE_NAMESPACE@
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 10
#include "@MOE_HELPER_HEADER@"
#include "@MOE_DPAS_HEADER@"
#include "sycl_tla_moe_prefill_upcast.hpp"
namespace ark {
namespace @MOE_NAMESPACE@ {
void @MOE_FUNCTION_NAME@(const MoePrefillParams& params) {
using ScalarT = @MOE_SCALAR_TYPE@;
auto* upcast_i8 = static_cast<int8_t*>(params.dequant_workspace);
if (params.weight_dtype == BTLA_DTYPE::S4_CLIP) {
moe_mixed_detail::launch_upcast_int4_sym_to_int8(
params.q, static_cast<const uint8_t*>(params.weights), upcast_i8, params.num_experts, params.N, params.K,
params.num_tokens_per_expert);
} else {
moe_mixed_detail::launch_upcast_int2_sym_to_int8(
params.q, static_cast<const uint8_t*>(params.weights), upcast_i8, params.num_experts, params.N, params.K,
params.num_tokens_per_expert);
}
@MOE_DPAS_NAMESPACE@::@MOE_DISPATCH_FUNCTION@<ScalarT, @MOE_POLICY_ARGS@>(
params.q, static_cast<const ScalarT*>(params.activations), upcast_i8, static_cast<const ScalarT*>(params.scales),
static_cast<ScalarT*>(params.outputs), params.num_tokens_per_expert, params.num_experts, params.N, params.K,
params.group_size, params.total_tokens);
}
} // namespace @MOE_NAMESPACE@
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 11
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_mixed.hpp"
namespace ark {
void sycl_tla_moe_prefill_dequant_@MOE_DEQUANT_DTYPE@(const MoePrefillParams& params) {
using ScalarT = @MOE_DEQUANT_SCALAR@;
auto* weights_kn = static_cast<ScalarT*>(params.dequant_workspace);
moe_mixed_detail::dequant_to_KN<ScalarT>(
params.q, params.weights, params.scales, params.zeros, weights_kn, params.weight_dtype, params.num_experts,
params.N, params.K, params.group_size, params.asym, params.num_tokens_per_expert);
moe_gemm(params.q, params.activations, weights_kn, nullptr, params.outputs, params.act_dtype, params.N, params.K,
params.num_tokens_per_expert, params.num_experts);
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 12
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_prefill_fp8_helpers.hpp"
namespace ark {
bool sycl_tla_moe_prefill_fp8_dpas(const MoePrefillParams& params) {
if ((params.weight_dtype != BTLA_DTYPE::F8_E4M3 && params.weight_dtype != BTLA_DTYPE::F8_E5M2) || params.asym ||
!moe_prefill_fp8_detail::enabled() ||
!moe_prefill_fp8_detail::shape_ok(params.N, params.K, params.group_size)) {
return false;
}
const bool is_e4m3 = params.weight_dtype == BTLA_DTYPE::F8_E4M3;
const int average_tokens = params.num_experts > 0 ? params.total_tokens / params.num_experts : 0;
if (params.act_dtype == BTLA_DTYPE::F16) {
if (is_e4m3) {
if (average_tokens <= 8) moe_prefill_fp8_detail::dispatch_f16_e4m3_small(params);
else if (average_tokens <= 32) moe_prefill_fp8_detail::dispatch_f16_e4m3_mid(params);
else moe_prefill_fp8_detail::dispatch_f16_e4m3_large(params);
} else {
if (average_tokens <= 8) moe_prefill_fp8_detail::dispatch_f16_e5m2_small(params);
else if (average_tokens <= 32) moe_prefill_fp8_detail::dispatch_f16_e5m2_mid(params);
else moe_prefill_fp8_detail::dispatch_f16_e5m2_large(params);
}
return true;
}
if (params.act_dtype == BTLA_DTYPE::BF16) {
if (is_e4m3) {
if (average_tokens <= 8) moe_prefill_fp8_detail::dispatch_bf16_e4m3_small(params);
else if (average_tokens <= 32) moe_prefill_fp8_detail::dispatch_bf16_e4m3_mid(params);
else moe_prefill_fp8_detail::dispatch_bf16_e4m3_large(params);
} else {
if (average_tokens <= 8) moe_prefill_fp8_detail::dispatch_bf16_e5m2_small(params);
else if (average_tokens <= 32) moe_prefill_fp8_detail::dispatch_bf16_e5m2_mid(params);
else moe_prefill_fp8_detail::dispatch_bf16_e5m2_large(params);
}
return true;
}
return false;
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 13
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_prefill_fp8_tensor_helpers.hpp"
namespace ark {
void moe_gemm_prefill_fp8_dpas(sycl::queue* q, void* activations, void* weights, void* scales, void* outputs,
BTLA_DTYPE act_dtype, BTLA_DTYPE weight_dtype, int N, int K,
int* num_tokens_per_expert, int num_experts, int total_tokens) {
if (total_tokens == 0) return;
if (weight_dtype != BTLA_DTYPE::F8_E4M3 && weight_dtype != BTLA_DTYPE::F8_E5M2) {
throw std::invalid_argument("moe_gemm_prefill_fp8_dpas: weight_dtype must be F8_E4M3 or F8_E5M2");
}
MoePrefillParams params{q, activations, weights, scales, nullptr, outputs, nullptr, act_dtype, weight_dtype, N, K,
0, num_tokens_per_expert, num_experts, total_tokens, false};
const int average_tokens = num_experts > 0 ? total_tokens / num_experts : 0;
const bool is_e4m3 = weight_dtype == BTLA_DTYPE::F8_E4M3;
if (act_dtype == BTLA_DTYPE::F16) {
if (is_e4m3) {
if (average_tokens <= 8) moe_prefill_fp8_tensor_detail::dispatch_f16_e4m3_small(params);
else if (average_tokens <= 32) moe_prefill_fp8_tensor_detail::dispatch_f16_e4m3_mid(params);
else moe_prefill_fp8_tensor_detail::dispatch_f16_e4m3_large(params);
} else {
if (average_tokens <= 8) moe_prefill_fp8_tensor_detail::dispatch_f16_e5m2_small(params);
else if (average_tokens <= 32) moe_prefill_fp8_tensor_detail::dispatch_f16_e5m2_mid(params);
else moe_prefill_fp8_tensor_detail::dispatch_f16_e5m2_large(params);
}
return;
}
if (act_dtype == BTLA_DTYPE::BF16) {
if (is_e4m3) {
if (average_tokens <= 8) moe_prefill_fp8_tensor_detail::dispatch_bf16_e4m3_small(params);
else if (average_tokens <= 32) moe_prefill_fp8_tensor_detail::dispatch_bf16_e4m3_mid(params);
else moe_prefill_fp8_tensor_detail::dispatch_bf16_e4m3_large(params);
} else {
if (average_tokens <= 8) moe_prefill_fp8_tensor_detail::dispatch_bf16_e5m2_small(params);
else if (average_tokens <= 32) moe_prefill_fp8_tensor_detail::dispatch_bf16_e5m2_mid(params);
else moe_prefill_fp8_tensor_detail::dispatch_bf16_e5m2_large(params);
}
return;
}
throw std::invalid_argument("moe_gemm_prefill_fp8_dpas: act_dtype must be F16 or BF16");
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 14
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_prefill_int8_helpers.hpp"
namespace ark {
bool sycl_tla_moe_prefill_int8_dpas(const MoePrefillParams& params) {
if (params.weight_dtype == BTLA_DTYPE::S8 && !params.asym &&
moe_prefill_int8_detail::enabled() &&
moe_prefill_int8_detail::shape_ok(params.N, params.K, params.group_size)) {
const int average_tokens = params.num_experts > 0 ? params.total_tokens / params.num_experts : 0;
if (params.act_dtype == BTLA_DTYPE::F16) {
if (average_tokens <= 8) moe_prefill_int8_detail::dispatch_f16_small(params);
else if (average_tokens <= 32) moe_prefill_int8_detail::dispatch_f16_mid(params);
else moe_prefill_int8_detail::dispatch_f16_large(params);
return true;
}
if (params.act_dtype == BTLA_DTYPE::BF16) {
if (average_tokens <= 8) moe_prefill_int8_detail::dispatch_bf16_small(params);
else if (average_tokens <= 32) moe_prefill_int8_detail::dispatch_bf16_mid(params);
else moe_prefill_int8_detail::dispatch_bf16_large(params);
return true;
}
}
return false;
}
bool sycl_tla_moe_prefill_lowbit_int8_dpas(const MoePrefillParams& params) {
if ((params.weight_dtype != BTLA_DTYPE::S4_CLIP && params.weight_dtype != BTLA_DTYPE::S2_CLIP) || params.asym ||
!moe_prefill_int8_detail::enabled() ||
!moe_prefill_int8_detail::shape_ok(params.N, params.K, params.group_size) ||
params.dequant_workspace == nullptr ||
(params.act_dtype != BTLA_DTYPE::F16 && params.act_dtype != BTLA_DTYPE::BF16)) {
return false;
}
if (params.act_dtype == BTLA_DTYPE::F16) {
const int average_tokens = params.num_experts > 0 ? params.total_tokens / params.num_experts : 0;
if (average_tokens <= 8) moe_prefill_int8_detail::dispatch_lowbit_f16_small(params);
else if (average_tokens <= 32) moe_prefill_int8_detail::dispatch_lowbit_f16_mid(params);
else moe_prefill_int8_detail::dispatch_lowbit_f16_large(params);
} else {
const int average_tokens = params.num_experts > 0 ? params.total_tokens / params.num_experts : 0;
if (average_tokens <= 8) moe_prefill_int8_detail::dispatch_lowbit_bf16_small(params);
else if (average_tokens <= 32) moe_prefill_int8_detail::dispatch_lowbit_bf16_mid(params);
else moe_prefill_int8_detail::dispatch_lowbit_bf16_large(params);
}
return true;
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 15
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_prefill_int_tensor_helpers.hpp"
namespace ark {
void moe_gemm_prefill_int_dpas(sycl::queue* q, void* activations, void* weights, void* scales, void* outputs,
BTLA_DTYPE act_dtype, BTLA_DTYPE weight_dtype, int N, int K,
int* num_tokens_per_expert, int num_experts, int total_tokens) {
if (total_tokens == 0) return;
if (weight_dtype != BTLA_DTYPE::S8) {
throw std::invalid_argument("moe_gemm_prefill_int_dpas: weight_dtype must be S8");
}
MoePrefillParams params{q, activations, weights, scales, nullptr, outputs, nullptr, act_dtype, weight_dtype, N, K,
0, num_tokens_per_expert, num_experts, total_tokens, false};
const int average_tokens = num_experts > 0 ? total_tokens / num_experts : 0;
if (act_dtype == BTLA_DTYPE::F16) {
if (average_tokens <= 8) moe_prefill_int_tensor_detail::dispatch_f16_small(params);
else if (average_tokens <= 32) moe_prefill_int_tensor_detail::dispatch_f16_mid(params);
else moe_prefill_int_tensor_detail::dispatch_f16_large(params);
return;
}
if (act_dtype == BTLA_DTYPE::BF16) {
if (average_tokens <= 8) moe_prefill_int_tensor_detail::dispatch_bf16_small(params);
else if (average_tokens <= 32) moe_prefill_int_tensor_detail::dispatch_bf16_mid(params);
else moe_prefill_int_tensor_detail::dispatch_bf16_large(params);
return;
}
throw std::invalid_argument("moe_gemm_prefill_int_dpas: act_dtype must be F16 or BF16");
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 16
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_prefill_s4_helpers.hpp"
namespace ark {
bool sycl_tla_moe_prefill_s4_dpas(const MoePrefillParams& params) {
if (params.weight_dtype != BTLA_DTYPE::S4_CLIP || params.asym ||
!moe_prefill_s4_detail::enabled() ||
!moe_prefill_s4_detail::shape_ok(params.N, params.K, params.group_size) ||
(params.act_dtype != BTLA_DTYPE::F16 && params.act_dtype != BTLA_DTYPE::BF16)) {
return false;
}
// Four-tier bucketing on the average tokens-per-expert, matching the
// reference `w4a16` dispatch in vllm-xpu-kernels
// (`grouped_gemm_xe2_interface.hpp`): tiny M uses an 8-row tile, and the
// 32-row tile covers M up to 128 (instead of jumping to the wide large-M
// tile at 33) so mid-size chunked-prefill batches avoid the padding waste of
// the 128-row tile.
const int average_tokens = params.num_experts > 0 ? params.total_tokens / params.num_experts : 0;
if (params.act_dtype == BTLA_DTYPE::F16) {
if (average_tokens <= 4) {
moe_prefill_s4_detail::dispatch_f16_tiny(params);
} else if (average_tokens <= 8) {
moe_prefill_s4_detail::dispatch_f16_small(params);
} else if (average_tokens <= 128) {
moe_prefill_s4_detail::dispatch_f16_mid(params);
} else {
moe_prefill_s4_detail::dispatch_f16_large(params);
}
} else {
if (average_tokens <= 4) {
moe_prefill_s4_detail::dispatch_bf16_tiny(params);
} else if (average_tokens <= 8) {
moe_prefill_s4_detail::dispatch_bf16_small(params);
} else if (average_tokens <= 128) {
moe_prefill_s4_detail::dispatch_bf16_mid(params);
} else {
moe_prefill_s4_detail::dispatch_bf16_large(params);
}
}
return true;
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 17
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_prefill_fp8_native.hpp"
namespace ark {
bool sycl_tla_moe_prefill_native_fp8(const MoePrefillParams& params) {
if ((params.weight_dtype != BTLA_DTYPE::F8_E4M3 && params.weight_dtype != BTLA_DTYPE::F8_E5M2) || params.asym ||
!moe_prefill_native_fp8_detail::moe_prefill_native_fp8_enabled() ||
!moe_prefill_native_fp8_detail::moe_prefill_native_fp8_shape_ok(params.N, params.K, params.group_size)) {
return false;
}
const bool is_e4m3 = params.weight_dtype == BTLA_DTYPE::F8_E4M3;
const bool use_lut = moe_dequant::fp8_decode_use_lut();
if (params.act_dtype == BTLA_DTYPE::F16) {
using ScalarT = sycl::half;
if (is_e4m3) {
if (use_lut) {
moe_prefill_native_fp8_detail::moe_prefill_fp8_native_dispatch<ScalarT, true, true>(
params.q, static_cast<const ScalarT*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const ScalarT*>(params.scales), static_cast<ScalarT*>(params.outputs),
params.num_tokens_per_expert, params.num_experts, params.N, params.K, params.group_size,
params.total_tokens);
} else {
moe_prefill_native_fp8_detail::moe_prefill_fp8_native_dispatch<ScalarT, true, false>(
params.q, static_cast<const ScalarT*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const ScalarT*>(params.scales), static_cast<ScalarT*>(params.outputs),
params.num_tokens_per_expert, params.num_experts, params.N, params.K, params.group_size,
params.total_tokens);
}
} else if (use_lut) {
moe_prefill_native_fp8_detail::moe_prefill_fp8_native_dispatch<ScalarT, false, true>(
params.q, static_cast<const ScalarT*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const ScalarT*>(params.scales), static_cast<ScalarT*>(params.outputs),
params.num_tokens_per_expert, params.num_experts, params.N, params.K, params.group_size,
params.total_tokens);
} else {
moe_prefill_native_fp8_detail::moe_prefill_fp8_native_dispatch<ScalarT, false, false>(
params.q, static_cast<const ScalarT*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const ScalarT*>(params.scales), static_cast<ScalarT*>(params.outputs),
params.num_tokens_per_expert, params.num_experts, params.N, params.K, params.group_size,
params.total_tokens);
}
return true;
}
if (params.act_dtype == BTLA_DTYPE::BF16) {
using ScalarT = sycl::ext::oneapi::bfloat16;
if (is_e4m3) {
if (use_lut) {
moe_prefill_native_fp8_detail::moe_prefill_fp8_native_dispatch<ScalarT, true, true>(
params.q, static_cast<const ScalarT*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const ScalarT*>(params.scales), static_cast<ScalarT*>(params.outputs),
params.num_tokens_per_expert, params.num_experts, params.N, params.K, params.group_size,
params.total_tokens);
} else {
moe_prefill_native_fp8_detail::moe_prefill_fp8_native_dispatch<ScalarT, true, false>(
params.q, static_cast<const ScalarT*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const ScalarT*>(params.scales), static_cast<ScalarT*>(params.outputs),
params.num_tokens_per_expert, params.num_experts, params.N, params.K, params.group_size,
params.total_tokens);
}
} else if (use_lut) {
moe_prefill_native_fp8_detail::moe_prefill_fp8_native_dispatch<ScalarT, false, true>(
params.q, static_cast<const ScalarT*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const ScalarT*>(params.scales), static_cast<ScalarT*>(params.outputs),
params.num_tokens_per_expert, params.num_experts, params.N, params.K, params.group_size,
params.total_tokens);
} else {
moe_prefill_native_fp8_detail::moe_prefill_fp8_native_dispatch<ScalarT, false, false>(
params.q, static_cast<const ScalarT*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const ScalarT*>(params.scales), static_cast<ScalarT*>(params.outputs),
params.num_tokens_per_expert, params.num_experts, params.N, params.K, params.group_size,
params.total_tokens);
}
return true;
}
return false;
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 18
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_decode.hpp"
// S4-sym per-group DPAS grouped GEMM. The tile-policy instantiations live in
// the generated `sycl_tla_moe_prefill_s4_*` translation units; this TU only
// needs the env-flag / shape-gate helpers and the `dispatch_*` declarations.
#include "sycl_tla_moe_prefill_s4_dpas.hpp"
#include "sycl_tla_moe_prefill_s4_helpers.hpp"
namespace ark {
// Both scratch buffers released here are int4-decode-only, so this translation
// unit owns the public entry point declared in `sycl_tla_common.hpp`. The
// buffers themselves live in the extension-wide `DeviceMemoryPool`; see
// `sycl_tla_moe_decode_scratch.hpp`.
void moe_decode_release_scratch() {
moe_decode_detail::release_decode_scratch();
}
bool sycl_tla_moe_decode_int4_dpas_applicable(const MoeDecodeParams& params) {
return params.weight_dtype == BTLA_DTYPE::S4_CLIP && !params.asym &&
(params.act_dtype == BTLA_DTYPE::F16 || params.act_dtype == BTLA_DTYPE::BF16) &&
moe_decode_dpas_s4_enabled() &&
moe_decode_dpas_s4_occupancy_ok(params.total_tokens, params.num_experts) &&
moe_dpas_s4::moe_prefill_dpas_s4_pergroup_shape_ok(params.N, params.K, params.group_size);
}
void sycl_tla_moe_decode_int4(const MoeDecodeParams& params) {
if (params.act_dtype != BTLA_DTYPE::F16 && params.act_dtype != BTLA_DTYPE::BF16) {
throw std::invalid_argument("moe_gemm_decode(int4): act_dtype must be FP16 or BF16");
}
// Fast path: sym int4 through the shared per-group S4 DPAS grouped GEMM.
// Falls back to the scalar GEMV for asym weights (DPAS S4 is sym-only), when
// the env flag is off, when the batch is too small to fill the DPAS M tile
// (the usual decode case -- sym then runs the exact same `launch_int4*`
// kernel as asym, with `Asym=false`), or when the shape gate rejects the tile
// geometry (e.g. N%64!=0, K%32!=0, unsupported group_size). Shares
// `sycl_tla_moe_decode_int4_dpas_applicable` with `moe_gemm_decode` (which
// uses it to skip the `fill_expert_id` launch) so the two decisions cannot
// diverge.
if (sycl_tla_moe_decode_int4_dpas_applicable(params)) {
const MoePrefillParams dpas_params{params.q,
params.activations,
params.weights,
params.scales,
params.zeros,
params.outputs,
nullptr,
params.act_dtype,
params.weight_dtype,
params.N,
params.K,
params.group_size,
params.num_tokens_per_expert,
params.num_experts,
params.total_tokens,
params.asym};
// Same `A_avg_M` tile ladder as the prefill dispatch (`_m_8` -> `_m_16` ->
// `_m_32` -> wide), matching the reference `w4a16` decode dispatch.
// `ARK_MOE_DECODE_S4_DPAS_M8=1` forces the legacy hard-pinned 8-row tile
// for A/B comparison; the two paths are numerically identical and only the
// tile shape differs.
const int average_tokens = params.num_experts > 0 ? params.total_tokens / params.num_experts : 0;
const bool force_m8 = moe_dpas_s4::moe_decode_s4_dpas_force_m8();
if (params.act_dtype == BTLA_DTYPE::F16) {
if (force_m8 || average_tokens <= 4) {
moe_prefill_s4_detail::dispatch_f16_tiny(dpas_params);
} else if (average_tokens <= 8) {
moe_prefill_s4_detail::dispatch_f16_small(dpas_params);
} else if (average_tokens <= 128) {
moe_prefill_s4_detail::dispatch_f16_mid(dpas_params);
} else {
moe_prefill_s4_detail::dispatch_f16_large(dpas_params);
}
} else {
if (force_m8 || average_tokens <= 4) {
moe_prefill_s4_detail::dispatch_bf16_tiny(dpas_params);
} else if (average_tokens <= 8) {
moe_prefill_s4_detail::dispatch_bf16_small(dpas_params);
} else if (average_tokens <= 128) {
moe_prefill_s4_detail::dispatch_bf16_mid(dpas_params);
} else {
moe_prefill_s4_detail::dispatch_bf16_large(dpas_params);
}
}
return;
}
// Scalar FMA GEMV fallback (asym, flag off, or shape gate miss). By default
// this uses the coalesced-load variant, which repacks the weights on-device
// so sub-group loads are contiguous; `ARK_MOE_DECODE_COALESCE_INT4=0` forces
// the legacy per-lane-strided kernel.
const bool coalesce = moe_decode_coalesce_int4_enabled() &&
moe_decode_coalesce_int4_amortized(params.total_tokens, params.num_experts);
if (params.act_dtype == BTLA_DTYPE::F16) {
if (params.asym) {
if (coalesce) {
moe_decode_detail::launch_int4_coalesced<sycl::half, true>(
params.q, static_cast<const sycl::half*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const sycl::half*>(params.scales), static_cast<const sycl::half*>(params.zeros),
static_cast<sycl::half*>(params.outputs), params.expert_id_per_token, params.total_tokens, params.N,
params.K, params.group_size, params.num_experts);
} else {
moe_decode_detail::launch_int4<sycl::half, true>(
params.q, static_cast<const sycl::half*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const sycl::half*>(params.scales), static_cast<const sycl::half*>(params.zeros),
static_cast<sycl::half*>(params.outputs), params.expert_id_per_token, params.total_tokens, params.N,
params.K, params.group_size);
}
} else if (coalesce) {
moe_decode_detail::launch_int4_coalesced<sycl::half, false>(
params.q, static_cast<const sycl::half*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const sycl::half*>(params.scales), static_cast<const sycl::half*>(params.zeros),
static_cast<sycl::half*>(params.outputs), params.expert_id_per_token, params.total_tokens, params.N,
params.K, params.group_size, params.num_experts);
} else {
moe_decode_detail::launch_int4<sycl::half, false>(
params.q, static_cast<const sycl::half*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const sycl::half*>(params.scales), static_cast<const sycl::half*>(params.zeros),
static_cast<sycl::half*>(params.outputs), params.expert_id_per_token, params.total_tokens, params.N,
params.K, params.group_size);
}
return;
}
using BF = sycl::ext::oneapi::bfloat16;
if (params.asym) {
if (coalesce) {
moe_decode_detail::launch_int4_coalesced<BF, true>(
params.q, static_cast<const BF*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const BF*>(params.scales), static_cast<const BF*>(params.zeros), static_cast<BF*>(params.outputs),
params.expert_id_per_token, params.total_tokens, params.N, params.K, params.group_size, params.num_experts);
} else {
moe_decode_detail::launch_int4<BF, true>(
params.q, static_cast<const BF*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const BF*>(params.scales), static_cast<const BF*>(params.zeros), static_cast<BF*>(params.outputs),
params.expert_id_per_token, params.total_tokens, params.N, params.K, params.group_size);
}
} else if (coalesce) {
moe_decode_detail::launch_int4_coalesced<BF, false>(
params.q, static_cast<const BF*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const BF*>(params.scales), static_cast<const BF*>(params.zeros), static_cast<BF*>(params.outputs),
params.expert_id_per_token, params.total_tokens, params.N, params.K, params.group_size, params.num_experts);
} else {
moe_decode_detail::launch_int4<BF, false>(
params.q, static_cast<const BF*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const BF*>(params.scales), static_cast<const BF*>(params.zeros), static_cast<BF*>(params.outputs),
params.expert_id_per_token, params.total_tokens, params.N, params.K, params.group_size);
}
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 19
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_decode.hpp"
#include "sycl_tla_moe_decode_fp8_helpers.hpp"
namespace ark {
namespace moe_decode_fp8_detail {
// One FP8 scalar-GEMV decode mode for one (dtype, format) pair. Instantiates
// exactly four SYCL kernels -- the plain `MoEDecodeKernelFP8` plus the three
// `MoEDecodeKernelFP8KSplit` column factors -- instead of the 48 that expanding
// every dtype/format/mode combination inside `sycl_tla_moe_decode_fp8.cpp`
// would produce. The body is `launch_fp8_by_mode` with `Mode` fixed at
// configure time; the K-split decision is still made here, from the same
// inputs, so behaviour is unchanged.
void @MOE_FUNCTION_NAME@(const MoeDecodeParams& params) {
using ScalarT = @MOE_SCALAR_TYPE@;
const bool ksplit = moe_decode_detail::moe_decode_fp8_ksplit_enabled() &&
moe_decode_detail::moe_decode_fp8_ksplit_shape_ok(params.N, params.K, params.group_size);
moe_decode_detail::launch_fp8_dispatch<ScalarT, @MOE_FORMAT_ARGUMENT@,
moe_dequant::Fp8DecodeMode::@MOE_FP8_DECODE_MODE@>(
params.q, static_cast<const ScalarT*>(params.activations), static_cast<const uint8_t*>(params.weights),
static_cast<const ScalarT*>(params.scales), static_cast<ScalarT*>(params.outputs), params.expert_id_per_token,
params.total_tokens, params.N, params.K, params.group_size, ksplit);
}
} // namespace moe_decode_fp8_detail
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 20
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe.hpp"
#include "sycl_tla_moe_gemm_helpers.hpp"
namespace ark {
namespace moe_gemm_detail {
// One bf16/fp16 grouped MoE GEMM tile policy per translation unit. Expanding
// all three inside `sycl_tla_moe_<dtype>.cpp` put three full cutlass
// grouped-GEMM instantiations in one TU (~2.2 GiB peak compiler RSS); every
// other cutlass TU in this build instantiates exactly one policy. The policy is
// selected on the host by `select_tile_policy()`, so this is numerically
// identical to the fused dispatcher it replaces.
void dispatch_@MOE_DISPATCH_DTYPE@_@MOE_TILE_SUFFIX@(sycl::queue* q, const void* activations, const void* weights,
const void* scales, void* outputs, int N, int K,
int* num_tokens_per_expert, int num_experts) {
using Element = @MOE_DISPATCH_ELEMENT@;
moe_detail::moe_gemm_dispatch<Element, TilePolicy::@MOE_TILE_POLICY@>(
q, static_cast<const Element*>(activations), static_cast<const Element*>(weights),
static_cast<const Element*>(scales), static_cast<Element*>(outputs), N, K, num_tokens_per_expert, num_experts);
}
} // namespace moe_gemm_detail
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 21
// W4A8 dispatcher. Deliberately includes only the cutlass-free front end: the
// tile choice and the public entry points are host code, and every kernel
// lives in one of the per-variant TUs generated by modes 22-25. This TU
// therefore instantiates no kernels at all.
#include "sycl_tla_common.hpp"
#include "sycl_tla_moe_w4a8_helpers.hpp"
namespace ark {
void moe_w4a8_prepack(sycl::queue* q, void* weights_s4, void* scales, void* weights_s8, void* wscales,
BTLA_DTYPE act_dtype, int num_experts, int N, int K, int group_size,
int rescale_group_size) {
moe_w4a8_detail::moe_w4a8_prepack(q, weights_s4, scales, weights_s8, wscales, act_dtype, num_experts, N, K,
group_size, rescale_group_size);
}
void moe_gemm_w4a8(sycl::queue* q, void* activations, void* weights_s8, void* wscales, void* outputs,
BTLA_DTYPE act_dtype, int N, int K, int rescale_block_size, int* num_tokens_per_expert,
int num_experts, int total_tokens, int phase, const void* qact_in, const float* ascale_in,
const int* row_to_token, const float* routing_weights, float* fused_out, int fused_batch) {
moe_w4a8_detail::moe_gemm_w4a8(q, activations, weights_s8, wscales, outputs, act_dtype, N, K, rescale_block_size,
num_tokens_per_expert, num_experts, total_tokens, phase, qact_in, ascale_in,
row_to_token, routing_weights, fused_out, fused_batch);
}
int moe_w4a8_rescale_block_size(int K, int group_size, int rescale_group_size) {
return moe_w4a8_detail::moe_w4a8_rescale_block_size(K, group_size, rescale_group_size);
}
void moe_w4a8_quant_act(sycl::queue* q, const void* activations, void* qact, void* ascale, BTLA_DTYPE act_dtype,
int total_tokens, int K) {
moe_w4a8_detail::moe_w4a8_quant_act(q, activations, qact, ascale, act_dtype, total_tokens, K);
}
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 22
// W4A8 prefill, one DPAS policy per translation unit.
//
// The single TU this replaced instantiated 52 kernels -- 12 of them grouped
// DPAS GEMMs -- and peaked at ~4.2 GB of compiler RSS. Fanning the tile ladder
// out over (dtype x tile) leaves exactly one DPAS kernel here, mirroring how
// `sycl_tla_moe_prefill_s4_*.cpp` splits the S4 prefill.
#include "sycl_tla_moe_w4a8.hpp"
namespace ark {
namespace moe_w4a8_detail {
void @MOE_FUNCTION_NAME@(const W4A8PrefillParams& params) {
moe_w4a8::moe_w4a8_prefill_launch<moe_w4a8::@MOE_POLICY_NAME@, @MOE_SCALAR_TYPE@>(params);
}
} // namespace moe_w4a8_detail
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 23
// W4A8 decode GEMV, one dtype per translation unit (7 kernels: the legacy
// GEMV plus the six K-split (NCOLS, CH) specializations). Takes the
// cutlass-free kernel header, so this TU never parses CuTe.
#include "sycl_tla_moe_w4a8_kernels.hpp"
namespace ark {
namespace moe_w4a8_detail {
void @MOE_FUNCTION_NAME@(const W4A8DecodeParams& params) {
moe_w4a8::moe_w4a8_decode_launch<@MOE_SCALAR_TYPE@>(params);
}
} // namespace moe_w4a8_detail
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 24
// W4A8 per-token activation quantization, one dtype per translation unit
// (11 kernels: the message-width x unroll grid, the single-pass variants and
// the scalar fallback). Cutlass-free.
#include "sycl_tla_moe_w4a8_kernels.hpp"
namespace ark {
namespace moe_w4a8_detail {
void @MOE_FUNCTION_NAME@(const W4A8QuantParams& params) {
moe_w4a8::moe_w4a8_quant_launch<@MOE_SCALAR_TYPE@>(params);
}
} // namespace moe_w4a8_detail
} // namespace ark
#elif @MOE_SOURCE_MODE@ == 25
// W4A8 one-shot AUTO_S8 prepack, one dtype per translation unit (2 kernels:
// the per-block scale reduction and the int4 -> int8 re-scale). Cutlass-free.
#include "sycl_tla_moe_w4a8_kernels.hpp"
namespace ark {
namespace moe_w4a8_detail {
void @MOE_FUNCTION_NAME@(const W4A8PrepackParams& params) {
moe_w4a8::moe_w4a8_prepack_launch<@MOE_SCALAR_TYPE@>(params);
}
} // namespace moe_w4a8_detail
} // namespace ark
#endif
#endif // ARK_XPU && ARK_SYCL_TLA