Skip to content

feat(deepks): add descriptor-gradient training labels - #8504

Open
ErjieWu wants to merge 4 commits into
deepmodeling:developfrom
ErjieWu:feat/deepks-grad-labels
Open

ErjieWu wants to merge 4 commits into
deepmodeling:developfrom
ErjieWu:feat/deepks-grad-labels

Conversation

@ErjieWu

@ErjieWu ErjieWu commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

This PR adds deepks_grad to export the descriptor derivatives and contracted operators needed for gradient-based DeePKS training. A single calculation provides the underlying functional (base) and the model-corrected result (tot) at the same sampled density. The training workflow can combine these outputs from separate calculations to construct its targets.

Descriptor-gradient labels

Enable the new mode with:

deepks_out_labels  1
deepks_grad        1

The mode uses eigenvalue descriptors with nspin=1, deepks_bandgap=0, and deepks_v_delta=0. deepks_grad defaults to false and its label-selection constraints are inactive when deepks_out_labels=0. INPUT metadata describes the new outputs and checks these combinations.

Let d denote the flattened descriptor vector, g = dE_model/dd the model gradient, and B the real-space Hamiltonian precalculation operator, with rows indexed by lattice translation and orbital pair. The model Hamiltonian correction is linear in g, and B^T B supplies its quadratic training term. The output is organized as follows:

Output Training content
deepks_dm_eig.npy Eigenvalue descriptors at the sampled density.
deepks_gevdm.npy Descriptor derivatives with respect to the projected density matrices.
deepks_dot_phialpha_hamilt.npy Projection of the base real-space Hamiltonian onto the projector-overlap factors, in Hartree. Contracting it with gevdm gives the descriptor-space Hamiltonian projection.
deepks_dot_phialpha_hamilt_tot.npy Corresponding projection of the model-corrected Hamiltonian, at the same density.
deepks_vdrpre_square.npy The Hamiltonian Gram matrix B^T B, with shape (nat * des_per_atom, nat * des_per_atom).
deepks_gradvx.npy and deepks_gradvx_square.npy Atomic-coordinate descriptor derivatives and their Gram matrix, when force output is enabled.
deepks_gvepsl.npy and deepks_gvepsl_square.npy The six-component virial operator and its Gram matrix, when stress output is enabled.

Energy and physical-property labels accompany these operators. With a model active, the base Hamiltonian projection is obtained by subtracting the projected model contribution from the total projection. Without a model, base and total coincide. The force and virial operators reconstruct the model contributions by contraction with g.

The Hamiltonian contractions are implemented in deepks_vdrpre, and the force and virial Gram calculations extend deepks_fpre and deepks_spre. Real-space precalculation contracts projector indices into descriptor indices during assembly. The Gram calculation accumulates orbital-pair blocks directly, reducing the need to materialize the full real-space derivative tensor. The Hamiltonian projection supports real and complex matrix elements and distributed orbital ownership.

Stress and strain derivatives

The projected-density strain derivative previously paired grad_overlap[j] with r[i]. It now pairs grad_overlap[i] with r[j], consistent with the deformation convention r' = (I + epsilon) r: changing epsilon_ij changes coordinate r_i in proportion to r_j. This corrects the off-diagonal strain components used to construct gdmepsl and gvepsl.

The virial components are ordered xx, xy, xz, yy, yz, zz. Contracting gvepsl with the model gradient produces the virial in Hartree, corresponding to 0.5 * cell_volume * stress_delta for the internal stress in Ry/Bohr^3. Overlap derivatives are allocated and built when either cal_force or cal_stress is enabled, including stress-only calculations. The force-correction routine also sizes its output matrix to nat x 3 before accumulation.

Independent central differences deform the cell at fixed AO density matrix and fixed reduced k points. At strain step 1e-5, the maximum descriptor-derivative error changes from 6.18e-3 to 5.59e-10 for the Gamma-point case and from 2.04e-2 to 2.13e-11 for the multi-k case. The resulting model-energy derivatives agree with central differences to approximately 3.87e-12 and 6.56e-13 Hartree, respectively. These results motivate the updated gdmepsl_ref.txt and gvepsl_ref.txt files for both unit-test fixtures.

Electronic-step and unconverged output

