rlx_flow/blocks/
logit_softcap.rs1use anyhow::Result;
17use rlx_ir::HirGraphExt;
18use rlx_ir::hir::HirMut;
19use rlx_ir::op::Activation;
20use rlx_ir::{DType, Shape};
21
22use super::BlockStage;
23use crate::context::FlowCtx;
24use crate::value::FlowValue;
25
26#[derive(Debug, Clone)]
28pub struct LogitSoftcapStage {
29 pub cap: f32,
30}
31
32impl LogitSoftcapStage {
33 pub fn new(cap: f32) -> Self {
34 Self { cap }
35 }
36}
37
38impl BlockStage for LogitSoftcapStage {
39 fn emit(&self, ctx: &mut FlowCtx<'_>, input: FlowValue) -> Result<Option<FlowValue>> {
40 let cap = self.cap;
41 let inv_name = format!("gemma.logit_softcap.inv.{cap}");
42 let inv = ctx.synth_param(&inv_name, vec![1.0 / cap], Shape::new(&[1], DType::F32));
43 let cap_name = format!("gemma.logit_softcap.cap.{cap}");
44 let cap_id = ctx.synth_param(&cap_name, vec![cap], Shape::new(&[1], DType::F32));
45 let mut gb = HirMut::new(ctx.hir());
46 let scaled = gb.mul(input.id, inv);
47 let t = gb.activation(Activation::Tanh, scaled, gb.shape(scaled).clone());
48 let out = gb.mul(t, cap_id);
49 Ok(Some(ctx.wrap(out, input.shape.clone())))
50 }
51}