-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhierarchy.rs
More file actions
230 lines (201 loc) · 7.08 KB
/
hierarchy.rs
File metadata and controls
230 lines (201 loc) · 7.08 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! Hierarchical JEPA (H-JEPA) for multi-scale prediction.
//!
//! H-JEPA stacks multiple JEPA levels, each operating at a different
//! temporal and spatial abstraction scale:
//!
//! ```text
//! Level 2 (coarsest) ─── stride 24 ──── long-horizon plans
//! Level 1 ─── stride 6 ──── medium-horizon
//! Level 0 (finest) ─── stride 2 ──── short-horizon, detailed
//! ```
//!
//! Higher levels predict over longer time horizons with coarser spatial
//! resolution. The effective temporal stride at level *k* is the product
//! of all strides up to and including level *k*.
use burn::tensor::backend::Backend;
use jepa_core::types::Representation;
use jepa_core::{Encoder, Predictor};
/// A single level in the H-JEPA hierarchy.
///
/// Each level has its own encoder and predictor, and operates at
/// a specific temporal stride and spatial resolution.
pub struct JepaLevel<B: Backend> {
/// Encoder for this level.
pub encoder: Box<dyn Encoder<B, Input = Representation<B>>>,
/// Predictor for this level.
pub predictor: Box<dyn Predictor<B>>,
/// Temporal abstraction factor.
/// How many lower-level steps correspond to one step at this level.
pub temporal_stride: usize,
}
/// Hierarchical JEPA with multiple abstraction levels.
///
/// Processes representations through a stack of JEPA levels,
/// where each level operates at progressively coarser temporal
/// and spatial scales.
///
/// # Example
///
/// ```
/// use jepa_world::hierarchy::HierarchicalJepa;
/// use burn_ndarray::NdArray;
///
/// type B = NdArray<f32>;
///
/// // An empty hierarchy is valid (zero levels)
/// let hjepa = HierarchicalJepa::<B>::new(vec![]);
/// assert_eq!(hjepa.num_levels(), 0);
/// ```
pub struct HierarchicalJepa<B: Backend> {
/// Stack of JEPA levels, from finest (index 0) to coarsest.
pub levels: Vec<JepaLevel<B>>,
}
impl<B: Backend> HierarchicalJepa<B> {
/// Create a new hierarchical JEPA with the given levels.
pub fn new(levels: Vec<JepaLevel<B>>) -> Self {
Self { levels }
}
/// Get the number of hierarchy levels.
pub fn num_levels(&self) -> usize {
self.levels.len()
}
/// Encode input through all levels of the hierarchy.
///
/// Returns representations at each level, from finest to coarsest.
///
/// # Arguments
/// * `input` - Input representation (typically from a base encoder)
///
/// # Returns
/// Vector of representations, one per level
pub fn encode_all_levels(&self, input: &Representation<B>) -> Vec<Representation<B>> {
let mut representations = Vec::with_capacity(self.levels.len());
let mut current = input.clone();
for level in &self.levels {
let repr = level.encoder.encode(¤t);
representations.push(repr.clone());
current = repr;
}
representations
}
/// Get the temporal stride at a specific level.
///
/// The effective stride is the product of all strides up to
/// and including the given level.
///
/// # Panics
///
/// Panics if `level_idx >= self.num_levels()`. Use
/// [`try_effective_stride`](Self::try_effective_stride) when the index
/// comes from caller-controlled input.
pub fn effective_stride(&self, level_idx: usize) -> usize {
self.try_effective_stride(level_idx).unwrap_or_else(|e| {
panic!("{e}");
})
}
/// Get the temporal stride at a specific level, returning an error
/// if the index is out of bounds.
pub fn try_effective_stride(&self, level_idx: usize) -> Result<usize, HierarchyError> {
if level_idx >= self.levels.len() {
return Err(HierarchyError::LevelOutOfBounds {
index: level_idx,
num_levels: self.levels.len(),
});
}
Ok(self.levels[..=level_idx]
.iter()
.map(|l| l.temporal_stride)
.product())
}
}
/// Errors from hierarchy operations.
#[derive(Debug, thiserror::Error)]
pub enum HierarchyError {
#[error("level index {index} out of bounds for hierarchy with {num_levels} levels")]
LevelOutOfBounds { index: usize, num_levels: usize },
}
#[cfg(test)]
mod tests {
use super::*;
use burn::tensor::Tensor;
use burn_ndarray::NdArray;
type TestBackend = NdArray<f32>;
fn device() -> burn_ndarray::NdArrayDevice {
burn_ndarray::NdArrayDevice::Cpu
}
/// Identity encoder for testing hierarchy.
struct IdentityHierarchyEncoder {
dim: usize,
}
impl Encoder<TestBackend> for IdentityHierarchyEncoder {
type Input = Representation<TestBackend>;
fn encode(&self, input: &Self::Input) -> Representation<TestBackend> {
input.clone()
}
fn embed_dim(&self) -> usize {
self.dim
}
}
/// Zero predictor for testing hierarchy.
struct ZeroPredictorH {
embed_dim: usize,
}
impl Predictor<TestBackend> for ZeroPredictorH {
fn predict(
&self,
_context: &Representation<TestBackend>,
target_positions: &Tensor<TestBackend, 2>,
_latent: Option<&Tensor<TestBackend, 2>>,
) -> Representation<TestBackend> {
let [batch, num_targets] = target_positions.dims();
let device = target_positions.device();
Representation::new(Tensor::zeros([batch, num_targets, self.embed_dim], &device))
}
}
fn make_level(dim: usize, stride: usize) -> JepaLevel<TestBackend> {
JepaLevel {
encoder: Box::new(IdentityHierarchyEncoder { dim }),
predictor: Box::new(ZeroPredictorH { embed_dim: dim }),
temporal_stride: stride,
}
}
#[test]
fn test_hierarchy_num_levels() {
let hjepa = HierarchicalJepa::new(vec![
make_level(64, 1),
make_level(64, 2),
make_level(64, 4),
]);
assert_eq!(hjepa.num_levels(), 3);
}
#[test]
fn test_hierarchy_encode_all_levels() {
let hjepa = HierarchicalJepa::new(vec![make_level(32, 1), make_level(32, 2)]);
let input = Representation::new(Tensor::ones([1, 8, 32], &device()));
let reprs = hjepa.encode_all_levels(&input);
assert_eq!(reprs.len(), 2);
assert_eq!(reprs[0].seq_len(), 8);
assert_eq!(reprs[1].seq_len(), 8); // identity encoder preserves shape
}
#[test]
#[should_panic(expected = "level index 3 out of bounds")]
fn test_effective_stride_out_of_bounds() {
let hjepa = HierarchicalJepa::new(vec![
make_level(64, 2),
make_level(64, 3),
make_level(64, 4),
]);
let _ = hjepa.effective_stride(3);
}
#[test]
fn test_effective_stride() {
let hjepa = HierarchicalJepa::new(vec![
make_level(64, 2),
make_level(64, 3),
make_level(64, 4),
]);
assert_eq!(hjepa.effective_stride(0), 2);
assert_eq!(hjepa.effective_stride(1), 6); // 2 * 3
assert_eq!(hjepa.effective_stride(2), 24); // 2 * 3 * 4
}
}