use anyhow::{anyhow, ensure, Context, Result};
use mlx_native::ops::dense_matvec_f32_rows::DENSE_MATVEC_F32_ROWS_MAX_ROWS;
use mlx_native::ops::dflash2_attention_pack::dispatch_dflash2_mask_moe_routes;
use mlx_native::ops::moe_softmax_topk::dispatch_moe_softmax_topk_simd;
use mlx_native::ops::moe_weighted_reduce::{
dispatch_moe_weighted_reduce, dispatch_moe_weighted_reduce_masked,
};
use mlx_native::ops::quantized_matmul_ggml::GgmlType;
use mlx_native::{
moe_tiles_down, moe_tiles_gate_up, moe_tiles_gather, moe_tiles_grouped_rows,
moe_tiles_schedule, CommandEncoder, DType, KernelRegistry, MlxBuffer, MlxDevice,
MoeTilesBuffers, MoeTilesParams, MOE_TILES_GRID_WORDS,
};
use super::decode_pool::{with_id_mm_scratch, MmIdSlot};
use super::ffn::MoeFfnShape;
use super::gpu_ffn::MoeFfnWeightsGpuQ;
use super::moe_batched::{
encode_native_dense_proj, pooled, DFlash2MoeRouteMask, MoeBatchedRouting, MoeDenseRole,
QWEN_MOE_LIVE_TILES_ENV,
};
pub(super) const BLOCK_ROWS: u32 = DENSE_MATVEC_F32_ROWS_MAX_ROWS;
struct TileScratch {
gathered: MlxBuffer,
intermediate: MlxBuffer,
grids: MlxBuffer,
}
impl TileScratch {
fn pooled(device: &MlxDevice, params: &MoeTilesParams, label: &str) -> Result<Self> {
let rows = moe_tiles_grouped_rows(params.n_tokens, params.top_k, params.n_experts)
.map_err(|e| anyhow!("{label} tile rows: {e}"))?;
let (h, m) = (params.hidden as usize, params.intermediate as usize);
Ok(Self {
gathered: pooled(device, rows * h, DType::BF16, vec![rows, h], label)?,
intermediate: pooled(device, rows * m, DType::BF16, vec![rows, m], label)?,
grids: pooled(
device,
MOE_TILES_GRID_WORDS,
DType::U32,
vec![MOE_TILES_GRID_WORDS],
label,
)?,
})
}
fn buffers(&self) -> MoeTilesBuffers<'_> {
MoeTilesBuffers {
gathered: &self.gathered,
intermediate: &self.intermediate,
grids: &self.grids,
}
}
}
pub(super) fn row_block(buffer: &MlxBuffer, start: u32, count: u32, width: u32) -> MlxBuffer {
buffer.slice_view(
u64::from(start) * u64::from(width) * DType::F32.size_of() as u64,
count as usize * width as usize,
)
}
#[allow(clippy::too_many_arguments)]
fn encode_row_exact_proj(
enc: &mut CommandEncoder,
registry: &mut KernelRegistry,
device: &MlxDevice,
input: &MlxBuffer,
weights: &MoeFfnWeightsGpuQ,
role: MoeDenseRole,
output: &MlxBuffer,
rows: u32,
in_features: u32,
out_features: u32,
) -> Result<()> {
for start in (0..rows).step_by(BLOCK_ROWS as usize) {
let count = BLOCK_ROWS.min(rows - start);
encode_native_dense_proj(
enc,
registry,
device,
&row_block(input, start, count, in_features),
weights,
role,
&row_block(output, start, count, out_features),
count,
in_features,
out_features,
)?;
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(super) fn build_moe_ffn_layer_tile_stages_into(
enc: &mut CommandEncoder,
device: &MlxDevice,
registry: &mut KernelRegistry,
x: &MlxBuffer,
weights: &MoeFfnWeightsGpuQ,
shape: MoeFfnShape,
residual: Option<&MlxBuffer>,
out: &mut MlxBuffer,
rows: u32,
layer_idx: usize,
route_mask: Option<DFlash2MoeRouteMask<'_>>,
) -> Result<MoeBatchedRouting> {
let quantized = |t: GgmlType| matches!(t, GgmlType::Q4_K | GgmlType::Q6_K);
ensure!(
matches!(shape.num_experts_per_tok, 1 | 6 | 8)
&& shape.hidden_size % 256 == 0
&& shape.moe_intermediate_size % 256 == 0
&& shape.shared_intermediate_size % 256 == 0
&& quantized(weights.ggml_type_gate_up)
&& quantized(weights.ggml_type_down)
&& !crate::quantize::imatrix::is_active(),
"{QWEN_MOE_LIVE_TILES_ENV}=1 requires top-k 1/6/8, expert widths that are multiples of \
256, Q4_K/Q6_K native expert stacks and no imatrix collector"
);
let prepared = weights.experts.completed()
.context("live-tile route requires canonical completed expert planes")?;
let live = weights
.live_tiles
.as_ref()
.context("the shared expert's live-tile planes were not prepared at model upload")?;
let context = || format!("live-tile MoE layer {layer_idx}");
let r = rows as usize;
let h = shape.hidden_size as usize;
let top_k = shape.num_experts_per_tok as usize;
let ne = shape.num_experts;
ensure!(
x.element_count() >= r * h,
"live-tile MoE layer {layer_idx}: input has {} values, need {rows} x {h}",
x.element_count()
);
let pairs = r * top_k;
let routed = MoeTilesParams {
n_tokens: rows,
top_k: shape.num_experts_per_tok,
n_experts: ne,
hidden: shape.hidden_size,
intermediate: shape.moe_intermediate_size,
masked_routes: route_mask.is_some(),
column_tile: live.column_tile,
shared_planes: false,
};
let shared_blocks = rows.div_ceil(BLOCK_ROWS);
let shared = MoeTilesParams {
n_tokens: rows,
top_k: 1,
n_experts: shared_blocks,
hidden: shape.hidden_size,
intermediate: shape.shared_intermediate_size,
masked_routes: false,
column_tile: live.column_tile,
shared_planes: true,
};
let logits = pooled(
device,
r * ne as usize,
DType::F32,
vec![r, ne as usize],
"router logits",
)?;
let sh_logit = pooled(device, r, DType::F32, vec![r, 1], "shared gate logit")?;
let y_s = pooled(device, r * h, DType::F32, vec![r, h], "shared down")?;
let ids = pooled(device, pairs, DType::U32, vec![pairs], "expert ids")?;
let routing_weights = pooled(device, pairs, DType::F32, vec![pairs], "routing weights")?;
let y_all = pooled(device, pairs * h, DType::F32, vec![pairs, h], "expert down")?;
let routed_scratch = TileScratch::pooled(device, &routed, "routed expert tiles")?;
let shared_scratch = TileScratch::pooled(device, &shared, "shared expert tiles")?;
let mut shared_ids = pooled(device, r, DType::U32, vec![r], "shared expert blocks")?;
for (row, block) in shared_ids
.as_mut_slice::<u32>()
.map_err(|e| anyhow!("{}: shared expert blocks: {e}", context()))?[..r]
.iter_mut()
.enumerate()
{
*block = row as u32 / BLOCK_ROWS;
}
let dummy_residual;
let residual_ref = match residual {
Some(buffer) => buffer,
None => {
dummy_residual = pooled(device, 1, DType::F32, vec![1], "dummy residual")?;
&dummy_residual
}
};
let h32 = shape.hidden_size;
let tile_error = |stage: &str, e: mlx_native::MlxError| anyhow!("{} {stage}: {e}", context());
let routed_slot =
|f: &mut dyn FnMut(&mut mlx_native::IdMmScratch) -> mlx_native::Result<()>| {
with_id_mm_scratch(
MmIdSlot::Gate,
device,
ne,
rows * shape.num_experts_per_tok,
|s| f(s),
)
};
let shared_slot =
|f: &mut dyn FnMut(&mut mlx_native::IdMmScratch) -> mlx_native::Result<()>| {
with_id_mm_scratch(MmIdSlot::Up, device, shared_blocks, rows, |s| f(s))
};
for (role, output, out_features) in [
(MoeDenseRole::Router, &logits, ne),
(MoeDenseRole::SharedGateLogit, &sh_logit, 1),
] {
encode_row_exact_proj(
enc,
registry,
device,
x,
weights,
role,
output,
rows,
h32,
out_features,
)
.with_context(context)?;
}
shared_slot(&mut |scratch| {
moe_tiles_schedule(enc, registry, device, &shared_ids, scratch, &shared)
})
.map_err(|e| tile_error("shared schedule", e))?;
dispatch_moe_softmax_topk_simd(
enc,
registry,
device,
&logits,
&ids,
&routing_weights,
rows,
ne,
shape.num_experts_per_tok,
)
.map_err(|e| anyhow!("{} top-k: {e}", context()))?;
shared_slot(&mut |scratch| {
moe_tiles_gather(
enc,
registry,
device,
x,
&shared_ids,
&shared_scratch.buffers(),
scratch,
&shared,
)
})
.map_err(|e| tile_error("shared gather", e))?;
enc.memory_barrier();
if let Some(mask) = route_mask {
dispatch_dflash2_mask_moe_routes(
enc,
registry,
device,
mask.verify_rows,
&ids,
&routing_weights,
mask.params,
)
.map_err(|e| anyhow!("{} DFlash2 route mask: {e}", context()))?;
enc.memory_barrier();
}
routed_slot(&mut |scratch| {
moe_tiles_schedule(enc, registry, device, &ids, scratch, &routed)?;
moe_tiles_gather(
enc,
registry,
device,
x,
&ids,
&routed_scratch.buffers(),
scratch,
&routed,
)
})
.map_err(|e| tile_error("route schedule", e))?;
enc.memory_barrier();
routed_slot(&mut |scratch| {
moe_tiles_gate_up(
enc,
registry,
device,
&prepared.gate,
&prepared.up,
&ids,
&routed_scratch.buffers(),
scratch,
&routed,
)
})
.map_err(|e| tile_error("expert gate/up", e))?;
shared_slot(&mut |scratch| {
moe_tiles_gate_up(
enc,
registry,
device,
&live.shared[0],
&live.shared[1],
&shared_ids,
&shared_scratch.buffers(),
scratch,
&shared,
)
})
.map_err(|e| tile_error("shared gate/up", e))?;
enc.memory_barrier();
routed_slot(&mut |scratch| {
moe_tiles_down(
enc,
registry,
device,
&prepared.down,
&ids,
&routed_scratch.buffers(),
scratch,
&y_all,
&routed,
)
})
.map_err(|e| tile_error("expert down", e))?;
shared_slot(&mut |scratch| {
moe_tiles_down(
enc,
registry,
device,
&live.shared[2],
&shared_ids,
&shared_scratch.buffers(),
scratch,
&y_s,
&shared,
)
})
.map_err(|e| tile_error("shared down", e))?;
enc.memory_barrier();
let weighted_reduce = if route_mask.is_some() {
dispatch_moe_weighted_reduce_masked
} else {
dispatch_moe_weighted_reduce
};
weighted_reduce(
enc,
registry,
device,
&routing_weights,
&y_all,
&sh_logit,
&y_s,
residual_ref,
out,
rows,
shape.num_experts_per_tok,
h32,
residual.is_some(),
)
.map_err(|e| anyhow!("{} weighted reduce: {e}", context()))?;
#[cfg(test)]
super::moe_batched::retain_routing_for_tests(
&logits,
&ids,
&routing_weights,
r,
ne as usize,
top_k,
)?;
Ok(MoeBatchedRouting {
ids,
weights: routing_weights,
})
}