diff --git a/docs/api/source/conf.py b/docs/api/source/conf.py index ba401c3b5bd..3e60d7f4ec4 100644 --- a/docs/api/source/conf.py +++ b/docs/api/source/conf.py @@ -5,8 +5,7 @@ import sys from dataclasses import dataclass from dataclasses import field -from typing import Any -from typing import Dict, List +from typing import Any, Dict, List from sphinx.ext.autodoc import mock @@ -17,9 +16,7 @@ author = "Intel Corporation" release = "v2.4.0" -extensions = ["autoapi.extension", - "sphinx.ext.autodoc", - "sphinx.ext.linkcode"] +extensions = ["autoapi.extension", "sphinx.ext.autodoc", "sphinx.ext.linkcode"] # The below line in conjunction with specifying the 'sphinx.ext.autodoc' as extension # makes the type hints from the function signature disappear from the signature in the HTML and instead @@ -30,11 +27,7 @@ autodoc_typehints = "description" autoapi_dirs = ["../../../nncf"] -autoapi_options = ["members", - "show-inheritance", - "show-module-summary", - "special-members", - "imported-members"] +autoapi_options = ["members", "show-inheritance", "show-module-summary", "special-members", "imported-members"] autoapi_template_dir = "_autoapi_templates" autoapi_keep_files = True @@ -161,21 +154,21 @@ def linkcode_resolve(domain, info): # sphinx.ext.linkcode interface; will link to Github here. target_ref = "develop" base_url = f"https://github.com/openvinotoolkit/nncf/blob/{target_ref}/" - if not info['module']: + if not info["module"]: return None - fullname = info["module"] + '.' + info["fullname"] + fullname = info["module"] + "." + info["fullname"] if fullname not in api_info.api_names_vs_obj_dict: # Got a method/property description, info["fullname"] contained class.method_name - fullname = fullname.rpartition('.')[0] + fullname = fullname.rpartition(".")[0] if fullname in api_info.canonical_name_vs_fqn: fullname = api_info.canonical_name_vs_fqn[fullname] - module_name = fullname.rpartition('.')[0] + module_name = fullname.rpartition(".")[0] else: module_name = info["module"] - filename = module_name.replace('.', '/') - complete_url = base_url + filename + '.py' + filename = module_name.replace(".", "/") + complete_url = base_url + filename + ".py" return complete_url diff --git a/docs/styleguide/PyGuide.md b/docs/styleguide/PyGuide.md index 7b5f2d3cfe9..3e7cb969a2c 100644 --- a/docs/styleguide/PyGuide.md +++ b/docs/styleguide/PyGuide.md @@ -771,10 +771,11 @@ if __name__ == "__main__": ## 5 API documentation rules -All functions and classes that belong to NNCF API should be documented. +All functions and classes that belong to NNCF API should be documented. +The documentation should utilize the reStructuredText (.rst) format for specifying parameters, return types and otherwise formatting the docstring, since the docstring is used as a source for generating the HTML API documentation with Sphinx. Argument descriptions for `__init__(...)` methods of API classes should be located in the docstring of the class itself, not the docstring of the `__init__(...)` method. This is required so that the autogenerated API documentation is rendered properly. If the autogenerated API documentation does not show type hints for certain arguments despite the fact that the type hints are present in the object's implementation code, -or if the type hints do not refer to the API symbol's canonical alias, then the type hint should be explicitly declared in the docstring using the `:type:` directive: +or if the type hints do not refer to the API symbol's canonical alias, then the type hint should be explicitly declared in the docstring using the `:type *param_name*:` directive (or `:rtype:` for return types). diff --git a/nncf/api/compression.py b/nncf/api/compression.py index 34745785c8b..6814fb60820 100644 --- a/nncf/api/compression.py +++ b/nncf/api/compression.py @@ -326,6 +326,7 @@ def maximal_compression_rate(self) -> float: Returns the maximal model compression rate supported by the compression controller. """ + @api() class CompressionAlgorithmBuilder(ABC): """ diff --git a/nncf/common/__init__.py b/nncf/common/__init__.py index 83f3bb3fc6f..b687a5c33f7 100644 --- a/nncf/common/__init__.py +++ b/nncf/common/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-agnostic or backend-common NNCF functionality. -""" \ No newline at end of file +""" diff --git a/nncf/common/accuracy_aware_training/__init__.py b/nncf/common/accuracy_aware_training/__init__.py index 05cda6d3202..62a7a2c15dc 100644 --- a/nncf/common/accuracy_aware_training/__init__.py +++ b/nncf/common/accuracy_aware_training/__init__.py @@ -8,9 +8,9 @@ # 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. +""" +Accuracy Aware Training functionality. +""" from nncf.common.accuracy_aware_training.training_loop import AccuracyAwareTrainingMode from nncf.common.accuracy_aware_training.training_loop import create_accuracy_aware_training_loop -""" -Accuracy Aware Training functionality. -""" \ No newline at end of file diff --git a/nncf/common/accuracy_aware_training/training_loop.py b/nncf/common/accuracy_aware_training/training_loop.py index 41be72e7b3e..8c456c95062 100644 --- a/nncf/common/accuracy_aware_training/training_loop.py +++ b/nncf/common/accuracy_aware_training/training_loop.py @@ -15,10 +15,7 @@ from abc import ABC from abc import abstractmethod from functools import partial -from typing import Callable -from typing import Optional -from typing import TypeVar -from typing import Union +from typing import Callable, Optional, TypeVar, Union import numpy as np from scipy.interpolate import interp1d @@ -91,23 +88,24 @@ class BaseEarlyExitCompressionTrainingLoop(TrainingLoop, ABC): """ Base class to generalize functionality of derived training loop classes. """ + def __init__(self, compression_controller: CompressionAlgorithmController): self.runner = None # type: BaseAccuracyAwareTrainingRunner self.compression_controller = compression_controller self._current_compression_rate = None def run( - self, - model: TModel, - train_epoch_fn: Callable, - validate_fn: Callable, - configure_optimizers_fn: Callable = None, - dump_checkpoint_fn: Callable = None, - load_checkpoint_fn: Callable = None, - early_stopping_fn: Callable = None, - tensorboard_writer: Optional[TensorboardWriterType] = None, - log_dir: Union[pathlib.Path, str] = None, - update_learning_rate_fn: Callable = None, + self, + model: TModel, + train_epoch_fn: Callable, + validate_fn: Callable, + configure_optimizers_fn: Callable = None, + dump_checkpoint_fn: Callable = None, + load_checkpoint_fn: Callable = None, + early_stopping_fn: Callable = None, + tensorboard_writer: Optional[TensorboardWriterType] = None, + log_dir: Union[pathlib.Path, str] = None, + update_learning_rate_fn: Callable = None, ): self.runner.initialize_training_loop_fns( train_epoch_fn, @@ -275,7 +273,7 @@ def __init__( verbose: bool = True, minimal_compression_rate: float = 0.0, maximal_compression_rate: float = 0.95, - dump_checkpoints: bool = True + dump_checkpoints: bool = True, ): super().__init__(compression_controller) self.adaptive_controller = self._get_adaptive_compression_ctrl(compression_controller) @@ -334,17 +332,17 @@ def remove_registry_prefix(algo_name): ) def run( - self, - model: TModel, - train_epoch_fn: Callable, - validate_fn: Callable, - configure_optimizers_fn: Callable = None, - dump_checkpoint_fn: Callable = None, - load_checkpoint_fn: Callable = None, - early_stopping_fn: Callable = None, - tensorboard_writer: Optional[TensorboardWriterType] = None, - log_dir: Union[pathlib.Path, str] = None, - update_learning_rate_fn: Callable = None, + self, + model: TModel, + train_epoch_fn: Callable, + validate_fn: Callable, + configure_optimizers_fn: Callable = None, + dump_checkpoint_fn: Callable = None, + load_checkpoint_fn: Callable = None, + early_stopping_fn: Callable = None, + tensorboard_writer: Optional[TensorboardWriterType] = None, + log_dir: Union[pathlib.Path, str] = None, + update_learning_rate_fn: Callable = None, ): self.runner.initialize_training_loop_fns( train_epoch_fn, diff --git a/nncf/common/initialization/__init__.py b/nncf/common/initialization/__init__.py index f4b2234b747..39cabd39176 100644 --- a/nncf/common/initialization/__init__.py +++ b/nncf/common/initialization/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Functions and classes utilized during the user dataset-driven initialization of the compression algorithms. -""" \ No newline at end of file +""" diff --git a/nncf/common/quantization/structs.py b/nncf/common/quantization/structs.py index 06bb2db0b80..95e5ea3e9d8 100644 --- a/nncf/common/quantization/structs.py +++ b/nncf/common/quantization/structs.py @@ -28,6 +28,7 @@ class QuantizationMode: :param SYMMETRIC: :param ASYMMETRIC: """ + SYMMETRIC = "symmetric" ASYMMETRIC = "asymmetric" @@ -321,6 +322,7 @@ class QuantizationPreset(Enum): """ An enum with values corresponding to the available quantization presets. """ + PERFORMANCE = "performance" MIXED = "mixed" diff --git a/nncf/config/config.py b/nncf/config/config.py index 10ab5754862..8ed3e8422d2 100644 --- a/nncf/config/config.py +++ b/nncf/config/config.py @@ -11,8 +11,7 @@ from copy import deepcopy from pathlib import Path -from typing import Dict -from typing import List, Optional, Type +from typing import Dict, List, Optional, Type import jsonschema import jstyleson as json diff --git a/nncf/config/structures.py b/nncf/config/structures.py index 4852ddcdd0f..c29c6637155 100644 --- a/nncf/config/structures.py +++ b/nncf/config/structures.py @@ -90,6 +90,7 @@ class ModelEvaluationArgs(NNCFExtraConfigStruct): :param eval_fn: A function accepting a single argument - the model object - and returning the model's metric on the evaluation split of the dataset corresponding to the model. """ + def __init__(self, eval_fn: Callable): self.eval_fn = eval_fn diff --git a/nncf/onnx/__init__.py b/nncf/onnx/__init__.py index dfffc6426cb..ad92d0721d4 100644 --- a/nncf/onnx/__init__.py +++ b/nncf/onnx/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Base subpackage for NNCF ONNX functionality. -""" \ No newline at end of file +""" diff --git a/nncf/openvino/__init__.py b/nncf/openvino/__init__.py index ed2b4ef53e0..8a07dd5505c 100644 --- a/nncf/openvino/__init__.py +++ b/nncf/openvino/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Base subpackage for NNCF OpenVINO functionality. -""" \ No newline at end of file +""" diff --git a/nncf/tensorflow/helpers/callback_creation.py b/nncf/tensorflow/helpers/callback_creation.py index 7a9618d47f5..c7f91e971e5 100644 --- a/nncf/tensorflow/helpers/callback_creation.py +++ b/nncf/tensorflow/helpers/callback_creation.py @@ -19,10 +19,12 @@ @api(canonical_alias="nncf.tensorflow.create_compression_callbacks") -def create_compression_callbacks(compression_ctrl: CompressionAlgorithmController, - log_tensorboard: bool = True, - log_text: bool = True, - log_dir: bool = None): +def create_compression_callbacks( + compression_ctrl: CompressionAlgorithmController, + log_tensorboard: bool = True, + log_text: bool = True, + log_dir: bool = None, +): compression_controllers = ( compression_ctrl.child_ctrls if isinstance(compression_ctrl, CompositeCompressionAlgorithmController) diff --git a/nncf/tensorflow/pruning/__init__.py b/nncf/tensorflow/pruning/__init__.py index 9d0de1faba1..23f8e4a89d5 100644 --- a/nncf/tensorflow/pruning/__init__.py +++ b/nncf/tensorflow/pruning/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementations of pruning algorithms. -""" \ No newline at end of file +""" diff --git a/nncf/tensorflow/pruning/filter_pruning/__init__.py b/nncf/tensorflow/pruning/filter_pruning/__init__.py index 1fcd3cbf736..7f6f2029b35 100644 --- a/nncf/tensorflow/pruning/filter_pruning/__init__.py +++ b/nncf/tensorflow/pruning/filter_pruning/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementations of filter-wise pruning. -""" \ No newline at end of file +""" diff --git a/nncf/tensorflow/quantization/algorithm.py b/nncf/tensorflow/quantization/algorithm.py index 1817ddbb3ec..96a2e61deaa 100644 --- a/nncf/tensorflow/quantization/algorithm.py +++ b/nncf/tensorflow/quantization/algorithm.py @@ -696,6 +696,7 @@ class QuantizationController(BaseCompressionAlgorithmController): """ Controller for the quantization algorithm in TensorFlow. """ + def __init__(self, target_model, config: NNCFConfig, op_names: List[str]): super().__init__(target_model) self._scheduler = BaseCompressionScheduler() diff --git a/nncf/tensorflow/sparsity/__init__.py b/nncf/tensorflow/sparsity/__init__.py index 5de73dd0a87..3a0b0fe26b2 100644 --- a/nncf/tensorflow/sparsity/__init__.py +++ b/nncf/tensorflow/sparsity/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementations of sparsity algorithms. -""" \ No newline at end of file +""" diff --git a/nncf/tensorflow/sparsity/magnitude/__init__.py b/nncf/tensorflow/sparsity/magnitude/__init__.py index 2ddddc17e3c..9b059339542 100644 --- a/nncf/tensorflow/sparsity/magnitude/__init__.py +++ b/nncf/tensorflow/sparsity/magnitude/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementations of magnitude sparsity. -""" \ No newline at end of file +""" diff --git a/nncf/tensorflow/sparsity/rb/__init__.py b/nncf/tensorflow/sparsity/rb/__init__.py index dd3f0f330db..6ce683c2d15 100644 --- a/nncf/tensorflow/sparsity/rb/__init__.py +++ b/nncf/tensorflow/sparsity/rb/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementations of regularization-based (RB) sparsity. -""" \ No newline at end of file +""" diff --git a/nncf/tensorflow/sparsity/rb/algorithm.py b/nncf/tensorflow/sparsity/rb/algorithm.py index e7631c5ce77..22c926e262b 100644 --- a/nncf/tensorflow/sparsity/rb/algorithm.py +++ b/nncf/tensorflow/sparsity/rb/algorithm.py @@ -113,6 +113,7 @@ class RBSparsityController(BaseSparsityController): """ Controller class for regularization-based (RB) sparsity in TF. """ + def __init__(self, target_model, config: NNCFConfig, op_names: List[str]): super().__init__(target_model, op_names) algo_config = extract_algo_specific_config(config, "rb_sparsity") diff --git a/nncf/torch/knowledge_distillation/__init__.py b/nncf/torch/knowledge_distillation/__init__.py index afb837e8eb5..4682a156e43 100644 --- a/nncf/torch/knowledge_distillation/__init__.py +++ b/nncf/torch/knowledge_distillation/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementations of knowledge distillation algorithms. -""" \ No newline at end of file +""" diff --git a/nncf/torch/knowledge_distillation/algo.py b/nncf/torch/knowledge_distillation/algo.py index 3976b73606f..2887b22f40d 100644 --- a/nncf/torch/knowledge_distillation/algo.py +++ b/nncf/torch/knowledge_distillation/algo.py @@ -61,6 +61,7 @@ class KnowledgeDistillationController(PTCompressionAlgorithmController): """ Controller for the knowledge distillation in PT. """ + def __init__( self, target_model: NNCFNetwork, original_model: nn.Module, kd_type: str, scale: float, temperature: float ): diff --git a/nncf/torch/pruning/__init__.py b/nncf/torch/pruning/__init__.py index 9d0de1faba1..23f8e4a89d5 100644 --- a/nncf/torch/pruning/__init__.py +++ b/nncf/torch/pruning/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementations of pruning algorithms. -""" \ No newline at end of file +""" diff --git a/nncf/torch/pruning/filter_pruning/__init__.py b/nncf/torch/pruning/filter_pruning/__init__.py index 1fcd3cbf736..7f6f2029b35 100644 --- a/nncf/torch/pruning/filter_pruning/__init__.py +++ b/nncf/torch/pruning/filter_pruning/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementations of filter-wise pruning. -""" \ No newline at end of file +""" diff --git a/nncf/torch/quantization/algo.py b/nncf/torch/quantization/algo.py index d283e2e2741..08f833c7e81 100644 --- a/nncf/torch/quantization/algo.py +++ b/nncf/torch/quantization/algo.py @@ -1290,6 +1290,7 @@ class QuantizationControllerBase(PTCompressionAlgorithmController): """ Base controller class for the quantization controllers in PT. """ + def enable_activation_quantization(self): raise NotImplementedError @@ -1311,6 +1312,7 @@ class QuantizationController(QuantizationControllerBase): """ Controller for the quantization algorithm in PT. """ + def __init__( self, target_model: NNCFNetwork, diff --git a/nncf/torch/sparsity/__init__.py b/nncf/torch/sparsity/__init__.py index 5de73dd0a87..3a0b0fe26b2 100644 --- a/nncf/torch/sparsity/__init__.py +++ b/nncf/torch/sparsity/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementations of sparsity algorithms. -""" \ No newline at end of file +""" diff --git a/nncf/torch/sparsity/base_algo.py b/nncf/torch/sparsity/base_algo.py index 24158f82e01..b9df542a4a3 100644 --- a/nncf/torch/sparsity/base_algo.py +++ b/nncf/torch/sparsity/base_algo.py @@ -100,6 +100,7 @@ class BaseSparsityAlgoController(PTCompressionAlgorithmController, SparsityContr """ Base class for sparsity algorithm controllers in PT. """ + def __init__(self, target_model: NNCFNetwork, sparsified_module_info: List[SparseModuleInfo]): super().__init__(target_model) self._loss = ZeroCompressionLoss(get_model_device(target_model)) diff --git a/nncf/torch/sparsity/const/__init__.py b/nncf/torch/sparsity/const/__init__.py index faab9592f04..39635d41f85 100644 --- a/nncf/torch/sparsity/const/__init__.py +++ b/nncf/torch/sparsity/const/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementation of the auxiliary "constant" sparsity algorithm. -""" \ No newline at end of file +""" diff --git a/nncf/torch/sparsity/const/algo.py b/nncf/torch/sparsity/const/algo.py index fb456010f6e..682d1b600e1 100644 --- a/nncf/torch/sparsity/const/algo.py +++ b/nncf/torch/sparsity/const/algo.py @@ -40,6 +40,7 @@ class ConstSparsityController(BaseSparsityAlgoController): """ Controller for the auxiliary constant sparsity algorithm in PT. """ + def freeze(self): pass diff --git a/nncf/torch/sparsity/magnitude/__init__.py b/nncf/torch/sparsity/magnitude/__init__.py index a537f14c33a..1ad0ebb0026 100644 --- a/nncf/torch/sparsity/magnitude/__init__.py +++ b/nncf/torch/sparsity/magnitude/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementation of magnitude sparsity algorithm. -""" \ No newline at end of file +""" diff --git a/nncf/torch/sparsity/magnitude/algo.py b/nncf/torch/sparsity/magnitude/algo.py index fdf147c5ded..64a5d40f89e 100644 --- a/nncf/torch/sparsity/magnitude/algo.py +++ b/nncf/torch/sparsity/magnitude/algo.py @@ -56,6 +56,7 @@ class MagnitudeSparsityController(BaseSparsityAlgoController): """ Controller for the magnitude sparsity algorithm in PT. """ + def __init__(self, target_model: NNCFNetwork, sparsified_module_info: List[SparseModuleInfo], config: NNCFConfig): super().__init__(target_model, sparsified_module_info) self._config = config diff --git a/nncf/torch/sparsity/rb/__init__.py b/nncf/torch/sparsity/rb/__init__.py index dd3f0f330db..6ce683c2d15 100644 --- a/nncf/torch/sparsity/rb/__init__.py +++ b/nncf/torch/sparsity/rb/__init__.py @@ -10,4 +10,4 @@ # limitations under the License. """ Backend-specific implementations of regularization-based (RB) sparsity. -""" \ No newline at end of file +""" diff --git a/nncf/torch/sparsity/rb/algo.py b/nncf/torch/sparsity/rb/algo.py index e6884f7b50a..dfaa3151a26 100644 --- a/nncf/torch/sparsity/rb/algo.py +++ b/nncf/torch/sparsity/rb/algo.py @@ -59,6 +59,7 @@ class RBSparsityController(BaseSparsityAlgoController): """ Controller for the regularization-based (RB) sparsity algorithm in PT. """ + def __init__(self, target_model: NNCFNetwork, sparsified_module_info: List[SparseModuleInfo], config: NNCFConfig): super().__init__(target_model, sparsified_module_info) algo_config = extract_algo_specific_config(config, "rb_sparsity") diff --git a/nncf/torch/structures.py b/nncf/torch/structures.py index f9af6b437be..2cb63c1b4ce 100644 --- a/nncf/torch/structures.py +++ b/nncf/torch/structures.py @@ -59,7 +59,7 @@ def __init__( self.data_loader = data_loader self.device = device - @classmethod + @classmethod def get_id(cls) -> str: return "quantization_precision_init_args"