Skip to content

Commit

Permalink
update style and ban mutiple split
Browse files Browse the repository at this point in the history
  • Loading branch information
qinyiqun committed Jul 22, 2024
1 parent c552caa commit 259e5db
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 50 deletions.
12 changes: 11 additions & 1 deletion ggus/src/metadata/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! See <https://github.com/ggerganov/ggml/blob/master/docs/gguf.md#standardized-key-value-pairs>.
//! See <https://github.com/ggerganov/ggml/blob/master/docs/gguf.md#standardized-key-value-pairs>.
mod general;
mod llm;
Expand Down Expand Up @@ -141,6 +141,16 @@ impl<'a> GGufMetaKVPairs<'a> {
.map(|(&key, &len)| GGufMetaKV { key, len })
}

pub fn remove(&mut self, _key: &str) -> bool {
match self.indices.swap_remove_entry(_key) {
Some((_, v)) => {
self.nbytes -= v;
true
}
None => false,
}
}

fn get_typed(
&self,
name: impl AsRef<str>,
Expand Down
6 changes: 3 additions & 3 deletions xtask/src/gguf_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ impl<'a> GGufFile<'a> {
})
}

pub(crate) fn get_header(&self) -> &GGufFileHeader {
pub fn header(&self) -> &GGufFileHeader {
&self.header
}

pub(crate) fn get_meta_kvs(&self) -> &GGufMetaKVPairs<'a> {
pub fn meta_kvs(&self) -> &GGufMetaKVPairs<'a> {
&self.meta_kvs
}

