bunsen 0.40.0

bunsen is a batteries included common library for burn
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
542
543
544
545
546
547
use burn::{
    prelude::TensorData,
    tensor::{
        DType,
        Element,
        FloatDType,
        Tolerance,
        bf16,
        f16,
    },
};
use num_traits::Float;
use serde::{
    Deserialize,
    Serialize,
};

use crate::{
    errors::{
        BunsenError,
        BunsenResult,
        ConstraintError,
    },
    prelude::TensorDataCheckExt,
};

/// Unpacks [`Tolerance<F>`] into `(relative, absolute)`.
///
/// The fields of [`Tolerance`] aren't public, so this is a workaround.
///
/// TODO: remove when <https://github.com/tracel-ai/burn/pull/5674> is released.
pub fn unpack_tolerance<F: Float + Sized>(tolerance: Tolerance<F>) -> (F, F) {
    #[repr(C)]
    struct ToleranceShim<F> {
        pub relative: F,
        pub absolute: F,
    }

    // `std::mem::transmute` requires the compiler to prove that both types have
    // the same size at monomorphization time, which it cannot do through a
    // generic parameter `F`. `transmute_copy` performs the same bitwise move
    // without that static check; guard it with a runtime size assertion to
    // preserve safety in case burn's `Tolerance<F>` layout ever changes.
    assert_eq!(
        std::mem::size_of::<Tolerance<F>>(),
        std::mem::size_of::<ToleranceShim<F>>(),
    );
    let tolerance = std::mem::ManuallyDrop::new(tolerance);
    let shim: ToleranceShim<F> =
        unsafe { std::mem::transmute_copy::<Tolerance<F>, ToleranceShim<F>>(&tolerance) };
    (shim.relative, shim.absolute)
}

/// Float-type-independent description of a [`Tolerance`] rule.
///
/// Each variant corresponds to one [`Tolerance`] constructor. The rule is held
/// symbolically rather than as resolved numbers, because the resolved values
/// depend on the float type: [`Tolerance::strict`] is
/// `64 * F::min_positive_value()`, which differs by many orders of magnitude
/// between `f16` and `f64`. Deferring resolution to
/// [`to_tolerance`](`TolerancePolicy::to_tolerance`) also reproduces burn's
/// rounding of the literal constants into `F`.
///
/// # Equality
/// [`PartialEq`] is *syntactic*, not semantic: [`TolerancePolicy::Strict`] and
/// a [`TolerancePolicy::RelAbs`] carrying the same resolved values are not
/// equal. This is the desired behaviour for audit matching, which compares
/// recorded intent.
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
#[serde(tag = "kind")]
pub enum TolerancePolicy {
    /// `relative = 0`, `absolute = 64 * F::MIN_POSITIVE`.
    ///
    /// See [`Tolerance::strict`].
    Strict,

    /// `relative = 0.5%`, `absolute = 1e-5`.
    ///
    /// See [`Tolerance::balanced`].
    #[default]
    Balanced,

    /// `relative = 1%`, `absolute = 1%`.
    ///
    /// See [`Tolerance::permissive`].
    Permissive,

    /// Relative difference only; `|x - y| < R * max(|x|, |y|)`.
    ///
    /// See [`Tolerance::relative`].
    Relative {
        /// Relative tolerance; must be in `0.0..=1.0`.
        relative: f64,
    },

    /// Absolute difference only; `|x - y| < A`.
    ///
    /// See [`Tolerance::absolute`].
    Absolute {
        /// Absolute tolerance; must be non-negative.
        absolute: f64,
    },

    /// `|x - y| < max(R * max(|x|, |y|), A)`.
    ///
    /// See [`Tolerance::rel_abs`].
    RelAbs {
        /// Relative tolerance; must be in `0.0..=1.0`.
        relative: f64,

        /// Absolute tolerance; must be non-negative.
        absolute: f64,
    },
}

