aprender-core 0.31.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! Per-Layer Merge Granularity for Model Merging (GH-452)
//!
//! Provides fine-grained control over how individual layers are merged
//! when combining multiple models. Supports per-layer strategy overrides
//! via a YAML-like configuration format.
//!
//! # References
//!
//! - [Wortsman et al. 2022] "Model soups: averaging weights of multiple
//!   fine-tuned models improves accuracy without increasing inference time"
//! - [Yadav et al. 2023] "TIES-Merging: Resolving Interference When Merging
//!   Models"
//!
//! # Toyota Way Principles
//!
//! - **Standardization**: Declarative YAML config for reproducible merges
//! - **Poka-Yoke**: Validation prevents misconfigured merges before execution
//! - **Heijunka**: Per-layer weights enable load-leveled contribution

use std::collections::HashMap;

use crate::error::{AprenderError, Result};

/// Valid merge strategy names
const VALID_STRATEGIES: &[&str] = &[
    "average",
    "weighted_average",
    "slerp",
    "ties",
    "dare",
    "passthrough",
];

/// Configuration for per-layer merge granularity
///
/// Allows different merge strategies and weights for different layer
/// patterns, enabling fine-grained control over model combination.
///
/// # Example
///
/// ```rust,ignore
/// use aprender::online::per_layer_merge::LayerMergeConfig;
///
/// let config = LayerMergeConfig {
///     layer_rules: vec![
///         LayerRule {
///             layer_pattern: "attn".to_string(),
///             strategy: "slerp".to_string(),
///             weights: Some(vec![0.7, 0.3]),
///             scale: None,
///         },
///     ],
///     default_strategy: "average".to_string(),
///     default_weights: vec![0.5, 0.5],
/// };
/// ```
#[derive(Debug, Clone)]
pub struct LayerMergeConfig {
    /// Ordered list of layer rules (first match wins)
    pub layer_rules: Vec<LayerRule>,
    /// Fallback strategy when no rule matches
    pub default_strategy: String,
    /// Default weights for models when no rule-specific weights exist
    pub default_weights: Vec<f64>,
}

/// A single rule mapping a layer name pattern to a merge strategy
///
/// Patterns use simple substring matching with support for `*` wildcards.
/// For example, `"layers.0."` matches any tensor name containing that
/// substring, while `"attn*weight"` matches names containing "attn"
/// followed (possibly later) by "weight".
#[derive(Debug, Clone)]
pub struct LayerRule {
    /// Pattern to match against tensor names (substring or wildcard `*`)
    pub layer_pattern: String,
    /// Merge strategy for matched tensors
    pub strategy: String,
    /// Optional per-model weights (overrides default_weights)
    pub weights: Option<Vec<f64>>,
    /// Optional scaling factor applied after merge
    pub scale: Option<f64>,
}

/// Top-level YAML merge configuration
///
/// Parsed from a YAML-like config file specifying models to merge,
/// output path, default strategy, and optional per-layer rules.
#[derive(Debug, Clone)]
pub struct MergeYamlConfig {
    /// Source models to merge
    pub models: Vec<ModelSource>,
    /// Output path for merged model
    pub output: String,
    /// Default merge strategy
    pub default_strategy: String,
    /// Optional per-layer rules
    pub layers: Option<Vec<LayerRule>>,
}

/// A model source for merging
#[derive(Debug, Clone)]
pub struct ModelSource {
    /// Path to the model file
    pub path: String,
    /// Optional weight for this model in the merge
    pub weight: Option<f64>,
}

/// Report summarizing a merge operation
#[derive(Debug, Clone)]
pub struct LayerMergeReport {
    /// Total tensors processed during merge
    pub tensors_processed: usize,
    /// Count of tensors matched by each rule pattern
    pub rules_matched: HashMap<String, usize>,
}

impl LayerMergeReport {
    /// Create a new empty report
    #[must_use]
    pub fn new() -> Self {
        Self {
            tensors_processed: 0,
            rules_matched: HashMap::new(),
        }
    }

    /// Record a tensor being processed, optionally matched by a rule
    pub fn record_tensor(&mut self, matched_pattern: Option<&str>) {
        self.tensors_processed += 1;
        if let Some(pattern) = matched_pattern {
            *self.rules_matched.entry(pattern.to_string()).or_insert(0) += 1;
        }
    }

    /// Total number of tensors that matched at least one rule
    #[must_use]
    pub fn total_matched(&self) -> usize {
        self.rules_matched.values().sum()
    }

