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

Lyuda jan6 #20

Merged
merged 35 commits into from
Jan 7, 2025
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9b63a2c
updating skp's torch_branch
osu1191 Jul 10, 2024
829cae9
edited the optimization code
osu1191 Jul 11, 2024
9af610a
added module list
osu1191 Jul 13, 2024
6cc9c4a
removing unwanted files
osu1191 Jul 14, 2024
479686e
getting rid of torch warnings
osu1191 Jul 14, 2024
d23111d
no more warnings
osu1191 Jul 14, 2024
d7e1d3b
added torch_nn variable for choice of model potential
osu1191 Jul 16, 2024
c49287d
debugging... issues in opt_step routine in libopt/
osu1191 Jul 17, 2024
ad82349
debugging and verbose
osu1191 Jul 18, 2024
643d2f3
editing comments for better understanding
osu1191 Jul 18, 2024
3214bda
atom_types fixed, extra calling of torch headers commented out
osu1191 Aug 7, 2024
f8168b4
added setup.sh for environmental variables
osu1191 Aug 26, 2024
8ba1393
added new_README for installation help
osu1191 Aug 26, 2024
23b9352
working on single loading of TorchANI NNPs
osu1191 Aug 27, 2024
0b04b30
parity with lyuda_torch
osu1191 Sep 11, 2024
91475bb
added aev.pt and spec_elpot
osu1191 Oct 8, 2024
688bdaf
removed global_state part
osu1191 Oct 10, 2024
91494ec
added custom path options
osu1191 Oct 17, 2024
7f71ba0
added custom path options
osu1191 Oct 17, 2024
1765e0d
added enable_elpot switch to turn off e_elec and e_qq for custom models
osu1191 Oct 20, 2024
7d668fd
removed duplicate files
osu1191 Oct 20, 2024
c8b361f
switched on OpenMP option in cmake
osu1191 Oct 23, 2024
76a3490
auto + backprop
osu1191 Oct 23, 2024
afa7d53
added torch_switch option for compilation
osu1191 Oct 24, 2024
3ba978b
parity with lyuda_Oct20
osu1191 Oct 27, 2024
19a1fee
parity with lyuda_Oct20
osu1191 Oct 27, 2024
4f780ea
fixed autograd and back prop issues
osu1191 Nov 19, 2024
4f4fad2
cleaning up test files
osu1191 Nov 21, 2024
ead383c
updated torch work
slipchenko Jan 3, 2025
2b54379
small changes in test jobs and printouts
slipchenko Jan 3, 2025
a2ee202
added installed dir and share/libefp/nnlib as asked in README-torch.md
osu1191 Jan 5, 2025
7f90532
added installation option in setup and lib64 to lib
osu1191 Jan 6, 2025
e70b3aa
final fixes torch version
slipchenko Jan 7, 2025
c7ea848
final fixes
slipchenko Jan 7, 2025
978ce1e
Merge branch 'master' into lyuda_Jan6
slipchenko Jan 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed autograd and back prop issues
osu1191 committed Nov 19, 2024

Verified

