-
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.
feat(service): service for cpu 拆分出去方便添加 nvidia
Signed-off-by: YdrMaster <ydrml@hotmail.com>
Showing
8 changed files
with
131 additions
and
86 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
fn main() { | ||
if find_cuda_helper::find_cuda_root().is_some() { | ||
println!("cargo:rustc-cfg=detected_cuda"); | ||
} | ||
} |
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,103 @@ | ||
use crate::Command; | ||
use common::{upos, utok}; | ||
use half::f16; | ||
use std::{collections::HashMap, path::Path, time::Instant}; | ||
use tensor::reslice; | ||
use transformer_cpu::{LayerCache, Llama2, Memory, Prompt, Request, Transformer}; | ||
|
||
pub struct CpuTask { | ||
eos: utok, | ||
transformer: Transformer, | ||
sessions: HashMap<usize, SessionContext>, | ||
} | ||
|
||
impl CpuTask { | ||
pub fn new(model_dir: impl AsRef<Path>) -> Self { | ||
let time = Instant::now(); | ||
let model = Box::new(Memory::load_safetensors_from_dir(model_dir).unwrap()); | ||
info!("load model ... {:?}", time.elapsed()); | ||
|
||
let eos = model.eos_token_id(); | ||
let time = Instant::now(); | ||
let transformer = Transformer::new(model); | ||
info!("build transformer ... {:?}", time.elapsed()); | ||
|
||
let sessions = HashMap::new(); | ||
Self { | ||
eos, | ||
transformer, | ||
sessions, | ||
} | ||
} | ||
|
||
pub fn invoke(&mut self, cmd: Command) { | ||
match cmd { | ||
Command::Chat { | ||
id, | ||
prompt, | ||
responsing, | ||
} => { | ||
let ctx = self | ||
.sessions | ||
.entry(id) | ||
.or_insert_with(|| SessionContext::new(&self.transformer)); | ||
|
||
let time = Instant::now(); | ||
let (last, tokens) = prompt.split_last().expect("prompt is empty"); | ||
if !tokens.is_empty() { | ||
self.transformer.decode(vec![Request { | ||
prompt: Prompt::Prefill(tokens), | ||
cache: &mut ctx.cache, | ||
pos: ctx.pos, | ||
}]); | ||
} | ||
info!("prefill transformer ... {:?}", time.elapsed()); | ||
|
||
ctx.pos += tokens.len() as upos; | ||
let mut token = *last; | ||
let max_seq_len = self.transformer.max_seq_len() as upos; | ||
while ctx.pos < max_seq_len { | ||
let logits = self.transformer.decode(vec![Request { | ||
prompt: transformer_cpu::Prompt::Decode(token), | ||
cache: &mut ctx.cache, | ||
pos: ctx.pos, | ||
}]); | ||
token = argmax(reslice::<u8, f16>(logits.access().as_slice())); | ||
responsing.send(token).unwrap(); | ||
|
||
if token == self.eos { | ||
break; | ||
} | ||
|
||
ctx.pos += 1; | ||
} | ||
} | ||
Command::Drop { id } => { | ||
self.sessions.remove(&id); | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct SessionContext { | ||
pos: upos, | ||
cache: Vec<LayerCache>, | ||
} | ||
|
||
impl SessionContext { | ||
fn new(transformer: &Transformer) -> Self { | ||
Self { | ||
pos: 0, | ||
cache: transformer.new_cache(), | ||
} | ||
} | ||
} | ||
|
||
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
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,7 +1,6 @@ | ||
mod cast; | ||
mod common; | ||
mod generate; | ||
// mod service; | ||
|
||
use clap::Parser; | ||
|
||
|