training-benchmark.py 1.57 KB
Newer Older
1
import sys
Xiang Gao's avatar
Xiang Gao committed
2
import torch
3
import ignite
Xiang Gao's avatar
Xiang Gao committed
4
5
import torchani
import timeit
6
import model
Xiang Gao's avatar
Xiang Gao committed
7

8
9
10
11
12
13
14
15
16
chunk_size = 256
batch_chunks = 4
dataset_path = sys.argv[1]
shift_energy = torchani.EnergyShifter()
dataset = torchani.data.ANIDataset(
    dataset_path, chunk_size,
    transform=[shift_energy.dataset_subtract_sae])
dataloader = torchani.data.dataloader(dataset, batch_chunks)
nnp = model.get_or_create_model('/tmp/model.pt', True)
Xiang Gao's avatar
Xiang Gao committed
17
18


19
class Flatten(torch.nn.Module):
Xiang Gao's avatar
Xiang Gao committed
20

21
22
23
    def __init__(self, model):
        super(Flatten, self).__init__()
        self.model = model
Xiang Gao's avatar
Xiang Gao committed
24

25
26
    def forward(self, *input):
        return self.model(*input).flatten()
Xiang Gao's avatar
Xiang Gao committed
27
28


29
30
31
batch_nnp = torchani.models.BatchModel(Flatten(nnp))
container = torchani.ignite.Container({'energies': batch_nnp})
optimizer = torch.optim.Adam(nnp.parameters())
Xiang Gao's avatar
Xiang Gao committed
32

33
34
trainer = ignite.engine.create_supervised_trainer(
    container, optimizer, torchani.ignite.energy_mse_loss)
Xiang Gao's avatar
Xiang Gao committed
35
36

start = timeit.default_timer()
37
trainer.run(dataloader, max_epochs=1)
Xiang Gao's avatar
Xiang Gao committed
38
elapsed = round(timeit.default_timer() - start, 2)
39
40
41
42
43
44
45
46
47
48
print('Radial terms:', nnp.aev_computer.timers['radial terms'])
print('Angular terms:', nnp.aev_computer.timers['angular terms'])
print('Terms and indices:', nnp.aev_computer.timers['terms and indices'])
print('Combinations:', nnp.aev_computer.timers['combinations'])
print('Mask R:', nnp.aev_computer.timers['mask_r'])
print('Mask A:', nnp.aev_computer.timers['mask_a'])
print('Assemble:', nnp.aev_computer.timers['assemble'])
print('Total AEV:', nnp.aev_computer.timers['total'])
print('NN:', nnp.timers['nn'])
print('Total Forward:', nnp.timers['forward'])
Xiang Gao's avatar
Xiang Gao committed
49
print('Epoch time:', elapsed)