    /// Total number of tensors that used the default strategy
    #[must_use]
    pub fn total_defaulted(&self) -> usize {
        self.tensors_processed.saturating_sub(self.total_matched())
    }
}

impl Default for LayerMergeReport {
    fn default() -> Self {
        Self::new()
    }
}

/// Match a tensor name against an ordered list of layer rules
///
/// Returns the first matching rule. Patterns support:
/// - Substring match: `"attn"` matches `"model.layers.0.self_attn.q_proj.weight"`
/// - Wildcard `*`: `"layers.0.*weight"` matches names containing `"layers.0."`
///   followed (possibly later) by `"weight"`
/// - Escaped dot `\\.`: treated as a literal `.` in the pattern
///
/// # Arguments
///
/// * `tensor_name` - The fully qualified tensor name
/// * `rules` - Ordered slice of layer rules to try
///
/// # Returns
///
/// Reference to the first matching `LayerRule`, or `None` if no rule matches.
pub fn match_layer_rule<'a>(tensor_name: &str, rules: &'a [LayerRule]) -> Option<&'a LayerRule> {
    rules
        .iter()
        .find(|rule| pattern_matches(tensor_name, &rule.layer_pattern))
}

/// Simple pattern matching supporting substring and `*` wildcards
///
/// Splits the pattern on `*` and checks that all segments appear in order
/// within the target string. Escaped dots (`\\.`) are treated as literal dots.
fn pattern_matches(name: &str, pattern: &str) -> bool {
    // Normalize escaped dots to literal dots for matching
    let normalized = pattern.replace("\\.", ".");

    if !normalized.contains('*') {
        // Pure substring match
        return name.contains(&normalized);
    }

    // Wildcard match: split on * and ensure all parts appear in order
    let parts: Vec<&str> = normalized.split('*').collect();
    let mut search_from = 0;

    for part in &parts {
        if part.is_empty() {
            continue;
        }
        match name[search_from..].find(part) {
            Some(pos) => {
                search_from += pos + part.len();
            }
            None => return false,
        }
    }
    true
}

/// Parser state machine section tracking
#[derive(Debug, PartialEq)]
enum ParserSection {
    Root,
    Models,
    ModelEntry,
    Layers,
    LayerEntry,
}

