MCPcopy Create free account
hub / github.com/XiaoMi/mace / run

Method run

tools/python/quantize/quantize_stat.py:28–96  ·  view source on GitHub ↗
(log_file, percentile, enhance, enhance_ratio)

Source from the content-addressed store, hash-verified

26
27 @staticmethod
28 def run(log_file, percentile, enhance, enhance_ratio):
29 res = {}
30 tensor_id = {}
31 idx = 0
32 tensor_ranges = {}
33 with open(log_file) as log:
34 for line in log:
35 if line.find("Tensor range @@") != -1:
36 tensor_name, minmax = line.split("@@")[1:]
37 min_val, max_val = [float(i) for i in
38 minmax.strip().split(",")]
39 if tensor_name not in tensor_ranges:
40 tensor_ranges[tensor_name] = ([], [])
41 tensor_ranges[tensor_name][0].append(min_val)
42 tensor_ranges[tensor_name][1].append(max_val)
43 if tensor_name not in tensor_id:
44 tensor_id[tensor_name] = idx
45 idx = idx + 1
46
47 for tensor_name in tensor_ranges:
48 samples = len(tensor_ranges[tensor_name][0])
49 tensor_min = np.percentile(tensor_ranges[tensor_name][0],
50 percentile)
51 tensor_max = np.percentile(tensor_ranges[tensor_name][1],
52 100 - percentile)
53 assert tensor_min < tensor_max, \
54 "min should be < max, %s min:%f max:%f" % \
55 (tensor_name, tensor_min, tensor_max)
56 if not enhance or samples <= 1:
57 res[tensor_name] = (tensor_min, tensor_max)
58 else:
59 """
60 Enhancement mode:
61 This policy eliminates outliers that cause long-tail
62 statistical range. We try to reduce as much range as it could
63 while retaining more samples. d(range)/d(sample_quantile) is
64 used to measure this qualitatively.
65 """
66 tensor_mins = np.sort(tensor_ranges[tensor_name][0])
67 tensor_maxs = np.sort(tensor_ranges[tensor_name][1])[::-1]
68 cur_min_idx = 0
69 cur_max_idx = 0
70 cur_min = tensor_min
71 cur_max = tensor_max
72 for i in range(samples):
73 if tensor_mins[i] + 0.1 > cur_max:
74 break
75
76 delta_range = (tensor_mins[i] - cur_min) / (cur_max - cur_min) # noqa
77 delta_quantile = float(i - cur_min_idx) / (samples - cur_min_idx) # noqa
78 if delta_quantile > 0 and delta_range / delta_quantile > enhance_ratio: # noqa
79 cur_min_idx = i
80 cur_min = tensor_mins[i]
81
82 if cur_min + 0.1 > tensor_maxs[i]:
83 break
84
85 delta_range = (cur_max - tensor_maxs[i]) / (cur_max - cur_min) # noqa

Callers 1

quantize_stat.pyFile · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected