sim-lib-interference-compute 0.1.0

Normalized tile-local f32 Tensor lowering for coherent interference.
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
//! Fixed-tolerance differential reporting against the deterministic f64 oracle.

use std::{f64::consts::PI, fmt};

use sim_lib_interference_solve::HostPhasorField;

use crate::DenseF32Field;

/// A quantity checked by the portable f32 differential contract.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ConformanceMetric {
    /// Real Cartesian component.
    Real,
    /// Imaginary Cartesian component.
    Imaginary,
    /// Complex amplitude.
    Amplitude,
    /// Wrapped phase in radians, for cells above the amplitude floor.
    Phase,
    /// Squared complex magnitude.
    MagnitudeSquared,
}

/// Absolute and relative tolerances for one scalar quantity.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ScalarTolerance {
    /// Fixed absolute error allowance.
    pub absolute: f64,
    /// Fixed relative error allowance.
    pub relative: f64,
}

/// Published fixed tolerances for dense f32 interference conformance.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DifferentialTolerances {
    /// Real and imaginary component tolerance.
    pub component: ScalarTolerance,
    /// Complex amplitude tolerance.
    pub amplitude: ScalarTolerance,
    /// Wrapped phase tolerance in radians.
    pub phase: ScalarTolerance,
    /// Squared-magnitude tolerance.
    pub magnitude_squared: ScalarTolerance,
    /// Reference amplitude below which phase is undefined and not compared.
    pub phase_amplitude_floor: f64,
}

impl Default for DifferentialTolerances {
    fn default() -> Self {
        Self {
            component: ScalarTolerance {
                absolute: 2.0e-5,
                relative: 2.0e-4,
            },
            amplitude: ScalarTolerance {
                absolute: 2.0e-5,
                relative: 2.0e-4,
            },
            phase: ScalarTolerance {
                absolute: 3.0e-4,
                relative: 1.0e-4,
            },
            magnitude_squared: ScalarTolerance {
                absolute: 4.0e-5,
                relative: 4.0e-4,
            },
            phase_amplitude_floor: 1.0e-5,
        }
    }
}

/// Maximum observed error and its fixed tolerance at one cell.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DifferentialMaximum {
    /// Absolute error.
    pub error: f64,
    /// Absolute-plus-relative limit at this cell.
    pub limit: f64,
    /// Row containing the maximum.
    pub row: usize,
    /// Column containing the maximum.
    pub column: usize,
}

impl DifferentialMaximum {
    /// Returns whether the maximum satisfies its fixed tolerance.
    pub fn passed(self) -> bool {
        self.error <= self.limit
    }

    fn ratio(self) -> f64 {
        if self.limit == 0.0 {
            if self.error == 0.0 {
                0.0
            } else {
                f64::INFINITY
            }
        } else {
            self.error / self.limit
        }
    }
}

/// Complete comparison report shared by portable and accelerated providers.
#[derive(Clone, Debug, PartialEq)]
pub struct DifferentialReport {
    /// Maximum real-component error.
    pub real: DifferentialMaximum,
    /// Maximum imaginary-component error.
    pub imaginary: DifferentialMaximum,
    /// Maximum amplitude error.
    pub amplitude: DifferentialMaximum,
    /// Maximum wrapped-phase error, absent when every cell is below the floor.
    pub phase: Option<DifferentialMaximum>,
    /// Maximum squared-magnitude error.
    pub magnitude_squared: DifferentialMaximum,
    /// Number of cells whose phase was compared.
    pub phase_cells: usize,
    /// Metric with the largest error-to-limit ratio.
    pub worst_metric: ConformanceMetric,
    /// Row containing the worst normalized error.
    pub worst_row: usize,
    /// Column containing the worst normalized error.
    pub worst_column: usize,
}

impl DifferentialReport {
    /// Returns true when every compared quantity is within tolerance.
    pub fn passed(&self) -> bool {
        self.real.passed()
            && self.imaginary.passed()
            && self.amplitude.passed()
            && self.phase.is_none_or(DifferentialMaximum::passed)
            && self.magnitude_squared.passed()
    }

    /// Returns the largest absolute Cartesian-component error.
    pub fn max_component_absolute_error(&self) -> f64 {
        self.real.error.max(self.imaginary.error)
    }

    /// Returns the largest compared absolute wrapped-phase error.
    ///
    /// A field containing only below-floor cancellation cells returns zero
    /// because phase is deliberately undefined there.
    pub fn max_phase_absolute_error(&self) -> f64 {
        self.phase.map_or(0.0, |maximum| maximum.error)
    }
}

/// Structural or non-finite input rejected before differential reporting.
#[derive(Clone, Debug, PartialEq)]
pub struct DifferentialError {
    detail: String,
}

impl DifferentialError {
    fn new(detail: impl Into<String>) -> Self {
        Self {
            detail: detail.into(),
        }
    }

    /// Returns the stable diagnostic.
    pub fn detail(&self) -> &str {
        &self.detail
    }
}

