Skip to content

Fix related to performing AD on molecular integrals#22

Draft
tansongchen wants to merge 1 commit intoFermiQC:mainfrom
tansongchen:main
Draft

Fix related to performing AD on molecular integrals#22
tansongchen wants to merge 1 commit intoFermiQC:mainfrom
tansongchen:main

Conversation

@tansongchen
Copy link
Copy Markdown

@tansongchen tansongchen commented Apr 25, 2025

Hi there,

I'm trying to make automatic differentiation work on molecular integrals, as a part of attempts to implement differentiable Hartree-Fock. Most of the code base is compatible with AD, but I did find some places where Float64 is assumed which cause ForwardDiff to fail.

In addition, the multithreading seems to be not compatible with AD. I'm currently figuring out why it isn't, and once I understand how to do AD under multithreading, I will add it back.

@tansongchen tansongchen marked this pull request as draft April 25, 2025 19:22
@gustavojra
Copy link
Copy Markdown
Member

Hi @tansongchen,

I have played with this idea before. The main roadblock is introducing derivatives for the integrals. Since we are using an external C library to do the job AutoDiff won't be able to get that for you. I believe you will need to use something like ChainRules to obtain both integrals and derivatives out of libcint at once.

As for the parts of the code assuming Float64, feel free to adjust that, but please test it out! The assumption may be important somewhere, e.g., getting integrals out of libcint.

@tansongchen
Copy link
Copy Markdown
Author

Hi @gustavojra ,

I agree that libcint is a bit hard to get differentiated, but I'm thinking about improving your ACSint performance and run Enzyme (or ForwardDiff) on it to synthesize efficient derivatives. I understand the math behind it (Hermite Gaussians etc.) and I have some experience on this kind of scalar code optimization on other projects.

This isn't easy, but if we can get to a similar performance with hand-coded derivative, this will be a great demonstration of Julia language's differentiable programming capability. As a background, many other "differentiable" quantum chemistry frameworks (such as https://arxiv.org/abs/2207.13836 ) cannot do well on differentiating molecular integrals because they are highly scalar and mutating routines. With Julia and especially ForwardDiff / Enzyme, we could do better.

Right now, I have been able to AD the ACSint to compute force with both ForwardDiff and Enzyme, the efficiency is not perfect but promising. https://github.com/tansongchen/GaussianBasis.jl/blob/bench/bench/adtest.jl

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