From d67ebbcc04e50ed0c7e9696821d735c3b3c2d536 Mon Sep 17 00:00:00 2001 From: Jonghwa Lee Date: Thu, 28 Nov 2024 10:07:23 +0900 Subject: [PATCH] [tools/mec] Add quantization parameters to tensor information (#14374) If the model is quantized, it will show quantization parameters of each tensors. ONE-DCO-1.0-Signed-off-by: Jonghwa Lee --- .../src/model_explorer_circle/main.py | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/tools/model_explorer_circle/src/model_explorer_circle/main.py b/tools/model_explorer_circle/src/model_explorer_circle/main.py index 976d1e10247..6d0681728d3 100644 --- a/tools/model_explorer_circle/src/model_explorer_circle/main.py +++ b/tools/model_explorer_circle/src/model_explorer_circle/main.py @@ -81,15 +81,27 @@ def add_output_tensor_info(self, tensor_shape = self.dict_tensor_type_to_string[tensor.type].lower() tensor_shape += f'{tensor.shape.tolist()}' tensor_name = tensor.name.decode('utf-8') - me_node.outputsMetadata.append( - graph_builder.MetadataItem( - id=f'{output_id}', - attrs=[ - graph_builder.KeyValue(key='shape', value=tensor_shape), - graph_builder.KeyValue(key='tensor_index', value=f'{tensor_id}'), - graph_builder.KeyValue(key='tensor_name', value=tensor_name) - ], - )) + + metadata = graph_builder.MetadataItem( + id=f'{output_id}', + attrs=[ + graph_builder.KeyValue(key='shape', value=tensor_shape), + graph_builder.KeyValue(key='tensor_index', value=f'{tensor_id}'), + graph_builder.KeyValue(key='tensor_name', value=tensor_name) + ], + ) + + # Quantization parameter (if exists) + if tensor.quantization: + # Show the most significant 6 digits of the scale + scale = format(tensor.quantization.scale[0], '.6g') + zp = tensor.quantization.zeroPoint[0] + # If the type is larger than INT8, exponential notation will be used + quantparam = f'{scale} * (q + {zp})' + metadata.attrs.append( + graph_builder.KeyValue(key='quantization', value=quantparam)) + + me_node.outputsMetadata.append(metadata) def build_graph(self, me_graph: graph_builder.Graph) -> None: """Build the graph using the model."""