1 # Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 '''Test visqlib.QErrorComputer module'''
22 from visqlib.QErrorComputer import MPEIRComputer
23 from visqlib.QErrorComputer import MSEComputer
24 from visqlib.QErrorComputer import TAEComputer
25 from visqlib.QErrorComputer import SRMSEComputer
28 class VisqQErrorComputerTest(unittest.TestCase):
30 "Called before running each test"
31 self.fp32_dir = tempfile.TemporaryDirectory()
32 self.fq_dir = tempfile.TemporaryDirectory()
35 "Called after running each test"
36 self.fp32_dir.cleanup()
39 def _setUpSingleTensorData(self):
42 with open(self.fp32_dir.name + '/tensors.json', 'w') as f:
43 json.dump(tensor_id, f)
44 with open(self.fq_dir.name + '/tensors.json', 'w') as f:
45 json.dump(tensor_id, f)
48 with open(self.fq_dir.name + '/scales.txt', 'w') as f:
50 os.mkdir(self.fp32_dir.name + '/0')
51 os.mkdir(self.fq_dir.name + '/0')
52 test_data = np.zeros(16)
53 np.save(self.fp32_dir.name + '/0/0.npy', test_data)
54 np.save(self.fq_dir.name + '/0/0.npy', test_data)
56 def _setUpTwoTensorData(self):
59 with open(self.fp32_dir.name + '/tensors.json', 'w') as f:
60 json.dump(tensor_id, f)
61 with open(self.fq_dir.name + '/tensors.json', 'w') as f:
62 json.dump(tensor_id, f)
65 with open(self.fq_dir.name + '/scales.txt', 'w') as f:
67 os.mkdir(self.fp32_dir.name + '/0')
68 os.mkdir(self.fp32_dir.name + '/1')
69 os.mkdir(self.fq_dir.name + '/0')
70 os.mkdir(self.fq_dir.name + '/1')
71 test_data_one = np.ones(16)
72 test_data_zero = np.zeros(16)
73 np.save(self.fp32_dir.name + '/0/0.npy', test_data_one)
74 np.save(self.fp32_dir.name + '/1/0.npy', test_data_zero)
75 np.save(self.fq_dir.name + '/0/0.npy', test_data_zero)
76 np.save(self.fq_dir.name + '/1/0.npy', test_data_zero)
77 # Golden: (1 + 0) / 2 = 0.5 for MSE
79 def _setUpDifferentTensorData(self):
80 # Two fp32 data (test, test2)
82 # NOTE When does this happen?
83 # This case can happen because visq ignores nodes that do not affect qerrors.
84 # For example, RESHAPE Op does not affect qerrors, so its fq data is not dumped,
85 # although it is listed in 'tensors.json'.
88 tensor_id['test2'] = 1
89 with open(self.fp32_dir.name + '/tensors.json', 'w') as f:
90 json.dump(tensor_id, f)
91 with open(self.fq_dir.name + '/tensors.json', 'w') as f:
92 json.dump(tensor_id, f)
96 with open(self.fq_dir.name + '/scales.txt', 'w') as f:
98 os.mkdir(self.fp32_dir.name + '/0')
99 os.mkdir(self.fq_dir.name + '/0')
100 test_data = np.zeros(16)
101 np.save(self.fp32_dir.name + '/0/0.npy', test_data)
102 np.save(self.fp32_dir.name + '/0/1.npy', test_data)
103 np.save(self.fq_dir.name + '/0/0.npy', test_data)
105 def test_MPEIR(self):
106 self._setUpSingleTensorData()
108 computer = MPEIRComputer(self.fp32_dir.name, self.fq_dir.name)
109 qmap, _, _ = computer.run()
110 self.assertAlmostEqual(0.0, qmap['test'])
112 def test_MPEIR_different_tensors(self):
113 self._setUpDifferentTensorData()
115 computer = MPEIRComputer(self.fp32_dir.name, self.fq_dir.name)
116 qmap, _, _ = computer.run()
117 self.assertAlmostEqual(0.0, qmap['test'])
120 self._setUpSingleTensorData()
122 computer = MSEComputer(self.fp32_dir.name, self.fq_dir.name)
123 qmap, qmin, qmax = computer.run()
124 self.assertAlmostEqual(0.0, qmap['test'])
125 self.assertAlmostEqual(0.0, qmin)
126 self.assertAlmostEqual(0.0, qmax)
128 def test_MSE_two(self):
129 self._setUpTwoTensorData()
131 computer = MSEComputer(self.fp32_dir.name, self.fq_dir.name)
132 qmap, qmin, qmax = computer.run()
133 self.assertAlmostEqual(0.5, qmap['test'])
134 self.assertAlmostEqual(0.0, qmin)
135 self.assertAlmostEqual(1.0, qmax)
137 def test_MSE_different_tensors(self):
138 self._setUpDifferentTensorData()
140 computer = MSEComputer(self.fp32_dir.name, self.fq_dir.name)
141 qmap, qmin, qmax = computer.run()
142 self.assertAlmostEqual(0.0, qmap['test'])
143 self.assertAlmostEqual(0.0, qmin)
144 self.assertAlmostEqual(0.0, qmax)
147 self._setUpSingleTensorData()
149 computer = TAEComputer(self.fp32_dir.name, self.fq_dir.name)
150 qmap, qmin, qmax = computer.run()
151 self.assertAlmostEqual(0.0, qmap['test'])
153 def test_TAE_different_options(self):
154 self._setUpDifferentTensorData()
156 computer = TAEComputer(self.fp32_dir.name, self.fq_dir.name)
157 qmap, qmin, qmax = computer.run()
158 self.assertAlmostEqual(0.0, qmap['test'])
159 self.assertAlmostEqual(0.0, qmin)
160 self.assertAlmostEqual(0.0, qmax)
162 def test_TAE_two(self):
163 self._setUpTwoTensorData()
164 computer = TAEComputer(self.fp32_dir.name, self.fq_dir.name)
165 qmap, qmin, qmax = computer.run()
166 self.assertAlmostEqual(0.0, qmin)
167 self.assertAlmostEqual(8.0, qmap['test'])
168 self.assertAlmostEqual(16.0, qmax)
170 def test_SRMSE(self):
171 self._setUpSingleTensorData()
173 computer = SRMSEComputer(self.fp32_dir.name, self.fq_dir.name)
174 qmap, qmin, qmax = computer.run()
175 self.assertAlmostEqual(0.0, qmap['test'])
176 self.assertAlmostEqual(0.0, qmin)
177 self.assertAlmostEqual(0.0, qmax)
179 def test_SRMSE_different_options(self):
180 self._setUpDifferentTensorData()
182 computer = SRMSEComputer(self.fp32_dir.name, self.fq_dir.name)
183 qmap, qmin, qmax = computer.run()
184 self.assertAlmostEqual(0.0, qmap['test'])
185 self.assertAlmostEqual(0.0, qmin)
186 self.assertAlmostEqual(0.0, qmax)
188 def test_SRMSE_two(self):
189 self._setUpTwoTensorData()
190 computer = SRMSEComputer(self.fp32_dir.name, self.fq_dir.name)
191 qmap, qmin, qmax = computer.run()
192 # Golden: sqrt(Golden of MSE) / scale = sqrt(0.5) / 2
193 self.assertAlmostEqual(np.sqrt(0.5) / 2, qmap['test'])
194 self.assertAlmostEqual(0.0, qmin)
195 self.assertAlmostEqual(np.sqrt(0.5) / 2, qmax)
198 if __name__ == '__main__':