bunsen 0.35.1

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
use core::fmt::Debug;
use std::{
    collections::HashMap,
    panic::Location,
};

use burn::{
    Tensor,
    prelude::{
        Backend,
        TensorData,
    },
    tensor::{
        BasicOps,
        DType,
        Element,
        FloatDType,
        f16,
    },
};
use num_traits::Float;
use time::{
    OffsetDateTime,
    macros::format_description,
};

use crate::{
    burner::{
        descriptors::{
            ToleranceDesc,
            TolerancePolicy,
        },
        testing::audit::{
            AuditProbeEventHandler,
            AuditProbeEventHeader,
            AuditProbeEventStub,
            AuditProbeEventView,
            unpack_audit_probe_event_data,
        },
    },
    errors::{
        BunsenError,
        BunsenResult,
    },
    prelude::{
        TensorDataCheckExt,
        TensorElemOpExt,
    },
    support::CloneRef,
};

/// Argument conversion trait for [`Tensor`] and [`TensorData`].
pub trait DataArg<'a>: Sized {
    /// Build a default [`CloneRef`] to [`TensorData`].
    fn into_data_ref(self) -> CloneRef<'a, TensorData>;

    /// Build a [`CloneRef`] to [`TensorData`] with a specific element type.
    fn into_data_ref_as<E: Element>(self) -> CloneRef<'a, TensorData>;

    /// Build a [`CloneRef`] to [`TensorData`] with a specific element type.
    fn into_data_ref_cast(
        self,
        dtype: DType,
    ) -> CloneRef<'a, TensorData> {
        match dtype {
            DType::QFloat(_) => self.into_data_ref_as::<f32>(),
            DType::F64 => self.into_data_ref_as::<f64>(),
            DType::F16 => self.into_data_ref_as::<f16>(),
            DType::F32 => self.into_data_ref_as::<f32>(),
            DType::Flex32 => self.into_data_ref_as::<f32>(),
            DType::BF16 => self.into_data_ref_as::<f16>(),
            DType::I8 => self.into_data_ref_as::<i8>(),
            DType::I16 => self.into_data_ref_as::<i16>(),
            DType::I32 => self.into_data_ref_as::<i32>(),
            DType::I64 => self.into_data_ref_as::<i64>(),
            DType::U8 => self.into_data_ref_as::<u8>(),
            DType::U16 => self.into_data_ref_as::<u16>(),
            DType::U32 => self.into_data_ref_as::<u32>(),
            DType::U64 => self.into_data_ref_as::<u64>(),
            DType::Bool(_) => self.into_data_ref_as::<bool>(),
        }
    }
}

impl<'a> DataArg<'a> for &'a TensorData {
    fn into_data_ref(self) -> CloneRef<'a, TensorData> {
        CloneRef::Ref(self)
    }

    fn into_data_ref_as<E: Element>(self) -> CloneRef<'a, TensorData> {
        CloneRef::Clone(self.clone().convert::<E>())
    }
}

impl<'a, B: Backend, const R: usize, K: BasicOps<B>> DataArg<'a> for &'a Tensor<B, R, K> {
    fn into_data_ref(self) -> CloneRef<'a, TensorData> {
        CloneRef::Clone(self.to_data())
    }

    fn into_data_ref_as<E: Element>(self) -> CloneRef<'a, TensorData> {
        CloneRef::Clone(self.to_data_as::<E>())
    }
}

/// Probe for auditing purposes.
#[derive(Debug)]
pub struct AuditProbe<'a> {
    handlers: Vec<&'a mut dyn AuditProbeEventHandler>,
}

impl<'a> AuditProbeEventHandler for AuditProbe<'a> {
    fn on_event(
        &mut self,
        stub: &AuditProbeEventStub<'_>,
    ) -> BunsenResult<()> {
        for handler in &mut self.handlers {
            handler.on_event(stub)?;
        }
        Ok(())
    }
}

impl<'a> AuditProbe<'a> {
    /// Construct a new audit probe.
    pub fn new(handlers: Vec<&'a mut dyn AuditProbeEventHandler>) -> Self {
        Self { handlers }
    }

    /// [`Tensor`] / [`TensorData`] equality.
    ///
    /// # Arguments
    /// * `header` - Event header.
    /// * `data` - the [`TensorData`] to compare.
    ///
    /// # Panics and/or Err Returns
    /// If `policy` is out of range, or if the data do not match under
    /// verification.
    fn dispatch_assert_eq(
        &mut self,
        header: AuditProbeEventHeader,
        data: &TensorData,
    ) -> BunsenResult<()> {
        let data: HashMap<String, Vec<&TensorData>> =
            HashMap::from([("data".to_string(), vec![data])]);

        self.on_event(&AuditProbeEventStub {
            header,
            params: AuditProbeEventParams::AssertTensorEq,
            data,
        })
    }