impl TolerancePolicy {
    /// Validate the tolerance bounds.
    ///
    /// [`Tolerance`]'s own constructors assert these, so a policy which has
    /// not been checked will panic inside burn rather than returning an error.
    ///
    /// # Errors
    /// [`Illegal`](crate::errors::BunsenErrorKind::Illegal), with a
    /// [`ConstraintError`] cause, if a relative tolerance is outside
    /// `0.0..=1.0`, or an absolute tolerance is negative.
    pub fn validate(&self) -> BunsenResult<()> {
        let check_relative = |relative: f64| -> BunsenResult<()> {
            if !(0.0..=1.0).contains(&relative) {
                return Err(ConstraintError::out_of_range(
                    "TolerancePolicy",
                    "relative",
                    relative,
                    "[0.0, 1.0]",
                )
                .into());
            }
            Ok(())
        };
        let check_absolute = |absolute: f64| -> BunsenResult<()> {
            if absolute < 0.0 {
                return Err(ConstraintError::out_of_range(
                    "TolerancePolicy",
                    "absolute",
                    absolute,
                    ">= 0.0",
                )
                .into());
            }
            Ok(())
        };

        match *self {
            Self::Strict | Self::Balanced | Self::Permissive => Ok(()),
            Self::Relative { relative } => check_relative(relative),
            Self::Absolute { absolute } => check_absolute(absolute),
            Self::RelAbs { relative, absolute } => {
                check_relative(relative)?;
                check_absolute(absolute)
            }
        }
    }

    /// Resolve to a burn [`Tolerance`] at the float type `F`.
    ///
    /// # Panics
    /// If the policy has not been validated; see
    /// [`validate`](`TolerancePolicy::validate`).
    pub fn to_tolerance<F: Float>(&self) -> Tolerance<F> {
        match *self {
            Self::Strict => Tolerance::strict(),
            Self::Balanced => Tolerance::balanced(),
            Self::Permissive => Tolerance::permissive(),
            Self::Relative { relative } => Tolerance::relative(relative),
            Self::Absolute { absolute } => Tolerance::absolute(absolute),
            Self::RelAbs { relative, absolute } => Tolerance::rel_abs(relative, absolute),
        }
    }
}

/// Reference-free description of a [`Tolerance<F>`], including the `F`.
///
/// A [`TolerancePolicy`] alone under-specifies a comparison, because the
/// resolved tolerance depends on the float type. Pairing the element type with
/// the policy makes the description self-contained, and lets
/// [`try_assert_tensor_data_approx_eq`](`ToleranceDesc::try_assert_tensor_data_approx_eq`)
/// dispatch on it without a type parameter.
///
/// # Element Type
/// `dtype` is the type the *comparison* runs at, which need not match the
/// dtype of the data being compared: [`TensorData`] iteration converts. A
/// `dtype` narrower than the data deliberately compares at reduced precision.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct ToleranceDesc {
    /// The float type the comparison runs at.
    #[serde(with = "crate::burner::descriptors::shims::FloatDTypeShim")]
    float_dtype: FloatDType,

    /// The float-type-independent tolerance rule.
    policy: TolerancePolicy,
}

impl ToleranceDesc {
    /// Construct a [`ToleranceDesc`].
    ///
    /// # Arguments
    /// * `dtype` - the float type to compare at.
    /// * `policy` - the tolerance rule.
    ///
    /// # Errors
    /// [`Illegal`](crate::errors::BunsenErrorKind::Illegal), with a
    /// [`ConstraintError`] cause, if `policy` fails
    /// [`validate`](`TolerancePolicy::validate`).
    pub fn new(
        dtype: FloatDType,
        policy: TolerancePolicy,
    ) -> BunsenResult<Self> {
        policy.validate()?;
        Ok(Self {
            float_dtype: dtype,
            policy,
        })
    }

