Skip to content

Commit

Permalink
ruff format (#3308)
Browse files Browse the repository at this point in the history
### Changes

Use [ruff format](https://docs.astral.sh/ruff/formatter/) instead of
black

Deviations from black: https://docs.astral.sh/ruff/formatter/black/
  • Loading branch information
AlexanderDokuchaev authored Feb 28, 2025
1 parent f43ac70 commit bf9fe48
Show file tree
Hide file tree
Showing 100 changed files with 212 additions and 276 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ ENV/
# snapshots
*.tar

# pylint config is left at dev's discretion, CI uses ruff/isort/black instead
# pylint config is left at dev's discretion, CI uses ruff instead
.pylintrc

# VSCode
Expand Down
8 changes: 2 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ repos:
hooks:
- id: ruff
args: [--fix, --show-fixes]

- repo: https://github.com/psf/black
rev: 24.10.0
hooks:
- id: black
files: '^.*\.py'
- id: ruff-format
types_or: [ python, pyi ]

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.43.0
Expand Down
2 changes: 0 additions & 2 deletions custom_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
This ensures that `nncf/version.py` remains in its original state after the dynamic versioning process.
"""


from __future__ import annotations

import contextlib
Expand Down Expand Up @@ -98,7 +97,6 @@ def get_custom_version() -> str:


def __getattr__(name: str) -> str:

if name == "version":
global version
version = get_custom_version()
Expand Down
10 changes: 5 additions & 5 deletions docs/styleguide/PyGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ the [PEP 8 -- Style Guide for Python Code](https://www.python.org/dev/peps/pep-0

## 2 Automating Code Formatting

To maintain consistency and readability throughout the codebase, we use the [black](https://github.com/psf/black)
and [ruff](https://docs.astral.sh/ruff/) tools for formatting. Before committing any changes,
it's important to run a pre-commit command to ensure that the code is properly formatted.
To maintain consistency and readability throughout the codebase, we use the [ruff](https://docs.astral.sh/ruff/)
tool for formatting. Before committing any changes, it's important to run a pre-commit command to ensure
that the code is properly formatted.
You can use the following commands for this:

```bash
make pre-commit
```

Also recommend configuring your IDE to run Black and Ruff tools automatically when saving files.
Also recommend configuring your IDE to run Ruff tools automatically when saving files.

Automatic code formatting is mandatory for all Python files, but you can disable it for specific cases if required:

Expand All @@ -87,7 +87,7 @@ import b
import a
```

Example for 'black':
Example for 'ruff format':

```python
arr1 = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def print_results(optimized_model: ov.Model, similarity: float) -> None:
else:
print(best_params_info)
footprint = Path(MODEL_PATH).with_suffix(".bin").stat().st_size
print(f"Memory footprint: {footprint / 2**20 :.2f} MB")
print(f"Memory footprint: {footprint / 2**20:.2f} MB")
print(f"Similarity: {similarity:.2f}")


Expand Down
5 changes: 2 additions & 3 deletions examples/tensorflow/object_detection/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,8 @@ def run(config):
# Create dataset
train_builder, test_builder = get_dataset_builders(config, strategy.num_replicas_in_sync)
train_dataset, test_dataset = train_builder.build(), test_builder.build()
train_dist_dataset, test_dist_dataset = strategy.experimental_distribute_dataset(
train_dataset
), strategy.experimental_distribute_dataset(test_dataset)
train_dist_dataset = strategy.experimental_distribute_dataset(train_dataset)
test_dist_dataset = strategy.experimental_distribute_dataset(test_dataset)

# Training parameters
epochs = config.epochs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ def __init__(
replace_stride_with_dilation = [False, False, False]
if len(replace_stride_with_dilation) != 3:
msg = (
"replace_stride_with_dilation should be None "
f"or a 3-element tuple, got {replace_stride_with_dilation}"
f"replace_stride_with_dilation should be None or a 3-element tuple, got {replace_stride_with_dilation}"
)
raise ValueError(msg)
self.groups = groups
Expand Down
12 changes: 6 additions & 6 deletions examples/torch/semantic_segmentation/metric/confusionmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ def add(self, predicted, target):
assert predicted.shape[0] == target.shape[0], "number of targets and predicted outputs do not match"

if np.ndim(predicted) != 1:
assert (
predicted.shape[1] == self.num_classes
), "number of predictions does not match size of confusion matrix"
assert predicted.shape[1] == self.num_classes, (
"number of predictions does not match size of confusion matrix"
)
predicted = np.argmax(predicted, 1)
else:
assert (predicted.max() < self.num_classes) and (
predicted.min() >= 0
), "predicted values are not between 0 and k-1"
assert (predicted.max() < self.num_classes) and (predicted.min() >= 0), (
"predicted values are not between 0 and k-1"
)

if np.ndim(target) != 1:
assert target.shape[1] == self.num_classes, "Onehot target does not match size of confusion matrix"
Expand Down
6 changes: 3 additions & 3 deletions examples/torch/semantic_segmentation/metric/iou.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def add(self, predicted, target):
"""
# Dimensions check
assert predicted.size(0) == target.size(0), "number of targets and predicted outputs do not match"
assert (
predicted.dim() == 3 or predicted.dim() == 4
), "predictions must be of dimension (N, H, W) or (N, K, H, W)"
assert predicted.dim() == 3 or predicted.dim() == 4, (
"predictions must be of dimension (N, H, W) or (N, K, H, W)"
)
assert target.dim() == 3 or target.dim() == 4, "targets must be of dimension (N, H, W) or (N, K, H, W)"

# If the tensor is in categorical format convert it to integer format
Expand Down
11 changes: 4 additions & 7 deletions nncf/common/accuracy_aware_training/training_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""
Implementations of training loops to be used for accuracy aware training.
"""

import pathlib
from abc import ABC
from abc import abstractmethod
Expand Down Expand Up @@ -321,10 +322,7 @@ def __init__(
super().__init__(compression_controller)
self.adaptive_controller = self._get_adaptive_compression_ctrl(compression_controller)
if self.adaptive_controller is None:
msg = (
"No compression algorithm supported by the accuracy-aware training "
"runner was specified in the config"
)
msg = "No compression algorithm supported by the accuracy-aware training runner was specified in the config"
raise nncf.InternalError(msg)

maximal_compression_rate = min(maximal_compression_rate, self.adaptive_controller.maximal_compression_rate)
Expand Down Expand Up @@ -606,9 +604,8 @@ def _interpolate_compression_step_update(
nncf_logger.info(f"Compressed training history: {training_history}")
training_history[minimal_compression_rate] = runner.maximal_accuracy_drop # type: ignore
training_history[maximal_compression_rate] = -full_compression_factor * runner.maximal_accuracy_drop # type: ignore
compression_rates, evaluated_acc_budgets = cast(List[float], training_history.keys()), cast(
List[float], training_history.values()
)
compression_rates = cast(List[float], training_history.keys())
evaluated_acc_budgets = cast(List[float], training_history.values())
interp_kind = "linear" if len(compression_rates) < 4 else "cubic"
acc_budget_vs_comp_rate_curve = interp1d(compression_rates, evaluated_acc_budgets, kind=interp_kind)
rate_interval = np.linspace(
Expand Down
1 change: 0 additions & 1 deletion nncf/common/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def deprecated(
"""

def decorator(obj: TObj) -> TObj:

if isinstance(obj, types.FunctionType):

@wraps(obj)
Expand Down
1 change: 1 addition & 0 deletions nncf/common/initialization/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Interface for user-defined data usage during the compression algorithm initialization process."""

from abc import ABC
from abc import abstractmethod
from typing import Any, Iterator
Expand Down
3 changes: 2 additions & 1 deletion nncf/common/pruning/model_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def get_position(nodes_list: List[NNCFNode], idx: int) -> Optional[int]:


def merge_clusters_for_nodes(
nodes_to_merge: List[NNCFNode], clusterization: Clusterization # type:ignore[type-arg]
nodes_to_merge: List[NNCFNode],
clusterization: Clusterization, # type:ignore[type-arg]
) -> None:
"""
Merges clusters to which nodes from nodes_to_merge belongs.
Expand Down
13 changes: 8 additions & 5 deletions nncf/common/pruning/node_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ def _check_all_closing_nodes_are_feasible(
return can_prune_updated

def _check_internal_groups_dim(
self, pruned_nodes_clusterization: Clusterization # type: ignore[type-arg]
self,
pruned_nodes_clusterization: Clusterization, # type: ignore[type-arg]
) -> Dict[int, PruningAnalysisDecision]:
"""
Checks pruning dimensions of all nodes in each cluster group are equal and
Expand Down Expand Up @@ -310,7 +311,9 @@ def _should_prune_groups_analysis(
return can_prune_updated

def _filter_groups(
self, pruned_nodes_clusterization: Clusterization, can_prune: Dict[int, PruningAnalysisDecision] # type: ignore[type-arg]
self,
pruned_nodes_clusterization: Clusterization, # type: ignore[type-arg]
can_prune: Dict[int, PruningAnalysisDecision],
) -> None:
"""
Check whether all nodes in group can be pruned based on user-defined constraints and
Expand All @@ -332,12 +335,12 @@ def _filter_groups(
cannot_prune_messages.append(message)

nncf_logger.debug(
f'Could not prune node group [{", ".join(nodes_names)}], '
f'reason: {", ".join(cannot_prune_messages)}.'
f"Could not prune node group [{', '.join(nodes_names)}], "
f"reason: {', '.join(cannot_prune_messages)}."
)
pruned_nodes_clusterization.delete_cluster(cluster.id)
else:
nncf_logger.debug(f'Node group [{", ".join(nodes_names)}] will be pruned together.')
nncf_logger.debug(f"Node group [{', '.join(nodes_names)}] will be pruned together.")

def _is_module_prunable(self, graph: NNCFGraph, node: NNCFNode) -> PruningAnalysisDecision:
"""
Expand Down
6 changes: 3 additions & 3 deletions nncf/common/pruning/symbolic_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def merge_producers(cls, masks: List["SymbolicMask"]) -> List["SymbolicMaskProdu
for mask in masks:
for mask_producer in mask.mask_producers:
if mask_producer.id in merged_producers:
assert (
mask_producer.sparse_multiplier == merged_producers[mask_producer.id].sparse_multiplier
), f"Inconsistent sparse multiplier for NNCF node with id={mask_producer.id}"
assert mask_producer.sparse_multiplier == merged_producers[mask_producer.id].sparse_multiplier, (
f"Inconsistent sparse multiplier for NNCF node with id={mask_producer.id}"
)
merged_producers.update({p.id: p for p in mask.mask_producers})
return list(merged_producers.values())

Expand Down
6 changes: 3 additions & 3 deletions nncf/common/pruning/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ def wrap(obj: TObj) -> TObj:
if name not in self._op_name_to_op_class:
self._op_name_to_op_class[name] = obj
else:
assert (
self._op_name_to_op_class[name] == obj
), "Inconsistent operator type registry - single patched op name maps to multiple metatypes!"
assert self._op_name_to_op_class[name] == obj, (
"Inconsistent operator type registry - single patched op name maps to multiple metatypes!"
)
return obj

return wrap
Expand Down
5 changes: 3 additions & 2 deletions nncf/common/quantization/quantizer_propagation/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def get_node_keys_by_metatype(self, metatype: Type[OperatorMetatype]) -> List[st

@staticmethod
def _insertion_point_to_quant_insertion_point(
ip: Union[PreHookInsertionPoint, PostHookInsertionPoint]
ip: Union[PreHookInsertionPoint, PostHookInsertionPoint],
) -> QuantizationInsertionPointBase:
if isinstance(ip, PreHookInsertionPoint):
return ActivationQuantizationInsertionPoint(ip.target_node_name, input_port_id=ip.input_port_id)
Expand Down Expand Up @@ -1389,7 +1389,8 @@ def create_quantizer_setup(
)
for pq_set in pq_sets_grouped_by_unified_scale:
setup.register_unified_scale_group_with_types(
[pqid_vs_qpid[pq.id] for pq in pq_set], [pq.unified_scale_type for pq in pq_set] # type: ignore
[pqid_vs_qpid[pq.id] for pq in pq_set],
[pq.unified_scale_type for pq in pq_set], # type: ignore
)

setup = self._handle_output_quantizers_for_weights_as_outputs_ops(setup, pqid_vs_qpid, wao_op_node_key_vs_wq_id)
Expand Down
14 changes: 7 additions & 7 deletions nncf/common/quantization/quantizer_propagation/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,9 +889,8 @@ def _get_trait_for_op_meta_not_specified_in_hw_config(self, op_meta: Type[Operat
def _get_operator_qconfigs_map(self) -> Dict[Type[OperatorMetatype], Optional[List[QuantizerConfig]]]:
# TODO (vshampor): ensure that there are no name collisions between ops in different torch subpackages
# with the same name
retval: Dict[Type[OperatorMetatype], Optional[List[QuantizerConfig]]] = (
{}
) # Metas not in retval will correspond to wildcard quantization
# Metas not in retval will correspond to wildcard quantization
retval: Dict[Type[OperatorMetatype], Optional[List[QuantizerConfig]]] = {}
if self._hw_config is None:
for trait, meta_list in self._default_trait_to_metatype_map.items():
if trait == QuantizationTrait.INPUTS_QUANTIZABLE:
Expand Down Expand Up @@ -1139,9 +1138,9 @@ def _setup_initial_quantizers_for_operator_node(
for pred_ip_key in preds:
pred_node = quant_prop_graph.nodes[pred_ip_key]
pred_node_type = pred_node[QuantizerPropagationStateGraph.NODE_TYPE_NODE_ATTR]
assert QuantizerPropagationStateGraph.is_insertion_point(
pred_node_type
), "Invalid insertion point graph supplied for quantizer propagation!"
assert QuantizerPropagationStateGraph.is_insertion_point(pred_node_type), (
"Invalid insertion point graph supplied for quantizer propagation!"
)

ip = pred_node[QuantizerPropagationStateGraph.QUANT_INSERTION_POINT_DATA_NODE_ATTR]
input_port_id = ip.input_port_id
Expand Down Expand Up @@ -1457,7 +1456,8 @@ def compatible_wo_requant(qconf: QuantizerConfig, other_qconf_list: List[Quantiz

merged_qconfig_list_counter = Counter(merged_qconfig_list)
resulting_branch_qconfig_lists: List[List[QuantizerConfig]] = [
None for _ in potential_qconfigs_for_each_branch # type: ignore[misc]
None # type: ignore[misc]
for _ in potential_qconfigs_for_each_branch
]

if self._propagation_strategy == QuantizerPropagationRule.MERGE_WITH_POTENTIAL_REQUANTIZATION:
Expand Down
3 changes: 1 addition & 2 deletions nncf/common/scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ def check_scopes_in_graph(

if not_matched_ignored_scopes or not_matched_target_scopes:
err_message = (
"No match has been found among the model operations "
"for the following ignored/target scope definitions:\n"
"No match has been found among the model operations for the following ignored/target scope definitions:\n"
)
if not_matched_ignored_scopes:
err_message += f" - ignored_scope: {not_matched_ignored_scopes}\n"
Expand Down
6 changes: 2 additions & 4 deletions nncf/common/sparsity/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def to_str(self) -> str:
)

pretty_string = (
f"Statistics of the sparsified model:\n{model_string}\n\n"
f"Statistics by sparsified layers:\n{layers_string}"
f"Statistics of the sparsified model:\n{model_string}\n\nStatistics by sparsified layers:\n{layers_string}"
)
return pretty_string

Expand Down Expand Up @@ -231,7 +230,6 @@ def to_str(self) -> str:
)

pretty_string = (
f"{self.model_statistics.to_str()}\n\n"
f"Statistics of the movement-sparsity algorithm:\n{algorithm_string}"
f"{self.model_statistics.to_str()}\n\nStatistics of the movement-sparsity algorithm:\n{algorithm_string}"
)
return pretty_string
1 change: 1 addition & 0 deletions nncf/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
"""
NNCF configuration file schemata and associated structures.
"""

from nncf.config.config import NNCFConfig as NNCFConfig
5 changes: 1 addition & 4 deletions nncf/config/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ def extract_algo_specific_config(config: NNCFConfig, algo_name_to_match: str) ->
matches.append(compression_algo_dict)

if len(matches) > 1:
msg = (
f"Multiple algorithm configurations specified for the same "
f"algo {algo_name_to_match} in the NNCF config!"
)
msg = f"Multiple algorithm configurations specified for the same algo {algo_name_to_match} in the NNCF config!"
raise nncf.ValidationError(msg)
if not matches:
msg = f"Did not find an algorithm configuration for algo {algo_name_to_match} in the NNCF config!"
Expand Down
3 changes: 1 addition & 2 deletions nncf/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ def validate_single_compression_algo_schema(
)
if algo_name in ALGO_NAME_VS_README_URL:
e.message += (
f"or to the algorithm documentation for examples of the configs: "
f"{ALGO_NAME_VS_README_URL[algo_name]}"
f"or to the algorithm documentation for examples of the configs: {ALGO_NAME_VS_README_URL[algo_name]}"
)
raise e

Expand Down
6 changes: 2 additions & 4 deletions nncf/config/schemata/algo/quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,7 @@
"items": [NUMBER, STRING],
"description": "A tuple of a bitwidth and a scope of the quantizer to assign the bitwidth to.",
},
"description": "Manual settings for the quantizer bitwidths. Scopes are used to identify "
"the quantizers.",
"description": "Manual settings for the quantizer bitwidths. Scopes are used to identify the quantizers.",
"examples": [
[
[2, "ResNet/NNCFConv2d[conv1]/conv2d_0|WEIGHT"],
Expand Down Expand Up @@ -463,8 +462,7 @@
),
"quantize_inputs": with_attributes(
BOOLEAN,
description="Whether the model inputs should be immediately quantized prior "
"to any other model operations.",
description="Whether the model inputs should be immediately quantized prior to any other model operations.",
default=QUANTIZE_INPUTS,
),
"quantize_outputs": with_attributes(
Expand Down
Loading

0 comments on commit bf9fe48

Please sign in to comment.