    /// [`Tensor`] / [`TensorData`] approximate equality.
    ///
    /// Data is compared at the float type `F`.
    ///
    /// `F` is recorded in the event as part of the [`ToleranceDesc`], so a
    /// replay comparing at a different float type fails on params equality
    /// rather than silently applying a different tolerance.
    ///
    /// # Arguments
    /// * `header` - Event header.
    /// * `data` - the [`TensorData`] to compare.
    /// * `tolerance` - the [`TolerancePolicy`] to compare under.
    ///
    /// # Panics and/or Err Returns
    /// If `policy` is out of range, or if the data do not match under
    /// verification.
    fn dispatch_assert_approx_eq(
        &mut self,
        header: AuditProbeEventHeader,
        data: &TensorData,
        tolerance: ToleranceDesc,
    ) -> BunsenResult<()> {
        let data: HashMap<String, Vec<&TensorData>> =
            HashMap::from([("data".to_string(), vec![data])]);

        self.on_event(&AuditProbeEventStub {
            header,
            params: AuditProbeEventParams::AssertTensorApproxEx { tolerance },
            data,
        })
    }

    /// [`Tensor`] / [`TensorData`] equality.
    ///
    /// Converts to `E` before storage/comparison.
    ///
    /// # Arguments
    /// * `label` - Event Label.
    /// * `data` - the data to compare.
    ///
    /// # Result
    /// This *may* return an error if the data or data types do not match under
    /// verification.
    #[track_caller]
    pub fn assert_eq_as<'b, E: Element>(
        &mut self,
        label: &str,
        data: impl DataArg<'b>,
    ) -> BunsenResult<()> {
        let header = AuditProbeEventHeader::new(
            Some(label.to_string()),
            Some(Location::caller().into()),
            None,
        );
        let data_cr = data.into_data_ref_as::<E>();

        self.dispatch_assert_eq(header, data_cr.as_ref())
    }

    /// [`Tensor`] / [`TensorData`] equality.
    ///
    /// # Arguments
    /// * `label` - Event Label.
    /// * `dtype` - the data type to convert to / compare as.
    /// * `data` - the data to compare.
    ///
    /// # Result
    /// This *may* return an error if the data or data types do not match under
    /// verification.
    #[track_caller]
    pub fn assert_eq_cast<'b>(
        &mut self,
        label: &str,
        data: impl DataArg<'b>,
        dtype: DType,
    ) -> BunsenResult<()> {
        let header = AuditProbeEventHeader::new(
            Some(label.to_string()),
            Some(Location::caller().into()),
            None,
        );
        let data_cr = data.into_data_ref_cast(dtype);

        self.dispatch_assert_eq(header, data_cr.as_ref())
    }

    /// [`Tensor`] / [`TensorData`] approximate equality.
    ///
    /// Data is compared at the float type `F`.
    ///
    /// `F` is recorded in the event as part of the [`ToleranceDesc`], so a
    /// replay comparing at a different float type fails on params equality
    /// rather than silently applying a different tolerance.
    ///
    /// # Arguments
    /// * `label` - Event Label.
    /// * `data` - the [`TensorData`] to compare.
    /// * `tolerance` - the [`TolerancePolicy`] to compare under.
    ///
    /// # Panics and/or Err Returns
    /// If `policy` is out of range, or if the data do not match under
    /// verification.
    #[track_caller]
    pub fn assert_approx_eq_as<'b, F: Float + Element>(
        &mut self,
        label: &str,
        data: impl DataArg<'b>,
        tolerance: TolerancePolicy,
    ) -> BunsenResult<()> {
        let header = AuditProbeEventHeader::new(
            Some(label.to_string()),
            Some(Location::caller().into()),
            None,
        );
        let data_cr = data.into_data_ref_as::<F>();

        let tolerance = ToleranceDesc::of::<F>(tolerance)?;

        self.dispatch_assert_approx_eq(header, data_cr.as_ref(), tolerance)
    }

    /// [`Tensor`] / [`TensorData`] approximate equality.
    ///
    /// Data is converted to and compared at the float type `F`.
    ///
    /// # Arguments
    /// * `label` - Event Label.
    /// * `data` - the [`TensorData`] to compare.
    /// * `tolerance` - the [`TolerancePolicy`] to compare under.
    ///
    /// # Panics and/or Err Returns
    /// If the data or data types do not match under verification.
    #[track_caller]
    pub fn assert_approx_eq_cast<'b>(
        &mut self,
        label: &str,
        data: impl DataArg<'b>,
        tolerance: TolerancePolicy,
        dtype: FloatDType,
    ) -> BunsenResult<()> {
        let header = AuditProbeEventHeader::new(
            Some(label.to_string()),
            Some(Location::caller().into()),
            None,
        );
        let (data_cr, tolerance) = match dtype {
            FloatDType::F64 => (
                data.into_data_ref_as::<f64>(),
                ToleranceDesc::of::<f64>(tolerance)?,
            ),
            FloatDType::F32 => (
                data.into_data_ref_as::<f32>(),
                ToleranceDesc::of::<f32>(tolerance)?,
            ),
            FloatDType::Flex32 => (
                data.into_data_ref_as::<f32>(),
                ToleranceDesc::of::<f32>(tolerance)?,
            ),
            FloatDType::F16 => (
                data.into_data_ref_as::<f16>(),
                ToleranceDesc::of::<f16>(tolerance)?,
            ),
            FloatDType::BF16 => (
                data.into_data_ref_as::<f16>(),
                ToleranceDesc::of::<f16>(tolerance)?,
            ),
        };

        self.dispatch_assert_approx_eq(header, data_cr.as_ref(), tolerance)
    }
}

