Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daniil-lyakhov committed Sep 8, 2023
1 parent 669f0a9 commit 0982ce4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion nncf/openvino/graph/layer_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_backend_agnostic_attributes(self):


def get_weighted_layer_attributes(
ov_node: ov.Node, ov_metatype: OVOpMetatype, constant_attributes: Dict[str, Any]
ov_node: ov.Node, ov_metatype: OVOpMetatype, constant_attributes: Dict[int, Any]
) -> WeightedLayerAttributes:
"""
Funciton retrieves common layer attributes from the given node.
Expand Down
15 changes: 12 additions & 3 deletions nncf/openvino/quantization/weights_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from nncf.common.graph.layer_attributes import LayoutElem
from nncf.common.graph.layer_attributes import LinearLayerAttributes
from nncf.common.graph.operator_metatypes import OperatorMetatype
from nncf.openvino.graph.layer_attributes import get_weighted_layer_attributes
from nncf.openvino.graph.metatypes.openvino_metatypes import OVEmbeddingMetatype
from nncf.openvino.graph.metatypes.openvino_metatypes import OVMatMulMetatype
from nncf.openvino.graph.metatypes.openvino_metatypes import get_node_metatype
Expand Down Expand Up @@ -57,7 +58,8 @@ def insert_pre_compression_operations(model: ov.Model, bits: int = 8) -> None:
continue

weight = get_const_value(weight_node)
axes = _get_reduction_axes(metatype, node, const_port_id)
weight_shape = weight_node.get_output_shape(0)
axes = _get_reduction_axes(metatype, node, const_port_id, weight_shape)
min_values = np.min(weight, axis=axes, keepdims=True)
max_values = np.max(weight, axis=axes, keepdims=True)

Expand All @@ -78,18 +80,25 @@ def insert_pre_compression_operations(model: ov.Model, bits: int = 8) -> None:
target_input.replace_source_output(mul.output(0))


def _get_reduction_axes(metatype: Type[OperatorMetatype], node: ov.Node, weight_port_id: int) -> Union[int, Tuple[int]]:
def _get_reduction_axes(
metatype: Type[OperatorMetatype], node: ov.Node, weight_port_id: int, weight_shape: ov.Shape
) -> Union[int, Tuple[int]]:
"""
Determines reduction axes by given metatype and node information.
:param metatype: The metatype of the operator.
:param node: The OpenVINO node.
:param weight_port_id: The weight port ID.
:param weight_shape: Shape of the target node weight.
:return: The reduction axes as an integer or a tuple of integers.
"""
if metatype is OVMatMulMetatype:
layer_attributes = node.layer_attributes.get_backend_agnostic_attributes()
transpose = node.get_attributes()[f"transpose_{'a' if weight_port_id == 0 else 'b'}"]
constant_attributes = {weight_port_id: {"shape": weight_shape, "transpose": transpose}}
layer_attributes = get_weighted_layer_attributes(
ov_node=node, ov_metatype=OVMatMulMetatype, constant_attributes=constant_attributes
)
assert isinstance(layer_attributes, LinearLayerAttributes)
axes = tuple(idx for idx, elem in enumerate(layer_attributes.weights_layout) if elem == LayoutElem.C_IN)
elif metatype is OVEmbeddingMetatype:
Expand Down
5 changes: 3 additions & 2 deletions nncf/quantization/algorithms/channel_alignment/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from nncf.common.tensor_statistics.statistic_point import StatisticPointsContainer
from nncf.common.utils.backend import BackendType
from nncf.common.utils.backend import get_backend
from nncf.openvino.graph.node_utils import get_channel_agnostic_reduction_shape
from nncf.quantization.algorithms.algorithm import Algorithm
from nncf.quantization.algorithms.channel_alignment.backend import ALGO_BACKENDS
from nncf.quantization.algorithms.channel_alignment.backend import ChannelAlignmentAlgoBackend
Expand Down Expand Up @@ -392,7 +391,9 @@ def get_statistic_points(self, model: TModel, graph: NNCFGraph) -> StatisticPoin

channel_axis = conv_in.metatype.output_channel_axis
activation_shape = list(range(len(graph.get_output_edges(node_in)[0].tensor_shape)))
reduction_shape = get_channel_agnostic_reduction_shape([channel_axis], activation_shape)
reduction_shape = self._backend_entity.get_channel_agnostic_reduction_shape(
[channel_axis], activation_shape
)

statistic_collector = self._backend_entity.get_statistic_collector(
tuple(reduction_shape), self._quantile, self.subset_size, self.inplace_statistics
Expand Down
11 changes: 11 additions & 0 deletions nncf/quantization/algorithms/channel_alignment/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,14 @@ def create_bias_tensor(node: NNCFNode, nncf_graph: NNCFGraph, value: Any) -> np.
:param value: Value to fill bias constant array.
:return: Bias value constant array filled by given value.
"""

@staticmethod
@abstractmethod
def get_channel_agnostic_reduction_shape(channel_axis: int, shape: Tuple[int]) -> Tuple[int]:
"""
Returns filtered reduction shape without axes that corresponds channels.
:param channel_axes: List of the channel axes.
:param shape: Shape that need to be filtered.
:return: Reduction shape in tuple format.
"""
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from nncf.openvino.graph.metatypes.openvino_metatypes import OVSubtractMetatype
from nncf.openvino.graph.node_utils import create_bias_tensor
from nncf.openvino.graph.node_utils import get_bias_value
from nncf.openvino.graph.node_utils import get_channel_agnostic_reduction_shape
from nncf.openvino.graph.node_utils import get_node_with_bias_value
from nncf.openvino.graph.node_utils import get_weight_value
from nncf.openvino.graph.transformations.commands import OVTargetPoint
Expand Down Expand Up @@ -101,3 +102,7 @@ def is_node_with_bias(node: NNCFNode, nncf_graph: NNCFGraph) -> bool:
@staticmethod
def create_bias_tensor(node: NNCFNode, nncf_graph: NNCFGraph, value: Any) -> np.ndarray:
return create_bias_tensor(node, nncf_graph, value)

@staticmethod
def get_channel_agnostic_reduction_shape(channel_axis: int, shape: Tuple[int]) -> Tuple[int]:
return get_channel_agnostic_reduction_shape(channel_axes=channel_axis, shape=shape)

0 comments on commit 0982ce4

Please sign in to comment.