impl fmt::Display for DifferentialError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.detail)
    }
}

impl std::error::Error for DifferentialError {}

/// Compares one dense f32 result with the deterministic f64 reference field.
pub fn compare_dense_to_reference(
    reference: &HostPhasorField,
    candidate: &DenseF32Field,
    tolerances: DifferentialTolerances,
) -> Result<DifferentialReport, DifferentialError> {
    compare_candidate(
        reference,
        CandidateField {
            name: "dense",
            rows: candidate.rows(),
            columns: candidate.columns(),
            real: CandidateComponent::F32(candidate.real()),
            imaginary: CandidateComponent::F32(candidate.imaginary()),
        },
        tolerances,
    )
}

/// Compares one materialized provider result with the deterministic f64 oracle.
///
/// Provider results use the same component, amplitude, phase-floor, wrapped
/// phase, and squared-magnitude reporting path as [`compare_dense_to_reference`].
/// The candidate is expected to be a materialized f32 Tensor field represented
/// by the runtime's host phasor container.
pub fn compare_materialized_to_reference(
    reference: &HostPhasorField,
    candidate: &HostPhasorField,
    tolerances: DifferentialTolerances,
) -> Result<DifferentialReport, DifferentialError> {
    compare_candidate(
        reference,
        CandidateField {
            name: "materialized",
            rows: candidate.rows(),
            columns: candidate.columns(),
            real: CandidateComponent::F64(candidate.real()),
            imaginary: CandidateComponent::F64(candidate.imaginary()),
        },
        tolerances,
    )
}

fn compare_candidate(
    reference: &HostPhasorField,
    candidate: CandidateField<'_>,
    tolerances: DifferentialTolerances,
) -> Result<DifferentialReport, DifferentialError> {
    validate_inputs(reference, candidate, tolerances)?;
    let mut maxima = Maxima::default();
    let columns = reference.columns();
    for index in 0..reference.len() {
        let row = index / columns;
        let column = index % columns;
        let rr = reference.real()[index];
        let ri = reference.imaginary()[index];
        let cr = candidate.real.get(index);
        let ci = candidate.imaginary.get(index);
        let reference_amplitude = rr.hypot(ri);
        let candidate_amplitude = cr.hypot(ci);
        maxima.observe(
            ConformanceMetric::Real,
            (rr - cr).abs(),
            limit(tolerances.component, rr, cr),
            row,
            column,
        );
        maxima.observe(
            ConformanceMetric::Imaginary,
            (ri - ci).abs(),
            limit(tolerances.component, ri, ci),
            row,
            column,
        );
        maxima.observe(
            ConformanceMetric::Amplitude,
            (reference_amplitude - candidate_amplitude).abs(),
            limit(
                tolerances.amplitude,
                reference_amplitude,
                candidate_amplitude,
            ),
            row,
            column,
        );
        if reference_amplitude >= tolerances.phase_amplitude_floor {
            maxima.phase_cells += 1;
            let reference_phase = ri.atan2(rr);
            let candidate_phase = ci.atan2(cr);
            maxima.observe(
                ConformanceMetric::Phase,
                wrapped_phase_error(reference_phase, candidate_phase),
                limit(tolerances.phase, reference_phase, candidate_phase),
                row,
                column,
            );
        }
        let reference_squared = rr.mul_add(rr, ri * ri);
        let candidate_squared = cr.mul_add(cr, ci * ci);
        maxima.observe(
            ConformanceMetric::MagnitudeSquared,
            (reference_squared - candidate_squared).abs(),
            limit(
                tolerances.magnitude_squared,
                reference_squared,
                candidate_squared,
            ),
            row,
            column,
        );
    }
    Ok(maxima.finish())
}

fn validate_inputs(
    reference: &HostPhasorField,
    candidate: CandidateField<'_>,
    tolerances: DifferentialTolerances,
) -> Result<(), DifferentialError> {
    if reference.rows() != candidate.rows || reference.columns() != candidate.columns {
        return Err(DifferentialError::new(format!(
            "reference shape [{}, {}] differs from {} shape [{}, {}]",
            reference.rows(),
            reference.columns(),
            candidate.name,
            candidate.rows,
            candidate.columns
        )));
    }
    if candidate.real.len() != reference.len() || candidate.imaginary.len() != reference.len() {
        return Err(DifferentialError::new(format!(
            "{} component lengths [{}, {}] differ from reference length {}",
            candidate.name,
            candidate.real.len(),
            candidate.imaginary.len(),
            reference.len()
        )));
    }
    for (name, tolerance) in [
        ("component", tolerances.component),
        ("amplitude", tolerances.amplitude),
        ("phase", tolerances.phase),
        ("magnitude-squared", tolerances.magnitude_squared),
    ] {
        if !tolerance.absolute.is_finite()
            || tolerance.absolute < 0.0
            || !tolerance.relative.is_finite()
            || tolerance.relative < 0.0
        {
            return Err(DifferentialError::new(format!(
                "{name} tolerances must be finite and non-negative"
            )));
        }
    }
    if !tolerances.phase_amplitude_floor.is_finite() || tolerances.phase_amplitude_floor <= 0.0 {
        return Err(DifferentialError::new(
            "phase amplitude floor must be finite and positive",
        ));
    }
    for (name, values) in [
        ("reference real", reference.real()),
        ("reference imaginary", reference.imaginary()),
    ] {
        if let Some((index, value)) = values
            .iter()
            .copied()
            .enumerate()
            .find(|(_, value)| !value.is_finite())
        {
            return Err(DifferentialError::new(format!(
                "{name} cell {index} is non-finite: {value}"
            )));
        }
    }
    for (component, values) in [("real", candidate.real), ("imaginary", candidate.imaginary)] {
        if let Some((index, value)) = values.first_non_finite() {
            return Err(DifferentialError::new(format!(
                "{} {component} cell {index} is non-finite: {value}",
                candidate.name
            )));
        }
    }
    Ok(())
}

