Skip to content

Commit 9cba691

Browse files
[ruff] INP rule, extend install tests (#3245)
#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 - Moved fx quantizers to fx directory - 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/13142880275
1 parent 909544d commit 9cba691

File tree

24 files changed

+110
-28
lines changed

24 files changed

+110
-28
lines changed
+10
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.

Diff for: 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

Diff for: 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.

Diff for: 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

Diff for: 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.

Diff for: nncf/experimental/quantization/quantizers/openvino_adapter.py renamed to 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):

Diff for: nncf/experimental/quantization/quantizers/torch_ao_adapter.py renamed to 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]]

Diff for: nncf/experimental/torch2/quantization/__init__.py

+10
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.

Diff for: nncf/experimental/torch2/statistics/__init__.py

+10
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.

Diff for: 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
@@ -127,7 +126,7 @@ def get_weight_value(node_with_weight: NNCFNode, model: NNCFNetwork, nncf_graph:
127126
return Tensor(weight_data)
128127

129128
@staticmethod
130-
def weight_update_command(node_with_weight: NNCFNode, weight_value: np.ndarray) -> OVWeightUpdateCommand:
129+
def weight_update_command(node_with_weight: NNCFNode, weight_value: np.ndarray) -> PTWeightUpdateCommand:
131130
return create_command_to_update_weight(node_with_weight, weight_value)
132131

133132
@staticmethod
@@ -137,7 +136,7 @@ def scale_insertion_command(
137136
source_output_port_id: int,
138137
nodes: List[NNCFNode],
139138
scale_node_name: str,
140-
) -> OVMultiplyInsertionCommand:
139+
) -> PTSharedFnInsertionCommand:
141140
input_port_id = 0
142141
target_points = []
143142
for node in nodes:

Diff for: 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
@@ -104,7 +102,7 @@ def get_weight_value(node_with_weight: NNCFNode, model: torch.fx.GraphModule, nn
104102
return Tensor(weight_data.data)
105103

106104
@staticmethod
107-
def weight_update_command(node_with_weight: NNCFNode, weight_value: torch.Tensor) -> OVWeightUpdateCommand:
105+
def weight_update_command(node_with_weight: NNCFNode, weight_value: torch.Tensor) -> FXApplyTransformationCommand:
108106
# TODO(dlyakhov): Use input port id depending on the node metatype/attributes.
109107
return FXApplyTransformationCommand(
110108
constant_update_transformation_builder(node_with_weight, weight_value.data, input_port_id=1)
@@ -117,7 +115,7 @@ def scale_insertion_command(
117115
source_output_port_id: int,
118116
nodes: List[NNCFNode],
119117
scale_node_name: str,
120-
) -> OVMultiplyInsertionCommand:
118+
) -> FXApplyTransformationCommand:
121119
input_port_id = 0
122120
target_points = []
123121
for node in nodes:

Diff for: pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ select = [
138138
"F", # pyflakes rules
139139
"CPY001", # copyright check
140140
"NPY", # numpy rules
141+
"INP", # flake8-no-pep420
141142
]
142143
extend-select = [
143144
"SIM", # https://pypi.org/project/flake8-simplify
@@ -149,6 +150,7 @@ extend-select = [
149150
"tests/**/*.py" = ["F403"]
150151
"tests/**/__init__.py" = ["F401"]
151152
"examples/**/*.py" = ["F403"]
153+
"!nncf/**.py" = ["INP"]
152154

153155
[tool.ruff.lint.flake8-copyright]
154156
notice-rgx = """\

Diff for: tests/cross_fw/install/common.py

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

Diff for: 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)

Diff for: 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)

Diff for: 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)

Diff for: 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
raise nncf.ValidationError(
@@ -54,3 +55,17 @@
5455
)
5556
else:
5657
raise nncf.ValidationError(f"Invalid execution type {execution_type} (expected 'cpu' or 'gpu')!")
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)

Diff for: 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"],

Diff for: 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)