Skip to content

Commit 4d26e3a

Browse files
Add progress bar for woq and sq (#1467)
Co-authored-by: yuwenzho <[email protected]>
1 parent f341da7 commit 4d26e3a

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

neural_compressor/adaptor/ox_utils/calibration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ def calib_smooth(self, percentile, op_types, q_config):
757757
max_vals_per_channel: max values per channel of input tensors
758758
shape_infos: The shape information of input tensors
759759
"""
760+
logger.info("Start smooth model calibration.")
760761
# add the input tensors of {op_types} to outputs of the model
761762
tensors_to_node = self._get_input_tensor_of_ops(op_types)
762763
self.model_wrapper.add_tensors_to_outputs(tensors_to_node.keys())

neural_compressor/adaptor/ox_utils/smooth_quant.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@
2525
from onnx import helper, numpy_helper
2626
from onnx import onnx_pb as onnx_proto
2727

28-
from neural_compressor.adaptor.ox_utils.util import _get_qrange_for_qType, is_B_transposed, quantize_data, to_numpy
28+
from neural_compressor.adaptor.ox_utils.util import (
29+
_get_qrange_for_qType,
30+
is_B_transposed,
31+
quantize_data,
32+
simple_progress_bar,
33+
to_numpy,
34+
)
2935
from neural_compressor.model.model import BaseModel
3036
from neural_compressor.model.onnx_model import ONNXModel
3137

@@ -562,6 +568,7 @@ def _get_smooth_scales(self, alpha, target_list=[]):
562568
Returns:
563569
the smooth scales for weights, currently one input tensor only have one scale
564570
"""
571+
logger.info("Start smooth scales collection.")
565572
scales = {}
566573
for tensor, nodes in self.tensors_to_node.items():
567574
# if scales_per_op the key of scales is the node name, otherwise the activation of node
@@ -673,7 +680,8 @@ def _adjust_weights(self, scales):
673680
Args:
674681
scales (dict): The input scales
675682
"""
676-
for tensor_name, nodes in self.tensors_to_node.items():
683+
for idx, (tensor_name, nodes) in enumerate(self.tensors_to_node.items()):
684+
simple_progress_bar(len(self.tensors_to_node), idx + 1)
677685
for node_info in nodes:
678686
key = node_info[0] if self.scales_per_op else tensor_name
679687
if key not in scales:

neural_compressor/adaptor/ox_utils/util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@
9393
MAXIMUM_PROTOBUF = 2147483648
9494

9595

96+
def simple_progress_bar(total, i):
97+
"""Progress bar for cases where tqdm can't be used."""
98+
progress = i / total
99+
bar_length = 20
100+
bar = "#" * int(bar_length * progress)
101+
spaces = " " * (bar_length - len(bar))
102+
percentage = progress * 100
103+
print(f"\rProgress: [{bar}{spaces}] {percentage:.2f}%", end="")
104+
105+
96106
def dtype_to_name(dtype_mapping, dtype):
97107
"""Map data type and its string representation."""
98108
return list(dtype_mapping.keys())[list(dtype_mapping.values()).index(dtype)]

neural_compressor/adaptor/ox_utils/weight_only.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from onnx import onnx_pb as onnx_proto
3030
from packaging.version import Version
3131

32+
from neural_compressor.adaptor.ox_utils.util import simple_progress_bar
3233
from neural_compressor.model.model import BaseModel
3334
from neural_compressor.model.onnx_model import ONNXModel
3435
from neural_compressor.utils.utility import LazyImport
@@ -320,7 +321,12 @@ def rtn_quantize(
320321
base_dir = os.path.dirname(model.model_path) if model.model_path is not None else ""
321322
new_nodes = []
322323
remove_nodes = []
324+
total_num = len([i for i in model.nodes() if i.op_type in ["MatMul"]])
325+
curr_id = 0
323326
for node in model.nodes():
327+
if node.op_type in ["MatMul"]:
328+
curr_id += 1
329+
simple_progress_bar(total_num, curr_id)
324330
if (
325331
node.op_type in ["MatMul"]
326332
and model.get_initializer(node.input[1]) is not None
@@ -1025,8 +1031,8 @@ def gptq_quantize(
10251031

10261032
new_nodes = []
10271033
remove_nodes = []
1028-
1029-
for input_name in output_names:
1034+
for idx, input_name in enumerate(output_names):
1035+
simple_progress_bar(len(output_names), idx + 1)
10301036
node_list = []
10311037
weights = []
10321038

0 commit comments

Comments
 (0)