Skip to content

Commit

Permalink
[tools/mec] Add quantization parameters to tensor information (#14374)
Browse files Browse the repository at this point in the history
If the model is quantized, it will show quantization parameters of
each tensors.

ONE-DCO-1.0-Signed-off-by: Jonghwa Lee <[email protected]>
  • Loading branch information
batcheu authored Nov 28, 2024
1 parent 26ce24b commit d67ebbc
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions tools/model_explorer_circle/src/model_explorer_circle/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit d67ebbc

Please sign in to comment.