-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathbench_cfg_guide.py
64 lines (49 loc) · 2.03 KB
/
bench_cfg_guide.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import random
from transformers import AutoTokenizer
import outlines.grammars
from outlines.caching import cache_disabled
from outlines.fsm.guide import CFGGuide
from outlines.models.transformers import TransformerTokenizer
random.seed(42)
def get_tiny_tokenizer():
"""1000 tokens in vocabulary"""
return TransformerTokenizer(
AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2")
)
benched_grammars = {
"json": outlines.grammars.json,
"arithmetic": outlines.grammars.arithmetic,
}
class CFGGuideBenchmark:
params = benched_grammars.keys()
def setup(self, grammar_name):
self.tokenizer = get_tiny_tokenizer()
self.prebuilt_cfg_guide = CFGGuide(
benched_grammars[grammar_name], self.tokenizer
)
@staticmethod
def _run_random_cfg(guide, rejection_sampling=True):
state = guide.initial_state
token_ids = list(guide.tokenizer.vocabulary.values())
for i in range(40):
# simulate ordering of logits top prob to lowest prob
random.shuffle(token_ids)
# simulate sampling and state update
if rejection_sampling:
next_token_id = next(guide.iter_valid_token_ids(state, token_ids))
state = guide.get_next_state(state, next_token_id)
else:
next_token_id = random.choice(guide.get_next_instruction(state).tokens)
state = guide.get_next_state(state, next_token_id)
@cache_disabled()
def time_cfg_guide_setup(self, grammar_name):
CFGGuide(benched_grammars[grammar_name], self.tokenizer)
@cache_disabled()
def time_cfg_guide_run_rejection_sampling(self, grammar):
self._run_random_cfg(self.prebuilt_cfg_guide, rejection_sampling=True)
@cache_disabled()
def time_cfg_guide_run(self, grammar):
self._run_random_cfg(self.prebuilt_cfg_guide, rejection_sampling=False)
@cache_disabled()
def peakmem_cfg_guide_run(self, grammar):
self._run_random_cfg(self.prebuilt_cfg_guide)