aprender-core 0.29.1

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
//! Model Family Contract Types (PMAT-241)
//!
//! Defines the `ModelFamily` trait and associated configuration types for
//! compiler-enforced model family contracts.
//!
//! # Theoretical Foundation
//!
//! - Shingo (1986): Poka-Yoke / Zero Quality Control
//! - Strom & Yemini (1986): Typestate programming
//! - Parsons (2019): Parse, Don't Validate
//!
//! # Contract
//!
//! See `contracts/model-families/*.yaml` and
//! `docs/specifications/compiler-enforced-model-types-model-oracle.md`

use std::collections::HashMap;
use std::fmt;

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

// ============================================================================
// Enums
// ============================================================================

/// Attention mechanism type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttentionType {
    /// Multi-Head Attention (standard transformer)
    Mha,
    /// Grouped Query Attention (GQA)
    Gqa,
    /// Multi-Query Attention (MQA)
    Mqa,
    /// State Space Model (no attention — selective scan)
    Ssm,
    /// Linear Attention (WKV recurrence, no softmax)
    Linear,
}

impl fmt::Display for AttentionType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Mha => write!(f, "MHA"),
            Self::Gqa => write!(f, "GQA"),
            Self::Mqa => write!(f, "MQA"),
            Self::Ssm => write!(f, "SSM"),
            Self::Linear => write!(f, "Linear"),
        }
    }
}

impl AttentionType {
    /// Parse from YAML string
    pub fn from_str_contract(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "mha" => Ok(Self::Mha),
            "gqa" => Ok(Self::Gqa),
            "mqa" => Ok(Self::Mqa),
            "ssm" => Ok(Self::Ssm),
            "linear" => Ok(Self::Linear),
            _ => Err(AprenderError::FormatError {
                message: format!(
                    "Unknown attention type: {s}. Expected: mha, gqa, mqa, ssm, linear"
                ),
            }),
        }
    }
}

/// Activation function type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Activation {
    /// SiLU (Swish) activation
    Silu,
    /// GELU activation
    Gelu,
    /// ReLU activation
    Relu,
}

impl fmt::Display for Activation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Silu => write!(f, "SiLU"),
            Self::Gelu => write!(f, "GELU"),
            Self::Relu => write!(f, "ReLU"),
        }
    }
}

impl Activation {
    pub fn from_str_contract(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "silu" | "swish" => Ok(Self::Silu),
            "gelu" => Ok(Self::Gelu),
            "relu" => Ok(Self::Relu),
            _ => Err(AprenderError::FormatError {
                message: format!("Unknown activation: {s}. Expected: silu, gelu, relu"),
            }),
        }
    }
}

/// Normalization type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NormType {
    /// RMS Normalization (LLaMA, Qwen2)
    RmsNorm,
    /// Layer Normalization (BERT, Whisper)
    LayerNorm,
}

impl fmt::Display for NormType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::RmsNorm => write!(f, "RMSNorm"),
            Self::LayerNorm => write!(f, "LayerNorm"),
        }
    }
}

impl NormType {
    pub fn from_str_contract(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "rmsnorm" | "rms_norm" => Ok(Self::RmsNorm),
            "layernorm" | "layer_norm" => Ok(Self::LayerNorm),
            _ => Err(AprenderError::FormatError {
                message: format!("Unknown norm type: {s}. Expected: rmsnorm, layernorm"),
            }),
        }
    }
}

/// Positional encoding type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PositionalEncoding {
    /// Rotary Position Embeddings (LLaMA, Qwen2)
    Rope,
    /// ALiBi (Bloom)
    Alibi,
    /// Absolute position embeddings (BERT, Whisper)
    Absolute,
    /// Relative position embeddings
    Relative,
    /// No positional encoding (RWKV, Mamba — state carries temporal info)
    None,
}

impl fmt::Display for PositionalEncoding {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Rope => write!(f, "RoPE"),
            Self::Alibi => write!(f, "ALiBi"),
            Self::Absolute => write!(f, "Absolute"),
            Self::Relative => write!(f, "Relative"),
            Self::None => write!(f, "None"),
        }
    }
}

impl PositionalEncoding {
    pub fn from_str_contract(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "rope" => Ok(Self::Rope),
            "alibi" => Ok(Self::Alibi),
            "absolute" | "sinusoidal" => Ok(Self::Absolute),
            "relative" => Ok(Self::Relative),
            "none" => Ok(Self::None),
            _ => Err(AprenderError::FormatError {
                message: format!(
                    "Unknown positional encoding: {s}. Expected: rope, alibi, absolute, relative, none"
                ),
            }),
        }
    }
}

/// MLP type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MlpType {
    /// SwiGLU (LLaMA, Qwen2) - gated with SiLU
    SwiGlu,
    /// Standard GELU MLP (BERT, Whisper)
    GeluMlp,
    /// Gated MLP (generic)
    GatedMlp,
}

