File size: 869 Bytes
1d777c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from torch_grammar import GrammarSampler
from transformers.generation.logits_process import LogitsProcessor

from modules import shared

sampler = None
grammar = None
grammar_string = ''


class GrammarLogitsProcessor(LogitsProcessor):
    def __init__(self, string):

        global sampler, grammar, grammar_string

        if string != grammar_string:
            grammar_string = string
            if string.strip() != '':
                string = string.strip() + '\n'
                sampler = GrammarSampler(string, 'root', shared.tokenizer)
            else:
                sampler = None

        if sampler is not None:
            grammar = sampler.logits_processor()
        else:
            grammar = None

    def __call__(self, input_ids, scores):
        if grammar is not None:
            scores = grammar(input_ids, scores)

        return scores