-
Notifications
You must be signed in to change notification settings - Fork 25
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 <ydrml@hotmail.com>
- Loading branch information
Showing
8 changed files
with
115 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[alias] | ||
xtask = "run --package xtask --release --" | ||
cast = "xtask cast" | ||
generate = "xtask generate" |
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,59 @@ | ||
use std::{fs, path::PathBuf, time::Instant}; | ||
use transformer_cpu::model_parameters::{save, DataType, Llama2, Memory}; | ||
|
||
#[derive(Args, Default)] | ||
pub(crate) struct CastArgs { | ||
/// Original model directory. | ||
#[clap(short, long)] | ||
model: String, | ||
/// Target model directory. | ||
#[clap(short, long)] | ||
target: Option<String>, | ||
/// Target model type. | ||
#[clap(short, long)] | ||
dt: Option<String>, | ||
} | ||
|
||
impl CastArgs { | ||
pub fn invode(self) { | ||
let ty = match self.dt.as_deref() { | ||
Some("f32") | Some("float") | Some("float32") | None => DataType::F32, | ||
Some("f16") | Some("half") | Some("float16") => DataType::F16, | ||
Some("bf16") | Some("bfloat16") => DataType::BF16, | ||
Some(ty) => panic!("Unknown data type: \"{ty}\""), | ||
}; | ||
let model_dir = PathBuf::from(self.model); | ||
|
||
let time = Instant::now(); | ||
let model = Memory::load_safetensors(&model_dir).unwrap(); | ||
println!("load model ... {:?}", time.elapsed()); | ||
|
||
if model.data_type() == ty { | ||
println!("Model already has target data type"); | ||
return; | ||
} | ||
|
||
let target = self.target.map(PathBuf::from).unwrap_or_else(|| { | ||
model_dir.parent().unwrap().join(format!( | ||
"{}_{ty:?}", | ||
model_dir.file_name().unwrap().to_str().unwrap() | ||
)) | ||
}); | ||
fs::create_dir_all(&target).unwrap(); | ||
|
||
let time = Instant::now(); | ||
let model = Memory::cast(&model, ty); | ||
println!("cast data type ... {:?}", time.elapsed()); | ||
|
||
let time = Instant::now(); | ||
save(&model, &target).unwrap(); | ||
println!("save model ... {:?}", time.elapsed()); | ||
|
||
let tokenizer = model_dir.join("tokenizer.model"); | ||
if tokenizer.is_file() { | ||
let time = Instant::now(); | ||
fs::copy(&tokenizer, target.join("tokenizer.model")).unwrap(); | ||
println!("copy tokenizer ... {:?}", time.elapsed()); | ||
} | ||
} | ||
} |
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,35 @@ | ||
use std::{path::PathBuf, time::Instant}; | ||
use tokenizer::{Tokenizer, BPE}; | ||
use transformer_cpu::{model_parameters::Memory, Transformer}; | ||
|
||
#[derive(Args, Default)] | ||
pub(crate) struct GenerateArgs { | ||
/// Model directory. | ||
#[clap(short, long)] | ||
model: String, | ||
/// Prompt. | ||
#[clap(short, long)] | ||
prompt: String, | ||
} | ||
|
||
impl GenerateArgs { | ||
pub fn invoke(self) { | ||
let model_dir = PathBuf::from(self.model); | ||
|
||
let time = Instant::now(); | ||
let model = Box::new(Memory::load_safetensors(&model_dir).unwrap()); | ||
info!("load model ... {:?}", time.elapsed()); | ||
|
||
let time = Instant::now(); | ||
let _transformer = Transformer::new(model, 1); | ||
info!("build transformer ... {:?}", time.elapsed()); | ||
|
||
let time = Instant::now(); | ||
let tokenizer = BPE::from_model_file(model_dir.join("tokenizer.model")).unwrap(); | ||
info!("build tokenizer ... {:?}", time.elapsed()); | ||
|
||
let time = Instant::now(); | ||
let _prompt_tokens = tokenizer.encode(self.prompt.trim()); | ||
info!("encode prompt ... {:?}", time.elapsed()); | ||
} | ||
} |
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