File size: 11,068 Bytes
930ea3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# model.py
from __future__ import annotations

from typing import List, Optional, Literal

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.data import Batch

from src.conv import build_gnn_encoder, GNNEncoder


def get_activation(name: str) -> nn.Module:
    name = name.lower()
    if name == "relu":
        return nn.ReLU()
    if name == "gelu":
        return nn.GELU()
    if name == "silu":
        return nn.SiLU()
    if name in ("leaky_relu", "lrelu"):
        return nn.LeakyReLU(0.1)
    raise ValueError(f"Unknown activation: {name}")


class FiLM(nn.Module):
    """
    Simple FiLM: gamma, beta from condition vector; apply to features as (1+gamma)*h + beta
    """
    def __init__(self, feat_dim: int, cond_dim: int):
        super().__init__()
        self.gamma = nn.Linear(cond_dim, feat_dim)
        self.beta = nn.Linear(cond_dim, feat_dim)

    def forward(self, h: torch.Tensor, cond: torch.Tensor) -> torch.Tensor:
        g = self.gamma(cond)
        b = self.beta(cond)
        return (1.0 + g) * h + b


class TaskHead(nn.Module):
    """
    Per-task MLP head. Input is concatenation of [graph_embed, optional task_embed].
    Outputs either a mean only (scalar) or mean+logvar (heteroscedastic).
    """
    def __init__(
        self,
        in_dim: int,
        hidden_dim: int = 512,
        depth: int = 2,
        act: str = "relu",
        dropout: float = 0.0,
        heteroscedastic: bool = False,
    ):
        super().__init__()
        layers: List[nn.Module] = []
        d = in_dim
        for _ in range(depth):
            layers.append(nn.Linear(d, hidden_dim))
            layers.append(get_activation(act))
            if dropout > 0:
                layers.append(nn.Dropout(dropout))
            d = hidden_dim
        out_dim = 2 if heteroscedastic else 1
        layers.append(nn.Linear(d, out_dim))
        self.net = nn.Sequential(*layers)
        self.hetero = heteroscedastic

    def forward(self, z: torch.Tensor) -> torch.Tensor:
        # returns [B, 1] or [B, 2] where [...,0] is mean and [...,1] is logvar if heteroscedastic
        return self.net(z)


