Skip to content

Commit

Permalink
Add pragma to some error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Ofir Gordon authored and Ofir Gordon committed Jun 3, 2024
1 parent caf821c commit 12980d3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _sample_batch_representative_dataset(self,
# Collect the requested number of samples from the representative dataset
for batch in representative_dataset:
if not isinstance(batch, list):
Logger.critical(f'Expected batch to be a list; found type: {type(batch)}.')
Logger.critical(f'Expected batch to be a list; found type: {type(batch)}.') # pragma: no cover
all_inp_remaining_samples = [[] for _ in range(num_inputs)]
for inp_idx in range(len(batch)):
inp_batch = batch[inp_idx]
Expand Down Expand Up @@ -260,8 +260,9 @@ def _get_representing_of_reuse_group(self, node) -> Any:
Returns: A reuse group representative node (BaseNode).
"""
father_nodes = [n for n in self.graph.nodes if not n.reuse and n.reuse_group == node.reuse_group]
if len(father_nodes) != 1:
Logger.critical(f"Expected a single non-reused node in the reused group, but found {len(father_nodes)}.")
if len(father_nodes) != 1: # pragma: no cover
Logger.critical(f"Expected a single non-reused node in the reused group, "
f"but found {len(father_nodes)}.")

return father_nodes[0]

Expand Down Expand Up @@ -327,9 +328,9 @@ def _collect_saved_hessians_for_request(self, trace_hessian_request: TraceHessia
node)

res_for_node = self.trace_hessian_request_to_score_list.get(single_node_request)
if res_for_node is None:
if res_for_node is None: # pragma: no cover
Logger.critical(f"Couldn't find saved Hessian approximations for node {node.name}.")
if len(res_for_node) < required_size:
if len(res_for_node) < required_size: # pragma: no cover
Logger.critical(f"Missing Hessian approximations for node {node.name}, requested {required_size} "
f"but found only {len(res_for_node)}.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(self,
self.num_iterations_for_approximation = num_iterations_for_approximation

# Validate representative dataset has same inputs as graph
if len(self.input_images) != len(graph.get_inputs()):
if len(self.input_images) != len(graph.get_inputs()): # pragma: no cover
Logger.critical(f"The graph requires {len(graph.get_inputs())} inputs, but the provided representative dataset contains {len(self.input_images)} inputs.")

self.fw_impl = fw_impl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def compute(self) -> List[np.ndarray]:
else:
outputs = model(*self.input_images)

if len(outputs) != len(grad_model_outputs):
if len(outputs) != len(grad_model_outputs): # pragma: no cover
Logger.critical(
f"Model for computing activation Hessian approximation expects {len(grad_model_outputs)} "
f"outputs, but got {len(outputs)} output tensors.")
Expand Down Expand Up @@ -117,7 +117,7 @@ def compute(self) -> List[np.ndarray]:
if hess_v is None:
# In case we have an output node, which is an interest point, but it is not
# differentiable, we consider its Hessian to be the initial value 0.
continue
continue # pragma: no cover

# Mean over all dims but the batch (CXHXW for conv)
hessian_trace_approx = tf.reduce_sum(hess_v ** 2.0,
Expand Down Expand Up @@ -150,6 +150,6 @@ def compute(self) -> List[np.ndarray]:

return hessian_results

else:
else: # pragma: no cover
Logger.critical(f"{self.hessian_request.granularity} "
f"is not supported for Keras activation hessian\'s trace approximation calculator.")
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self,
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian trace.
"""

if len(trace_hessian_request.target_nodes) > 1:
if len(trace_hessian_request.target_nodes) > 1: # pragma: no cover
Logger.critical(f"Weights Hessian approximation is currently supported only for a single target node,"
f" but the provided request contains the following target nodes: "
f"{trace_hessian_request.target_nodes}.")
Expand Down Expand Up @@ -87,7 +87,7 @@ def compute(self) -> List[np.ndarray]:
weight_attributes = DEFAULT_KERAS_INFO.get_kernel_op_attributes(target_node.type)

# Get the weight tensor for the target node
if len(weight_attributes) != 1:
if len(weight_attributes) != 1: # pragma: no cover
Logger.critical(f"Hessian-based scoring with respect to weights is currently supported only for nodes with "
f"a single weight attribute. Found {len(weight_attributes)} attributes.")

Expand Down Expand Up @@ -148,7 +148,7 @@ def compute(self) -> List[np.ndarray]:
del tape

if self.hessian_request.granularity == HessianInfoGranularity.PER_TENSOR:
if final_approx.shape != (1,):
if final_approx.shape != (1,): # pragma: no cover
Logger.critical(f"For HessianInfoGranularity.PER_TENSOR, the expected score shape is (1,), but found {final_approx.shape}.")
elif self.hessian_request.granularity == HessianInfoGranularity.PER_ELEMENT:
# Reshaping the scores to the original weight shape
Expand Down Expand Up @@ -207,5 +207,5 @@ def _get_num_scores_by_granularity(self,
return weight_tensor.shape[output_channel_axis]
elif self.hessian_request.granularity == HessianInfoGranularity.PER_ELEMENT:
return tf.size(weight_tensor).numpy()
else:
else: # pragma: no cover
Logger.critical(f"Unexpected granularity encountered: {self.hessian_request.granularity}.")
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def compute(self) -> List[np.ndarray]:

outputs = model(*self.input_images)

if len(outputs) != len(grad_model_outputs):
if len(outputs) != len(grad_model_outputs): # pragma: no cover
Logger.critical(f"Mismatch in expected and actual model outputs for activation Hessian approximation. "
f"Expected {len(grad_model_outputs)} outputs, received {len(outputs)}.")

Expand Down Expand Up @@ -118,7 +118,7 @@ def compute(self) -> List[np.ndarray]:
if hess_v is None:
# In case we have an output node, which is an interest point, but it is not differentiable,
# we consider its Hessian to be the initial value 0.
continue
continue # pragma: no cover

# Mean over all dims but the batch (CXHXW for conv)
hessian_trace_approx = torch.sum(hess_v ** 2.0, dim=tuple(d for d in range(1, len(hess_v.shape))))
Expand Down Expand Up @@ -146,7 +146,7 @@ def compute(self) -> List[np.ndarray]:

return hessian_results

else:
else: # pragma: no cover
Logger.critical(f"PyTorch activation Hessian's trace approximation does not support "
f"{self.hessian_request.granularity} granularity.")

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self,
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian trace.
"""

if len(trace_hessian_request.target_nodes) > 1:
if len(trace_hessian_request.target_nodes) > 1: # pragma: no cover
Logger.critical(f"Weights Hessian approximation is currently supported only for a single target node,"
f" but the provided request contains the following target nodes: "
f"{trace_hessian_request.target_nodes}.")
Expand Down Expand Up @@ -88,8 +88,9 @@ def compute(self) -> List[np.ndarray]:
weights_attributes = DEFAULT_PYTORCH_INFO.get_kernel_op_attributes(target_node.type)

# Get the weight tensor for the target node
if len(weights_attributes) != 1:
Logger.critical(f"Currently, Hessian scores with respect to weights are supported only for nodes with a single weight attribute. {len(weights_attributes)} attributes found.")
if len(weights_attributes) != 1: # pragma: no cover
Logger.critical(f"Currently, Hessian scores with respect to weights are supported only for nodes with a "
f"single weight attribute. {len(weights_attributes)} attributes found.")

weights_tensor = getattr(getattr(model, target_node.name), weights_attributes[0])

Expand Down

0 comments on commit 12980d3

Please sign in to comment.