    /// Construct a [`ToleranceDesc`], narrowing a burn [`DType`].
    ///
    /// # Arguments
    /// * `dtype` - the float type to compare at; see [`FloatDType`].
    /// * `policy` - the tolerance rule.
    ///
    /// # Errors
    /// [`Illegal`](crate::errors::BunsenErrorKind::Illegal) if `dtype` is not
    /// a float type, or if `policy` fails
    /// [`validate`](`TolerancePolicy::validate`) (with a [`ConstraintError`]
    /// cause).
    pub fn try_from_dtype(
        dtype: DType,
        policy: TolerancePolicy,
    ) -> BunsenResult<Self> {
        if !dtype.is_float() {
            return Err(BunsenError::illegal(format!(
                "ToleranceDesc.dtype: {dtype:?} is not a float type"
            )));
        }
        Self::new(FloatDType::from(dtype), policy)
    }

    /// Construct a [`ToleranceDesc`] comparing at the float type `F`.
    ///
    /// # Arguments
    /// * `policy` - the tolerance rule.
    ///
    /// # Errors
    /// [`Illegal`](crate::errors::BunsenErrorKind::Illegal), with a
    /// [`ConstraintError`] cause, if `policy` fails
    /// [`validate`](`TolerancePolicy::validate`).
    pub fn of<F: Float + Element>(policy: TolerancePolicy) -> BunsenResult<Self> {
        Self::new(FloatDType::from(F::dtype()), policy)
    }

    /// The [`FloatDType`] the comparison runs at.
    pub fn float_dtype(&self) -> FloatDType {
        self.float_dtype
    }

    /// The [`DType`] the comparison runs at.
    pub fn dtype(&self) -> DType {
        self.float_dtype.into()
    }

    /// The float-type-independent tolerance rule.
    pub fn policy(&self) -> TolerancePolicy {
        self.policy
    }

    /// Resolve to a burn [`Tolerance`] at the float type `F`.
    ///
    /// Prefer
    /// [`try_assert_tensor_data_approx_eq`](`ToleranceDesc::try_assert_tensor_data_approx_eq`),
    /// which resolves `F` from [`dtype`](`ToleranceDesc::dtype`); this bypasses
    /// that and lets `F` disagree with the recorded element type.
    pub fn to_tolerance_as<F: Float>(&self) -> Tolerance<F> {
        self.policy.to_tolerance()
    }