class MultiTaskMultiFidelityModel(nn.Module):
    """
    General multi-task, multi-fidelity GNN.

    - Any number of tasks (properties) via T = len(task_names)
    - Any number of fidelities via num_fids
    - Fidelity conditioning with an embedding and FiLM on the graph embedding
    - Optional task embeddings concatenated into each task head input
    - Single forward returning predictions [B, T] (means); if heteroscedastic, also returns log-variances

    Expected input Batch fields (PyG):
      - x          : [N_nodes, F_node]
      - edge_index : [2, N_edges]
      - edge_attr  : [N_edges, F_edge] (required if gnn_type="gine")
      - batch      : [N_nodes]
      - fid_idx    : [B] or [B, 1] long; integer fidelity per graph

    Notes:
      - Targets should already be normalized outside the model; apply inverse transform for plots.
      - Loss weighting/equal-importance and curriculum happen in the trainer, not here.
    """

    def __init__(
        self,
        in_dim_node: int,
        in_dim_edge: int,
        task_names: List[str],
        num_fids: int,
        gnn_type: Literal["gine", "gin", "gcn"] = "gine",
        gnn_emb_dim: int = 256,
        gnn_layers: int = 5,
        gnn_norm: Literal["batch", "layer", "none"] = "batch",
        gnn_readout: Literal["mean", "sum", "max"] = "mean",
        gnn_act: str = "relu",
        gnn_dropout: float = 0.0,
        gnn_residual: bool = True,
        # Fidelity conditioning
        fid_emb_dim: int = 64,
        use_film: bool = True,
        # Task conditioning
        use_task_embed: bool = True,
        task_emb_dim: int = 32,
        # Heads
        head_hidden: int = 512,
        head_depth: int = 2,
        head_act: str = "relu",
        head_dropout: float = 0.0,
        heteroscedastic: bool = False,
        # Optional homoscedastic task uncertainty (used in loss, kept here for checkpoint parity)
        use_task_uncertainty: bool = False,
        # Embedding regularization (used via regularization_loss)
        fid_emb_l2: float = 0.0,
        task_emb_l2: float = 0.0,
    ):
        super().__init__()
        self.task_names = list(task_names)
        self.num_tasks = len(task_names)
        self.num_fids = int(num_fids)
        self.hetero = heteroscedastic
        self.fid_emb_l2 = float(fid_emb_l2)
        self.task_emb_l2 = float(task_emb_l2)
        self.use_film = use_film
        self.use_task_embed = use_task_embed

        # Optional learned homoscedastic uncertainty per task (trainer may use it)
        self.use_task_uncertainty = bool(use_task_uncertainty)
        if self.use_task_uncertainty:
            self.task_log_sigma2 = nn.Parameter(torch.zeros(self.num_tasks))
        else:
            self.task_log_sigma2 = None

        # Encoder
        self.encoder: GNNEncoder = build_gnn_encoder(
            in_dim_node=in_dim_node,
            emb_dim=gnn_emb_dim,
            num_layers=gnn_layers,
            gnn_type=gnn_type,
            in_dim_edge=in_dim_edge,
            act=gnn_act,
            dropout=gnn_dropout,
            residual=gnn_residual,
            norm=gnn_norm,
            readout=gnn_readout,
        )

        # Fidelity embedding + FiLM
        self.fid_embed = nn.Embedding(self.num_fids, fid_emb_dim) if fid_emb_dim > 0 else None
        self.film = FiLM(gnn_emb_dim, fid_emb_dim) if (use_film and fid_emb_dim > 0) else None

        # --- Compute the true feature dim sent to heads ---
        # If FiLM is ON: g stays [B, gnn_emb_dim]
        # If FiLM is OFF but fid_embed exists: we CONCAT c → g becomes [B, gnn_emb_dim + fid_emb_dim]
        self.gnn_out_dim = gnn_emb_dim + (fid_emb_dim if (self.fid_embed is not None and self.film is None) else 0)

        # Task embeddings
        self.task_embed = nn.Embedding(self.num_tasks, task_emb_dim) if (use_task_embed and task_emb_dim > 0) else None

        # Per-task heads
        head_in_dim = self.gnn_out_dim + (task_emb_dim if self.task_embed is not None else 0)
        self.heads = nn.ModuleList([
            TaskHead(
                in_dim=head_in_dim,
                hidden_dim=head_hidden,
                depth=head_depth,
                act=head_act,
                dropout=head_dropout,
                heteroscedastic=heteroscedastic,
            ) for _ in range(self.num_tasks)
        ])


    def reset_parameters(self):
        if self.fid_embed is not None:
            nn.init.normal_(self.fid_embed.weight, mean=0.0, std=0.02)
        if self.task_embed is not None:
            nn.init.normal_(self.task_embed.weight, mean=0.0, std=0.02)
        # Encoder/heads rely on their internal initializations.

    def forward(self, data: Batch) -> dict:
        """
        Returns:
          {
            "pred":   [B, T] means,
            "logvar": [B, T] optional if heteroscedastic,
            "h":      [B, D] graph embedding after FiLM (useful for diagnostics).
          }
        """
        x, edge_index = data.x, data.edge_index
        edge_attr = getattr(data, "edge_attr", None)
        batch = data.batch
        if edge_attr is None and hasattr(self.encoder, "gnn_type") and self.encoder.gnn_type == "gine":
            raise ValueError("GINE encoder requires edge_attr, but Batch.edge_attr is None.")

        # Graph embedding
        g = self.encoder(x, edge_index, edge_attr, batch)  # [B, D]

        # Fidelity conditioning
        fid_idx = data.fid_idx.view(-1).long()  # [B]
        if self.fid_embed is not None:
            c = self.fid_embed(fid_idx)  # [B, C]
            if self.film is not None:
                g = self.film(g, c)  # [B, D]
            else:
                g = torch.cat([g, c], dim=-1)

        # Per-task heads
        preds: List[torch.Tensor] = []
        logvars: Optional[List[torch.Tensor]] = [] if self.hetero else None
        for t_idx, head in enumerate(self.heads):
            if self.task_embed is not None:
                tvec = self.task_embed.weight[t_idx].unsqueeze(0).expand(g.size(0), -1)
                z = torch.cat([g, tvec], dim=-1)
            else:
                z = g
            out = head(z)  # [B, 1] or [B, 2]
            if self.hetero:
                mu = out[..., 0:1]
                lv = out[..., 1:2]
                preds.append(mu)
                logvars.append(lv)  # type: ignore[arg-type]
            else:
                preds.append(out)

        pred = torch.cat(preds, dim=-1)  # [B, T]
        result = {"pred": pred, "h": g}
        if self.hetero and logvars is not None:
            result["logvar"] = torch.cat(logvars, dim=-1)  # [B, T]
        return result

    def regularization_loss(self) -> torch.Tensor:
        """
        Optional small L2 on embeddings to keep them bounded.
        """
        device = next(self.parameters()).device
        reg = torch.zeros([], device=device)
        if self.fid_embed is not None and self.fid_emb_l2 > 0:
            reg = reg + self.fid_emb_l2 * (self.fid_embed.weight.pow(2).mean())
        if self.task_embed is not None and self.task_emb_l2 > 0:
            reg = reg + self.task_emb_l2 * (self.task_embed.weight.pow(2).mean())
        return reg