impl fmt::Display for MlpType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SwiGlu => write!(f, "SwiGLU"),
            Self::GeluMlp => write!(f, "GELU MLP"),
            Self::GatedMlp => write!(f, "Gated MLP"),
        }
    }
}

impl MlpType {
    pub fn from_str_contract(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "swiglu" => Ok(Self::SwiGlu),
            "gelu_mlp" | "gelu" | "standard" => Ok(Self::GeluMlp),
            "gated_mlp" | "gated" => Ok(Self::GatedMlp),
            _ => Err(AprenderError::FormatError {
                message: format!("Unknown MLP type: {s}. Expected: swiglu, gelu_mlp, gated_mlp"),
            }),
        }
    }
}

// ============================================================================
// Configuration Structs
// ============================================================================

/// Configuration for a specific model size within a family.
#[derive(Debug, Clone)]
pub struct ModelSizeConfig {
    /// Human-readable parameter count (e.g., "0.5B", "7B")
    pub parameters: String,
    /// Hidden dimension
    pub hidden_dim: usize,
    /// Number of transformer layers
    pub num_layers: usize,
    /// Number of attention heads
    pub num_heads: usize,
    /// Number of key-value heads (for GQA)
    pub num_kv_heads: usize,
    /// Intermediate (FFN) dimension
    pub intermediate_dim: usize,
    /// Vocabulary size
    pub vocab_size: usize,
    /// Maximum position embeddings
    pub max_position_embeddings: usize,
    /// Per-head dimension (`hidden_dim / num_heads`)
    pub head_dim: usize,
    /// RoPE theta frequency (0.0 if not using RoPE)
    pub rope_theta: f64,
    /// Normalization epsilon
    pub norm_eps: f64,
}

/// Architectural constraints for a model family.
#[derive(Debug, Clone)]
pub struct ModelConstraints {
    pub attention_type: AttentionType,
    pub activation: Activation,
    pub norm_type: NormType,
    pub has_bias: bool,
    pub tied_embeddings: bool,
    pub positional_encoding: PositionalEncoding,
    pub mlp_type: MlpType,
    /// GH-280: Whether Q and K projections have per-head RMSNorm (e.g., Qwen3)
    pub qk_norm: bool,
}

/// Tensor name template for a model family.
#[derive(Debug, Clone)]
pub struct TensorTemplate {
    /// Embedding tensor name (e.g., "model.embed\_tokens.weight")
    pub embedding: String,
    /// LM head tensor name (e.g., "lm\_head.weight")
    pub lm_head: Option<String>,
    /// Final normalization tensor name
    pub final_norm: Option<String>,
    /// Per-layer tensor name patterns (keys: q\_proj, k\_proj, etc., values contain {n} placeholder)
    pub per_layer: HashMap<String, Option<String>>,
}

/// GH-277: GGUF tensor name template for contract-driven export.
///
/// Maps APR canonical tensor roles to GGUF canonical tensor names
/// (from llama.cpp llama-arch.cpp). Used by the GGUF exporter to
/// produce llama.cpp-compatible files without hardcoded name tables.
#[derive(Debug, Clone, Default)]
pub struct GgufTensorTemplate {
    /// Embedding tensor GGUF name (e.g., "token_embd.weight")
    pub embedding: Option<String>,
    /// Position embedding GGUF name (e.g., "position_embd.weight"), None if not used
    pub position_embedding: Option<String>,
    /// LM head GGUF name (e.g., "output.weight")
    pub lm_head: Option<String>,
    /// Final norm weight GGUF name (e.g., "output_norm.weight")
    pub final_norm_weight: Option<String>,
    /// Final norm bias GGUF name (e.g., "output_norm.bias"), None for RMSNorm models
    pub final_norm_bias: Option<String>,
    /// Per-layer tensor role → GGUF suffix (after "blk.{n}.")
    /// None values mean the tensor should be SKIPPED during export
    pub per_layer: HashMap<String, Option<String>>,
    /// GH-277: If true, transpose 2D weight tensors from Conv1D to Linear layout during export.
    /// GPT-2 uses Conv1D `[in_features, out_features]`, llama.cpp expects `[out_features, in_features]`.
    pub transpose_weights: bool,
    /// GH-277: Fusion rules for concatenating multiple APR tensors into one GGUF tensor.
    /// Used for architectures like GPT-2 where llama.cpp expects fused QKV.
    pub fuse: Vec<GgufFusionRule>,
}

