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//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, version 3.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16use 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/// Gemma 2 final logit softcap: `cap * tanh(logits / cap)`.
27#[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}