realizar 0.8.5

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors
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
#![allow(dead_code)]
// GH-311: Generated from tensor-names-v1.yaml by build.rs.
// The include! pulls in the generated enums, normalize_architecture(),
// and name/template lookup functions.
//
// Fallback: if build.rs can't find the YAML (CI/crates.io), it uses
// tensor_names_fallback.rs instead.
include!(concat!(env!("OUT_DIR"), "/tensor_names_generated.rs"));

use crate::error::{RealizarError, Result};
use crate::safetensors_infer::TensorSource;

/// Resolve a global tensor from a source using contract-driven name lookup.
///
/// Tries architecture-specific names first, then GGUF fallbacks, then
/// bare names (stripping "model." prefix).
///
/// # Errors
///
/// Returns error listing all attempted names if tensor is not found.
pub(crate) fn resolve_global<S: TensorSource>(
    source: &S,
    arch: &str,
    role: GlobalTensorRole,
) -> Result<Vec<f32>> {
    let arch_key = normalize_architecture(arch);
    let mut tried = Vec::new();

    // 1. Architecture-specific names
    for name in global_names(arch_key, role) {
        if let Ok(t) = source.get_tensor_auto(name) {
            return Ok(t);
        }
        tried.push(*name);
    }

    // 2. GGUF fallback names
    for name in global_fallback_names(role) {
        if let Ok(t) = source.get_tensor_auto(name) {
            return Ok(t);
        }
        tried.push(*name);
    }

    // 3. Bare names (strip "model." prefix)
    for name in global_names(arch_key, role) {
        if let Some(bare) = name.strip_prefix("model.") {
            if let Ok(t) = source.get_tensor_auto(bare) {
                return Ok(t);
            }
            tried.push(bare);
        }
    }

    // Diagnostic error
    let available = source.tensor_names();
    let sample: Vec<&str> = available.iter().take(5).copied().collect();
    Err(RealizarError::UnsupportedOperation {
        operation: "tensor_names::resolve_global".to_string(),
        reason: format!(
            "Tensor not found for {:?} (arch='{}'). Tried: {:?}. \
             Available tensors ({} total): {:?}{}",
            role,
            arch,
            tried,
            available.len(),
            sample,
            if available.len() > 5 { ", ..." } else { "" }
        ),
    })
}

/// Resolve a global tensor, returning None if not found.
pub(crate) fn resolve_global_optional<S: TensorSource>(
    source: &S,
    arch: &str,
    role: GlobalTensorRole,
) -> Option<Vec<f32>> {
    resolve_global(source, arch, role).ok()
}

/// Check if a global tensor exists in the source.
pub(crate) fn has_global<S: TensorSource>(source: &S, arch: &str, role: GlobalTensorRole) -> bool {
    let arch_key = normalize_architecture(arch);

    for name in global_names(arch_key, role) {
        if source.has_tensor(name) {
            return true;
        }
    }
    for name in global_fallback_names(role) {
        if source.has_tensor(name) {
            return true;
        }
    }
    // Bare names
    for name in global_names(arch_key, role) {
        if let Some(bare) = name.strip_prefix("model.") {
            if source.has_tensor(bare) {
                return true;
            }
        }
    }
    false
}

/// Resolve a per-layer tensor from a source using contract-driven name lookup.
///
/// Substitutes `{n}` in templates with the layer index.
///
/// # Errors
///
/// Returns error listing all attempted names if tensor is not found.
pub(crate) fn resolve_layer<S: TensorSource>(
    source: &S,
    arch: &str,
    layer_idx: usize,
    role: LayerTensorRole,
) -> Result<Vec<f32>> {
    let arch_key = normalize_architecture(arch);
    let mut tried = Vec::new();

    // 1. Architecture-specific templates
    for template in layer_templates(arch_key, role) {
        let name = template.replace("{n}", &layer_idx.to_string());
        if let Ok(t) = source.get_tensor_auto(&name) {
            return Ok(t);
        }
        tried.push(name);
    }

    // 2. GGUF fallback templates
    for template in layer_fallback_templates(role) {
        let name = template.replace("{n}", &layer_idx.to_string());
        if let Ok(t) = source.get_tensor_auto(&name) {
            return Ok(t);
        }
        tried.push(name);
    }

    // 3. Bare names (strip "model." prefix)
    for template in layer_templates(arch_key, role) {
        let name = template.replace("{n}", &layer_idx.to_string());
        if let Some(bare) = name.strip_prefix("model.") {
            if let Ok(t) = source.get_tensor_auto(bare) {
                return Ok(t);
            }
            tried.push(bare.to_string());
        }
    }

    // Diagnostic error
    let available = source.tensor_names();
    let sample: Vec<&str> = available.iter().take(5).copied().collect();
    Err(RealizarError::UnsupportedOperation {
        operation: "tensor_names::resolve_layer".to_string(),
        reason: format!(
            "Tensor not found for {:?} layer {} (arch='{}'). Tried: {:?}. \
             Available tensors ({} total): {:?}{}",
            role,
            layer_idx,
            arch,
            tried,
            available.len(),
            sample,
            if available.len() > 5 { ", ..." } else { "" }
        ),
    })
}

