1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// RLX — versatile ML compiler + runtime.
// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Gated DeltaNet scan — generic op wrapper (Qwen3.5 trunk, …).
use anyhow::Result;
use rlx_ir::HirGraphExt;
use rlx_ir::Shape;
use rlx_ir::hir::HirMut;
use super::BlockStage;
use crate::context::FlowCtx;
use crate::value::FlowValue;
/// Q/K/V/G/Beta tensors must already be shaped `[batch, seq, heads, state]`.
#[derive(Debug, Clone)]
pub struct GdnScanStage {
pub state_size: usize,
pub out_shape: Shape,
pub carry_state: bool,
pub state_key: Option<String>,
}
impl GdnScanStage {
pub fn prefill(state_size: usize, out_shape: Shape) -> Self {
Self {
state_size,
out_shape,
carry_state: false,
state_key: None,
}
}
pub fn with_carry(mut self, state_key: impl Into<String>) -> Self {
self.carry_state = true;
self.state_key = Some(state_key.into());
self
}
}
impl BlockStage for GdnScanStage {
fn emit(&self, ctx: &mut FlowCtx<'_>, input: FlowValue) -> Result<Option<FlowValue>> {
let slots = ctx
.state
.gdn
.ok_or_else(|| anyhow::anyhow!("GdnScan requires gdn inputs in FlowState"))?;
let carry_state = if self.carry_state {
let key = self
.state_key
.as_deref()
.ok_or_else(|| anyhow::anyhow!("GdnScan carry requires state_key"))?;
Some(
*ctx.state
.named
.get(key)
.ok_or_else(|| anyhow::anyhow!("GdnScan missing carry state `{key}`"))?,
)
} else {
None
};
let mut gb = HirMut::new(ctx.hir());
let id = if let Some(state) = carry_state {
gb.gated_delta_net_carry(
slots.q,
slots.k,
slots.v,
slots.g,
slots.beta,
state,
self.state_size,
self.out_shape.clone(),
)
} else {
gb.gated_delta_net(
slots.q,
slots.k,
slots.v,
slots.g,
slots.beta,
self.state_size,
self.out_shape.clone(),
)
};
let _ = input;
Ok(Some(ctx.wrap(id, self.out_shape.clone())))
}
}