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
// =========================================================================
// FALSIFY-LF: loss-functions-v1.yaml contract (aprender MSELoss, L1Loss)
//
// Five-Whys (PMAT-354):
// Why 1: aprender had no inline FALSIFY-LF-* tests for loss functions
// Why 2: loss tests exist but lack contract-mapped FALSIFY naming
// Why 3: no mapping from loss-functions-v1.yaml to inline test names
// Why 4: aprender predates the inline FALSIFY convention
// Why 5: MSE/L1 were "obviously correct" (textbook formulas)
//
// References:
// - provable-contracts/contracts/loss-functions-v1.yaml
// =========================================================================
#[cfg(test)]
mod tests {
use super::super::*;
/// OBLIG-BCE-POSWEIGHT-PYTORCH-PARITY (PMAT-915).
///
/// PyTorch `binary_cross_entropy_with_logits` applies `pos_weight` ONLY to the
/// positive (log σ(x)) term, not the whole loss:
/// log_weight = 1 + (w - 1) * y
/// loss = (1 - y) * x + log_weight * (log(1 + exp(-|x|)) + max(-x, 0))
///
/// The previous aprender impl scaled the WHOLE loss by `y*(w-1)+1`, which only
/// coincides with PyTorch for hard targets y ∈ {0,1}. With SOFT targets the two
/// diverge — this falsifier uses soft targets so the bug is observable.
///
/// Reference (torch 2.x, CPU):
/// logits = [0.5, -1.2, 2.0, 0.1], y = [0.7, 0.2, 0.9, 0.4], pos_weight = 3.0
/// F.binary_cross_entropy_with_logits(..., reduction="mean") == 1.0379231
#[test]
#[allow(clippy::float_cmp)]
fn falsify_lf_007_bce_pos_weight_pytorch_parity() {
let logits = Tensor::from_slice(&[0.5, -1.2, 2.0, 0.1]);
let soft = Tensor::from_slice(&[0.7, 0.2, 0.9, 0.4]);
// Mean reduction (with_pos_weight forces Mean).
let mean = BCEWithLogitsLoss::with_pos_weight(3.0)
.forward(&logits, &soft)
.data()[0];
let torch_mean = 1.037_923_1_f32;
assert!(
(mean - torch_mean).abs() < 1e-5,
"FALSIFIED OBLIG-BCE-POSWEIGHT-PYTORCH-PARITY (mean): apr={mean} torch={torch_mean}"
);
// Hard targets must STILL match PyTorch after the fix (regression guard:
// the new positive-term weighting reduces to the old behaviour for y ∈ {0,1}).
// torch(pos_weight=3, hard, mean) = 0.7026735
let hard = Tensor::from_slice(&[1.0, 0.0, 1.0, 0.0]);
let hard_mean = BCEWithLogitsLoss::with_pos_weight(3.0)
.forward(&logits, &hard)
.data()[0];
let torch_hard_mean = 0.702_673_5_f32;
assert!(
(hard_mean - torch_hard_mean).abs() < 1e-5,
"FALSIFIED OBLIG-BCE-POSWEIGHT-PYTORCH-PARITY (hard): apr={hard_mean} torch={torch_hard_mean}"
);
}
/// FALSIFY-LF-001: MSE is non-negative
#[test]
fn falsify_lf_001_mse_non_negative() {
let pred = Tensor::new(&[1.0_f32, 2.0, 3.0], &[3]);
let target = Tensor::new(&[1.5, 2.5, 3.5], &[3]);
let criterion = MSELoss::new();
let loss = criterion.forward(&pred, &target);
assert!(
loss.data()[0] >= 0.0,
"FALSIFIED LF-001: MSE loss = {} < 0",
loss.data()[0]
);
}
/// FALSIFY-LF-002: MSE = 0 when pred == target
#[test]
fn falsify_lf_002_mse_zero_on_match() {
let pred = Tensor::new(&[1.0_f32, 2.0, 3.0], &[3]);
let target = Tensor::new(&[1.0, 2.0, 3.0], &[3]);
let criterion = MSELoss::new();
let loss = criterion.forward(&pred, &target);
assert!(
loss.data()[0].abs() < 1e-6,
"FALSIFIED LF-002: MSE = {} for identical pred/target",
loss.data()[0]
);
}
/// FALSIFY-LF-003: L1 is non-negative
#[test]
fn falsify_lf_003_l1_non_negative() {
let pred = Tensor::new(&[1.0_f32, 2.0, 3.0], &[3]);
let target = Tensor::new(&[1.5, 2.5, 3.5], &[3]);
let criterion = L1Loss::new();
let loss = criterion.forward(&pred, &target);
assert!(
loss.data()[0] >= 0.0,
"FALSIFIED LF-003: L1 loss = {} < 0",
loss.data()[0]
);
}
/// FALSIFY-LF-004: MSE(a, b) == MSE(b, a) (symmetric)
#[test]
fn falsify_lf_004_mse_symmetric() {
let a = Tensor::new(&[1.0_f32, 3.0, 5.0], &[3]);
let b = Tensor::new(&[2.0, 4.0, 6.0], &[3]);
let criterion = MSELoss::new();
let loss_ab = criterion.forward(&a, &b);
let loss_ba = criterion.forward(&b, &a);
assert!(
(loss_ab.data()[0] - loss_ba.data()[0]).abs() < 1e-6,
"FALSIFIED LF-004: MSE(a,b)={} != MSE(b,a)={}",
loss_ab.data()[0],
loss_ba.data()[0]
);
}
/// FALSIFY-LF-005: L1(a, b) == L1(b, a) (symmetric)
#[test]
fn falsify_lf_005_l1_symmetric() {
let a = Tensor::new(&[1.0_f32, 3.0, -2.0], &[3]);
let b = Tensor::new(&[4.0, -1.0, 6.0], &[3]);
let criterion = L1Loss::new();
let loss_ab = criterion.forward(&a, &b);
let loss_ba = criterion.forward(&b, &a);
assert!(
(loss_ab.data()[0] - loss_ba.data()[0]).abs() < 1e-6,
"FALSIFIED LF-005: L1(a,b)={} != L1(b,a)={}",
loss_ab.data()[0],
loss_ba.data()[0]
);
}
mod lf_proptest_falsify {
use super::super::super::*;
use proptest::prelude::*;
// FALSIFY-LF-001-prop: MSE is non-negative for random inputs
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
#[test]
fn falsify_lf_001_prop_mse_non_negative(
seed in 0..1000u32,
n in 2..=16usize,
) {
let pred_data: Vec<f32> = (0..n)
.map(|i| ((i as f32 + seed as f32) * 0.37).sin() * 10.0)
.collect();
let target_data: Vec<f32> = (0..n)
.map(|i| ((i as f32 + seed as f32) * 0.73).cos() * 10.0)
.collect();
let pred = Tensor::new(&pred_data, &[n]);
let target = Tensor::new(&target_data, &[n]);
let loss = MSELoss::new().forward(&pred, &target);
prop_assert!(
loss.data()[0] >= 0.0,
"FALSIFIED LF-001-prop: MSE = {} < 0",
loss.data()[0]
);
}
}
// FALSIFY-LF-005-prop: L1 symmetry for random inputs
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
#[test]
fn falsify_lf_005_prop_l1_symmetric(
seed in 0..1000u32,
n in 2..=16usize,
) {
let a_data: Vec<f32> = (0..n)
.map(|i| ((i as f32 + seed as f32) * 0.37).sin() * 10.0)
.collect();
let b_data: Vec<f32> = (0..n)
.map(|i| ((i as f32 + seed as f32) * 0.73).cos() * 10.0)
.collect();
let a = Tensor::new(&a_data, &[n]);
let b = Tensor::new(&b_data, &[n]);
let loss_ab = L1Loss::new().forward(&a, &b);
let loss_ba = L1Loss::new().forward(&b, &a);
prop_assert!(
(loss_ab.data()[0] - loss_ba.data()[0]).abs() < 1e-5,
"FALSIFIED LF-005-prop: L1(a,b)={} != L1(b,a)={}",
loss_ab.data()[0], loss_ba.data()[0]
);
}
}
// FALSIFY-LF-002-prop: MSE = 0 when pred == target
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
#[test]
fn falsify_lf_002_prop_mse_zero_on_match(
seed in 0..1000u32,
n in 2..=16usize,
) {
let data: Vec<f32> = (0..n)
.map(|i| ((i as f32 + seed as f32) * 0.37).sin() * 10.0)
.collect();
let pred = Tensor::new(&data, &[n]);
let target = Tensor::new(&data, &[n]);
let loss = MSELoss::new().forward(&pred, &target);
prop_assert!(
loss.data()[0].abs() < 1e-5,
"FALSIFIED LF-002-prop: MSE(x,x) = {} != 0",
loss.data()[0]
);
}
}
}
}