/// Resolve a per-layer tensor, returning None if not found.
pub(crate) fn resolve_layer_optional<S: TensorSource>(
    source: &S,
    arch: &str,
    layer_idx: usize,
    role: LayerTensorRole,
) -> Option<Vec<f32>> {
    resolve_layer(source, arch, layer_idx, role).ok()
}

/// Check if a per-layer tensor exists in the source.
pub(crate) fn has_layer<S: TensorSource>(
    source: &S,
    arch: &str,
    layer_idx: usize,
    role: LayerTensorRole,
) -> bool {
    let arch_key = normalize_architecture(arch);

    for template in layer_templates(arch_key, role) {
        let name = template.replace("{n}", &layer_idx.to_string());
        if source.has_tensor(&name) {
            return true;
        }
    }
    for template in layer_fallback_templates(role) {
        let name = template.replace("{n}", &layer_idx.to_string());
        if source.has_tensor(&name) {
            return true;
        }
    }
    // Bare names
    for template in layer_templates(arch_key, role) {
        let name = template.replace("{n}", &layer_idx.to_string());
        if let Some(bare) = name.strip_prefix("model.") {
            if source.has_tensor(bare) {
                return true;
            }
        }
    }
    false
}

/// Check if a fused tensor exists in the source.
pub(crate) fn has_fused<S: TensorSource>(
    source: &S,
    arch: &str,
    layer_idx: usize,
    role: FusedTensorRole,
) -> bool {
    let arch_key = normalize_architecture(arch);

    for template in fused_templates(arch_key, role) {
        let name = template.replace("{n}", &layer_idx.to_string());
        if source.has_tensor(&name) {
            return true;
        }
    }
    for template in fused_fallback_templates(role) {
        let name = template.replace("{n}", &layer_idx.to_string());
        if source.has_tensor(&name) {
            return true;
        }
    }
    false
}

