Buckets:
| """Original TriMul task/MaxRL with a local verifier; optional single-anchor actions.""" | |
| from __future__ import annotations | |
| import asyncio | |
| import hashlib | |
| from ttt_discover import Environment, BaseRewardEvaluator, State | |
| from ttt_discover.tinker_utils.dataset_builder import SAFE_GRADE_EXECUTOR, VerifyResult | |
| from examples.gpu_mode.local_judge import evaluate_submission | |
| from examples.gpu_mode.official import original_question | |
| from examples.gpu_mode.single_anchor import merge_single_anchor, symbols, SPEC | |
| from ttt_discover.tinker_utils.program_graph import validate_program_graph, ProgramGraphError | |
| def invalid(message): | |
| return dict(reward=0., correctness=0., raw_score=-1_000_000., msg=message, | |
| result_construction=[], stdout="") | |
| class GpuModeRewardEvaluator(BaseRewardEvaluator): | |
| def __init__(self, *args, **kwargs): | |
| if kwargs.get("problem_type") != "trimul": | |
| raise ValueError("This aligned local implementation supports TriMul only") | |
| self.eval_timeout = kwargs.get("eval_timeout", 530) | |
| def get_reward(self, code, state): | |
| # Preserve both upstream guards; judge the merged PROGRAM, not the patch. | |
| if "@triton.jit" not in code: | |
| return invalid("Code must contain @triton.jit.") | |
| if "identity" in code: | |
| return invalid("Identity kernel is not allowed.") | |
| result = evaluate_submission(code, timeout_seconds=self.eval_timeout) | |
| metrics = {"trimul/" + key: result.get(key) for key in | |
| ("request_id", "gpu_index", "gpu_uuid", "wall_seconds", "queue_seconds", "image", "manifest_sha256")} | |
| return dict(reward=result["reward"], correctness=result["correctness"], raw_score=result["raw_score"], | |
| msg=f"Official TriMul geometric-mean runtime: {result['score_us']} us ({result['status']})", | |
| result_construction=[], stdout="", metrics=metrics) | |
| class GpuModeEnv(Environment): | |
| state_type = State | |
| reward_function = GpuModeRewardEvaluator | |
| def create_initial_state(cls, problem_type): | |
| if problem_type != "trimul": | |
| raise ValueError("Expected trimul") | |
| return State(timestep=-1, code="", value=-1_000_000, construction=None) | |
| def is_maximize(self): | |
| return False | |
| def _should_keep_code_separators(self): | |
| return False | |
| def get_question(self): | |
| return original_question()(self) | |
| async def _safe_grade(self, given_answer, step): | |
| # Waiting for the two GPU workers is not a candidate timeout. A failed | |
| # image/occupied GPU aborts instead of becoming a misleading RL failure. | |
| loop = asyncio.get_running_loop() | |
| return await loop.run_in_executor(SAFE_GRADE_EXECUTOR, self._run_verification, | |
| given_answer, self.problem_type, self.log_path, self.state) | |
| def _build_metrics(self, outs, correct_format, message, parsed_code): | |
| result = super()._build_metrics(outs, correct_format, message, parsed_code) | |
| result.update(outs.metrics or {}) | |
| return result | |
| class SingleAnchorTriMulState(State): | |
| def __init__(self, *args, step0_source="", bootstrap_winner_id=None, patch_history=None, **kwargs): | |
| super().__init__(*args, **kwargs) | |
| self.step0_source = step0_source | |
| self.bootstrap_winner_id = bootstrap_winner_id | |
| self.patch_history = list(patch_history or []) | |
| def from_root(cls, row): | |
| validate_program_graph(row["code"], SPEC) | |
| return cls(timestep=0, code=row["code"], value=float(row["value"]), construction=[], | |
| step0_source=row["code"], bootstrap_winner_id=row["id"]) | |
| def to_dict(self): | |
| return {**super().to_dict(), "type": type(self).__name__, "step0_source": self.step0_source, | |
| "bootstrap_winner_id": self.bootstrap_winner_id, "patch_history": self.patch_history} | |
| def from_dict(cls, row): | |
| return cls(**{key: value for key, value in row.items() if key != "type"}) | |
| class SingleAnchorTriMulRewardEvaluator(GpuModeRewardEvaluator): | |
| def get_reward(self, code, state): | |
| try: | |
| merged = merge_single_anchor(state.code, code) | |
| except ProgramGraphError as exc: | |
| return invalid(f"Invalid single-anchor patch: {exc}") | |
| return super().get_reward(merged.source, state) | |
| class SingleAnchorTriMulEnv(GpuModeEnv): | |
| state_type = SingleAnchorTriMulState | |
| reward_function = SingleAnchorTriMulRewardEvaluator | |
| def create_initial_state(cls, problem_type): | |
| raise ValueError("Use the verified shared baseline Step-0; no hand-optimized root") | |
| def get_question(self): | |
| base = super().get_question() | |
| inventory = ", ".join(symbols(self.initial_state.code)) | |
| return base + f''' | |
| ## Single-anchor program editing | |
| The code above is the CURRENT complete accumulated program. The verifier runs | |
| the merged complete program ONCE; it does not execute an old solver first. | |
| Return a Python PATCH, not the whole program: | |
| 1. Change exactly ONE existing top-level function or constant. Existing symbols: {inventory} | |
| 2. Omit every other existing definition; unchanged code is retained automatically. | |
| 3. You may add new helpers/kernels/constants reachable from your one changed anchor. | |
| 4. Triton decorators, tl.constexpr annotations and launch syntax are supported. | |
| 5. custom_kernel itself is editable, including calling a new fused kernel. | |
| 6. Keep the original task, correctness, precision and hardware rules unchanged. | |
| ''' | |
| def check_format(self, parsed_code): | |
| try: | |
| merge_single_anchor(self.initial_state.code, parsed_code) | |
| self._patch_error = None | |
| return True | |
| except (ProgramGraphError, SyntaxError) as exc: | |
| self._patch_error = str(exc) | |
| return False | |
| def _create_next_state(self, step_idx, parsed_code, outs): | |
| child = merge_single_anchor(self.initial_state.code, parsed_code) | |
| parent = self.initial_state | |
| return self.state_type(timestep=step_idx, construction=[], code=child.source, | |
| value=-outs.raw_score, observation=outs.stdout, step0_source=parent.step0_source, | |
| bootstrap_winner_id=parent.bootstrap_winner_id, | |
| patch_history=[*parent.patch_history, parsed_code]) | |
| def _build_metrics(self, outs, correct_format, message, parsed_code): | |
| result = super()._build_metrics(outs, correct_format, message, parsed_code) | |
| result.update({"single_anchor_pfg/enabled": 1, "single_anchor_pfg/variant": "trimul_program", | |
| "single_anchor_pfg/policy_update_allowed": 1}) | |
| if correct_format: | |
| child = merge_single_anchor(self.initial_state.code, parsed_code) | |
| result.update({"single_anchor_pfg/anchor_symbol": child.anchor, | |
| "single_anchor_pfg/new_symbols": list(child.added_symbols), | |
| "single_anchor_pfg/patch_chars": len(parsed_code), | |
| "single_anchor_pfg/child_chars": len(child.source), | |
| "single_anchor_pfg/step0_sha256": hashlib.sha256(self.initial_state.step0_source.encode()).hexdigest()}) | |
| return result | |
Xet Storage Details
- Size:
- 7.26 kB
- Xet hash:
- e2b1e352e5c4ebb58e22f415c340961e568627126ca88105f2cd3f7de48fa9eb
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.