pub(crate) fn get_tensors_as_indexmap(&self) -> IndexMap<ggus::GGufTensorInfo, &[u8]> {
pub fn tensors_as_indexmap(&self) -> IndexMap<ggus::GGufTensorInfo, &[u8]> {
self.tensors
.iter()
.map(move |t| (t, self.data))
Expand Down
6 changes: 3 additions & 3 deletions xtask/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ impl MergeArgs {

let kvs = files
.iter()
.flat_map(|file| file.get_meta_kvs().kvs())
.flat_map(|file| file.meta_kvs().kvs())
.filter(|kv| {
let key = kv.key();
!key.starts_with("split.") && key != "general.alignment"
})
.collect::<IndexSet<_>>();
let tensors = files
.iter()
.flat_map(|file| file.get_tensors_as_indexmap())
.flat_map(|file| file.tensors_as_indexmap())
.collect::<IndexMap<_, _>>();

let out = File::create(shards.single_file()).unwrap();
Expand All @@ -62,7 +62,7 @@ impl MergeArgs {

let align = files
.iter()
.map(|file| file.get_meta_kvs().alignment())
.map(|file| file.meta_kvs().alignment())
.max()
.unwrap();

Expand Down
68 changes: 25 additions & 43 deletions xtask/src/split.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::gguf_file::{pad, GGufError, GGufFile};
use crate::{
gguf_file::{pad, GGufError, GGufFile},
loose_shards::LooseShards,
};
use ggus::{GGufFileHeader, GGufMetaDataValueType, GGufMetaKVPairs, GGufWriter};
use std::{fs::File, path::PathBuf};

Expand Down Expand Up @@ -50,6 +53,11 @@ impl<'a> GGufFileInfo<'a> {

impl SplitArgs {
pub fn split(self) {
let shards = LooseShards::from(&*self.input);
if shards.count() > 1 {
println!("Model has already been splited");
return;
}
let file = File::open(&self.input)
.map_err(|e| {
println!("Failed to open");
Expand All @@ -60,10 +68,10 @@ impl SplitArgs {
let mmap = unsafe { memmap2::Mmap::map(&file).unwrap() };
let ctx_gguf: GGufFile = GGufFile::new(&mmap).unwrap();

let align = ctx_gguf.get_meta_kvs().alignment();
let align = ctx_gguf.meta_kvs().alignment();
let ggufs = self.split_strategy(ctx_gguf.clone()).unwrap();

let tensors = ctx_gguf.get_tensors_as_indexmap();
let tensors = ctx_gguf.tensors_as_indexmap();
let mut tensor_iter: indexmap::map::Iter<ggus::GGufTensorInfo, &[u8]> = tensors.iter();

for gguf in ggufs {
Expand All @@ -75,12 +83,6 @@ impl SplitArgs {

let kvs = gguf.meta_kvs.kvs();
for kv in kvs {
if kv.key() == LLM_KV_SPLIT_TENSORS_COUNT
|| kv.key() == LLM_KV_SPLIT_COUNT
|| kv.key() == LLM_KV_SPLIT_NO
{
continue;
}
writer
.write_meta_kv(kv.key(), kv.ty(), kv.value_bytes())
.unwrap();
Expand Down Expand Up @@ -197,10 +199,10 @@ impl SplitArgs {
},
}

let tensors = ctx_gguf.get_tensors_as_indexmap();
let tensors = ctx_gguf.tensors_as_indexmap();

let mut ggufs: Vec<GGufFileInfo> = Vec::new();
let n_tensors: u64 = ctx_gguf.get_header().tensor_count;
let n_tensors: u64 = ctx_gguf.header().tensor_count;

let setup_gguf_file = |i_split: u64, n_tensors: u64| {
let mut gguf_file = GGufFileInfo::new_empty();
Expand All @@ -221,20 +223,20 @@ impl SplitArgs {

let mut i_split: u64 = 1;
let mut gguf_file = setup_gguf_file(i_split, n_tensors);
gguf_file.meta_kvs = ctx_gguf.get_meta_kvs().clone();
gguf_file.meta_kvs = ctx_gguf.meta_kvs().clone();

gguf_file.header.metadata_kv_count += ctx_gguf.header().metadata_kv_count;

if gguf_file.meta_kvs.get(LLM_KV_SPLIT_NO).is_some() {
if gguf_file.meta_kvs.remove(LLM_KV_SPLIT_NO) {
gguf_file.header.metadata_kv_count -= 1;
}
if gguf_file.meta_kvs.get(LLM_KV_SPLIT_COUNT).is_some() {
if gguf_file.meta_kvs.remove(LLM_KV_SPLIT_COUNT) {
gguf_file.header.metadata_kv_count -= 1;
}
if gguf_file.meta_kvs.get(LLM_KV_SPLIT_TENSORS_COUNT).is_some() {
if gguf_file.meta_kvs.remove(LLM_KV_SPLIT_TENSORS_COUNT) {
gguf_file.header.metadata_kv_count -= 1;
}

gguf_file.header.metadata_kv_count += ctx_gguf.get_header().metadata_kv_count;

if self.no_tensor_first_split {
ggufs.push(gguf_file);
gguf_file = setup_gguf_file(i_split, n_tensors);
Expand Down Expand Up @@ -267,7 +269,12 @@ impl SplitArgs {
let output_path = &self.output.unwrap();
let mut index = 0;
while index < ggufs.len() {
ggufs[index].output_path = split_path(output_path, index + 1, ggufs.len());
ggufs[index].output_path = format!(
"{}-{:05}-of-{:05}.gguf",
output_path,
index + 1,
ggufs.len()
);
ggufs[index].new_kv_tuples[1] = (LLM_KV_SPLIT_COUNT.to_string(), ty::U16, tensor_count);
index += 1;
}
Expand All @@ -283,28 +290,3 @@ impl SplitArgs {
}
}
}

fn split_path(path_prefix: &String, split_no: usize, split_count: usize) -> String {
format!("{}-{:05}-of-{:05}.gguf", path_prefix, split_no, split_count)
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let input = PathBuf::from("/home/qinyiqun/gguf/xtask/src/test/rust/rust_ori.gguf");
let output = Some("/home/qinyiqun/gguf/xtask/src/test/rust/rust_ori_oi".to_string());
let split_args = SplitArgs {
input,
output,
split_max_tensors: None,
split_max_size: Some("300M".to_string()),
no_tensor_first_split: true,
n_bytes_split: 0,
n_split_tensors: 0,
};

split_args.split();
}
}

0 comments on commit 259e5db

Please sign in to comment.