jepa-core 0.1.0

Core traits and abstractions for JEPA (Joint Embedding Predictive Architecture)
Documentation
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Collapse prevention regularizers for JEPA.
//!
//! Implements RFC-006 (Collapse Prevention).
//!
//! Without explicit regularization, joint-embedding models can
//! *collapse* — all inputs map to the same constant representation,
//! yielding zero energy but zero information. While EMA target updates
//! (see [`crate::ema`]) help, they are not sufficient alone. This
//! module provides two complementary regularizers:
//!
//! | Regularizer | Strategy | Reference |
//! |-------------|----------|-----------|
//! | [`VICReg`] | Variance + Invariance + Covariance | Bardes et al. (2022), VICReg |
//! | [`BarlowTwins`] | Cross-correlation → identity | Zbontar et al. (2021), Barlow Twins |
//!
//! Both implement the [`CollapseRegularizer`] trait and return a scalar
//! loss term that should be added (with appropriate weighting) to the
//! energy loss during training.

use burn::tensor::{backend::Backend, Tensor};

/// Trait for collapse prevention regularizers.
///
/// Collapse regularizers add a loss term that prevents all representations
/// from converging to the same point in embedding space.
pub trait CollapseRegularizer<B: Backend> {
    /// Compute the regularization loss.
    ///
    /// # Arguments
    /// * `z_a` - First set of representations. Shape: `[batch, embed_dim]`
    /// * `z_b` - Second set of representations. Shape: `[batch, embed_dim]`
    ///
    /// # Returns
    /// A scalar loss tensor (higher means more collapse detected).
    fn loss(&self, z_a: &Tensor<B, 2>, z_b: &Tensor<B, 2>) -> Tensor<B, 1>;
}

/// VICReg regularization loss components.
///
/// Contains the three VICReg terms for inspection and logging.
#[derive(Debug, Clone)]
pub struct VICRegLoss<B: Backend> {
    /// Invariance: MSE between paired representations.
    pub invariance: Tensor<B, 1>,
    /// Variance: hinge loss on per-dimension standard deviation.
    pub variance: Tensor<B, 1>,
    /// Covariance: penalty on off-diagonal covariance elements.
    pub covariance: Tensor<B, 1>,
}

impl<B: Backend> VICRegLoss<B> {
    /// Total weighted loss: invariance + variance + covariance.
    pub fn total(&self) -> Tensor<B, 1> {
        self.invariance.clone() + self.variance.clone() + self.covariance.clone()
    }
}

/// VICReg (Variance-Invariance-Covariance Regularization).
///
/// Prevents collapse by enforcing:
/// 1. **Variance**: each embedding dimension has high variance across the batch
/// 2. **Invariance**: representations of positive pairs are similar
/// 3. **Covariance**: different embedding dimensions capture different information
///
/// # Example
///
/// ```
/// use jepa_core::collapse::{VICReg, CollapseRegularizer};
/// use burn::tensor::{Tensor, ElementConversion};
/// use burn_ndarray::NdArray;
///
/// type B = NdArray<f32>;
/// let device = burn_ndarray::NdArrayDevice::Cpu;
///
/// let vicreg = VICReg::default();
/// let z_a: Tensor<B, 2> = Tensor::random(
///     [32, 64],
///     burn::tensor::Distribution::Normal(0.0, 1.0),
///     &device,
/// );
/// let z_b = z_a.clone();
///
/// // Compute decomposed loss for logging
/// let loss = vicreg.compute(&z_a, &z_b);
/// let total: f32 = loss.total().into_scalar().elem();
/// assert!(total.is_finite());
///
/// // Or use the trait interface for a single scalar
/// let scalar_loss: f32 = vicreg.loss(&z_a, &z_b).into_scalar().elem();
/// assert!(scalar_loss.is_finite());
/// ```
#[derive(Debug, Clone)]
pub struct VICReg {
    /// Weight for the invariance term (default: 25.0).
    pub lambda_inv: f64,
    /// Weight for the variance term (default: 25.0).
    pub mu_var: f64,
    /// Weight for the covariance term (default: 1.0).
    pub nu_cov: f64,
    /// Target standard deviation for variance hinge loss (default: 1.0).
    pub gamma: f64,
    /// Epsilon for numerical stability (default: 1e-4).
    pub eps: f64,
}

