aprender-core 0.29.2

Next-generation machine learning library in pure Rust
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
//! Model Family YAML Contract Loader (PMAT-242)
//!
//! Parses model family YAML files at runtime without external dependencies.
//! This is the runtime fallback; build.rs codegen (PMAT-250) is the preferred path.
//!
//! # Contract
//!
//! See `contracts/model-families/*.yaml` and
//! `docs/specifications/compiler-enforced-model-types-model-oracle.md` ยง4

use std::collections::HashMap;
use std::path::Path;

use crate::error::{AprenderError, Result};
use crate::format::model_family::{
    Activation, AttentionType, CertificationConfig, ChatTemplateConfig, DynModelFamily,
    FamilyRegistry, GgufFusionRule, GgufTensorTemplate, MlpType, ModelConstraints,
    ModelFamilyConfig, ModelSizeConfig, NormType, PositionalEncoding, ShapeTemplate,
    TensorTemplate,
};

// ============================================================================
// Minimal YAML Parser
// ============================================================================

/// A simple YAML value representation
#[derive(Debug, Clone)]
enum YamlValue {
    String(String),
    Int(i64),
    Float(f64),
    Bool(bool),
    Null,
    Sequence(Vec<YamlValue>),
    Mapping(Vec<(String, YamlValue)>),
}

impl YamlValue {
    fn as_str(&self) -> Option<&str> {
        match self {
            Self::String(s) => Some(s),
            _ => None,
        }
    }

    fn as_i64(&self) -> Option<i64> {
        match self {
            Self::Int(n) => Some(*n),
            Self::Float(f) => Some(*f as i64),
            _ => None,
        }
    }

    fn as_f64(&self) -> Option<f64> {
        match self {
            Self::Float(f) => Some(*f),
            Self::Int(n) => Some(*n as f64),
            _ => None,
        }
    }

    fn as_bool(&self) -> Option<bool> {
        match self {
            Self::Bool(b) => Some(*b),
            _ => None,
        }
    }

    fn as_usize(&self) -> Option<usize> {
        self.as_i64().and_then(|n| usize::try_from(n).ok())
    }

    fn as_sequence(&self) -> Option<&[YamlValue]> {
        match self {
            Self::Sequence(s) => Some(s),
            _ => None,
        }
    }

    #[cfg(test)]
    fn as_mapping(&self) -> Option<&[(String, YamlValue)]> {
        match self {
            Self::Mapping(m) => Some(m),
            _ => None,
        }
    }

    fn get(&self, key: &str) -> Option<&YamlValue> {
        match self {
            Self::Mapping(m) => m.iter().find(|(k, _)| k == key).map(|(_, v)| v),
            _ => None,
        }
    }

    fn get_str(&self, key: &str) -> Option<&str> {
        self.get(key)?.as_str()
    }

    fn get_usize(&self, key: &str) -> Option<usize> {
        self.get(key)?.as_usize()
    }

    fn get_f64(&self, key: &str) -> Option<f64> {
        self.get(key)?.as_f64()
    }

    fn get_bool(&self, key: &str) -> Option<bool> {
        self.get(key)?.as_bool()
    }
}

/// Parse a YAML string into a `YamlValue`.
/// Handles the subset of YAML used in model family contracts:
/// - Top-level mapping
/// - Nested mappings (indentation-based)
/// - Sequences (- item)
/// - Scalars: strings, ints, floats, bools, null
fn parse_yaml(input: &str) -> Result<YamlValue> {
    let lines: Vec<&str> = input.lines().collect();
    let (val, _) = parse_mapping(&lines, 0, 0)?;
    Ok(val)
}

/// Skip blank lines and comments, returning the index of the next content line.
fn skip_to_content(lines: &[&str], start: usize) -> usize {
    let mut i = start;
    while i < lines.len() {
        let trimmed = lines[i].trim();
        if !trimmed.is_empty() && !trimmed.starts_with('#') {
            break;
        }
        i += 1;
    }
    i
}

/// Parse a nested YAML value (sequence, mapping, or null) after an empty colon.
fn parse_nested_value(
    lines: &[&str],
    start: usize,
    parent_indent: usize,
) -> Result<(YamlValue, usize)> {
    let next_i = skip_to_content(lines, start);

    if next_i >= lines.len() {
        return Ok((YamlValue::Null, next_i));
    }

    let next_line = lines[next_i];
    let next_indent = next_line.len() - next_line.trim_start().len();
    let next_trimmed = next_line.trim();

    if next_trimmed.starts_with("- ") {
        parse_sequence(lines, next_i, next_indent)
    } else if next_indent > parent_indent {
        parse_mapping(lines, next_i, next_indent)
    } else {
        Ok((YamlValue::Null, next_i))
    }
}