def build_model(
    *,
    in_dim_node: int,
    in_dim_edge: int,
    task_names: List[str],
    num_fids: int,
    gnn_type: Literal["gine", "gin", "gcn"] = "gine",
    gnn_emb_dim: int = 256,
    gnn_layers: int = 5,
    gnn_norm: Literal["batch", "layer", "none"] = "batch",
    gnn_readout: Literal["mean", "sum", "max"] = "mean",
    gnn_act: str = "relu",
    gnn_dropout: float = 0.0,
    gnn_residual: bool = True,
    fid_emb_dim: int = 64,
    use_film: bool = True,
    use_task_embed: bool = True,
    task_emb_dim: int = 32,
    head_hidden: int = 512,
    use_task_uncertainty: bool = False,
    head_depth: int = 2,
    head_act: str = "relu",
    head_dropout: float = 0.0,
    heteroscedastic: bool = False,
    fid_emb_l2: float = 0.0,
    task_emb_l2: float = 0.0,
) -> MultiTaskMultiFidelityModel:
    """
    Factory to construct the multi-task, multi-fidelity model with a consistent API.
    """
    return MultiTaskMultiFidelityModel(
        in_dim_node=in_dim_node,
        in_dim_edge=in_dim_edge,
        task_names=task_names,
        num_fids=num_fids,
        gnn_type=gnn_type,
        gnn_emb_dim=gnn_emb_dim,
        gnn_layers=gnn_layers,
        gnn_norm=gnn_norm,
        gnn_readout=gnn_readout,
        gnn_act=gnn_act,
        gnn_dropout=gnn_dropout,
        gnn_residual=gnn_residual,
        fid_emb_dim=fid_emb_dim,
        use_film=use_film,
        use_task_embed=use_task_embed,
        task_emb_dim=task_emb_dim,
        head_hidden=head_hidden,
        head_depth=head_depth,
        head_act=head_act,
        head_dropout=head_dropout,
        heteroscedastic=heteroscedastic,
        fid_emb_l2=fid_emb_l2,
        task_emb_l2=task_emb_l2,
        use_task_uncertainty=use_task_uncertainty,
    )