/// Resolve a fused tensor (e.g., combined QKV) from a source.
///
/// Returns None if no fused tensor exists for this architecture.
pub(crate) fn resolve_fused<S: TensorSource>(
    source: &S,
    arch: &str,
    layer_idx: usize,
    role: FusedTensorRole,
) -> Option<Vec<f32>> {
    let arch_key = normalize_architecture(arch);

    // 1. Architecture-specific templates
    for template in fused_templates(arch_key, role) {
        let name = template.replace("{n}", &layer_idx.to_string());
        if let Ok(t) = source.get_tensor_auto(&name) {
            return Some(t);
        }
    }

    // 2. GGUF fallback templates
    for template in fused_fallback_templates(role) {
        let name = template.replace("{n}", &layer_idx.to_string());
        if let Ok(t) = source.get_tensor_auto(&name) {
            return Some(t);
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    // FALSIFY-TNAME-004: PhiForCausalLM maps to phi2, Phi3ForCausalLM maps to phi
    #[test]
    fn test_phi_architecture_distinction() {
        assert_eq!(normalize_architecture("PhiForCausalLM"), "phi2");
        assert_eq!(normalize_architecture("Phi3ForCausalLM"), "phi");
        assert_eq!(normalize_architecture("Phi3SmallForCausalLM"), "phi");
    }

    // FALSIFY-TNAME-005: Unknown architecture defaults to llama
    #[test]
    fn test_unknown_architecture_defaults_to_llama() {
        assert_eq!(normalize_architecture("FutureArch2027"), "llama");
        assert_eq!(normalize_architecture(""), "llama");
        assert_eq!(normalize_architecture("SomeRandomModel"), "llama");
    }

    // FALSIFY-TNAME-006: GPT-2 bare names
    #[test]
    fn test_gpt2_global_names() {
        let names = global_names("gpt2", GlobalTensorRole::Embedding);
        assert!(names.contains(&"wte.weight"));
        // GPT-2 names should NOT have "model." prefix
        assert!(!names.iter().any(|n| n.starts_with("model.")));
    }

    // FALSIFY-TNAME-007: Fused QKV for GPT-2
    #[test]
    fn test_gpt2_fused_qkv() {
        let fused = fused_templates("gpt2", FusedTensorRole::FusedQkv);
        assert!(!fused.is_empty(), "GPT-2 should have fused QKV templates");

        // GPT-2 should NOT have separate Q/K/V templates
        let q_templates = layer_templates("gpt2", LayerTensorRole::QProjWeight);
        assert!(
            q_templates.is_empty(),
            "GPT-2 should not have separate Q template"
        );
    }

    // FALSIFY-TNAME-001: Architecture map includes all referenced keys
    #[test]
    fn test_architecture_map_completeness() {
        let known_archs = [
            "llama", "qwen2", "qwen3", "mistral", "gemma", "phi", "phi2", "deepseek", "gpt2",
            "gpt_neox", "bert", "openelm", "falcon", "stablelm",
        ];
        for arch in known_archs {
            // Verify that at least one of the global roles has entries for this arch
            let embed = global_names(arch, GlobalTensorRole::Embedding);
            let norm = global_names(arch, GlobalTensorRole::OutputNormWeight);
            assert!(
                !embed.is_empty() || !norm.is_empty(),
                "Architecture '{}' has no global names defined",
                arch
            );
        }
    }

    // FALSIFY-TNAME-003: Required roles have fallbacks
    #[test]
    fn test_required_roles_have_fallbacks() {
        // Embedding is required — must have fallback
        let fb = global_fallback_names(GlobalTensorRole::Embedding);
        assert!(!fb.is_empty(), "Embedding must have fallback names");

        // Output norm is required — must have fallback
        let fb = global_fallback_names(GlobalTensorRole::OutputNormWeight);
        assert!(!fb.is_empty(), "OutputNormWeight must have fallback names");

        // Layer roles: attn_norm, q_proj, etc. must have fallbacks
        let fb = layer_fallback_templates(LayerTensorRole::AttnNormWeight);
        assert!(
            !fb.is_empty(),
            "AttnNormWeight must have fallback templates"
        );

        let fb = layer_fallback_templates(LayerTensorRole::FfnUpWeight);
        assert!(!fb.is_empty(), "FfnUpWeight must have fallback templates");
    }

    // Verify LLaMA names match the existing hardcoded values
    #[test]
    fn test_llama_names_backward_compatible() {
        let embed = global_names("llama", GlobalTensorRole::Embedding);
        assert!(embed.contains(&"model.embed_tokens.weight"));

        let norm = global_names("llama", GlobalTensorRole::OutputNormWeight);
        assert!(norm.contains(&"model.norm.weight"));

        let q = layer_templates("llama", LayerTensorRole::QProjWeight);
        assert!(q.contains(&"model.layers.{n}.self_attn.q_proj.weight"));
    }

    // Verify Phi-2 uses fc1/fc2 instead of gate_proj/up_proj
    #[test]
    fn test_phi2_mlp_names() {
        let up = layer_templates("phi2", LayerTensorRole::FfnUpWeight);
        assert!(
            up.iter().any(|t| t.contains("fc1")),
            "Phi-2 should use fc1 for FFN up: {:?}",
            up
        );

        let down = layer_templates("phi2", LayerTensorRole::FfnDownWeight);
        assert!(
            down.iter().any(|t| t.contains("fc2")),
            "Phi-2 should use fc2 for FFN down: {:?}",
            down
        );

        let gate = layer_templates("phi2", LayerTensorRole::FfnGateWeight);
        assert!(gate.is_empty(), "Phi-2 should have no gate projection");
    }

    // Verify Phi-2 output norm uses final_layernorm
    #[test]
    fn test_phi2_output_norm() {
        let norm = global_names("phi2", GlobalTensorRole::OutputNormWeight);
        assert!(
            norm.iter().any(|n| n.contains("final_layernorm")),
            "Phi-2 should use final_layernorm: {:?}",
            norm
        );
    }

    // Verify GPT-NeoX fused QKV
    #[test]
    fn test_gpt_neox_fused_qkv() {
        let fused = fused_templates("gpt_neox", FusedTensorRole::FusedQkv);
        assert!(
            fused.iter().any(|t| t.contains("query_key_value")),
            "GPT-NeoX should have query_key_value fused template"
        );
    }

    // Verify all HF model class names map correctly
    #[test]
    fn test_hf_class_name_mapping() {
        assert_eq!(normalize_architecture("LlamaForCausalLM"), "llama");
        assert_eq!(normalize_architecture("Qwen2ForCausalLM"), "qwen2");
        assert_eq!(normalize_architecture("Qwen3ForCausalLM"), "qwen3");
        assert_eq!(normalize_architecture("MistralForCausalLM"), "mistral");
        assert_eq!(normalize_architecture("GemmaForCausalLM"), "gemma");
        assert_eq!(normalize_architecture("GPT2LMHeadModel"), "gpt2");
        assert_eq!(normalize_architecture("GPTNeoXForCausalLM"), "gpt_neox");
        assert_eq!(normalize_architecture("BertModel"), "bert");
        assert_eq!(normalize_architecture("FalconForCausalLM"), "falcon");
    }
}