-
Notifications
You must be signed in to change notification settings - Fork 333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support multiple GGUF files #379
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd989d5
Move to gguf module
EricLBuehler 0f078b0
Add content abstraction for multiple gguf files
EricLBuehler 851a268
Fix test
EricLBuehler 6e7aa62
Allow specifying and loading multiple gguf files
EricLBuehler 83cd87c
Update docs and examples
EricLBuehler c2bed23
Merge branch 'master' into multi_gguf_files
EricLBuehler 6229d3f
Print some info
EricLBuehler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
use std::fs; | ||
|
||
use anyhow::Context; | ||
use candle_core::{ | ||
quantized::{ | ||
gguf_file::{self, Value}, | ||
QTensor, | ||
}, | ||
Device, Result, | ||
}; | ||
use indexmap::IndexMap; | ||
use tracing::info; | ||
|
||
use crate::{pipeline::GGUFArchitecture, DEBUG}; | ||
|
||
fn parse_gguf_value(value: &Value) -> String { | ||
match value { | ||
Value::Array(vs) => vs | ||
.iter() | ||
.map(parse_gguf_value) | ||
.collect::<Vec<String>>() | ||
.join(", "), | ||
Value::Bool(b) => b.to_string(), | ||
Value::F32(x) => x.to_string(), | ||
Value::F64(x) => x.to_string(), | ||
Value::I8(x) => x.to_string(), | ||
Value::I16(x) => x.to_string(), | ||
Value::I32(x) => x.to_string(), | ||
Value::I64(x) => x.to_string(), | ||
Value::String(x) => x.to_string(), | ||
Value::U8(x) => x.to_string(), | ||
Value::U16(x) => x.to_string(), | ||
Value::U32(x) => x.to_string(), | ||
Value::U64(x) => x.to_string(), | ||
} | ||
} | ||
|
||
// Internal invariant: contents and readers must be paired. | ||
/// This abstracts the files for a GGUF model and enables multiple files to be used. | ||
pub struct Content<'a, R: std::io::Seek + std::io::Read> { | ||
contents: Vec<gguf_file::Content>, | ||
readers: &'a mut [&'a mut R], | ||
arch: GGUFArchitecture, | ||
} | ||
|
||
impl<'a, R: std::io::Seek + std::io::Read> Content<'a, R> { | ||
/// Create a `Content` from a set of file readers. | ||
pub fn from_readers(readers: &'a mut [&'a mut R]) -> Result<Self> { | ||
let mut contents = Vec::new(); | ||
let n_readers = readers.len(); | ||
for reader in readers.iter_mut() { | ||
contents.push(gguf_file::Content::read(reader)?); | ||
} | ||
let n_splits = contents | ||
.iter() | ||
.filter_map(|ct| { | ||
ct.metadata | ||
.get("split.count") | ||
.map(|val| val.to_u64().unwrap()) | ||
}) | ||
.collect::<Vec<_>>(); | ||
if n_splits.len() > 1 { | ||
candle_core::bail!("Multiple contents have multiple `split.count` fields"); | ||
} | ||
#[allow(clippy::cast_possible_truncation)] | ||
if !n_splits.is_empty() && n_readers != n_splits[0] as usize { | ||
candle_core::bail!("Number of readers does not match the number of splits."); | ||
} else if n_splits.len() == 1 { | ||
info!("Model n splits: {}", n_splits[0]); | ||
} | ||
|
||
let mut arch = None; | ||
for ct in &contents { | ||
if !ct.metadata.contains_key("general.architecture") { | ||
continue; | ||
} | ||
|
||
arch = Some( | ||
ct.metadata["general.architecture"] | ||
.to_string() | ||
.context("Model metadata should have declared an architecture") | ||
.and_then(GGUFArchitecture::from_value) | ||
.unwrap(), | ||
); | ||
} | ||
let arch = arch.expect("GGUF files must specify `general.architecture`"); | ||
Ok(Self { | ||
contents, | ||
readers, | ||
arch, | ||
}) | ||
} | ||
|
||
pub fn arch(&self) -> GGUFArchitecture { | ||
self.arch | ||
} | ||
|
||
/// Retrieve a tensor, searching through each content. | ||
pub fn tensor(&mut self, name: &str, device: &Device) -> Result<QTensor> { | ||
for (ct, reader) in self.contents.iter().zip(self.readers.iter_mut()) { | ||
if let Some(tensor_info) = ct.tensor_infos.get(name) { | ||
return tensor_info.read(reader, ct.tensor_data_offset, device); | ||
} | ||
} | ||
candle_core::bail!("Cannot find tensor info for {name}") | ||
} | ||
|
||
/// Print metadata for these contents. | ||
/// This will also log tensor name, shape and dtype to `mistralrs_gguf_tensors.txt` is DEBUG is enabled. | ||
pub fn print_metadata(&self) -> anyhow::Result<()> { | ||
// Find the ct with general.architecture | ||
let mut keys = Vec::new(); | ||
let mut metadatas = Vec::new(); | ||
let mut tensors = Vec::new(); | ||
for ct in &self.contents { | ||
keys.extend(ct.metadata.keys()); | ||
metadatas.push(&ct.metadata); | ||
|
||
if DEBUG.load(std::sync::atomic::Ordering::Relaxed) { | ||
for (name, info) in &ct.tensor_infos { | ||
tensors.push(format!( | ||
"name = `{name}`, shape = {:?}, dtype = {:?}", | ||
info.shape.clone(), | ||
info.ggml_dtype | ||
)); | ||
} | ||
} | ||
} | ||
|
||
info!("Model config:"); | ||
keys.sort(); | ||
let mut output_keys = IndexMap::new(); | ||
for name in keys { | ||
if !name.contains("tokenizer") { | ||
for metadata in &metadatas { | ||
if let Some(val) = metadata.get(name) { | ||
output_keys.insert(name, parse_gguf_value(val)); | ||
} | ||
} | ||
} | ||
} | ||
for (name, val) in output_keys { | ||
println!("{name}: {val}") | ||
} | ||
|
||
if DEBUG.load(std::sync::atomic::Ordering::Relaxed) { | ||
fs::write( | ||
"mistralrs_gguf_tensors.txt", | ||
serde_json::to_string_pretty(&tensors).expect("Serialization failed."), | ||
)?; | ||
|
||
info!("Debug is enabled, wrote the names and information about each tensor to `mistralrs_gguf_tensors.txt`."); | ||
} | ||
|
||
anyhow::Ok(()) | ||
} | ||
|
||
/// Get metadata | ||
pub fn get_metadata(&self, name: &str) -> Result<&Value> { | ||
for content in &self.contents { | ||
if let Some(v) = content.metadata.get(name) { | ||
return Ok(v); | ||
} | ||
} | ||
candle_core::bail!("Cannot find metadata for {name}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod content; | ||
mod gguf_tokenizer; | ||
|
||
pub use content::Content; | ||
pub use gguf_tokenizer::{convert_gguf_to_hf_tokenizer, GgufTokenizerConversion}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have an example GGUF file where this is actually the case? Or was this a misunderstanding from referenced motivation in #380 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes: https://huggingface.co/mradermacher/dolphin-2.9-mixtral-8x22b-GGUF/tree/main
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why the sharding is necessary other than as this comment suggests, to workaround size restrictions on a file host?
Seems like it'd be more appropriate to have a tool that concatenates the files together if there's a UX issue you want to address, but I don't really see the point in adding additional complexity / maintenance to support something that should realistically be addressed outside of
mistral.rs
(runtime) 🤷♂️Similar to the tokenizer patching you added recently, these are workarounds that seem more appropriate as CLI tools to apply "fixes" 🤔