/// Parse a YAML-like merge configuration string
///
/// Supports a simplified YAML subset with the following structure:
///
/// ```yaml
/// models:
///   - path: /path/to/model1.apr
///     weight: 0.6
///   - path: /path/to/model2.apr
///     weight: 0.4
/// output: /path/to/merged.apr
/// default_strategy: average
/// layers:
///   - layer_pattern: "attn"
///     strategy: slerp
///     weights: [0.7, 0.3]
///     scale: 1.0
/// ```
///
/// # Errors
///
/// Returns `AprenderError::ValidationError` if the YAML is malformed
/// or missing required fields.
pub fn parse_merge_yaml(yaml_str: &str) -> Result<MergeYamlConfig> {
    let mut models: Vec<ModelSource> = Vec::new();
    let mut output = String::new();
    let mut default_strategy = String::new();
    let mut layer_rules: Vec<LayerRule> = Vec::new();

    let mut section = ParserSection::Root;
    let mut current_model_path = String::new();
    let mut current_model_weight: Option<f64> = None;
    let mut current_layer_pattern = String::new();
    let mut current_layer_strategy = String::new();
    let mut current_layer_weights: Option<Vec<f64>> = None;
    let mut current_layer_scale: Option<f64> = None;

    for line in yaml_str.lines() {
        let trimmed = line.trim();

        // Skip empty lines and comments
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }

        // Detect top-level section headers (e.g., "models:" at column 0)
        if !line.starts_with(' ') && !line.starts_with('\t') && !trimmed.starts_with('-') {
            // Flush pending entries before switching sections
            flush_model_entry(
                &section,
                &mut models,
                &mut current_model_path,
                &mut current_model_weight,
            );
            flush_layer_entry(
                &section,
                &mut layer_rules,
                &mut current_layer_pattern,
                &mut current_layer_strategy,
                &mut current_layer_weights,
                &mut current_layer_scale,
            );

            if trimmed.ends_with(':') && !trimmed.contains(": ") {
                let key = trimmed.trim_end_matches(':');
                match key {
                    "models" => {
                        section = ParserSection::Models;
                        continue;
                    }
                    "layers" => {
                        section = ParserSection::Layers;
                        continue;
                    }
                    _ => {}
                }
            }

            // Root-level "key: value" pairs
            if let Some(val) = parse_kv(trimmed, "output") {
                output = unquote(val);
                section = ParserSection::Root;
            } else if let Some(val) = parse_kv(trimmed, "default_strategy") {
                default_strategy = unquote(val);
                section = ParserSection::Root;
            }
            continue;
        }

        // Indented content — belongs to current section
        match section {
            ParserSection::Models | ParserSection::ModelEntry => {
                if trimmed.starts_with("- ") || trimmed == "-" {
                    // Flush previous model entry
                    flush_model_entry(
                        &section,
                        &mut models,
                        &mut current_model_path,
                        &mut current_model_weight,
                    );
                    section = ParserSection::ModelEntry;

                    // Handle inline "- path: value"
                    let after_dash = trimmed.trim_start_matches('-').trim();
                    if let Some(val) = parse_kv(after_dash, "path") {
                        current_model_path = unquote(val);
                    }
                } else if let Some(val) = parse_kv(trimmed, "path") {
                    current_model_path = unquote(val);
                } else if let Some(val) = parse_kv(trimmed, "weight") {
                    current_model_weight = val.trim().parse::<f64>().ok();
                }
            }
            ParserSection::Layers | ParserSection::LayerEntry => {
                if trimmed.starts_with("- ") || trimmed == "-" {
                    // Flush previous layer entry
                    flush_layer_entry(
                        &section,
                        &mut layer_rules,
                        &mut current_layer_pattern,
                        &mut current_layer_strategy,
                        &mut current_layer_weights,
                        &mut current_layer_scale,
                    );
                    section = ParserSection::LayerEntry;

                    let after_dash = trimmed.trim_start_matches('-').trim();
                    if let Some(val) = parse_kv(after_dash, "layer_pattern") {
                        current_layer_pattern = unquote(val);
                    }
                } else if let Some(val) = parse_kv(trimmed, "layer_pattern") {
                    current_layer_pattern = unquote(val);
                } else if let Some(val) = parse_kv(trimmed, "strategy") {
                    current_layer_strategy = unquote(val);
                } else if let Some(val) = parse_kv(trimmed, "weights") {
                    current_layer_weights = Some(parse_float_list(val));
                } else if let Some(val) = parse_kv(trimmed, "scale") {
                    current_layer_scale = val.trim().parse::<f64>().ok();
                }
            }
            ParserSection::Root => {
                // Indented root content — try key: value
                if let Some(val) = parse_kv(trimmed, "output") {
                    output = unquote(val);
                } else if let Some(val) = parse_kv(trimmed, "default_strategy") {
                    default_strategy = unquote(val);
                }
            }
        }
    }

    // Flush final pending entries
    flush_model_entry(
        &section,
        &mut models,
        &mut current_model_path,
        &mut current_model_weight,
    );
    flush_layer_entry(
        &section,
        &mut layer_rules,
        &mut current_layer_pattern,
        &mut current_layer_strategy,
        &mut current_layer_weights,
        &mut current_layer_scale,
    );

    if output.is_empty() {
        return Err(AprenderError::ValidationError {
            message: "merge config missing required field: output".to_string(),
        });
    }

    if default_strategy.is_empty() {
        return Err(AprenderError::ValidationError {
            message: "merge config missing required field: default_strategy".to_string(),
        });
    }

    let layers = if layer_rules.is_empty() {
        None
    } else {
        Some(layer_rules)
    };

    Ok(MergeYamlConfig {
        models,
        output,
        default_strategy,
        layers,
    })
}

