-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add accept.py in example, which are used to check if a sentence is ac…
…cepted by a grammar; fix `geo_query.ebnf` path in test_geo_query.py (#33)
- Loading branch information
1 parent
69c3795
commit 9511ec9
Showing
3 changed files
with
152 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import argparse | ||
from transformers import AutoModelForCausalLM, AutoTokenizer | ||
from transformers_cfg.grammar_utils import IncrementalGrammarConstraint | ||
from transformers_cfg.generation.logits_process import GrammarConstrainedLogitsProcessor | ||
import logging | ||
|
||
from transformers_cfg.parser import parse_ebnf | ||
from transformers_cfg.recognizer import StringRecognizer | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
|
||
def main(args): | ||
|
||
with open(args.grammar_file_path, "r") as file: | ||
grammar_str = file.read() | ||
parsed_grammar = parse_ebnf(grammar_str) | ||
start_rule_id = parsed_grammar.symbol_table["root"] | ||
recognizer = StringRecognizer(parsed_grammar.grammar_encoding, start_rule_id) | ||
|
||
if args.mode == "prefix": | ||
result = recognizer._accept_prefix(args.sentence) | ||
else: | ||
result = recognizer._accept_string(args.sentence) | ||
|
||
print(result) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description="Generate text with grammar constraints." | ||
) | ||
parser.add_argument( | ||
"-g", | ||
"--grammar_file_path", | ||
type=str, | ||
required=True, | ||
help="Path to the grammar file (supports both relative and absolute paths)", | ||
) | ||
parser.add_argument( | ||
"-s", "--sentence", type=str, required=True, help="Prefix prompt for generation" | ||
) | ||
parser.add_argument( | ||
"-m", | ||
"--mode", | ||
type=str, | ||
choices=["prefix", "sentence"], | ||
default="prefix", | ||
help="Mode of operation, " | ||
"prefix mode accepts a prefix string, sentence mode only accepts a full sentence", | ||
) | ||
|
||
args = parser.parse_args() | ||
main(args) |
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,73 @@ | ||
import argparse | ||
from transformers import AutoModelForCausalLM, AutoTokenizer | ||
from transformers_cfg.grammar_utils import IncrementalGrammarConstraint | ||
from transformers_cfg.generation.logits_process import GrammarConstrainedLogitsProcessor | ||
import logging | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
|
||
def main(args): | ||
|
||
# Load model and tokenizer | ||
tokenizer = AutoTokenizer.from_pretrained(args.model_id) | ||
tokenizer.pad_token = tokenizer.eos_token | ||
model = AutoModelForCausalLM.from_pretrained(args.model_id) | ||
|
||
# Load grammar | ||
with open(args.grammar_file_path, "r") as file: | ||
grammar_str = file.read() | ||
grammar = IncrementalGrammarConstraint( | ||
grammar_str, "root", tokenizer, unicode=False | ||
) | ||
grammar_processor = GrammarConstrainedLogitsProcessor(grammar) | ||
|
||
# Generate | ||
prefix = args.prefix_prompt | ||
input_ids = tokenizer( | ||
prefix, add_special_tokens=False, return_tensors="pt", padding=True | ||
)["input_ids"] | ||
|
||
output = model.generate( | ||
input_ids, | ||
do_sample=False, | ||
max_new_tokens=20, | ||
logits_processor=[grammar_processor], | ||
repetition_penalty=1.1, | ||
num_return_sequences=1, | ||
) | ||
# decode output | ||
generations = tokenizer.batch_decode(output, skip_special_tokens=True) | ||
|
||
print(generations) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description="Generate text with grammar constraints." | ||
) | ||
parser.add_argument( | ||
"-m", | ||
"--model_id", | ||
type=str, | ||
required=True, | ||
help="Model identifier for loading the tokenizer and model", | ||
default="gpt2", | ||
) | ||
parser.add_argument( | ||
"-g", | ||
"--grammar_file_path", | ||
type=str, | ||
required=True, | ||
help="Path to the grammar file (supports both relative and absolute paths)", | ||
) | ||
parser.add_argument( | ||
"-p", | ||
"--prefix_prompt", | ||
type=str, | ||
required=True, | ||
help="Prefix prompt for generation", | ||
) | ||
|
||
args = parser.parse_args() | ||
main(args) |
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