Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ruff version #2623

Merged
merged 9 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.3
rev: v0.3.7
hooks:
- id: ruff

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def run_benchmark(model_path: str, shape=None, verbose: bool = True) -> float:

class COCO128Dataset(torch.utils.data.Dataset):
category_mapping = [
1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,
34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,
61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90
] # fmt: skip

def __init__(self, data_path: str, transform: Callable):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def _mobilenet_v3_model(
):
model = MobileNetV3(inverted_residual_setting, last_channel, **kwargs)
if pretrained:
if model_urls.get(arch, None) is None:
if model_urls.get(arch) is None:
raise ValueError("No checkpoint is available for model type {}".format(arch))
state_dict = load_state_dict_from_url(model_urls[arch], progress=progress)
model.load_state_dict(state_dict)
Expand Down
2 changes: 1 addition & 1 deletion nncf/common/sparsity/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init__(self, controller: SparsityController, params: Dict[str, Any]):
self._update_per_optimizer_step = params.get(
"update_per_optimizer_step", SPARSITY_SCHEDULER_UPDATE_PER_OPTIMIZER_STEP
)
self._steps_per_epoch = params.get("steps_per_epoch", None)
self._steps_per_epoch = params.get("steps_per_epoch")
self._should_skip = False

def step(self, next_step: Optional[int] = None) -> None:
Expand Down
8 changes: 6 additions & 2 deletions nncf/experimental/common/tensor_statistics/collectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,9 @@ def __init__(self, tensor_collectors: List[TensorCollector]) -> None:
self._aggregators[key] = unique_aggregator


##################################################Reducers##################################################
##################################################
# Reducers
##################################################


class NoopReducer(TensorReducerBase):
Expand Down Expand Up @@ -578,7 +580,9 @@ def __hash__(self) -> int:
return hash((self.__class__.__name__, self.inplace, self._reduction_axes, self._channel_axis))


##################################################Aggregators##################################################
##################################################
# Aggregators
##################################################


class NoopAggregator(AggregatorBase):
Expand Down
4 changes: 1 addition & 3 deletions nncf/experimental/torch/nas/bootstrapNAS/search/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,17 +710,15 @@ def _evaluate(self, x: List[float], out: Dict[str, Any], *args, **kargs) -> NoRe

result = [sample]

eval_idx = 0
bn_adaption_executed = False
for evaluator_handler in self._evaluator_handlers:
for eval_idx, evaluator_handler in enumerate(self._evaluator_handlers):
in_cache, value = evaluator_handler.retrieve_from_cache(tuple(x_i))
if not in_cache:
if not bn_adaption_executed and self._search.bn_adaptation is not None:
self._search.bn_adaptation.run(self._model)
bn_adaption_executed = True
value = evaluator_handler.evaluate_and_add_to_cache_from_pymoo(tuple(x_i))
evaluators_arr[eval_idx].append(value)
eval_idx += 1

