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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Parsed GGUF file layout: metadata KV store + tensor directory.
//!
//! This module is file-agnostic: it operates on an already-loaded
//! `&[u8]` slice (typically the full contents of a `.gguf` file). It
//! does **not** perform any neural-network math — only header parsing,
//! bounds-checked tensor-directory extraction, and metadata coercion.

use std::collections::HashMap;

use super::cursor::{
    GGUF_MAGIC, GGUF_VERSION, GgufCursor, invalid_layout, is_signed_layout_type, unsupported,
};
use super::limits::{ParseLimits, align_up_checked, u64_to_usize};
use super::tensor::{DType, Tensor};
use crate::error::{HostSizeField, ParseLimitKind, ParserError, Result};

/// Parsed GGUF metadata key-value store.
///
/// GGUF stores arbitrary scalar KV pairs. We keep strings and numeric
/// values in typed maps; signed integers are additionally kept in
/// `signed_numerics` so callers can distinguish bit-preserved negatives
/// from large unsigned `UINT64` values. Array values are skipped (their
/// byte contents remain available via the original file buffer if needed).
#[derive(Debug, Clone, Default)]
pub struct GgufMetadata {
    /// String-typed KV pairs (e.g. `general.architecture = "olmoe"`).
    pub strings: HashMap<String, String>,
    /// Numeric-typed KV pairs coerced to `u64`
    /// (e.g. `olmoe.expert_count = 64`).
    pub numerics: HashMap<String, u64>,
    /// Signed integer KV pairs coerced to `i64`, keyed separately so
    /// `numeric()` can reject negatives without rejecting large unsigned
    /// `UINT64` metadata.
    pub(crate) signed_numerics: HashMap<String, i64>,
    /// `f32`-typed KV pairs.
    pub floats_32: HashMap<String, f32>,
    /// `f64`-typed KV pairs.
    pub floats_64: HashMap<String, f64>,
}

impl GgufMetadata {
    /// Convenience: architecture string (`general.architecture`) or `"unknown"`.
    pub fn architecture(&self) -> &str {
        self.strings
            .get("general.architecture")
            .map(String::as_str)
            .unwrap_or("unknown")
    }

    /// Convenience: numeric KV coerced to `usize`.
    ///
    /// Signed integer KVs are checked first: a negative value returns
    /// `None`. Unsigned `UINT64` values larger than `i64::MAX` are still
    /// accepted as long as they fit in `usize`.
    pub fn numeric(&self, key: &str) -> Option<usize> {
        if let Some(&s) = self.signed_numerics.get(key) {
            return usize::try_from(s).ok();
        }
        let &v = self.numerics.get(key)?;
        usize::try_from(v).ok()
    }

    /// Convenience: quantization label.
    ///
    /// Prefers the string KV `general.quantization_type`. When that is
    /// missing, falls back to the **current** `general.file_type` numeric
    /// (GGUF enum): `0 → "F32"`, `1 → "F16"`, otherwise `"GGUF(n)"`.
    /// Returns `"unknown"` when neither is present. Derived at call time
    /// so `Default` + public map edits stay consistent.
    pub fn quantization(&self) -> String {
        if let Some(s) = self.strings.get("general.quantization_type") {
            return s.clone();
        }
        // If a signed value was stored, reject negatives rather than
        // rendering them as huge unsigned labels.
        if let Some(&s) = self.signed_numerics.get("general.file_type") {
            return match s {
                0 => "F32".into(),
                1 => "F16".into(),
                n if n >= 0 => format!("GGUF({n})"),
                _ => "unknown".into(),
            };
        }
        match self.numerics.get("general.file_type").copied() {
            Some(0) => "F32".into(),
            Some(1) => "F16".into(),
            Some(n) => format!("GGUF({n})"),
            None => "unknown".into(),
        }
    }

    /// Convenience: numeric KV coerced to `usize`, looking up
    /// `{architecture}.{key}` (e.g. `olmoe.block_count`).
    ///
    /// Returns `None` if the architecture is unknown or the key is missing.
    pub fn arch_numeric(&self, key: &str) -> Option<usize> {
        let arch = self.architecture();
        if arch == "unknown" {
            return None;
        }
        let full_key = format!("{arch}.{key}");
        self.numeric(&full_key)
    }

    /// Convenience: block count from `{architecture}.block_count`.
    pub fn block_count(&self) -> Option<usize> {
        self.arch_numeric("block_count")
    }

    /// Convenience: expert count from `{architecture}.expert_count`
    /// (some models use `num_experts` instead).
    pub fn expert_count(&self) -> Option<usize> {
        self.arch_numeric("expert_count")
            .or_else(|| self.arch_numeric("num_experts"))
    }

    /// Convenience: number of experts used per token from
    /// `{architecture}.expert_used_count` (some models use
    /// `num_experts_per_tok`).
    pub fn expert_used_count(&self) -> Option<usize> {
        self.arch_numeric("expert_used_count")
            .or_else(|| self.arch_numeric("num_experts_per_tok"))
    }

    /// Convenience: embedding length from `{architecture}.embedding_length`.
    pub fn embedding_length(&self) -> Option<usize> {
        self.arch_numeric("embedding_length")
    }

    /// Convenience: attention head count from
    /// `{architecture}.attention.head_count`.
    pub fn head_count(&self) -> Option<usize> {
        self.arch_numeric("attention.head_count")
    }

    /// Generic string metadata lookup.
    pub fn string(&self, key: &str) -> Option<&str> {
        self.strings.get(key).map(String::as_str)
    }

    /// Generic f32 metadata lookup.
    pub fn float32(&self, key: &str) -> Option<f32> {
        self.floats_32.get(key).copied()
    }

    /// Generic f64 metadata lookup.
    pub fn float64(&self, key: &str) -> Option<f64> {
        self.floats_64.get(key).copied()
    }
}

/// Fully-parsed GGUF checkpoint layout.
///
/// Owns the raw file bytes (loaded into a `Vec<u8>` by [`load_gguf`])
/// and all derived tensor directory + metadata information. Callers use
/// the [`Tensor`] handles + [`GgufLayout::tensor_bytes`] to obtain raw
/// byte slices for each weight.
///
/// [`load_gguf`]: super::load_gguf
#[derive(Debug)]
pub struct GgufLayout {
    /// Path the checkpoint was loaded from (kept for error messages).
    pub path: String,
    /// Parsed KV metadata.
    pub metadata: GgufMetadata,
    /// Name -> tensor directory entry.
    pub tensors: HashMap<String, Tensor>,
    /// Byte alignment specified by the file (default 32).
    pub alignment: usize,
    /// Absolute byte offset within `bytes` where tensor payloads begin.
    pub tensor_data_offset: usize,
    /// Full file contents. Tensor payloads live at `bytes[tensor.absolute_offset ..]`.
    pub bytes: Vec<u8>,
}

impl GgufLayout {
    /// Return a borrowed slice of the raw tensor payload bytes.
    pub fn tensor_bytes<'a>(&'a self, tensor: &Tensor) -> Result<&'a [u8]> {
        tensor_payload_bytes(&self.bytes, &self.path, tensor)
    }

    /// Lookup a tensor by exact name.
    pub fn tensor(&self, name: &str) -> Result<&Tensor> {
        self.tensors
            .get(name)
            .ok_or_else(|| ParserError::MissingTensor {
                name: name.to_owned(),
                path: self.path.clone(),
            })
    }

    /// Find all tensors whose name ends with the given suffix. Useful
    /// for locating per-block MoE tensors like `ffn_gate_exps.weight`.
    pub fn find_tensors_with_suffix<'a>(&'a self, suffix: &str) -> Vec<&'a Tensor> {
        let mut matches: Vec<&Tensor> = self
            .tensors
            .values()
            .filter(|t| t.name.ends_with(suffix))
            .collect();
        matches.sort_unstable_by_key(|t| tensor_block_sort_key(&t.name));
        matches
    }
}

