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

impl LayoutContract {
    /// Create a new layout contract with all tensor specifications.
    ///
    /// This is the AUTHORITATIVE source of truth for tensor layouts.
    /// DO NOT modify these values without updating `contracts/tensor-layout-v1.yaml`.
    #[must_use]
    pub fn new() -> Self {
        let contracts = vec![
            // Embedding layer
            TensorContract {
                gguf_name: "token_embd.weight",
                apr_name: "model.embed_tokens.weight",
                gguf_shape_formula: "[hidden, vocab]",
                apr_shape_formula: "[vocab, hidden]",
                should_transpose: true,
                kernel_signature: "lookup (row = token embedding, not matmul)",
                kernel_out_dim: "vocab_size",
                kernel_in_dim: "hidden_dim",
                is_critical: false,
                notes: "Embedding lookup table - row = token embedding",
            },
            // LM Head (CRITICAL - GH-202 root cause)
            TensorContract {
                gguf_name: "output.weight",
                apr_name: "lm_head.weight",
                gguf_shape_formula: "[hidden, vocab]",
                apr_shape_formula: "[vocab, hidden]",
                should_transpose: true,
                kernel_signature: "matmul_q*k_rowmajor(W, x, vocab_size, hidden_dim)",
                kernel_out_dim: "vocab_size",
                kernel_in_dim: "hidden_dim",
                is_critical: true,
                notes: "GH-202 root cause - wrong shape caused [PAD] garbage output",
            },
            // Q projection
            TensorContract {
                gguf_name: "blk.{n}.attn_q.weight",
                apr_name: "model.layers.{n}.self_attn.q_proj.weight",
                gguf_shape_formula: "[hidden, heads*head_dim]",
                apr_shape_formula: "[heads*head_dim, hidden]",
                should_transpose: true,
                kernel_signature: "matmul_q*k_rowmajor(W, x, num_heads*head_dim, hidden_dim)",
                kernel_out_dim: "num_heads * head_dim",
                kernel_in_dim: "hidden_dim",
                is_critical: false,
                notes: "Query projection in attention",
            },
            // K projection
            TensorContract {
                gguf_name: "blk.{n}.attn_k.weight",
                apr_name: "model.layers.{n}.self_attn.k_proj.weight",
                gguf_shape_formula: "[hidden, kv_heads*head_dim]",
                apr_shape_formula: "[kv_heads*head_dim, hidden]",
                should_transpose: true,
                kernel_signature: "matmul_q*k_rowmajor(W, x, num_kv_heads*head_dim, hidden_dim)",
                kernel_out_dim: "num_kv_heads * head_dim",
                kernel_in_dim: "hidden_dim",
                is_critical: false,
                notes: "Key projection in attention",
            },
            // V projection
            TensorContract {
                gguf_name: "blk.{n}.attn_v.weight",
                apr_name: "model.layers.{n}.self_attn.v_proj.weight",
                gguf_shape_formula: "[hidden, kv_heads*head_dim]",
                apr_shape_formula: "[kv_heads*head_dim, hidden]",
                should_transpose: true,
                kernel_signature: "matmul_q*k_rowmajor(W, x, num_kv_heads*head_dim, hidden_dim)",
                kernel_out_dim: "num_kv_heads * head_dim",
                kernel_in_dim: "hidden_dim",
                is_critical: false,
                notes: "Value projection in attention",
            },
            // O projection
            TensorContract {
                gguf_name: "blk.{n}.attn_output.weight",
                apr_name: "model.layers.{n}.self_attn.o_proj.weight",
                gguf_shape_formula: "[heads*head_dim, hidden]",
                apr_shape_formula: "[hidden, heads*head_dim]",
                should_transpose: true,
                kernel_signature: "matmul_q*k_rowmajor(W, x, hidden_dim, num_heads*head_dim)",
                kernel_out_dim: "hidden_dim",
                kernel_in_dim: "num_heads * head_dim",
                is_critical: false,
                notes: "Output projection in attention",
            },
            // Gate projection (MLP)
            TensorContract {
                gguf_name: "blk.{n}.ffn_gate.weight",
                apr_name: "model.layers.{n}.mlp.gate_proj.weight",
                gguf_shape_formula: "[hidden, intermediate]",
                apr_shape_formula: "[intermediate, hidden]",
                should_transpose: true,
                kernel_signature: "matmul_q*k_rowmajor(W, x, intermediate_dim, hidden_dim)",
                kernel_out_dim: "intermediate_dim",
                kernel_in_dim: "hidden_dim",
                is_critical: false,
                notes: "Gate projection in SwiGLU MLP",
            },
            // Up projection (MLP)
            TensorContract {
                gguf_name: "blk.{n}.ffn_up.weight",
                apr_name: "model.layers.{n}.mlp.up_proj.weight",
                gguf_shape_formula: "[hidden, intermediate]",
                apr_shape_formula: "[intermediate, hidden]",
                should_transpose: true,
                kernel_signature: "matmul_q*k_rowmajor(W, x, intermediate_dim, hidden_dim)",
                kernel_out_dim: "intermediate_dim",
                kernel_in_dim: "hidden_dim",
                is_critical: false,
                notes: "Up projection in SwiGLU MLP",
            },
            // Down projection (MLP)
            TensorContract {
                gguf_name: "blk.{n}.ffn_down.weight",
                apr_name: "model.layers.{n}.mlp.down_proj.weight",
                gguf_shape_formula: "[intermediate, hidden]",
                apr_shape_formula: "[hidden, intermediate]",
                should_transpose: true,
                kernel_signature: "matmul_q*k_rowmajor(W, x, hidden_dim, intermediate_dim)",
                kernel_out_dim: "hidden_dim",
                kernel_in_dim: "intermediate_dim",
                is_critical: false,
                notes: "Down projection in SwiGLU MLP",
            },
            // Input LayerNorm (1D - no transpose)
            TensorContract {
                gguf_name: "blk.{n}.attn_norm.weight",
                apr_name: "model.layers.{n}.input_layernorm.weight",
                gguf_shape_formula: "[hidden]",
                apr_shape_formula: "[hidden]",
                should_transpose: false,
                kernel_signature: "element-wise multiply",
                kernel_out_dim: "hidden_dim",
                kernel_in_dim: "hidden_dim",
                is_critical: false,
                notes: "RMSNorm before attention - 1D tensor, no transpose",
            },
            // Post-attention LayerNorm (1D - no transpose)
            TensorContract {
                gguf_name: "blk.{n}.ffn_norm.weight",
                apr_name: "model.layers.{n}.post_attention_layernorm.weight",
                gguf_shape_formula: "[hidden]",
                apr_shape_formula: "[hidden]",
                should_transpose: false,
                kernel_signature: "element-wise multiply",
                kernel_out_dim: "hidden_dim",
                kernel_in_dim: "hidden_dim",
                is_critical: false,
                notes: "RMSNorm before MLP - 1D tensor, no transpose",
            },
            // Final LayerNorm (1D - no transpose)
            TensorContract {
                gguf_name: "output_norm.weight",
                apr_name: "model.norm.weight",
                gguf_shape_formula: "[hidden]",
                apr_shape_formula: "[hidden]",
                should_transpose: false,
                kernel_signature: "element-wise multiply",
                kernel_out_dim: "hidden_dim",
                kernel_in_dim: "hidden_dim",
                is_critical: false,
                notes: "Final RMSNorm - 1D tensor, no transpose",
            },
        ];

        let mut gguf_contracts = HashMap::new();
        let mut apr_contracts = HashMap::new();

        for contract in contracts {
            gguf_contracts.insert(contract.gguf_name, contract.clone());
            apr_contracts.insert(contract.apr_name, contract);
        }

        Self {
            gguf_contracts,
            apr_contracts,
        }
    }

