"tests/benchmarks/bm_chamfer.py" did not exist on "d57daa6f855f581268d875a0ad93c570871f457b"
test_benchmark.py 3.98 KB
Newer Older
Xiang Gao's avatar
Xiang Gao committed
1
2
3
4
5
6
7
8
import torch
import torchani
import unittest
import copy


class TestBenchmark(unittest.TestCase):

9
    def setUp(self):
Xiang Gao's avatar
Xiang Gao committed
10
11
        self.conformations = 100
        self.species = list('HHCCNNOO')
12
        self.coordinates = torch.randn(self.conformations, 8, 3)
Xiang Gao's avatar
Xiang Gao committed
13
14
        self.count = 100

15
    def _testModule(self, run_module, result_module, asserts):
Xiang Gao's avatar
Xiang Gao committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
        keys = []
        for i in asserts:
            if '>=' in i:
                i = i.split('>=')
                keys += [i[0].strip(), i[1].strip()]
            elif '<=' in i:
                i = i.split('<=')
                keys += [i[0].strip(), i[1].strip()]
            elif '>' in i:
                i = i.split('>')
                keys += [i[0].strip(), i[1].strip()]
            elif '<' in i:
                i = i.split('<')
                keys += [i[0].strip(), i[1].strip()]
            elif '=' in i:
                i = i.split('=')
                keys += [i[0].strip(), i[1].strip()]
            else:
                keys.append(i.strip())
35
        self.assertEqual(set(result_module.timers.keys()), set(keys))
Xiang Gao's avatar
Xiang Gao committed
36
        for i in keys:
37
38
            self.assertEqual(result_module.timers[i], 0)
        old_timers = copy.copy(result_module.timers)
Xiang Gao's avatar
Xiang Gao committed
39
        for _ in range(self.count):
40
            run_module((self.species, self.coordinates))
Xiang Gao's avatar
Xiang Gao committed
41
            for i in keys:
42
                self.assertLess(old_timers[i], result_module.timers[i])
Xiang Gao's avatar
Xiang Gao committed
43
44
45
46
47
48
            for i in asserts:
                if '>=' in i:
                    i = i.split('>=')
                    key0 = i[0].strip()
                    key1 = i[1].strip()
                    self.assertGreaterEqual(
49
                        result_module.timers[key0], result_module.timers[key1])
Xiang Gao's avatar
Xiang Gao committed
50
51
52
53
54
                elif '<=' in i:
                    i = i.split('<=')
                    key0 = i[0].strip()
                    key1 = i[1].strip()
                    self.assertLessEqual(
55
                        result_module.timers[key0], result_module.timers[key1])
Xiang Gao's avatar
Xiang Gao committed
56
57
58
59
60
                elif '>' in i:
                    i = i.split('>')
                    key0 = i[0].strip()
                    key1 = i[1].strip()
                    self.assertGreater(
61
                        result_module.timers[key0], result_module.timers[key1])
Xiang Gao's avatar
Xiang Gao committed
62
63
64
65
                elif '<' in i:
                    i = i.split('<')
                    key0 = i[0].strip()
                    key1 = i[1].strip()
66
67
                    self.assertLess(result_module.timers[key0],
                                    result_module.timers[key1])
Xiang Gao's avatar
Xiang Gao committed
68
69
70
71
                elif '=' in i:
                    i = i.split('=')
                    key0 = i[0].strip()
                    key1 = i[1].strip()
72
73
74
75
76
                    self.assertEqual(result_module.timers[key0],
                                     result_module.timers[key1])
            old_timers = copy.copy(result_module.timers)
        result_module.reset_timers()
        self.assertEqual(set(result_module.timers.keys()), set(keys))
Xiang Gao's avatar
Xiang Gao committed
77
        for i in keys:
78
            self.assertEqual(result_module.timers[i], 0)
Xiang Gao's avatar
Xiang Gao committed
79
80

    def testAEV(self):
81
82
        aev_computer = torchani.SortedAEV(benchmark=True)
        prepare = torchani.PrepareInput(aev_computer.species)
83
84
        run_module = torch.nn.Sequential(prepare, aev_computer)
        self._testModule(run_module, aev_computer, [
Xiang Gao's avatar
Xiang Gao committed
85
86
87
                         'terms and indices>radial terms',
                         'terms and indices>angular terms',
                         'total>terms and indices',
88
89
90
                         'total>combinations', 'total>assemble',
                         'total>mask_r', 'total>mask_a'
                         ])
Xiang Gao's avatar
Xiang Gao committed
91

92
    def testANIModel(self):
93
94
95
96
        aev_computer = torchani.SortedAEV()
        prepare = torchani.PrepareInput(aev_computer.species)
        model = torchani.models.NeuroChemNNP(aev_computer.species,
                                             benchmark=True)
97
98
        run_module = torch.nn.Sequential(prepare, aev_computer, model)
        self._testModule(run_module, model, ['forward'])
Xiang Gao's avatar
Xiang Gao committed
99
100
101
102


if __name__ == '__main__':
    unittest.main()