use rlx_ir::{DType, Graph, GraphExt, NodeId, Shape};
#[derive(Copy, Clone)]
pub struct MlpDims {
pub in_dim: usize,
pub hidden: usize,
pub out_dim: usize,
}
pub type Params = [NodeId; 4];
fn body(g: &mut Graph, d: MlpDims) -> (NodeId, Params) {
let x = g.input("x", Shape::new(&[1, d.in_dim], DType::F32));
let w1 = g.param("w1", Shape::new(&[d.in_dim, d.hidden], DType::F32));
let b1 = g.param("b1", Shape::new(&[1, d.hidden], DType::F32));
let w2 = g.param("w2", Shape::new(&[d.hidden, d.out_dim], DType::F32));
let b2 = g.param("b2", Shape::new(&[1, d.out_dim], DType::F32));
let h = g.matmul(x, w1, Shape::new(&[1, d.hidden], DType::F32));
let h = g.add(h, b1);
let h = g.relu(h);
let y = g.matmul(h, w2, Shape::new(&[1, d.out_dim], DType::F32));
let y = g.add(y, b2);
(y, [w1, b1, w2, b2])
}
pub fn build_forward(d: MlpDims) -> (Graph, Params) {
let mut g = Graph::new("rlx_web_mlp");
let (y, params) = body(&mut g, d);
g.set_outputs(vec![y]);
(g, params)
}
pub fn build_loss(d: MlpDims) -> (Graph, Params) {
let mut g = Graph::new("rlx_web_mlp_loss");
let (y, params) = body(&mut g, d);
let target = g.input("target", Shape::new(&[1, d.out_dim], DType::F32));
let diff = g.sub(y, target);
let sq = g.mul(diff, diff);
let loss = g.sum(sq, vec![0, 1], false);
g.set_outputs(vec![loss]);
(g, params)
}