File size: 14,604 Bytes
4900749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
046a4b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4900749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
046a4b1
 
 
 
 
 
4900749
 
 
046a4b1
 
 
 
 
 
 
 
 
 
4900749
 
 
046a4b1
4900749
 
046a4b1
4900749
046a4b1
4900749
 
 
 
046a4b1
4900749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
046a4b1
 
 
 
 
 
 
 
4900749
046a4b1
 
 
 
 
 
 
 
 
 
 
4900749
046a4b1
 
 
 
4900749
046a4b1
4900749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
046a4b1
 
 
 
 
 
 
 
 
4900749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
046a4b1
 
4900749
 
 
 
 
 
 
046a4b1
 
4900749
 
 
 
 
 
 
 
 
046a4b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4900749
046a4b1
4900749
 
 
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Copyright 2025 Dhruv Nair. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import List, Tuple, Union

import torch

from diffusers.utils import logging
from diffusers.modular_pipelines import ModularPipeline, ModularPipelineBlocks, PipelineState
from diffusers.modular_pipelines.modular_pipeline_utils import ComponentSpec, InputParam, OutputParam


logger = logging.get_logger(__name__)

# Feature widths the checkpoint's token initializer was trained with, from
# rfd3/configs/model/components/rfd3_net.yaml. They set the input width of the embedding layers,
# so a mismatch raises on layer shape rather than silently degrading the design.
_TOKEN_1D_FEATURES = {"ref_motif_token_type": 3, "restype": 32, "ref_plddt": 1, "is_non_loopy": 1}
_ATOM_1D_FEATURES = {
    "ref_atom_name_chars": 256,
    "ref_element": 128,
    "ref_charge": 1,
    "ref_mask": 1,
    "ref_is_motif_atom_with_fixed_coord": 1,
    "ref_is_motif_atom_unindexed": 1,
    "has_zero_occupancy": 1,
    "ref_pos": 3,
    "ref_atomwise_rasa": 3,
    "active_donor": 1,
    "active_acceptor": 1,
    "is_atom_level_hotspot": 1,
}

# Every token is padded to this many atom slots, so the coordinate tensors the model sees are
# atom level with L = _N_ATOMS_PER_TOKEN * n_residues.
_N_ATOMS_PER_TOKEN = 14


def build_design_features(length: int, diffusion_batch_size: int, sigma_data: float) -> dict:
    """
    Build the foundry feature dict for an unconditional design of `length` residues.

    The token initializer embeds real chemical and positional features; there is no meaningful
    zero substitute for them, and no API in foundry that turns a length into features in one
    call. This mirrors what `rfd3.engine.RFD3InferenceEngine` does, minus the hydra app and the
    checkpoint, by driving the same specification and transform pipeline directly.

    Returns:
        The transformed example, carrying `feats` and `coord_atom_lvl_to_be_noised`.
    """
    from rfd3.inference.input_parsing import DesignInputSpecification
    from rfd3.transforms.pipelines import build_atom14_base_pipeline

    # `length` and `contig` are mutually exclusive in the specification: passing `contig` without
    # a structure input fails validation, so unconditional designs go through `length`.
    spec = DesignInputSpecification(length=str(length))
    data = spec.to_pipeline_input(example_id=f"rfd3_{length}")

    pipeline = build_atom14_base_pipeline(
        is_inference=True,
        diffusion_batch_size=diffusion_batch_size,
        sigma_data=sigma_data,
        central_atom="CB",
        n_atoms_per_token=_N_ATOMS_PER_TOKEN,
        generate_conformers=True,
        provide_reference_conformer_when_unmasked=True,
        ground_truth_conformer_policy="IGNORE",
        use_element_for_atom_names_of_atomized_tokens=True,
        token_1d_features=_TOKEN_1D_FEATURES,
        atom_1d_features=_ATOM_1D_FEATURES,
    )
    return pipeline(data)


def parse_contig_string(contig_str: str) -> Tuple[int, List[Tuple[int, int]]]:
    """
    Parse contig specification string.

    Supports formats like:
    - "100" -> 100 residues to design
    - "50-100" -> random length between 50-100
    - "A10-25/50" -> motif from chain A residues 10-25, plus 50 designed

    Returns:
        total_length: Total protein length
        motif_ranges: List of (start, end) for motif residues (0-indexed)
    """
    parts = contig_str.split("/")
    total_length = 0
    motif_ranges = []

    for part in parts:
        part = part.strip()
        if not part:
            continue

        if part[0].isalpha():
            chain = part[0]
            residue_spec = part[1:]
            if "-" in residue_spec:
                start, end = map(int, residue_spec.split("-"))
            else:
                start = end = int(residue_spec)
            motif_len = end - start + 1
            motif_ranges.append((total_length, total_length + motif_len))
            total_length += motif_len
        else:
            if "-" in part:
                min_len, max_len = map(int, part.split("-"))
                add_len = (min_len + max_len) // 2
            else:
                add_len = int(part)
            total_length += add_len

    return total_length, motif_ranges


