engram-parser 0.3.0

Pure-Rust GGUF + Safetensors checkpoint/tensor substrate: format-independent tensor inventory, metadata, and raw payload access for ANN/SNN research. Default builds are zero-dependency (`fs::read` into `Vec<u8>`). Optional `mmap` feature adds memmap2. Packed dequant for Q8_0/Q5_K/Q6_K/IQ3_M. Off-by-default `safetensors` feature adds manifest/discovery plus raw tensor payload access via the upstream safetensors crate. MoE per-expert weight extraction ships as a GGUF model-analysis specialization.
Documentation
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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Name/shape heuristics for MoE router and expert candidate discovery.

use super::SafetensorsTensorRecord;
use std::collections::{BTreeMap, BTreeSet};

/// Layout-family guess plus scored router/expert candidates.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SafetensorsCandidateSummary {
    /// Inferred family when both router and expert evidence is present.
    pub detected_layout_family: Option<&'static str>,
    /// Router tensor names, score-then-name ordered.
    pub router_tensors: Vec<String>,
    /// Expert tensor names in discovery order.
    pub expert_tensors: Vec<String>,
    /// Scored router candidates.
    pub router_candidates: Vec<SafetensorsRouterCandidate>,
    /// Expert groups keyed by the path prefix before `experts`/`expert`.
    pub expert_groups: Vec<SafetensorsExpertGroup>,
}

/// A tensor whose name/shape looks like a MoE router / gate.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SafetensorsRouterCandidate {
    /// Tensor name from the Safetensors header.
    pub name: String,
    /// Layer index parsed from the name, if present.
    pub layer_hint: Option<usize>,
    /// Relative shard path that owns this tensor.
    pub source_shard: String,
    /// Tensor shape from the header.
    pub shape: Vec<usize>,
    /// Heuristic score (higher is more router-like).
    pub score: u32,
    /// Human-readable score reasons.
    pub reasons: Vec<&'static str>,
}

/// A group of expert tensors that share a path prefix.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SafetensorsExpertGroup {
    /// Path prefix up to (but not including) `experts`/`expert`.
    pub group_key: String,
    /// Layer index parsed from the name, if present.
    pub layer_hint: Option<usize>,
    /// Distinct expert indices observed in this group.
    pub expert_indices: Vec<usize>,
    /// Tensor names in this group, sorted.
    pub tensor_names: Vec<String>,
    /// Shards contributing tensors to this group, sorted.
    pub source_shards: Vec<String>,
    /// Weight-kind labels (`w1`, `gate_proj`, `unknown`, …), sorted.
    pub weight_kinds: Vec<&'static str>,
}

#[derive(Debug, Clone)]
struct TensorNameParts {
    lower: String,
    path_segments: Vec<String>,
    all_segments: Vec<String>,
}

#[derive(Debug, Clone)]
struct ExpertTensorParts {
    layer_hint: Option<usize>,
    expert_index: Option<usize>,
    group_key: String,
    weight_kind: &'static str,
}

#[derive(Debug)]
struct ExpertGroupBuilder {
    group_key: String,
    layer_hint: Option<usize>,
    expert_indices: BTreeSet<usize>,
    tensor_names: Vec<String>,
    source_shards: BTreeSet<String>,
    weight_kinds: BTreeSet<&'static str>,
}

/// Discover MoE router/expert candidates from header metadata only.
#[must_use]
pub fn discover_candidates(tensors: &[SafetensorsTensorRecord]) -> SafetensorsCandidateSummary {
    let mut router_candidates = Vec::new();
    let mut expert_records = Vec::new();
    let mut family_evidence = LayoutFamilyEvidence::default();

    for tensor in tensors {
        let parts = TensorNameParts::new(&tensor.name);
        let expert_parts = expert_tensor_parts(&parts);
        let router_scoring = expert_parts
            .is_none()
            .then(|| router_candidate_score(&parts, &tensor.shape))
            .flatten();

        family_evidence.observe(&parts, router_scoring.is_some(), expert_parts.is_some());

        if let Some((score, reasons)) = router_scoring {
            router_candidates.push(SafetensorsRouterCandidate {
                name: tensor.name.clone(),
                layer_hint: extract_layer_hint(&parts),
                source_shard: tensor.source_shard.clone(),
                shape: tensor.shape.clone(),
                score,
                reasons,
            });
        }

        if let Some(expert_parts) = expert_parts {
            expert_records.push((tensor, expert_parts));
        }
    }

    router_candidates.sort_by(|left, right| {
        right
            .score
            .cmp(&left.score)
            .then(left.name.cmp(&right.name))
            .then(left.source_shard.cmp(&right.source_shard))
    });

    let expert_groups = discover_expert_groups(expert_records.iter().cloned());
    let expert_tensors = expert_records
        .iter()
        .map(|(tensor, _)| tensor.name.clone())
        .collect::<Vec<_>>();

    SafetensorsCandidateSummary {
        detected_layout_family: family_evidence.detected_layout_family(),
        router_tensors: router_candidates
            .iter()
            .map(|candidate| candidate.name.clone())
            .collect(),
        expert_tensors,
        router_candidates,
        expert_groups,
    }
}

