Skip to content

Commit 6c4b227

Browse files
[ruff] INP rule, extend install tests (#3244)
### Changes - Enabled INP rule for ruff https://docs.astral.sh/ruff/rules/#flake8-no-pep420-inp - Added missed `__init__.py` files - Extended install tests for torch and tf to load modules - Renamed openvino_adapter.py → torch_ov_adapter.py and openvino_quantizer.py → torch_ov_quantizer.py to follow naming rule - Removed import openvino in torch backend files ### Reason for changes Some files are not added to package ### Tests https://github.com/openvinotoolkit/nncf/actions/runs/13137591065
1 parent 7ed1617 commit 6c4b227

24 files changed

+111
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Intel Corporation
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.

nncf/experimental/quantization/algorithms/post_training/algorithm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from nncf.common.tensor_statistics.statistic_point import StatisticPointsContainer
1818
from nncf.common.utils.backend import BackendType
1919
from nncf.experimental.quantization.algorithms.post_training.pipeline import experimental_create_ptq_pipeline
20-
from nncf.experimental.quantization.quantizers.quantizer import Quantizer
20+
from nncf.experimental.quantization.quantizer import Quantizer
2121
from nncf.quantization.advanced_parameters import AdvancedBiasCorrectionParameters
2222
from nncf.quantization.advanced_parameters import AdvancedSmoothQuantParameters
2323
from nncf.quantization.advanced_parameters import RangeEstimatorParameters

nncf/experimental/quantization/algorithms/post_training/pipeline.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from typing import Optional, TypeVar
1313

1414
from nncf.experimental.quantization.algorithms.range_estimator.algorithm import MinMaxRangeEstimator
15-
from nncf.experimental.quantization.quantizers.quantizer import Quantizer
15+
from nncf.experimental.quantization.quantizer import Quantizer
1616
from nncf.quantization.advanced_parameters import AdvancedBiasCorrectionParameters
1717
from nncf.quantization.advanced_parameters import AdvancedSmoothQuantParameters
1818
from nncf.quantization.advanced_parameters import RangeEstimatorParameters
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Intel Corporation
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.

nncf/experimental/quantization/algorithms/range_estimator/algorithm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from nncf.common.graph.graph import NNCFGraph
1616
from nncf.common.tensor_statistics.statistic_point import StatisticPointsContainer
1717
from nncf.common.utils.backend import BackendType
18-
from nncf.experimental.quantization.quantizers.quantizer import Quantizer
18+
from nncf.experimental.quantization.quantizer import Quantizer
1919
from nncf.quantization.algorithms.algorithm import Algorithm
2020
from nncf.quantization.algorithms.min_max.algorithm import MinMaxQuantization
2121
from nncf.quantization.range_estimator import RangeEstimatorParameters

nncf/experimental/torch/fx/quantization/quantize_pt2e.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
from nncf.common.logging import nncf_logger
2828
from nncf.data import Dataset
2929
from nncf.experimental.quantization.algorithms.post_training.algorithm import ExperimentalPostTrainingQuantization
30-
from nncf.experimental.quantization.quantizers.openvino_adapter import OpenVINOQuantizerAdapter
31-
from nncf.experimental.quantization.quantizers.openvino_quantizer import OpenVINOQuantizer
32-
from nncf.experimental.quantization.quantizers.torch_ao_adapter import TorchAOQuantizerAdapter
3330
from nncf.experimental.torch.fx.constant_folding import constant_fold
31+
from nncf.experimental.torch.fx.quantization.quantizer.openvino_adapter import OpenVINOQuantizerAdapter
32+
from nncf.experimental.torch.fx.quantization.quantizer.openvino_quantizer import OpenVINOQuantizer
33+
from nncf.experimental.torch.fx.quantization.quantizer.torch_ao_adapter import TorchAOQuantizerAdapter
3434
from nncf.experimental.torch.fx.transformations import QUANTIZE_NODE_TARGETS
3535
from nncf.experimental.torch.fx.transformations import compress_post_quantize_transformation
3636
from nncf.quantization.advanced_parameters import AdvancedBiasCorrectionParameters
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Intel Corporation
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.

nncf/experimental/quantization/quantizers/openvino_adapter.py nncf/experimental/torch/fx/quantization/quantizer/openvino_adapter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
from nncf.common.graph.graph import NNCFGraph
1515
from nncf.common.quantization.quantizer_setup import SingleConfigQuantizerSetup
16-
from nncf.experimental.quantization.quantizers.openvino_quantizer import OpenVINOQuantizer
17-
from nncf.experimental.quantization.quantizers.quantizer import Quantizer
16+
from nncf.experimental.quantization.quantizer import Quantizer
17+
from nncf.experimental.torch.fx.quantization.quantizer.openvino_quantizer import OpenVINOQuantizer
1818

1919

2020
class OpenVINOQuantizerAdapter(Quantizer):

nncf/experimental/quantization/quantizers/torch_ao_adapter.py nncf/experimental/torch/fx/quantization/quantizer/torch_ao_adapter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from nncf.common.quantization.quantizer_setup import WeightQuantizationInsertionPoint
3131
from nncf.common.quantization.structs import QuantizationScheme as QuantizationMode
3232
from nncf.common.quantization.structs import QuantizerConfig
33-
from nncf.experimental.quantization.quantizers.quantizer import Quantizer
33+
from nncf.experimental.quantization.quantizer import Quantizer
3434
from nncf.experimental.torch.fx.nncf_graph_builder import GraphConverter
3535

3636
EdgeOrNode = Union[Tuple[torch.fx.Node, torch.fx.Node]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Intel Corporation
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Intel Corporation
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.

nncf/quantization/algorithms/smooth_quant/torch_backend.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@
2424
from nncf.experimental.common.tensor_statistics.collectors import AbsMaxReducer
2525
from nncf.experimental.common.tensor_statistics.collectors import MaxAggregator
2626
from nncf.experimental.common.tensor_statistics.collectors import TensorCollector
27-
from nncf.openvino.graph.transformations.commands import OVMultiplyInsertionCommand
28-
from nncf.openvino.graph.transformations.commands import OVWeightUpdateCommand
2927
from nncf.quantization.algorithms.smooth_quant.backend import SmoothQuantAlgoBackend
3028
from nncf.tensor import Tensor
3129
from nncf.torch.graph.transformations.command_creation import create_command_to_update_weight
3230
from nncf.torch.graph.transformations.commands import PTSharedFnInsertionCommand
3331
from nncf.torch.graph.transformations.commands import PTTargetPoint
32+
from nncf.torch.graph.transformations.commands import PTWeightUpdateCommand
3433
from nncf.torch.layer_utils import COMPRESSION_MODULES
3534
from nncf.torch.layer_utils import CompressionParameter
3635
from nncf.torch.layer_utils import StatefullModuleInterface
@@ -128,7 +127,7 @@ def get_weight_value(node_with_weight: NNCFNode, model: NNCFNetwork, nncf_graph:
128127
return Tensor(weight_data)
129128

130129
@staticmethod
131-
def weight_update_command(node_with_weight: NNCFNode, weight_value: np.ndarray) -> OVWeightUpdateCommand:
130+
def weight_update_command(node_with_weight: NNCFNode, weight_value: np.ndarray) -> PTWeightUpdateCommand:
132131
return create_command_to_update_weight(node_with_weight, weight_value)
133132

134133
@staticmethod
@@ -138,7 +137,7 @@ def scale_insertion_command(
138137
source_output_port_id: int,
139138
nodes: List[NNCFNode],
140139
scale_node_name: str,
141-
) -> OVMultiplyInsertionCommand:
140+
) -> PTSharedFnInsertionCommand:
142141
input_port_id = 0
143142
target_points = []
144143
for node in nodes:

nncf/quantization/algorithms/smooth_quant/torch_fx_backend.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
from nncf.experimental.torch.fx.transformations import constant_update_transformation_builder
2929
from nncf.experimental.torch.fx.transformations import get_graph_node_by_name
3030
from nncf.experimental.torch.fx.transformations import module_insertion_transformation_builder
31-
from nncf.openvino.graph.transformations.commands import OVMultiplyInsertionCommand
32-
from nncf.openvino.graph.transformations.commands import OVWeightUpdateCommand
3331
from nncf.quantization.algorithms.smooth_quant.backend import SmoothQuantAlgoBackend
3432
from nncf.tensor import Tensor
3533
from nncf.torch.graph.transformations.commands import PTTargetPoint
@@ -106,7 +104,7 @@ def get_weight_value(node_with_weight: NNCFNode, model: torch.fx.GraphModule, nn
106104
return Tensor(weight_data.data)
107105

108106
@staticmethod
109-
def weight_update_command(node_with_weight: NNCFNode, weight_value: torch.Tensor) -> OVWeightUpdateCommand:
107+
def weight_update_command(node_with_weight: NNCFNode, weight_value: torch.Tensor) -> FXApplyTransformationCommand:
110108
# TODO(dlyakhov): Use input port id depending on the node metatype/attributes.
111109
return FXApplyTransformationCommand(
112110
constant_update_transformation_builder(node_with_weight, weight_value.data, input_port_id=1)
@@ -119,7 +117,7 @@ def scale_insertion_command(
119117
source_output_port_id: int,
120118
nodes: List[NNCFNode],
121119
scale_node_name: str,
122-
) -> OVMultiplyInsertionCommand:
120+
) -> FXApplyTransformationCommand:
123121
input_port_id = 0
124122
target_points = []
125123
for node in nodes:

pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ select = [
139139
"CPY001", # copyright check
140140
"NPY", # numpy rules
141141
"UP", # pyupgrade
142-
"EM" # flake8-errmsg
142+
"EM", # flake8-errmsg
143+
"INP", # flake8-no-pep420
143144
]
144145
extend-select = [
145146
"SIM", # https://pypi.org/project/flake8-simplify
@@ -151,6 +152,7 @@ extend-select = [
151152
"tests/**/*.py" = ["F403"]
152153
"tests/**/__init__.py" = ["F401"]
153154
"examples/**/*.py" = ["F403"]
155+
"!nncf/**.py" = ["INP"]
154156

155157
[tool.ruff.lint.flake8-copyright]
156158
notice-rgx = """\

tests/cross_fw/install/common.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ def onerror(name, excluded_modules_patterns):
3333
def load_nncf_modules(excluded_modules_patterns, verbose=False):
3434
onerror_partial = partial(onerror, excluded_modules_patterns=excluded_modules_patterns)
3535
for loader, module_name, _ in pkgutil.walk_packages(nncf.__path__, nncf.__name__ + ".", onerror_partial):
36-
if module_name in sys.modules or excluded_module(module_name, excluded_modules_patterns):
36+
if excluded_module(module_name, excluded_modules_patterns):
3737
if verbose:
3838
print(f"Module {module_name} ------ SKIPPED")
3939
continue
40+
if module_name in sys.modules:
41+
if verbose:
42+
print(f"Module {module_name} ------ LOADED")
43+
continue
4044
loader.find_module(module_name).load_module(module_name)
4145
if verbose:
4246
print(f"Module {module_name} ------ LOADED")

tests/cross_fw/install/install_checks_onnx.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
"nncf\\.experimental\\.tensorflow.*",
1919
"nncf\\.experimental\\.torch.*",
2020
"nncf\\.experimental\\.openvino.*",
21-
".*?openvino_[^\\.]*",
22-
".*?torch_[^\\.]*",
23-
".*?tf_[^\\.]*",
21+
"^(?!nncf(?:\\.experimental)*\\.onnx.*?\\.).*?openvino_[^\\.]*",
22+
"^(?!nncf(?:\\.experimental)*\\.onnx.*?\\.).*?torch_[^\\.]*",
23+
"^(?!nncf(?:\\.experimental)*\\.onnx.*?\\.).*?tf_[^\\.]*",
2424
)
2525

2626
load_nncf_modules(EXCLUDED_MODULES_PATTERNS)

tests/cross_fw/install/install_checks_openvino.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
"nncf\\.experimental\\.onnx.*",
1919
"nncf\\.experimental\\.tensorflow.*",
2020
"nncf\\.experimental\\.torch.*",
21-
".*?onnx_[^\\.]*",
22-
".*?torch_[^\\.]*",
23-
".*?tf_[^\\.]*",
24-
"nncf\\.openvino\\.pot.*",
21+
"^(?!nncf(?:\\.experimental)*\\.openvino.*?\\.).*?onnx_[^\\.]*",
22+
"^(?!nncf(?:\\.experimental)*\\.openvino.*?\\.).*?torch_[^\\.]*",
23+
"^(?!nncf(?:\\.experimental)*\\.openvino.*?\\.).*?tf_[^\\.]*",
2524
)
2625

2726
load_nncf_modules(EXCLUDED_MODULES_PATTERNS)

tests/cross_fw/install/install_checks_tf.py

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import nncf # noqa: F401
1515
from nncf.common.compression import BaseCompressionAlgorithmController
1616
from nncf.tensorflow.helpers.model_creation import create_compressed_model
17+
from tests.cross_fw.install.common import load_nncf_modules
1718
from tests.tensorflow.quantization.utils import get_basic_quantization_config
1819

1920
# Do not remove - these imports are for testing purposes.
@@ -26,3 +27,17 @@
2627
config = get_basic_quantization_config()
2728
compression_state_to_skip_init = {BaseCompressionAlgorithmController.BUILDER_STATE: {}}
2829
compression_model, compression_ctrl = create_compressed_model(model, config, compression_state_to_skip_init)
30+
31+
EXCLUDED_MODULES_PATTERNS = (
32+
"nncf\\.openvino.*",
33+
"nncf\\.torch.*",
34+
"nncf\\.onnx.*",
35+
"nncf\\.experimental\\.torch.*",
36+
"nncf\\.experimental\\.openvino.*",
37+
"nncf\\.experimental\\.onnx.*",
38+
"^(?!nncf(?:\\.experimental)*\\.tensorflow.*?\\.).*?openvino_[^\\.]*",
39+
"^(?!nncf(?:\\.experimental)*\\.tensorflow.*?\\.).*?onnx_[^\\.]*",
40+
"^(?!nncf(?:\\.experimental)*\\.tensorflow.*?\\.).*?torch_[^\\.]*",
41+
)
42+
43+
load_nncf_modules(EXCLUDED_MODULES_PATTERNS)

tests/cross_fw/install/install_checks_torch.py

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import torch
1515

1616
import nncf
17+
from tests.cross_fw.install.common import load_nncf_modules
1718

1819
if len(sys.argv) != 3:
1920
msg = "Must be run with an execution type as argument (either 'cpu' or 'gpu') and package type"
@@ -54,3 +55,17 @@
5455
else:
5556
msg = f"Invalid execution type {execution_type} (expected 'cpu' or 'gpu')!"
5657
raise nncf.ValidationError(msg)
58+
59+
EXCLUDED_MODULES_PATTERNS = (
60+
"nncf\\.openvino.*",
61+
"nncf\\.tensorflow.*",
62+
"nncf\\.onnx.*",
63+
"nncf\\.experimental\\.tensorflow.*",
64+
"nncf\\.experimental\\.openvino.*",
65+
"nncf\\.experimental\\.onnx.*",
66+
"^(?!nncf(?:\\.experimental)*\\.torch.*?\\.).*?openvino_[^\\.]*",
67+
"^(?!nncf(?:\\.experimental)*\\.torch.*?\\.).*?onnx_[^\\.]*",
68+
"^(?!nncf(?:\\.experimental)*\\.torch.*?\\.).*?tf_[^\\.]*",
69+
)
70+
71+
load_nncf_modules(EXCLUDED_MODULES_PATTERNS)

tests/cross_fw/shared/helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_cli_dict_args(args):
3636

3737

3838
MAP_BACKEND_PACKAGES = {
39-
"torch": ["torch"],
39+
"torch": ["torch", "torchvision"],
4040
"openvino": ["openvino"],
4141
"onnx": ["onnx", "onnxruntime"],
4242
"tf": ["tensorflow"],

tests/torch/fx/test_quantizer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
from torch.ao.quantization.quantizer.x86_inductor_quantizer import get_default_x86_inductor_quantization_config
3232

3333
import nncf
34-
from nncf.experimental.quantization.quantizers.openvino_quantizer import OpenVINOQuantizer
35-
from nncf.experimental.quantization.quantizers.torch_ao_adapter import _get_edge_or_node_to_qspec
3634
from nncf.experimental.torch.fx.nncf_graph_builder import GraphConverter
3735
from nncf.experimental.torch.fx.quantization.quantize_pt2e import quantize_pt2e
36+
from nncf.experimental.torch.fx.quantization.quantizer.openvino_quantizer import OpenVINOQuantizer
37+
from nncf.experimental.torch.fx.quantization.quantizer.torch_ao_adapter import _get_edge_or_node_to_qspec
3838
from tests.torch import test_models
3939
from tests.torch.fx.helpers import get_torch_fx_model
4040
from tests.torch.test_compressed_graph import check_graph

0 commit comments

Comments
 (0)