-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YdrMaster <[email protected]>
- Loading branch information
Showing
19 changed files
with
195 additions
and
68 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,9 @@ authors = ["YdrMaster <[email protected]>"] | |
[dependencies] | ||
common = { path = "../common" } | ||
tensor = { path = "../tensor" } | ||
half = "2.4" | ||
half.workspace = true | ||
rayon = "1.9" | ||
memmap2 = "0.9" | ||
safetensors = "0.4" | ||
serde_json = "1.0" | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde = { workspace = true, features = ["derive"] } | ||
serde_json.workspace = true |
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 |
---|---|---|
|
@@ -7,8 +7,8 @@ authors = ["YdrMaster <[email protected]>"] | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
half = "2.4" | ||
half.workspace = true | ||
smallvec = "1.13" | ||
nalgebra = "0.32" | ||
rayon = "1.9" | ||
serde = "1.0" | ||
serde.workspace = true |
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
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
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,52 @@ | ||
use common::utok; | ||
use log::LevelFilter; | ||
use simple_logger::SimpleLogger; | ||
use std::{io::ErrorKind::NotFound, path::Path}; | ||
use tokenizer::{Tokenizer, VocabTxt, BPE}; | ||
|
||
pub(crate) fn logger_init(log_level: &Option<String>) { | ||
let log = log_level | ||
.as_ref() | ||
.and_then(|log| match log.to_lowercase().as_str() { | ||
"off" | "none" => Some(LevelFilter::Off), | ||
"trace" => Some(LevelFilter::Trace), | ||
"debug" => Some(LevelFilter::Debug), | ||
"info" => Some(LevelFilter::Info), | ||
"error" => Some(LevelFilter::Error), | ||
_ => None, | ||
}) | ||
.unwrap_or(LevelFilter::Warn); | ||
SimpleLogger::new().with_level(log).init().unwrap(); | ||
} | ||
|
||
pub(crate) fn tokenizer(path: Option<String>, model_dir: impl AsRef<Path>) -> Box<dyn Tokenizer> { | ||
match path { | ||
Some(path) => match Path::new(&path).extension() { | ||
Some(ext) if ext == "txt" => Box::new(VocabTxt::from_txt_file(path).unwrap()), | ||
Some(ext) if ext == "model" => Box::new(BPE::from_model_file(path).unwrap()), | ||
_ => panic!("Tokenizer file {path:?} not supported"), | ||
}, | ||
None => { | ||
match BPE::from_model_file(model_dir.as_ref().join("tokenizer.model")) { | ||
Ok(bpe) => return Box::new(bpe), | ||
Err(e) if e.kind() == NotFound => {} | ||
Err(e) => panic!("{e:?}"), | ||
} | ||
match VocabTxt::from_txt_file(model_dir.as_ref().join("vocabs.txt")) { | ||
Ok(voc) => return Box::new(voc), | ||
Err(e) if e.kind() == NotFound => {} | ||
Err(e) => panic!("{e:?}"), | ||
} | ||
panic!("Tokenizer file not found"); | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn argmax<T: PartialOrd>(logits: &[T]) -> utok { | ||
logits | ||
.iter() | ||
.enumerate() | ||
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap()) | ||
.unwrap() | ||
.0 as _ | ||
} |
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,58 @@ | ||
use super::ServiceArgs; | ||
use crate::common::{argmax, tokenizer}; | ||
use common::upos; | ||
use std::{collections::HashMap, path::Path, time::Instant}; | ||
use transformer_cpu::{model_parameters::Memory, LayerCache, Transformer}; | ||
|
||
pub(super) fn run(args: ServiceArgs) { | ||
let model_dir = Path::new(&args.model); | ||
|
||
let time = Instant::now(); | ||
let tokenizer = tokenizer(args.tokenizer, &model_dir); | ||
info!("build tokenizer ... {:?}", time.elapsed()); | ||
|
||
let time = Instant::now(); | ||
let model = Box::new(Memory::load_safetensors_from_dir(model_dir).unwrap()); | ||
info!("load model ... {:?}", time.elapsed()); | ||
|
||
let time = Instant::now(); | ||
let mut transformer = Transformer::new(model); | ||
info!("build transformer ... {:?}", time.elapsed()); | ||
|
||
struct SessionContext { | ||
pos: upos, | ||
kv_cache: Vec<LayerCache>, | ||
} | ||
|
||
let mut sessions = HashMap::<usize, SessionContext>::new(); | ||
|
||
loop { | ||
let id = 0; | ||
let prompt = "The quick brown fox jumps over the lazy dog"; | ||
|
||
let session = sessions.entry(id).or_insert_with(|| SessionContext { | ||
pos: 0, | ||
kv_cache: transformer.new_cache(), | ||
}); | ||
|
||
let prompt_tokens = tokenizer.encode(&prompt.trim()); | ||
let (last, tokens) = prompt_tokens.split_last().expect("prompt is empty"); | ||
if !tokens.is_empty() { | ||
transformer.update(tokens, &mut session.kv_cache, session.pos as _); | ||
session.pos += tokens.len() as upos; | ||
} | ||
|
||
let mut token = *last; | ||
let max_pos = transformer.max_seq_len() as upos; | ||
let mut out = String::new(); | ||
while session.pos < max_pos { | ||
let logits = transformer.forward(token, &mut session.kv_cache, session.pos as _); | ||
let next = argmax(logits); | ||
|
||
token = next; | ||
session.pos += 1; | ||
|
||
out.push_str(&tokenizer.decode(next).replace('▁', " ")); | ||
} | ||
} | ||
} |
Oops, something went wrong.