Download code_repair/transfer/src/models.py from Gen-Verse/RSI-Anything: direct link, hf CLI and curl.
- Browser
- Download file 3.35 kB
-
https://huggingface.co/datasets/Gen-Verse/RSI-Anything/resolve/main/code_repair/transfer/src/models.py
- Command line
-
hf download hf://datasets/Gen-Verse/RSI-Anything/code_repair/transfer/src/models.py
-
curl -L -o models.py https://huggingface.co/datasets/Gen-Verse/RSI-Anything/resolve/main/code_repair/transfer/src/models.py
3.35 kB
| import gc, json, math, time | |
| from common import ROOT, BASE, BASE_MODELS, cached, context, seed, identity, format_feedback, read, sha | |
| from coder import FrozenCoder, extract_code | |
| class Coder(FrozenCoder): | |
| def __init__(self): | |
| super().__init__(BASE_MODELS / 'ReasonFlux-Coder-7B') | |
| def _generate(self, prompt, **kwargs): | |
| ids = self.tokenizer.encode(prompt, add_special_tokens=False) | |
| if len(ids) > 15000: | |
| prompt = self.tokenizer.decode(ids[:5000]) + '\n[Context shortened to fit token budget]\n' + self.tokenizer.decode(ids[-10000:]) | |
| return super()._generate(prompt, **kwargs) | |
| def proposal(self, task, run_seed, current=None, feedback=None, round_id=0): | |
| n = 1 if current is None else 4 | |
| boundary = 'Implement the required callable named ' + task['fn_name'] + ' as a method of class Solution. Preserve the provided signature.\nStarter code:\n' + task['starter_code'] if task['fn_name'] else 'Write a complete Python script that reads standard input and prints to standard output.' | |
| prompt = 'Solve the following programming task. ' + boundary + '\nReturn the complete solution in one ```python code block.\nProblem:\n' + task['problem'] | |
| if current is not None: | |
| prompt += '\nCurrent implementation:\n```python\n' + current + '\n```\nObserved public test feedback:\n' + format_feedback(feedback) + '\nPublic examples:\n' + json.dumps(task['public_tests'], ensure_ascii=False) + '\nRevise the implementation using this feedback.' | |
| key = dict(task=task['task_id'], prompt_sha=identity(prompt), fn_name=task['fn_name'], seed=run_seed, current=current, feedback=json.loads(format_feedback(feedback)) if feedback is not None else None, round=round_id, n=n, sampler='ReasonFlux-7B-temperature0.8-top_p0.95-max4096-v1') | |
| def generate(): | |
| rows = self._generate(prompt, n=n, seed=seed(task['task_id'], run_seed, round_id, identity(current), 'initial' if current is None else 'repair'), max_new_tokens=4096) | |
| return [dict(code=extract_code(r.text), text=r.text, input_tokens=r.input_tokens, output_tokens=r.output_tokens, seconds=r.seconds) for r in rows] | |
| return cached('generation', key, generate) | |
| class World: | |
| def __init__(self, path): | |
| from semantic_world import SemanticExecutionWorld | |
| self.model = SemanticExecutionWorld.load(path, BASE_MODELS / 'Qwen2.5-Coder-3B-Instruct', device='cuda').eval() | |
| self.identity = self.model.identity | |
| def score(self, task, current, feedback, codes): | |
| texts = [context(task, current, feedback, c) for c in codes] | |
| tok = self.model.public_world.online.tokenizer | |
| trimmed = [] | |
| for text in texts: | |
| ids = tok.encode(text, add_special_tokens=False) | |
| trimmed.append(text if len(ids) <= 16000 else tok.decode(ids[:4000]) + '\n[Context shortened]\n' + tok.decode(ids[-12000:])) | |
| out = self.model.score_suite(trimmed) | |
| values = out['utility'].detach().float().cpu().tolist() | |
| if any((r['status'] != 'AC' for r in feedback['results'])): | |
| for (i, code) in enumerate(codes): | |
| if code == current: | |
| values[i] = 0.25 * float(out['predicted_pass_fraction'][i]) | |
| assert all((math.isfinite(x) and 0 <= x <= 1.25 for x in values)) | |
| return values | |