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

Allowing torchani to use GPU #327

Merged
merged 6 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion qcengine/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class TaskConfig(pydantic.BaseModel):
scratch_messy: bool = pydantic.Field(
False, description="Leave scratch directory and contents on disk after completion."
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing the whitespace here probably caused a problem with our linter.

Could you run make format and issue a commit with the changed files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done by d7ced82 😉

class Config:
extra = "forbid"

Expand Down
11 changes: 6 additions & 5 deletions qcengine/programs/torchani.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def compute(self, input_data: "AtomicInput", config: "TaskConfig") -> "AtomicRes
import torch
import torchani

device = torch.device("cpu")
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Failure flag
ret_data = {"success": False}
Expand All @@ -126,7 +126,8 @@ def compute(self, input_data: "AtomicInput", config: "TaskConfig") -> "AtomicRes
# Build coord array
geom_array = input_data.molecule.geometry.reshape(1, -1, 3) * ureg.conversion_factor("bohr", "angstrom")
coordinates = torch.tensor(geom_array.tolist(), requires_grad=True, device=device)

model.to(device)

_, energy_array = model((species, coordinates))
energy = energy_array.mean()
ensemble_std = energy_array.std()
Expand All @@ -139,11 +140,11 @@ def compute(self, input_data: "AtomicInput", config: "TaskConfig") -> "AtomicRes
elif input_data.driver == "gradient":
derivative = torch.autograd.grad(energy.sum(), coordinates)[0].squeeze()
ret_data["return_result"] = (
np.asarray(derivative * ureg.conversion_factor("angstrom", "bohr")).ravel().tolist()
np.asarray(derivative.cpu() * ureg.conversion_factor("angstrom", "bohr")).ravel().tolist()
)
elif input_data.driver == "hessian":
hessian = torchani.utils.hessian(coordinates, energies=energy)
ret_data["return_result"] = np.asarray(hessian)
ret_data["return_result"] = np.asarray(hessian.cpu())
else:
raise InputError(
f"TorchANI can only compute energy, gradient, and hessian driver methods. Found {input_data.driver}."
Expand Down Expand Up @@ -172,7 +173,7 @@ def compute(self, input_data: "AtomicInput", config: "TaskConfig") -> "AtomicRes
ret_data["extras"] = input_data.extras.copy()
ret_data["extras"].update(
{
"ensemble_energies": energy_array.detach().numpy(),
"ensemble_energies": energy_array.cpu().detach().numpy(),
"ensemble_energy_avg": energy.item(),
WardLT marked this conversation as resolved.
Show resolved Hide resolved
"ensemble_energy_std": ensemble_std.item(),
"ensemble_per_root_atom_disagreement": ensemble_scaled_std.item(),
Expand Down