/// GH-277: Rule for fusing multiple APR per-layer tensors into a single GGUF tensor.
///
/// Example: GPT-2's separate Q, K, V projections are concatenated into a single
/// `attn_qkv.weight` tensor for llama.cpp compatibility.
#[derive(Debug, Clone)]
pub struct GgufFusionRule {
    /// GGUF suffix for the fused tensor (e.g., "attn_qkv.weight")
    pub gguf_suffix: String,
    /// APR tensor role names to concatenate, in order (e.g., `q_proj_weight`, `k_proj_weight`, `v_proj_weight`)
    pub source_roles: Vec<String>,
}

/// Shape template for a model family (parameterized expressions).
#[derive(Debug, Clone)]
pub struct ShapeTemplate {
    /// Map of tensor role to parameterized shape expression
    /// e.g., "q\_proj" maps to "\[num\_heads * head\_dim, hidden\_dim\]"
    pub shapes: HashMap<String, String>,
}

/// Chat template configuration.
#[derive(Debug, Clone)]
pub struct ChatTemplateConfig {
    pub format: String,
    pub template: String,
    pub bos_token: String,
    pub eos_token: String,
    pub special_tokens: HashMap<String, String>,
}

/// Certification cross-reference configuration.
#[derive(Debug, Clone)]
pub struct CertificationConfig {
    pub playbook_path: String,
    pub csv_family_key: String,
    pub size_categories: HashMap<String, String>,
}

/// Complete configuration for a model family.
#[derive(Debug, Clone)]
pub struct ModelFamilyConfig {
    /// Canonical family name (e.g., "qwen2")
    pub family: String,
    /// Human-readable display name
    pub display_name: String,
    /// Vendor/organization
    pub vendor: String,
    /// HuggingFace architecture identifiers
    pub architectures: Vec<String>,
    /// HuggingFace repo name pattern
    pub hf_pattern: String,
    /// Size variants keyed by name (e.g., "0.5b", "7b")
    pub size_variants: HashMap<String, ModelSizeConfig>,
    /// Architectural constraints
    pub constraints: ModelConstraints,
    /// Tensor name template
    pub tensor_template: TensorTemplate,
    /// GH-277: GGUF tensor name template for export
    pub gguf_tensor_template: GgufTensorTemplate,
    /// Shape template
    pub shape_template: ShapeTemplate,
    /// Supported quantization formats
    pub quantizations: Vec<String>,
    /// Chat template (None for non-chat models like Whisper, BERT)
    pub chat_template: Option<ChatTemplateConfig>,
    /// Certification cross-reference
    pub certification: Option<CertificationConfig>,
}

// ============================================================================
// Contract Error
// ============================================================================

/// Model family contract error
#[derive(Debug, Clone)]
pub struct ContractError {
    pub family: String,
    pub message: String,
}

impl fmt::Display for ContractError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Model family contract error [{}]: {}",
            self.family, self.message
        )
    }
}

impl std::error::Error for ContractError {}

impl From<ContractError> for AprenderError {
    fn from(err: ContractError) -> Self {
        AprenderError::FormatError {
            message: err.to_string(),
        }
    }
}

// ============================================================================
// ModelFamily Trait
// ============================================================================

/// Trait implemented by each model family.
///
/// This trait is the compile-time bridge between YAML contracts and Rust code.
/// Implementations can be generated by build.rs from model family YAMLs (PMAT-250)
/// or loaded at runtime from YAML files (PMAT-242).
pub trait ModelFamily: fmt::Debug + Send + Sync {
    /// Canonical family name (e.g., "qwen2")
    fn family_name(&self) -> &str;

    /// Human-readable display name
    fn display_name(&self) -> &str;

    /// Get the full configuration
    fn config(&self) -> &ModelFamilyConfig;

    /// Get configuration for a specific size variant
    fn size_config(&self, size: &str) -> Option<&ModelSizeConfig>;

    /// Detect size variant from model config (`hidden_dim`, `num_layers`)
    fn detect_size(&self, hidden_dim: usize, num_layers: usize) -> Option<String>;

    /// Get architectural constraints
    fn constraints(&self) -> &ModelConstraints;

    /// Expected tensor count for a given size variant
    fn expected_tensor_count(&self, size: &str) -> Option<usize>;

    /// Validate that a set of tensor names matches the contract
    fn validate_tensor_names(
        &self,
        names: &[&str],
        size: &str,
    ) -> std::result::Result<(), ContractError>;
}

// ============================================================================
// DynModelFamily - Runtime implementation backed by ModelFamilyConfig
// ============================================================================

/// Dynamic model family implementation backed by a `ModelFamilyConfig`.
/// Used when family is loaded from YAML at runtime.
#[derive(Debug, Clone)]
pub struct DynModelFamily {
    config: ModelFamilyConfig,
}

impl DynModelFamily {
    /// Create from a loaded config
    #[must_use]
    pub fn new(config: ModelFamilyConfig) -> Self {
        Self { config }
    }
}

include!("family_registry.rs");
include!("model_family_tests.rs");