/// Validate a parsed merge configuration
///
/// Checks:
/// - At least 2 models are specified
/// - All strategy names are recognized
/// - Weights (if provided) are non-negative and finite
/// - Layer patterns are non-empty
///
/// # Errors
///
/// Returns `AprenderError::ValidationError` with a descriptive message
/// on the first validation failure.
pub fn validate_merge_config(config: &MergeYamlConfig) -> Result<()> {
    if config.models.len() < 2 {
        return Err(AprenderError::ValidationError {
            message: format!(
                "merge requires at least 2 models, got {}",
                config.models.len()
            ),
        });
    }

    // Validate default strategy
    if !is_valid_strategy(&config.default_strategy) {
        return Err(AprenderError::ValidationError {
            message: format!(
                "unknown default strategy '{}', valid: {}",
                config.default_strategy,
                VALID_STRATEGIES.join(", ")
            ),
        });
    }

    // Validate model paths
    for (i, model) in config.models.iter().enumerate() {
        if model.path.is_empty() {
            return Err(AprenderError::ValidationError {
                message: format!("model {} has empty path", i),
            });
        }
        if let Some(w) = model.weight {
            if !w.is_finite() || w < 0.0 {
                return Err(AprenderError::ValidationError {
                    message: format!(
                        "model {} weight must be non-negative and finite, got {}",
                        i, w
                    ),
                });
            }
        }
    }

    // Validate output path
    if config.output.is_empty() {
        return Err(AprenderError::ValidationError {
            message: "output path is empty".to_string(),
        });
    }

    // Validate layer rules
    if let Some(ref rules) = config.layers {
        for (i, rule) in rules.iter().enumerate() {
            if rule.layer_pattern.is_empty() {
                return Err(AprenderError::ValidationError {
                    message: format!("layer rule {} has empty pattern", i),
                });
            }
            if !is_valid_strategy(&rule.strategy) {
                return Err(AprenderError::ValidationError {
                    message: format!(
                        "layer rule {} has unknown strategy '{}', valid: {}",
                        i,
                        rule.strategy,
                        VALID_STRATEGIES.join(", ")
                    ),
                });
            }
            if let Some(ref weights) = rule.weights {
                for (j, &w) in weights.iter().enumerate() {
                    if !w.is_finite() {
                        return Err(AprenderError::ValidationError {
                            message: format!("layer rule {} weight[{}] is not finite: {}", i, j, w),
                        });
                    }
                }
            }
            if let Some(s) = rule.scale {
                if !s.is_finite() {
                    return Err(AprenderError::ValidationError {
                        message: format!("layer rule {} scale is not finite: {}", i, s),
                    });
                }
            }
        }
    }

    Ok(())
}

/// Check whether a strategy name is recognized
fn is_valid_strategy(name: &str) -> bool {
    VALID_STRATEGIES.contains(&name)
}

/// Parse a `key: value` pair, returning the value if the key matches
fn parse_kv<'a>(line: &'a str, key: &str) -> Option<&'a str> {
    let trimmed = line.trim();
    let prefix_with_space = format!("{}: ", key);
    let prefix_bare = format!("{}:", key);

    if trimmed.starts_with(&prefix_with_space) {
        Some(trimmed[prefix_with_space.len()..].trim())
    } else if trimmed == prefix_bare {
        // Key with no value (e.g., "models:")
        None
    } else if trimmed.starts_with(&prefix_bare) {
        let rest = &trimmed[prefix_bare.len()..];
        if rest.is_empty() {
            None
        } else {
            Some(rest.trim())
        }
    } else {
        None
    }
}

/// Remove surrounding quotes from a string value
fn unquote(s: &str) -> String {
    let trimmed = s.trim();
    if (trimmed.starts_with('"') && trimmed.ends_with('"'))
        || (trimmed.starts_with('\'') && trimmed.ends_with('\''))
    {
        if trimmed.len() >= 2 {
            trimmed[1..trimmed.len() - 1].to_string()
        } else {
            trimmed.to_string()
        }
    } else {
        trimmed.to_string()
    }
}

/// Parse a bracketed list of floats like `[0.7, 0.3]`
fn parse_float_list(s: &str) -> Vec<f64> {
    let inner = s
        .trim()
        .trim_start_matches('[')
        .trim_end_matches(']')
        .trim();
    if inner.is_empty() {
        return Vec::new();
    }
    inner
        .split(',')
        .filter_map(|part| part.trim().parse::<f64>().ok())
        .collect()
}

/// Flush a pending model entry into the models list
fn flush_model_entry(
    section: &ParserSection,
    models: &mut Vec<ModelSource>,
    path: &mut String,
    weight: &mut Option<f64>,
) {
    if matches!(section, ParserSection::ModelEntry) && !path.is_empty() {
        models.push(ModelSource {
            path: std::mem::take(path),
            weight: weight.take(),
        });
    }
}

/// Flush a pending layer rule entry into the rules list
fn flush_layer_entry(
    section: &ParserSection,
    rules: &mut Vec<LayerRule>,
    pattern: &mut String,
    strategy: &mut String,
    weights: &mut Option<Vec<f64>>,
    scale: &mut Option<f64>,
) {
    if matches!(section, ParserSection::LayerEntry) && !pattern.is_empty() {
        rules.push(LayerRule {
            layer_pattern: std::mem::take(pattern),
            strategy: if strategy.is_empty() {
                "average".to_string()
            } else {
                std::mem::take(strategy)
            },
            weights: weights.take(),
            scale: scale.take(),
        });
    }
}

#[cfg(test)]
#[path = "per_layer_merge_tests.rs"]
mod tests;