Skip to content

Commit

Permalink
Merge branch 'develop' into ad/e2e_convert_model
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDokuchaev committed Oct 12, 2023
2 parents 8d1beb2 + d516321 commit 71e553d
Show file tree
Hide file tree
Showing 471 changed files with 207,673 additions and 2,926 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ repos:
- id: isort
name: isort (python)

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

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.33.0
hooks:
Expand Down
3 changes: 2 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ disable = arguments-differ,
duplicate-code,
consider-using-f-string,
logging-fstring-interpolation,
cyclic-import
cyclic-import,
useless-import-alias

max-line-length = 120
ignore-docstrings = yes
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,12 @@ Refer to the [CONTRIBUTING.md](./CONTRIBUTING.md) file for guidelines on contrib
- [Notebooks](https://github.com/openvinotoolkit/openvino_notebooks#-model-training)
- [HuggingFace Optimum Intel](https://huggingface.co/docs/optimum/intel/optimization_ov)
- [OpenVINO Model Optimization Guide](https://docs.openvino.ai/latest/openvino_docs_model_optimization_guide.html)

## Telemetry

NNCF as part of the OpenVINO™ toolkit collects anonymous usage data for the purpose of improving OpenVINO™ tools.
You can opt-out at any time by running the following command in the Python environment where you have NNCF installed:

`opt_in_out --opt_out`

More information is available at https://docs.openvino.ai/latest/openvino_docs_telemetry_information.html.
3 changes: 2 additions & 1 deletion docs/api/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def collect_api_entities() -> APIInfo:
"""
retval = APIInfo()
modules = {}
skipped_modules = {} # type: Dict[str, str]
skipped_modules: Dict[str, str] = {}
import nncf

for _, modname, _ in pkgutil.walk_packages(path=nncf.__path__, prefix=nncf.__name__ + ".", onerror=lambda x: None):
Expand Down Expand Up @@ -135,6 +135,7 @@ def collect_api_entities() -> APIInfo:
"onnxruntime",
"openvino",
"tensorflow",
"keras",
"tensorflow_addons",
# Need add backend implementation functions to avoid endless loops on registered functions by mock module,
"nncf.experimental.tensor.torch_functions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,19 @@ def _preprocess_true_boxes(
true_boxes[..., 2:4] = boxes_wh / input_shape[::-1]

batch_size = true_boxes.shape[0]
grid_shapes = [input_shape // {0: 32, 1: 16, 2: 8}[l] for l in range(num_layers)]
grid_shapes = [input_shape // {0: 32, 1: 16, 2: 8}[layer_idx] for layer_idx in range(num_layers)]
y_true = [
np.zeros(
(batch_size, grid_shapes[l][0], grid_shapes[l][1], len(anchor_mask[l]), 5 + num_classes),
(
batch_size,
grid_shapes[layer_idx][0],
grid_shapes[layer_idx][1],
len(anchor_mask[layer_idx]),
5 + num_classes,
),
dtype="float32",
)
for l in range(num_layers)
for layer_idx in range(num_layers)
]

# Expand dim to apply broadcasting.
Expand Down Expand Up @@ -196,22 +202,22 @@ def _preprocess_true_boxes(
best_anchors = np.expand_dims(best_anchors, -1)

for t, row in enumerate(best_anchors):
for l in range(num_layers):
for layer_idx in range(num_layers):
for n in row:
# use different matching policy for single & multi anchor assign
if multi_anchor_assign:
matching_rule = iou[t, n] > iou_thresh and n in anchor_mask[l]
matching_rule = iou[t, n] > iou_thresh and n in anchor_mask[layer_idx]
else:
matching_rule = n in anchor_mask[l]
matching_rule = n in anchor_mask[layer_idx]

if matching_rule:
i = np.floor(true_boxes[b, t, 0] * grid_shapes[l][1]).astype("int32")
j = np.floor(true_boxes[b, t, 1] * grid_shapes[l][0]).astype("int32")
k = anchor_mask[l].index(n)
i = np.floor(true_boxes[b, t, 0] * grid_shapes[layer_idx][1]).astype("int32")
j = np.floor(true_boxes[b, t, 1] * grid_shapes[layer_idx][0]).astype("int32")
k = anchor_mask[layer_idx].index(n)
c = true_boxes[b, t, 4].astype("int32")
y_true[l][b, j, i, k, 0:4] = true_boxes[b, t, 0:4]
y_true[l][b, j, i, k, 4] = 1
y_true[l][b, j, i, k, 5 + c] = 1
y_true[layer_idx][b, j, i, k, 0:4] = true_boxes[b, t, 0:4]
y_true[layer_idx][b, j, i, k, 4] = 1
y_true[layer_idx][b, j, i, k, 5 + c] = 1
return y_true

def _preprocess2(self, image_data, box_data):
Expand Down
10 changes: 10 additions & 0 deletions examples/torch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 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.
2 changes: 1 addition & 1 deletion examples/torch/common/model_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def is_url(uri):
try:
parsed_url = urllib.parse.urlparse(uri)
return parsed_url.scheme and parsed_url.netloc
except: # pylint: disable=bare-except
except: # pylint: disable=bare-except # noqa: E722
return False


Expand Down
10 changes: 10 additions & 0 deletions examples/torch/object_detection/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 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 examples/torch/object_detection/datasets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 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 examples/torch/object_detection/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 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 examples/torch/object_detection/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 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.
26 changes: 26 additions & 0 deletions licensing/third-party-programs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1544,3 +1544,29 @@ rwightman/pytorch-image-models
limitations under the License.

-------------------------------------------------------------

microsoft/Swin-Transformer

MIT License

Copyright (c) Microsoft Corporation.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE

-------------------------------------------------------------
38 changes: 19 additions & 19 deletions nncf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@
Neural Network Compression Framework (NNCF) for enhanced OpenVINO™ inference.
"""

from nncf.common.logging import nncf_logger
from nncf.common.logging.logger import disable_logging
from nncf.common.logging.logger import set_log_level
from nncf.common.strip import strip
from nncf.config import NNCFConfig
from nncf.data import Dataset
from nncf.parameters import CompressWeightsMode
from nncf.parameters import DropType
from nncf.parameters import ModelType
from nncf.parameters import TargetDevice
from nncf.quantization import QuantizationPreset
from nncf.quantization import compress_weights
from nncf.quantization import quantize
from nncf.quantization import quantize_with_accuracy_control
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.scopes import IgnoredScope
from nncf.version import __version__
from nncf.common.logging import nncf_logger as nncf_logger
from nncf.common.logging.logger import disable_logging as disable_logging
from nncf.common.logging.logger import set_log_level as set_log_level
from nncf.common.strip import strip as strip
from nncf.config import NNCFConfig as NNCFConfig
from nncf.data import Dataset as Dataset
from nncf.parameters import CompressWeightsMode as CompressWeightsMode
from nncf.parameters import DropType as DropType
from nncf.parameters import ModelType as ModelType
from nncf.parameters import TargetDevice as TargetDevice
from nncf.quantization import QuantizationPreset as QuantizationPreset
from nncf.quantization import compress_weights as compress_weights
from nncf.quantization import quantize as quantize
from nncf.quantization import quantize_with_accuracy_control as quantize_with_accuracy_control
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters as AdvancedQuantizationParameters
from nncf.scopes import IgnoredScope as IgnoredScope
from nncf.version import __version__ as __version__

_SUPPORTED_FRAMEWORKS = ["torch", "tensorflow", "onnx", "openvino"]


from importlib.util import find_spec as _find_spec # pylint:disable=wrong-import-position
from pathlib import Path as _Path # pylint:disable=wrong-import-position
from importlib.util import find_spec as _find_spec # noqa: E402 # pylint:disable=wrong-import-position
from pathlib import Path as _Path # noqa: E402 # pylint:disable=wrong-import-position

_AVAILABLE_FRAMEWORKS = {}

Expand Down
6 changes: 4 additions & 2 deletions nncf/common/accuracy_aware_training/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
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
from nncf.common.accuracy_aware_training.training_loop import AccuracyAwareTrainingMode as AccuracyAwareTrainingMode
from nncf.common.accuracy_aware_training.training_loop import (
create_accuracy_aware_training_loop as create_accuracy_aware_training_loop,
)
3 changes: 2 additions & 1 deletion nncf/common/accuracy_aware_training/training_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from scipy.interpolate import interp1d

from nncf.api.compression import CompressionAlgorithmController
from nncf.common.accuracy_aware_training.runner import BaseAccuracyAwareTrainingRunner
from nncf.common.accuracy_aware_training.runner_factory import AdaptiveCompressionLevelTrainingRunnerCreator
from nncf.common.accuracy_aware_training.runner_factory import EarlyExitTrainingRunnerCreator
from nncf.common.accuracy_aware_training.statistics import TrainingLoopStatistics
Expand Down Expand Up @@ -89,7 +90,7 @@ class BaseEarlyExitCompressionTrainingLoop(TrainingLoop, ABC):
"""

def __init__(self, compression_controller: CompressionAlgorithmController):
self.runner = None # type: BaseAccuracyAwareTrainingRunner
self.runner: BaseAccuracyAwareTrainingRunner = None
self.compression_controller = compression_controller
self._current_compression_rate = None

Expand Down
16 changes: 11 additions & 5 deletions nncf/common/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def create(model: TModel) -> NNCFGraph:
return GraphConverter.create_nncf_graph(model)
if model_backend == BackendType.TORCH:
return model.nncf.get_graph()
raise RuntimeError("Cannot create backend-specific graph because {} is not supported!".format(model_backend))
raise RuntimeError(
"Cannot create backend-specific graph because {} is not supported!".format(model_backend.value)
)


class ModelTransformerFactory:
Expand All @@ -71,7 +73,7 @@ def create(model: TModel) -> ModelTransformer:

return PTModelTransformer(model)
raise RuntimeError(
"Cannot create backend-specific model transformer because {} is not supported!".format(model_backend)
"Cannot create backend-specific model transformer because {} is not supported!".format(model_backend.value)
)


Expand Down Expand Up @@ -103,7 +105,9 @@ def create(model: TModel) -> Engine:
from nncf.torch.engine import PTEngine

return PTEngine(model)
raise RuntimeError("Cannot create backend-specific engine because {} is not supported!".format(model_backend))
raise RuntimeError(
"Cannot create backend-specific engine because {} is not supported!".format(model_backend.value)
)


class CommandCreatorFactory:
Expand All @@ -121,7 +125,7 @@ def create(model: TModel) -> CommandCreator:

return OVCommandCreator()
raise RuntimeError(
"Cannot create backend-specific command creator because {} is not supported!".format(model_backend)
"Cannot create backend-specific command creator because {} is not supported!".format(model_backend.value)
)


Expand All @@ -148,5 +152,7 @@ def create(model: TModel, dataset: Dataset) -> aggregator.StatisticsAggregator:

return PTStatisticsAggregator(dataset)
raise RuntimeError(
"Cannot create backend-specific statistics aggregator because {} is not supported!".format(model_backend)
"Cannot create backend-specific statistics aggregator because {} is not supported!".format(
model_backend.value
)
)
8 changes: 7 additions & 1 deletion nncf/common/graph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from nncf.common.graph.graph import *
# Make these names available from nncf.common.graph directly
from nncf.common.graph.graph import LayerName as LayerName
from nncf.common.graph.graph import NNCFGraph as NNCFGraph
from nncf.common.graph.graph import NNCFGraphEdge as NNCFGraphEdge
from nncf.common.graph.graph import NNCFGraphPatternIO as NNCFGraphPatternIO
from nncf.common.graph.graph import NNCFNode as NNCFNode
from nncf.common.graph.graph import NNCFNodeName as NNCFNodeName
8 changes: 4 additions & 4 deletions nncf/common/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ def __init__(self):
self._nx_graph = nx.DiGraph()
self._node_id_to_key_dict = {}
self._nodes: Dict[str, NNCFNode] = {}
self._input_nncf_nodes = {} # type: Dict[int, NNCFNode]
self._output_nncf_nodes = {} # type: Dict[int, NNCFNode]
self._input_nncf_nodes: Dict[int, NNCFNode] = {}
self._output_nncf_nodes: Dict[int, NNCFNode] = {}

self._node_ids_vs_layer_names = {} # type: Dict[int, LayerName]
self._layer_name_vs_shared_nodes = defaultdict(list) # type: Dict[LayerName, List[NNCFNode]]
self._node_ids_vs_layer_names: Dict[int, LayerName] = {}
self._layer_name_vs_shared_nodes: Dict[LayerName, List[NNCFNode]] = defaultdict(list)

@property
def nodes(self) -> Dict[str, NNCFNode]:
Expand Down
10 changes: 5 additions & 5 deletions nncf/common/graph/patterns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from nncf.common.graph.patterns.patterns import GraphPattern
from nncf.common.graph.patterns.patterns import HWFusedPatternNames
from nncf.common.graph.patterns.patterns import IgnoredPatternNames
from nncf.common.graph.patterns.patterns import Patterns
from nncf.common.graph.patterns.patterns import merge_two_types_of_operations
from nncf.common.graph.patterns.patterns import GraphPattern as GraphPattern
from nncf.common.graph.patterns.patterns import HWFusedPatternNames as HWFusedPatternNames
from nncf.common.graph.patterns.patterns import IgnoredPatternNames as IgnoredPatternNames
from nncf.common.graph.patterns.patterns import Patterns as Patterns
from nncf.common.graph.patterns.patterns import merge_two_types_of_operations as merge_two_types_of_operations
6 changes: 3 additions & 3 deletions nncf/common/insertion_point_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

import networkx as nx

from nncf.common.graph import Dtype
from nncf.common.graph import NNCFGraph
from nncf.common.graph import NNCFNodeName
from nncf.common.graph.graph import NNCFNode
from nncf.common.graph.graph_matching import find_subgraphs_matching_pattern
from nncf.common.graph.layer_attributes import Dtype
from nncf.common.graph.operator_metatypes import INPUT_NOOP_METATYPES
from nncf.common.graph.patterns import GraphPattern
from nncf.common.logging import nncf_logger
Expand Down Expand Up @@ -107,11 +107,11 @@ def __init__(
# Post-hook all nodes if an exact list is not specified
allowed_post_hook_insertion_points = self._get_default_post_hook_ip_list(nncf_graph)

target_node_name_vs_pre_hook_ips = defaultdict(set) # type: Dict[NNCFNodeName, Set[PreHookInsertionPoint]]
target_node_name_vs_pre_hook_ips: Dict[NNCFNodeName, Set[PreHookInsertionPoint]] = defaultdict(set)
for pre_hook_ip in allowed_pre_hook_insertion_points:
target_node_name_vs_pre_hook_ips[pre_hook_ip.target_node_name].add(pre_hook_ip)

target_node_name_vs_post_hook_ips = defaultdict(set) # type: Dict[NNCFNodeName, Set[PostHookInsertionPoint]]
target_node_name_vs_post_hook_ips: Dict[NNCFNodeName, Set[PostHookInsertionPoint]] = defaultdict(set)
for post_hook_ip in allowed_post_hook_insertion_points:
target_node_name_vs_post_hook_ips[post_hook_ip.target_node_name].add(post_hook_ip)

Expand Down
Loading

0 comments on commit 71e553d

Please sign in to comment.