Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into nl/compress_ingore…
Browse files Browse the repository at this point in the history
…d_scope
  • Loading branch information
ljaljushkin committed Oct 9, 2023
2 parents aaeaa69 + 881cca6 commit 3491617
Show file tree
Hide file tree
Showing 21 changed files with 3,656 additions and 3,237 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ You may also use one of the Dockerfiles in the [docker](./docker) directory to b
- ONNX\* ~=1.13.1
- OpenVINO\* >=2022.3.0

This repository is tested on Python* 3.8.10, PyTorch* 2.0.1 (NVidia CUDA\* Toolkit 11.7) and TensorFlow* 2.12.1 (NVidia CUDA\* Toolkit 11.8).
This repository is tested on Python* 3.8.10, PyTorch* 2.0.1 (NVidia CUDA\* Toolkit 11.8) and TensorFlow* 2.12.1 (NVidia CUDA\* Toolkit 11.8).

## NNCF Compressed Model Zoo

Expand Down
13 changes: 12 additions & 1 deletion docs/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Use the same `pip install` syntax as above to install NNCF along with the backen
pip install .[<BACKEND>]
```

List of supported backends: `torch`, `tf`, `onnx` and `openvino`.
List of supported backends: `torch`, `tf`, `onnx` and `openvino`.

For development purposes install extra packages by

Expand All @@ -61,3 +61,14 @@ Note that in order for this to work for pip versions >= 21.3, your Git version m
## As a Docker image

Use one of the Dockerfiles in the [docker](../docker) directory to build an image with an environment already set up and ready for running NNCF [sample scripts](../README.md#model-compression-samples).

## Corresponding versions

The following table lists the recommended corresponding versions of backend packages
as well as the supported versions of Python:

| NNCF | OpenVINO | PyTorch | ONNX | TensorFlow | Python |
| ------- | ---------- | -------- | -------- | ---------- | ------- |
| `2.6.0` | `2023.1.0` | `2.0.1` | `1.13.1` | `2.12.0` | `3.8` |
| `2.5.0` | `2023.0.0` | `1.13.1` | `1.13.1` | `2.11.1` | `3.8` |
| `2.4.0` | `2022.1.0` | `1.12.1` | `1.12.0` | `2.8.2` | `3.8` |
3 changes: 3 additions & 0 deletions nncf/common/graph/patterns/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ class HWFusedPatternNames(Enum):

# ATOMIC OPERATIONS
L2_NORM = PatternDesc("l2_norm")
MVN = PatternDesc("mvn")
GELU = PatternDesc("gelu")

# BLOCK PATTERNS
ADD_SCALE_SHIFT_OUTPUT = PatternDesc("add_scale_shift_output")
Expand Down Expand Up @@ -338,6 +340,7 @@ class HWFusedPatternNames(Enum):
LINEAR_ACTIVATIONS_BATCH_NORM = PatternDesc("linear_activations_batch_norm")
LINEAR_ACTIVATIONS_SCALE_SHIFT = PatternDesc("linear_activations_scale_shift")
LINEAR_ARITHMETIC = PatternDesc("linear_arithmetic")
LINEAR_SHIFT_SCALE = PatternDesc("linear_shift_scale")
LINEAR_ARITHMETIC_ACTIVATIONS = PatternDesc("linear_arithmetic_activations")
# Found in PicoDet models
LINEAR_ARITHMETIC_ACTIVATIONS_ARITHMETIC = PatternDesc("linear_arithmetic_activations_arithmetic")
Expand Down
6 changes: 6 additions & 0 deletions nncf/onnx/graph/metatypes/onnx_metatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,12 @@ class ONNXDeformableConvolutionMetatype(ONNXOpMetatype):
op_names = ["DeformConv"]


@ONNX_OPERATION_METATYPES.register()
class ONNXErfMetatype(ONNXOpMetatype):
name = "ErfOp"
op_names = ["Erf"]


def get_operator_metatypes() -> List[Type[OperatorMetatype]]:
"""
Returns a list of the operator metatypes.
Expand Down
99 changes: 99 additions & 0 deletions nncf/onnx/hardware/fused_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,93 @@
# BLOCK PATTERNS


@ONNX_HW_FUSED_PATTERNS.register(HWFusedPatternNames.MVN)
def create_mvn() -> GraphPattern:
pattern = GraphPattern()
pattern_input_node = pattern.add_node(
**{GraphPattern.LABEL_ATTR: "*INPUT_NODE*", GraphPattern.METATYPE_ATTR: GraphPattern.NON_PATTERN_NODE_TYPE}
)
reduce_mean_node_1 = pattern.add_node(
**{GraphPattern.LABEL_ATTR: "REDUCE_MEAN_1", GraphPattern.METATYPE_ATTR: om.ONNXReduceMeanMetatype}
)
sub_node = pattern.add_node(
**{
GraphPattern.LABEL_ATTR: "SUBTRACT",
GraphPattern.METATYPE_ATTR: [om.ONNXSubMetatype],
}
)
pow_node = pattern.add_node(
**{
GraphPattern.LABEL_ATTR: "POW",
GraphPattern.METATYPE_ATTR: [om.ONNXPowMetatype],
}
)
reduce_mean_node_2 = pattern.add_node(
**{GraphPattern.LABEL_ATTR: "REDUCE_MEAN_2", GraphPattern.METATYPE_ATTR: om.ONNXReduceMeanMetatype}
)
add_node = pattern.add_node(**{GraphPattern.LABEL_ATTR: "ADD", GraphPattern.METATYPE_ATTR: om.ONNXAddLayerMetatype})
sqrt_node = pattern.add_node(**{GraphPattern.LABEL_ATTR: "SQRT", GraphPattern.METATYPE_ATTR: om.ONNXSqrtMetatype})
div_node = pattern.add_node(**{GraphPattern.LABEL_ATTR: "DIV", GraphPattern.METATYPE_ATTR: om.ONNXDivLayerMetatype})

pattern.add_edge(pattern_input_node, reduce_mean_node_1)
pattern.add_edge(reduce_mean_node_1, sub_node)
pattern.add_edge(pattern_input_node, sub_node)
pattern.add_edge(sub_node, pow_node)
pattern.add_edge(pow_node, reduce_mean_node_2)
pattern.add_edge(reduce_mean_node_2, add_node)
pattern.add_edge(add_node, sqrt_node)
pattern.add_edge(sqrt_node, div_node)
pattern.add_edge(sub_node, div_node)
return pattern


@ONNX_HW_FUSED_PATTERNS.register(HWFusedPatternNames.MVN_SCALE_SHIFT)
def create_mvn_scale_shift() -> GraphPattern:
mvn = create_mvn()
scale_shift = create_scale_shift()

mvn.join_patterns(scale_shift)
return mvn


@ONNX_HW_FUSED_PATTERNS.register(HWFusedPatternNames.GELU)
def create_gelu() -> GraphPattern:
pattern = GraphPattern()
pattern_input_node = pattern.add_node(
**{GraphPattern.LABEL_ATTR: "*INPUT_NODE*", GraphPattern.METATYPE_ATTR: GraphPattern.NON_PATTERN_NODE_TYPE}
)
div_node = pattern.add_node(
**{
GraphPattern.LABEL_ATTR: "DIV",
GraphPattern.METATYPE_ATTR: [om.ONNXDivLayerMetatype, om.ONNXMulLayerMetatype],
}
)
erf_node = pattern.add_node(
**{
GraphPattern.LABEL_ATTR: "ERF",
GraphPattern.METATYPE_ATTR: om.ONNXErfMetatype,
}
)
add_node = pattern.add_node(
**{
GraphPattern.LABEL_ATTR: "ADD",
GraphPattern.METATYPE_ATTR: [om.ONNXAddLayerMetatype, om.ONNXSubMetatype],
}
)
mul_node = pattern.add_node(
**{
GraphPattern.LABEL_ATTR: "MUL",
GraphPattern.METATYPE_ATTR: [om.ONNXMulLayerMetatype, om.ONNXDivLayerMetatype],
}
)
pattern.add_edge(pattern_input_node, div_node)
pattern.add_edge(div_node, erf_node)
pattern.add_edge(erf_node, add_node)
pattern.add_edge(add_node, mul_node)
pattern.add_edge(pattern_input_node, mul_node)
return pattern


@ONNX_HW_FUSED_PATTERNS.register(HWFusedPatternNames.SCALE_SHIFT)
def create_scale_shift() -> GraphPattern:
pattern = GraphPattern()
Expand Down Expand Up @@ -372,6 +459,15 @@ def create_linear_arithmetic_activations() -> GraphPattern:
return linear


@ONNX_HW_FUSED_PATTERNS.register(HWFusedPatternNames.LINEAR_SHIFT_SCALE)
def create_linear_shift_scale() -> GraphPattern:
linear = linear_operations()
shift_scale = create_shift_scale()

linear.join_patterns(shift_scale)
return linear


@ONNX_HW_FUSED_PATTERNS.register(HWFusedPatternNames.LINEAR_ARITHMETIC_ACTIVATIONS_ARITHMETIC)
def create_linear_arithmetic_activations_arithmetic() -> GraphPattern:
linear_arithmetic_activations = create_linear_arithmetic_activations()
Expand Down Expand Up @@ -423,6 +519,9 @@ def atomic_activations_operations() -> GraphPattern:

hswish_without_denominator = create_hswish_without_denominator()
pattern.add_pattern_alternative(hswish_without_denominator)

gelu = create_gelu()
pattern.add_pattern_alternative(gelu)
return pattern


Expand Down
8 changes: 8 additions & 0 deletions nncf/openvino/hardware/fused_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@ def create_linear_arithmetic_activations() -> GraphPattern:
return linear


@OPENVINO_HW_FUSED_PATTERNS.register(HWFusedPatternNames.LINEAR_SHIFT_SCALE)
def create_linear_shift_scale() -> GraphPattern:
linear = linear_operations()
shift_scale = create_shift_scale()
linear.join_patterns(shift_scale)
return linear


@OPENVINO_HW_FUSED_PATTERNS.register(HWFusedPatternNames.LINEAR_ARITHMETIC_ACTIVATIONS_ARITHMETIC)
def create_linear_arithmetic_activations_arithmetic() -> GraphPattern:
linear_arithmetic_activations = create_linear_arithmetic_activations()
Expand Down
1 change: 1 addition & 0 deletions nncf/openvino/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from nncf.openvino.quantization.backend_parameters import BackendParameters
from nncf.openvino.quantization.backend_parameters import is_weight_compression_needed
from nncf.openvino.quantization.quantize_ifmodel import apply_algorithm_if_bodies
from nncf.openvino.quantization.weights_compression import insert_pre_compression_operations
from nncf.parameters import CompressWeightsMode
from nncf.parameters import DropType
from nncf.parameters import ModelType
Expand Down
8 changes: 8 additions & 0 deletions nncf/torch/hardware/fused_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ def create_linear_arithmetic_operations() -> GraphPattern:
return linear


@PT_HW_FUSED_PATTERNS.register(HWFusedPatternNames.LINEAR_SHIFT_SCALE)
def create_linear_shift_scale() -> GraphPattern:
linear = linear_operations()
shift_scale = create_shift_scale()
linear.join_patterns(shift_scale)
return linear


@PT_HW_FUSED_PATTERNS.register(HWFusedPatternNames.BATCH_NORM_ACTIVATIONS)
def create_batch_norm_activations_operations() -> GraphPattern:
batch_norm = batch_norm_operations()
Expand Down
65 changes: 0 additions & 65 deletions nncf/torch/tensor_statistics/reduction.py

This file was deleted.

6 changes: 0 additions & 6 deletions nncf/torch/tensor_statistics/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional, Tuple

import torch

from nncf.common.tensor_statistics.statistics import MeanTensorStatistic
Expand All @@ -20,10 +18,6 @@
from nncf.common.tensor_statistics.statistics import TensorStatistic


def _reshape_all(targets: Tuple[torch.Tensor, ...], target_shape: Tuple[int, ...]):
return map(lambda stat: torch.reshape(stat, target_shape), targets)


class PTMinMaxTensorStatistic(MinMaxTensorStatistic):
@staticmethod
def tensor_eq(tensor1: torch.Tensor, tensor2: torch.Tensor, rtol=1e-6) -> bool:
Expand Down
1 change: 0 additions & 1 deletion tests/onnx/test_pattern_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
HWFusedPatternNames.LINEAR_CONST_MULTIPLY: "Not relevant for ONNX.",
HWFusedPatternNames.ADD_SCALE_SHIFT_OUTPUT: "Not relevant for ONNX.",
HWFusedPatternNames.BATCH_INDEX: "Not relevant for ONNX.",
HWFusedPatternNames.MVN_SCALE_SHIFT: "Not relevant for ONNX.",
HWFusedPatternNames.NORMALIZE_L2_MULTIPLY: "Not relevant for ONNX.",
HWFusedPatternNames.LINEAR_WITH_BIAS: "Linear layers contains biases in ONNX.",
HWFusedPatternNames.SE_BLOCK: "Not relevant for ONNX.",
Expand Down
Loading

0 comments on commit 3491617

Please sign in to comment.