From c7af7953321b7a731e2e87f138947aa4ca9907e4 Mon Sep 17 00:00:00 2001 From: irenab Date: Wed, 8 Jan 2025 12:36:38 +0200 Subject: [PATCH] improve coverage --- .../resource_utilization_calculator.py | 18 ++++---- .../resource_utilization_tools/ru_methods.py | 42 +------------------ model_compression_toolkit/core/runner.py | 6 +++ 3 files changed, 16 insertions(+), 50 deletions(-) diff --git a/model_compression_toolkit/core/common/mixed_precision/resource_utilization_tools/resource_utilization_calculator.py b/model_compression_toolkit/core/common/mixed_precision/resource_utilization_tools/resource_utilization_calculator.py index e1e9eaafe..601608cf8 100644 --- a/model_compression_toolkit/core/common/mixed_precision/resource_utilization_tools/resource_utilization_calculator.py +++ b/model_compression_toolkit/core/common/mixed_precision/resource_utilization_tools/resource_utilization_calculator.py @@ -178,10 +178,10 @@ def compute_resource_utilization(self, w_total, a_total = None, None if {RUTarget.WEIGHTS, RUTarget.TOTAL}.intersection(metrics): w_total, *_ = self.compute_weights_utilization(target_criterion, bitwidth_mode, w_qcs) - elif w_qcs is not None: + elif w_qcs is not None: # pragma: no cover raise ValueError('Weight configuration passed but no relevant metric requested.') - if act_qcs and not {RUTarget.ACTIVATION, RUTarget.TOTAL}.intersection(metrics): + if act_qcs and not {RUTarget.ACTIVATION, RUTarget.TOTAL}.intersection(metrics): # pragma: no cover raise ValueError('Activation configuration passed but no relevant metric requested.') if RUTarget.ACTIVATION in metrics: a_total, *_ = self.compute_activations_utilization(target_criterion, bitwidth_mode, act_qcs) @@ -258,7 +258,7 @@ def compute_node_weights_utilization(self, - Detailed per weight utilization. """ weight_attrs = self._get_target_weight_attrs(n, target_criterion) - if not weight_attrs: + if not weight_attrs: # pragma: no cover return Utilization.zero_utilization(bitwidth_mode, ), {} attr_util = {} @@ -298,10 +298,10 @@ def compute_cut_activation_utilization(self, - Total utilization per cut. - Detailed utilization per cut per node. """ - if target_criterion != TargetInclusionCriterion.AnyQuantized: + if target_criterion != TargetInclusionCriterion.AnyQuantized: # pragma: no cover raise NotImplementedError('Computing MaxCut activation utilization is currently only supported for quantized targets.') - graph_target_nodes = self._get_target_activation_nodes(target_criterion, include_reused=False) + graph_target_nodes = self._get_target_activation_nodes(target_criterion, include_reused=True) # if there are no target activations in the graph, don't waste time looking for cuts if not graph_target_nodes: return 0, {}, {} @@ -382,7 +382,7 @@ def compute_node_activation_tensor_utilization(self, """ if target_criterion: nodes = self._get_target_activation_nodes(target_criterion=target_criterion, include_reused=True, nodes=[n]) - if not nodes: + if not nodes: # pragma: no cover return Utilization.zero_utilization(bitwidth_mode) size = self._act_tensors_size[n] @@ -415,7 +415,7 @@ def compute_bops(self, """ # currently we compute bops for all nodes with quantized weights, regardless of whether the input # activation is quantized. - if target_criterion != TargetInclusionCriterion.AnyQuantized: + if target_criterion != TargetInclusionCriterion.AnyQuantized: # pragma: no cover raise NotImplementedError('BOPS computation is currently only supported for quantized targets.') nodes = [n for n in self.graph.nodes if n.has_kernel_weight_to_quantize(self.fw_info)] @@ -447,7 +447,7 @@ def compute_node_bops(self, BOPS count. """ node_mac = self.fw_impl.get_node_mac_operations(n, self.fw_info) - if node_mac == 0 or bitwidth_mode == BitwidthMode.Size: + if node_mac == 0 or bitwidth_mode == BitwidthMode.Size: # pragma: no cover return node_mac incoming_edges = self.graph.incoming_edges(n, sort_by_attr=EDGE_SINK_INDEX) @@ -591,7 +591,7 @@ def _get_activation_nbits(n: BaseNode, Returns: Activation bit-width. """ - if bitwidth_mode == BitwidthMode.Float: + if bitwidth_mode == BitwidthMode.Float or not n.is_activation_quantization_enabled(): return FLOAT_BITWIDTH if bitwidth_mode in _bitwidth_mode_fn: diff --git a/model_compression_toolkit/core/common/mixed_precision/resource_utilization_tools/ru_methods.py b/model_compression_toolkit/core/common/mixed_precision/resource_utilization_tools/ru_methods.py index e915d0254..a350aa0b9 100644 --- a/model_compression_toolkit/core/common/mixed_precision/resource_utilization_tools/ru_methods.py +++ b/model_compression_toolkit/core/common/mixed_precision/resource_utilization_tools/ru_methods.py @@ -212,47 +212,7 @@ def _get_node_cfg_idx(node: BaseNode, mp_cfg: List[int], sorted_configurable_nod if node.name in sorted_configurable_nodes_names: node_idx = sorted_configurable_nodes_names.index(node.name) return mp_cfg[node_idx] - else: + else: # pragma: no cover assert len(node.candidates_quantization_cfg) > 0, \ "Any node should have at least one candidate configuration." return 0 - - -def _get_origin_weights_node(n: BaseNode) -> BaseNode: - """ - In case we run a resource utilization computation on a virtual graph, - this method is used to retrieve the original node out of a virtual weights node, - - Args: - n: A possibly virtual node. - - Returns: A node from the original (non-virtual) graph which the given node represents. - - """ - - if isinstance(n, VirtualActivationWeightsNode): - return n.original_weights_node - if isinstance(n, VirtualSplitWeightsNode): - return n.origin_node - - return n - - -def _get_origin_activation_node(n: BaseNode) -> BaseNode: - """ - In case we run a resource utilization computation on a virtual graph, - this method is used to retrieve the original node out of a virtual activation node, - - Args: - n: A possibly virtual node. - - Returns: A node from the original (non-virtual) graph which the given node represents. - - """ - - if isinstance(n, VirtualActivationWeightsNode): - return n.original_activation_node - if isinstance(n, VirtualSplitActivationNode): - return n.origin_node - - return n diff --git a/model_compression_toolkit/core/runner.py b/model_compression_toolkit/core/runner.py index 83ad5e363..8c585fb1f 100644 --- a/model_compression_toolkit/core/runner.py +++ b/model_compression_toolkit/core/runner.py @@ -226,6 +226,12 @@ def _set_final_resource_utilization(graph: Graph, ru_calculator = ResourceUtilizationCalculator(graph, fw_impl, fw_info) final_ru = ru_calculator.compute_resource_utilization(TargetInclusionCriterion.AnyQuantized, BitwidthMode.MpCustom, act_qcs=a_qcs, w_qcs=w_qcs) + + for ru_target, ru in final_ru.get_resource_utilization_dict().items(): + if ru == 0: + Logger.warning(f"No relevant quantized layers for the ru target {ru_target} were found, the recorded " + f"final ru for this target would be 0.") + print(final_ru) graph.user_info.final_resource_utilization = final_ru graph.user_info.mixed_precision_cfg = final_bit_widths_config