Skip to content

Commit

Permalink
[tools/mec] Move quantization parameters to input metadata (#14483)
Browse files Browse the repository at this point in the history
It moves quantization parameters to input metadata.

ONE-DCO-1.0-Signed-off-by: Jonghwa Lee <[email protected]>
  • Loading branch information
batcheu authored Dec 19, 2024
1 parent d657636 commit 82e7d9a
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions tools/model_explorer_circle/src/model_explorer_circle/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def add_incoming_edge(self, me_node: graph_builder.GraphNode, tensor_id: int,
graph_builder.IncomingEdge(sourceNodeId=sid,
sourceNodeOutputId=soid,
targetNodeInputId=f'{input_id}'))

metadata = graph_builder.MetadataItem(id=f'{input_id}')
# Add input metadata of the node if it exists
if self.input_args.get(me_node.label) is not None:
arity = len(self.input_args[me_node.label])
Expand All @@ -91,11 +93,27 @@ def add_incoming_edge(self, me_node: graph_builder.GraphNode, tensor_id: int,
if input_id == arity:
me_node.inputsMetadata[-1].attrs[0].value = arg_name + '[0]'
arg_name += f'[{input_id - arity + 1}]'
metadata.attrs.append(
graph_builder.KeyValue(key='__tensor_tag', value=arg_name))

me_node.inputsMetadata.append(
graph_builder.MetadataItem(
id=f'{input_id}',
attrs=[graph_builder.KeyValue(key='__tensor_tag', value=arg_name)]))
# Quantization parameter (if exists)
tensor = self.model.subgraphs[0].tensors[tensor_id]
if tensor.quantization and tensor.quantization.scale is not None:
quantparam = '['
for i, scale in enumerate(tensor.quantization.scale):
if i != 0:
quantparam += ', '
# Show the most significant 6 digits of the scale
scale = format(scale, '.6g')
zp = tensor.quantization.zeroPoint[i]
# If the type is larger than INT8, exponential notation will be used
quantparam += f'{scale} * (q + {zp})'
quantparam += ']'
metadata.attrs.append(
graph_builder.KeyValue(key='quantization', value=quantparam))

if len(metadata.attrs) > 0:
me_node.inputsMetadata.append(metadata)

def add_tensor_value_attribute(self, me_node: graph_builder.GraphNode,
tensor_id: int) -> None:
Expand Down Expand Up @@ -170,21 +188,6 @@ def add_output_tensor_info(self,
],
)

# Quantization parameter (if exists)
if tensor.quantization and tensor.quantization.scale is not None:
quantparam = '['
for i, scale in enumerate(tensor.quantization.scale):
if i != 0:
quantparam += ', '
# Show the most significant 6 digits of the scale
scale = format(scale, '.6g')
zp = tensor.quantization.zeroPoint[i]
# If the type is larger than INT8, exponential notation will be used
quantparam += f'{scale} * (q + {zp})'
quantparam += ']'
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:
Expand Down

0 comments on commit 82e7d9a

Please sign in to comment.