deepks_out_freq_elec now requires deepks_out_labels>0 and controls sampling for both direct and gradient labels. A value of zero disables periodic sampling; a positive value saves the first electronic step and subsequent interval-selected steps. These samples use DeePKS_Labels_Elec/ and the _eN suffix.

The SCF-end caller now passes the actual convergence state to out_deepks_labels. The output routine handles the two stages centrally:

  • During electronic iterations, a converged step defers output to the SCF-end call, even when its step number matches deepks_out_freq_elec. That state appears in the final output only, without an additional _eN intermediate sample.
  • A converged SCF-end call writes the regular final electronic labels, the requested force/virial precalculation operators, and applicable final geometry files.
  • An unconverged SCF-end call writes an electronic snapshot in the main output directory using _efinal, for example deepks_etot_efinal.npy. This snapshot is saved even when the periodic sampling frequency is zero. Its contents follow the electronic-state output path; final force/virial precalculation operators and final geometry files are produced by the converged path.

Physical force and stress labels follow the ordinary force/stress postprocessing calculations, including calculations performed after an unconverged SCF. Filename selection is centralized in get_filename.

Diagnostic output and reference updates

deepks_projdm.dat and deepks_desc.dat are diagnostic text dumps. They are now written by rank zero when deepks_out_unittest is enabled. Routine descriptor output uses deepks_dm_eig.npy.

The previous integration-property collector read deepks_desc.dat as well as the NPY descriptor output. It now checks the NPY data directly with the existing get_sum_abs.py helper. Consequently, each of the 27 affected existing result.ref files drops its deepks_desc entry. This is the reason the PR touches many integration reference files. The four strain-derivative reference files described above reflect the separate numerical correction.

Four existing integration cases also require numerical reference updates:

  • 09_NO_GO_deepks_basic and 10_NO_KP_deepks_basic: update deepks_spre, the absolute-value sum of the descriptor strain derivatives, to reflect the corrected grad_overlap[i] * r[j] convention.
  • 25_NO_GO_deepks_out_freq_elec and 26_NO_KP_deepks_out_freq_elec: update the same strain-derivative summary and the six electronic-step summaries for energy, energy difference, Hamiltonian, Hamiltonian difference, Hamiltonian precalculation, and overlap. These cases converge at electronic step 5 with an output interval of 1. The old references summed five intermediate samples; the updated output contains steps 1–4 in DeePKS_Labels_Elec/ and writes the converged state through the final-output path. The changed sums therefore exclude the formerly duplicated fifth sample.

These reference entries are regenerated from the corresponding calculations. The numerical updates are limited to the affected strain-derivative and electronic-sampling summaries.

The force/stress, force-operator, strain-operator, and real-space Hamiltonian tests move to unittests/test_deepks_{force,fpre,spre,vdrpre}.cpp. They cover Gram construction, Hamiltonian projection, model-gradient reconstruction, and the relevant units. Two Autotest cases add Gamma-point data without a model and multi-k data with a model and electronic-step sampling. Their references are generated from actual calculations, and the existing property collector checks the new NPY outputs through absolute-value sums.

The unit-test migration in this PR is partial. The 16 reference files used directly by the migrated checks move to unittests/support/ with the .txt extension, preserving their contents during relocation. The standalone strain-PDM check moves into test_deepks_spre.cpp, alongside the strain-operator checks.

The migrated tests still use shared runners, input preparation, and prerequisite checks from test/. Their numerical targets are created in test/CMakeLists.txt and receive the migrated sources through target_sources() in unittests/CMakeLists.txt; the test processes also use data from both support directories. These cross-directory references are a temporary arrangement. A separate follow-up PR will separate input preparation from feature-specific assertions and consolidate the remaining test infrastructure and data in unittests/.

@ErjieWu ErjieWu added Bugs Bugs that only solvable with sufficient knowledge of DFT Features Needed The features are indeed needed, and developers should have sophisticated knowledge Machine Learning & AI Issues related to AI Tests/Examples Issues/PR related to unit tests and integrate tests labels Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bugs Bugs that only solvable with sufficient knowledge of DFT Features Needed The features are indeed needed, and developers should have sophisticated knowledge Machine Learning & AI Issues related to AI Tests/Examples Issues/PR related to unit tests and integrate tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants