-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.py
33 lines (25 loc) · 1.01 KB
/
stream.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
import torch
from transformers import LogitsWarper
# inspired by comment in https://github.com/tloen/alpaca-lora/issues/51#issuecomment-1474506296
class CallbackLogitsWarper(LogitsWarper):
def __init__(self, tokenizer, callback):
self.tokenizer = tokenizer
self.callback = callback
self.res_tokens = []
def __call__(self, input_ids: torch.Tensor, scores: torch.Tensor) -> torch.FloatTensor:
self.res_tokens.append(input_ids[0][-1])
result = self.tokenizer.decode(input_ids[0][-1])
self.callback(result)
return scores
def evaluate(prompt, model, tokenizer, configuration, callback):
inputs = tokenizer(prompt, return_tensors="pt")
input_ids = inputs["input_ids"].cuda()
generation_output = model.generate(
input_ids=input_ids,
**configuration,
logits_processor=[CallbackLogitsWarper(tokenizer, callback)],
)
output = ''
for s in generation_output.sequences:
output += tokenizer.decode(s)
return output