burn-nn 0.22.0

Neural network building blocks for the Burn deep learning framework
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use burn_core as burn;
use burn_core::tensor::IndexingUpdateOp;

use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;
use burn::module::{Content, DisplaySettings, ModuleDisplay};
use burn::tensor::activation::log_softmax;
use burn::tensor::{Bool, Device, Int, Tensor, assert_shape};
use burn::{config::Config, module::Module};

#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
use num_traits::Float;

/// Configuration to create a [Cross-entropy loss](CrossEntropyLoss) using the [init function](CrossEntropyLossConfig::init).
#[derive(Config, Debug)]
pub struct CrossEntropyLossConfig {
    /// Create padded cross entropy.
    ///
    /// Prevents pad tokens from impacting loss calculation.
    ///
    /// An entirely padded batch returns NaN, since the mean has no valid samples.
    pub pad_tokens: Option<Vec<usize>>,
    /// Create weighted cross-entropy.
    ///
    /// The loss of a specific sample will simply be given by: weight * log(p(x)) * 1,
    ///
    /// # Pre-conditions
    ///   - The order of the weight vector should correspond to the label integer assignment.
    ///   - Targets assigned negative Int's will not be allowed.
    pub weights: Option<Vec<f32>>,

    /// Create cross-entropy with label smoothing.
    ///
    /// Hard labels {0, 1} will be changed to y_smoothed = y(1 - a) + a / nr_classes.
    /// Alpha = 0 would be the same as default.
    pub smoothing: Option<f32>,

    /// Create cross-entropy with probabilities as input instead of logits.
    ///
    #[config(default = true)]
    pub logits: bool,
}

impl CrossEntropyLossConfig {
    /// Initialize [Cross-entropy loss](CrossEntropyLoss).
    pub fn init(&self, device: &Device) -> CrossEntropyLoss {
        self.assertions();
        CrossEntropyLoss {
            pad_tokens: self.pad_tokens.clone(),
            weights: self
                .weights
                .as_ref()
                .map(|e| Tensor::<1>::from_floats(e.as_slice(), device)),
            smoothing: self.smoothing,
            logits: self.logits,
        }
    }

    fn assertions(&self) {
        if let Some(alpha) = self.smoothing {
            assert!(
                (0.0..=1.).contains(&alpha),
                "Alpha of Cross-entropy loss with smoothed labels should be in interval [0, 1]. Got {alpha}"
            );
        };
        if let Some(weights) = self.weights.as_ref() {
            assert!(
                weights.iter().all(|e| e > &0.),
                "Weights of cross-entropy have to be positive."
            );
        }
    }
}

/// Calculate the cross entropy loss from the input logits and the targets.
///
/// Should be created using [CrossEntropyLossConfig]
#[derive(Module, Debug)]
#[module(custom_display)]
pub struct CrossEntropyLoss {
    /// Pad tokens to ignore in the loss calculation.
    pub pad_tokens: Option<Vec<usize>>,
    /// Weights for cross-entropy.
    pub weights: Option<Tensor<1>>,
    /// Label smoothing factor.
    pub smoothing: Option<f32>,
    /// Use logits as input.
    pub logits: bool,
}

impl ModuleDisplay for CrossEntropyLoss {
    fn custom_settings(&self) -> Option<DisplaySettings> {
        DisplaySettings::new()
            .with_new_line_after_attribute(false)
            .optional()
    }

    fn custom_content(&self, content: Content) -> Option<Content> {
        let pad_tokens = if let Some(pad_tokens) = &self.pad_tokens {
            alloc::format!("Vec<0..{}>", pad_tokens.len())
        } else {
            "None".to_string()
        };

        content
            .add("pad_tokens", &pad_tokens)
            .add("weights", &self.weights)
            .add("smoothing", &self.smoothing)
            .add("logits", &self.logits)
            .optional()
    }
}

impl CrossEntropyLoss {
    /// For backward compatibility.
    pub fn new(pad_index: Option<usize>, device: &Device) -> Self {
        CrossEntropyLossConfig::new()
            .with_pad_tokens(pad_index.map(|e| vec![e]))
            .init(device)
    }

