Skip to content

Commit

Permalink
[ruff] INP rule, extend install tests (#3245)
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
AlexanderDokuchaev authored Feb 5, 2025
1 parent 909544d commit 9cba691
Show file tree
Hide file tree
Showing 24 changed files with 110 additions and 28 deletions.
File renamed without changes.
10 changes: 10 additions & 0 deletions nncf/experimental/quantization/algorithms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2025 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from nncf.common.tensor_statistics.statistic_point import StatisticPointsContainer
from nncf.common.utils.backend import BackendType
from nncf.experimental.quantization.algorithms.post_training.pipeline import experimental_create_ptq_pipeline
from nncf.experimental.quantization.quantizers.quantizer import Quantizer
from nncf.experimental.quantization.quantizer import Quantizer
from nncf.quantization.advanced_parameters import AdvancedBiasCorrectionParameters
from nncf.quantization.advanced_parameters import AdvancedSmoothQuantParameters
from nncf.quantization.advanced_parameters import RangeEstimatorParameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Optional, TypeVar

from nncf.experimental.quantization.algorithms.range_estimator.algorithm import MinMaxRangeEstimator
from nncf.experimental.quantization.quantizers.quantizer import Quantizer
from nncf.experimental.quantization.quantizer import Quantizer
from nncf.quantization.advanced_parameters import AdvancedBiasCorrectionParameters
from nncf.quantization.advanced_parameters import AdvancedSmoothQuantParameters
from nncf.quantization.advanced_parameters import RangeEstimatorParameters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2025 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from nncf.common.graph.graph import NNCFGraph
from nncf.common.tensor_statistics.statistic_point import StatisticPointsContainer
from nncf.common.utils.backend import BackendType
from nncf.experimental.quantization.quantizers.quantizer import Quantizer
from nncf.experimental.quantization.quantizer import Quantizer
from nncf.quantization.algorithms.algorithm import Algorithm
from nncf.quantization.algorithms.min_max.algorithm import MinMaxQuantization
from nncf.quantization.range_estimator import RangeEstimatorParameters
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions nncf/experimental/torch/fx/quantization/quantize_pt2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
from nncf.common.logging import nncf_logger
from nncf.data import Dataset
from nncf.experimental.quantization.algorithms.post_training.algorithm import ExperimentalPostTrainingQuantization
from nncf.experimental.quantization.quantizers.openvino_adapter import OpenVINOQuantizerAdapter
from nncf.experimental.quantization.quantizers.openvino_quantizer import OpenVINOQuantizer
from nncf.experimental.quantization.quantizers.torch_ao_adapter import TorchAOQuantizerAdapter
from nncf.experimental.torch.fx.constant_folding import constant_fold
from nncf.experimental.torch.fx.quantization.quantizer.openvino_adapter import OpenVINOQuantizerAdapter
from nncf.experimental.torch.fx.quantization.quantizer.openvino_quantizer import OpenVINOQuantizer
from nncf.experimental.torch.fx.quantization.quantizer.torch_ao_adapter import TorchAOQuantizerAdapter
from nncf.experimental.torch.fx.transformations import QUANTIZE_NODE_TARGETS
from nncf.experimental.torch.fx.transformations import compress_post_quantize_transformation
from nncf.quantization.advanced_parameters import AdvancedBiasCorrectionParameters
Expand Down
10 changes: 10 additions & 0 deletions nncf/experimental/torch/fx/quantization/quantizer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2025 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

from nncf.common.graph.graph import NNCFGraph
from nncf.common.quantization.quantizer_setup import SingleConfigQuantizerSetup
from nncf.experimental.quantization.quantizers.openvino_quantizer import OpenVINOQuantizer
from nncf.experimental.quantization.quantizers.quantizer import Quantizer
from nncf.experimental.quantization.quantizer import Quantizer
from nncf.experimental.torch.fx.quantization.quantizer.openvino_quantizer import OpenVINOQuantizer


class OpenVINOQuantizerAdapter(Quantizer):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from nncf.common.quantization.quantizer_setup import WeightQuantizationInsertionPoint
from nncf.common.quantization.structs import QuantizationScheme as QuantizationMode
from nncf.common.quantization.structs import QuantizerConfig
from nncf.experimental.quantization.quantizers.quantizer import Quantizer
from nncf.experimental.quantization.quantizer import Quantizer
from nncf.experimental.torch.fx.nncf_graph_builder import GraphConverter

EdgeOrNode = Union[Tuple[torch.fx.Node, torch.fx.Node]]
Expand Down
10 changes: 10 additions & 0 deletions nncf/experimental/torch2/quantization/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2025 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
10 changes: 10 additions & 0 deletions nncf/experimental/torch2/statistics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2025 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
7 changes: 3 additions & 4 deletions nncf/quantization/algorithms/smooth_quant/torch_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
from nncf.experimental.common.tensor_statistics.collectors import AbsMaxReducer
from nncf.experimental.common.tensor_statistics.collectors import MaxAggregator
from nncf.experimental.common.tensor_statistics.collectors import TensorCollector
from nncf.openvino.graph.transformations.commands import OVMultiplyInsertionCommand
from nncf.openvino.graph.transformations.commands import OVWeightUpdateCommand
from nncf.quantization.algorithms.smooth_quant.backend import SmoothQuantAlgoBackend
from nncf.tensor import Tensor
from nncf.torch.graph.transformations.command_creation import create_command_to_update_weight
from nncf.torch.graph.transformations.commands import PTSharedFnInsertionCommand
from nncf.torch.graph.transformations.commands import PTTargetPoint
from nncf.torch.graph.transformations.commands import PTWeightUpdateCommand
from nncf.torch.layer_utils import COMPRESSION_MODULES
from nncf.torch.layer_utils import CompressionParameter
from nncf.torch.layer_utils import StatefullModuleInterface
Expand Down Expand Up @@ -127,7 +126,7 @@ def get_weight_value(node_with_weight: NNCFNode, model: NNCFNetwork, nncf_graph:
return Tensor(weight_data)

@staticmethod
def weight_update_command(node_with_weight: NNCFNode, weight_value: np.ndarray) -> OVWeightUpdateCommand:
def weight_update_command(node_with_weight: NNCFNode, weight_value: np.ndarray) -> PTWeightUpdateCommand:
return create_command_to_update_weight(node_with_weight, weight_value)

@staticmethod
Expand All @@ -137,7 +136,7 @@ def scale_insertion_command(
source_output_port_id: int,
nodes: List[NNCFNode],
scale_node_name: str,
) -> OVMultiplyInsertionCommand:
) -> PTSharedFnInsertionCommand:
input_port_id = 0
target_points = []
for node in nodes:
Expand Down
6 changes: 2 additions & 4 deletions nncf/quantization/algorithms/smooth_quant/torch_fx_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
from nncf.experimental.torch.fx.transformations import constant_update_transformation_builder
from nncf.experimental.torch.fx.transformations import get_graph_node_by_name
from nncf.experimental.torch.fx.transformations import module_insertion_transformation_builder
from nncf.openvino.graph.transformations.commands import OVMultiplyInsertionCommand
from nncf.openvino.graph.transformations.commands import OVWeightUpdateCommand
from nncf.quantization.algorithms.smooth_quant.backend import SmoothQuantAlgoBackend
from nncf.tensor import Tensor
from nncf.torch.graph.transformations.commands import PTTargetPoint
Expand Down Expand Up @@ -104,7 +102,7 @@ def get_weight_value(node_with_weight: NNCFNode, model: torch.fx.GraphModule, nn
return Tensor(weight_data.data)

@staticmethod
def weight_update_command(node_with_weight: NNCFNode, weight_value: torch.Tensor) -> OVWeightUpdateCommand:
def weight_update_command(node_with_weight: NNCFNode, weight_value: torch.Tensor) -> FXApplyTransformationCommand:
# TODO(dlyakhov): Use input port id depending on the node metatype/attributes.
return FXApplyTransformationCommand(
constant_update_transformation_builder(node_with_weight, weight_value.data, input_port_id=1)
Expand All @@ -117,7 +115,7 @@ def scale_insertion_command(
source_output_port_id: int,
nodes: List[NNCFNode],
scale_node_name: str,
) -> OVMultiplyInsertionCommand:
) -> FXApplyTransformationCommand:
input_port_id = 0
target_points = []
for node in nodes:
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ select = [
"F", # pyflakes rules
"CPY001", # copyright check
"NPY", # numpy rules
"INP", # flake8-no-pep420
]
extend-select = [
"SIM", # https://pypi.org/project/flake8-simplify
Expand All @@ -149,6 +150,7 @@ extend-select = [
"tests/**/*.py" = ["F403"]
"tests/**/__init__.py" = ["F401"]
"examples/**/*.py" = ["F403"]
"!nncf/**.py" = ["INP"]

[tool.ruff.lint.flake8-copyright]
notice-rgx = """\
Expand Down
6 changes: 5 additions & 1 deletion tests/cross_fw/install/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ def onerror(name, excluded_modules_patterns):
def load_nncf_modules(excluded_modules_patterns, verbose=False):
onerror_partial = partial(onerror, excluded_modules_patterns=excluded_modules_patterns)
for loader, module_name, _ in pkgutil.walk_packages(nncf.__path__, nncf.__name__ + ".", onerror_partial):
if module_name in sys.modules or excluded_module(module_name, excluded_modules_patterns):
if excluded_module(module_name, excluded_modules_patterns):
if verbose:
print(f"Module {module_name} ------ SKIPPED")
continue
if module_name in sys.modules:
if verbose:
print(f"Module {module_name} ------ LOADED")
continue
loader.find_module(module_name).load_module(module_name)
if verbose:
print(f"Module {module_name} ------ LOADED")
6 changes: 3 additions & 3 deletions tests/cross_fw/install/install_checks_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"nncf\\.experimental\\.tensorflow.*",
"nncf\\.experimental\\.torch.*",
"nncf\\.experimental\\.openvino.*",
".*?openvino_[^\\.]*",
".*?torch_[^\\.]*",
".*?tf_[^\\.]*",
"^(?!nncf(?:\\.experimental)*\\.onnx.*?\\.).*?openvino_[^\\.]*",
"^(?!nncf(?:\\.experimental)*\\.onnx.*?\\.).*?torch_[^\\.]*",
"^(?!nncf(?:\\.experimental)*\\.onnx.*?\\.).*?tf_[^\\.]*",
)

load_nncf_modules(EXCLUDED_MODULES_PATTERNS)
7 changes: 3 additions & 4 deletions tests/cross_fw/install/install_checks_openvino.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
"nncf\\.experimental\\.onnx.*",
"nncf\\.experimental\\.tensorflow.*",
"nncf\\.experimental\\.torch.*",
".*?onnx_[^\\.]*",
".*?torch_[^\\.]*",
".*?tf_[^\\.]*",
"nncf\\.openvino\\.pot.*",
"^(?!nncf(?:\\.experimental)*\\.openvino.*?\\.).*?onnx_[^\\.]*",
"^(?!nncf(?:\\.experimental)*\\.openvino.*?\\.).*?torch_[^\\.]*",
"^(?!nncf(?:\\.experimental)*\\.openvino.*?\\.).*?tf_[^\\.]*",
)

load_nncf_modules(EXCLUDED_MODULES_PATTERNS)
15 changes: 15 additions & 0 deletions tests/cross_fw/install/install_checks_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import nncf # noqa: F401
from nncf.common.compression import BaseCompressionAlgorithmController
from nncf.tensorflow.helpers.model_creation import create_compressed_model
from tests.cross_fw.install.common import load_nncf_modules
from tests.tensorflow.quantization.utils import get_basic_quantization_config

# Do not remove - these imports are for testing purposes.
Expand All @@ -26,3 +27,17 @@
config = get_basic_quantization_config()
compression_state_to_skip_init = {BaseCompressionAlgorithmController.BUILDER_STATE: {}}
compression_model, compression_ctrl = create_compressed_model(model, config, compression_state_to_skip_init)

EXCLUDED_MODULES_PATTERNS = (
"nncf\\.openvino.*",
"nncf\\.torch.*",
"nncf\\.onnx.*",
"nncf\\.experimental\\.torch.*",
"nncf\\.experimental\\.openvino.*",
"nncf\\.experimental\\.onnx.*",
"^(?!nncf(?:\\.experimental)*\\.tensorflow.*?\\.).*?openvino_[^\\.]*",
"^(?!nncf(?:\\.experimental)*\\.tensorflow.*?\\.).*?onnx_[^\\.]*",
"^(?!nncf(?:\\.experimental)*\\.tensorflow.*?\\.).*?torch_[^\\.]*",
)

load_nncf_modules(EXCLUDED_MODULES_PATTERNS)
15 changes: 15 additions & 0 deletions tests/cross_fw/install/install_checks_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import torch

import nncf
from tests.cross_fw.install.common import load_nncf_modules

if len(sys.argv) != 3:
raise nncf.ValidationError(
Expand Down Expand Up @@ -54,3 +55,17 @@
)
else:
raise nncf.ValidationError(f"Invalid execution type {execution_type} (expected 'cpu' or 'gpu')!")

EXCLUDED_MODULES_PATTERNS = (
"nncf\\.openvino.*",
"nncf\\.tensorflow.*",
"nncf\\.onnx.*",
"nncf\\.experimental\\.tensorflow.*",
"nncf\\.experimental\\.openvino.*",
"nncf\\.experimental\\.onnx.*",
"^(?!nncf(?:\\.experimental)*\\.torch.*?\\.).*?openvino_[^\\.]*",
"^(?!nncf(?:\\.experimental)*\\.torch.*?\\.).*?onnx_[^\\.]*",
"^(?!nncf(?:\\.experimental)*\\.torch.*?\\.).*?tf_[^\\.]*",
)

load_nncf_modules(EXCLUDED_MODULES_PATTERNS)
2 changes: 1 addition & 1 deletion tests/cross_fw/shared/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_cli_dict_args(args):


MAP_BACKEND_PACKAGES = {
"torch": ["torch"],
"torch": ["torch", "torchvision"],
"openvino": ["openvino"],
"onnx": ["onnx", "onnxruntime"],
"tf": ["tensorflow"],
Expand Down
4 changes: 2 additions & 2 deletions tests/torch/fx/test_quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
from torch.ao.quantization.quantizer.x86_inductor_quantizer import get_default_x86_inductor_quantization_config

import nncf
from nncf.experimental.quantization.quantizers.openvino_quantizer import OpenVINOQuantizer
from nncf.experimental.quantization.quantizers.torch_ao_adapter import _get_edge_or_node_to_qspec
from nncf.experimental.torch.fx.nncf_graph_builder import GraphConverter
from nncf.experimental.torch.fx.quantization.quantize_pt2e import quantize_pt2e
from nncf.experimental.torch.fx.quantization.quantizer.openvino_quantizer import OpenVINOQuantizer
from nncf.experimental.torch.fx.quantization.quantizer.torch_ao_adapter import _get_edge_or_node_to_qspec
from tests.torch import test_models
from tests.torch.fx.helpers import get_torch_fx_model
from tests.torch.test_compressed_graph import check_graph
Expand Down

0 comments on commit 9cba691

Please sign in to comment.