This commit was signed with the committer’s verified signature.
Freyskeyd Simon Paitrault
commit 4f780ea3c8b232628a3cbf626396320be043cbc5
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ option_with_default(BUILD_FPIC "Libraries will be compiled with position indepen
if((${BUILD_SHARED_LIBS}) AND NOT ${BUILD_FPIC})
message(FATAL_ERROR "BUILD_SHARED_LIBS ON and BUILD_FPIC OFF are incompatible, as shared library requires position independent code")
endif()
option_with_print(LIBEFP_ENABLE_OPENMP "Enable OpenMP parallelization. Psi4 wants OFF" OFF)
option_with_print(LIBEFP_ENABLE_OPENMP "Enable OpenMP parallelization. Psi4 wants OFF" ON)
option_with_print(ENABLE_GENERIC "Enable mostly static linking in shared library" OFF)
include(xhost) # defines: option(ENABLE_XHOST "Enable processor-specific optimization" ON)
option_with_print(FRAGLIB_UNDERSCORE_L "DEPRECATED: Installed fragment library has names ending in _L. Psi4 wants OFF" ON)
67 changes: 52 additions & 15 deletions efpmd/src/energy.c
Original file line number Diff line number Diff line change
@@ -89,34 +89,35 @@ void compute_energy(struct state *state, bool do_grad)

/* Torch fragment part here */
#ifdef TORCH_SWITCH
if (cfg_get_bool(state->cfg, "enable_torch")) {

if (cfg_get_bool(state->cfg, "enable_torch") && cfg_get_int(state->cfg, "opt_special_frag") > -1) {

spec_frag = cfg_get_int(state->cfg, "special_fragment");
check_fail(efp_get_frag_atom_count(state->efp, spec_frag, &n_special_atoms)); // SKP

if (cfg_get_bool(state->cfg, "enable_elpot")) {

double *elpot;
struct efp_atom *atoms;
size_t n_atoms;
size_t spec_frag = cfg_get_int(state->cfg, "special_fragment");

check_fail(efp_get_frag_atom_count(state->efp, spec_frag, &n_atoms)); // SKP
atoms = xmalloc(n_atoms * sizeof(struct efp_atom));
check_fail(efp_get_frag_atoms(state->efp, spec_frag, n_atoms, atoms));
atoms = xmalloc(n_special_atoms * sizeof(struct efp_atom));
check_fail(efp_get_frag_atoms(state->efp, spec_frag, n_special_atoms, atoms));

elpot = xcalloc(n_atoms, sizeof(double));
elpot = xcalloc(n_special_atoms, sizeof(double));

for (size_t j = 0; j < n_atoms; j++) {
for (size_t j = 0; j < n_special_atoms; j++) {
check_fail(efp_get_elec_potential(state->efp, spec_frag, &atoms[j].x, elpot + j));
}

free(atoms);

if (cfg_get_int(state->cfg, "print") > 1) {
//if (cfg_get_int(state->cfg, "print") > 1) {
printf("\nTesting elpot printing\n");
for (iatom = 0; iatom < n_atoms; iatom++) {
for (iatom = 0; iatom < n_special_atoms; iatom++) {
printf("%12.6f\n", *(elpot + iatom));
}
printf("Done testing elpot\n\n");
}
// }

torch_set_elpot(state->torch, elpot);

@@ -127,26 +128,62 @@ void compute_energy(struct state *state, bool do_grad)
torch_custom_compute(state->torch, cfg_get_int(state->cfg, "print"));
clock_t end_time = clock();
double time_taken = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
printf("Time taken by energy_compute() is: %f seconds\n", time_taken);
printf("=======================================================\n\n");
if (cfg_get_int(state->cfg, "print")>0) printf("Time taken by energy_compute() is: %f seconds\n", time_taken);
//printf("=======================================================\n\n");
}
else {
printf("\n\n=================REGULAR ANI-MODEL=====================\n");
clock_t start_time = clock();
torch_compute(state->torch, cfg_get_string(state->cfg, "ml_path"), cfg_get_int(state->cfg, "print"));
clock_t end_time = clock();
double time_taken = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
printf("Time taken by energy_compute() is: %f seconds\n", time_taken);
printf("\n\n========================================================\n");
if (cfg_get_int(state->cfg, "print")>0) printf("Time taken by energy_compute() is: %f seconds\n", time_taken);
//printf("\n\n========================================================\n");
}

state->torch_energy = torch_get_energy(state->torch);
state->energy += state->torch_energy;

if (do_grad) {
torch_get_gradient(state->torch, state->torch_grad);

if (cfg_get_int(state->cfg, "print") > 1) {
printf("\nTorch gradient in energy.c\n");
for (size_t i = 0; i < 3*n_special_atoms; i++)
printf("%lf ", state->torch_grad[i]);
}

// combine EFP and torch (atomic) gradients on special fragments
// do not add EFP contribution on a special fragment if it is the only fragment in the system
if (nfrag > 1) {
double *tmp_grad = xcalloc(n_special_atoms * 3, sizeof(double));

// get gradient from fragment atoms directly if QQ and LJ terms are computed
if (cfg_get_enum(state->cfg, "atom_gradient") == ATOM_GRAD_MM) {
check_fail(efp_get_atom_gradient(state->efp, spec_frag, tmp_grad));
} else
check_fail(efp_get_frag_atomic_gradient(state->efp, spec_frag, tmp_grad));

if (cfg_get_int(state->cfg, "print") > 1) {
printf("\nEFP fragment atomic gradient\n");
for (size_t i = 0; i < 3 * n_special_atoms; i++)
printf("%lf ", tmp_grad[i]);
printf("\n");
}

// add EFP and torch gradients
if (cfg_get_int(state->cfg, "print") > 1) {
printf("\nTotal torch + EFP gradient\n");
for (size_t i = 0; i < 3 * n_special_atoms; i++)
printf("%lf ", state->torch_grad[i]);
printf("\n");
}
free(tmp_grad);
}
}
}


#endif
/* MM force field part */
if (state->ff == NULL)
198 changes: 198 additions & 0 deletions efpmd/src/energy.c_orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*-
* Copyright (c) 2012-2015 Ilya Kaliman
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include "common.h"
#ifdef TORCH_SWITCH
#include "torch.h"
#endif
#include "time.h"
//#include "../torch/torch.h"

// void get_frag_elpot(struct state *);

/* current coordinates from efp struct are used */
void compute_energy(struct state *state, bool do_grad)
{
struct efp_atom *atoms;
struct efp_energy efp_energy;
double xyz[3], xyzabc[6], *grad;
size_t ifrag, nfrag, iatom, natom, spec_frag, n_special_atoms;
int itotal;

if (cfg_get_int(state->cfg, "print")>0) {
print_geometry(state->efp);
}

/* EFP part */
// print_geometry(state->efp);
check_fail(efp_compute(state->efp, do_grad));
check_fail(efp_get_energy(state->efp, &efp_energy));
check_fail(efp_get_frag_count(state->efp, &nfrag));

if (do_grad) {
check_fail(efp_get_gradient(state->efp, state->grad));
check_fail(efp_get_point_charge_gradient(state->efp,
state->grad + 6 * nfrag));
}

state->energy = efp_energy.total;
// print_energy(state);
// printf("\n State energy (state->energy) %lf \n", state->energy);

/* constraints */
for (ifrag = 0; ifrag < nfrag; ifrag++) {
const struct frag *frag = state->sys->frags + ifrag;

check_fail(efp_get_frag_xyzabc(state->efp, ifrag, xyzabc));

if (frag->constraint_enable) {
double dr2, drx, dry, drz;

drx = xyzabc[0] - frag->constraint_xyz.x;
dry = xyzabc[1] - frag->constraint_xyz.y;
drz = xyzabc[2] - frag->constraint_xyz.z;

dr2 = drx * drx + dry * dry + drz * drz;
state->energy += 0.5 * frag->constraint_k * dr2;

if (do_grad) {
grad = state->grad + 6 * ifrag;
grad[0] += frag->constraint_k * drx;
grad[1] += frag->constraint_k * dry;
grad[2] += frag->constraint_k * drz;
}
}
}

/* Torch fragment part here */
#ifdef TORCH_SWITCH

if (cfg_get_bool(state->cfg, "enable_torch")) {

if (cfg_get_bool(state->cfg, "enable_elpot")) {

double *elpot;
struct efp_atom *atoms;
size_t n_atoms;
size_t spec_frag = cfg_get_int(state->cfg, "special_fragment");

check_fail(efp_get_frag_atom_count(state->efp, spec_frag, &n_atoms)); // SKP
atoms = xmalloc(n_atoms * sizeof(struct efp_atom));
check_fail(efp_get_frag_atoms(state->efp, spec_frag, n_atoms, atoms));

elpot = xcalloc(n_atoms, sizeof(double));

for (size_t j = 0; j < n_atoms; j++) {
check_fail(efp_get_elec_potential(state->efp, spec_frag, &atoms[j].x, elpot + j));
}

free(atoms);

if (cfg_get_int(state->cfg, "print") > 1) {
printf("\nTesting elpot printing\n");
for (iatom = 0; iatom < n_atoms; iatom++) {
printf("%12.6f\n", *(elpot + iatom));
}
printf("Done testing elpot\n\n");
}

torch_set_elpot(state->torch, elpot);

free(elpot);

printf("\n\n=================CUSTOM MODEL=====================\n\n");
clock_t start_time = clock();
torch_custom_compute(state->torch, cfg_get_int(state->cfg, "print"));
clock_t end_time = clock();
double time_taken = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
printf("Time taken by energy_compute() is: %f seconds\n", time_taken);
printf("=======================================================\n\n");
}
else {
printf("\n\n=================REGULAR ANI-MODEL=====================\n");
clock_t start_time = clock();
torch_compute(state->torch, cfg_get_string(state->cfg, "ml_path"), cfg_get_int(state->cfg, "print"));
clock_t end_time = clock();
double time_taken = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
printf("Time taken by energy_compute() is: %f seconds\n", time_taken);
printf("\n\n========================================================\n");
}

state->torch_energy = torch_get_energy(state->torch);
state->energy += state->torch_energy;

if (do_grad) {
torch_get_gradient(state->torch, state->torch_grad);
}
}


#endif
/* MM force field part */
if (state->ff == NULL)
return;

for (ifrag = 0, itotal = 0; ifrag < nfrag; ifrag++) {
check_fail(efp_get_frag_atom_count(state->efp, ifrag, &natom));
atoms = xmalloc(natom * sizeof(struct efp_atom));
check_fail(efp_get_frag_atoms(state->efp, ifrag, natom, atoms));

for (iatom = 0; iatom < natom; iatom++, itotal++)
ff_set_atom_xyz(state->ff, itotal, &atoms[iatom].x);

free(atoms);
}

ff_compute(state->ff, do_grad);

if (do_grad) {
for (ifrag = 0, itotal = 0, grad = state->grad; ifrag < nfrag; ifrag++, grad += 6) {
check_fail(efp_get_frag_xyzabc(state->efp, ifrag, xyzabc));
check_fail(efp_get_frag_atom_count(state->efp, ifrag, &natom));
atoms = xmalloc(natom * sizeof(struct efp_atom));
check_fail(efp_get_frag_atoms(state->efp, ifrag, natom, atoms));

for (iatom = 0; iatom < natom; iatom++, itotal++) {
ff_get_atom_gradient(state->ff, itotal, xyz);

grad[0] += xyz[0];
grad[1] += xyz[1];
grad[2] += xyz[2];

grad[3] += (atoms[iatom].y - xyzabc[1]) * xyz[2] -
(atoms[iatom].z - xyzabc[2]) * xyz[1];
grad[4] += (atoms[iatom].z - xyzabc[2]) * xyz[0] -
(atoms[iatom].x - xyzabc[0]) * xyz[2];
grad[5] += (atoms[iatom].x - xyzabc[0]) * xyz[1] -
(atoms[iatom].y - xyzabc[1]) * xyz[0];
}

free(atoms);
}
}

state->energy += ff_get_energy(state->ff);
}
Loading