/// Bounds-checked tensor payload slice from any byte source (owned buffer or mmap).
pub(crate) fn tensor_payload_bytes<'a>(
    bytes: &'a [u8],
    path: &str,
    tensor: &Tensor,
) -> Result<&'a [u8]> {
    let start = tensor.absolute_offset;
    let end = start.checked_add(tensor.byte_len).ok_or_else(|| {
        invalid_layout(
            path,
            format!("tensor '{}' byte-length overflow", tensor.name),
        )
    })?;
    if end > bytes.len() {
        return Err(invalid_layout(
            path,
            format!(
                "tensor '{}' extends beyond mapped file ({end} > {})",
                tensor.name,
                bytes.len()
            ),
        ));
    }
    Ok(&bytes[start..end])
}

struct LayoutHeader {
    tensor_count: usize,
    kv_count: usize,
}

/// Parse the GGUF header + KV metadata + tensor directory out of a
/// byte slice. Does not validate payload bytes, only directory offsets.
pub(crate) fn parse_layout(
    bytes: &[u8],
    path: &str,
    limits: ParseLimits,
) -> Result<(GgufMetadata, HashMap<String, Tensor>, usize, usize)> {
    let mut cursor = GgufCursor::with_limits(bytes, path, limits);
    let header = read_layout_header(&mut cursor, path)?;
    let (alignment, metadata) = read_metadata_section(&mut cursor, path, header.kv_count)?;
    let mut tensors = read_tensor_directory(&mut cursor, path, header.tensor_count)?;
    let tensor_data_offset =
        finalize_tensor_offsets(&mut tensors, cursor.offset(), alignment, bytes.len(), path)?;
    Ok((metadata, tensors, alignment, tensor_data_offset))
}

fn read_layout_header(cursor: &mut GgufCursor<'_>, path: &str) -> Result<LayoutHeader> {
    validate_gguf_header(cursor, path)?;
    let limits = cursor.limits();
    let tensor_count = limits.bounded_usize(
        path,
        ParseLimitKind::TensorCount,
        HostSizeField::TensorCount,
        cursor.read_u64()?,
    )?;
    let kv_count = limits.bounded_usize(
        path,
        ParseLimitKind::KvCount,
        HostSizeField::KvCount,
        cursor.read_u64()?,
    )?;
    Ok(LayoutHeader {
        tensor_count,
        kv_count,
    })
}

fn validate_gguf_header(cursor: &mut GgufCursor<'_>, path: &str) -> Result<()> {
    let magic = cursor.read_exact(4)?;
    if magic != GGUF_MAGIC {
        return Err(unsupported(
            path,
            format!("unrecognised GGUF magic bytes: {magic:?}"),
        ));
    }

    let version = cursor.read_u32()?;
    if version != GGUF_VERSION {
        return Err(unsupported(
            path,
            format!("unsupported GGUF version {version}; expected {GGUF_VERSION}"),
        ));
    }

    Ok(())
}

fn read_metadata_section(
    cursor: &mut GgufCursor<'_>,
    path: &str,
    kv_count: usize,
) -> Result<(usize, GgufMetadata)> {
    let mut alignment: usize = 32;
    let mut metadata = GgufMetadata::default();
    cursor.begin_metadata_section();

    for _ in 0..kv_count {
        let key = cursor.read_string()?;
        let value_type = cursor.read_u32()?;
        if key == "general.alignment" {
            // Layout-critical: reject signed negatives (do not wrap to huge usize)
            // and require a positive power of two.
            alignment = cursor.read_nonneg_layout_usize(value_type)?;
            if alignment == 0 || !alignment.is_power_of_two() {
                return Err(invalid_layout(
                    path,
                    format!("general.alignment must be a positive power of 2, got {alignment}"),
                ));
            }
        } else {
            capture_kv(cursor, &mut metadata, key, value_type)?;
        }
    }

    cursor.end_metadata_section();
    Ok((alignment, metadata))
}

