Skip to content

Commit

Permalink
[NNCF][BC] Add test case with depthwise/transpose convolutions to tem…
Browse files Browse the repository at this point in the history
…plate and backend specific tests (#3004)

### Changes

- Added simple Depthwise and Transpose Convolution models in
`tests/cross_fw/test_templates/helpers.py`.

- Updated `map_references` for Torch FX backend to assign the right
reference node names for model classes Depthwise and Transpose
Convolutions.

- Added `ONNXConvolutionTransposeMetatype` into the list of
OPERATIONS_WITH_BIAS for ONNX.

- Added the missing target for Transpose Conv in `transformations.py`
for Torch FX in the function `_is_conv`.

- Added `OVConvolutionBackpropDataMetatype` into the list of
OPERATIONS_WITH_BIAS for OpenVino backend

- Replaced the unet graph in quantized reference graphs for FX backend.

### Extra Changes

- [X] Update and finalize the right changes to make in
`tests/openvino/native/test_bias_correction.py` for Transpose
Convolution node name and accommodate for the changes in the name after
each run.

### Closes issue

#2916

---------

Co-authored-by: dlyakhov <[email protected]>
  • Loading branch information
rk119 and daniil-lyakhov authored Oct 16, 2024
1 parent cb0fe0d commit 17f799e
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 59 deletions.
1 change: 1 addition & 0 deletions nncf/experimental/torch/fx/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ def _is_conv(n: torch.fx.Node):
return n.op == "call_function" and n.target in (
torch.ops.aten.conv1d.default,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv_transpose2d.input,
)


Expand Down
6 changes: 5 additions & 1 deletion nncf/onnx/graph/metatypes/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@
# TODO: Need to add MatMul with the separate bias support (CVS-135433)
]

OPERATIONS_WITH_BIAS = [*OPERATIONS_WITH_BIAS_REDUCED, onnx_metatypes.ONNXDepthwiseConvolutionMetatype]
OPERATIONS_WITH_BIAS = [
*OPERATIONS_WITH_BIAS_REDUCED,
onnx_metatypes.ONNXDepthwiseConvolutionMetatype,
onnx_metatypes.ONNXConvolutionTransposeMetatype,
]


QUANTIZE_DEQUANTIZE_OPERATIONS = [
Expand Down
6 changes: 5 additions & 1 deletion nncf/openvino/graph/metatypes/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@
ov_metatypes.OVMatMulMetatype,
]

OPERATIONS_WITH_BIAS = [*OPERATIONS_WITH_BIAS_REDUCED, ov_metatypes.OVDepthwiseConvolutionMetatype]
OPERATIONS_WITH_BIAS = [
*OPERATIONS_WITH_BIAS_REDUCED,
ov_metatypes.OVDepthwiseConvolutionMetatype,
ov_metatypes.OVConvolutionBackpropDataMetatype,
]

CONV_OPERATIONS = [
ov_metatypes.OVConvolutionMetatype,
Expand Down
30 changes: 30 additions & 0 deletions tests/cross_fw/test_templates/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from nncf import Dataset
from tests.torch.helpers import create_bn
from tests.torch.helpers import create_conv
from tests.torch.helpers import create_depthwise_conv
from tests.torch.helpers import create_transpose_conv
from tests.torch.helpers import set_torch_seed

TTensor = TypeVar("TTensor")
Expand Down Expand Up @@ -436,3 +438,31 @@ def __init__(self):

def forward(self, query, key, value):
return nn.functional.scaled_dot_product_attention(query, key, value)


class DepthwiseConvTestModel(nn.Module):
INPUT_SIZE = [1, 2, 4, 4]

def __init__(self):
super().__init__()
with set_torch_seed():
self.conv = create_depthwise_conv(2, 1, 1, 1)
self.conv.weight.data = torch.randn([2, 1, 1, 1])
self.conv.bias.data = torch.randn([2])

def forward(self, x):
return self.conv(x)


class TransposeConvTestModel(nn.Module):
INPUT_SIZE = [1, 1, 3, 3]

def __init__(self):
super().__init__()
with set_torch_seed():
self.conv = create_transpose_conv(1, 2, 2, 1, 1, 2)
self.conv.weight.data = torch.randn([1, 2, 2, 2])
self.conv.bias.data = torch.randn([2])

def forward(self, x):
return self.conv(x)
4 changes: 4 additions & 0 deletions tests/cross_fw/test_templates/test_bias_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
from nncf.quantization.algorithms.bias_correction.backend import BiasCorrectionAlgoBackend
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from tests.cross_fw.test_templates.helpers import ConvTestModel
from tests.cross_fw.test_templates.helpers import DepthwiseConvTestModel
from tests.cross_fw.test_templates.helpers import MultipleConvTestModel
from tests.cross_fw.test_templates.helpers import SplittedModel
from tests.cross_fw.test_templates.helpers import StaticDatasetMock
from tests.cross_fw.test_templates.helpers import TransposeConvTestModel

TModel = TypeVar("TModel")
TTensor = TypeVar("TTensor")
Expand Down Expand Up @@ -139,6 +141,8 @@ def quantized_test_model(self, tmpdir) -> TModel:
},
),
(ConvTestModel, {"/conv/Conv": [0.11085186, 1.0017344]}),
(DepthwiseConvTestModel, {"/conv/Conv": [-1.1229, -0.1863]}),
(TransposeConvTestModel, {"/conv/ConvTranspose": [0.66797173, -0.7070703]}),
),
)
def test_update_bias(self, model_cls, ref_biases, tmpdir):
Expand Down
4 changes: 4 additions & 0 deletions tests/onnx/quantization/test_bias_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
from nncf.onnx.graph.node_utils import get_bias_value
from nncf.quantization.algorithms.bias_correction.onnx_backend import ONNXBiasCorrectionAlgoBackend
from tests.cross_fw.test_templates.helpers import ConvTestModel
from tests.cross_fw.test_templates.helpers import DepthwiseConvTestModel
from tests.cross_fw.test_templates.helpers import MultipleConvTestModel
from tests.cross_fw.test_templates.helpers import SplittedModel
from tests.cross_fw.test_templates.helpers import TransposeConvTestModel
from tests.cross_fw.test_templates.test_bias_correction import TemplateTestBCAlgorithm
from tests.onnx.quantization.common import compare_nncf_graph