/// Classify a YAML line's relationship to the current mapping scope.
enum MappingLineAction {
    /// Skip blank or comment lines.
    Skip,
    /// Line is dedented and entries exist: the mapping is complete.
    EndMapping,
    /// Line is dedented but no entries yet: skip it.
    SkipDedented,
    /// Line is over-indented and entries exist: the mapping is complete.
    EndOverindent,
    /// Line is at the correct indent level: parse it as a key-value entry.
    ParseEntry,
}

fn classify_mapping_line(
    trimmed: &str,
    line_indent: usize,
    indent: usize,
    has_entries: bool,
) -> MappingLineAction {
    if trimmed.is_empty() || trimmed.starts_with('#') {
        return MappingLineAction::Skip;
    }
    if line_indent < indent && has_entries {
        return MappingLineAction::EndMapping;
    }
    if line_indent < indent {
        return MappingLineAction::SkipDedented;
    }
    if line_indent > indent && has_entries {
        return MappingLineAction::EndOverindent;
    }
    MappingLineAction::ParseEntry
}

/// Parse a single key-value entry from a YAML mapping line.
/// Returns the parsed entry and the next line index.
fn parse_mapping_entry(
    lines: &[&str],
    trimmed: &str,
    current_idx: usize,
    indent: usize,
) -> Result<Option<((String, YamlValue), usize)>> {
    let Some(colon_pos) = trimmed.find(':') else {
        return Ok(None);
    };
    let key = trimmed[..colon_pos].trim().to_string();
    let after_colon = trimmed[colon_pos + 1..].trim();

    if after_colon.is_empty() {
        let (value, new_i) = parse_nested_value(lines, current_idx + 1, indent)?;
        Ok(Some(((key, value), new_i)))
    } else {
        let value = parse_scalar(after_colon);
        Ok(Some(((key, value), current_idx + 1)))
    }
}

fn parse_mapping(lines: &[&str], start: usize, indent: usize) -> Result<(YamlValue, usize)> {
    let mut entries = Vec::new();
    let mut i = start;

    while i < lines.len() {
        let line = lines[i];
        let trimmed = line.trim();
        let line_indent = line.len() - line.trim_start().len();

        match classify_mapping_line(trimmed, line_indent, indent, !entries.is_empty()) {
            MappingLineAction::Skip | MappingLineAction::SkipDedented => {
                i += 1;
            }
            MappingLineAction::EndMapping | MappingLineAction::EndOverindent => {
                return Ok((YamlValue::Mapping(entries), i));
            }
            MappingLineAction::ParseEntry => {
                if let Some((entry, new_i)) = parse_mapping_entry(lines, trimmed, i, indent)? {
                    entries.push(entry);
                    i = new_i;
                } else {
                    i += 1;
                }
            }
        }
    }

    Ok((YamlValue::Mapping(entries), i))
}

fn parse_sequence(lines: &[&str], start: usize, indent: usize) -> Result<(YamlValue, usize)> {
    let mut items = Vec::new();
    let mut i = start;

    while i < lines.len() {
        let line = lines[i];
        let trimmed = line.trim();

        if trimmed.is_empty() || trimmed.starts_with('#') {
            i += 1;
            continue;
        }

        let line_indent = line.len() - line.trim_start().len();
        if line_indent < indent {
            break;
        }

        if let Some(stripped) = trimmed.strip_prefix("- ") {
            let item_str = stripped.trim();
            items.push(parse_scalar(item_str));
            i += 1;
        } else {
            break;
        }
    }

    Ok((YamlValue::Sequence(items), i))
}

fn parse_scalar(s: &str) -> YamlValue {
    let s = s.trim();

    // Handle quoted strings (preserve inline comments inside quotes)
    if (s.starts_with('"') && s.ends_with('"')) || (s.starts_with('\'') && s.ends_with('\'')) {
        return YamlValue::String(s[1..s.len() - 1].to_string());
    }

    // Strip inline comments from unquoted values (YAML spec: # preceded by space)
    let s = if let Some(pos) = s.find(" #") {
        s[..pos].trim()
    } else {
        s
    };

    // Handle null
    if s == "null" || s == "~" {
        return YamlValue::Null;
    }

    // Handle booleans
    match s.to_lowercase().as_str() {
        "true" | "yes" => return YamlValue::Bool(true),
        "false" | "no" => return YamlValue::Bool(false),
        _ => {}
    }

    // Handle integers
    if let Ok(n) = s.parse::<i64>() {
        return YamlValue::Int(n);
    }

    // Handle floats
    if let Ok(f) = s.parse::<f64>() {
        return YamlValue::Float(f);
    }

    // Default to string
    YamlValue::String(s.to_string())
}

