dynamo_llm/gguf.rs
1// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4// Adapted from mistral.rs
5//
6// MIT License
7//
8// Copyright (c) 2025 Eric Buehler
9//
10// Permission is hereby granted, free of charge, to any person obtaining a copy
11// of this software and associated documentation files (the "Software"), to deal
12// in the Software without restriction, including without limitation the rights
13// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14// copies of the Software, and to permit persons to whom the Software is
15// furnished to do so, subject to the following conditions:
16//
17// The above copyright notice and this permission notice shall be included in all
18// copies or substantial portions of the Software.
19//
20// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26// SOFTWARE.
27
28mod content;
29mod gguf_metadata;
30mod gguf_tokenizer;
31use strum::EnumString;
32
33use anyhow::{Context, Result};
34pub(crate) use content::Content;
35pub(crate) use gguf_metadata::ContentConfig;
36pub use gguf_metadata::ModelConfigLike;
37pub(crate) use gguf_tokenizer::convert_gguf_to_hf_tokenizer;
38
39use std::str::FromStr;
40
41pub const GGUF_MULTI_FILE_DELIMITER: &str = " ";
42
43#[derive(Debug, EnumString, Clone, Copy, strum::Display)]
44#[strum(serialize_all = "lowercase")]
45pub enum GGUFArchitecture {
46 Llama,
47 Mpt,
48 Gptneox,
49 Gptj,
50 Gpt2,
51 Bloom,
52 Falcon,
53 Mamba,
54 Rwkv,
55 Phi2,
56 Phi3,
57 Starcoder2,
58 Qwen2,
59 Qwen3,
60 Gemma3,
61 Granite,
62}
63
64// Wraps from_str() for some convenience:
65// - Case-insensitive variant matching (TODO: is this desirable?)
66// - Customized error until potential upstream support: https://github.com/Peternator7/strum/issues/332
67impl GGUFArchitecture {
68 pub fn from_value<T: AsRef<str> + std::fmt::Display>(value: T) -> Result<Self> {
69 Self::from_str(&value.as_ref().to_ascii_lowercase())
70 .with_context(|| format!("Unknown GGUF architecture `{value}`"))
71 .map_err(anyhow::Error::msg)
72 }
73}