Expand Down Expand Up @@ -211,6 +213,8 @@ def test__get_subgraph_data_for_node(self, quantized_test_model, layer_name, ref
},
),
(ConvTestModel, {("/conv/Conv", 0): ("nncf_model_input_0", 0)}),
(DepthwiseConvTestModel, {("/conv/Conv", 0): ("nncf_model_input_0", 0)}),
(TransposeConvTestModel, {("/conv/ConvTranspose", 0): ("nncf_model_input_0", 0)}),
),
)
def test_verify_collected_stat_inputs_map(self, model_cls, ref_stat_inputs_map, tmpdir):
Expand Down
10 changes: 10 additions & 0 deletions tests/openvino/native/test_bias_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
from nncf.openvino.graph.node_utils import get_bias_value
from nncf.quantization.algorithms.bias_correction.openvino_backend import OVBiasCorrectionAlgoBackend
from tests.cross_fw.test_templates.helpers import ConvTestModel
from tests.cross_fw.test_templates.helpers import DepthwiseConvTestModel
from tests.cross_fw.test_templates.helpers import MultipleConvTestModel
from tests.cross_fw.test_templates.helpers import SplittedModel
from tests.cross_fw.test_templates.helpers import TransposeConvTestModel
from tests.cross_fw.test_templates.test_bias_correction import TemplateTestBCAlgorithm
from tests.openvino.native.common import compare_nncf_graphs


class TestOVBCAlgorithm(TemplateTestBCAlgorithm):
TRANSPOSE_CONV_NAME = "/conv/ConvTranspose/WithoutBiases"

@staticmethod
def list_to_backend_type(data: List) -> np.ndarray:
return np.array(data)
Expand All @@ -42,6 +46,10 @@ def backend_specific_model(model: torch.nn.Module, tmp_dir: str):
onnx_path = f"{tmp_dir}/model.onnx"
torch.onnx.export(model, torch.rand(model.INPUT_SIZE), onnx_path, opset_version=13, input_names=["input.1"])
ov_model = ov.convert_model(onnx_path)
if isinstance(model, TransposeConvTestModel):
for node in ov_model.get_ops():
if node.get_type_name() == "ConvolutionBackpropData":
node.set_friendly_name(TestOVBCAlgorithm.TRANSPOSE_CONV_NAME)
return ov_model

@staticmethod
Expand Down Expand Up @@ -206,6 +214,8 @@ def test__get_subgraph_data_for_node(self, quantized_test_model, layer_name, ref
},
),
(ConvTestModel, {("/conv/Conv/WithoutBiases", 0): ("input.1", 0)}),
(DepthwiseConvTestModel, {("/conv/Conv/WithoutBiases", 0): ("input.1", 0)}),
(TransposeConvTestModel, {(TRANSPOSE_CONV_NAME, 0): ("input.1", 0)}),
),
)
def test_verify_collected_stat_inputs_map(self, model_cls, ref_stat_inputs_map, tmpdir):
Expand Down
Loading

0 comments on commit 17f799e

Please sign in to comment.