GO-Diff 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- go_diff/__init__.py +3 -0
- go_diff/controllers/__init__.py +4 -0
- go_diff/controllers/buffer.py +89 -0
- go_diff/controllers/sample.py +98 -0
- go_diff/controllers/temperature.py +116 -0
- go_diff/controllers/training.py +407 -0
- go_diff/filter.py +70 -0
- go_diff/go_diff.py +751 -0
- go_diff/logger.py +453 -0
- go_diff/noisers/__init__.py +1 -0
- go_diff/noisers/weighted_pos.py +59 -0
- go_diff/utils.py +50 -0
- go_diff-1.0.0.dist-info/METADATA +153 -0
- go_diff-1.0.0.dist-info/RECORD +16 -0
- go_diff-1.0.0.dist-info/WHEEL +4 -0
- go_diff-1.0.0.dist-info/licenses/LICENSE +674 -0
go_diff/__init__.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from ase import Atoms
|
|
5
|
+
|
|
6
|
+
from go_diff.utils import effective_sample_size
|
|
7
|
+
|
|
8
|
+
class BufferController:
|
|
9
|
+
"""Controls the replay buffer size via ESS-based adaptive updates.
|
|
10
|
+
|
|
11
|
+
Parameters
|
|
12
|
+
----------
|
|
13
|
+
initial_buffer_size : int
|
|
14
|
+
Starting buffer size. Default: 16.
|
|
15
|
+
min_buffer_size : int
|
|
16
|
+
Lower bound on the buffer size. Default: 16.
|
|
17
|
+
max_buffer_size : int
|
|
18
|
+
Upper bound on the buffer size. Default: 512.
|
|
19
|
+
adaptation_rate : float
|
|
20
|
+
Exponential smoothing coefficient for buffer-size updates (between 0
|
|
21
|
+
and 1). Higher values adapt faster. Default: 0.2.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
initial_buffer_size: int = 16,
|
|
27
|
+
min_buffer_size: int = 16,
|
|
28
|
+
max_buffer_size: int = 512,
|
|
29
|
+
adaptation_rate: float = 0.2,
|
|
30
|
+
) -> None:
|
|
31
|
+
self.min_buffer_size = min_buffer_size
|
|
32
|
+
self.max_buffer_size = max_buffer_size
|
|
33
|
+
self.adaptation_rate = adaptation_rate
|
|
34
|
+
|
|
35
|
+
self.current_buffer_size = initial_buffer_size
|
|
36
|
+
|
|
37
|
+
# ------------------------------------------------------------------
|
|
38
|
+
# Public API
|
|
39
|
+
# ------------------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
def compute_ess(self, energies, temperature) -> float:
|
|
42
|
+
"""Compute the Effective Sample Size (ESS).
|
|
43
|
+
|
|
44
|
+
Delegates to :func:`go_diff.utils.effective_sample_size`.
|
|
45
|
+
"""
|
|
46
|
+
return effective_sample_size(energies, temperature)
|
|
47
|
+
|
|
48
|
+
def update_buffer_size(
|
|
49
|
+
self,
|
|
50
|
+
energies: list[float],
|
|
51
|
+
temperature: float | None = None,
|
|
52
|
+
) -> int:
|
|
53
|
+
"""Update the buffer size based on the current ESS.
|
|
54
|
+
|
|
55
|
+
Parameters
|
|
56
|
+
----------
|
|
57
|
+
energies : list of float
|
|
58
|
+
Energies of structures collected so far in the current iteration.
|
|
59
|
+
temperature : float or None
|
|
60
|
+
Current annealing temperature. ``None`` indicates the first
|
|
61
|
+
iteration (no Boltzmann weighting yet).
|
|
62
|
+
|
|
63
|
+
Returns
|
|
64
|
+
-------
|
|
65
|
+
int
|
|
66
|
+
The new buffer size for the next sampling step.
|
|
67
|
+
"""
|
|
68
|
+
if len(energies) == 0:
|
|
69
|
+
return self.current_buffer_size
|
|
70
|
+
|
|
71
|
+
ess = self.compute_ess(energies, temperature)
|
|
72
|
+
target_B = int(ess)
|
|
73
|
+
|
|
74
|
+
new_B = (1.0 - self.adaptation_rate) * self.current_buffer_size + self.adaptation_rate * target_B
|
|
75
|
+
self.current_buffer_size = int(np.clip(new_B, self.min_buffer_size, self.max_buffer_size))
|
|
76
|
+
|
|
77
|
+
return self.current_buffer_size
|
|
78
|
+
|
|
79
|
+
def get_buffer_size(self) -> int:
|
|
80
|
+
"""Return the current buffer size."""
|
|
81
|
+
return self.current_buffer_size
|
|
82
|
+
|
|
83
|
+
def set_buffer_size(self, size: int) -> None:
|
|
84
|
+
"""Set the current buffer size."""
|
|
85
|
+
self.current_buffer_size = int(size)
|
|
86
|
+
|
|
87
|
+
def reset(self) -> None:
|
|
88
|
+
"""Reset the buffer size to the initial value."""
|
|
89
|
+
self.current_buffer_size = self.min_buffer_size
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Sampling controller for GO-Diff: decides when enough structures have been
|
|
2
|
+
collected based on the Effective Sample Size (ESS)."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from go_diff.utils import boltzmann_weights, effective_sample_size
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SampleController:
|
|
10
|
+
"""Controls how many structures to sample per GO-Diff iteration.
|
|
11
|
+
|
|
12
|
+
Sampling continues until either the ESS reaches *target_ess* or the total
|
|
13
|
+
number of structures reaches *max_N*. On the very first iteration
|
|
14
|
+
(temperature = ``None``) sampling continues until *initial_N* structures
|
|
15
|
+
are collected.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
initial_N : int
|
|
20
|
+
Minimum (and default) number of structures to sample when no
|
|
21
|
+
temperature is available (first iteration). Default: 32.
|
|
22
|
+
max_N : int
|
|
23
|
+
Hard upper limit on the number of structures per iteration.
|
|
24
|
+
Default: 64.
|
|
25
|
+
target_ess : float
|
|
26
|
+
Target Effective Sample Size. Sampling stops once the ESS computed
|
|
27
|
+
from the current structures exceeds this value. Default: 16.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
initial_N: int = 32,
|
|
33
|
+
max_N: int = 64,
|
|
34
|
+
target_ess: float = 16,
|
|
35
|
+
) -> None:
|
|
36
|
+
self.initial_N = initial_N
|
|
37
|
+
self.max_N = max_N
|
|
38
|
+
self.target_ess = target_ess
|
|
39
|
+
|
|
40
|
+
# ------------------------------------------------------------------
|
|
41
|
+
# Public API
|
|
42
|
+
# ------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
def compute_weights(self, energies, temperature):
|
|
45
|
+
"""Compute Boltzmann importance weights.
|
|
46
|
+
|
|
47
|
+
Delegates to :func:`go_diff.utils.boltzmann_weights`.
|
|
48
|
+
"""
|
|
49
|
+
return boltzmann_weights(energies, temperature)
|
|
50
|
+
|
|
51
|
+
def compute_ess(self, energies, temperature) -> float:
|
|
52
|
+
"""Compute the Effective Sample Size (ESS).
|
|
53
|
+
|
|
54
|
+
Delegates to :func:`go_diff.utils.effective_sample_size`.
|
|
55
|
+
"""
|
|
56
|
+
return effective_sample_size(energies, temperature)
|
|
57
|
+
|
|
58
|
+
def continue_sampling(
|
|
59
|
+
self,
|
|
60
|
+
energies: list[float],
|
|
61
|
+
temperature: float | None = None,
|
|
62
|
+
) -> bool:
|
|
63
|
+
"""Decide whether to sample more structures.
|
|
64
|
+
|
|
65
|
+
Parameters
|
|
66
|
+
----------
|
|
67
|
+
energies : list of float
|
|
68
|
+
Energies of structures collected so far in the current iteration.
|
|
69
|
+
temperature : float or None
|
|
70
|
+
Current annealing temperature. ``None`` indicates the first
|
|
71
|
+
iteration (no Boltzmann weighting yet).
|
|
72
|
+
|
|
73
|
+
Returns
|
|
74
|
+
-------
|
|
75
|
+
bool
|
|
76
|
+
``True`` if more structures should be sampled.
|
|
77
|
+
"""
|
|
78
|
+
if len(energies) == 0:
|
|
79
|
+
return True
|
|
80
|
+
|
|
81
|
+
if len(energies) >= self.max_N:
|
|
82
|
+
return False
|
|
83
|
+
|
|
84
|
+
if temperature is None:
|
|
85
|
+
keep_going = len(energies) < self.initial_N
|
|
86
|
+
return keep_going
|
|
87
|
+
|
|
88
|
+
current_ess = self.compute_ess(energies, temperature)
|
|
89
|
+
|
|
90
|
+
if current_ess < self.target_ess:
|
|
91
|
+
print(
|
|
92
|
+
f"Continue sampling: ESS {current_ess:.3f} does not meet "
|
|
93
|
+
f"target of {self.target_ess}."
|
|
94
|
+
)
|
|
95
|
+
return True
|
|
96
|
+
|
|
97
|
+
print(f"Stopping sampling: ESS {current_ess:.3f} meets target.")
|
|
98
|
+
return False
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""Temperature scheduling for GO-Diff annealing."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from numpy.typing import ArrayLike
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TemperatureSchedule:
|
|
10
|
+
"""Adaptive temperature schedule for Boltzmann-weighted diffusion training.
|
|
11
|
+
|
|
12
|
+
The temperature is initialised from the standard deviation of the first
|
|
13
|
+
batch of energies and is then adapted each iteration based on the
|
|
14
|
+
dimensionless heat capacity C(T) = Var(E) / T².
|
|
15
|
+
|
|
16
|
+
* When C > 1 the energy landscape is still "rough" – apply a *slow*
|
|
17
|
+
(conservative) cooling factor.
|
|
18
|
+
* When C ≤ 1 the distribution is narrow – apply a *fast* (aggressive)
|
|
19
|
+
cooling factor to anneal more quickly.
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
k : float
|
|
24
|
+
Scale factor applied to the initial temperature estimate
|
|
25
|
+
(``T₀ = k · std(E)``). Default: 1.0.
|
|
26
|
+
fast : float
|
|
27
|
+
Multiplicative cooling factor used when C ≤ 1 (< 1 means cooling).
|
|
28
|
+
Default: 0.5.
|
|
29
|
+
slow : float
|
|
30
|
+
Multiplicative cooling factor used when C > 1 (< 1 means cooling).
|
|
31
|
+
Default: 0.95.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, k: float = 1.0, fast: float = 0.5, slow: float = 0.95) -> None:
|
|
35
|
+
self.k = k
|
|
36
|
+
self.fast = fast
|
|
37
|
+
self.slow = slow
|
|
38
|
+
self.temperature: float | None = None
|
|
39
|
+
self.history: list[float] = []
|
|
40
|
+
|
|
41
|
+
# ------------------------------------------------------------------
|
|
42
|
+
# Public API
|
|
43
|
+
# ------------------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
def get_temperature(self) -> float:
|
|
46
|
+
"""Return the current temperature.
|
|
47
|
+
|
|
48
|
+
Raises
|
|
49
|
+
------
|
|
50
|
+
ValueError
|
|
51
|
+
If :py:meth:`next` has not been called yet.
|
|
52
|
+
"""
|
|
53
|
+
if self.temperature is None:
|
|
54
|
+
raise ValueError(
|
|
55
|
+
"Temperature schedule not initialised – call next() first."
|
|
56
|
+
)
|
|
57
|
+
return float(self.temperature)
|
|
58
|
+
|
|
59
|
+
def next(self, energies: ArrayLike) -> float:
|
|
60
|
+
"""Update the temperature given the current batch of energies.
|
|
61
|
+
|
|
62
|
+
On the first call the temperature is initialised as
|
|
63
|
+
``T = k · std(energies)``. On subsequent calls it is multiplied by
|
|
64
|
+
*slow* or *fast* depending on the heat capacity.
|
|
65
|
+
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
energies : array-like of float
|
|
69
|
+
Scalar potential energies (eV) of the sampled structures.
|
|
70
|
+
|
|
71
|
+
Returns
|
|
72
|
+
-------
|
|
73
|
+
float
|
|
74
|
+
The new temperature after the update.
|
|
75
|
+
"""
|
|
76
|
+
energies = np.asarray(energies, dtype=float)
|
|
77
|
+
if self.temperature is None:
|
|
78
|
+
self.temperature = float(max(np.std(energies) * self.k, 1e-6))
|
|
79
|
+
else:
|
|
80
|
+
C = self._compute_heat_capacity(energies)
|
|
81
|
+
print(
|
|
82
|
+
f"Temperature: {self.temperature:.4f}, "
|
|
83
|
+
f"variance: {float(np.var(energies)):.4f}, "
|
|
84
|
+
f"std: {float(np.std(energies)):.4f}, "
|
|
85
|
+
f"C: {float(C):.4f}"
|
|
86
|
+
)
|
|
87
|
+
if C > 1:
|
|
88
|
+
self.temperature *= self.slow
|
|
89
|
+
else:
|
|
90
|
+
self.temperature *= self.fast
|
|
91
|
+
|
|
92
|
+
self.history.append(float(self.temperature))
|
|
93
|
+
return float(self.temperature)
|
|
94
|
+
|
|
95
|
+
# ------------------------------------------------------------------
|
|
96
|
+
# Private helpers
|
|
97
|
+
# ------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
def _compute_heat_capacity(self, energies: np.ndarray) -> float:
|
|
100
|
+
"""Dimensionless heat capacity C(T) = Var(E) / T².
|
|
101
|
+
|
|
102
|
+
Parameters
|
|
103
|
+
----------
|
|
104
|
+
energies : np.ndarray
|
|
105
|
+
Array of scalar energies.
|
|
106
|
+
|
|
107
|
+
Returns
|
|
108
|
+
-------
|
|
109
|
+
float
|
|
110
|
+
The estimated heat capacity.
|
|
111
|
+
"""
|
|
112
|
+
variance = float(np.var(energies))
|
|
113
|
+
return variance / (self.temperature ** 2 + 1e-8)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|