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
//! Validated Classification Types - Compile-Time Contract Enforcement
//!
//! Poka-Yoke (mistake-proofing) types for classification fine-tuning.
//! Makes invalid classification states unrepresentable at the type level.
//!
//! # Contract
//!
//! See `contracts/classification-finetune-v1.yaml` for the full specification.
//!
//! # Compiler Guarantee
//!
//! It is IMPOSSIBLE to use invalid classification data because:
//! 1. Inner fields are private
//! 2. `new()` is the ONLY constructor (no Default, no unsafe backdoor)
//! 3. `new()` runs ALL validation checks from the contract
use super::validated_tensors::ContractValidationError;
use crate::text::shell_vocab::SafetyClass;
use std::fmt;
// =============================================================================
// VALIDATED CLASS LOGITS (F-CLASS-001)
// =============================================================================
/// Validated classification logits — private constructor enforces shape.
///
/// Guarantees:
/// - `data.len() == num_classes`
/// - `num_classes >= 2` (binary classification minimum)
/// - No NaN or Inf values
///
/// # Poka-Yoke
///
/// Inner `data` field is private. The only way to construct this type
/// is through `new()`, which enforces all invariants.
#[derive(Debug, Clone)]
pub struct ValidatedClassLogits {
data: Vec<f32>,
num_classes: usize,
}
impl ValidatedClassLogits {
/// Construct validated logits.
///
/// # Errors
///
/// Returns `ContractValidationError` if:
/// - `data.len() != num_classes` (F-CLASS-001)
/// - `num_classes < 2` (degenerate classifier)
/// - Any value is NaN or Inf
pub fn new(data: Vec<f32>, num_classes: usize) -> Result<Self, ContractValidationError> {
if num_classes < 2 {
return Err(ContractValidationError {
tensor_name: "class_logits".to_string(),
rule_id: "F-CLASS-001".to_string(),
message: format!("num_classes must be >= 2, got {num_classes}"),
});
}
if data.len() != num_classes {
return Err(ContractValidationError {
tensor_name: "class_logits".to_string(),
rule_id: "F-CLASS-001".to_string(),
message: format!(
"Logit shape mismatch: got {} elements, expected {num_classes}",
data.len()
),
});
}
for (i, &v) in data.iter().enumerate() {
if v.is_nan() {
return Err(ContractValidationError {
tensor_name: "class_logits".to_string(),
rule_id: "F-CLASS-001".to_string(),
message: format!("NaN at index {i}"),
});
}
if v.is_infinite() {
return Err(ContractValidationError {
tensor_name: "class_logits".to_string(),
rule_id: "F-CLASS-001".to_string(),
message: format!("Inf at index {i}"),
});
}
}
Ok(Self { data, num_classes })
}
/// Access the validated logit values.
#[must_use]
pub fn data(&self) -> &[f32] {
&self.data
}
/// Number of classes.
#[must_use]
pub fn num_classes(&self) -> usize {
self.num_classes
}
/// Compute softmax probabilities.
///
/// Contract: output sums to 1.0 (within epsilon=1e-5).
#[must_use]
pub fn softmax(&self) -> Vec<f32> {
let max_val = self.data.iter().copied().fold(f32::NEG_INFINITY, f32::max);
let exp_sum: f32 = self.data.iter().map(|&v| (v - max_val).exp()).sum();
self.data
.iter()
.map(|&v| (v - max_val).exp() / exp_sum)
.collect()
}
/// Return the predicted class index (argmax).
#[must_use]
pub fn predicted_class(&self) -> usize {
self.data
.iter()
.enumerate()
.max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
.map(|(i, _)| i)
.unwrap_or(0)
}
/// Return predicted class and confidence (softmax probability).
#[must_use]
pub fn predicted_class_with_confidence(&self) -> (usize, f32) {
let probs = self.softmax();
let (idx, &conf) = probs
.iter()
.enumerate()
.max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or((0, &0.0));
(idx, conf)
}
}
impl fmt::Display for ValidatedClassLogits {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (cls, conf) = self.predicted_class_with_confidence();
write!(
f,
"ClassLogits[{} classes, predicted={cls}, conf={conf:.1}%]",
self.num_classes
)
}
}
// =============================================================================
// VALIDATED SAFETY LABEL (F-CLASS-002)
// =============================================================================
/// Validated safety label — bounded to valid class indices.
///
/// Guarantees:
/// - `index < num_classes`
/// - Maps to a valid `SafetyClass` variant
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ValidatedSafetyLabel {
class: SafetyClass,
index: usize,
}
impl ValidatedSafetyLabel {
/// Construct a validated safety label.
///
/// # Errors
///
/// Returns `ContractValidationError` if:
/// - `index >= num_classes` (F-CLASS-002)
/// - `index` does not map to a valid `SafetyClass`
pub fn new(index: usize, num_classes: usize) -> Result<Self, ContractValidationError> {
if index >= num_classes {
return Err(ContractValidationError {
tensor_name: "safety_label".to_string(),
rule_id: "F-CLASS-002".to_string(),
message: format!("Label index {index} out of range (num_classes={num_classes})"),
});
}
let class = SafetyClass::from_index(index).ok_or_else(|| ContractValidationError {
tensor_name: "safety_label".to_string(),
rule_id: "F-CLASS-002".to_string(),
message: format!("Index {index} does not map to a SafetyClass variant"),
})?;
Ok(Self { class, index })
}
/// The safety class.
#[must_use]
pub fn class(&self) -> SafetyClass {
self.class
}
/// The class index (0-4).
#[must_use]
pub fn index(&self) -> usize {
self.index
}
/// Human-readable label.
#[must_use]
pub fn label(&self) -> &'static str {
self.class.label()
}
}
impl fmt::Display for ValidatedSafetyLabel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}({})", self.class.label(), self.index)
}
}
// =============================================================================
// VALIDATED CLASSIFIER WEIGHT (F-CLASS-004)
// =============================================================================
/// Validated classifier head weight — enforces shape invariant.
///
/// Guarantees:
/// - `data.len() == hidden_size * num_classes`
/// - `hidden_size > 0`
/// - `num_classes >= 2`
/// - No NaN or Inf values
#[derive(Debug, Clone)]
pub struct ValidatedClassifierWeight {
data: Vec<f32>,
hidden_size: usize,
num_classes: usize,
}
impl ValidatedClassifierWeight {
/// Construct validated classifier weight.
///
/// # Errors
///
/// Returns `ContractValidationError` if shape or data quality invariants violated.
pub fn new(
data: Vec<f32>,
hidden_size: usize,
num_classes: usize,
) -> Result<Self, ContractValidationError> {
if hidden_size == 0 {
return Err(ContractValidationError {
tensor_name: "classifier_weight".to_string(),
rule_id: "F-CLASS-004".to_string(),
message: "hidden_size must be > 0".to_string(),
});
}
if num_classes < 2 {
return Err(ContractValidationError {
tensor_name: "classifier_weight".to_string(),
rule_id: "F-CLASS-004".to_string(),
message: format!("num_classes must be >= 2, got {num_classes}"),
});
}
let expected = hidden_size * num_classes;
if data.len() != expected {
return Err(ContractValidationError {
tensor_name: "classifier_weight".to_string(),
rule_id: "F-CLASS-004".to_string(),
message: format!(
"Shape mismatch: got {} elements, expected {expected} ({hidden_size}x{num_classes})",
data.len()
),
});
}
for (i, &v) in data.iter().enumerate() {
if v.is_nan() {
return Err(ContractValidationError {
tensor_name: "classifier_weight".to_string(),
rule_id: "F-CLASS-004".to_string(),
message: format!("NaN at index {i}"),
});
}
if v.is_infinite() {
return Err(ContractValidationError {
tensor_name: "classifier_weight".to_string(),
rule_id: "F-CLASS-004".to_string(),
message: format!("Inf at index {i}"),
});
}
}
Ok(Self {
data,
hidden_size,
num_classes,
})
}
/// Access the validated weight data.
#[must_use]
pub fn data(&self) -> &[f32] {
&self.data
}
/// Mutable access for training (gradient updates).
pub fn data_mut(&mut self) -> &mut [f32] {
&mut self.data
}
/// Hidden dimension.
#[must_use]
pub fn hidden_size(&self) -> usize {
self.hidden_size
}
/// Number of output classes.
#[must_use]
pub fn num_classes(&self) -> usize {
self.num_classes
}
}
impl fmt::Display for ValidatedClassifierWeight {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"ClassifierWeight[{}x{}, {} elements]",
self.hidden_size,
self.num_classes,
self.data.len()
)
}
}