forked from alibaba/TinyNeuralNetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qat_module_test.py
53 lines (37 loc) · 1.48 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
import unittest
import torch
import torch.nn as nn
from tinynn.graph.quantization.modules import QPReLU
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)
if __name__ == '__main__':
unittest.main(failfast=True)