fn read_tensor_directory(
    cursor: &mut GgufCursor<'_>,
    path: &str,
    tensor_count: usize,
) -> Result<HashMap<String, Tensor>> {
    let mut tensors = HashMap::with_capacity(tensor_count);
    for _ in 0..tensor_count {
        let tensor = read_tensor_entry(cursor, path)?;
        // A HashMap cannot report a duplicate after `insert` (it silently
        // last-wins), so reject the collision before inserting.
        if tensors.contains_key(&tensor.name) {
            return Err(invalid_layout(
                path,
                format!("duplicate tensor name '{}' in directory", tensor.name),
            ));
        }
        tensors.insert(tensor.name.clone(), tensor);
    }
    Ok(tensors)
}

fn read_tensor_entry(cursor: &mut GgufCursor<'_>, path: &str) -> Result<Tensor> {
    let name = cursor.read_string()?;
    let dims = read_tensor_dims(cursor, path, &name)?;
    let ggml_type = cursor.read_u32()?;
    let relative_offset = u64_to_usize(cursor.read_u64()?, HostSizeField::RelativeOffset, path)?;
    let dtype = DType::from_ggml_type(ggml_type);
    let n_elements = tensor_element_count(&dims, &name, path)?;
    validate_blocked_inner_dim(dtype, &dims, &name, path)?;
    let byte_len = tensor_byte_len(dtype, ggml_type, n_elements, &name, path)?;

    Ok(Tensor {
        name,
        dims,
        dtype,
        ggml_type,
        n_elements,
        byte_len,
        relative_offset,
        absolute_offset: 0,
    })
}

/// Blocked quant layouts pack along the **innermost** GGUF dim (`dims[0]`).
/// Total element count alone can accept shapes that cannot form valid blocks
/// per row (e.g. Q4_0 with dims `[16, 2]` → 32 elems but row len 16).
fn validate_blocked_inner_dim(dtype: DType, dims: &[usize], name: &str, path: &str) -> Result<()> {
    let Some(block) = dtype.quant_block_size() else {
        return Ok(());
    };
    let Some(&inner) = dims.first() else {
        return Ok(());
    };
    if inner.is_multiple_of(block) {
        return Ok(());
    }
    Err(invalid_layout(
        path,
        format!(
            "tensor '{name}' innermost dim {inner} is not divisible by \
             quant block size {block} for dtype {}",
            dtype.label()
        ),
    ))
}

fn read_tensor_dims(cursor: &mut GgufCursor<'_>, path: &str, _name: &str) -> Result<Vec<usize>> {
    let n_dims_raw = u64::from(cursor.read_u32()?);
    let n_dims = cursor.limits().bounded_usize(
        path,
        ParseLimitKind::TensorRank,
        HostSizeField::TensorRank,
        n_dims_raw,
    )?;

    let mut dims = Vec::with_capacity(n_dims);
    for _ in 0..n_dims {
        dims.push(u64_to_usize(
            cursor.read_u64()?,
            HostSizeField::TensorDim,
            path,
        )?);
    }
    Ok(dims)
}

fn tensor_element_count(dims: &[usize], name: &str, path: &str) -> Result<usize> {
    dims.iter()
        .try_fold(1usize, |acc, &d| acc.checked_mul(d))
        .ok_or_else(|| invalid_layout(path, format!("tensor '{name}' element count overflow")))
}

fn tensor_byte_len(
    dtype: DType,
    ggml_type: u32,
    n_elements: usize,
    name: &str,
    path: &str,
) -> Result<usize> {
    dtype.byte_len_for_elements(n_elements).ok_or_else(|| {
        invalid_layout(
            path,
            format!(
                "tensor '{name}' (ggml_type={ggml_type}) has unknown byte-length for {n_elements} elements",
            ),
        )
    })
}

