-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsingle_stochastic_interpolant_identity.py
More file actions
144 lines (124 loc) · 5.37 KB
/
Copy pathsingle_stochastic_interpolant_identity.py
File metadata and controls
144 lines (124 loc) · 5.37 KB
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
from typing import Callable, Dict, Iterable
import torch
from .abstracts import Corrector, StochasticInterpolantSpecies
from .corrector import IdentityCorrector
class SingleStochasticInterpolantIdentity(StochasticInterpolantSpecies):
"""
Stochastic interpolant x_t = x_0 = x_1, between points x_0 and x_1 from two distributions p_0 and p_1 at
times t. Differs from the SingleStochasticInterpolant class insofar as the quantity represented
by x_0 and x_1 (such as atom types) must be equal during interpolation.
"""
def __init__(self) -> None:
"""Construct stochastic interpolant."""
super().__init__()
def interpolate(self, t: torch.Tensor, x_0: torch.Tensor, x_1: torch.Tensor,
batch_indices: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
"""
Stochastically interpolate between points x_0 and x_1 from two distributions p_0 and p_1 at times t.
:param t:
Times in [0,1].
:type t: torch.Tensor
:param x_0:
Points from p_0.
:type x_0: torch.Tensor
:param x_1:
Points from p_1.
:type x_1: torch.Tensor, must be same as x_0.
:param batch_indices:
Tensor containing the configuration index for every atom in the batch.
:type batch_indices: torch.Tensor
:return:
Stochastically interpolated points x_t, random variables z used for interpolation.
:rtype: tuple[torch.Tensor, torch.Tensor]
"""
assert torch.equal(x_0, x_1)
# Always return new object.
return x_0.clone(), torch.zeros_like(x_0)
def loss_keys(self) -> Iterable[str]:
"""
Get the keys of the losses returned by the loss function.
:return:
Keys of the losses.
:rtype: Iterable[str]
"""
yield "loss"
def loss(self, model_function: Callable[[torch.Tensor], tuple[torch.Tensor, torch.Tensor]],
t: torch.Tensor, x_0: torch.Tensor, x_1: torch.Tensor, x_t: torch.Tensor, z: torch.Tensor,
batch_indices: torch.Tensor) -> Dict[str, torch.Tensor]:
"""
Compute the losses for the stochastic interpolant between points x_0 and x_1 from two distributions p_0 and
p_1 at times t based on the model prediction for the velocity fields b and the denoisers eta.
This class always returns a zero loss with the key 'loss'.
:param model_function:
Model function returning the velocity fields b and the denoisers eta given the current positions x_t.
:type model_function: Callable[[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]
:param t:
Times in [0,1].
:type t: torch.Tensor
:param x_0:
Points from p_0.
:type x_0: torch.Tensor
:param x_1:
Points from p_1.
:type x_1: torch.Tensor
:param x_t:
Stochastically interpolated points x_t.
:type x_t: torch.Tensor
:param z:
Random variable z that was used for the stochastic interpolation to get the model prediction.
:type z: torch.Tensor
:param batch_indices:
Tensor containing the configuration index for every atom in the batch.
:type batch_indices: torch.Tensor
:return:
Losses.
:rtype: Dict[str, torch.Tensor]
"""
assert torch.equal(x_0, x_1)
return {"loss": torch.tensor(0.0, device=x_0.device)}
def integrate(self, model_function: Callable[[torch.Tensor, torch.Tensor], tuple[torch.Tensor, torch.Tensor]],
x_t: torch.Tensor, time: torch.Tensor, time_step: torch.Tensor,
batch_indices: torch.Tensor) -> torch.Tensor:
"""
Integrate the current positions x_t at the given time for the given time step based on the velocity fields b and
the denoisers eta returned by the model function.
:param model_function:
Model function returning the velocity fields b and the denoisers eta given the current times t and positions
x_t.
:type model_function: Callable[[torch.Tensor, torch.Tensor], tuple[torch.Tensor, torch.Tensor]]
:param x_t:
Current positions.
:type x_t: torch.Tensor
:param time:
Initial time (0-dimensional torch tensor).
:type time: torch.Tensor
:param time_step:
Time step (0-dimensional torch tensor).
:type time_step: torch.Tensor
:param batch_indices:
Tensor containing the configuration index for every atom in the batch.
:type batch_indices: torch.Tensor
:return:
Integrated position.
:rtype: torch.Tensor
"""
# Always return new object.
return x_t.clone()
def get_corrector(self) -> Corrector:
"""
Get the corrector implied by the stochastic interpolant (for instance, a corrector that considers periodic
boundary conditions).
:return:
Corrector.
:rtype: Corrector
"""
return IdentityCorrector()
def uses_masked_species(self) -> bool:
"""
Return whether the stochastic interpolant uses masked species.
:return:
Whether the stochastic interpolant uses masked species.
:rtype: bool
"""
# Dataset does not contain masked species.
return False