result.append(evaluator_handler.name)
result.append(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,10 @@ def get_current_stage_desc(self) -> Tuple[Optional[StageDescriptor], int]:
:return: current stage descriptor and its index in the list of all descriptors
"""
partial_epochs = 0
stage_desc_idx = 0
for stage_desc in self.list_stage_descriptors:
for stage_desc_idx, stage_desc in enumerate(self.list_stage_descriptors):
partial_epochs += stage_desc.epochs
if self.current_epoch < partial_epochs:
return stage_desc, stage_desc_idx
stage_desc_idx += 1
return None, -1

def get_total_training_epochs(self) -> int:
Expand Down
10 changes: 5 additions & 5 deletions nncf/experimental/torch/sparsity/movement/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ def from_dict(cls, params: Dict[str, Any]) -> "MovementSchedulerParams":
:param params: A dict that specifies the parameters of movement sparsity scheduler.
:return: A `MovementSchedulerParams` object that stores the parameters from `params`.
"""
warmup_start_epoch: int = params.get("warmup_start_epoch", None)
warmup_end_epoch: int = params.get("warmup_end_epoch", None)
importance_regularization_factor: float = params.get("importance_regularization_factor", None)
warmup_start_epoch: int = params.get("warmup_start_epoch")
warmup_end_epoch: int = params.get("warmup_end_epoch")
importance_regularization_factor: float = params.get("importance_regularization_factor")
enable_structured_masking: bool = params.get("enable_structured_masking", MOVEMENT_ENABLE_STRUCTURED_MASKING)
init_importance_threshold: Optional[float] = params.get("init_importance_threshold", None)
init_importance_threshold: Optional[float] = params.get("init_importance_threshold")
final_importance_threshold: float = params.get(
"final_importance_threshold", MOVEMENT_FINAL_IMPORTANCE_THRESHOLD
)
power: float = params.get("power", MOVEMENT_POWER)
steps_per_epoch: Optional[int] = params.get("steps_per_epoch", None)
steps_per_epoch: Optional[int] = params.get("steps_per_epoch")

if None in [warmup_start_epoch, warmup_end_epoch, importance_regularization_factor]:
raise ValueError(
Expand Down
4 changes: 1 addition & 3 deletions nncf/tensorflow/graph/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,7 @@ def _collect_edge_information(self):
node_name = layer_name
input_shapes = self._node_info[node_name]["input_shapes"]

layer_instance_input_port_id = 0
for inbound_node in inbound_nodes:
for layer_instance_input_port_id, inbound_node in enumerate(inbound_nodes):
producer_layer_name, producer_layer_instance, producer_layer_instance_output_port, _ = inbound_node

if self._is_layer_shared(producer_layer_name):
Expand All @@ -573,7 +572,6 @@ def _collect_edge_information(self):
"to_node_input_port_id": layer_instance_input_port_id,
"from_node_output_port_id": producer_layer_instance_output_port,
}
layer_instance_input_port_id += 1

def convert(self) -> NNCFGraph:
nncf_graph = NNCFGraph()
Expand Down
2 changes: 1 addition & 1 deletion nncf/torch/nested_objects_traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def is_tuple(obj) -> bool:


def is_named_tuple(obj) -> bool:
return is_tuple(obj) and (obj.__class__ != tuple)
return is_tuple(obj) and (obj.__class__ is not tuple)


def maybe_get_iterator(obj):
Expand Down
6 changes: 3 additions & 3 deletions nncf/torch/quantization/algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def _parse_range_init_params(self) -> Optional[PTRangeInitParams]:
return PTRangeInitParams(**range_init_params) if range_init_params is not None else None

def _parse_precision_init_params(self, initializer_config: Dict) -> Tuple[str, BasePrecisionInitParams]:
init_precision_config = initializer_config.get("precision", None)
init_precision_config = initializer_config.get("precision")
if not init_precision_config:
return None, None
precision_init_type = init_precision_config.get("type", "manual")
Expand Down Expand Up @@ -934,7 +934,7 @@ def _build_insertion_commands_list_for_quantizer_setup(

range_init_minmax_values = None
if minmax_values_for_range_init:
minmax_stat = minmax_values_for_range_init[qp_id] if qp_id in minmax_values_for_range_init else None
minmax_stat = minmax_values_for_range_init.get(qp_id)
if minmax_stat is not None:
range_init_minmax_values = (minmax_stat.min_values, minmax_stat.max_values)

Expand Down Expand Up @@ -1084,7 +1084,7 @@ def ip_str_repr_key_lambda(x):
min_values = None
max_values = None
for qp_id in sorted_qp_ids:
minmax_stat = minmax_values_for_range_init[qp_id] if qp_id in minmax_values_for_range_init else None
minmax_stat = minmax_values_for_range_init.get(qp_id)
if minmax_stat is None:
continue

Expand Down
2 changes: 1 addition & 1 deletion nncf/torch/quantization/precision_init/hawq_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def from_config(
return cls(
user_init_args=user_init_args,
bitwidths=hawq_init_config_dict.get("bits", PRECISION_INIT_BITWIDTHS),
traces_per_layer_path=hawq_init_config_dict.get("traces_per_layer_path", None),
traces_per_layer_path=hawq_init_config_dict.get("traces_per_layer_path"),
num_data_points=hawq_init_config_dict.get("num_data_points", HAWQ_NUM_DATA_POINTS),
iter_number=hawq_init_config_dict.get("iter_number", HAWQ_ITER_NUMBER),
tolerance=hawq_init_config_dict.get("tolerance", HAWQ_TOLERANCE),
Expand Down
14 changes: 11 additions & 3 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
line-length = 120
exclude = ["nncf/tensorflow/__init__.py"]

[lint]
preview = true
ignore-init-module-imports = true
ignore = [
"E201", # whitespace-after-open-bracket
"E203", # whitespace-before-punctuation
"E231", # missing-whitespace
"E251", # unexpected-spaces-around-keyword-parameter-equals
"E731", # lambda-assignment
"SIM108", # if-else-block-instead-of-if-exp
"SIM110", # reimplemented-builtin
"SIM117", # multiple-with-statements
"SIM103", # needless-bool
]
select = [
"E", # pycodestyle rules
Expand All @@ -14,17 +23,16 @@ select = [
extend-select = [
"SIM", # https://pypi.org/project/flake8-simplify
]
exclude = ["nncf/tensorflow/__init__.py"]

[per-file-ignores]
[lint.per-file-ignores]
"nncf/experimental/torch/nas/bootstrapNAS/__init__.py" = ["F401"]
"nncf/torch/__init__.py" = ["F401", "E402"]
"tests/**/*.py" = ["F403"]
"tests/**/__init__.py" = ["F401"]
"examples/**/*.py" = ["F403"]


[flake8-copyright]
[lint.flake8-copyright]
notice-rgx = """\
# Copyright \\(c\\) (\\d{4}|\\d{4}-\\d{4}) Intel Corporation
# Licensed under the Apache License, Version 2.0 \\(the "License"\\);
Expand Down
2 changes: 1 addition & 1 deletion tests/onnx/benchmarking/ac_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _read_image_annotation(image, annotations, label_id_to_label):

@staticmethod
def convert_to_voc(image_labels):
return [COCO_TO_VOC[label] if label in COCO_TO_VOC else 0 for label in image_labels]
return [COCO_TO_VOC.get(label, 0) for label in image_labels]


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/openvino/tools/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ def main():
"quantize_with_accuracy_control": quantize_model_with_accuracy_control,
}
for algo_name, algo_config in nncf_algorithms_config.items():
algo_fn = algo_name_to_method_map.get(algo_name, None)
algo_fn = algo_name_to_method_map.get(algo_name)
if algo_fn:
quantize_model_arguments = {
"xml_path": xml_path,
Expand Down
2 changes: 1 addition & 1 deletion tests/openvino/tools/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def _configure_ac_params(self):
ac_conf = ConfigReader.convert_paths(ac_conf)
ConfigReader._filter_launchers(ac_conf, filtering_params, mode=mode)
for req_num in ["stat_requests_number", "eval_requests_number"]:
ac_conf[req_num] = self.engine[req_num] if req_num in self.engine else None
ac_conf[req_num] = self.engine.get(req_num, None)

self["engine"] = ac_conf

Expand Down
4 changes: 1 addition & 3 deletions tests/shared/test_templates/template_test_nncf_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,9 @@ def test_getitem(self):
def test_iter(self):
arr = [0, 1, 2]
nncf_tensor = Tensor(self.to_tensor(arr))
i = 0
for x in nncf_tensor:
for i, x in enumerate(nncf_tensor):
assert x == arr[i]
assert isinstance(x, Tensor)
i += 1

# Math

Expand Down
2 changes: 1 addition & 1 deletion tests/torch/nas/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def validate_model_fn(model, eval_datasets):
)
max_subnetwork_acc = validate_model_fn(model, eval_datasets)

_, best_config, performance_metrics = search.run(validate_model_fn, eval_datasets, tmp_path)
_, _, performance_metrics = search.run(validate_model_fn, eval_datasets, tmp_path)

assert max_subnetwork_acc == search_result_descriptors.expected_accuracy
assert performance_metrics[1] == search_result_descriptors.subnet_expected_accuracy[search_algo_name]
Expand Down
8 changes: 3 additions & 5 deletions tests/torch/qat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ def get_quantization_preset(config_quantization_params: Dict[str, Any]) -> Optio
def get_advanced_ptq_parameters(config_quantization_params: Dict[str, Any]) -> AdvancedQuantizationParameters:
range_estimator_params = get_range_init_type(config_quantization_params)
return AdvancedQuantizationParameters(
overflow_fix=convert_overflow_fix_param(config_quantization_params.get("overflow_fix", None)),
weights_quantization_params=convert_quantization_params(config_quantization_params.get("weights", None)),
activations_quantization_params=convert_quantization_params(
config_quantization_params.get("activations", None)
),
overflow_fix=convert_overflow_fix_param(config_quantization_params.get("overflow_fix")),
weights_quantization_params=convert_quantization_params(config_quantization_params.get("weights")),
activations_quantization_params=convert_quantization_params(config_quantization_params.get("activations")),
weights_range_estimator_params=range_estimator_params,
activations_range_estimator_params=range_estimator_params,
)
Expand Down
4 changes: 2 additions & 2 deletions tests/torch/qat/test_qat_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def train(
datasets.train_data_loader.sampler.set_epoch(epoch)

logger.info(">>>> [Epoch: {0:d}] Validation".format(epoch))
loss, (iou, current_miou) = val_obj.run_epoch(config.print_step)
_, (_, current_miou) = val_obj.run_epoch(config.print_step)
# best_metric = max(current_miou, best_metric)
acc_drop = original_metric - current_miou
best_miou = max(current_miou, best_miou)
Expand All @@ -225,7 +225,7 @@ def train(
return acc_drop

logger.info(">>>> [Epoch: {0:d}] Training".format(epoch))
epoch_loss, (iou, miou) = train_obj.run_epoch(config.print_step)
epoch_loss, (_, miou) = train_obj.run_epoch(config.print_step)

logger.info(">>>> [Epoch: {0:d}] Avg. loss: {1:.4f} | Mean IoU: {2:.4f}".format(epoch, epoch_loss, miou))

Expand Down
2 changes: 1 addition & 1 deletion tests/torch/quantization/quantization_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get_squeezenet_quantization_config(image_size=32, batch_size=3):

def distributed_init_test_default(gpu, ngpus_per_node, config):
config.batch_size = 3
config.workers = 0 # workaround for the pytorch multiprocessingdataloader issue/
config.workers = 0 # workaround for the pytorch multiprocessingdataloader issue/
config.gpu = gpu
config.ngpus_per_node = ngpus_per_node
config.rank = gpu
Expand Down
5 changes: 1 addition & 4 deletions tests/torch/sparsity/magnitude/test_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,15 @@ def test_can_create_magnitude_sparse_algo__with_defaults():

_, sparse_model_conv = check_correct_nncf_modules_replacement(model, sparse_model)

i = 0

nncf_stats = compression_ctrl.statistics()
for layer_info in nncf_stats.magnitude_sparsity.thresholds:
assert layer_info.threshold == approx(0.24, 0.1)

assert isinstance(compression_ctrl._weight_importance_fn, type(normed_magnitude))

for sparse_module in sparse_model_conv.values():
for i, sparse_module in enumerate(sparse_model_conv.values()):
store = []
ref_mask = torch.ones_like(sparse_module.weight) if i == 0 else ref_mask_2
i += 1
AlexanderDokuchaev marked this conversation as resolved.
Show resolved Hide resolved
for op in sparse_module.pre_ops.values():
if isinstance(op, UpdateWeight) and isinstance(op.operand, BinaryMask):
assert torch.allclose(op.operand.binary_mask, ref_mask)
Expand Down
Loading
Loading