    /// Assert two [`TensorData`] are approximately equal under this tolerance.
    ///
    /// Dispatches [`TensorDataCheckExt::try_assert_approx_eq`] at
    /// [`dtype`](`ToleranceDesc::dtype`).
    ///
    /// # Arguments
    /// * `actual` - the observed data.
    /// * `expected` - the expected data.
    /// * `strict` - whether to enforce strict dtype equality.
    ///
    /// # Errors
    /// [`Policy`](crate::errors::BunsenErrorKind::Policy), with a
    /// [`ValueMismatch`](crate::errors::ValueMismatch) cause listing the first
    /// differing positions, if the data are not approximately equal.
    pub fn try_assert_tensor_data_approx_eq(
        &self,
        actual: &TensorData,
        expected: &TensorData,
        strict: bool,
    ) -> BunsenResult<()> {
        match self.float_dtype {
            FloatDType::F64 => {
                actual.try_assert_approx_eq::<f64>(expected, self.policy.to_tolerance(), strict)
            }
            FloatDType::F32 | FloatDType::Flex32 => {
                actual.try_assert_approx_eq::<f32>(expected, self.policy.to_tolerance(), strict)
            }
            FloatDType::F16 => {
                actual.try_assert_approx_eq::<f16>(expected, self.policy.to_tolerance(), strict)
            }
            FloatDType::BF16 => {
                actual.try_assert_approx_eq::<bf16>(expected, self.policy.to_tolerance(), strict)
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::errors::{
        BunsenErrorKind,
        ValueMismatch,
        testing::ErrorMatcher,
    };

    #[test]
    fn test_default_is_balanced() {
        assert_eq!(TolerancePolicy::default(), TolerancePolicy::Balanced);
    }

    /// `Strict` resolves differently per float type; this is the property that
    /// justifies holding the policy symbolically instead of as resolved values.
    #[test]
    fn test_strict_depends_on_float_type() {
        let policy = TolerancePolicy::Strict;

        // 64 * f16::MIN_POSITIVE ~= 3.9e-3; 64 * f32::MIN_POSITIVE ~= 7.5e-37.
        let half = policy.to_tolerance::<f16>();
        let single = policy.to_tolerance::<f32>();

        assert!(half.approx_eq(f16::from_f32(1.0), f16::from_f32(1.001)));
        assert!(!single.approx_eq(1.0f32, 1.001f32));
    }

    #[test]
    fn test_policies_resolve_to_burn_behaviour() {
        assert!(
            TolerancePolicy::Permissive
                .to_tolerance::<f32>()
                .approx_eq(1.0, 1.005)
        );
        assert!(
            !TolerancePolicy::Strict
                .to_tolerance::<f32>()
                .approx_eq(1.0, 1.005)
        );

        let rel_abs = TolerancePolicy::RelAbs {
            relative: 0.0,
            absolute: 0.01,
        };
        assert!(rel_abs.to_tolerance::<f32>().approx_eq(1.0, 1.005));
        assert!(!rel_abs.to_tolerance::<f32>().approx_eq(1.0, 1.05));

        let relative = TolerancePolicy::Relative { relative: 0.1 };
        assert!(relative.to_tolerance::<f32>().approx_eq(100.0, 105.0));
        assert!(!relative.to_tolerance::<f32>().approx_eq(100.0, 120.0));

        let absolute = TolerancePolicy::Absolute { absolute: 0.5 };
        assert!(absolute.to_tolerance::<f32>().approx_eq(100.0, 100.4));
        assert!(!absolute.to_tolerance::<f32>().approx_eq(100.0, 100.6));
    }

    #[test]
    fn test_validate() -> BunsenResult<()> {
        TolerancePolicy::Strict.validate()?;
        TolerancePolicy::Balanced.validate()?;
        TolerancePolicy::Permissive.validate()?;
        TolerancePolicy::Relative { relative: 1.0 }.validate()?;
        TolerancePolicy::Absolute { absolute: 0.0 }.validate()?;

        ErrorMatcher::kind(BunsenErrorKind::Illegal)
            .message_eq("TolerancePolicy.relative: 2 is outside [0.0, 1.0]")
            .has_cause::<ConstraintError>()
            .assert_err(&TolerancePolicy::Relative { relative: 2.0 }.validate());

        ErrorMatcher::kind(BunsenErrorKind::Illegal)
            .message_eq("TolerancePolicy.absolute: -1 is outside >= 0.0")
            .assert_err(&TolerancePolicy::Absolute { absolute: -1.0 }.validate());

        ErrorMatcher::kind(BunsenErrorKind::Illegal)
            .message_contains("TolerancePolicy.absolute")
            .assert_err(
                &TolerancePolicy::RelAbs {
                    relative: 0.5,
                    absolute: -1.0,
                }
                .validate(),
            );

        Ok(())
    }

    #[test]
    fn test_desc_records_the_float_type() -> BunsenResult<()> {
        let policy = TolerancePolicy::Balanced;

        assert_eq!(
            ToleranceDesc::of::<f32>(policy)?.float_dtype(),
            FloatDType::F32
        );
        assert_eq!(
            ToleranceDesc::of::<f64>(policy)?.float_dtype(),
            FloatDType::F64
        );
        assert_eq!(
            ToleranceDesc::of::<f16>(policy)?.float_dtype(),
            FloatDType::F16
        );
        assert_eq!(
            ToleranceDesc::of::<bf16>(policy)?.float_dtype(),
            FloatDType::BF16
        );

        assert_eq!(ToleranceDesc::of::<f32>(policy)?.policy(), policy);

        Ok(())
    }

    /// A desc differing only in dtype is a different desc; this is what makes
    /// a replay at a different float type fail on params equality.
    #[test]
    fn test_desc_equality_includes_dtype() -> BunsenResult<()> {
        let policy = TolerancePolicy::Strict;
        assert_ne!(
            ToleranceDesc::of::<f32>(policy)?,
            ToleranceDesc::of::<f16>(policy)?
        );
        Ok(())
    }

    #[test]
    fn test_new_rejects_non_float_dtype() {
        ErrorMatcher::kind(BunsenErrorKind::Illegal)
            .message_contains("is not a float type")
            .assert_err(&ToleranceDesc::try_from_dtype(
                DType::I32,
                TolerancePolicy::Balanced,
            ));
    }

    #[test]
    fn test_new_rejects_invalid_policy() {
        ErrorMatcher::kind(BunsenErrorKind::Illegal)
            .message_contains("is outside [0.0, 1.0]")
            .assert_err(&ToleranceDesc::new(
                FloatDType::F32,
                TolerancePolicy::Relative { relative: 2.0 },
            ));
    }

    #[test]
    fn test_flex32_is_preserved_and_compares_as_f32() -> BunsenResult<()> {
        let desc = ToleranceDesc::try_from_dtype(DType::Flex32, TolerancePolicy::Balanced)?;
        assert_eq!(desc.float_dtype(), FloatDType::Flex32);
        desc.try_assert_tensor_data_approx_eq(
            &TensorData::from([1.0f32]),
            &TensorData::from([1.001f32]),
            true,
        )
    }

    /// The desc dispatches at its own recorded dtype: the same policy accepts a
    /// difference at `f16` that it rejects at `f32`.
    #[test]
    fn test_assert_dispatches_at_recorded_dtype() -> BunsenResult<()> {
        let a = TensorData::from([1.0f32]);
        let b = TensorData::from([1.001f32]);

        ToleranceDesc::of::<f16>(TolerancePolicy::Strict)?
            .try_assert_tensor_data_approx_eq(&a, &b, true)
    }

    #[test]
    fn test_assert_dispatches_at_recorded_dtype_f32() -> BunsenResult<()> {
        let a = TensorData::from([1.0f32]);
        let b = TensorData::from([1.001f32]);

        let result = ToleranceDesc::of::<f32>(TolerancePolicy::Strict)?
            .try_assert_tensor_data_approx_eq(&a, &b, true);
        ErrorMatcher::kind(BunsenErrorKind::Policy)
            .message_eq("1 of 1 values differ")
            .details_contains("position 0: 1 != 1.001")
            .has_cause::<ValueMismatch>()
            .assert_err(&result);

        Ok(())
    }

    #[test]
    fn test_policy_serde_round_trip() -> BunsenResult<()> {
        for policy in [
            TolerancePolicy::Strict,
            TolerancePolicy::Balanced,
            TolerancePolicy::Permissive,
            TolerancePolicy::Relative { relative: 0.25 },
            TolerancePolicy::Absolute { absolute: 1e-5 },
            TolerancePolicy::RelAbs {
                relative: 0.01,
                absolute: 1e-6,
            },
        ] {
            let json = serde_json::to_string(&policy).unwrap();
            let back: TolerancePolicy = serde_json::from_str(&json).unwrap();
            assert_eq!(policy, back, "round trip failed for {json}");
        }

        assert_eq!(
            serde_json::to_string(&TolerancePolicy::Strict).unwrap(),
            r#"{"kind":"Strict"}"#
        );

        Ok(())
    }

    #[test]
    fn test_desc_serde_round_trip() -> BunsenResult<()> {
        let desc = ToleranceDesc::of::<f16>(TolerancePolicy::RelAbs {
            relative: 0.01,
            absolute: 1e-6,
        })?;

        let json = serde_json::to_string(&desc).unwrap();
        let back: ToleranceDesc = serde_json::from_str(&json).unwrap();

        assert_eq!(desc, back);
        assert!(json.contains("F16"), "{json}");

        Ok(())
    }
}