/// Classify one tensor name/shape as expert, router, or possible router shape.
#[must_use]
pub fn classify_tensor(name: &str, shape: &[usize]) -> Vec<&'static str> {
    let parts = TensorNameParts::new(name);
    let mut labels = Vec::new();

    if expert_tensor_parts(&parts).is_some() {
        labels.push("moe_expert_candidate");
    } else if router_candidate_score(&parts, shape).is_some() {
        labels.push("moe_router_candidate");
    } else if router_shape_hint(shape) {
        labels.push("possible_moe_router_shape");
    }

    labels
}

impl TensorNameParts {
    fn new(name: &str) -> Self {
        let lower = name.to_ascii_lowercase();
        let path_segments = split_name(&lower, &['.', '/']);
        let all_segments = split_name(&lower, &['.', '/', '_']);
        Self {
            lower,
            path_segments,
            all_segments,
        }
    }

    fn has_path_segment(&self, needle: &str) -> bool {
        self.path_segments.iter().any(|segment| segment == needle)
    }

    fn has_any_segment(&self, needle: &str) -> bool {
        self.all_segments.iter().any(|segment| segment == needle)
    }
}

#[derive(Debug, Default)]
struct LayoutFamilyEvidence {
    seen_phimoe: bool,
    seen_granitemoe: bool,
    seen_afmoe: bool,
    seen_lfm2_moe: bool,
    seen_deepseek_v3_family: bool,
    has_router: bool,
    has_expert: bool,
}

impl LayoutFamilyEvidence {
    fn observe(&mut self, parts: &TensorNameParts, is_router: bool, is_expert: bool) {
        self.seen_phimoe |= parts.lower.contains("phimoe") || parts.lower.contains("phi_moe");
        self.seen_granitemoe |=
            parts.lower.contains("granitemoe") || parts.lower.contains("granite_moe");
        self.seen_afmoe |= parts.lower.contains("afmoe") || parts.lower.contains("af_moe");
        self.seen_lfm2_moe |= parts.lower.contains("lfm2_moe") || parts.lower.contains("lfm2moe");
        self.seen_deepseek_v3_family |=
            parts.lower.contains("moonlight") || parts.lower.contains("block_sparse_moe");
        self.has_router |= is_router;
        self.has_expert |= is_expert;
    }

    fn detected_layout_family(&self) -> Option<&'static str> {
        if !(self.has_router && self.has_expert) {
            return None;
        }
        if self.seen_phimoe {
            return Some("phimoe");
        }
        if self.seen_granitemoe {
            return Some("granitemoe");
        }
        if self.seen_afmoe {
            return Some("afmoe");
        }
        if self.seen_lfm2_moe {
            return Some("lfm2_moe");
        }
        if self.seen_deepseek_v3_family {
            return Some("deepseek_v3_family");
        }
        Some("generic_moe")
    }
}

fn router_candidate_score(
    parts: &TensorNameParts,
    shape: &[usize],
) -> Option<(u32, Vec<&'static str>)> {
    let lower = parts.lower.as_str();
    let mut score = 0u32;
    let mut reasons = Vec::new();
    let has_moe = has_moe_context(parts);
    let has_router_shape = router_shape_hint(shape);

    if lower.contains("router") {
        score += 80;
        reasons.push("router_name");
    }
    if lower.contains("gate_inp") {
        score += 80;
        reasons.push("gate_inp_name");
    }
    if lower.contains("block_sparse_moe.gate.weight")
        || lower.contains("block_sparse_moe/gate/weight")
    {
        score += 90;
        reasons.push("deepseek_v3_family_block_sparse_moe_gate");
    }
    if lower.contains("moe.gate.weight")
        || lower.contains("moe/gate/weight")
        || lower.contains("feed_forward.gate.weight")
        || lower.contains("feed_forward/gate/weight")
        || lower.contains("ffn.gate.weight")
        || lower.contains("ffn/gate/weight")
    {
        score += 70;
        reasons.push("moe_gate_name");
    }
    if (lower.contains("gating.weight") || lower.contains("gating/weight"))
        && (has_moe || has_router_shape)
    {
        score += 65;
        reasons.push("gating_name");
    }

    let gate_weight_suffix = lower.ends_with(".gate.weight") || lower.ends_with("/gate/weight");
    if gate_weight_suffix && has_moe {
        score += 60;
        reasons.push("moe_context_gate_weight");
    } else if gate_weight_suffix && has_router_shape {
        score += 45;
        reasons.push("shape_guarded_gate_weight");
    }

    if has_router_shape {
        score += 30;
        reasons.push("router_shape");
    }

    (!reasons.is_empty() && score >= 60).then_some((score, reasons))
}