    /// Get the contract for a GGUF tensor name.
    ///
    /// Handles both exact matches and pattern matches (e.g., "blk.0.attn_q.weight").
    #[must_use]
    pub fn get_gguf_contract(&self, name: &str) -> Option<&TensorContract> {
        // Try exact match first
        if let Some(contract) = self.gguf_contracts.get(name) {
            return Some(contract);
        }

        // Try pattern match (replace layer numbers with {n})
        let pattern = normalize_layer_pattern(name);
        self.gguf_contracts.get(pattern.as_str())
    }

    /// Get the contract for an APR tensor name.
    ///
    /// Handles both exact matches and pattern matches.
    #[must_use]
    pub fn get_apr_contract(&self, name: &str) -> Option<&TensorContract> {
        // Try exact match first
        if let Some(contract) = self.apr_contracts.get(name) {
            return Some(contract);
        }

        // Try pattern match (replace layer numbers with {n})
        let pattern = normalize_layer_pattern(name);
        self.apr_contracts.get(pattern.as_str())
    }

    /// Check if a tensor should be transposed during GGUF→APR conversion.
    ///
    /// Returns `true` for all 2D weight tensors, `false` for 1D tensors.
    #[must_use]
    pub fn should_transpose_gguf(&self, name: &str) -> bool {
        self.get_gguf_contract(name)
            .map_or(false, |c| c.should_transpose)
    }

