Skip to content

Commit

Permalink
conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDokuchaev committed Apr 18, 2024
2 parents a7878a5 + ff0c070 commit 3a74dce
Show file tree
Hide file tree
Showing 30 changed files with 227 additions and 101 deletions.
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
24 changes: 24 additions & 0 deletions nncf/config/schemata/algo/filter_pruning.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
from nncf.config.schemata.defaults import PRUNING_INTERLAYER_RANKING_TYPE
from nncf.config.schemata.defaults import PRUNING_LEGR_GENERATIONS
from nncf.config.schemata.defaults import PRUNING_LEGR_MAX_PRUNING
from nncf.config.schemata.defaults import PRUNING_LEGR_MUTATE_PERCENT
from nncf.config.schemata.defaults import PRUNING_LEGR_NUM_SAMPLES
from nncf.config.schemata.defaults import PRUNING_LEGR_POPULATION_SIZE
from nncf.config.schemata.defaults import PRUNING_LEGR_RANDOM_SEED
from nncf.config.schemata.defaults import PRUNING_LEGR_SIGMA_SCALE
from nncf.config.schemata.defaults import PRUNING_LEGR_TRAIN_STEPS
from nncf.config.schemata.defaults import PRUNING_NUM_INIT_STEPS
from nncf.config.schemata.defaults import PRUNING_SCHEDULE
Expand Down Expand Up @@ -162,6 +166,26 @@
description="Random seed for LeGR coefficients generation.",
default=PRUNING_LEGR_RANDOM_SEED,
),
"population_size": with_attributes(
NUMBER,
description="Size of population for the evolution algorithm.",
default=PRUNING_LEGR_POPULATION_SIZE,
),
"num_samples": with_attributes(
NUMBER,
description="Number of samples for the evolution algorithm.",
default=PRUNING_LEGR_NUM_SAMPLES,
),
"mutate_percent": with_attributes(
NUMBER,
description="Percent of mutate for the evolution algorithm.",
default=PRUNING_LEGR_MUTATE_PERCENT,
),
"scale_sigma": with_attributes(
NUMBER,
description="Scale sigma for the evolution algorithm.",
default=PRUNING_LEGR_SIGMA_SCALE,
),
},
"additionalProperties": False,
},
Expand Down
4 changes: 4 additions & 0 deletions nncf/config/schemata/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
PRUNING_LEGR_TRAIN_STEPS = 200
PRUNING_LEGR_MAX_PRUNING = 0.8
PRUNING_LEGR_RANDOM_SEED = 42
PRUNING_LEGR_POPULATION_SIZE = 64
PRUNING_LEGR_NUM_SAMPLES = 16
PRUNING_LEGR_MUTATE_PERCENT = 0.1
PRUNING_LEGR_SIGMA_SCALE = 1

SPARSITY_INIT = 0.0
MAGNITUDE_SPARSITY_WEIGHT_IMPORTANCE = "normed_abs"
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
from torch import optim

from nncf.config.config import NNCFConfig
from nncf.config.schemata.defaults import PRUNING_LEGR_GENERATIONS
from nncf.config.schemata.defaults import PRUNING_LEGR_MUTATE_PERCENT
from nncf.config.schemata.defaults import PRUNING_LEGR_NUM_SAMPLES
from nncf.config.schemata.defaults import PRUNING_LEGR_POPULATION_SIZE
from nncf.config.schemata.defaults import PRUNING_LEGR_SIGMA_SCALE
from nncf.torch.utils import get_filters_num


