File size: 2,806 Bytes
3e3ee14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared parameterization and optional trace records for sampling steps.

The integrator remains the source of transition semantics. Callbacks can inspect
the same deletion, insertion, edge, and schedule decisions without duplicating
the transition implementation.
"""

from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    import torch

    from morph.dataset.core.insertion_data import InsertedAtoms
    from morph.dataset.core.insertion_targets import (
        BondsBetweenInserted,
        BondsToExisting,
    )


@dataclass(frozen=True)
class ScheduleStepMasses:
    """Integrated categorical jump masses for one graph-level time step."""

    substitution: torch.Tensor
    edge_substitution: torch.Tensor
    insertion: torch.Tensor
    deletion: torch.Tensor


@dataclass(frozen=True)
class NodeEditProbabilities:
    """Mutually exclusive numerical-step edit probabilities."""

    any_edit: torch.Tensor
    deletion_given_edit: torch.Tensor


def node_edit_probabilities(
    substitution: torch.Tensor,
    deletion: torch.Tensor,
    eps: float = 1e-8,
) -> NodeEditProbabilities:
    """Combine mutually exclusive substitution and deletion probabilities."""
    raw_any = substitution + deletion
    return NodeEditProbabilities(
        any_edit=raw_any.clamp(0.0, 1.0),
        deletion_given_edit=(deletion / raw_any.clamp_min(eps)).clamp(0.0, 1.0),
    )


@dataclass(frozen=True)
class InsertionStepTrace:
    """Realized insertion edits in the pre-edit node index space."""

    spawn_node_idx: torch.Tensor
    clean_atoms: InsertedAtoms
    bonds_to_existing: BondsToExisting | None
    bonds_between_inserted: BondsBetweenInserted | None


@dataclass(frozen=True)
class IntegrationStepTrace:
    """Edits realized by one production integration step."""

    positions: torch.Tensor
    atom_type_targets: torch.Tensor
    charge_targets: torch.Tensor
    node_substitutions: torch.Tensor
    node_deletions: torch.Tensor
    edge_index: torch.Tensor
    edge_type_targets: torch.Tensor
    edge_substitutions: torch.Tensor
    insertions: torch.Tensor
    insertion: InsertionStepTrace | None


@dataclass(frozen=True)
class SamplingStepTrace:
    """Observable model outputs, edits, and states for one sampling step.

    Tensor references are valid during the callback. Consumers retaining a
    trace should detach and clone only the fields they need.
    """

    mol_t: object
    mol_t_next: object
    t: torch.Tensor
    dt: float | torch.Tensor
    preds: dict
    integration: IntegrationStepTrace
    is_final: bool


__all__ = [
    "InsertionStepTrace",
    "IntegrationStepTrace",
    "NodeEditProbabilities",
    "SamplingStepTrace",
    "ScheduleStepMasses",
    "node_edit_probabilities",
]