Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sampler Expansion - GBNF Grammar #57

Merged
merged 10 commits into from
Mar 12, 2024
Merged

Sampler Expansion - GBNF Grammar #57

merged 10 commits into from
Mar 12, 2024

Conversation

ianmarmour
Copy link
Contributor

This PR is functional but probably not in an ideal state? All of the grammar related code is cribbed from UtilityAI's library I'm not sure if we want to dump this and build something totally new the code seemed fairly well written and I didn't see value in re-inventing the wheel here. I will note that neither the UtilityAI or this implementation of sampler code is exactly the same as the llama.cpp sampling logic specifically we're missing the resampling behavior for increased performance when using grammars.

Additional Notes

For what it's worth I don't know if it's trivial to attempt to implement this as a sampler stage? We always need to accept the token's after sampling has been completed and a token has been selected I wasn't able to figure out a strategy for dealing with moving around the grammar in a way that made sense and it also felt awkward to include token acceptance as a SamplerStage.

Usage

Basic usage example below,

Code

let gbnf_bytes = include_bytes!("json.gbnf");
let gbnf: &str = str::from_utf8(gbnf_bytes).unwrap();
let grammar = LlamaGrammar::from_str(gbnf).unwrap();

let stages = vec![
    SamplerStage::RepetitionPenalty {
        repetition_penalty: 1.1,
        frequency_penalty: 0.0,
        presence_penalty: 0.0,
        last_n: 64,
    },
    SamplerStage::TopK(40),
    SamplerStage::TopP(0.95),
    SamplerStage::MinP(0.05),
    SamplerStage::Temperature(0.8),
];

let sampler = StandardSampler::new_softmax(stages, 1, Some(grammar));

Grammar

root ::= Response
Response ::= "{"   ws   "\"importance\":"   ws   ResponseValue   ","   ws   "\"category\":"   ws   Category   ","   ws   "\"summary\":"   ws   string   "}"
Responselist ::= "[]" | "["   ws   Response   (","   ws   Response)*   "]"
Category ::= "\"promotional\"" | "\"receipt\"" | "\"notification\"" | "'advertisement'" | "'professional'" | "'news'"
ResponseValue ::= "\"high\"" | "\"low\""
string ::= "\""   ([^"]*)   "\""
boolean ::= "true" | "false"
ws ::= [ \t\n]*
number ::= [0-9]+   "."?   [0-9]*
stringlist ::= "["   ws   "]" | "["   ws   string   (","   ws   string)*   ws   "]"
numberlist ::= "["   ws   "]" | "["   ws   string   (","   ws   number)*   ws   "]"

Output

ggml_gallocr_reserve_n: reallocating Metal buffer from size 0.00 MiB to 2114.53 MiB
ggml_gallocr_reserve_n: reallocating CPU buffer from size 0.00 MiB to 8.00 MiB
{"importance": "low", "category": "promotional", "summary": "Marketing email from Adobe Inc., most likely promoting their products and services. You can unsubscribe from future emails by clicking the link provided."}

@nkoppel
Copy link
Contributor

nkoppel commented Mar 11, 2024

@ianmarmour I've opened a pull request for your pull request here: ianmarmour#1

@pedro-devv pedro-devv linked an issue Mar 12, 2024 that may be closed by this pull request
@pedro-devv pedro-devv merged commit 9a21a8e into edgenai:main Mar 12, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sampler Expansion - GBNF Grammar
3 participants