Skip to content

Commit

Permalink
[ONNX] ONNX FBC/BC metatypes groups (#2599)
Browse files Browse the repository at this point in the history
### Changes

Update metatypes list for FBC/BC
Fix bug with determing depthwise conv.

### Reason for changes

Red PTQ conformance

### Related tickets

N/A

### Tests

Extend test_metatypes by quantized models
  • Loading branch information
kshpv authored Mar 26, 2024
1 parent b7ba5ad commit b6e1aa9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
4 changes: 3 additions & 1 deletion nncf/onnx/graph/metatypes/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@


# Contains the operation metatypes for which bias can be applied.
OPERATIONS_WITH_BIAS = [
OPERATIONS_WITH_BIAS_REDUCED = [
onnx_metatypes.ONNXConvolutionMetatype,
onnx_metatypes.ONNXGemmMetatype,
# TODO: Need to add MatMul with the separate bias support (CVS-135433)
]

OPERATIONS_WITH_BIAS = [*OPERATIONS_WITH_BIAS_REDUCED, onnx_metatypes.ONNXDepthwiseConvolutionMetatype]


QUANTIZE_DEQUANTIZE_OPERATIONS = [
onnx_metatypes.ONNXQuantizeLinearMetatype,
Expand Down
2 changes: 1 addition & 1 deletion nncf/onnx/graph/metatypes/onnx_metatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def _is_depthwise_conv(model: onnx.ModelProto, node: onnx.NodeProto) -> bool:
if attribute.name == "group":
conv_group = onnx.helper.get_attribute_value(attribute)
weight_tensor_value = None
initializer_name = node.input[1]
initializer_name = get_tensor_edge_name(model, node, 1, get_parents_node_mapping(model))
for init in model.graph.initializer:
if init.name == initializer_name:
weight_tensor_value = onnx.numpy_helper.to_array(init)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from nncf.common.graph.transformations.commands import TargetType
from nncf.experimental.common.tensor_statistics.collectors import TensorCollector
from nncf.experimental.tensor import Tensor
from nncf.onnx.graph.metatypes.groups import OPERATIONS_WITH_BIAS_REDUCED
from nncf.onnx.graph.node_utils import get_bias_value
from nncf.onnx.graph.node_utils import is_any_weight_quantized
from nncf.onnx.graph.node_utils import is_node_with_bias
Expand Down Expand Up @@ -89,7 +90,7 @@ def is_quantized_weights(node: NNCFNode, nncf_graph: NNCFGraph) -> bool:

@staticmethod
def is_node_with_bias(node: NNCFNode, nncf_graph: NNCFGraph) -> bool:
return is_node_with_bias(node)
return is_node_with_bias(node) and node.metatype in OPERATIONS_WITH_BIAS_REDUCED

@staticmethod
def get_node_names_for_input_output_statistics(node: NNCFNode, nncf_graph: NNCFGraph) -> Tuple[str, str]:
Expand Down
49 changes: 43 additions & 6 deletions tests/onnx/test_metatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXConstantOfShapeMetatype
from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXConvolutionMetatype
from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXDepthwiseConvolutionMetatype
from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXDequantizeLinearMetatype
from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXGlobalAveragePoolMetatype
from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXQuantizeLinearMetatype
from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXReluMetatype
from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXShapeMetatype
from nncf.onnx.graph.nncf_graph_builder import GraphConverter
from tests.onnx.models import LinearModel
from tests.onnx.models import ModelWithIntEdges
from tests.onnx.models import MultiInputOutputModel
from tests.onnx.models import OneDepthwiseConvolutionalModel
from tests.onnx.quantization.common import min_max_quantize_model

TEST_MODELS = [LinearModel, MultiInputOutputModel, ModelWithIntEdges, OneDepthwiseConvolutionalModel]
REF_METATYPES_COUNTERS = [
Expand All @@ -52,11 +55,45 @@
[InputNoopMetatype, ONNXConstantOfShapeMetatype, ONNXShapeMetatype, OutputNoopMetatype],
[InputNoopMetatype, ONNXDepthwiseConvolutionMetatype, OutputNoopMetatype],
]
QUANTIZED_REF_METATYPES_COUNTERS = [
REF_METATYPES_COUNTERS[0]
+ [
ONNXQuantizeLinearMetatype,
ONNXDequantizeLinearMetatype,
]
* 5,
REF_METATYPES_COUNTERS[1]
+ [
ONNXQuantizeLinearMetatype,
ONNXDequantizeLinearMetatype,
]
* 2,
REF_METATYPES_COUNTERS[2]
+ [
ONNXQuantizeLinearMetatype,
ONNXDequantizeLinearMetatype,
]
* 0,
REF_METATYPES_COUNTERS[3]
+ [
ONNXQuantizeLinearMetatype,
ONNXDequantizeLinearMetatype,
]
* 2,
]


@pytest.mark.parametrize(
("model_creator_func, ref_metatypes, q_ref_metatypes"),
zip(TEST_MODELS, REF_METATYPES_COUNTERS, QUANTIZED_REF_METATYPES_COUNTERS),
)
def test_mapping_onnx_metatypes(model_creator_func, ref_metatypes, q_ref_metatypes):
def _check_metatypes(model, ref_metatypes):
nncf_graph = GraphConverter.create_nncf_graph(model)
actual_metatypes = [node.metatype for node in nncf_graph.get_all_nodes()]
assert Counter(ref_metatypes) == Counter(actual_metatypes)

@pytest.mark.parametrize(("model_creator_func, ref_metatypes"), zip(TEST_MODELS, REF_METATYPES_COUNTERS))
def test_mapping_onnx_metatypes(model_creator_func, ref_metatypes):
model = model_creator_func()
nncf_graph = GraphConverter.create_nncf_graph(model.onnx_model)
actual_metatypes = [node.metatype for node in nncf_graph.get_all_nodes()]
assert Counter(ref_metatypes) == Counter(actual_metatypes)
model = model_creator_func().onnx_model
q_model = min_max_quantize_model(model)
_check_metatypes(model, ref_metatypes)
_check_metatypes(q_model, q_ref_metatypes)

0 comments on commit b6e1aa9

Please sign in to comment.