Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
fix(ggml/llmb): use IndexMap for GGUF
Browse files Browse the repository at this point in the history
  • Loading branch information
philpax committed Oct 23, 2023
1 parent 43ebc3d commit e4db5b9
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 26 deletions.
14 changes: 8 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [
"crates/llm",
"crates/llm-base",
"crates/models/*",
"binaries/*"
"binaries/*",
]
resolver = "2"
default-members = ["binaries/llm-cli", "crates/llm"]
Expand All @@ -33,6 +33,7 @@ memmap2 = "0.5.10"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing = { version = "0.1", features = ["log"] }
llm-samplers = "=0.0.6"
indexmap = "2.0.2"

# Config for 'cargo dist'
[workspace.metadata.dist]
Expand All @@ -45,7 +46,12 @@ ci = ["github"]
# The installers to generate for each app
installers = ["shell", "powershell"]
# Target platforms to build apps for (Rust target-triple syntax)
targets = ["x86_64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "aarch64-apple-darwin"]
targets = [
"x86_64-unknown-linux-gnu",
"x86_64-apple-darwin",
"x86_64-pc-windows-msvc",
"aarch64-apple-darwin",
]

# The profile that 'cargo dist' will build with
[profile.dist]
Expand Down
1 change: 1 addition & 0 deletions crates/ggml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ggml-sys = { path = "sys", version = "0.2.0-dev" }

thiserror = { workspace = true }
memmap2 = { workspace = true }
indexmap = { workspace = true }

[dev-dependencies]
rand = { workspace = true }
Expand Down
5 changes: 3 additions & 2 deletions crates/ggml/src/format/gguf/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::{collections::HashMap, io::BufRead};
use std::io::BufRead;

use indexmap::IndexMap;
use thiserror::Error;

use crate::util;

use super::{GgufContext, GgufLoadError};

#[derive(Debug, Clone, PartialEq)]
pub struct Metadata(pub HashMap<String, MetadataValue>);
pub struct Metadata(pub IndexMap<String, MetadataValue>);
impl Metadata {
pub fn iter(&self) -> impl Iterator<Item = (&String, &MetadataValue)> {
self.0.iter()
Expand Down
12 changes: 5 additions & 7 deletions crates/ggml/src/format/gguf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#![allow(missing_docs)]

use std::{
collections::HashMap,
io::{BufRead, Seek},
};
use std::io::{BufRead, Seek};

use super::{data_size, header_size, ContainerType, ContainerTypeReadError};
use crate::{util, ElementType};

use indexmap::IndexMap;
use thiserror::Error;

mod metadata;
Expand Down Expand Up @@ -49,7 +47,7 @@ pub enum GgufSaveError {
// TODO!
}

pub type TensorInfos = HashMap<String, TensorInfo>;
pub type TensorInfos = IndexMap<String, TensorInfo>;

#[derive(Debug, Clone, PartialEq)]
pub struct Gguf {
Expand All @@ -74,7 +72,7 @@ impl Gguf {
let tensor_count = util::read_length(reader, ctx.use_64_bit_length)?;
let metadata_kv_count = util::read_length(reader, ctx.use_64_bit_length)?;

let mut metadata = HashMap::with_capacity(metadata_kv_count);
let mut metadata = IndexMap::with_capacity(metadata_kv_count);
for _ in 0..metadata_kv_count {
let (key, value) = MetadataValue::read_key_value(&ctx, reader)?;
metadata.insert(key, value);
Expand All @@ -86,7 +84,7 @@ impl Gguf {
.and_then(|v| v.as_uint32())
.unwrap_or(DEFAULT_ALIGNMENT) as u64;

let mut tensor_infos = HashMap::with_capacity(tensor_count);
let mut tensor_infos = IndexMap::with_capacity(tensor_count);
for _ in 0..tensor_count {
let (key, value) = TensorInfo::read_name_value(&ctx, reader)?;
tensor_infos.insert(key, value);
Expand Down
11 changes: 7 additions & 4 deletions crates/llm-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ bytemuck = { workspace = true }
rand = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
indexmap = { workspace = true }
memmap2 = { workspace = true }
tracing = { workspace = true }
llm-samplers = { workspace = true }

partial_sort = "0.2.0"
serde_bytes = "0.11"
memmap2 = { workspace = true }
half = "2"
tokenizers = {version="0.13.4", default-features=false, features=["onig"]}
tokenizers = { version = "0.13.4", default-features = false, features = [
"onig",
] }
regex = "1.8"
tracing = { workspace = true }

llm-samplers = { workspace = true }

[features]
tokenizers-remote = ["tokenizers/http"]
Expand Down
8 changes: 3 additions & 5 deletions crates/llm-base/src/lora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use ggml::{
format::gguf::{Gguf, Metadata, TensorInfo},
GraphExecutionPlan,
};
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
};
use indexmap::IndexMap;
use std::{collections::HashSet, path::PathBuf};

#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
/// Parameters for a [LoRA](https://arxiv.org/abs/2106.09685) adapter.
Expand Down Expand Up @@ -50,7 +48,7 @@ pub struct LoraAdapter {
/// Scaling to apply to the LoRA weights.
pub scaling: f32,
/// The tensors of the LoRA.
pub tensors: HashMap<String, TensorInfo>,
pub tensors: IndexMap<String, TensorInfo>,
/// Names of the tensors that should be patched.
pub tensors_to_patch: HashSet<String>,
/// Source containing the LoRA weights.
Expand Down

0 comments on commit e4db5b9

Please sign in to comment.