-
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.
Merge pull request #16 from InfiniTensor/dev
重构 chat-template 和 tokenizer
- Loading branch information
Showing
34 changed files
with
1,081 additions
and
463 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,9 @@ | ||
[package] | ||
name = "chat-template" | ||
version = "0.0.0" | ||
edition = "2021" | ||
authors = ["YdrMaster <[email protected]>"] | ||
|
||
[dependencies] | ||
serde = { workspace = true, features = ["derive"] } | ||
minijinja = { version = "2.1", default-features = false, features = ["loader"] } |
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,119 @@ | ||
#![deny(warnings)] | ||
|
||
use minijinja::Environment; | ||
use serde::Serialize; | ||
use std::sync::{ | ||
atomic::{AtomicUsize, Ordering::Relaxed}, | ||
OnceLock, RwLock, | ||
}; | ||
|
||
#[repr(transparent)] | ||
pub struct ChatTemplate(String); | ||
|
||
#[derive(Serialize)] | ||
pub struct Message<'a> { | ||
pub role: &'a str, | ||
pub content: &'a str, | ||
} | ||
|
||
impl ChatTemplate { | ||
pub fn new(template: String) -> Self { | ||
static NEXT: AtomicUsize = AtomicUsize::new(0); | ||
let id = NEXT.fetch_add(1, Relaxed).to_string(); | ||
|
||
jinja() | ||
.write() | ||
.unwrap() | ||
.add_template_owned(id.clone(), template) | ||
.unwrap(); | ||
|
||
Self(id) | ||
} | ||
|
||
pub fn render( | ||
&self, | ||
messages: &[Message<'_>], | ||
bos_token: &str, | ||
eos_token: &str, | ||
add_generation_prompt: bool, | ||
) -> Result<String, minijinja::Error> { | ||
#[derive(Serialize)] | ||
struct Args<'a> { | ||
messages: &'a [Message<'a>], | ||
bos_token: &'a str, | ||
eos_token: &'a str, | ||
add_generation_prompt: bool, | ||
} | ||
|
||
jinja() | ||
.read() | ||
.unwrap() | ||
.get_template(&self.0) | ||
.unwrap() | ||
.render(Args { | ||
messages, | ||
bos_token, | ||
eos_token, | ||
add_generation_prompt, | ||
}) | ||
} | ||
} | ||
|
||
impl Drop for ChatTemplate { | ||
fn drop(&mut self) { | ||
jinja().write().unwrap().remove_template(&self.0); | ||
} | ||
} | ||
|
||
fn jinja() -> &'static RwLock<Environment<'static>> { | ||
static ENV: OnceLock<RwLock<Environment<'_>>> = OnceLock::new(); | ||
ENV.get_or_init(|| { | ||
let mut env = Environment::empty(); | ||
env.set_unknown_method_callback(|_, value, method, args| { | ||
use minijinja::{value::ValueKind as ThisType, ErrorKind::UnknownMethod, Value}; | ||
match (method, value.kind(), args) { | ||
("strip", ThisType::String, []) => Ok(Value::from_safe_string( | ||
value.to_str().unwrap().trim().into(), | ||
)), | ||
_ => Err(UnknownMethod.into()), | ||
} | ||
}); | ||
RwLock::new(env) | ||
}) | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
const TAIDE: &str = "{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}{% set system_message = '<<SYS>>\n' + messages[0]['content'] + '\n<</SYS>>\n\n' %}{% else %}{% set loop_messages = messages %}{% set system_message = '' %}{% endif %}{% for message in loop_messages %}{% if loop.index0 == 0 %}{% set content = system_message + message['content'] %}{% else %}{% set content = message['content'] %}{% endif %}{% if message['role'] == 'user' %}{{ bos_token + '[INST] ' + content + ' [/INST]'}}{% elif message['role'] == 'assistant' %}{{ ' ' + content + ' ' + eos_token }}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}"; | ||
const MINICPM: &str = "{% for message in messages %}{% if message['role'] == 'user' %}{{'<用户>' + message['content'].strip() + '<AI>'}}{% else %}{{message['content'].strip()}}{% endif %}{% endfor %}"; | ||
|
||
let result = ChatTemplate::new(TAIDE.into()) | ||
.render( | ||
&[Message { | ||
role: "user", | ||
content: "Hello, who are you?", | ||
}], | ||
"<s>", | ||
"</s>", | ||
true, | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
result, | ||
"<s>[INST] Hello, who are you? [/INST]<|im_start|>assistant\n" | ||
); | ||
|
||
let result = ChatTemplate::new(MINICPM.into()) | ||
.render( | ||
&[Message { | ||
role: "user", | ||
content: "Hello, who are you?", | ||
}], | ||
"<s>", | ||
"</s>", | ||
true, | ||
) | ||
.unwrap(); | ||
assert_eq!(result, "<用户>Hello, who are you?<AI>"); | ||
} |
This file was deleted.
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,17 @@ | ||
[package] | ||
name = "common-acl" | ||
version = "0.0.0" | ||
edition = "2021" | ||
authors = ["YdrMaster <[email protected]>"] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
common = { path = "../../common" } | ||
common-devices = { path = "../common" } | ||
tensor = { path = "../../tensor" } | ||
operators = { workspace = true, features = ["ascend-card"] } | ||
|
||
[build-dependencies] | ||
build-script-cfg.workspace = true | ||
search-ascend-tools.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
fn main() { | ||
use build_script_cfg::Cfg; | ||
use search_ascend_tools::find_ascend_toolkit_home; | ||
|
||
let ascend = Cfg::new("detected_ascend"); | ||
if find_ascend_toolkit_home().is_some() { | ||
ascend.define(); | ||
} | ||
} |
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,4 @@ | ||
#![cfg(detected_ascend)] | ||
|
||
pub use operators::ascendcl; | ||
pub use tensor::Tensor; |
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
Oops, something went wrong.