From a59aab01a5d41d5b8dab042486ced7381e67b43b Mon Sep 17 00:00:00 2001 From: Qiming Sun Date: Thu, 6 Nov 2025 00:12:56 -0800 Subject: [PATCH 1/7] Adjust the default setting of auxbasis_response for DF-Hessian --- gpu4pyscf/df/hessian/rhf.py | 2 +- gpu4pyscf/df/hessian/rks.py | 2 +- gpu4pyscf/df/hessian/uhf.py | 2 +- gpu4pyscf/df/hessian/uks.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gpu4pyscf/df/hessian/rhf.py b/gpu4pyscf/df/hessian/rhf.py index f7a67f4fd..082d7403e 100644 --- a/gpu4pyscf/df/hessian/rhf.py +++ b/gpu4pyscf/df/hessian/rhf.py @@ -645,7 +645,7 @@ class Hessian(rhf_hess.Hessian): from gpu4pyscf.lib.utils import to_gpu, device - auxbasis_response = 1 + auxbasis_response = 2 partial_hess_elec = partial_hess_elec make_h1 = make_h1 get_jk_mo = _get_jk_mo diff --git a/gpu4pyscf/df/hessian/rks.py b/gpu4pyscf/df/hessian/rks.py index 671cbbfaf..05d7c4e5c 100644 --- a/gpu4pyscf/df/hessian/rks.py +++ b/gpu4pyscf/df/hessian/rks.py @@ -106,7 +106,7 @@ class Hessian(rks_hess.Hessian): '''Non-relativistic RKS hessian''' from gpu4pyscf.lib.utils import to_gpu, device - auxbasis_response = 1 + auxbasis_response = 2 partial_hess_elec = partial_hess_elec make_h1 = make_h1 get_jk_mo = df_rhf_hess._get_jk_mo diff --git a/gpu4pyscf/df/hessian/uhf.py b/gpu4pyscf/df/hessian/uhf.py index 5e94a248b..3d24e0045 100644 --- a/gpu4pyscf/df/hessian/uhf.py +++ b/gpu4pyscf/df/hessian/uhf.py @@ -710,7 +710,7 @@ class Hessian(uhf_hess.Hessian): from gpu4pyscf.lib.utils import to_gpu, device - auxbasis_response = 1 + auxbasis_response = 2 partial_hess_elec = partial_hess_elec make_h1 = make_h1 get_jk_mo = _get_jk_mo diff --git a/gpu4pyscf/df/hessian/uks.py b/gpu4pyscf/df/hessian/uks.py index 996617404..dbf40a1b5 100644 --- a/gpu4pyscf/df/hessian/uks.py +++ b/gpu4pyscf/df/hessian/uks.py @@ -134,7 +134,7 @@ class Hessian(uks_hess.Hessian): '''Non-relativistic RKS hessian''' from gpu4pyscf.lib.utils import to_gpu, device - auxbasis_response = 1 + auxbasis_response = 2 partial_hess_elec = partial_hess_elec make_h1 = make_h1 get_jk_mo = df_uhf_hess._get_jk_mo From 747a15a33fea6d3c5ad0a13ff03e8c0f57816bbe Mon Sep 17 00:00:00 2001 From: Qiming Sun Date: Thu, 6 Nov 2025 10:46:14 -0800 Subject: [PATCH 2/7] Update tests --- .../df/hessian/tests/test_df_rks_hessian.py | 4 +- .../df/hessian/tests/test_df_uks_hessian.py | 4 +- gpu4pyscf/lib/tests/test_to_gpu.py | 4 +- gpu4pyscf/solvent/tests/test_pcm_hessian.py | 43 ++++--------------- gpu4pyscf/solvent/tests/test_smd_hessian.py | 34 ++++----------- 5 files changed, 26 insertions(+), 63 deletions(-) diff --git a/gpu4pyscf/df/hessian/tests/test_df_rks_hessian.py b/gpu4pyscf/df/hessian/tests/test_df_rks_hessian.py index f737e92ab..61f9ce2cb 100644 --- a/gpu4pyscf/df/hessian/tests/test_df_rks_hessian.py +++ b/gpu4pyscf/df/hessian/tests/test_df_rks_hessian.py @@ -66,6 +66,7 @@ def test_df_lda(self): mf = mf.to_gpu() hessobj = mf.Hessian() + hessobj.auxbasis_response = 1 hess_gpu = hessobj.kernel() assert numpy.linalg.norm(hess_cpu - hess_gpu) < 1e-5 @@ -81,6 +82,7 @@ def test_df_gga(self): mf = mf.to_gpu() hessobj = mf.Hessian() + hessobj.auxbasis_response = 1 hessobj.base.cphf_grids = hessobj.base.grids hess_gpu = hessobj.kernel() assert numpy.linalg.norm(hess_cpu - hess_gpu) < 1e-5 @@ -97,6 +99,7 @@ def test_df_mgga(self): mf = mf.to_gpu() hessobj = mf.Hessian() + hessobj.auxbasis_response = 1 hessobj.base.cphf_grids = hessobj.base.grids hess_gpu = hessobj.kernel() assert numpy.linalg.norm(hess_cpu - hess_gpu) < 1e-5 @@ -104,4 +107,3 @@ def test_df_mgga(self): if __name__ == "__main__": print("Full Tests for DF RKS Hessian") unittest.main() - \ No newline at end of file diff --git a/gpu4pyscf/df/hessian/tests/test_df_uks_hessian.py b/gpu4pyscf/df/hessian/tests/test_df_uks_hessian.py index 31804715d..8ba5180c1 100644 --- a/gpu4pyscf/df/hessian/tests/test_df_uks_hessian.py +++ b/gpu4pyscf/df/hessian/tests/test_df_uks_hessian.py @@ -67,6 +67,7 @@ def test_df_lda(self): mf = mf.to_gpu() hessobj = mf.Hessian() + hessobj.auxbasis_response = 1 hess_gpu = hessobj.kernel() assert numpy.linalg.norm(hess_cpu - hess_gpu) < 1e-5 @@ -82,6 +83,7 @@ def test_df_gga(self): mf = mf.to_gpu() hessobj = mf.Hessian() + hessobj.auxbasis_response = 1 hessobj.base.cphf_grids = hessobj.base.grids hess_gpu = hessobj.kernel() assert numpy.linalg.norm(hess_cpu - hess_gpu) < 1e-5 @@ -98,6 +100,7 @@ def test_df_mgga(self): mf = mf.to_gpu() hessobj = mf.Hessian() + hessobj.auxbasis_response = 1 hessobj.base.cphf_grids = hessobj.base.grids hess_gpu = hessobj.kernel() assert numpy.linalg.norm(hess_cpu - hess_gpu) < 1e-5 @@ -105,4 +108,3 @@ def test_df_mgga(self): if __name__ == "__main__": print("Full Tests for DF UKS Hessian") unittest.main() - diff --git a/gpu4pyscf/lib/tests/test_to_gpu.py b/gpu4pyscf/lib/tests/test_to_gpu.py index dd11b9fce..408b1c2bc 100644 --- a/gpu4pyscf/lib/tests/test_to_gpu.py +++ b/gpu4pyscf/lib/tests/test_to_gpu.py @@ -89,6 +89,7 @@ def test_df_RHF(self): mf = scf.RHF(mol).density_fit().run() mf.conv_tol_cpscf = 1e-7 hobj = mf.Hessian().to_gpu() + hobj.auxbasis_response = 1 h = hobj.kernel() assert numpy.abs(lib.fp(h) - 2.198079352288524) < 1e-4 @@ -106,6 +107,7 @@ def test_df_b3lyp(self): mf = rks.RKS(mol, xc='b3lyp').density_fit().run() mf.conv_tol_cpscf = 1e-7 hobj = mf.Hessian().to_gpu() + hobj.auxbasis_response = 1 h = hobj.kernel() assert numpy.abs(lib.fp(h) - 2.1527804103141848) < 1e-4 @@ -123,10 +125,10 @@ def test_df_RKS(self): mf = rks.RKS(mol, xc='wb97x').density_fit().run() mf.conv_tol_cpscf = 1e-7 hobj = mf.Hessian().to_gpu() + hobj.auxbasis_response = 1 h = hobj.kernel() assert numpy.abs(lib.fp(h) - 2.1858589608638384) < 1e-4 if __name__ == "__main__": print("Full tests for to_gpu module") unittest.main() - diff --git a/gpu4pyscf/solvent/tests/test_pcm_hessian.py b/gpu4pyscf/solvent/tests/test_pcm_hessian.py index c88081a20..3dc18bcd6 100644 --- a/gpu4pyscf/solvent/tests/test_pcm_hessian.py +++ b/gpu4pyscf/solvent/tests/test_pcm_hessian.py @@ -326,41 +326,7 @@ def test_hess_solver_ssvpe(self): assert abs(ref_grad_vmat - test_grad_vmat).max() < 1e-9 @pytest.mark.skipif(pyscf_25, reason='requires pyscf 2.6 or higher') - def test_to_gpu(self): - import pyscf - mol = gto.Mole() - mol.atom = ''' -O 0.0000000000 -0.0000000000 0.1174000000 -H -0.7570000000 -0.0000000000 -0.4696000000 -H 0.7570000000 0.0000000000 -0.4696000000 - ''' - mol.basis = 'sto-3g' - mol.output = '/dev/null' - mol.build(verbose=0) - mf = pyscf.dft.RKS(mol, xc='b3lyp').PCM() - mf.conv_tol = 1e-12 - mf.conv_tol_cpscf = 1e-7 - mf.grids.atom_grid = (50,194) - mf.kernel() - hessobj = mf.Hessian() - hess_cpu = hessobj.kernel() - hessobj = hessobj.to_gpu() - hess_gpu = hessobj.kernel() - assert np.linalg.norm(hess_cpu - hess_gpu) < 1e-5 - - mf = pyscf.dft.RKS(mol, xc='b3lyp').density_fit().PCM() - mf.conv_tol = 1e-12 - mf.conv_tol_cpscf = 1e-7 - mf.grids.atom_grid = (50,194) - mf.kernel() - hessobj = mf.Hessian() - hess_cpu = hessobj.kernel() - hessobj = hessobj.to_gpu() - hess_gpu = hessobj.kernel() - assert np.linalg.norm(hess_cpu - hess_gpu) < 1e-5 - - @pytest.mark.skipif(pyscf_25, reason='requires pyscf 2.6 or higher') - def test_to_cpu(self): + def test_to_gpu_to_cpu(self): mol = gto.Mole() mol.atom = ''' O 0.0000000000 -0.0000000000 0.1174000000 @@ -381,6 +347,9 @@ def test_to_cpu(self): hessobj = hessobj.to_cpu() hess_cpu = hessobj.kernel() assert np.linalg.norm(hess_cpu - hess_gpu) < 1e-5 + hessobj = hessobj.to_gpu() + hess_gpu = hessobj.kernel() + assert np.linalg.norm(hess_cpu - hess_gpu) < 1e-5 mf = dft.RKS(mol, xc='b3lyp').density_fit().PCM() mf.conv_tol = 1e-12 @@ -388,10 +357,14 @@ def test_to_cpu(self): mf.grids.atom_grid = (50,194) mf.kernel() hessobj = mf.Hessian() + hessobj.auxbasis_response = 2 hess_gpu = hessobj.kernel() hessobj = hessobj.to_cpu() hess_cpu = hessobj.kernel() assert np.linalg.norm(hess_cpu - hess_gpu) < 1e-5 + hessobj = hessobj.to_gpu() + hess_gpu = hessobj.kernel() + assert np.linalg.norm(hess_cpu - hess_gpu) < 1e-5 if __name__ == "__main__": print("Full Tests for Hessian of PCMs") diff --git a/gpu4pyscf/solvent/tests/test_smd_hessian.py b/gpu4pyscf/solvent/tests/test_smd_hessian.py index cf260da6f..c1979ca9d 100644 --- a/gpu4pyscf/solvent/tests/test_smd_hessian.py +++ b/gpu4pyscf/solvent/tests/test_smd_hessian.py @@ -23,7 +23,7 @@ from gpu4pyscf.solvent import smd from packaging import version -pyscf_25 = version.parse(pyscf.__version__) <= version.parse('2.5.0') +pyscf_211 = version.parse(pyscf.__version__) <= version.parse('2.11.0') def setUpModule(): global mol @@ -225,49 +225,33 @@ def test_Br(self): _check_hess(atom, solvent='water') _check_hess(atom, solvent='toluene') - @pytest.mark.skipif(pyscf_25, reason='requires pyscf 2.6 or higher') - def test_to_gpu(self): - import pyscf - mf = pyscf.dft.RKS(mol, xc='b3lyp').SMD() + @pytest.mark.skipif(pyscf_211, reason='requires pyscf 2.12 or higher') + def test_to_gpu_to_cpu(self): + mf = dft.RKS(mol, xc='b3lyp').SMD() mf.conv_tol = 1e-12 mf.conv_tol_cpscf = 1e-7 mf.kernel() hessobj = mf.Hessian() - hess_cpu = hessobj.kernel() - hessobj = hessobj.to_gpu() hess_gpu = hessobj.kernel() - assert numpy.linalg.norm(hess_cpu - hess_gpu) < 1e-5 - - mf = pyscf.dft.RKS(mol, xc='b3lyp').density_fit().SMD() - mf.conv_tol = 1e-12 - mf.conv_tol_cpscf = 1e-7 - mf.kernel() - hessobj = mf.Hessian() + hessobj = hessobj.to_cpu() hess_cpu = hessobj.kernel() + assert numpy.linalg.norm(hess_cpu - hess_gpu) < 1e-5 hessobj = hessobj.to_gpu() hess_gpu = hessobj.kernel() assert numpy.linalg.norm(hess_cpu - hess_gpu) < 1e-5 - @pytest.mark.skipif(pyscf_25, reason='requires pyscf 2.6 or higher') - def test_to_cpu(self): - mf = dft.RKS(mol, xc='b3lyp').SMD() + mf = dft.RKS(mol, xc='b3lyp').density_fit().SMD() mf.conv_tol = 1e-12 mf.conv_tol_cpscf = 1e-7 mf.kernel() hessobj = mf.Hessian() + hessobj.auxbasis_response = 2 hess_gpu = hessobj.kernel() hessobj = hessobj.to_cpu() hess_cpu = hessobj.kernel() assert numpy.linalg.norm(hess_cpu - hess_gpu) < 1e-5 - - mf = dft.RKS(mol, xc='b3lyp').density_fit().SMD() - mf.conv_tol = 1e-12 - mf.conv_tol_cpscf = 1e-7 - mf.kernel() - hessobj = mf.Hessian() + hessobj = hessobj.to_gpu() hess_gpu = hessobj.kernel() - hessobj = hessobj.to_cpu() - hess_cpu = hessobj.kernel() assert numpy.linalg.norm(hess_cpu - hess_gpu) < 1e-5 if __name__ == "__main__": From be995be2084c88ca451bc9c759c98a2634e7d030 Mon Sep 17 00:00:00 2001 From: Qiming Sun Date: Thu, 6 Nov 2025 19:19:41 -0800 Subject: [PATCH 3/7] lint --- gpu4pyscf/solvent/tests/test_pcm_hessian.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpu4pyscf/solvent/tests/test_pcm_hessian.py b/gpu4pyscf/solvent/tests/test_pcm_hessian.py index 8512d6331..b94cf14a8 100644 --- a/gpu4pyscf/solvent/tests/test_pcm_hessian.py +++ b/gpu4pyscf/solvent/tests/test_pcm_hessian.py @@ -355,7 +355,7 @@ def test_to_gpu_to_cpu(self): mf.kernel() hessobj = mf.Hessian() # The auxbasis_response attribute was not handled in pyscf-2.11 - if version.parse(pyscf.__version__) <= version.parse('2.11.0') + if version.parse(pyscf.__version__) <= version.parse('2.11.0'): hessobj.auxbasis_response = 1 else: hessobj.auxbasis_response = 2 From d2e8e3f6f4888e1237c8f336c7129a019af21afe Mon Sep 17 00:00:00 2001 From: Qiming Sun Date: Thu, 13 Nov 2025 12:16:53 -0800 Subject: [PATCH 4/7] Fix to_gpu/to_cpu tests --- gpu4pyscf/df/df.py | 3 ++- gpu4pyscf/dft/gks.py | 2 +- gpu4pyscf/dft/rks.py | 29 +++++++++++---------- gpu4pyscf/dft/uks.py | 9 ------- gpu4pyscf/solvent/tests/test_pcm_hessian.py | 4 +-- gpu4pyscf/solvent/tests/test_smd_hessian.py | 4 +-- 6 files changed, 22 insertions(+), 29 deletions(-) diff --git a/gpu4pyscf/df/df.py b/gpu4pyscf/df/df.py index 46aa44a90..9946fc8fc 100644 --- a/gpu4pyscf/df/df.py +++ b/gpu4pyscf/df/df.py @@ -40,6 +40,8 @@ class DF(lib.StreamObject): + use_gpu_memory = True + _keys = {'intopt', 'nao', 'naux', 'cd_low', 'mol', 'auxmol', 'use_gpu_memory'} def __init__(self, mol, auxbasis=None): @@ -47,7 +49,6 @@ def __init__(self, mol, auxbasis=None): self.stdout = mol.stdout self.verbose = mol.verbose self.max_memory = mol.max_memory - self.use_gpu_memory = True self._auxbasis = auxbasis self.auxmol = None diff --git a/gpu4pyscf/dft/gks.py b/gpu4pyscf/dft/gks.py index 8d83e3f92..48daa8cb0 100644 --- a/gpu4pyscf/dft/gks.py +++ b/gpu4pyscf/dft/gks.py @@ -23,7 +23,7 @@ class GKS(gks.GKS, GHF): def __init__(self, mol, xc='LDA,VWN'): raise NotImplementedError - reset = rks.RKS.reset + reset = rks.KohnShamDFT.reset energy_elec = rks.RKS.energy_elec get_veff = NotImplemented nuc_grad_method = NotImplemented diff --git a/gpu4pyscf/dft/rks.py b/gpu4pyscf/dft/rks.py index a6c62fd75..5826e1540 100644 --- a/gpu4pyscf/dft/rks.py +++ b/gpu4pyscf/dft/rks.py @@ -245,7 +245,21 @@ def dump_flags(self, verbose=None): log.info('small_rho_cutoff = %g', self.small_rho_cutoff) return self - reset = rks.KohnShamDFT.reset + def reset(self, mol=None): + hf.SCF.reset(self, mol) + self.grids.reset(mol) + self.nlcgrids.reset(mol) + self._numint.reset() + # The cphf_grids attribute is not available in the PySCF CPU version. + # In PySCF's to_gpu() function, this attribute is not initialized. + if hasattr(self, 'cphf_grids'): + self.cphf_grids.reset(self.mol) + else: + cphf_grids = self.cphf_grids = gen_grid.Grids(self.mol) + cphf_grids.prune = gen_grid.sg1_prune + cphf_grids.atom_grid = (50,194) + return self + do_nlc = rks.KohnShamDFT.do_nlc hf.KohnShamDFT = KohnShamDFT @@ -264,19 +278,6 @@ def dump_flags(self, verbose=None): hf.RHF.dump_flags(self, verbose) return KohnShamDFT.dump_flags(self, verbose) - def reset(self, mol=None): - hf.SCF.reset(self, mol) - self.grids.reset(mol) - self.nlcgrids.reset(mol) - self._numint.reset() - # The cphf_grids attribute is not available in the PySCF CPU version. - # In PySCF's to_gpu() function, this attribute is not properly - # initialized. mol of the KS object must be used for initialization. - if mol is None: - mol = self.mol - self.cphf_grids.reset(mol) - return self - def nuc_grad_method(self): from gpu4pyscf.grad import rks as rks_grad return rks_grad.Gradients(self) diff --git a/gpu4pyscf/dft/uks.py b/gpu4pyscf/dft/uks.py index eb412ccc8..4eeef1e2a 100644 --- a/gpu4pyscf/dft/uks.py +++ b/gpu4pyscf/dft/uks.py @@ -119,17 +119,8 @@ def __init__(self, mol, xc='LDA,VWN'): energy_elec = energy_elec energy_tot = hf.RHF.energy_tot init_guess_by_vsap = uks_cpu.UKS.init_guess_by_vsap - to_hf = NotImplemented - def reset(self, mol=None): - hf.SCF.reset(self, mol) - self.grids.reset(mol) - self.nlcgrids.reset(mol) - self.cphf_grids.reset(mol) - self._numint.reset() - return self - def nuc_grad_method(self): from gpu4pyscf.grad import uks as uks_grad return uks_grad.Gradients(self) diff --git a/gpu4pyscf/solvent/tests/test_pcm_hessian.py b/gpu4pyscf/solvent/tests/test_pcm_hessian.py index b94cf14a8..18ed32e2f 100644 --- a/gpu4pyscf/solvent/tests/test_pcm_hessian.py +++ b/gpu4pyscf/solvent/tests/test_pcm_hessian.py @@ -356,9 +356,9 @@ def test_to_gpu_to_cpu(self): hessobj = mf.Hessian() # The auxbasis_response attribute was not handled in pyscf-2.11 if version.parse(pyscf.__version__) <= version.parse('2.11.0'): - hessobj.auxbasis_response = 1 - else: hessobj.auxbasis_response = 2 + else: + hessobj.auxbasis_response = 1 hess_gpu = hessobj.kernel() hessobj = hessobj.to_cpu() hess_cpu = hessobj.kernel() diff --git a/gpu4pyscf/solvent/tests/test_smd_hessian.py b/gpu4pyscf/solvent/tests/test_smd_hessian.py index d6ba5412c..176cb3438 100644 --- a/gpu4pyscf/solvent/tests/test_smd_hessian.py +++ b/gpu4pyscf/solvent/tests/test_smd_hessian.py @@ -244,9 +244,9 @@ def test_to_gpu_to_cpu(self): mf.kernel() hessobj = mf.Hessian() if version.parse(pyscf.__version__) <= version.parse('2.11.0'): - hessobj.auxbasis_response = 1 - else: hessobj.auxbasis_response = 2 + else: + hessobj.auxbasis_response = 1 hess_gpu = hessobj.kernel() hessobj = hessobj.to_cpu() hess_cpu = hessobj.kernel() From 03f4eafc784d80ab0e038bf707a42315aba6bd80 Mon Sep 17 00:00:00 2001 From: Qiming Sun Date: Thu, 13 Nov 2025 13:41:17 -0800 Subject: [PATCH 5/7] Improve the compatiblity for various pyscf versions for df hessian to_cpu functions --- gpu4pyscf/df/hessian/rhf.py | 8 ++++++++ gpu4pyscf/df/hessian/rks.py | 1 + gpu4pyscf/df/hessian/uhf.py | 1 + gpu4pyscf/df/hessian/uks.py | 1 + 4 files changed, 11 insertions(+) diff --git a/gpu4pyscf/df/hessian/rhf.py b/gpu4pyscf/df/hessian/rhf.py index a7e080215..d30e440d1 100644 --- a/gpu4pyscf/df/hessian/rhf.py +++ b/gpu4pyscf/df/hessian/rhf.py @@ -649,3 +649,11 @@ class Hessian(rhf_hess.Hessian): partial_hess_elec = partial_hess_elec make_h1 = make_h1 get_jk_mo = _get_jk_mo + + # In pyscf-2.10, the auxbasis_response attributed is automatically converted + # by the utils.to_cpu() due to a bug in the df.Hessian classes. To support + # old pyscf versions, explictly assgin auxbasis_response + def to_cpu(self): + out = utils.to_cpu(self) + out.auxbasis_response = auxbasis_response + return out diff --git a/gpu4pyscf/df/hessian/rks.py b/gpu4pyscf/df/hessian/rks.py index a41aa6824..8376f1edd 100644 --- a/gpu4pyscf/df/hessian/rks.py +++ b/gpu4pyscf/df/hessian/rks.py @@ -111,3 +111,4 @@ class Hessian(rks_hess.Hessian): partial_hess_elec = partial_hess_elec make_h1 = make_h1 get_jk_mo = df_rhf_hess._get_jk_mo + to_cpu = df_rhf_hess.Hessian.to_cpu diff --git a/gpu4pyscf/df/hessian/uhf.py b/gpu4pyscf/df/hessian/uhf.py index ab56ecd24..26f03fffb 100644 --- a/gpu4pyscf/df/hessian/uhf.py +++ b/gpu4pyscf/df/hessian/uhf.py @@ -714,3 +714,4 @@ class Hessian(uhf_hess.Hessian): partial_hess_elec = partial_hess_elec make_h1 = make_h1 get_jk_mo = _get_jk_mo + to_cpu = df_rhf_hess.Hessian.to_cpu diff --git a/gpu4pyscf/df/hessian/uks.py b/gpu4pyscf/df/hessian/uks.py index 54c990c81..3d6da99af 100644 --- a/gpu4pyscf/df/hessian/uks.py +++ b/gpu4pyscf/df/hessian/uks.py @@ -139,3 +139,4 @@ class Hessian(uks_hess.Hessian): partial_hess_elec = partial_hess_elec make_h1 = make_h1 get_jk_mo = df_uhf_hess._get_jk_mo + to_cpu = df_uhf_hess.Hessian.to_cpu From e90e8d2a9569774b23d299f9d32d9846ab86f0aa Mon Sep 17 00:00:00 2001 From: Qiming Sun Date: Thu, 13 Nov 2025 13:44:44 -0800 Subject: [PATCH 6/7] lint --- gpu4pyscf/df/hessian/rhf.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gpu4pyscf/df/hessian/rhf.py b/gpu4pyscf/df/hessian/rhf.py index d30e440d1..123ef4b16 100644 --- a/gpu4pyscf/df/hessian/rhf.py +++ b/gpu4pyscf/df/hessian/rhf.py @@ -654,6 +654,7 @@ class Hessian(rhf_hess.Hessian): # by the utils.to_cpu() due to a bug in the df.Hessian classes. To support # old pyscf versions, explictly assgin auxbasis_response def to_cpu(self): + from gpu4pyscf.lib import utils out = utils.to_cpu(self) - out.auxbasis_response = auxbasis_response + out.auxbasis_response = self.auxbasis_response return out From fb84df9b5fd86879cb0cca16c9dbd5b474d04e70 Mon Sep 17 00:00:00 2001 From: Qiming Sun Date: Thu, 13 Nov 2025 16:56:46 -0800 Subject: [PATCH 7/7] Updates for pyscf compatibility --- gpu4pyscf/gto/tests/test_int1e_grids_ip.py | 2 ++ gpu4pyscf/gto/tests/test_int1e_grids_ipip.py | 4 ++++ gpu4pyscf/hessian/rhf.py | 6 ++++++ gpu4pyscf/mp/dfmp2.py | 2 ++ gpu4pyscf/mp/tests/test_mp2.py | 4 ---- gpu4pyscf/pbc/dft/tests/test_multigrid.py | 7 ++++--- 6 files changed, 18 insertions(+), 7 deletions(-) diff --git a/gpu4pyscf/gto/tests/test_int1e_grids_ip.py b/gpu4pyscf/gto/tests/test_int1e_grids_ip.py index 4e46382b2..bb10aa71a 100644 --- a/gpu4pyscf/gto/tests/test_int1e_grids_ip.py +++ b/gpu4pyscf/gto/tests/test_int1e_grids_ip.py @@ -65,6 +65,7 @@ class KnownValues(unittest.TestCase): def test_int1e_grids_ip_full_tensor_cart(self): mol = mol_cart fakemol = gto.fakemol_for_charges(grid_points) + fakemol.cart = True int3c2e_ip1 = mol._add_suffix('int3c2e_ip1') cintopt = gto.moleintor.make_cintopt(mol._atm, mol._bas, mol._env, int3c2e_ip1) @@ -183,6 +184,7 @@ def test_int1e_grids_ip_contracted_cart(self): mol = mol_cart fakemol = gto.fakemol_for_charges(grid_points) + fakemol.cart = True int3c2e_ip1 = mol._add_suffix('int3c2e_ip1') cintopt = gto.moleintor.make_cintopt(mol._atm, mol._bas, mol._env, int3c2e_ip1) diff --git a/gpu4pyscf/gto/tests/test_int1e_grids_ipip.py b/gpu4pyscf/gto/tests/test_int1e_grids_ipip.py index 388d721dd..936db6e1a 100644 --- a/gpu4pyscf/gto/tests/test_int1e_grids_ipip.py +++ b/gpu4pyscf/gto/tests/test_int1e_grids_ipip.py @@ -68,6 +68,7 @@ def test_int1e_grids_ipip1_charge_contracted_cart(self): mol = mol_cart fakemol = gto.fakemol_for_charges(grid_points) + fakemol.cart = True int3c2e_ipip1 = mol._add_suffix('int3c2e_ipip1') cintopt = gto.moleintor.make_cintopt(mol._atm, mol._bas, mol._env, int3c2e_ipip1) @@ -170,6 +171,7 @@ def test_int1e_grids_ipvip1_charge_contracted_cart(self): mol = mol_cart fakemol = gto.fakemol_for_charges(grid_points) + fakemol.cart = True int3c2e_ipvip1 = mol._add_suffix('int3c2e_ipvip1') cintopt = gto.moleintor.make_cintopt(mol._atm, mol._bas, mol._env, int3c2e_ipvip1) @@ -272,6 +274,7 @@ def test_int1e_grids_ip1ip2_charge_contracted_cart(self): mol = mol_cart fakemol = gto.fakemol_for_charges(grid_points) + fakemol.cart = True int3c2e_ip1ip2 = mol._add_suffix('int3c2e_ip1ip2') cintopt = gto.moleintor.make_cintopt(mol._atm, mol._bas, mol._env, int3c2e_ip1ip2) @@ -374,6 +377,7 @@ def test_int1e_grids_ipip2_charge_contracted_cart(self): mol = mol_cart fakemol = gto.fakemol_for_charges(grid_points) + fakemol.cart = True # Note: we cannot compute ipip2 (dCdC) directly due to numerical problems, # pyscf treat a point charge as a sharp Gaussian, and we cannot take 2nd derivative of it. diff --git a/gpu4pyscf/hessian/rhf.py b/gpu4pyscf/hessian/rhf.py index b0a56722e..2ac74c1f3 100644 --- a/gpu4pyscf/hessian/rhf.py +++ b/gpu4pyscf/hessian/rhf.py @@ -953,6 +953,12 @@ def dump_flags(self, verbose=None): self.max_memory, lib.current_memory()[0]) return self + def reset(self, mol=None): + if mol is not None: + self.mol = mol + self.base.reset(mol) + return self + class Hessian(HessianBase): '''Non-relativistic restricted Hartree-Fock hessian''' diff --git a/gpu4pyscf/mp/dfmp2.py b/gpu4pyscf/mp/dfmp2.py index 23ca83a3f..b337a099e 100644 --- a/gpu4pyscf/mp/dfmp2.py +++ b/gpu4pyscf/mp/dfmp2.py @@ -163,3 +163,5 @@ def update_amps(self, t2, eris): def init_amps(self, mo_energy=None, mo_coeff=None, eris=None, with_t2=WITH_T2): return kernel(self, mo_energy, mo_coeff, eris, with_t2) + +DFRMP2 = DFMP2 diff --git a/gpu4pyscf/mp/tests/test_mp2.py b/gpu4pyscf/mp/tests/test_mp2.py index b51278166..1c944945e 100644 --- a/gpu4pyscf/mp/tests/test_mp2.py +++ b/gpu4pyscf/mp/tests/test_mp2.py @@ -47,9 +47,6 @@ def tearDownModule(): mol.stdout.close() del mol, mf -import pyscf -from packaging import version -pyscf_25 = version.parse(pyscf.__version__) <= version.parse('2.5.0') class KnownValues(unittest.TestCase): def test_mp2(self): nocc = mol.nelectron//2 @@ -139,7 +136,6 @@ def test_to_cpu(self): e_cpu = pt.kernel()[0] assert abs(e_cpu - e_gpu) < 1e-6 - @pytest.mark.skipif(pyscf_25, reason='requires pyscf 2.6 or higher') def test_to_gpu(self): pt = mp_cpu.mp2.MP2(mf) e_cpu = pt.kernel()[0] diff --git a/gpu4pyscf/pbc/dft/tests/test_multigrid.py b/gpu4pyscf/pbc/dft/tests/test_multigrid.py index a283047c7..299945963 100644 --- a/gpu4pyscf/pbc/dft/tests/test_multigrid.py +++ b/gpu4pyscf/pbc/dft/tests/test_multigrid.py @@ -81,13 +81,11 @@ class KnownValues(unittest.TestCase): def test_get_pp(self): ref = MultiGridNumInt_cpu(cell_orth).get_pp() out = multigrid.MultiGridNumInt(cell_orth).get_pp().get() - self.assertEqual(out.shape, ref.shape) self.assertAlmostEqual(abs(ref-out).max(), 0, 9) def test_get_nuc(self): ref = MultiGridNumInt_cpu(cell_orth).get_nuc() out = multigrid.MultiGridNumInt(cell_orth).get_nuc().get() - self.assertEqual(out.shape, ref.shape) self.assertAlmostEqual(abs(ref-out).max(), 0, 9) def test_eval_nucG(self): @@ -132,7 +130,10 @@ def test_get_rho(self): np.random.seed(2) dm = np.random.random((nao,nao)) - .5 dm = dm.dot(dm.T) - ref = MultiGridNumInt_cpu(cell_orth).get_rho(dm) + if hasattr(multigrid_cpu, 'MultiGridNumInt'): + ref = multigrid_cpu.MultiGridNumInt(cell_orth).get_rho(cell_orth, dm, None) + else: + ref = multigrid_cpu.MultiGridFFTDF(cell_orth).get_rho(dm) out = multigrid.MultiGridNumInt(cell_orth).get_rho(dm).get() self.assertAlmostEqual(abs(ref-out).max(), 0, 9)