forked from huismiling/wenet_trt8
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathonnx_qant.py
executable file
·55 lines (45 loc) · 1.55 KB
/
onnx_qant.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
import os
import random
import onnx
import onnxruntime
import numpy as np
import json
from onnxruntime.quantization import CalibrationDataReader
import math
class onnxDataReader(CalibrationDataReader):
def __init__(self,
npData,
batch_size,
run_times=-1
):
self.npData = npData
self.batch_size = batch_size
self.enum_data_dicts = iter([])
self.run_times = run_times
self.total_data_num = 1000
self.current_idx = 0
def get_next(self):
iter_data = next(self.enum_data_dicts, None)
if iter_data:
self.start_of_new_stride= False
self.current_idx += 1
return iter_data
if self.total_data_num < self.current_idx:
return None
self.current_idx += 1
self.enum_data_dicts = None
data = []
for itd in self.npData:
dkeys = list(itd.keys())
num_batch = math.ceil(len(itd[dkeys[0]])/self.batch_size)
for itn in range(num_batch):
ddict = {}
for itk in dkeys:
ddict[itk] = itd[itk][itn*self.batch_size : (itn+1)*self.batch_size]
data.append(ddict)
self.total_data_num = min(self.run_times, len(data)) if self.run_times>0 else len(data)
random.shuffle(data)
data = data[:self.total_data_num]
self.enum_data_dicts = iter(data)
self.start_of_new_stride = True
return next(self.enum_data_dicts, None)