    /// Compute the criterion on the input tensor.
    ///
    /// # Shapes
    ///
    /// - logits: `[batch_size, num_targets]`
    /// - targets: `[batch_size]`
    pub fn forward(&self, logits: Tensor<2>, targets: Tensor<1, Int>) -> Tensor<1> {
        Self::assertions(logits.clone(), targets.clone());
        match self.smoothing {
            Some(alpha) => self.forward_smoothed(logits, targets, alpha),
            _ => self.forward_default(logits, targets),
        }
    }

    fn forward_smoothed(
        &self,
        logits: Tensor<2>,
        targets: Tensor<1, Int>,
        alpha: f32,
    ) -> Tensor<1> {
        let mask = self.padding_mask(&targets);
        let tensor = if self.logits {
            log_softmax(logits, 1)
        } else {
            Self::clamp_probs(logits).log()
        };
        let [batch_size, nr_classes] = tensor.dims();
        let tensor = tensor
            * Self::compute_smoothed_targets([batch_size, nr_classes], targets.clone(), alpha);

        let (tensor, weights) = match &self.weights {
            Some(weights) => {
                let tensor = tensor
                    * weights
                        .clone()
                        .reshape([1, nr_classes])
                        .repeat_dim(0, batch_size);
                let weights = weights.clone().gather(0, targets);
                (tensor, Some(weights))
            }
            None => (tensor, None),
        };

        let tensor = tensor.sum_dim(1).squeeze_dim::<1>(1);
        Self::reduce_mean(tensor, weights, mask)
    }

    fn forward_default(&self, logits: Tensor<2>, targets: Tensor<1, Int>) -> Tensor<1> {
        let [batch_size] = targets.dims();

        let mask = self.padding_mask(&targets);
        let target_indices = targets.clone().reshape([batch_size, 1]);
        let tensor = if self.logits {
            log_softmax(logits, 1).gather(1, target_indices)
        } else {
            Self::clamp_probs(logits).gather(1, target_indices).log()
        };

        let tensor = tensor.reshape([batch_size]);
        let (tensor, weights) = match &self.weights {
            Some(weights) => {
                let weights = weights.clone().gather(0, targets);
                (tensor * weights.clone(), Some(weights))
            }
            None => (tensor, None),
        };

        Self::reduce_mean(tensor, weights, mask)
    }

    /// Clamps probabilities away from zero so their log stays finite.
    fn clamp_probs(probs: Tensor<2>) -> Tensor<2> {
        // TODO: finfo stable eps
        let finfo = probs.dtype().finfo().unwrap();
        let eps = finfo.min_positive.sqrt();
        probs.clamp_min(eps)
    }

    fn compute_smoothed_targets(
        shape: [usize; 2],
        targets: Tensor<1, Int>,
        alpha: f32,
    ) -> Tensor<2> {
        let [batch_size, nr_classes] = shape;
        let device = &targets.device();
        let targets_matrix = Tensor::<2>::zeros(shape, device).scatter(
            1,
            targets.reshape([batch_size, 1]),
            Tensor::ones([batch_size, 1], device),
            IndexingUpdateOp::Add,
        );
        targets_matrix * (1. - alpha) + alpha / nr_classes as f32
    }

    fn padding_mask(&self, targets: &Tensor<1, Int>) -> Option<Tensor<1, Bool>> {
        self.pad_tokens.as_ref().and_then(|pad_tokens| {
            pad_tokens
                .iter()
                .map(|token| targets.clone().equal_scalar(*token as i64))
                .reduce(|mask, token_mask| mask.bool_or(token_mask))
        })
    }

    fn reduce_mean(
        tensor: Tensor<1>,
        weights: Option<Tensor<1>>,
        mask: Option<Tensor<1, Bool>>,
    ) -> Tensor<1> {
        if weights.is_none() && mask.is_none() {
            return tensor.mean().neg();
        }

        let normalizer = weights.unwrap_or_else(|| tensor.ones_like());
        let tensor = Self::apply_mask(tensor, mask.clone());
        let normalizer = Self::apply_mask(normalizer, mask);

        tensor.sum().neg() / normalizer.sum()
    }

    fn apply_mask(mut tensor: Tensor<1>, mask: Option<Tensor<1, Bool>>) -> Tensor<1> {
        if let Some(mask) = mask {
            tensor = tensor.mask_fill(mask, 0);
        }

        tensor
    }

