Skip to main content

rlx_flow/blocks/
logit_softcap.rs

1// RLX — versatile ML compiler + runtime.
2// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
3
4use anyhow::Result;
5use rlx_ir::HirGraphExt;
6use rlx_ir::hir::HirMut;
7use rlx_ir::op::Activation;
8use rlx_ir::{DType, Shape};
9
10use super::BlockStage;
11use crate::context::FlowCtx;
12use crate::value::FlowValue;
13
14/// Gemma 2 final logit softcap: `cap * tanh(logits / cap)`.
15#[derive(Debug, Clone)]
16pub struct LogitSoftcapStage {
17    pub cap: f32,
18}
19
20impl LogitSoftcapStage {
21    pub fn new(cap: f32) -> Self {
22        Self { cap }
23    }
24}
25
26impl BlockStage for LogitSoftcapStage {
27    fn emit(&self, ctx: &mut FlowCtx<'_>, input: FlowValue) -> Result<Option<FlowValue>> {
28        let cap = self.cap;
29        let inv_name = format!("gemma.logit_softcap.inv.{cap}");
30        let inv = ctx.synth_param(&inv_name, vec![1.0 / cap], Shape::new(&[1], DType::F32));
31        let cap_name = format!("gemma.logit_softcap.cap.{cap}");
32        let cap_id = ctx.synth_param(&cap_name, vec![cap], Shape::new(&[1], DType::F32));
33        let mut gb = HirMut::new(ctx.hir());
34        let scaled = gb.mul(input.id, inv);
35        let t = gb.activation(Activation::Tanh, scaled, gb.shape(scaled).clone());
36        let out = gb.mul(t, cap_id);
37        Ok(Some(ctx.wrap(out, input.shape.clone())))
38    }
39}