Skip to content

Commit

Permalink
[PTQ] Remove insert_null_biases pass (#2217)
Browse files Browse the repository at this point in the history
### Changes

- Removed `insert_bull_biases` method

### Reason for changes

- Inserting is not necessary since correcting the null biases is limited
by magnitude.
- Inserting of the new increases binary file size.

### Related tickets

- 120843
  • Loading branch information
KodiaqQ authored Oct 26, 2023
1 parent faf687c commit 4f0a6de
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 74 deletions.
33 changes: 0 additions & 33 deletions nncf/openvino/graph/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,9 @@
from nncf.common.graph.graph import NNCFGraph
from nncf.common.graph.transformations.layout import TransformationLayout
from nncf.openvino.graph.metatypes.groups import FAKE_QUANTIZE_OPERATIONS
from nncf.openvino.graph.metatypes.openvino_metatypes import OVConvolutionBackpropDataMetatype
from nncf.openvino.graph.metatypes.openvino_metatypes import OVConvolutionMetatype
from nncf.openvino.graph.metatypes.openvino_metatypes import OVDepthwiseConvolutionMetatype
from nncf.openvino.graph.metatypes.openvino_metatypes import OVGroupConvolutionBackpropDataMetatype
from nncf.openvino.graph.metatypes.openvino_metatypes import OVGroupConvolutionMetatype
from nncf.openvino.graph.node_utils import create_bias_tensor
from nncf.openvino.graph.node_utils import is_node_with_bias
from nncf.openvino.graph.transformations.command_creation import OVCommandCreator


def insert_null_biases(model: ov.Model, graph: NNCFGraph) -> ov.Model:
"""
This method finds and inserts zero biases for the layers that should have it.
:param model: ov.Model instance.
:param graph: Model graph.
:return: Updated ov.Model instance with zero biases
"""
types_to_insert_bias = [
OVConvolutionMetatype,
OVGroupConvolutionMetatype,
OVDepthwiseConvolutionMetatype,
OVConvolutionBackpropDataMetatype,
OVGroupConvolutionBackpropDataMetatype,
]
nodes_without_biases = graph.get_nodes_by_metatypes(types_to_insert_bias)
nodes_without_biases = [node for node in nodes_without_biases if not is_node_with_bias(node, graph)]
transformation_layout = TransformationLayout()
model_transformer = ModelTransformerFactory.create(model)
for node_without_bias in nodes_without_biases:
const_value = create_bias_tensor(node_without_bias, graph, 0)
bias_insertion_command = OVCommandCreator.create_command_to_insert_bias(node_without_bias, const_value)
transformation_layout.register(bias_insertion_command)
return model_transformer.transform(transformation_layout)


def remove_fq_from_inputs(model: ov.Model, graph: NNCFGraph) -> ov.Model:
"""
This method removes the activation Fake Quantize nodes from the model.
Expand Down
3 changes: 0 additions & 3 deletions nncf/quantization/algorithms/bias_correction/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def apply(
dataset: Optional[Dataset] = None,
) -> TModel:
self._set_backend_entity(model)
model = self._backend_entity.insert_null_biases(model, graph)
main_transformations_layout = TransformationLayout()
main_model_transformer = ModelTransformerFactory.create(model)

Expand Down Expand Up @@ -488,8 +487,6 @@ def output_filter_func(point):
def get_statistic_points(self, model: TModel, graph: NNCFGraph) -> StatisticPointsContainer:
self._set_backend_entity(model)
model_copy = self._backend_entity.remove_fq_from_inputs(copy_model(model), graph)
graph_copy = NNCFGraphFactory.create(model_copy)
model_copy = self._backend_entity.insert_null_biases(model_copy, graph_copy)
nncf_graph = NNCFGraphFactory.create(model_copy)
statistic_container = StatisticPointsContainer()

Expand Down
11 changes: 0 additions & 11 deletions nncf/quantization/algorithms/bias_correction/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,3 @@ def remove_fq_from_inputs(model: TModel, nncf_graph: NNCFGraph) -> TModel:
:param nncf_graph: NNCFGraph instance.
:return: TModel without activation Fake Quantize nodes (or Quantize-Dequantize pairs).
"""

@staticmethod
@abstractmethod
def insert_null_biases(model: TModel, nncf_graph: NNCFGraph) -> TModel:
"""
This method finds and inserts zero biases for the layers that should have it.
:param model: TModel instance.
:param nncf_graph: NNCFGraph instance.
:return: TModel instance with zero biases
"""
4 changes: 0 additions & 4 deletions nncf/quantization/algorithms/bias_correction/onnx_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,3 @@ def is_node_with_bias(node: NNCFNode, nncf_graph: NNCFGraph) -> bool:
@staticmethod
def remove_fq_from_inputs(model: onnx.ModelProto, nncf_graph: NNCFGraph) -> onnx.ModelProto:
return remove_fq_from_inputs(model, nncf_graph)

@staticmethod
def insert_null_biases(model: onnx.ModelProto, nncf_graph: NNCFGraph) -> onnx.ModelProto:
return model
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from nncf.common.graph.transformations.commands import TargetType
from nncf.experimental.common.tensor_statistics.collectors import TensorCollector
from nncf.openvino.graph.metatypes.groups import FAKE_QUANTIZE_OPERATIONS
from nncf.openvino.graph.model_utils import insert_null_biases
from nncf.openvino.graph.model_utils import remove_fq_from_inputs
from nncf.openvino.graph.node_utils import get_bias_value
from nncf.openvino.graph.node_utils import is_node_with_bias
Expand Down Expand Up @@ -132,7 +131,3 @@ def is_node_with_bias(node: NNCFNode, nncf_graph: NNCFGraph) -> bool:
@staticmethod
def remove_fq_from_inputs(model: ov.Model, nncf_graph: NNCFGraph) -> ov.Model:
return remove_fq_from_inputs(model, nncf_graph)

@staticmethod
def insert_null_biases(model: ov.Model, nncf_graph: NNCFGraph) -> ov.Model:
return insert_null_biases(model, nncf_graph)
18 changes: 0 additions & 18 deletions nncf/quantization/passes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

from nncf.common.graph.graph import NNCFGraph
from nncf.common.graph.operator_metatypes import OperatorMetatype
from nncf.common.utils.backend import BackendType
from nncf.common.utils.backend import get_backend

TModel = TypeVar("TModel")

Expand Down Expand Up @@ -173,19 +171,3 @@ def filter_constant_nodes(
constant_nodes = [node for node in nncf_graph.get_all_nodes() if node not in visited_nodes]
nncf_graph.remove_nodes_from(constant_nodes)
return nncf_graph


def insert_null_biases_pass(model: TModel, graph: NNCFGraph) -> TModel:
"""
This pass finds and inserts zero biases to the given model for the layers that should have it.
:param model: Model instance.
:param graph: NNCFGraph instance.
:return: Updated Model instance with zero biases
"""
model_backend = get_backend(model)
if model_backend == BackendType.OPENVINO:
from nncf.openvino.graph.model_utils import insert_null_biases

return insert_null_biases(model, graph)
return model

0 comments on commit 4f0a6de

Please sign in to comment.