MCPcopy
hub / github.com/OpenPPL/ppq / MeasurePrinter

Class MeasurePrinter

ppq/quantization/analyse/util/__init__.py:49–96  ·  view source on GitHub ↗

Helper class for print top-k record.

Source from the content-addressed store, hash-verified

47 self.num_of_elements += elements
48
49class MeasurePrinter():
50 """Helper class for print top-k record."""
51 def __init__(self, data: Dict[str, float], measure: str, label: str = 'Layer',
52 k: int = None, order: str = 'large_to_small',
53 percentage: bool = False) -> None:
54
55 if order not in {'large_to_small', 'small_to_large', None}:
56 raise ValueError('Parameter "order" can only be "large_to_small" or "small_to_large"')
57 self.collection = [(name, value) for name, value in data.items()]
58 if order is not None:
59 self.collection = sorted(self.collection, key=lambda x: x[1])
60 if order == 'large_to_small': self.collection = self.collection[::-1]
61 if k is not None: self.collection = self.collection[:k]
62
63 if order is None:
64 sorted_collection = sorted(self.collection, key=lambda x: x[1])
65 largest_element, smallest_element = sorted_collection[-1][1], sorted_collection[0][1]
66 elif order == 'large_to_small':
67 largest_element, smallest_element = self.collection[0][1], self.collection[-1][1]
68 else: largest_element, smallest_element = self.collection[-1][1], self.collection[0][1]
69 self.normalized_by = largest_element - smallest_element
70 self.min = smallest_element
71
72 max_name_length = len(label)
73 for name, _ in self.collection:
74 max_name_length = max(len(name), max_name_length)
75 self.max_name_length = max_name_length
76 self.measure_str = measure
77 self.label = label
78 self.percentage = percentage
79
80 def print(self, max_blocks: int = 20):
81 print(f'{self.label}{" " * (self.max_name_length - len(self.label))} | {self.measure_str} ')
82 for name, value in self.collection:
83 normalized_value = (value - self.min) / (self.normalized_by + 1e-7)
84 if math.isnan(value):
85 ppq_warning('MeasurePrinter found an NaN value in your data.')
86 normalized_value = 0
87 num_of_blocks = round(normalized_value * max_blocks)
88
89 if not self.percentage:
90 print(f'{name}:{" " * (self.max_name_length - len(name))} | '
91 f'{"█" * num_of_blocks}{" " * (max_blocks - num_of_blocks)} | '
92 f'{value:.4f}')
93 else:
94 print(f'{name}:{" " * (self.max_name_length - len(name))} | '
95 f'{"█" * num_of_blocks}{" " * (max_blocks - num_of_blocks)} | '
96 f'{value * 100:.3f}%')

Callers 5

reportMethod · 0.90
layerwise_error_analyseFunction · 0.85
parameter_analyseFunction · 0.85
graphwise_error_analyseFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected