forked from alibaba/TinyNeuralNetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qat_module_test.py
94 lines (66 loc) · 2.7 KB
/
qat_module_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import unittest
import torch
import torch.nn as nn
from tinynn.graph.quantization.modules import QGLU, QPReLU, QSiLU
class QATModuleTester(unittest.TestCase):
def test_prelu(self):
for i in range(100):
val = torch.rand([])
orig = nn.PReLU(init=val)
quant = QPReLU(orig)
inp = (torch.randn((100, 100)) - 0.5) * 2
orig_outp = orig(inp)
quant_outp = quant(inp)
if not torch.allclose(orig_outp, quant_outp):
print('original:')
print(orig_outp)
print('quanted:')
print(quant_outp)
print('diff (min, max):', torch.max(quant_outp - orig_outp), torch.min(quant_outp - orig_outp))
self.assertTrue(False)
def test_prelu_multi_channel(self):
for i in range(100):
val = torch.rand([])
orig = nn.PReLU(num_parameters=32, init=val)
quant = QPReLU(orig)
inp = (torch.randn((1, 32, 24, 24)) - 0.5) * 2
orig_outp = orig(inp)
quant_outp = quant(inp)
if not torch.allclose(orig_outp, quant_outp):
print('original:')
print(orig_outp)
print('quanted:')
print(quant_outp)
print('diff (min, max):', torch.max(quant_outp - orig_outp), torch.min(quant_outp - orig_outp))
self.assertTrue(False)
@unittest.skipIf(not hasattr(nn, 'SiLU'), 'nn.SiLU is not available')
def test_silu(self):
for i in range(100):
orig = nn.SiLU()
quant = QSiLU(orig)
inp = (torch.randn((100, 100)) - 0.5) * 2
orig_outp = orig(inp)
quant_outp = quant(inp)
if not torch.allclose(orig_outp, quant_outp):
print('original:')
print(orig_outp)
print('quanted:')
print(quant_outp)
print('diff (min, max):', torch.max(quant_outp - orig_outp), torch.min(quant_outp - orig_outp))
self.assertTrue(False)
def test_glu(self):
for i in range(100):
orig = nn.GLU()
quant = QGLU(orig)
inp = (torch.randn((100, 100)) - 0.5) * 2
orig_outp = orig(inp)
quant_outp = quant(inp)
if not torch.allclose(orig_outp, quant_outp):
print('original:')
print(orig_outp)
print('quanted:')
print(quant_outp)
print('diff (min, max):', torch.max(quant_outp - orig_outp), torch.min(quant_outp - orig_outp))
self.assertTrue(False)
if __name__ == '__main__':
unittest.main()