/// Resolve each tensor's absolute offset and enforce the tensor-directory
/// packing contract.
///
/// Every tensor payload range `[absolute_offset, absolute_offset + byte_len)`
/// must lie fully within the file/mapping (`total_len`) and must not overlap
/// any other tensor's range. Gaps between consecutive ranges are permitted
/// (they are normal alignment padding), so contiguity is not required.
/// Zero-length tensors (`byte_len == 0`) occupy no bytes: their
/// `absolute_offset` is still range-checked against `total_len`, but they are
/// excluded from the overlap comparison because an empty range cannot overlap.
///
/// This is the semantic directory contract (duplicate names are rejected
/// earlier in [`read_tensor_directory`]). The RM-1358 offset-wrap guard
/// (`tensor_data_offset + relative_offset` via `checked_add`) is a separate,
/// preserved concern.
fn finalize_tensor_offsets(
    tensors: &mut HashMap<String, Tensor>,
    cursor_offset: usize,
    alignment: usize,
    total_len: usize,
    path: &str,
) -> Result<usize> {
    let tensor_data_offset = align_up_checked(cursor_offset, alignment, path)?;
    for tensor in tensors.values_mut() {
        tensor.absolute_offset = tensor_data_offset
            .checked_add(tensor.relative_offset)
            .ok_or_else(|| {
                invalid_layout(
                    path,
                    format!("tensor '{}' absolute offset overflow", tensor.name),
                )
            })?;
        let end = tensor
            .absolute_offset
            .checked_add(tensor.byte_len)
            .ok_or_else(|| {
                invalid_layout(
                    path,
                    format!("tensor '{}' byte-length overflow", tensor.name),
                )
            })?;
        if end > total_len {
            return Err(invalid_layout(
                path,
                format!(
                    "tensor '{}' extends beyond file ({end} > {total_len})",
                    tensor.name
                ),
            ));
        }
    }
    check_no_overlap(tensors, path)?;
    Ok(tensor_data_offset)
}

/// Reject overlapping tensor payload ranges. Zero-length tensors are skipped
/// (an empty range cannot overlap). Gaps between ranges are allowed.
fn check_no_overlap(tensors: &HashMap<String, Tensor>, path: &str) -> Result<()> {
    let mut ranges: Vec<(&str, usize, usize)> = tensors
        .values()
        .filter(|t| t.byte_len > 0)
        .map(|t| (t.name.as_str(), t.absolute_offset, t.byte_len))
        .collect();
    // Sort by start offset, tie-break by name for deterministic messages.
    ranges.sort_unstable_by(|a, b| a.1.cmp(&b.1).then_with(|| a.0.cmp(b.0)));
    for window in ranges.windows(2) {
        let (prev_name, prev_start, prev_len) = window[0];
        let (name, start, _) = window[1];
        // Ranges are sorted by start; `prev_start + prev_len` cannot overflow
        // because both are already bounded by `total_len`.
        if start < prev_start + prev_len {
            return Err(invalid_layout(
                path,
                format!("tensors '{prev_name}' and '{name}' have overlapping payloads"),
            ));
        }
    }
    Ok(())
}

fn capture_kv(
    cursor: &mut GgufCursor<'_>,
    metadata: &mut GgufMetadata,
    key: String,
    value_type: u32,
) -> Result<()> {
    use super::cursor::{
        GGUF_VALUE_TYPE_BOOL, GGUF_VALUE_TYPE_FLOAT32, GGUF_VALUE_TYPE_FLOAT64,
        GGUF_VALUE_TYPE_INT8, GGUF_VALUE_TYPE_INT16, GGUF_VALUE_TYPE_INT32, GGUF_VALUE_TYPE_INT64,
        GGUF_VALUE_TYPE_STRING, GGUF_VALUE_TYPE_UINT8, GGUF_VALUE_TYPE_UINT16,
        GGUF_VALUE_TYPE_UINT32, GGUF_VALUE_TYPE_UINT64,
    };
    match value_type {
        GGUF_VALUE_TYPE_UINT8
        | GGUF_VALUE_TYPE_INT8
        | GGUF_VALUE_TYPE_UINT16
        | GGUF_VALUE_TYPE_INT16
        | GGUF_VALUE_TYPE_UINT32
        | GGUF_VALUE_TYPE_INT32
        | GGUF_VALUE_TYPE_UINT64
        | GGUF_VALUE_TYPE_INT64
        | GGUF_VALUE_TYPE_BOOL => capture_numeric_kv(cursor, metadata, key, value_type),
        GGUF_VALUE_TYPE_FLOAT32 => capture_f32_kv(cursor, metadata, key),
        GGUF_VALUE_TYPE_FLOAT64 => capture_f64_kv(cursor, metadata, key),
        GGUF_VALUE_TYPE_STRING => capture_string_kv(cursor, metadata, key),
        _ => capture_skipped_kv(cursor, value_type),
    }
}