    /// Check if a tensor is critical (affects inference correctness).
    #[must_use]
    pub fn is_critical_tensor(&self, name: &str) -> bool {
        self.get_gguf_contract(name)
            .or_else(|| self.get_apr_contract(name))
            .map_or(false, |c| c.is_critical)
    }

    /// Validate that an APR tensor shape matches the contract expectation.
    ///
    /// # Arguments
    ///
    /// * `name` - APR tensor name
    /// * `shape` - Actual tensor shape
    /// * `vocab_size` - Model vocabulary size
    /// * `hidden_dim` - Model hidden dimension
    ///
    /// # Errors
    ///
    /// Returns `ContractError::ShapeMismatch` if the shape doesn't match expectations.
    pub fn validate_apr_shape(
        &self,
        name: &str,
        shape: &[usize],
        vocab_size: usize,
        hidden_dim: usize,
    ) -> Result<(), ContractError> {
        let Some(contract) = self.get_apr_contract(name) else {
            return Ok(()); // Unknown tensor - skip validation
        };

        // Special validation for critical tensors
        if contract.is_critical {
            // lm_head must be [vocab_size, hidden_dim]
            if name.contains("lm_head") || name.contains("output.weight") {
                if shape.len() != 2 {
                    return Err(ContractError::ShapeMismatch {
                        tensor: name.to_string(),
                        expected: format!("[{}, {}]", vocab_size, hidden_dim),
                        actual: shape.to_vec(),
                    });
                }
                if shape[0] != vocab_size || shape[1] != hidden_dim {
                    return Err(ContractError::ShapeMismatch {
                        tensor: name.to_string(),
                        expected: format!("[{}, {}]", vocab_size, hidden_dim),
                        actual: shape.to_vec(),
                    });
                }
            }
        }

        Ok(())
    }

    /// Calculate the expected byte size for a quantized tensor.
    ///
    /// Uses the formula: `out_dim * ceil(in_dim / QK_K) * block_bytes`
    #[must_use]
    pub fn calculate_q4k_bytes(out_dim: usize, in_dim: usize) -> usize {
        let superblocks = in_dim.div_ceil(block_sizes::QK_K);
        out_dim * superblocks * block_sizes::Q4_K
    }

    /// Calculate the expected byte size for Q6K quantized tensor.
    #[must_use]
    pub fn calculate_q6k_bytes(out_dim: usize, in_dim: usize) -> usize {
        let superblocks = in_dim.div_ceil(block_sizes::QK_K);
        out_dim * superblocks * block_sizes::Q6_K
    }