class RFDiffusionInputStep(ModularPipelineBlocks):
    """
    Input processing step for RFDiffusion.

    Parses contigs to prepare features for structure generation.
    """

    model_name = "rfdiffusion"

    @property
    def description(self) -> str:
        return (
            "Input processing step that:\n"
            "  1. Parses contig specification to determine protein length and design regions\n"
            "  2. Generates masks for motif positions\n"
        )

    @property
    def inputs(self) -> List[InputParam]:
        return [
            InputParam(
                "contigs",
                required=True,
                type_hint=Union[str, List[str]],
                description="Contig specification defining design regions (e.g., '100' or 'A10-25/50-100')",
            ),
            InputParam(
                "input_xyz",
                type_hint=torch.Tensor,
                description="Input coordinates for motif residues [N_motif, 3]",
            ),
        ]

    @property
    def expected_components(self) -> List[ComponentSpec]:
        return [
            ComponentSpec("scheduler", description="RFDiffusion3 EDM scheduler"),
        ]

    @property
    def intermediate_outputs(self) -> List[OutputParam]:
        return [
            OutputParam(
                "f",
                type_hint=dict,
                description="Foundry feature dict consumed by the token initializer",
            ),
            OutputParam(
                "coord_atom_lvl_to_be_noised",
                type_hint=torch.Tensor,
                description="Reference atom-level coordinates [D, L_atom, 3]",
            ),
            OutputParam(
                "motif_mask",
                type_hint=torch.Tensor,
                description="Atom-level boolean mask for motif (fixed) positions [L_atom]",
            ),
            OutputParam(
                "motif_token_mask",
                type_hint=torch.Tensor,
                description="Residue-level boolean mask for motif (fixed) positions [L]",
            ),
            OutputParam(
                "L",
                type_hint=int,
                description="Total length of the protein being designed, in residues",
            ),
            OutputParam(
                "batch_size",
                type_hint=int,
                description="Batch size (typically 1 for RFDiffusion)",
            ),
        ]

    def check_inputs(self, components, block_state):
        if block_state.contigs is None:
            raise ValueError("`contigs` must be provided to specify protein design regions")

    @torch.no_grad()
    def __call__(self, components: ModularPipeline, state: PipelineState) -> PipelineState:
        block_state = self.get_block_state(state)
        self.check_inputs(components, block_state)

        contigs = block_state.contigs
        input_xyz = block_state.input_xyz

        if isinstance(contigs, list):
            contig_str = "/".join(contigs)
        else:
            contig_str = contigs

        L, motif_ranges = parse_contig_string(contig_str)

        # Motif conditioning needs a reference structure so the transform pipeline can build
        # per-atom features for the fixed residues. A coordinate tensor alone cannot supply the
        # element, atom-name and occupancy annotations those features are derived from.
        if motif_ranges:
            raise ValueError(
                f"Motif-conditioned contigs are not supported yet, got `contigs={contig_str!r}`. "
                "Pass a plain design length such as `contigs='100'`."
            )
        if input_xyz is not None:
            raise ValueError(
                "`input_xyz` is not supported yet. Pass a plain design length such as "
                "`contigs='100'`."
            )

        batch_size = 1
        example = build_design_features(
            length=L,
            diffusion_batch_size=batch_size,
            sigma_data=components.scheduler.config.sigma_data,
        )

        block_state.f = example["feats"]
        block_state.coord_atom_lvl_to_be_noised = example["coord_atom_lvl_to_be_noised"]
        block_state.motif_mask = example["feats"]["is_motif_atom_with_fixed_coord"]
        block_state.motif_token_mask = example["feats"]["is_motif_token_with_fully_fixed_coord"]
        block_state.L = L
        block_state.batch_size = batch_size

        self.set_block_state(state, block_state)
        return components, state


