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
// RLX — versatile ML compiler + runtime.
// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! NaN/inf check epilogue (plan #18).
//!
//! Borrowed from MAX's `Mogg/MOGGKernelAPI/nan_check.mojo` pattern.
//! When the `nan-check` Cargo feature is on, [`scan`] reports the
//! first NaN or inf in a slice — useful as a debug epilogue on
//! every output buffer to localize precision blow-ups to the op
//! that introduced them.
//!
//! Always present in the API surface so callers can compile against
//! it; the feature flag controls whether it's a real scan or a
//! no-op (returns `Ok(())` immediately).
/// What was found in a buffer that fails the check.
#[derive(Debug, Clone, Copy)]
pub enum BadValue {
Nan,
PosInf,
NegInf,
}
#[derive(Debug)]
pub struct NanCheckError {
pub kind: BadValue,
pub index: usize,
pub label: String,
}
impl std::fmt::Display for NanCheckError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let what = match self.kind {
BadValue::Nan => "NaN",
BadValue::PosInf => "+inf",
BadValue::NegInf => "-inf",
};
write!(f, "{} at index {} of `{}`", what, self.index, self.label)
}
}
impl std::error::Error for NanCheckError {}
/// Scan `data` for the first NaN or infinity. With the `nan-check`
/// feature OFF, returns `Ok(())` immediately (the optimizer
/// eliminates the call). With it ON, walks the slice — the cost is
/// O(n) but only paid when a caller opts in.
#[cfg(feature = "nan-check")]
#[inline(always)]
pub fn scan(label: &str, data: &[f32]) -> Result<(), NanCheckError> {
for (i, &v) in data.iter().enumerate() {
if v.is_nan() {
return Err(NanCheckError {
kind: BadValue::Nan,
index: i,
label: label.to_string(),
});
}
if v.is_infinite() {
let kind = if v > 0.0 {
BadValue::PosInf
} else {
BadValue::NegInf
};
return Err(NanCheckError {
kind,
index: i,
label: label.to_string(),
});
}
}
Ok(())
}
#[cfg(not(feature = "nan-check"))]
#[inline(always)]
pub fn scan(_label: &str, _data: &[f32]) -> Result<(), NanCheckError> {
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clean_data_passes() {
let data = [1.0, 2.0, -3.5, 0.0];
assert!(scan("clean", &data).is_ok());
}
#[cfg(feature = "nan-check")]
#[test]
fn detects_nan() {
let data = [1.0, f32::NAN, 3.0];
let err = scan("nan", &data).unwrap_err();
assert!(matches!(err.kind, BadValue::Nan));
assert_eq!(err.index, 1);
}
#[cfg(feature = "nan-check")]
#[test]
fn detects_pos_inf() {
let data = [f32::INFINITY, 0.0];
let err = scan("inf", &data).unwrap_err();
assert!(matches!(err.kind, BadValue::PosInf));
assert_eq!(err.index, 0);
}
}