File size: 4,291 Bytes
f1adc24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/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)