class RFDiffusionSetTimestepsStep(ModularPipelineBlocks):
    """
    Set up the EDM noise schedule for RFDiffusion3.
    """

    model_name = "rfdiffusion"

    @property
    def description(self) -> str:
        return "Sets up the EDM noise schedule matching the original inference sampler."

    @property
    def expected_components(self) -> List[ComponentSpec]:
        return [
            ComponentSpec("scheduler", description="RFDiffusion3 EDM scheduler"),
        ]

    @property
    def inputs(self) -> List[InputParam]:
        return [
            InputParam(
                "num_inference_steps",
                default=None,
                type_hint=int,
                description="Number of denoising steps (default: use scheduler config)",
            ),
            InputParam("L", required=True, type_hint=int, description="Protein length"),
        ]

    @property
    def intermediate_outputs(self) -> List[OutputParam]:
        return [
            OutputParam(
                "noise_schedule",
                type_hint=torch.Tensor,
                description="EDM noise schedule [num_timesteps] from high to low noise",
            ),
            OutputParam(
                "num_inference_steps",
                type_hint=int,
                description="Number of inference steps",
            ),
        ]

    @torch.no_grad()
    def __call__(self, components: ModularPipeline, state: PipelineState) -> PipelineState:
        block_state = self.get_block_state(state)

        # A linear stand-in for the EDM schedule silently changes the sampler, so require
        # the real one rather than degrading.
        if components.scheduler is None:
            raise ValueError(
                "`scheduler` is not loaded. Call `load_components(trust_remote_code=True)` on the "
                "pipeline before calling it."
            )

        noise_schedule = components.scheduler.get_noise_schedule()

        block_state.noise_schedule = noise_schedule
        block_state.num_inference_steps = len(noise_schedule)

        self.set_block_state(state, block_state)
        return components, state


class RFDiffusionPrepareLatentsStep(ModularPipelineBlocks):
    """
    Prepare initial noised coordinates for RFDiffusion3.

    Matches the original _get_initial_structure:
        noise = c0 * randn(D, L, 3)
        noise[..., is_motif, :] = 0
        X_L = noise + coord_motif
    """

    model_name = "rfdiffusion"

    @property
    def description(self) -> str:
        return (
            "Prepares initial coordinates by sampling Gaussian noise scaled by "
            "the first noise schedule value, matching the original sampler."
        )

    @property
    def expected_components(self) -> List[ComponentSpec]:
        return [
            ComponentSpec("scheduler", description="RFDiffusion3 EDM scheduler"),
            ComponentSpec("transformer", description="RFDiffusion transformer model"),
        ]

    @property
    def inputs(self) -> List[InputParam]:
        return [
            InputParam("generator", type_hint=torch.Generator, description="Random generator for reproducibility"),
            InputParam("diffusion_batch_size", default=1, type_hint=int, description="Number of samples to generate in parallel"),
            InputParam("L", required=True, type_hint=int, description="Protein length"),
            InputParam("f", required=True, type_hint=dict),
            InputParam("coord_atom_lvl_to_be_noised", required=True, type_hint=torch.Tensor),
            InputParam("motif_mask", required=True, type_hint=torch.Tensor),
            InputParam("noise_schedule", required=True, type_hint=torch.Tensor),
        ]

    @property
    def intermediate_outputs(self) -> List[OutputParam]:
        return [
            OutputParam("xyz", type_hint=torch.Tensor, description="Initial noised coordinates [D, L_atom, 3]"),
            OutputParam("initializer_outputs", type_hint=dict, description="Embedded conditioning, reused every step"),
        ]

    @torch.no_grad()
    def __call__(self, components: ModularPipeline, state: PipelineState) -> PipelineState:
        block_state = self.get_block_state(state)

        noise_schedule = block_state.noise_schedule
        generator = block_state.generator
        D = block_state.diffusion_batch_size or 1
        device = components.transformer.device

        # The feature dict is built on CPU and is read on every denoising step, so move it once.
        f = {k: v.to(device) if torch.is_tensor(v) else v for k, v in block_state.f.items()}
        coord = block_state.coord_atom_lvl_to_be_noised.to(device)
        motif_mask = f["is_motif_atom_with_fixed_coord"]

        # Matches rfd3.model.inference_sampler._get_initial_structure:
        #   noise = c0 * randn(D, L, 3); noise[..., is_motif, :] = 0; X_L = noise + coord
        c0 = noise_schedule[0].to(device)
        L_atom = coord.shape[-2]
        noise = c0 * torch.randn((D, L_atom, 3), device=device, generator=generator)
        noise[..., motif_mask, :] = 0.0
        xyz = noise + coord

        block_state.f = f
        block_state.xyz = xyz
        block_state.initializer_outputs = components.transformer.encode_conditioning(f)

        self.set_block_state(state, block_state)
        return components, state