fn router_shape_hint(shape: &[usize]) -> bool {
    shape.len() == 2
        && shape
            .iter()
            .copied()
            .min()
            .is_some_and(|v| (2..=512).contains(&v))
        && shape.iter().copied().max().is_some_and(|v| v >= 512)
}

fn has_moe_context(parts: &TensorNameParts) -> bool {
    parts.lower.contains("moe")
        || parts.has_path_segment("experts")
        || parts.has_path_segment("expert")
}

fn discover_expert_groups<'a>(
    expert_records: impl Iterator<Item = (&'a SafetensorsTensorRecord, ExpertTensorParts)>,
) -> Vec<SafetensorsExpertGroup> {
    let mut groups: BTreeMap<String, ExpertGroupBuilder> = BTreeMap::new();
    for (tensor, parts) in expert_records {
        groups
            .entry(parts.group_key.clone())
            .or_insert_with(|| ExpertGroupBuilder::new(parts.group_key.clone(), parts.layer_hint))
            .push(tensor, parts.expert_index, parts.weight_kind);
    }

    groups
        .into_values()
        .map(ExpertGroupBuilder::build)
        .collect()
}

impl ExpertGroupBuilder {
    fn new(group_key: String, layer_hint: Option<usize>) -> Self {
        Self {
            group_key,
            layer_hint,
            expert_indices: BTreeSet::new(),
            tensor_names: Vec::new(),
            source_shards: BTreeSet::new(),
            weight_kinds: BTreeSet::new(),
        }
    }

    fn push(
        &mut self,
        tensor: &SafetensorsTensorRecord,
        expert_index: Option<usize>,
        weight_kind: &'static str,
    ) {
        if let Some(expert_index) = expert_index {
            self.expert_indices.insert(expert_index);
        }
        self.tensor_names.push(tensor.name.clone());
        self.source_shards.insert(tensor.source_shard.clone());
        self.weight_kinds.insert(weight_kind);
    }

    fn build(mut self) -> SafetensorsExpertGroup {
        self.tensor_names.sort();
        SafetensorsExpertGroup {
            group_key: self.group_key,
            layer_hint: self.layer_hint,
            expert_indices: self.expert_indices.into_iter().collect(),
            tensor_names: self.tensor_names,
            source_shards: self.source_shards.into_iter().collect(),
            weight_kinds: self.weight_kinds.into_iter().collect(),
        }
    }
}

fn expert_tensor_parts(parts: &TensorNameParts) -> Option<ExpertTensorParts> {
    if !has_moe_context(parts) {
        return None;
    }
    let group_prefix = expert_group_prefix(parts)?;
    Some(ExpertTensorParts {
        layer_hint: extract_layer_hint(parts),
        expert_index: extract_expert_index(parts),
        group_key: group_prefix,
        weight_kind: expert_weight_kind(parts).unwrap_or("unknown"),
    })
}

fn expert_weight_kind(parts: &TensorNameParts) -> Option<&'static str> {
    [
        ("gate_proj", "gate_proj"),
        ("up_proj", "up_proj"),
        ("down_proj", "down_proj"),
        ("ffn_up", "ffn_up"),
        ("ffn_down", "ffn_down"),
        ("linear_1", "linear_1"),
        ("linear_2", "linear_2"),
        ("linear_3", "linear_3"),
        ("fc1", "fc1"),
        ("fc2", "fc2"),
        ("fc3", "fc3"),
        ("w1", "w1"),
        ("w2", "w2"),
        ("w3", "w3"),
    ]
    .into_iter()
    .find_map(|(needle, kind)| has_weight_kind_segment(parts, needle).then_some(kind))
}

fn has_weight_kind_segment(parts: &TensorNameParts, needle: &str) -> bool {
    if parts.has_path_segment(needle) {
        return true;
    }
    needle.contains('_') && parts.has_any_segment(needle)
}

fn extract_layer_hint(parts: &TensorNameParts) -> Option<usize> {
    number_after_any_segment(parts, &["layers", "layer", "blocks", "block", "h"])
}

fn extract_expert_index(parts: &TensorNameParts) -> Option<usize> {
    number_after_any_segment(parts, &["experts", "expert"])
}

fn expert_group_prefix(parts: &TensorNameParts) -> Option<String> {
    for marker in ["experts", "expert"] {
        if let Some(index) = parts
            .path_segments
            .iter()
            .position(|segment| segment == marker)
        {
            if index == 0 {
                return Some(marker.to_string());
            }
            return Some(parts.path_segments[..index].join("."));
        }
    }
    None
}

fn number_after_any_segment(parts: &TensorNameParts, markers: &[&str]) -> Option<usize> {
    parts.all_segments.windows(2).find_map(|window| {
        markers
            .contains(&window[0].as_str())
            .then(|| window[1].parse::<usize>().ok())
            .flatten()
    })
}

fn split_name(name: &str, separators: &[char]) -> Vec<String> {
    name.split(separators)
        .filter(|segment| !segment.is_empty())
        .map(str::to_owned)
        .collect()
}