// ============================================================================
// YAML to ModelFamilyConfig conversion
// ============================================================================

/// Load a `ModelFamilyConfig` from a YAML file path.
///
/// # Errors
///
/// Returns `AprenderError::FormatError` if the file cannot be read or
/// contains invalid YAML for a model family contract.
pub fn load_family_yaml(path: &Path) -> Result<ModelFamilyConfig> {
    let content = std::fs::read_to_string(path).map_err(|e| AprenderError::FormatError {
        message: format!("Failed to read YAML file {}: {e}", path.display()),
    })?;
    parse_family_yaml(&content, path)
}

/// Parse a YAML string into a `ModelFamilyConfig`.
///
/// # Errors
///
/// Returns `AprenderError::FormatError` if the YAML content is missing
/// required fields or contains invalid values.
pub fn parse_family_yaml(content: &str, source: &Path) -> Result<ModelFamilyConfig> {
    let yaml = parse_yaml(content)?;
    yaml_to_config(&yaml, source)
}

fn yaml_to_config(yaml: &YamlValue, source: &Path) -> Result<ModelFamilyConfig> {
    let err = |msg: &str| -> AprenderError {
        AprenderError::FormatError {
            message: format!("{}: {msg}", source.display()),
        }
    };

    let family = yaml
        .get_str("family")
        .ok_or_else(|| err("missing required field: family"))?
        .to_string();
    let display_name = yaml
        .get_str("display_name")
        .ok_or_else(|| err("missing required field: display_name"))?
        .to_string();
    let vendor = yaml
        .get_str("vendor")
        .ok_or_else(|| err("missing required field: vendor"))?
        .to_string();
    let hf_pattern = yaml
        .get_str("hf_pattern")
        .ok_or_else(|| err("missing required field: hf_pattern"))?
        .to_string();

    // Parse architectures sequence
    let architectures = yaml
        .get("architectures")
        .and_then(YamlValue::as_sequence)
        .ok_or_else(|| err("missing required field: architectures"))?
        .iter()
        .filter_map(YamlValue::as_str)
        .map(String::from)
        .collect();

    // Parse size_variants
    let size_variants_yaml = yaml
        .get("size_variants")
        .ok_or_else(|| err("missing required field: size_variants"))?;

    let mut size_variants = HashMap::new();
    if let YamlValue::Mapping(entries) = size_variants_yaml {
        for (name, variant) in entries {
            let config = parse_size_config(variant, &family, name)?;
            size_variants.insert(name.clone(), config);
        }
    } else {
        return Err(err("size_variants must be a mapping"));
    }

    // Parse constraints
    let constraints_yaml = yaml
        .get("constraints")
        .ok_or_else(|| err("missing required field: constraints"))?;
    let constraints = parse_constraints(constraints_yaml)?;

    // Parse tensor_template
    let template_yaml = yaml
        .get("tensor_template")
        .ok_or_else(|| err("missing required field: tensor_template"))?;
    let tensor_template = parse_tensor_template(template_yaml)?;

    // GH-277: Parse gguf_tensor_template (optional)
    let gguf_tensor_template = yaml
        .get("gguf_tensor_template")
        .map(parse_gguf_tensor_template)
        .unwrap_or_default();

    // Parse shape_template
    let shape_yaml = yaml.get("shape_template");
    let shape_template = if let Some(sy) = shape_yaml {
        parse_shape_template(sy)
    } else {
        ShapeTemplate {
            shapes: HashMap::new(),
        }
    };

    // Parse quantizations
    let quantizations = yaml
        .get("quantizations")
        .and_then(YamlValue::as_sequence)
        .map(|seq| {
            seq.iter()
                .filter_map(YamlValue::as_str)
                .map(String::from)
                .collect()
        })
        .unwrap_or_default();

    // Parse chat_template (optional)
    let chat_template = yaml
        .get("chat_template")
        .and_then(|ct| parse_chat_template(ct).ok());

    // Parse certification (optional)
    let certification = yaml
        .get("certification")
        .and_then(|c| parse_certification(c).ok());

    Ok(ModelFamilyConfig {
        family,
        display_name,
        vendor,
        architectures,
        hf_pattern,
        size_variants,
        constraints,
        tensor_template,
        gguf_tensor_template,
        shape_template,
        quantizations,
        chat_template,
        certification,
    })
}

include!("parsing.rs");
include!("model_family_loader_tests.rs");
include!("model_family_contract_falsify.rs");