use crate::tensor::Tensor;
use crate::weights::ModelWeights;
pub struct MLP {
_hidden_size: usize,
_intermediate_size: usize,
}
impl MLP {
pub fn new(hidden_size: usize, intermediate_size: usize) -> Self {
Self {
_hidden_size: hidden_size,
_intermediate_size: intermediate_size,
}
}
pub fn forward(&self, x: &Tensor, weights: &ModelWeights, layer_idx: usize) -> Tensor {
let fc_weight = &weights.tensors[&format!("h.{}.mlp.c_fc.weight", layer_idx)];
let fc_bias = &weights.tensors[&format!("h.{}.mlp.c_fc.bias", layer_idx)];
let intermediate = x.matmul(fc_weight).add(fc_bias);
let activated = intermediate.gelu();
let proj_weight = &weights.tensors[&format!("h.{}.mlp.c_proj.weight", layer_idx)];
let proj_bias = &weights.tensors[&format!("h.{}.mlp.c_proj.bias", layer_idx)];
activated.matmul(proj_weight).add(proj_bias)
}
}