#[derive(Clone, Copy)]
enum CandidateComponent<'a> {
    F32(&'a [f32]),
    F64(&'a [f64]),
}

impl CandidateComponent<'_> {
    fn len(self) -> usize {
        match self {
            Self::F32(values) => values.len(),
            Self::F64(values) => values.len(),
        }
    }

    fn get(self, index: usize) -> f64 {
        match self {
            Self::F32(values) => f64::from(values[index]),
            Self::F64(values) => values[index],
        }
    }

    fn first_non_finite(self) -> Option<(usize, f64)> {
        (0..self.len())
            .map(|index| (index, self.get(index)))
            .find(|(_, value)| !value.is_finite())
    }
}

#[derive(Clone, Copy)]
struct CandidateField<'a> {
    name: &'static str,
    rows: usize,
    columns: usize,
    real: CandidateComponent<'a>,
    imaginary: CandidateComponent<'a>,
}

fn limit(tolerance: ScalarTolerance, reference: f64, candidate: f64) -> f64 {
    tolerance.absolute + tolerance.relative * reference.abs().max(candidate.abs())
}

fn wrapped_phase_error(left: f64, right: f64) -> f64 {
    let difference = (left - right).abs().rem_euclid(2.0 * PI);
    difference.min(2.0 * PI - difference)
}

#[derive(Clone, Copy, Debug)]
struct ObservedMaximum {
    value: DifferentialMaximum,
    initialized: bool,
}

impl Default for ObservedMaximum {
    fn default() -> Self {
        Self {
            value: DifferentialMaximum {
                error: 0.0,
                limit: 0.0,
                row: 0,
                column: 0,
            },
            initialized: false,
        }
    }
}

impl ObservedMaximum {
    fn observe(&mut self, error: f64, limit: f64, row: usize, column: usize) {
        let candidate = DifferentialMaximum {
            error,
            limit,
            row,
            column,
        };
        if !self.initialized
            || candidate.error > self.value.error
            || (candidate.error == self.value.error && candidate.ratio() > self.value.ratio())
        {
            self.value = candidate;
            self.initialized = true;
        }
    }
}

#[derive(Default)]
struct Maxima {
    real: ObservedMaximum,
    imaginary: ObservedMaximum,
    amplitude: ObservedMaximum,
    phase: ObservedMaximum,
    magnitude_squared: ObservedMaximum,
    phase_cells: usize,
    worst: Option<(ConformanceMetric, DifferentialMaximum)>,
}

impl Maxima {
    fn observe(
        &mut self,
        metric: ConformanceMetric,
        error: f64,
        limit: f64,
        row: usize,
        column: usize,
    ) {
        let candidate = DifferentialMaximum {
            error,
            limit,
            row,
            column,
        };
        match metric {
            ConformanceMetric::Real => self.real.observe(error, limit, row, column),
            ConformanceMetric::Imaginary => self.imaginary.observe(error, limit, row, column),
            ConformanceMetric::Amplitude => self.amplitude.observe(error, limit, row, column),
            ConformanceMetric::Phase => self.phase.observe(error, limit, row, column),
            ConformanceMetric::MagnitudeSquared => {
                self.magnitude_squared.observe(error, limit, row, column);
            }
        }
        if self
            .worst
            .is_none_or(|(_, current)| candidate.ratio() > current.ratio())
        {
            self.worst = Some((metric, candidate));
        }
    }

    fn finish(self) -> DifferentialReport {
        let phase = (self.phase_cells != 0).then_some(self.phase.value);
        let (worst_metric, worst) = self
            .worst
            .expect("a valid interference field always has at least one cell");
        DifferentialReport {
            real: self.real.value,
            imaginary: self.imaginary.value,
            amplitude: self.amplitude.value,
            phase,
            magnitude_squared: self.magnitude_squared.value,
            phase_cells: self.phase_cells,
            worst_metric,
            worst_row: worst.row,
            worst_column: worst.column,
        }
    }
}