    fn assertions(logits: Tensor<2>, targets: Tensor<1, Int>) {
        let [batch_size, _] = logits.dims();
        assert_shape!(targets, [batch_size]);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use burn::tensor::Tolerance;
    use burn::tensor::{Distribution, TensorData, loss::cross_entropy_with_logits};
    type FT = f32;

    macro_rules! setup {
        () => {{
            let [batch_size, num_targets] = [4, 5];
            let device = Default::default();
            let logits = Tensor::<2>::random(
                [batch_size, num_targets],
                Distribution::Normal(0., 1.0),
                &device,
            );
            let targets = Tensor::<1, Int>::from_data(TensorData::from([2, 0, 4, 1]), &device);
            let targets_logits = Tensor::<2>::from_data(
                TensorData::from([
                    [0.0, 0.0, 1.0, 0.0, 0.0],
                    [1.0, 0.0, 0.0, 0.0, 0.0],
                    [0.0, 0.0, 0.0, 0.0, 1.0],
                    [0.0, 1.0, 0.0, 0.0, 0.0],
                ]),
                &device,
            );
            (logits, targets, targets_logits)
        }};
    }

    macro_rules! setup_padded {
        () => {{
            let [batch_size, num_targets, pad_index] = [4, 5, 1];
            let device = Default::default();
            let logits = Tensor::<2>::random(
                [batch_size, num_targets],
                Distribution::Normal(0., 1.0),
                &device,
            );
            let targets =
                Tensor::<1, Int>::from_data(TensorData::from([2, 0, 4, pad_index as i64]), &device);
            let targets_logits = Tensor::<2>::from_data(
                TensorData::from([
                    [0.0, 0.0, 0.0, 0.0, 0.0],
                    [1.0, 0.0, 0.0, 0.0, 0.0],
                    [0.0, 0.0, 0.0, 0.0, 1.0],
                    [0.0, 0.0, 0.0, 0.0, 0.0],
                ]),
                &device,
            );
            (logits, targets, targets_logits)
        }};
    }

    #[test]
    #[should_panic(expected = "assert_shape!(targets, [batch_size]): axis 0 expected 4, got 3")]
    fn test_cross_entropy_loss_targets_must_match_batch_size() {
        let (logits, _, _) = setup!();
        let device = logits.device();
        let targets = Tensor::<1, Int>::from_data(TensorData::from([2, 0, 4]), &device);
        let _ = CrossEntropyLossConfig::new()
            .init(&device)
            .forward(logits, targets);
    }

    #[test]
    fn test_cross_entropy_loss_with_weights() {
        let (logits, targets, targets_logits) = setup!();
        let weights = vec![1.0, 2., 3., 4., 5.];
        let device = Default::default();
        let loss_1 = CrossEntropyLossConfig::new()
            .with_weights(Some(weights.clone()))
            .init(&device)
            .forward(logits.clone(), targets);
        let tensor = log_softmax(logits, 1);
        let loss_2 = tensor
            * targets_logits
            * Tensor::<1>::from_floats(weights.as_slice(), &device)
                .unsqueeze()
                .repeat_dim(0, 4);
        let loss_2 = loss_2.sum().neg() / (1. + 2. + 3. + 5.);
        loss_1
            .into_data()
            .assert_approx_eq::<FT>(&loss_2.into_data(), Tolerance::default());
    }

    #[test]
    fn test_label_smoothing_with_weights_and_alpha_zero() {
        let (logits, targets, _) = setup!();
        let device = Default::default();
        let weights = vec![1.0, 2., 3., 4., 5.];
        let loss_1 = CrossEntropyLossConfig::new()
            .with_weights(Some(weights.clone()))
            .init(&device)
            .forward(logits.clone(), targets.clone());
        let loss_2 = CrossEntropyLossConfig::new()
            .with_weights(Some(weights.clone()))
            .with_smoothing(Some(0.))
            .init(&device)
            .forward(logits.clone(), targets);
        loss_1
            .into_data()
            .assert_approx_eq::<FT>(&loss_2.into_data(), Tolerance::default());
    }

    #[test]
    fn test_cross_entropy_loss() {
        let (logits, targets, targets_logits) = setup!();
        let device = Default::default();
        let loss_1 = CrossEntropyLossConfig::new()
            .init(&device)
            .forward(logits.clone(), targets);
        let loss_2 = cross_entropy_with_logits(logits, targets_logits);

        loss_1
            .into_data()
            .assert_approx_eq::<FT>(&loss_2.into_data(), Tolerance::default());
    }

    #[test]
    fn test_label_smoothing_alpha_equal_zero() {
        let (logits, targets, _) = setup!();
        let device = Default::default();
        let loss_1 = CrossEntropyLossConfig::new()
            .init(&device)
            .forward(logits.clone(), targets.clone());
        let loss_2 = CrossEntropyLossConfig::new()
            .with_smoothing(Some(0.))
            .init(&device)
            .forward(logits, targets);

        loss_1
            .into_data()
            .assert_approx_eq::<FT>(&loss_2.into_data(), Tolerance::default());
    }

    #[test]
    fn test_cross_entropy_loss_with_pad_token() {
        let (logits, targets, targets_logits) = setup_padded!();
        let device = logits.device();
        let pad_index = 1;

        let loss_1 = CrossEntropyLossConfig::new()
            .with_pad_tokens(Some(vec![pad_index, 2]))
            .init(&device)
            .forward(logits.clone(), targets);
        let valid_indices = Tensor::<1, Int>::from_data(TensorData::from([1i64, 2]), &device);
        let loss_2 = cross_entropy_with_logits(
            logits.select(0, valid_indices.clone()),
            targets_logits.select(0, valid_indices),
        );

        loss_1
            .into_data()
            .assert_approx_eq::<FT>(&loss_2.into_data(), Tolerance::default());
    }

    #[test]
    fn test_label_smoothing_with_zero_alpha_and_pad_token() {
        let (logits, targets, _) = setup_padded!();
        let pad_index = 1;

        let loss_1 = CrossEntropyLossConfig::new()
            .with_pad_tokens(Some(vec![pad_index, 2]))
            .init(&logits.device())
            .forward(logits.clone(), targets.clone());
        let loss_2 = CrossEntropyLossConfig::new()
            .with_pad_tokens(Some(vec![pad_index, 2]))
            .with_smoothing(Some(0.))
            .init(&logits.device())
            .forward(logits.clone(), targets);

        loss_1
            .into_data()
            .assert_approx_eq::<FT>(&loss_2.into_data(), Tolerance::default());
    }

    #[test]
    fn test_label_smoothing_target_conversion() {
        let (logits, targets, _) = setup!();
        let smoothed_targets =
            CrossEntropyLoss::compute_smoothed_targets(logits.dims(), targets, 0.05);
        let targets_logits = Tensor::<2>::from_data(
            TensorData::from([
                [0.01, 0.01, 0.96, 0.01, 0.01],
                [0.96, 0.01, 0.01, 0.01, 0.01],
                [0.01, 0.01, 0.01, 0.01, 0.96],
                [0.01, 0.96, 0.01, 0.01, 0.01],
            ]),
            &Default::default(),
        );
        smoothed_targets
            .into_data()
            .assert_approx_eq::<FT>(&targets_logits.into_data(), Tolerance::default());
    }

    #[test]
    fn test_label_smoothing() {
        let (logits, targets, _) = setup!();
        let device = Default::default();
        let loss_1 = CrossEntropyLossConfig::new()
            .with_smoothing(Some(0.05))
            .init(&device)
            .forward(logits.clone(), targets);
        let targets_logits = Tensor::<2>::from_data(
            TensorData::from([
                [0.01, 0.01, 0.96, 0.01, 0.01],
                [0.96, 0.01, 0.01, 0.01, 0.01],
                [0.01, 0.01, 0.01, 0.01, 0.96],
                [0.01, 0.96, 0.01, 0.01, 0.01],
            ]),
            &device,
        );

        let x = log_softmax(logits, 1);
        let loss_2 = (x * targets_logits).sum_dim(1).mean().neg();

        loss_1
            .into_data()
            .assert_approx_eq::<FT>(&loss_2.into_data(), Tolerance::default());
    }

    #[test]
    fn test_logits_flag_affects_output() {
        let device = Default::default();

        let probs = Tensor::<2>::from_data(
            TensorData::from([
                [0.1, 0.2, 0.7, 0.0, 0.0],
                [0.7, 0.1, 0.1, 0.1, 0.0],
                [0.2, 0.2, 0.2, 0.2, 0.2],
                [0.0, 0.3, 0.3, 0.2, 0.2],
            ]),
            &device,
        );

        let targets = Tensor::<1, Int>::from_data(TensorData::from([2, 0, 4, 1]), &device);

        let loss_logits = CrossEntropyLossConfig::new()
            .init(&device)
            .forward(probs.clone(), targets.clone());

        let loss_probs = CrossEntropyLossConfig::new()
            .with_logits(false)
            .init(&device)
            .forward(probs, targets);

        // They must differ if logits flag is implemented correctly
        let loss_logits = loss_logits.into_data();
        let loss_probs = loss_probs.into_data();

        loss_logits.assert_approx_eq::<f32>(&TensorData::from([1.354197]), Tolerance::default());
        loss_probs.assert_approx_eq::<f32>(&TensorData::from([0.88169014]), Tolerance::default());

        assert_ne!(
            loss_logits.as_slice::<f32>().unwrap(),
            loss_probs.as_slice::<f32>().unwrap(),
            "logits flag should change computation (log_softmax vs log)"
        );
    }

    #[test]
    fn test_label_smoothing_with_zero_probabilities() {
        let device = Default::default();

        let probs = Tensor::<2>::from_data(
            TensorData::from([
                [0.1, 0.2, 0.7, 0.0, 0.0],
                [0.7, 0.1, 0.1, 0.1, 0.0],
                [0.2, 0.2, 0.2, 0.2, 0.2],
                [0.0, 0.3, 0.3, 0.2, 0.2],
            ]),
            &device,
        );
        let targets = Tensor::<1, Int>::from_data(TensorData::from([2, 0, 4, 1]), &device);

        let loss_default = CrossEntropyLossConfig::new()
            .with_logits(false)
            .init(&device)
            .forward(probs.clone(), targets.clone());
        let loss_no_smoothing = CrossEntropyLossConfig::new()
            .with_logits(false)
            .with_smoothing(Some(0.0))
            .init(&device)
            .forward(probs.clone(), targets.clone());
        let loss_smoothed = CrossEntropyLossConfig::new()
            .with_logits(false)
            .with_smoothing(Some(0.1))
            .init(&device)
            .forward(probs, targets);

        // Zero probabilities are clamped like in the non-smoothed path, so alpha = 0
        // matches it and alpha > 0 stays finite.
        loss_no_smoothing
            .into_data()
            .assert_approx_eq::<FT>(&loss_default.into_data(), Tolerance::default());
        loss_smoothed
            .into_data()
            .assert_approx_eq::<FT>(&TensorData::from([1.7929223]), Tolerance::default());
    }

    fn assert_padding_invariant(weights: Option<Vec<f32>>, smoothing: Option<f32>, logits: bool) {
        let device = Default::default();
        let valid_input = Tensor::<2>::from_data(
            TensorData::from([[0.7f32, 0.2, 0.1], [0.1, 0.7, 0.2]]),
            &device,
        );
        let valid_targets = Tensor::<1, Int>::from_data(TensorData::from([0i64, 1]), &device);
        let padded_input = Tensor::<2>::from_data(
            TensorData::from([
                [0.7f32, 0.2, 0.1],
                [0.1, 0.7, 0.2],
                [0.2, 0.3, 0.5],
                [0.3, 0.2, 0.5],
            ]),
            &device,
        );
        let padded_targets =
            Tensor::<1, Int>::from_data(TensorData::from([0i64, 1, 2, 2]), &device);

        let create_loss = || {
            CrossEntropyLossConfig::new()
                .with_pad_tokens(Some(vec![2]))
                .with_weights(weights.clone())
                .with_smoothing(smoothing)
                .with_logits(logits)
                .init(&device)
        };
        let expected = create_loss().forward(valid_input, valid_targets);
        let actual = create_loss().forward(padded_input, padded_targets);

        actual
            .into_data()
            .assert_approx_eq::<FT>(&expected.into_data(), Tolerance::default());
    }

    #[test]
    fn padding_does_not_change_default_loss() {
        assert_padding_invariant(None, None, true);
    }

    #[test]
    fn padding_does_not_change_weighted_loss() {
        assert_padding_invariant(Some(vec![1.0, 2.0, 4.0]), None, true);
    }

    #[test]
    fn padding_does_not_change_smoothed_loss() {
        assert_padding_invariant(None, Some(0.1), true);
    }

    #[test]
    fn padding_does_not_change_smoothed_weighted_loss() {
        assert_padding_invariant(Some(vec![1.0, 2.0, 4.0]), Some(0.1), true);
    }

    #[test]
    fn padding_does_not_change_probability_loss() {
        assert_padding_invariant(None, None, false);
    }

    #[test]
    fn multiple_pad_tokens_are_excluded_from_normalization() {
        let device = Default::default();
        let valid_input = Tensor::<2>::from_data(TensorData::from([[0.7f32, 0.2, 0.1]]), &device);
        let valid_targets = Tensor::<1, Int>::from_data(TensorData::from([0i64]), &device);
        let padded_input = Tensor::<2>::from_data(
            TensorData::from([[0.7f32, 0.2, 0.1], [0.2, 0.6, 0.2], [0.1, 0.2, 0.7]]),
            &device,
        );
        let padded_targets = Tensor::<1, Int>::from_data(TensorData::from([0i64, 1, 2]), &device);

        let create_loss = || {
            CrossEntropyLossConfig::new()
                .with_pad_tokens(Some(vec![1, 2]))
                .init(&device)
        };
        let expected = create_loss().forward(valid_input, valid_targets);
        let actual = create_loss().forward(padded_input, padded_targets);

        actual
            .into_data()
            .assert_approx_eq::<FT>(&expected.into_data(), Tolerance::default());
    }

    #[test]
    fn empty_pad_token_list_behaves_like_no_padding() {
        let device = Default::default();
        let input = Tensor::<2>::from_data(
            TensorData::from([[0.7f32, 0.2, 0.1], [0.1, 0.7, 0.2]]),
            &device,
        );
        let targets = Tensor::<1, Int>::from_data(TensorData::from([0i64, 1]), &device);

        let expected = CrossEntropyLossConfig::new()
            .init(&device)
            .forward(input.clone(), targets.clone());
        let actual = CrossEntropyLossConfig::new()
            .with_pad_tokens(Some(vec![]))
            .init(&device)
            .forward(input, targets);

        actual
            .into_data()
            .assert_approx_eq::<FT>(&expected.into_data(), Tolerance::default());
    }

    #[test]
    fn entirely_padded_batch_returns_nan() {
        let device = Default::default();
        let input = Tensor::<2>::from_data(
            TensorData::from([[0.7f32, 0.2, 0.1], [0.1, 0.2, 0.7]]),
            &device,
        );
        let targets = Tensor::<1, Int>::from_data(TensorData::from([2i64, 2]), &device);

        let loss = CrossEntropyLossConfig::new()
            .with_pad_tokens(Some(vec![2]))
            .init(&device)
            .forward(input, targets)
            .into_scalar::<FT>();

        assert!(loss.is_nan());
    }

    #[cfg(feature = "std")]
    #[test]
    fn padded_targets_have_zero_gradient_without_scaling_valid_gradients() {
        let device = Device::default().autodiff();
        let logits = Tensor::<2>::from_data(
            TensorData::from([[2.0f32, 0.0, -1.0], [0.0, 0.0, 0.0]]),
            &device,
        )
        .require_grad();
        let targets = Tensor::<1, Int>::from_data(TensorData::from([0i64, 2]), &device);

        let loss = CrossEntropyLossConfig::new()
            .with_pad_tokens(Some(vec![2]))
            .init(&device)
            .forward(logits.clone(), targets);
        let grads = loss.backward();
        let grads_logits = logits.grad(&grads).unwrap();

        let expected = TensorData::from([[-0.1562053f32, 0.1141952, 0.04201007], [0.0, 0.0, 0.0]]);
        grads_logits
            .into_data()
            .assert_approx_eq::<FT>(&expected, Tolerance::relative(1e-4));
    }

    #[test]
    fn display() {
        let config = CrossEntropyLossConfig::new()
            .with_weights(Some(alloc::vec![3., 7., 0.9]))
            .with_smoothing(Some(0.5));
        let loss = config.init(&Default::default());

        assert_eq!(
            alloc::format!("{loss}"),
            "CrossEntropyLoss {pad_tokens: None, weights: Tensor {rank: 1, shape: [3]}, smoothing: 0.5, logits: true}"
        );
    }
}