File size: 11,077 Bytes
a376829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2025 Dhruv Nair. All rights reserved.
# Licensed under the Apache License, Version 2.0

"""
RF3 (RosettaFold3) Transformer model.

A diffusers-compatible wrapper around the foundry RF3 model components.
Reuses FeatureInitializer, Recycler, DiffusionModule, and DistogramHead
from ``rf3.model.*`` directly, adding only the ModelMixin/ConfigMixin
interface needed for diffusers ModularPipeline integration.

RF3 is structurally similar to RFD3 but adds a trunk recycler (48
pairformer blocks + MSA + templates) for sequence-conditioned folding.
"""

from dataclasses import dataclass
from typing import Optional

import torch
import torch.nn as nn

from diffusers.configuration_utils import ConfigMixin, register_to_config
from diffusers.models.modeling_utils import ModelMixin

from rf3.model.RF3_structure import DiffusionModule, DistogramHead, Recycler
from rf3.model.layers.pairformer_layers import FeatureInitializer


@dataclass
class RF3TransformerOutput:
    """Output class for RF3 transformer."""

    xyz: torch.Tensor                                       # [D, L, 3]
    distogram: Optional[torch.Tensor] = None                # [I, I, bins]
    single: Optional[torch.Tensor] = None                   # [I, c_s]
    pair: Optional[torch.Tensor] = None                     # [I, I, c_z]
    trajectory_noisy: Optional[list] = None                 # list of [D, L, 3]
    trajectory_denoised: Optional[list] = None              # list of [D, L, 3]