impl Default for VICReg {
    fn default() -> Self {
        Self {
            lambda_inv: 25.0,
            mu_var: 25.0,
            nu_cov: 1.0,
            gamma: 1.0,
            eps: 1e-4,
        }
    }
}

impl VICReg {
    /// Compute the decomposed VICReg loss.
    ///
    /// Returns individual variance, invariance, and covariance terms
    /// for inspection. Use [`VICRegLoss::total`] for the combined loss.
    ///
    /// # Arguments
    /// * `z_a` - Representations from branch A. Shape: `[batch, embed_dim]`
    /// * `z_b` - Representations from branch B. Shape: `[batch, embed_dim]`
    pub fn compute<B: Backend>(&self, z_a: &Tensor<B, 2>, z_b: &Tensor<B, 2>) -> VICRegLoss<B> {
        let device = z_a.device();
        let [batch, embed_dim] = z_a.dims();
        let n = batch as f64;

        // --- Invariance: MSE between paired representations ---
        let diff = z_a.clone() - z_b.clone();
        let inv_loss = (diff.clone() * diff).mean().reshape([1]);

        // --- Variance: hinge loss on std dev ---
        let var_loss = self.variance_loss(z_a, n, &device) + self.variance_loss(z_b, n, &device);

        // --- Covariance: off-diagonal elements should be zero ---
        let cov_loss =
            self.covariance_loss(z_a, n, embed_dim) + self.covariance_loss(z_b, n, embed_dim);

        VICRegLoss {
            invariance: inv_loss * self.lambda_inv,
            variance: var_loss * self.mu_var,
            covariance: cov_loss * self.nu_cov,
        }
    }

    fn variance_loss<B: Backend>(
        &self,
        z: &Tensor<B, 2>,
        n: f64,
        device: &B::Device,
    ) -> Tensor<B, 1> {
        // Compute per-dimension variance: var = E[(x - mean)^2]
        let mean = z.clone().mean_dim(0); // [1, embed_dim]
        let centered = z.clone() - mean;
        let var = (centered.clone() * centered).sum_dim(0) / (n - 1.0).max(1.0); // [1, embed_dim]
        let std = (var + self.eps).sqrt();

        // Hinge: max(0, gamma - std)
        let gamma_tensor: Tensor<B, 2> = Tensor::full(std.dims(), self.gamma, device);
        let hinge = (gamma_tensor - std).clamp_min(0.0);
        // mean() on [1, embed_dim] → reshape to [1]
        hinge.mean().reshape([1])
    }

    fn covariance_loss<B: Backend>(
        &self,
        z: &Tensor<B, 2>,
        n: f64,
        embed_dim: usize,
    ) -> Tensor<B, 1> {
        // Center the representations
        let mean = z.clone().mean_dim(0); // [1, embed_dim]
        let centered = z.clone() - mean; // [batch, embed_dim]

        // Covariance matrix: C = (1/N) * Z^T Z
        let cov = centered.clone().transpose().matmul(centered) / (n - 1.0).max(1.0); // [embed_dim, embed_dim]

        // Zero out diagonal (we only penalize off-diagonal)
        let diag_mask = Tensor::eye(embed_dim, &cov.device()); // [embed_dim, embed_dim]
        let off_diag = cov * (diag_mask.neg() + 1.0);

        // Sum of squared off-diagonal elements, normalized → reshape to [1]
        ((off_diag.clone() * off_diag).sum() / embed_dim as f64).reshape([1])
    }
}

impl<B: Backend> CollapseRegularizer<B> for VICReg {
    fn loss(&self, z_a: &Tensor<B, 2>, z_b: &Tensor<B, 2>) -> Tensor<B, 1> {
        self.compute(z_a, z_b).total()
    }
}

