Skip to content

Commit

Permalink
improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
irenaby committed Jan 8, 2025
1 parent d11460c commit c7af795
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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, {}, {}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions model_compression_toolkit/core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c7af795

Please sign in to comment.