class RF3TransformerModel(ModelMixin, ConfigMixin):
    """
    Diffusers-compatible wrapper around the foundry RF3 model.

    Wraps FeatureInitializer, Recycler, DiffusionModule, and DistogramHead
    to provide a diffusers ModelMixin/ConfigMixin interface.

    State dict keys match the foundry checkpoint format via the
    ``feature_initializer.*``, ``recycler.*``, ``diffusion_module.*``,
    and ``distogram_head.*`` prefixes.
    """

    config_name = "config.json"
    _supports_gradient_checkpointing = True

    @register_to_config
    def __init__(
        self,
        c_s: int = 384,
        c_z: int = 128,
        c_atom: int = 128,
        c_atompair: int = 16,
        c_s_inputs: int = 449,
        c_token: int = 768,
        sigma_data: float = 16.0,
        n_pairformer_blocks: int = 48,
        n_diffusion_blocks: int = 24,
        n_atom_encoder_blocks: int = 3,
        n_atom_decoder_blocks: int = 3,
        n_msa_blocks: int = 4,
        n_template_blocks: int = 2,
        n_head: int = 16,
        n_pairformer_head: int = 16,
        n_recycles: int = 10,
        distogram_bins: int = 65,
        p_drop: float = 0.25,
    ):
        super().__init__()

        # ── FeatureInitializer ──────────────────────────────────────────
        self.feature_initializer = FeatureInitializer(
            c_s=c_s,
            c_z=c_z,
            c_atom=c_atom,
            c_atompair=c_atompair,
            c_s_inputs=c_s_inputs,
            input_feature_embedder={
                "features": ["restype", "profile", "deletion_mean"],
                "atom_attention_encoder": {
                    "c_token": c_s,
                    "c_atom_1d_features": 389,
                    "c_tokenpair": c_z,
                    "use_inv_dist_squared": True,
                    "atom_1d_features": [
                        "ref_pos", "ref_charge", "ref_mask",
                        "ref_element", "ref_atom_name_chars",
                    ],
                    "atom_transformer": {
                        "n_queries": 32,
                        "n_keys": 128,
                        "diffusion_transformer": {
                            "n_block": 3,
                            "diffusion_transformer_block": {
                                "n_head": 4,
                                "no_residual_connection_between_attention_and_transition": True,
                                "kq_norm": True,
                            },
                        },
                    },
                },
            },
            relative_position_encoding={"r_max": 32, "s_max": 2},
        )

        # ── Recycler (trunk) ───────────────────────────────────────────
        self.recycler = Recycler(
            c_s=c_s,
            c_z=c_z,
            n_pairformer_blocks=n_pairformer_blocks,
            pairformer_block={
                "p_drop": p_drop,
                "triangle_multiplication": {"d_hidden": 128},
                "triangle_attention": {"n_head": 4, "d_hidden": 32},
                "attention_pair_bias": {"n_head": n_head},
            },
            template_embedder={
                "n_block": n_template_blocks,
                "raw_template_dim": 108,
                "c": 64,
                "p_drop": p_drop,
            },
            msa_module={
                "n_block": n_msa_blocks,
                "c_m": 64,
                "p_drop_msa": 0.15,
                "p_drop_pair": p_drop,
                "msa_subsample_embedder": {
                    "num_sequences": 1024,
                    "dim_raw_msa": 34,
                    "c_s_inputs": c_s_inputs,
                    "c_msa_embed": 64,
                },
                "outer_product": {
                    "c_msa_embed": 64,
                    "c_outer_product": 32,
                    "c_out": c_z,
                },
                "msa_pair_weighted_averaging": {
                    "n_heads": 8,
                    "c_weighted_average": 32,
                    "c_msa_embed": 64,
                    "c_z": c_z,
                    "separate_gate_for_every_channel": True,
                },
                "msa_transition": {"n": 4, "c": 64},
                "triangle_multiplication_outgoing": {
                    "d_pair": c_z, "d_hidden": 128, "bias": True,
                },
                "triangle_multiplication_incoming": {
                    "d_pair": c_z, "d_hidden": 128, "bias": True,
                },
                "triangle_attention_starting": {
                    "d_pair": c_z, "n_head": 4, "d_hidden": 32, "p_drop": 0.0,
                },
                "triangle_attention_ending": {
                    "d_pair": c_z, "n_head": 4, "d_hidden": 32, "p_drop": 0.0,
                },
                "pair_transition": {"n": 4, "c": c_z},
            },
        )

        # ── DiffusionModule ────────────────────────────────────────────
        self.diffusion_module = DiffusionModule(
            sigma_data=sigma_data,
            c_atom=c_atom,
            c_atompair=c_atompair,
            c_token=c_token,
            c_s=c_s,
            c_z=c_z,
            diffusion_conditioning={
                "c_s_inputs": c_s_inputs,
                "c_t_embed": 256,
                "relative_position_encoding": {"r_max": 32, "s_max": 2},
            },
            atom_attention_encoder={
                "c_tokenpair": c_z,
                "c_atom_1d_features": 389,
                "use_inv_dist_squared": True,
                "atom_1d_features": [
                    "ref_pos", "ref_charge", "ref_mask",
                    "ref_element", "ref_atom_name_chars",
                ],
                "atom_transformer": {
                    "n_queries": 32,
                    "n_keys": 128,
                    "diffusion_transformer": {
                        "n_block": n_atom_encoder_blocks,
                        "diffusion_transformer_block": {
                            "n_head": 4,
                            "no_residual_connection_between_attention_and_transition": True,
                            "kq_norm": True,
                        },
                    },
                },
                "broadcast_trunk_feats_on_1dim_old": False,
                "use_chiral_features": True,
                "no_grad_on_chiral_center": False,
            },
            diffusion_transformer={
                "n_block": n_diffusion_blocks,
                "diffusion_transformer_block": {
                    "n_head": n_head,
                    "no_residual_connection_between_attention_and_transition": True,
                    "kq_norm": True,
                },
            },
            atom_attention_decoder={
                "atom_transformer": {
                    "n_queries": 32,
                    "n_keys": 128,
                    "diffusion_transformer": {
                        "n_block": n_atom_decoder_blocks,
                        "diffusion_transformer_block": {
                            "n_head": 4,
                            "no_residual_connection_between_attention_and_transition": True,
                            "kq_norm": True,
                        },
                    },
                },
            },
        )

        # ── DistogramHead ──────────────────────────────────────────────
        self.distogram_head = DistogramHead(c_z=c_z, bins=distogram_bins)

        self._n_recycles = n_recycles

    def forward(
        self,
        f: dict,
        n_recycles: Optional[int] = None,
        diffusion_batch_size: int = 1,
        coord_atom_lvl_to_be_noised: Optional[torch.Tensor] = None,
    ) -> RF3TransformerOutput:
        """
        Forward pass: recycling trunk β†’ diffusion sampling.

        Args:
            f: Feature dictionary (sequence, MSA, templates, atom features).
            n_recycles: Number of recycling iterations (default: config value).
            diffusion_batch_size: Number of diffusion samples.
            coord_atom_lvl_to_be_noised: Initial coordinates for partial diffusion.

        Returns:
            RF3TransformerOutput with predicted coordinates and distogram.
        """
        n_recycles = n_recycles or self._n_recycles

        # Pre-recycle: initialize features
        initialized = self.feature_initializer(f)
        S_inputs_I = initialized["S_inputs_I"]
        S_I = initialized.get("S_init_I", initialized.get("S_I"))
        Z_II = initialized.get("Z_init_II", initialized.get("Z_II"))

        # Recycling trunk
        for i in range(n_recycles):
            ctx = torch.no_grad() if i < n_recycles - 1 else torch.enable_grad()
            with ctx:
                recycled = self.recycler(
                    S_I=S_I,
                    Z_II=Z_II,
                    S_inputs_I=S_inputs_I,
                    f=f,
                )
                S_I = recycled["S_I"]
                Z_II = recycled["Z_II"]

        # Distogram prediction
        distogram = self.distogram_head(Z_II)

        return RF3TransformerOutput(
            xyz=torch.zeros(1),  # placeholder β€” filled by sampler in denoise step
            distogram=distogram,
            single=S_I,
            pair=Z_II,
        )