/// Barlow Twins regularization.
///
/// Prevents collapse by making the cross-correlation matrix between two
/// representation branches approach the identity matrix. This enforces:
/// 1. **Invariance**: diagonal elements ≈ 1 (paired representations are similar)
/// 2. **Redundancy reduction**: off-diagonal elements ≈ 0 (dimensions are decorrelated)
///
/// Reference: Zbontar et al. (2021), "Barlow Twins: Self-Supervised Learning via
/// Redundancy Reduction", ICML.
#[derive(Debug, Clone)]
pub struct BarlowTwins {
    /// Weight for the off-diagonal (redundancy reduction) term (default: 0.005).
    ///
    /// The on-diagonal (invariance) term always has weight 1.0.
    /// Lower lambda means less penalty on redundant features.
    pub lambda_bt: f64,
}

impl Default for BarlowTwins {
    fn default() -> Self {
        Self { lambda_bt: 0.005 }
    }
}

/// Decomposed Barlow Twins loss components.
#[derive(Debug, Clone)]
pub struct BarlowTwinsLoss<B: Backend> {
    /// On-diagonal loss: `sum((1 - diag(C))^2)`. Shape: `[1]`
    pub on_diagonal: Tensor<B, 1>,
    /// Off-diagonal loss: `sum(off_diag(C)^2)`. Shape: `[1]`
    pub off_diagonal: Tensor<B, 1>,
}

impl<B: Backend> BarlowTwinsLoss<B> {
    /// Total loss: on_diagonal + lambda * off_diagonal.
    pub fn total(&self) -> Tensor<B, 1> {
        self.on_diagonal.clone() + self.off_diagonal.clone()
    }
}

impl BarlowTwins {
    /// Create a new Barlow Twins regularizer with the given lambda.
    pub fn new(lambda_bt: f64) -> Self {
        Self { lambda_bt }
    }

    /// Compute the decomposed Barlow Twins loss.
    ///
    /// 1. Standardize each branch (zero mean, unit std per dimension)
    /// 2. Compute the cross-correlation matrix C = (1/N) * Z_a^T * Z_b
    /// 3. On-diagonal: penalize deviation from 1 (invariance)
    /// 4. Off-diagonal: penalize non-zero elements (redundancy reduction)
    ///
    /// # Arguments
    /// * `z_a` - Representations from branch A. Shape: `[batch, embed_dim]`
    /// * `z_b` - Representations from branch B. Shape: `[batch, embed_dim]`
    pub fn compute<B: Backend>(
        &self,
        z_a: &Tensor<B, 2>,
        z_b: &Tensor<B, 2>,
    ) -> BarlowTwinsLoss<B> {
        let [batch, embed_dim] = z_a.dims();
        let n = batch as f64;
        let eps = 1e-5;

        // Standardize each branch: z_norm = (z - mean) / std
        let z_a_norm = standardize::<B>(z_a, n, eps);
        let z_b_norm = standardize::<B>(z_b, n, eps);

        // Cross-correlation matrix: C = (1/N) * Z_a^T * Z_b
        // Shape: [embed_dim, embed_dim]
        let cross_corr = z_a_norm.transpose().matmul(z_b_norm) / n;

        // On-diagonal: sum of (1 - C_ii)^2
        let diag_mask = Tensor::<B, 2>::eye(embed_dim, &cross_corr.device());
        let diag_values = cross_corr.clone() * diag_mask.clone();
        let on_diag_diff = diag_values - diag_mask;
        let on_diag_loss = (on_diag_diff.clone() * on_diag_diff).sum().reshape([1]);

        // Off-diagonal: sum of C_ij^2 for i != j
        let off_diag_mask = Tensor::<B, 2>::eye(embed_dim, &cross_corr.device()).neg() + 1.0;
        let off_diag = cross_corr * off_diag_mask;
        let off_diag_loss = ((off_diag.clone() * off_diag).sum() * self.lambda_bt).reshape([1]);

        BarlowTwinsLoss {
            on_diagonal: on_diag_loss,
            off_diagonal: off_diag_loss,
        }
    }
}

/// Standardize a batch of representations to zero mean, unit std per dimension.
fn standardize<B: Backend>(z: &Tensor<B, 2>, n: f64, eps: f64) -> Tensor<B, 2> {
    let mean = z.clone().mean_dim(0); // [1, embed_dim]
    let centered = z.clone() - mean;
    let var = (centered.clone() * centered.clone()).sum_dim(0) / (n - 1.0).max(1.0);
    let std = (var + eps).sqrt();
    centered / std
}

