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 dtype in self_energies #347

Merged
merged 5 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 4 additions & 8 deletions tools/comp6.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


HARTREE2KCAL = 627.509
dtype = torch.float32

# parse command line arguments
parser = argparse.ArgumentParser()
Expand All @@ -21,7 +20,7 @@
parser = parser.parse_args()

# run benchmark
ani1x = torchani.models.ANI1x().to(dtype).to(parser.device)
ani1x = torchani.models.ANI1x().to(parser.device)


def recursive_h5_files(base):
Expand Down Expand Up @@ -80,14 +79,11 @@ def do_benchmark(model):
rmse_averager_force = Averager()
for i in tqdm.tqdm(dataset, position=0, desc="dataset"):
# read
coordinates = torch.tensor(
i['coordinates'], dtype=dtype, device=parser.device)
coordinates = torch.tensor(i['coordinates'], device=parser.device)
species = model.species_to_tensor(i['species']) \
.unsqueeze(0).expand(coordinates.shape[0], -1)
energies = torch.tensor(i['energies'], dtype=dtype,
device=parser.device)
forces = torch.tensor(i['forces'], dtype=dtype,
device=parser.device)
energies = torch.tensor(i['energies'], device=parser.device)
forces = torch.tensor(i['forces'], device=parser.device)
# compute
energies2, forces2 = by_batch(species, coordinates, model)
ediff = energies - energies2
Expand Down
2 changes: 1 addition & 1 deletion torchani/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def sae(self, species):
intercept = self.self_energies[-1]

self_energies = self.self_energies[species]
self_energies[species == torch.tensor(-1, device=species.device)] = torch.tensor(0, device=species.device)
self_energies[species == torch.tensor(-1, device=species.device)] = torch.tensor(0, device=species.device, dtype=torch.double)
Copy link
Contributor

Choose a reason for hiding this comment

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

how about removing the dtype and replace the 0 with 0.0?

Copy link
Member Author

@farhadrgh farhadrgh Oct 24, 2019

Choose a reason for hiding this comment

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

@zasdfgbnm self_energies.dtype should be double, but float32 is enforced in comp6.py, is there any particular reason for that?

see in: https://github.com/aiqm/torchani/pull/347/checks?check_run_id=273594990#step:9:25

Copy link
Contributor

Choose a reason for hiding this comment

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

@farhadrgh No, there is no reason. I guess it is just randomly picking a dtype.

Copy link
Contributor

Choose a reason for hiding this comment

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

@farhadrgh Also, there are some trick here, energy shifter is a module, so if you put it inside
model = torch.nn.Sequential(aev_computer, nn, energy_shifter) and then model.to(torch.float) it will also be cast into float. I don't have a good idea on how to solve that.

Copy link
Member Author

Choose a reason for hiding this comment

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

I fixed the dtype in comp6.py (6930879), this will let the tests pass peacefully

return self_energies.sum(dim=1) + intercept

def subtract_from_dataset(self, atomic_properties, properties):
Expand Down