use model_io::ResidentIndex;
use crate::families::llama::{layer_tensor, RealLlamaState};
use crate::real_forward_dispatch::encode_gemv_any;
use crate::real_forward_types::{DecodeScratch, RealForwardError};
#[allow(clippy::too_many_arguments)]
pub(crate) fn encode_llama_layer_dense(
context: &mut gpu::MetalContext,
pass: &gpu::PassEncoder,
weights: &gpu::ResidentGpuWeights,
index: &ResidentIndex,
scratch: &DecodeScratch,
llama: &RealLlamaState,
layer: usize,
hidden: usize,
inter: usize,
use_silu: bool,
x_off: u64,
) -> Result<(), RealForwardError> {
let gpu_err = RealForwardError::Gpu;
for (suffix, out) in [
("mlp.gate_proj.weight", &scratch.ffn_gate),
("mlp.up_proj.weight", &scratch.ffn_up),
] {
encode_gemv_any(
context,
pass,
weights,
index,
&layer_tensor(layer, suffix),
inter,
hidden,
(&llama.moe_x, 0),
(out, 0),
)?;
}
let act = if use_silu {
gpu::encode_silu_mul
} else {
gpu::encode_gelu_mul
};
act(
context,
pass,
(&scratch.ffn_gate, 0),
(&scratch.ffn_up, 0),
(&scratch.ffn_act, 0),
inter as u32,
)
.map_err(gpu_err)?;
encode_gemv_any(
context,
pass,
weights,
index,
&layer_tensor(layer, "mlp.down_proj.weight"),
hidden,
inter,
(&scratch.ffn_act, 0),
(&llama.h2, 0),
)?;
gpu::encode_residual_add(
context,
pass,
(&scratch.x, x_off),
(&llama.h2, 0),
hidden as u32,
)
.map_err(gpu_err)?;
Ok(())
}