File size: 8,755 Bytes
3d87d8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""AAM Diffusion LLM — MCTS Reasoning Engine

AlphaZero-style tree search for reasoning about narrative arrangement
from graph evidence. AAM-specific: each node = a sentence arrangement,
value = narrative coherence, policy = next arrangement step.
"""

from __future__ import annotations

import math
from dataclasses import dataclass, field
from typing import Optional, List, Dict, Any, Tuple

import torch
import torch.nn as nn
import torch.nn.functional as F


class MCTSConfig:
    def __init__(
        self,
        num_simulations: int = 64,
        c_puct: float = 1.5,
        temperature: float = 1.0,
        max_depth: int = 10,
        use_value_network: bool = True,
        use_progressive_widening: bool = True,
        max_children: int = 8,
    ) -> None:
        self.num_simulations = num_simulations
        self.c_puct = c_puct
        self.temperature = temperature
        self.max_depth = max_depth
        self.use_value_network = use_value_network
        self.use_progressive_widening = use_progressive_widening
        self.max_children = max_children

        if num_simulations <= 0:
            raise ValueError(f"num_simulations must be positive, got {num_simulations}")
        if c_puct <= 0:
            raise ValueError(f"c_puct must be positive, got {c_puct}")
        if max_depth <= 0:
            raise ValueError(f"max_depth must be positive, got {max_depth}")


@dataclass
class MCTSNode:
    state: Optional[torch.Tensor] = None
    parent: Optional["MCTSNode"] = None
    children: List["MCTSNode"] = field(default_factory=list)
    visit_count: int = 0
    total_value: float = 0.0
    prior: float = 0.0
    depth: int = 0
    is_expanded: bool = False
    action: Optional[int] = None
    hidden_state: Optional[torch.Tensor] = None

    @property
    def q_value(self) -> float:
        if self.visit_count == 0:
            return 0.0
        return self.total_value / self.visit_count

    @property
    def is_leaf(self) -> bool:
        return not self.is_expanded

    @property
    def is_root(self) -> bool:
        """Whether this node is the root."""
        return self.parent is None


class ValueNetwork(nn.Module):
    """Evaluate narrative coherence of a state."""
    def __init__(self, d_model: int, hidden_dim: int = 256) -> None:
        super().__init__()
        self.network = nn.Sequential(
            nn.Linear(d_model, hidden_dim, bias=False),
            nn.SiLU(),
            nn.Linear(hidden_dim, hidden_dim // 2, bias=False),
            nn.SiLU(),
            nn.Linear(hidden_dim // 2, 1, bias=False),
            nn.Tanh(),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.network(x)


class PolicyNetwork(nn.Module):
    """Suggest next arrangement step."""
    def __init__(self, d_model: int, num_actions: int = 8) -> None:
        super().__init__()
        self.network = nn.Sequential(
            nn.Linear(d_model, d_model // 2, bias=False),
            nn.SiLU(),
            nn.Linear(d_model // 2, num_actions, bias=False),
        )
        self.num_actions = num_actions

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.network(x)


class MCTSReasoner(nn.Module):
    """MCTS Reasoning Engine for AAM sentence arrangement."""

    def __init__(
        self,
        d_model: int,
        num_actions: int = 8,
        config: Optional[MCTSConfig] = None,
    ) -> None:
        super().__init__()
        self.d_model = d_model
        self.num_actions = num_actions
        self.config = config or MCTSConfig()

        if self.config.use_value_network:
            self.value_network = ValueNetwork(d_model)
        else:
            self.value_network = None

        self.policy_network = PolicyNetwork(d_model, num_actions)

        self.state_encoder = nn.Sequential(
            nn.Linear(d_model, d_model, bias=False),
            nn.SiLU(),
            nn.Linear(d_model, d_model, bias=False),
        )

    def forward(
        self,
        x: torch.Tensor,
        num_simulations: Optional[int] = None,
    ) -> Tuple[torch.Tensor, Dict[str, Any]]:
        batch_size = x.shape[0]
        n_sims = num_simulations or self.config.num_simulations

        encoded_state = self.state_encoder(x)
        # Pool over sequence dimension if 3D input
        if encoded_state.dim() == 3:
            pooled_state = encoded_state.mean(dim=1)  # (batch, d_model)
        else:
            pooled_state = encoded_state  # (batch, d_model)

        policy_logits = self.policy_network(pooled_state)  # (batch, num_actions)
        policy_probs = F.softmax(policy_logits / self.config.temperature, dim=-1)  # (batch, num_actions)

        if self.value_network is not None:
            root_value = self.value_network(pooled_state)  # (batch, 1)
        else:
            root_value = torch.zeros(batch_size, 1, device=x.device)

        visit_counts = torch.zeros(batch_size, self.num_actions, device=x.device, dtype=torch.float32)
        total_values = torch.zeros(batch_size, self.num_actions, device=x.device, dtype=torch.float32)

        for sim_idx in range(n_sims):
            ucb_scores = self._compute_ucb(visit_counts, total_values, policy_probs, n_sims)
            selected_actions = ucb_scores.argmax(dim=-1)

            if self.value_network is not None:
                action_onehot = F.one_hot(selected_actions, self.num_actions).float()
                action_proj = action_onehot @ self.policy_network.network[-1].weight
                padding = torch.zeros(batch_size, self.d_model - action_proj.shape[-1], device=x.device)
                action_embedding = torch.cat([action_proj, padding], dim=-1)
                state_action = pooled_state + action_embedding
                sim_values = self.value_network(state_action)
            else:
                sim_values = torch.rand(batch_size, 1, device=x.device) * 2 - 1

            visit_counts.scatter_add_(1, selected_actions.unsqueeze(1),
                torch.ones(batch_size, 1, device=x.device, dtype=visit_counts.dtype))
            total_values.scatter_add_(1, selected_actions.unsqueeze(1), sim_values)

        if self.config.temperature > 0:
            action_probs = F.softmax(visit_counts.log() / self.config.temperature, dim=-1)
            action_probs = torch.where(visit_counts > 0, action_probs, torch.zeros_like(action_probs))
            row_sums = action_probs.sum(dim=-1, keepdim=True)
            action_probs = torch.where(
                row_sums > 1e-6,
                action_probs / (row_sums + 1e-8),
                torch.full_like(action_probs, 1.0 / self.num_actions),
            )
        else:
            action_probs = F.one_hot(visit_counts.argmax(dim=-1), self.num_actions).float()

        info = {
            "total_simulations": n_sims,
            "root_value": root_value.mean().item(),
            "max_visit_count": visit_counts.max().item(),
            "entropy": -(action_probs * (action_probs + 1e-8).log()).sum(-1).mean().item(),
            "visit_distribution": visit_counts / (visit_counts.sum(-1, keepdim=True) + 1e-8),
        }

        return action_probs, info

    def _compute_ucb(self, visit_counts, total_values, priors, total_simulations):
        q_values = torch.where(
            visit_counts > 0,
            total_values / (visit_counts + 1e-8),
            torch.zeros_like(total_values),
        )
        parent_visits = visit_counts.sum(dim=-1, keepdim=True)
        exploration = self.config.c_puct * priors * torch.sqrt(parent_visits + 1) / (1 + visit_counts)
        return q_values + exploration

    def compute_thinking_budget(self, complexity_score: float, base_simulations: int = 16, max_simulations: int = 256) -> int:
        """Compute number of MCTS simulations based on complexity.

        Adaptive compute budget: more complex inputs get more simulations.

        Args:
            complexity_score: Complexity score [0, 1] from ThinkingToggle.
            base_simulations: Minimum number of simulations.
            max_simulations: Maximum number of simulations.

        Returns:
            Recommended number of simulations.
        """
        return int(base_simulations + (max_simulations - base_simulations) * (complexity_score ** 2))

    def get_reasoning_summary(self, info: Dict[str, Any]) -> str:
        """Summary of reasoning for logging.

        Args:
            info: Dictionary from forward output.

        Returns:
            Summary string.
        """
        return (
            f"MCTS(sims={info['total_simulations']}, "
            f"root_val={info['root_value']:.3f}, "
            f"max_visits={info['max_visit_count']:.0f}, "
            f"entropy={info['entropy']:.3f})"
        )