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

fix(pt): optimize graph memory usage #4006

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions deepmd/pt/model/model/dipole_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def forward_lower(
fparam=fparam,
aparam=aparam,
do_atomic_virial=do_atomic_virial,
inference=True,
)
if self.get_fitting_net() is not None:
model_predict = {}
Expand Down
1 change: 1 addition & 0 deletions deepmd/pt/model/model/dos_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def forward_lower(
fparam=fparam,
aparam=aparam,
do_atomic_virial=do_atomic_virial,
inference=True,
)
if self.get_fitting_net() is not None:
model_predict = {}
Expand Down
1 change: 1 addition & 0 deletions deepmd/pt/model/model/dp_zbl_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def forward_lower(
fparam=fparam,
aparam=aparam,
do_atomic_virial=do_atomic_virial,
inference=True,
)

model_predict = {}
Expand Down
1 change: 1 addition & 0 deletions deepmd/pt/model/model/ener_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def forward_lower(
aparam=aparam,
do_atomic_virial=do_atomic_virial,
comm_dict=comm_dict,
inference=True,
)
if self.get_fitting_net() is not None:
model_predict = {}
Expand Down
4 changes: 4 additions & 0 deletions deepmd/pt/model/model/make_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def forward_common_lower(
aparam: Optional[torch.Tensor] = None,
do_atomic_virial: bool = False,
comm_dict: Optional[Dict[str, torch.Tensor]] = None,
inference: bool = False,
):
"""Return model prediction. Lower interface that takes
extended atomic coordinates and types, nlist, and mapping
Expand All @@ -239,6 +240,8 @@ def forward_common_lower(
whether calculate atomic virial.
comm_dict
The data needed for communication for parallel inference.
inference
Whether only perform inference rather than undergoing training.

Returns
-------
Expand Down Expand Up @@ -267,6 +270,7 @@ def forward_common_lower(
self.atomic_output_def(),
cc_ext,
do_atomic_virial=do_atomic_virial,
inference=inference,
iProzd marked this conversation as resolved.
Show resolved Hide resolved
)
model_predict = self.output_type_cast(model_predict, input_prec)
return model_predict
Expand Down
1 change: 1 addition & 0 deletions deepmd/pt/model/model/polar_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def forward_lower(
fparam=fparam,
aparam=aparam,
do_atomic_virial=do_atomic_virial,
inference=True,
)
if self.get_fitting_net() is not None:
model_predict = {}
Expand Down
3 changes: 3 additions & 0 deletions deepmd/pt/model/model/spin_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ def forward_common_lower(
fparam: Optional[torch.Tensor] = None,
aparam: Optional[torch.Tensor] = None,
do_atomic_virial: bool = False,
inference: bool = False,
):
nframes, nloc = nlist.shape[:2]
(
Expand All @@ -487,6 +488,7 @@ def forward_common_lower(
fparam=fparam,
aparam=aparam,
do_atomic_virial=do_atomic_virial,
inference=inference,
)
model_output_type = self.backbone_model.model_output_type()
if "mask" in model_output_type:
Expand Down Expand Up @@ -611,6 +613,7 @@ def forward_lower(
fparam=fparam,
aparam=aparam,
do_atomic_virial=do_atomic_virial,
inference=True,
)
model_predict = {}
model_predict["atom_energy"] = model_ret["energy"]
Expand Down
29 changes: 25 additions & 4 deletions deepmd/pt/model/model/transform_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,27 @@ def atomic_virial_corr(
faked_grad = torch.ones_like(sumce0)
lst = torch.jit.annotate(List[Optional[torch.Tensor]], [faked_grad])
extended_virial_corr0 = torch.autograd.grad(
[sumce0], [extended_coord], grad_outputs=lst, create_graph=True
[sumce0],
[extended_coord],
grad_outputs=lst,
create_graph=False,
retain_graph=True,
)[0]
assert extended_virial_corr0 is not None
extended_virial_corr1 = torch.autograd.grad(
[sumce1], [extended_coord], grad_outputs=lst, create_graph=True
[sumce1],
[extended_coord],
grad_outputs=lst,
create_graph=False,
retain_graph=True,
)[0]
assert extended_virial_corr1 is not None
extended_virial_corr2 = torch.autograd.grad(
[sumce2], [extended_coord], grad_outputs=lst, create_graph=True
[sumce2],
[extended_coord],
grad_outputs=lst,
create_graph=False,
retain_graph=True,
njzjz marked this conversation as resolved.
Show resolved Hide resolved
)[0]
assert extended_virial_corr2 is not None
extended_virial_corr = torch.concat(
Expand All @@ -61,11 +73,16 @@ def task_deriv_one(
extended_coord: torch.Tensor,
do_virial: bool = True,
do_atomic_virial: bool = False,
inference: bool = False,
):
faked_grad = torch.ones_like(energy)
lst = torch.jit.annotate(List[Optional[torch.Tensor]], [faked_grad])
extended_force = torch.autograd.grad(
[energy], [extended_coord], grad_outputs=lst, create_graph=True
[energy],
[extended_coord],
grad_outputs=lst,
create_graph=not inference,
njzjz marked this conversation as resolved.
Show resolved Hide resolved
retain_graph=True,
njzjz marked this conversation as resolved.
Show resolved Hide resolved
)[0]
assert extended_force is not None
extended_force = -extended_force
Expand Down Expand Up @@ -106,6 +123,7 @@ def take_deriv(
coord_ext: torch.Tensor,
do_virial: bool = False,
do_atomic_virial: bool = False,
inference: bool = False,
):
size = 1
for ii in vdef.shape:
Expand All @@ -123,6 +141,7 @@ def take_deriv(
coord_ext,
do_virial=do_virial,
do_atomic_virial=do_atomic_virial,
inference=inference,
)
# nf x nloc x 1 x 3, nf x nloc x 1 x 9
ffi = ffi.unsqueeze(-2)
Expand All @@ -146,6 +165,7 @@ def fit_output_to_model_output(
fit_output_def: FittingOutputDef,
coord_ext: torch.Tensor,
do_atomic_virial: bool = False,
inference: bool = False,
) -> Dict[str, torch.Tensor]:
"""Transform the output of the fitting network to
the model output.
Expand All @@ -169,6 +189,7 @@ def fit_output_to_model_output(
coord_ext,
do_virial=vdef.c_differentiable,
do_atomic_virial=do_atomic_virial,
inference=inference,
)
model_ret[kk_derv_r] = dr
if vdef.c_differentiable:
Expand Down