    /// Validate that a quantized tensor has the correct byte size.
    ///
    /// # Errors
    ///
    /// Returns `ContractError::ByteSizeMismatch` if the size doesn't match.
    pub fn validate_q6k_bytes(
        &self,
        name: &str,
        actual_bytes: usize,
        out_dim: usize,
        in_dim: usize,
    ) -> Result<(), ContractError> {
        let expected = Self::calculate_q6k_bytes(out_dim, in_dim);
        if actual_bytes != expected {
            return Err(ContractError::ByteSizeMismatch {
                tensor: name.to_string(),
                expected,
                actual: actual_bytes,
            });
        }
        Ok(())
    }

    /// Get all critical tensors from the contract.
    #[must_use]
    pub fn critical_tensors(&self) -> Vec<&TensorContract> {
        self.gguf_contracts
            .values()
            .filter(|c| c.is_critical)
            .collect()
    }

    /// Get all tensors that require transpose.
    #[must_use]
    pub fn transpose_tensors(&self) -> Vec<&TensorContract> {
        self.gguf_contracts
            .values()
            .filter(|c| c.should_transpose)
            .collect()
    }

    /// Get all 1D tensors (no transpose required).
    #[must_use]
    pub fn non_transpose_tensors(&self) -> Vec<&TensorContract> {
        self.gguf_contracts
            .values()
            .filter(|c| !c.should_transpose)
            .collect()
    }
}

/// Normalize a tensor name by replacing layer numbers with `{n}`.
///
/// Examples:
/// - `blk.0.attn_q.weight` → `blk.{n}.attn_q.weight`
/// - `model.layers.5.self_attn.q_proj.weight` → `model.layers.{n}.self_attn.q_proj.weight`
fn normalize_layer_pattern(name: &str) -> String {
    // Replace patterns like "blk.0." or "layers.5." with "blk.{n}." or "layers.{n}."
    let mut result = name.to_string();

    // GGUF pattern: blk.N.
    if let Some(start) = result.find("blk.") {
        let after_blk = start + 4;
        if let Some(dot_pos) = result[after_blk..].find('.') {
            let num_end = after_blk + dot_pos;
            if result[after_blk..num_end]
                .chars()
                .all(|c| c.is_ascii_digit())
            {
                result = format!("{}{{n}}{}", &result[..after_blk], &result[num_end..]);
            }
        }
    }

    // APR pattern: layers.N.
    if let Some(start) = result.find("layers.") {
        let after_layers = start + 7;
        if let Some(dot_pos) = result[after_layers..].find('.') {
            let num_end = after_layers + dot_pos;
            if result[after_layers..num_end]
                .chars()
                .all(|c| c.is_ascii_digit())
            {
                result = format!("{}{{n}}{}", &result[..after_layers], &result[num_end..]);
            }
        }
    }

    result
}

/// Global layout contract instance.
///
/// Use this for all layout validation instead of creating new instances.
/// Note: Using a function instead of LazyLock for MSRV 1.70 compatibility.
/// LazyLock requires Rust 1.80+.
pub fn contract() -> LayoutContract {
    LayoutContract::new()
}

/// Validation rule IDs from the contract.
pub mod validation_rules {
    /// F-LAYOUT-CONTRACT-001: All 2D weights are transposed
    pub const ALL_2D_TRANSPOSED: &str = "F-LAYOUT-CONTRACT-001";
    /// F-LAYOUT-CONTRACT-002: lm_head shape matches kernel expectation
    pub const LM_HEAD_SHAPE: &str = "F-LAYOUT-CONTRACT-002";
    /// F-LAYOUT-CONTRACT-003: 1D tensors unchanged
    pub const TENSORS_1D_UNCHANGED: &str = "F-LAYOUT-CONTRACT-003";
    /// F-LAYOUT-CONTRACT-004: Byte size matches kernel expectation
    pub const BYTE_SIZE_MATCHES: &str = "F-LAYOUT-CONTRACT-004";
    /// F-LAYOUT-CONTRACT-005: No garbage output from lm_head
    pub const NO_GARBAGE_OUTPUT: &str = "F-LAYOUT-CONTRACT-005";
}