Skip to content

Commit a2b22fd

Browse files
Arm backend: Replace asserts with exceptions in quantization annotator (#11356)
- In _annotate_input/output use RuntimeError/ValueError instead of assert so checks aren’t stripped under -O - In _match_pattern raise ValueError if no pattern is provided Signed-off-by: Sebastian Larsson <[email protected]>
1 parent 699e73d commit a2b22fd

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

backends/arm/quantizer/quantization_annotator.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ def _is_ok_for_quantization(
9595
continue
9696

9797
for n_arg in _as_list(node.args[quant_property.index]):
98-
assert isinstance(n_arg, Node)
98+
if not isinstance(n_arg, Node):
99+
raise TypeError(
100+
f"n_arg must be a Node instance, got {type(n_arg).__name__!r}"
101+
)
99102
if not is_ok_for_quantization(n_arg, gm): # type: ignore[attr-defined]
100103
logger.debug(
101104
f'could not quantize node due to input "{node}": '
@@ -108,7 +111,10 @@ def _is_ok_for_quantization(
108111

109112

110113
def _annotate_input(node: Node, quant_property: _QuantProperty):
111-
assert not is_annotated(node)
114+
if is_annotated(node):
115+
raise RuntimeError(
116+
f"Cannot annotate input: node '{node.name}' is already annotated"
117+
)
112118
if quant_property.optional and (
113119
quant_property.index >= len(node.args)
114120
or node.args[quant_property.index] is None
@@ -120,17 +126,28 @@ def _annotate_input(node: Node, quant_property: _QuantProperty):
120126
_as_list(quant_property.qspec),
121127
strict=True,
122128
):
123-
assert isinstance(n_arg, Node)
129+
if not isinstance(n_arg, Node):
130+
raise TypeError(
131+
f"n_arg must be a Node instance, got {type(n_arg).__name__!r}"
132+
)
124133
annotate_input_qspec_map(node, n_arg, qspec)
125134
if quant_property.mark_annotated:
126135
mark_node_as_annotated(n_arg) # type: ignore[attr-defined]
127136

128137

129138
def _annotate_output(node: Node, quant_property: _QuantProperty):
130-
assert not is_annotated(node)
131-
assert not quant_property.mark_annotated
132-
assert not quant_property.optional
133-
assert quant_property.index == 0, "Only one output annotation supported currently"
139+
if is_annotated(node):
140+
raise RuntimeError(
141+
f"Cannot annotate output: node '{node.name}' is already annotated"
142+
)
143+
if quant_property.mark_annotated:
144+
raise ValueError(
145+
"quant_property.mark_annotated must be False for output annotation"
146+
)
147+
if quant_property.optional:
148+
raise ValueError("quant_property.optional must be False for output annotation")
149+
if quant_property.index != 0:
150+
raise ValueError("Only one output annotation supported currently")
134151

135152
annotate_output_qspec(node, quant_property.qspec)
136153

@@ -145,7 +162,9 @@ def _match_pattern(
145162
146163
Each 'pattern' element is composed of a list of disjunctive nodes types.
147164
"""
148-
assert len(pattern) > 0, "No pattern provided"
165+
if len(pattern) < 1:
166+
raise ValueError("No pattern provided")
167+
149168
if filter_fn is not None:
150169
if not filter_fn(node):
151170
return False
@@ -417,8 +436,14 @@ def any_or_hardtanh_min_zero(n: Node):
417436
torch.ops.aten.concatenate.default,
418437
torch.ops.aten.stack.default,
419438
):
420-
assert isinstance(node.args[0], list)
421-
assert len(node.args[0]) != 0
439+
# first argument should be a non-empty list of nodes
440+
if not isinstance(node.args[0], list):
441+
raise TypeError(
442+
"Expected node.args[0] to be a list, got "
443+
f"{type(node.args[0]).__name__!r}"
444+
)
445+
if len(node.args[0]) == 0:
446+
raise ValueError("Expected non-empty list for node.args[0]")
422447

423448
shared_qspec = SharedQuantizationSpec((node.args[0][0], node))
424449
quant_properties.quant_inputs = [

0 commit comments

Comments
 (0)