Expand Down Expand Up @@ -48,11 +53,11 @@ def __init__(self, initial_filter_norms: Dict, hparams: Dict, random_seed: int):
"""
self.random_seed = random_seed
# Optimizer hyper-params
self.population_size = hparams.get("population_size", 64)
self.num_generations = hparams.get("num_generations", 400)
self.num_samples = hparams.get("num_samples", 16)
self.mutate_percent = hparams.get("mutate_percent", 0.1)
self.scale_sigma = hparams.get("sigma_scale", 1)
self.population_size = hparams.get("population_size", PRUNING_LEGR_POPULATION_SIZE)
self.num_generations = hparams.get("num_generations", PRUNING_LEGR_GENERATIONS)
self.num_samples = hparams.get("num_samples", PRUNING_LEGR_NUM_SAMPLES)
self.mutate_percent = hparams.get("mutate_percent", PRUNING_LEGR_MUTATE_PERCENT)
self.scale_sigma = hparams.get("sigma_scale", PRUNING_LEGR_SIGMA_SCALE)
self.max_reward = -np.inf
self.mean_rewards = []

Expand Down
20 changes: 19 additions & 1 deletion nncf/torch/pruning/filter_pruning/global_ranking/legr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
from nncf.common.logging import nncf_logger
from nncf.config.schemata.defaults import PRUNING_LEGR_GENERATIONS
from nncf.config.schemata.defaults import PRUNING_LEGR_MAX_PRUNING
from nncf.config.schemata.defaults import PRUNING_LEGR_MUTATE_PERCENT
from nncf.config.schemata.defaults import PRUNING_LEGR_NUM_SAMPLES
from nncf.config.schemata.defaults import PRUNING_LEGR_POPULATION_SIZE
from nncf.config.schemata.defaults import PRUNING_LEGR_RANDOM_SEED
from nncf.config.schemata.defaults import PRUNING_LEGR_SIGMA_SCALE
from nncf.config.schemata.defaults import PRUNING_LEGR_TRAIN_STEPS
from nncf.torch.pruning.filter_pruning.global_ranking.evolutionary_optimization import EvolutionOptimizer
from nncf.torch.pruning.filter_pruning.global_ranking.evolutionary_optimization import LeGREvolutionEnv
Expand All @@ -38,6 +42,10 @@ def __init__(
generations: int = PRUNING_LEGR_GENERATIONS,
max_pruning: float = PRUNING_LEGR_MAX_PRUNING,
random_seed: int = PRUNING_LEGR_RANDOM_SEED,
population_size: int = PRUNING_LEGR_POPULATION_SIZE,
num_samples: int = PRUNING_LEGR_NUM_SAMPLES,
mutate_percent: float = PRUNING_LEGR_MUTATE_PERCENT,
scale_sigma: float = PRUNING_LEGR_SIGMA_SCALE,
):
"""
Initializing all necessary structures for optimization- LeGREvolutionEnv environment and EvolutionOptimizer
Expand All @@ -53,10 +61,20 @@ def __init__(
self.num_generations = generations
self.max_pruning = max_pruning
self.train_steps = train_steps
self.population_size = population_size
self.num_samples = num_samples
self.mutate_percent = mutate_percent
self.scale_sigma = scale_sigma

self.pruner = LeGRPruner(pruning_ctrl, target_model)
init_filter_norms = self.pruner.init_filter_norms
agent_hparams = {"num_generations": self.num_generations}
agent_hparams = {
"num_generations": self.num_generations,
"population_size": self.population_size,
"num_samples": self.num_samples,
"mutate_percent": self.mutate_percent,
"sigma_scale": self.scale_sigma,
}
self.agent = EvolutionOptimizer(init_filter_norms, agent_hparams, random_seed)
self.env = LeGREvolutionEnv(
self.pruner,
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
4 changes: 2 additions & 2 deletions tests/cross_fw/examples/example_scope.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"cpu": "Intel(R) Core(TM) i9-10980XE CPU @ 3.00GHz",
"accuracy_metrics": {
"fp32_mAP": 0.45252755065175254,
"int8_mAP": 0.4425154975267813,
"accuracy_drop": 0.01001205312497122
"int8_mAP": 0.4561537594798616,
"accuracy_drop": -0.00362620882
},
"performance_metrics": {
"fp32_fps": 170.69,
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
Loading

0 comments on commit 3a74dce

Please sign in to comment.