1use anyhow::Result;
17use rlx_ir::HirGraphExt;
18use rlx_ir::hir::HirMut;
19
20use super::BlockStage;
21use crate::context::FlowCtx;
22use crate::value::FlowValue;
23#[derive(Debug, Clone)]
24pub struct EmbedStage {
25 pub weight_key: String,
26 pub axis: usize,
27}
28
29impl EmbedStage {
30 pub fn token(weight_key: impl Into<String>) -> Self {
31 Self {
32 weight_key: weight_key.into(),
33 axis: 0,
34 }
35 }
36}
37
38impl BlockStage for EmbedStage {
39 fn emit(&self, ctx: &mut FlowCtx<'_>, input: FlowValue) -> Result<Option<FlowValue>> {
40 let embed_w = ctx.load_param(&self.weight_key, false)?;
41 ctx.state.embed_weight = Some(embed_w);
42 let out_shape = {
43 let w_shape = ctx.hir().node(embed_w).shape.clone();
44 let mut dims: Vec<rlx_ir::Dim> = input.shape.dims().to_vec();
45 dims.push(w_shape.dim(1));
46 rlx_ir::Shape::from_dims(&dims, input.shape.dtype())
47 };
48 let mut gb = HirMut::new(ctx.hir());
49 let id = gb.gather_(embed_w, input.id, self.axis);
50 Ok(Some(ctx.wrap(id, out_shape)))
51 }
52}