/// Type-specific event params.
#[derive(Debug, Clone, PartialEq, strum::Display, strum::EnumDiscriminants)]
#[strum_discriminants(derive(strum::AsRefStr))]
pub enum AuditProbeEventParams {
    /// `assert_eq` event.
    AssertTensorEq,

    /// `assert_approx_eq` event.
    AssertTensorApproxEx {
        /// Tolerance.
        tolerance: ToleranceDesc,
    },
}

impl AuditProbeEventParams {
    /// Get the variant name.
    pub fn variant_name(&self) -> String {
        let d = AuditProbeEventParamsDiscriminants::from(self);

        let name: &str = d.as_ref();

        name.to_string()
    }
}

/// Applies per-type event equality.
pub fn try_match_events(
    actual: &impl AuditProbeEventView,
    expected: &impl AuditProbeEventView,
) -> BunsenResult<()> {
    fn cmp(
        actual: &impl AuditProbeEventView,
        expected: &impl AuditProbeEventView,
    ) -> BunsenResult<()> {
        if expected.params() != actual.params() {
            return Err(BunsenError::Invalid(format!(
                "StreamEvent params {:?} != expected {:?}",
                expected.params(),
                actual.params()
            )));
        }

        actual.assert_shape_signatures_eq(expected)?;
        match actual.params() {
            AuditProbeEventParams::AssertTensorEq => {
                let [actual_data, expected_data] =
                    unpack_audit_probe_event_data!([actual, expected], { data })?;
                actual_data.data.try_assert_eq(expected_data.data, true)
            }
            AuditProbeEventParams::AssertTensorApproxEx { tolerance } => {
                let [actual_data, expected_data] =
                    unpack_audit_probe_event_data!([actual, expected], { data })?;

                tolerance.try_assert_tensor_data_approx_eq(
                    actual_data.data,
                    expected_data.data,
                    true,
                )
            }
        }
    }
    cmp(actual, expected).map_err(|err| {
        let header = actual.header();
        let params = actual.params();

        let mut msg = format!("AuditEvent::{name}:", name = params.variant_name());
        if let Some(label) = &header.label {
            msg.push_str(&format!(" \"{label}\""));
        }

        let ts_utc: OffsetDateTime = header.ts.into();
        // 2. Format using a macro-compiled description
        let format = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
        let formatted = ts_utc.format(&format).unwrap();
        msg.push_str(&format!(" @{formatted}\n"));

        if let Some(loc) = &header.loc {
            msg.push_str(&format!(" at {loc}\n"));
        }
        msg.push_str(&format!("{params:#?}"));
        msg.push_str(&format!("{err}"));

        BunsenError::AssertionError(msg)
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        burner::testing::audit::AuditProbeVecRecorder,
        errors::WithOkOrPanic,
        support::testing::{
            CpuBackend,
            PerformanceBackend,
            default_device,
        },
    };

    #[test]
    #[serial_test::serial]
    #[should_panic = "iota"]
    fn test_missmatch() {
        let mut recorder = AuditProbeVecRecorder::default();
        let probe = &mut AuditProbe::new(vec![&mut recorder]);
        {
            type B = PerformanceBackend;
            let device = default_device();

            let iota: Tensor<B, 1> = Tensor::arange(0..10, &device).float();
            probe.assert_eq_as::<f64>("iota", &iota).ok_or_panic();
        }

        let mut verifier = recorder.into_verifier();
        let probe = &mut AuditProbe::new(vec![&mut verifier]);
        {
            type B = CpuBackend;
            let device = default_device();

            let iota: Tensor<B, 1> = Tensor::arange(5..15, &device).float();
            probe.assert_eq_as::<f64>("iota", &iota).ok_or_panic();
        }
    }

    #[test]
    #[serial_test::serial]
    fn test_stream() -> BunsenResult<()> {
        fn example<B: Backend>(probe: &mut AuditProbe) -> BunsenResult<()> {
            let device = default_device();

            let iota: Tensor<B, 1> = Tensor::arange(0..10, &device).float();
            probe.assert_eq_as::<f64>("iota", &iota)?;
            probe.assert_eq_cast("iota", &iota, DType::F16)?;

            probe.assert_approx_eq_as::<f32>(
                "iota.exp",
                &iota.clone().exp(),
                TolerancePolicy::Balanced,
            )?;

            probe.assert_approx_eq_cast(
                "iota.sin",
                &iota.clone().sin(),
                TolerancePolicy::Balanced,
                FloatDType::F16,
            )?;

            Ok(())
        }

        let mut recorder = AuditProbeVecRecorder::default();
        example::<PerformanceBackend>(&mut AuditProbe::new(vec![&mut recorder]))?;

        let mut verifier = recorder.into_verifier();
        example::<CpuBackend>(&mut AuditProbe::new(vec![&mut verifier]))?;

        Ok(())
    }
}