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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// 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/>.
//! Model execution variants — one object drives cache keys and [`DimBinding`].
//!
//! Mirrors the “shader components” idea from extensible shading systems: the same
//! granularity selects **what to specialize** and **which symbolic dims to bind**.
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use crate::dynamic::sym;
use crate::shape::DimBinding;
/// Coarse execution phase (prefill vs decode vs encoder).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ModelPhase {
Prefill,
Decode,
Encoder,
Inference,
}
/// Concrete shape bucket for compile-once / specialize-at-runtime workflows.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelVariant {
pub batch: usize,
pub seq: usize,
pub past_seq: Option<usize>,
pub phase: ModelPhase,
/// Extra dynamic symbols beyond batch/seq/past (e.g. custom ragged axes).
pub extra: Vec<(u32, usize)>,
}
impl ModelVariant {
pub fn prefill(batch: usize, seq: usize) -> Self {
Self {
batch,
seq,
past_seq: None,
phase: ModelPhase::Prefill,
extra: Vec::new(),
}
}
/// Single-step decode: `seq` is the new token count (often 1); `past_seq` is KV length.
pub fn decode(batch: usize, past_seq: usize, new_tokens: usize) -> Self {
Self {
batch,
seq: new_tokens,
past_seq: Some(past_seq),
phase: ModelPhase::Decode,
extra: Vec::new(),
}
}
pub fn encoder(batch: usize, seq: usize) -> Self {
Self {
batch,
seq,
past_seq: None,
phase: ModelPhase::Encoder,
extra: Vec::new(),
}
}
pub fn with_extra(mut self, symbol: u32, size: usize) -> Self {
self.extra.push((symbol, size));
self
}
/// Stable cache key: phase + bound leading dims + extra symbols.
pub fn cache_key(&self) -> u64 {
let mut h = DefaultHasher::new();
self.phase.hash(&mut h);
self.batch.hash(&mut h);
self.seq.hash(&mut h);
self.past_seq.hash(&mut h);
for (sym, size) in &self.extra {
sym.hash(&mut h);
size.hash(&mut h);
}
h.finish()
}
/// Symbol bindings used by [`crate::dynamic::bind_graph`] / compile specialization.
pub fn dim_binding(&self) -> DimBinding {
let mut b = match (self.phase, self.past_seq) {
(ModelPhase::Decode, Some(past)) => DimBinding::batch_past_seq(self.batch, past),
_ => DimBinding::batch_seq(self.batch, self.seq),
};
if self.phase == ModelPhase::Decode {
b.set(sym::SEQ, self.seq);
}
for (sym, size) in &self.extra {
b.set(*sym, *size);
}
b
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn prefill_binding_sets_batch_seq() {
let v = ModelVariant::prefill(2, 128);
let b = v.dim_binding();
assert_eq!(b.get(sym::BATCH), Some(2));
assert_eq!(b.get(sym::SEQ), Some(128));
}
#[test]
fn decode_binding_sets_past_and_new_seq() {
let v = ModelVariant::decode(1, 64, 1);
let b = v.dim_binding();
assert_eq!(b.get(sym::BATCH), Some(1));
assert_eq!(b.get(sym::PAST_SEQ), Some(64));
assert_eq!(b.get(sym::SEQ), Some(1));
}
#[test]
fn cache_key_differs_by_phase() {
let a = ModelVariant::prefill(1, 8).cache_key();
let b = ModelVariant::decode(1, 7, 1).cache_key();
assert_ne!(a, b);
}
}