Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Langevin adaptative corrector #109

Merged
merged 10 commits into from
Dec 3, 2024
Merged

Conversation

sblackburn86
Copy link
Collaborator

This implements the Langevin Corrector generator from
SCORE-BASED GENERATIVE MODELING THROUGH STOCHASTIC DIFFERENTIAL EQUATIONS
algorithm 4 in appendix G. I renamed it Adaptative Corrector to minimize name collisions.

The differences from the Langevin Generator only apply to the coordinates sampler, not the atom type.

@sblackburn86 sblackburn86 requested a review from rousseab December 2, 2024 14:29
@@ -1,3 +1,5 @@
from diffusion_for_multi_scale_molecular_dynamics.generators.adaptative_corrector import \
AdaptativeCorrectorGenerator
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Adaptative" -> "Adaptive"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

NoiseParameters


class AdaptativeCorrectorGenerator(LangevinGenerator):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Adaptatif" is french. "Adaptive" is a better choice in English.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it. But interesting grammatical fact: both adaptative and adaptive are in the oxford dictionary and act as synonyms.

@@ -518,7 +522,8 @@ def corrector_step(
) -> AXL:
"""Corrector Step.

Note this is not affecting the atom types. Only the reduced coordinates and lattice vectors.
Note this dones not affect the atom types unless specified with the atom_type_transition_in_corrector argument.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: dones -> doesn't.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Collaborator

@rousseab rousseab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a bit of refactoring could avoid a lot of duplicated code. Also, I'm not sure about how the norm of the score and Z are computed.

)
self.corrector_r = noise_parameters.corrector_r

def predictor_step(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm reading this right, this predictor_step is identical to the predictor_step in LangevinGenerator, with the only difference that x_im1 = x_i. I note that the two methods have diverged (you copy-pasted an older version I believe).

I propose that you create methods

  • LangevinGenerator._relative_coordinates_update_predictor_step
    and
  • LangevinGenerator._relative_coordinates_update_corrector_step

In LangevinGenerator, both methods could just be dummies that call LangevinGenerator._relative_coordinates_update.

In the class AdaptativeCorrectorGenerator, you could overload _relative_coordinates_update_predictor_step to return its own input.

This way, we avoid a lot of duplicated code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I changed the code accordingly.


return composition_im1

def corrector_step(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method also seems like it is very close to LangevinGenerator.corrector_step.

I propose that you create a method LangevinGenerator._get_corrector_step_epsilon. In LangevinGenerator, it could just return the correct entry from the tabulated values, and you could overload this method here to compute epsilon in a more sophisticated way.

We could get rid of the array sqrt_2_epsilon in LangevinDynamics: surely it's not taking a sqrt that slows us down.
Screenshot 2024-12-02 at 11 40 43 AM

Again, this would avoid a lot of duplication.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed. I did not delete sqrt_2_epsilon yet. It requires checking a few tests to make sure it is not called anywhere. We could either do it right now or leave it as a TODO as part of a code cleanup effort.

)

# to compute epsilon_i, we need the norm of the score. We average over the atoms.
relative_coordinates_sigma_score_norm = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The norm in the paper is calculated over all pixels.

I think you are doing this:
Screenshot 2024-12-02 at 11 34 09 AM

and I think the right way of doing it is this:
Screenshot 2024-12-02 at 11 34 30 AM

The paper talks about averaging over the batch dimension; you are averaging over the atoms within a single sample.

Screenshot 2024-12-02 at 11 34 59 AM

I don't think we should do this just yet...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure what was the best approach to take the norm, so I went with something reasonable. Let's stick to the paper recommendations for now.

from tests.generators.test_langevin_generator import TestLangevinGenerator


class TestAdaptativeCorrectorGenerator(TestLangevinGenerator):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adaptative -> Adaptive

Also, the word "adaptative" still apears in the main code at the following locations:

./src/diffusion_for_multi_scale_molecular_dynamics/noise_schedulers/noise_parameters.py: # Step size scaling for the Adaptative Corrector Generator. Default value comes from github implementation

./src/diffusion_for_multi_scale_molecular_dynamics/generators/adaptive_corrector.py: """Langevin Dynamics Generator using only a corrector step with adaptative step size for relative coordinates.

./src/diffusion_for_multi_scale_molecular_dynamics/generators/sde_position_generator.py: adaptative: bool = False

./src/diffusion_for_multi_scale_molecular_dynamics/generators/sde_position_generator.py: adaptive=self.sampling_parameters.adaptative,

z: torch.Tensor,
) -> torch.Tensor:
"""Compute the size of the corrector step for the relative coordinates update."""
del sigma_i # sigma_i is not needed to compute \epsilon_i because it is pre-tabulated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete these variables? It's ok not to use them.

@@ -518,7 +583,8 @@ def corrector_step(
) -> AXL:
"""Corrector Step.

Note this is not affecting the atom types. Only the reduced coordinates and lattice vectors.
Note this does not not affect the atom types unless specified with the atom_type_transition_in_corrector
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo : not not-> not

Copy link
Collaborator

@rousseab rousseab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I just note a few typos here and there. You can merge after fixing.

@sblackburn86 sblackburn86 merged commit 2f6bbb3 into main Dec 3, 2024
1 check passed
@sblackburn86 sblackburn86 deleted the langevin_adaptative_corrector branch December 3, 2024 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants