File size: 1,978 Bytes
a53340c
 
affa4ce
 
a53340c
 
 
 
 
 
 
 
affa4ce
a53340c
 
 
affa4ce
a53340c
affa4ce
 
 
 
a53340c
 
b35fa97
a53340c
 
 
 
b35fa97
affa4ce
a53340c
affa4ce
 
 
 
 
 
 
 
 
 
 
 
a53340c
affa4ce
 
 
 
 
a53340c
 
 
affa4ce
 
a53340c
 
affa4ce
 
486a2da
 
 
 
 
b35fa97
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
65
66
import os
import torch
# from vllm import LLM, SamplingParams
from transformers import AutoTokenizer, AutoModelForCausalLM
import logging

# Configure logging
logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)


class ChallengePromptGenerator:
    def __init__(
        self,
        model_local_dir="checkpoint-15000",
    ):
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.generator = AutoModelForCausalLM.from_pretrained(model_local_dir, device_map=self.device)
        self.generator.to_bettertransformer()
        self.tokenizer = AutoTokenizer.from_pretrained(model_local_dir)
        
    def infer_prompt(
        self,
        prompts,
        max_generation_length=77,
        beam_size=1,
        sampling_temperature=0.9,
        sampling_topk=100,
        sampling_topp=1
    ):
        # Add bos
        prompts = [f"{self.tokenizer.bos_token} {prompt}" for prompt in prompts]
        
        # Prepare inputs
        inputs = self.tokenizer(
            prompts,
            return_tensors="pt",
            padding=True,
            truncation=True,
            max_length=256,
            add_special_tokens=False
        ).to(self.device)
        
        # Generate
        outputs = self.generator.generate(
            **inputs,
            max_length=max_generation_length,
            num_beams=beam_size,
            temperature=sampling_temperature,
            top_k=sampling_topk,
            top_p=sampling_topp,
            do_sample=True,
            pad_token_id=self.tokenizer.pad_token_id
        )
        
        # Decode
        decoded_outputs = self.tokenizer.batch_decode(outputs, skip_special_tokens=True)
        outputs = []
        for out in decoded_outputs:
            if out[-1] != ".":
                out = ".".join(out.split(".")[:-1]) + "."
            outputs.append(out)
        return outputs