impl<B: Backend> CollapseRegularizer<B> for BarlowTwins {
    fn loss(&self, z_a: &Tensor<B, 2>, z_b: &Tensor<B, 2>) -> Tensor<B, 1> {
        self.compute(z_a, z_b).total()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use burn::tensor::{ElementConversion, Tensor};
    use burn_ndarray::NdArray;

    type TestBackend = NdArray<f32>;

    fn device() -> burn_ndarray::NdArrayDevice {
        burn_ndarray::NdArrayDevice::Cpu
    }

    #[test]
    fn test_vicreg_default_params() {
        let vicreg = VICReg::default();
        assert_eq!(vicreg.lambda_inv, 25.0);
        assert_eq!(vicreg.mu_var, 25.0);
        assert_eq!(vicreg.nu_cov, 1.0);
    }

    #[test]
    fn test_collapsed_representations_high_variance_loss() {
        let vicreg = VICReg::default();
        // All-zeros = fully collapsed
        let z: Tensor<TestBackend, 2> = Tensor::zeros([32, 64], &device());
        let loss = vicreg.compute(&z, &z);
        let var_val: f32 = loss.variance.into_scalar().elem();
        // Variance term should be high for collapsed representations
        assert!(
            var_val > 10.0,
            "expected high variance loss for collapse, got {var_val}"
        );
    }

    #[test]
    fn test_identical_pairs_zero_invariance() {
        let vicreg = VICReg::default();
        // Random non-collapsed representations
        let z: Tensor<TestBackend, 2> = Tensor::random(
            [32, 64],
            burn::tensor::Distribution::Normal(0.0, 1.0),
            &device(),
        );
        let loss = vicreg.compute(&z, &z);
        let inv_val: f32 = loss.invariance.into_scalar().elem();
        assert!(
            inv_val.abs() < 1e-5,
            "expected ~0 invariance for identical pairs, got {inv_val}"
        );
    }

    #[test]
    fn test_diverse_representations_low_variance_loss() {
        let vicreg = VICReg {
            gamma: 1.0,
            ..VICReg::default()
        };
        // Representations with std dev > 1.0 per dimension
        let z: Tensor<TestBackend, 2> = Tensor::random(
            [32, 64],
            burn::tensor::Distribution::Normal(0.0, 2.0),
            &device(),
        );
        let loss = vicreg.compute(&z, &z.clone());
        let var_val: f32 = loss.variance.into_scalar().elem();
        // With std > gamma, hinge should be near zero
        assert!(
            var_val < 5.0,
            "expected low variance loss for diverse representations, got {var_val}"
        );
    }

    #[test]
    fn test_vicreg_total_loss() {
        let vicreg = VICReg::default();
        let z: Tensor<TestBackend, 2> = Tensor::random(
            [16, 32],
            burn::tensor::Distribution::Normal(0.0, 1.0),
            &device(),
        );
        let loss = vicreg.compute(&z, &z);
        let total: f32 = loss.total().into_scalar().elem();
        let sum: f32 = {
            let i: f32 = loss.invariance.into_scalar().elem();
            let v: f32 = loss.variance.into_scalar().elem();
            let c: f32 = loss.covariance.into_scalar().elem();
            i + v + c
        };
        assert!(
            (total - sum).abs() < 1e-4,
            "total() should equal sum of components: {total} vs {sum}"
        );
    }

    #[test]
    fn test_collapse_regularizer_trait() {
        let vicreg = VICReg::default();
        let z: Tensor<TestBackend, 2> = Tensor::random(
            [16, 32],
            burn::tensor::Distribution::Normal(0.0, 1.0),
            &device(),
        );
        let loss: Tensor<TestBackend, 1> = vicreg.loss(&z, &z);
        let val: f32 = loss.into_scalar().elem();
        assert!(val.is_finite(), "loss should be finite, got {val}");
    }

    // --- Barlow Twins tests ---

    #[test]
    fn test_barlow_twins_default_params() {
        let bt = BarlowTwins::default();
        assert!((bt.lambda_bt - 0.005).abs() < 1e-10);
    }

    #[test]
    fn test_barlow_twins_identical_inputs_low_on_diagonal_loss() {
        // Identical inputs → cross-correlation diagonal ≈ 1 → on_diagonal loss ≈ 0
        let bt = BarlowTwins::default();
        let z: Tensor<TestBackend, 2> = Tensor::random(
            [64, 32],
            burn::tensor::Distribution::Normal(0.0, 1.0),
            &device(),
        );
        let loss = bt.compute(&z, &z);
        let on_diag: f32 = loss.on_diagonal.into_scalar().elem();
        assert!(
            on_diag < 1.0,
            "identical inputs should have low on-diagonal loss, got {on_diag}"
        );
    }

    #[test]
    fn test_barlow_twins_loss_is_finite() {
        let bt = BarlowTwins::default();
        let z_a: Tensor<TestBackend, 2> = Tensor::random(
            [32, 16],
            burn::tensor::Distribution::Normal(0.0, 1.0),
            &device(),
        );
        let z_b: Tensor<TestBackend, 2> = Tensor::random(
            [32, 16],
            burn::tensor::Distribution::Normal(0.0, 1.0),
            &device(),
        );
        let loss = bt.compute(&z_a, &z_b);
        let total: f32 = loss.total().into_scalar().elem();
        assert!(
            total.is_finite(),
            "total loss should be finite, got {total}"
        );
    }

    #[test]
    fn test_barlow_twins_loss_is_non_negative() {
        let bt = BarlowTwins::default();
        let z: Tensor<TestBackend, 2> = Tensor::random(
            [32, 16],
            burn::tensor::Distribution::Normal(0.0, 1.0),
            &device(),
        );
        let loss = bt.compute(&z, &z);
        let on_diag: f32 = loss.on_diagonal.into_scalar().elem();
        let off_diag: f32 = loss.off_diagonal.into_scalar().elem();
        assert!(
            on_diag >= -1e-6,
            "on_diagonal should be >= 0, got {on_diag}"
        );
        assert!(
            off_diag >= -1e-6,
            "off_diagonal should be >= 0, got {off_diag}"
        );
    }

    #[test]
    fn test_barlow_twins_total_equals_sum_of_components() {
        let bt = BarlowTwins::default();
        let z: Tensor<TestBackend, 2> = Tensor::random(
            [32, 16],
            burn::tensor::Distribution::Normal(0.0, 1.0),
            &device(),
        );
        let loss = bt.compute(&z, &z);
        let total: f32 = loss.total().into_scalar().elem();
        let on_diag: f32 = loss.on_diagonal.into_scalar().elem();
        let off_diag: f32 = loss.off_diagonal.into_scalar().elem();
        assert!(
            (total - (on_diag + off_diag)).abs() < 1e-4,
            "total should equal on_diag + off_diag: {total} vs {} + {}",
            on_diag,
            off_diag,
        );
    }

    #[test]
    fn test_barlow_twins_implements_collapse_regularizer() {
        let bt = BarlowTwins::default();
        let z: Tensor<TestBackend, 2> = Tensor::random(
            [16, 32],
            burn::tensor::Distribution::Normal(0.0, 1.0),
            &device(),
        );
        let loss: Tensor<TestBackend, 1> = CollapseRegularizer::loss(&bt, &z, &z);
        let val: f32 = loss.into_scalar().elem();
        assert!(
            val.is_finite(),
            "Barlow Twins loss should be finite, got {val}"
        );
    }

    #[test]
    fn test_barlow_twins_higher_lambda_increases_off_diagonal_penalty() {
        let z_a: Tensor<TestBackend, 2> = Tensor::random(
            [32, 16],
            burn::tensor::Distribution::Normal(0.0, 1.0),
            &device(),
        );
        let z_b: Tensor<TestBackend, 2> = Tensor::random(
            [32, 16],
            burn::tensor::Distribution::Normal(0.0, 1.0),
            &device(),
        );

        let bt_low = BarlowTwins::new(0.001);
        let bt_high = BarlowTwins::new(0.1);

        let loss_low: f32 = bt_low.compute(&z_a, &z_b).off_diagonal.into_scalar().elem();
        let loss_high: f32 = bt_high
            .compute(&z_a, &z_b)
            .off_diagonal
            .into_scalar()
            .elem();

        assert!(
            loss_high > loss_low,
            "higher lambda should increase off-diagonal penalty: {loss_high} vs {loss_low}"
        );
    }
}