ayush1801's picture
Upload folder using huggingface_hub
f1adc24 verified
Raw
History Blame
4.29 kB
#!/usr/bin/env python3
from dataclasses import dataclass
from typing import Optional
import traceback
import os
import torch
from omegaconf import DictConfig
from celery import Celery
import peano
import proofsearch
import policy
import hindsight
@dataclass
class StudentResult:
error: Optional[str]
success: bool
problem: str
solution_actions: Optional[list[str]]
proof: Optional[list[str]]
extracted_examples: list[str]
hindsight_examples: list[hindsight.HindsightExample]
iterations: int
logprob: float
timed_out: bool = False
proof_features: Optional[dict] = None
@dataclass
class BackgroundTheory:
theory: str
premises: list[str]
redis_url = f'redis://{os.environ.get("REDIS", "localhost")}'
app = Celery('worker', backend=redis_url, broker=redis_url)
# app.conf.task_acks_late = True
# app.conf.task_reject_on_worker_lost = True
app.conf.task_serializer = 'pickle'
app.conf.result_serializer = 'pickle'
app.conf.accept_content = ['application/json', 'application/x-python-serialize']
app.conf.result_expires = 3600
@app.task
def try_prove(agent_path: str, theory: BackgroundTheory, statement: str, timeout: float = None) -> StudentResult:
agent = torch.load(agent_path, weights_only=False)
print('Proving', statement, 'on', agent._policy._lm._lm.device,
f'(timeout={timeout}s)' if timeout else '')
state = peano.PyProofState(theory.theory,
theory.premises,
statement)
try:
agent_result = agent.proof_search(statement, state, timeout=timeout)
if agent_result.timed_out:
print(f'Timed out on: {statement}')
# Still extract hindsight examples from the partial search tree —
# MCTS may have found solved subgoals even if the full proof wasn't found.
try:
hindsight_examples = hindsight.extract_hindsight_examples(
agent_result.root,
theory.theory,
theory.premises,
agent._policy)
except BaseException:
hindsight_examples = []
return StudentResult(
None,
False,
statement,
None,
None,
agent_result.examples,
hindsight_examples,
agent_result.iterations,
None,
timed_out=True,
)
if agent_result.success:
proof = agent_result.root.state_node.reconstruct_proof(
agent_result.root.get_solution_actions())
solution_actions = agent_result.root.get_solution_actions()
logprob = agent_result.root.solution_logprob_under_policy(agent._policy, solution_actions)
try:
proof_features = proofsearch.extract_proof_features(
agent_result.root, solution_actions, agent_result.mcts_iters)
except Exception as e:
print(f'Warning: failed to extract proof features: {e}')
proof_features = None
else:
solution_actions, proof, logprob = None, None, None
proof_features = None
examples = []
# Policy examples for the proved goal.
examples.extend(agent._policy.extract_examples(root=agent_result.root))
# Hindsight examples (policy + conjecturing).
hindsight_examples = hindsight.extract_hindsight_examples(
agent_result.root,
theory.theory,
theory.premises,
agent._policy)
return StudentResult(
None,
agent_result.success,
statement,
list(map(str, solution_actions)) if solution_actions else None,
proof,
agent_result.examples,
hindsight_examples,
agent_result.iterations,
logprob,
proof_features=proof_features,
)
except BaseException as e:
tb = traceback.format_exception(e)
print('Error in try_prove!')
print(tb)
return StudentResult(tb, False, statement, None, None, [],
None, None, None)