File size: 4,021 Bytes
672ecc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
import re
import time
from dataclasses import dataclass
from cure_wm import TestCase
CODE_FENCE = re.compile('```(?:python|py)?\\s*\\n(.*?)```', re.I | re.S)
TEST_INPUT = re.compile('\\*\\*Test Input:\\*\\*\\s*```[^\\n]*\\n(.*?)```', re.I | re.S)
TEST_OUTPUT = re.compile('\\*\\*Test Output:\\*\\*\\s*```[^\\n]*\\n(.*?)```', re.I | re.S)

def extract_code(text):
    matches = CODE_FENCE.findall(text)
    if matches:
        return matches[-1].strip() + '\n'
    tail = text.split('</think>')[-1].strip()
    lines = tail.splitlines()
    for (i, line) in enumerate(lines):
        if line.startswith(('import ', 'from ', 'def ', 'class ', 'if __name__', 'n =', 'n=', 'a =', 'a=', 's =', 's=')):
            return '\n'.join(lines[i:]).strip() + '\n'
    return ''

@dataclass(frozen=True)
class Generation:
    text: str
    input_tokens: int
    output_tokens: int
    seconds: float

class FrozenCoder:

    def __init__(self, model_path):
        import torch
        from transformers import AutoModelForCausalLM, AutoTokenizer
        self.torch = torch
        self.tokenizer = AutoTokenizer.from_pretrained(model_path, local_files_only=True)
        self.model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.bfloat16, device_map='cuda:0', local_files_only=True).eval()

    def _generate(self, user_prompt, *, n=1, seed=0, max_new_tokens=2048):
        torch = self.torch
        prompt = self.tokenizer.apply_chat_template([{'role': 'system', 'content': 'You are a helpful assistant for solving Python coding problems.'}, {'role': 'user', 'content': user_prompt}], tokenize=False, add_generation_prompt=True)
        tokens = self.tokenizer(prompt, return_tensors='pt', truncation=False).to('cuda:0')
        input_length = tokens['input_ids'].shape[1]
        if input_length + max_new_tokens > 20000:
            raise ValueError(f'Prompt too long: {input_length} tokens')
        torch.manual_seed(seed)
        started = time.monotonic()
        with torch.inference_mode():
            outputs = self.model.generate(**tokens, do_sample=True, temperature=0.8, top_p=0.95, max_new_tokens=max_new_tokens, num_return_sequences=n, pad_token_id=self.tokenizer.eos_token_id)
        elapsed = time.monotonic() - started
        generations = []
        for seq in outputs:
            continuation = seq[input_length:].tolist()
            if self.tokenizer.eos_token_id in continuation:
                output_tokens = continuation.index(self.tokenizer.eos_token_id) + 1
            else:
                output_tokens = len(continuation)
            generations.append(Generation(self.tokenizer.decode(continuation[:output_tokens], skip_special_tokens=True), input_length, output_tokens, elapsed / n))
        return tuple(generations)

    def initial(self, problem, seed):
        prompt = f'Think first, then write a complete Python script for the problem below. Read from standard input and print the answer to standard output. Put the final runnable script in one ```python block.\n\nProblem:\n{problem}'
        raw = self._generate(prompt, seed=seed, max_new_tokens=4096)[0]
        return (extract_code(raw.text), raw)

    def refine(self, problem, current_code, feedback, visible_tests, *, k, seed):
        lines = []
        for (test, result) in zip(visible_tests, feedback.results):
            lines.append(f'Input: {test.input[:1000]!r}\nExpected: {test.expected[:500]!r}\nObserved: {result.actual[:500]!r}\nStatus: {result.status}\nError: {result.stderr[:500]}')
        prompt = f'Revise the following Python script to solve the problem. Read from standard input and print to standard output. Use the observed test feedback to fix the program. Return a complete runnable script in one ```python block.\n\nProblem:\n{problem}\n\nCurrent script:\n```python\n{current_code}\n```\n\nVisible test feedback:\n' + '\n\n'.join(lines)
        raws = self._generate(prompt, n=k, seed=seed, max_new_tokens=4096)
        return tuple(((extract_code(raw.text), raw) for raw in raws))