zefancainlp's picture
download
raw
7.28 kB
"""Private-entry Kernel environment on unchanged native PUCT and MaxRL."""
from __future__ import annotations
from dataclasses import dataclass, replace
import math
from pathlib import Path
import chz
from api_transport import ROLLOUT_SLOT
from common import NEW_STEPS, atomic, read
from examples.gpu_mode.private_entry_env import PrivateEntryTriMulEnv
from ttt_discover.runtime.shared_step0 import SharedStepZeroSampler, load_step0
from ttt_discover.rl.types import ProblemGroupBuilder, RLDatasetBuilder
from ttt_discover.tinker_utils.dataset_builder import SingleProblemDataset, DatasetConfig
from ttt_discover.tinker_utils.sampler import get_or_create_sampler_with_default
RUNTIME = None
class Env(PrivateEntryTriMulEnv):
state_sampler_type = SharedStepZeroSampler
shared_step0_method = 'single_anchor_private'
@classmethod
def create_initial_state(cls, problem_type):
if problem_type != 'trimul' or RUNTIME is None:
raise ValueError('Verified original Kernel Step0 required')
return cls.state_type.from_root(cls.shared_step0_payload['best_candidate'])
def _run_verification(self, *args, **kwargs):
RUNTIME.judge.local.slot = self._private_slot
try:
return super()._run_verification(*args, **kwargs)
finally:
RUNTIME.judge.local.slot = None
async def initial_observation(self):
ROLLOUT_SLOT.set(self._private_slot)
return await super().initial_observation()
def _record_function_analysis(self, step_idx, message, parsed_code, outs, metrics,
parse_success, correct_format, child_state=None):
super()._record_function_analysis(step_idx, message, parsed_code, outs, metrics,
parse_success, correct_format, child_state)
slot = self._private_slot
row = dict(slot=list(slot), parent_id=self.initial_state.id,
parent_latency_us=-self.initial_state.value,
state=child_state.to_dict() if child_state else None,
parse_success=bool(parse_success), correct_format=bool(correct_format),
edited_symbols=[], copied_bridges=[], model_patch=parsed_code,
response=str(message.get('content', '')), reward=float(outs.reward),
raw_score=float(outs.raw_score), correctness=float(outs.correctness), msg=outs.msg,
metrics=outs.metrics or {})
if correct_format:
merged = self._merge_patch(parsed_code)
row.update(entry=merged.anchor, edited_symbols=list(merged.edited_symbols),
private_symbols=merged.private_symbols, copied_bridges=list(merged.copied_bridges),
lowered_patch=merged.lowered_patch)
atomic(RUNTIME.root / 'run/transactions' / f's{slot[0]:02d}' / f'g{slot[1]:02d}_e{slot[2]:02d}.json', row)
@dataclass(frozen=True)
class Group(ProblemGroupBuilder):
initial_state: object = None
group_slot: int = -1
logical_step: int = -1
async def make_envs(self):
envs = await super().make_envs()
if len(envs) != 64 or not 1 <= self.logical_step <= NEW_STEPS or not 0 <= self.group_slot < 8:
raise RuntimeError('Wrong authorized 8x64/three-step sampling topology')
for i, env in enumerate(envs):
env._private_slot = (self.logical_step, self.group_slot, i)
return envs
Env.group_builder_type = Group
class Dataset(SingleProblemDataset):
def get_batch(self, index):
logical = int(self.sampler._current_step) + 1
if not 1 <= logical <= NEW_STEPS:
raise RuntimeError('No Step0 resampling or fourth batch')
if RUNTIME.ledger is not None:
RUNTIME.ledger.logical_step = logical
builders = [replace(b, group_slot=i, logical_step=logical) for i, b in enumerate(super().get_batch(index))]
if len(builders) != 8:
raise RuntimeError('Expected eight parents')
prompts = []
for builder in builders:
env = builder.env_thunk()
prompt = env.renderer.build_generation_prompt([dict(role='user', content=env.get_question())])
if prompt.length >= 26000:
raise RuntimeError('Selected parent exceeds unchanged phase1 context; never truncate/resample')
prompts.append(prompt.length)
atomic(RUNTIME.root / 'run/parent_batches' / f'{logical:02d}.json',
dict(logical_step=logical, parents=[b.initial_state.to_dict() for b in builders], prompt_tokens=prompts))
return builders
def flush(self, step=None):
super().flush(step)
if step is None or int(step) == 0:
return
states = [s for s in self.sampler._states if s.value is not None and math.isfinite(s.value)]
best = max(states, key=lambda s: s.value)
rows = [read(p) for p in sorted((RUNTIME.root / 'run/transactions' / f's{step:02d}').glob('*.json'))]
if len(rows) != 512:
raise RuntimeError('Cannot commit an incomplete original 512-candidate batch')
atomic(RUNTIME.root / 'run/step_summaries' / f'{step:02d}.json',
dict(logical_step=step, status='sampled_not_trained', original_episodes=512,
recorded_episodes=len(rows), syntactically_valid_transactions=sum(r['correct_format'] for r in rows),
fully_accepted=sum(r['correctness'] > 0 for r in rows),
coordinated_transactions=sum(len(r['edited_symbols']) > 1 for r in rows),
automatically_copied_bridges=sum(bool(r['copied_bridges']) for r in rows),
best_us=-best.value, best_state_id=best.id, synthetic_trajectories=0, extra_search_evaluations=0))
@chz.chz
class Builder(RLDatasetBuilder):
config: DatasetConfig
async def __call__(self):
from ttt_discover.tinker_utils.misc_utils import get_tokenizer
from ttt_discover.tinker_utils import renderers
tokenizer = get_tokenizer(self.config.model_name_for_tokenizer)
renderer = renderers.get_renderer(self.config.renderer_name, tokenizer=tokenizer)
sampler = get_or_create_sampler_with_default(log_path=self.config.log_path, env_type=self.config.env_type,
batch_size=8, problem_type='trimul', parent_selection_policy='puct',
resume_step=0 if Path(self.config.log_path, 'puct_sampler_step_000000.json').exists() else None)
return Dataset(self.config, renderer, sampler)
class Runtime:
def __init__(self, root):
self.root = Path(root)
self.plan = read(self.root / 'control/plan.json')
self.ledger = None
def configure(root):
global RUNTIME
RUNTIME = Runtime(root)
Env.shared_step0_payload = load_step0(Path(root) / 'control/shared_step0.json', 'trimul', RUNTIME.plan['step0_sha256'])
# Replace transport only; original evaluator/18 correctness cases/reward stay intact.
from pool_judge import Client
from examples.gpu_mode import env as kernel_env
RUNTIME.judge = Client(RUNTIME.plan['judge_url'], RUNTIME.plan['judge_token'], RUNTIME.root.name)
kernel_env.evaluate_submission = RUNTIME.judge.evaluate
return RUNTIME

Xet Storage Details

Size:
7.28 kB
·
Xet hash:
c1dd1186f2b9821dba605d01a0f684b7a14920521b564d4e93ffded6353f9bc1

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.