fn capture_numeric_kv(
    cursor: &mut GgufCursor<'_>,
    metadata: &mut GgufMetadata,
    key: String,
    value_type: u32,
) -> Result<()> {
    let v = cursor.read_numeric_as_u64(value_type)?;
    if is_signed_layout_type(value_type) {
        metadata.signed_numerics.insert(key.clone(), v as i64);
    }
    metadata.numerics.insert(key, v);
    Ok(())
}

fn capture_f32_kv(
    cursor: &mut GgufCursor<'_>,
    metadata: &mut GgufMetadata,
    key: String,
) -> Result<()> {
    let v = cursor.read_f32()?;
    metadata.floats_32.insert(key, v);
    Ok(())
}

fn capture_f64_kv(
    cursor: &mut GgufCursor<'_>,
    metadata: &mut GgufMetadata,
    key: String,
) -> Result<()> {
    let v = cursor.read_f64()?;
    metadata.floats_64.insert(key, v);
    Ok(())
}

fn capture_string_kv(
    cursor: &mut GgufCursor<'_>,
    metadata: &mut GgufMetadata,
    key: String,
) -> Result<()> {
    let v = cursor.read_string()?;
    metadata.strings.insert(key, v);
    Ok(())
}

fn capture_skipped_kv(cursor: &mut GgufCursor<'_>, value_type: u32) -> Result<()> {
    cursor.skip_value(value_type)
}

fn tensor_block_sort_key(name: &str) -> (usize, String) {
    let block = name
        .strip_prefix("blk.")
        .and_then(|rest| rest.split_once('.'))
        .and_then(|(idx, _)| idx.parse::<usize>().ok())
        .unwrap_or(usize::MAX);
    (block, name.to_owned())
}

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

    #[test]
    fn numeric_keeps_large_unsigned_and_rejects_negative_signed() {
        let mut meta = GgufMetadata::default();

        // A UINT64 larger than i64::MAX is still accepted when it fits in usize.
        meta.numerics.insert("big".into(), u64::MAX);
        #[cfg(target_pointer_width = "64")]
        assert_eq!(meta.numeric("big"), Some(usize::MAX));

        // A signed negative value must not wrap into a positive usize.
        meta.numerics.insert("neg".into(), (-7i64) as u64);
        meta.signed_numerics.insert("neg".into(), -7);
        assert_eq!(meta.numeric("neg"), None);
    }

    #[test]
    fn quantization_suppresses_negative_signed_file_type() {
        let mut meta = GgufMetadata::default();

        // A negative signed file_type must not render as an unsigned label.
        meta.numerics
            .insert("general.file_type".into(), (-7i64) as u64);
        meta.signed_numerics.insert("general.file_type".into(), -7);
        assert_eq!(meta.quantization(), "unknown");

        // A positive signed file_type still resolves normally.
        meta.signed_numerics.insert("general.file_type".into(), 15);
        assert_eq!(meta.quantization(), "GGUF(15)");

        // An unsigned file_type falls back to the unsigned map.
        meta.signed_numerics.remove("general.file_type");
        meta.numerics.insert("general.file_type".into(), 15);
        assert_eq!(meta.quantization(), "GGUF(15)");
    }
}