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

OVWeightUpdateCommand copies a constant #2277

Merged
merged 8 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Revert some changes
  • Loading branch information
andrey-churkin committed Nov 23, 2023
commit 9ed9f81952efeabe69d832c9905a165dc5f0ebe1
3 changes: 2 additions & 1 deletion nncf/openvino/graph/model_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import numpy as np
import openvino.runtime as ov
from openvino._pyopenvino import DescriptorTensor
from openvino.runtime import opeset13
andrey-churkin marked this conversation as resolved.
Show resolved Hide resolved
from openvino.runtime import opset9 as opset

from nncf.common.graph.model_transformer import ModelTransformer
Expand Down Expand Up @@ -358,7 +359,7 @@ def _set_const_value(node_with_const: ov.Node, const_port_id: int, const_value:
const_dtype = const_node.data.dtype
const_value = np.reshape(const_value, const_shape).astype(const_dtype)

new_const_node = ov.op.Constant(const_value, shared_memory=True)
new_const_node = opeset13.constant(const_value, dtype=const_dtype, shared_memory=True)
new_const_node.set_friendly_name(const_node.get_friendly_name())
const_port.replace_source_output(new_const_node.output(0))

Expand Down
15 changes: 13 additions & 2 deletions nncf/openvino/graph/node_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ def cnt_if_op(model: ov.Model, cnt: int) -> int:
return cnt_if_op(model, 0)


def get_const_value(const_node: ov.Node) -> np.ndarray:
"""
Returns the constant tensor for the node.

:param const_node: OpenVINO node.
:return: The constant value.
"""
return const_node.data


def get_bias_value(node_with_bias: NNCFNode, nncf_graph: NNCFGraph, model: ov.Model) -> np.ndarray:
"""
Returns the bias tensor for the biased node.
Expand All @@ -85,7 +95,7 @@ def get_bias_value(node_with_bias: NNCFNode, nncf_graph: NNCFGraph, model: ov.Mo
add_node = nncf_graph.get_next_nodes(node_with_bias)[0]
bias_constant = get_node_with_bias_value(add_node, nncf_graph)
ov_bias_constant = ops_dict[bias_constant.node_name]
return ov_bias_constant.data
return get_const_value(ov_bias_constant)


def get_weight_value(node_with_weight: NNCFNode, model: ov.Model, port_id: int) -> np.ndarray:
Expand All @@ -101,7 +111,8 @@ def get_weight_value(node_with_weight: NNCFNode, model: ov.Model, port_id: int)
const_op_friendly_name = node_with_weight.layer_attributes.constant_attributes[port_id]["name"]
friendly_name_to_op_map = {op.get_friendly_name(): op for op in model.get_ops()}
const_op = friendly_name_to_op_map[const_op_friendly_name]
return const_op.data
weight_tensor = get_const_value(const_op)
return weight_tensor


def get_node_with_bias_value(add_node: NNCFNode, nncf_graph: NNCFGraph) -> Optional[NNCFNode]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from nncf.openvino.graph.metatypes.openvino_metatypes import OVEmbeddingMetatype
from nncf.openvino.graph.metatypes.openvino_metatypes import OVMatMulMetatype
from nncf.openvino.graph.node_utils import get_channel_agnostic_reduction_axes
from nncf.openvino.graph.node_utils import get_const_value
from nncf.openvino.graph.node_utils import get_weight_channel_axes
from nncf.openvino.rt_info import dump_parameters
from nncf.parameters import CompressWeightsMode
Expand Down Expand Up @@ -118,7 +119,7 @@ def do_compression(
weight_name = weight_node.get_friendly_name()
target_inputs = weight_output.get_target_inputs()

weight = weight_node.data
weight = get_const_value(weight_node)
config = wp.compression_config
if config.mode == CompressWeightsMode.NF4:
original_shape = weight.shape
Expand Down
11 changes: 6 additions & 5 deletions tests/openvino/native/quantization/test_weights_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from attr import dataclass

from nncf import CompressWeightsMode
from nncf.openvino.graph.node_utils import get_const_value
from nncf.quantization import compress_weights
from nncf.quantization.algorithms.weight_compression.openvino_backend import WeightCompressionConfig
from nncf.quantization.algorithms.weight_compression.openvino_backend import _get_integer_quantization_error
Expand Down Expand Up @@ -48,7 +49,7 @@ def get_next_node(node):

def check_int8_node(op: ov.Node):
assert op.get_element_type() == ov.Type(np.uint8)
compressed_weight = op.data
compressed_weight = get_const_value(op)

convert_node = get_next_node(op)
assert convert_node.get_type_name() == "Convert"
Expand All @@ -60,12 +61,12 @@ def check_int8_node(op: ov.Node):
assert convert_node.get_type_name() == "Convert"

zero_point_node = convert_node.input_value(0).get_node()
zero_point = zero_point_node.data
zero_point = get_const_value(zero_point_node)

mul_node = get_next_node(sub_node)
assert mul_node.get_type_name() == "Multiply"
scale_node = mul_node.input_value(1).get_node()
scale = scale_node.data
scale = get_const_value(scale_node)

return {
"compressed_weight": compressed_weight,
Expand Down Expand Up @@ -107,7 +108,7 @@ def check_int4_grouped(op: ov.Node, mode: CompressWeightsMode, group_size: int =
assert reshape_node.get_type_name() == "Reshape"

return {
"scale": scale_node.data,
"scale": get_const_value(scale_node),
}


Expand All @@ -131,7 +132,7 @@ def check_nf4_grouped(op: ov.Node, group_size: int = 7):
assert reshape_node.get_type_name() == "Reshape"

return {
"scale": scale_node.data,
"scale": get_const_value(scale_node),
}


Expand Down