Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions maskbench/maskbench/outlines_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
from outlines_core import Vocabulary, Guide, Index
from outlines_core.json_schema import build_regex_from_schema
from outlines_core.kernels.torch import allocate_token_bitmask, fill_next_token_bitmask


class OutlinesEngine(Engine):
Expand All @@ -19,6 +20,7 @@ def get_name(self):

def init(self):
self.vocabulary = Vocabulary.from_pretrained(self.tokenizer.name_or_path)
self.mask = allocate_token_bitmask(len(self.vocabulary))

def compile_grammar(self, schema: dict):
rx = build_regex_from_schema(json.dumps(schema))
Expand All @@ -29,10 +31,10 @@ def reset(self):
self.guide = Guide(self.index)

def compute_mask(self):
self.res = self.guide.get_tokens()
fill_next_token_bitmask(self.guide, self.mask)

def commit_token(self, t: int) -> bool:
ok = t in self.res
ok = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to check check if token is in mask? It seem advance() will throw if givan in invalid token?

if ok:
self.guide.advance(t)
self.guide.advance(t, False)
return ok