-
Notifications
You must be signed in to change notification settings - Fork 20
Bug Fix: Fock Matrix Autograd Support in scp_mode="fock" #214
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
Draft
MauriceDHanisch
wants to merge
2
commits into
grimme-lab:main
Choose a base branch
from
MauriceDHanisch:fix-bug-gradient-scp_fock_mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # This file is part of dxtb. | ||
| # | ||
| # SPDX-Identifier: Apache-2.0 | ||
| # Copyright (C) 2024 Grimme Group | ||
| # | ||
| # 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # This file is part of dxtb. | ||
| # | ||
| # SPDX-Identifier: Apache-2.0 | ||
| # Copyright (C) 2024 Grimme Group | ||
| # | ||
| # 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. | ||
| """ | ||
| Molecules for testing the Hamiltonian. Reference values are stored in npz file. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from tad_mctc.data.molecules import mols | ||
|
|
||
| from dxtb._src.typing import Molecule | ||
|
|
||
| extra: dict[str, Molecule] = { | ||
| "H2_nocn": { | ||
| "numbers": mols["H2"]["numbers"], | ||
| "positions": mols["H2"]["positions"], | ||
| }, | ||
| "SiH4_nocn": { | ||
| "numbers": mols["SiH4"]["numbers"], | ||
| "positions": mols["SiH4"]["positions"], | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| samples: dict[str, Molecule] = {**mols, **extra} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # This file is part of dxtb. | ||
| # | ||
| # SPDX-Identifier: Apache-2.0 | ||
| # Copyright (C) 2024 Grimme Group | ||
| # | ||
| # 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. | ||
| """ | ||
| Testing overlap gradient (autodiff). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
| import torch | ||
| from tad_mctc.autograd import dgradcheck | ||
|
|
||
| from dxtb import GFN1_XTB as par | ||
| from dxtb import Calculator, OutputHandler, labels | ||
| from dxtb._src.typing import DD, Callable, Tensor | ||
| from dxtb.config import ConfigCache | ||
|
|
||
| from ..conftest import DEVICE | ||
| from .samples import samples | ||
|
|
||
| sample_list = ["H2", "HHe", "LiH", "S2", "H2O", "SiH4"] | ||
|
|
||
| # remove HHe as it does not pass the numerical gradient check | ||
| sample_list = [s for s in sample_list if s not in ["HHe"]] | ||
|
|
||
| tol = 5e-4 # increased tolerance | ||
|
|
||
|
|
||
| def gradchecker( | ||
| dtype: torch.dtype, name: str, scp_mode: str | ||
| ) -> tuple[Callable[[Tensor], Tensor], Tensor]: | ||
| """Prepare gradient check from `torch.autograd`.""" | ||
| dd: DD = {"dtype": dtype, "device": DEVICE} | ||
|
|
||
| sample = samples[name] | ||
| numbers = sample["numbers"].to(DEVICE) | ||
| positions = sample["positions"].to(**dd) | ||
|
|
||
| opts = { | ||
| "scf_mode": "implicit", | ||
| "scp_mode": scp_mode, | ||
| } | ||
|
|
||
| calc = Calculator(numbers, par, **dd, opts=opts) | ||
| calc.opts.cache = ConfigCache(enabled=False, fock=True) | ||
| OutputHandler.verbosity = 0 | ||
|
|
||
| # variables to be differentiated | ||
| pos = positions.clone().requires_grad_(True) | ||
|
|
||
| def func(p: Tensor) -> Tensor: | ||
| _ = calc.get_energy(p) # triggers Fock matrix computation | ||
| return calc.cache["fock"] | ||
|
|
||
| return func, pos | ||
|
|
||
|
|
||
| @pytest.mark.grad | ||
| @pytest.mark.parametrize("dtype", [torch.double]) | ||
| @pytest.mark.parametrize("name", sample_list) | ||
| @pytest.mark.parametrize("scp_mode", ["charge", "potential", "fock"]) | ||
| def test_grad_fock(dtype: torch.dtype, name: str, scp_mode: str) -> None: | ||
| """ | ||
| Check analytical gradient of Fock matrix against numerical | ||
| gradient from `torch.autograd.gradcheck`. | ||
| """ | ||
| func, diffvars = gradchecker(dtype, name, scp_mode) | ||
| assert dgradcheck(func, diffvars, atol=tol) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Unused import Note test