Skip to content

Commit d24eda4

Browse files
Arm backend: Replace asserts with exceptions in permutation code (#10774)
Refactor assertion statements to raise ValueErrors for better error handling in permutation matrix and vector transformations. Ensure that conditions are checked and appropriate exceptions are raised to enhance code robustness and readability. Signed-off-by: Sebastian Larsson <[email protected]>
1 parent c352813 commit d24eda4

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

backends/arm/operators/op_permute.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,26 @@ def permutation_matrix_to_vector(permutation_matrix: torch.Tensor) -> list[int]:
4646
(1,0,2)
4747
"""
4848
N = len(permutation_matrix)
49-
assert N == len(
50-
permutation_matrix[0]
51-
), f"A permutation matrix must be square, got shape {permutation_matrix.shape}"
49+
if N != len(permutation_matrix[0]):
50+
raise ValueError(
51+
f"A permutation matrix must be square, got shape {permutation_matrix.shape}"
52+
)
5253

5354
p = [0] * N
5455
for row_index, row in enumerate(permutation_matrix):
5556
saw_one = False
5657
for col_index, value in enumerate(row):
5758
if value == 1:
58-
assert (
59-
not saw_one
60-
), f"A permutation matrix can only have one 1 per row, got row {row}."
59+
if saw_one:
60+
raise ValueError(
61+
f"A permutation matrix can only have one 1 per row, got {row=}"
62+
)
6163
p[row_index] = col_index
6264
saw_one = True
63-
else:
64-
assert (
65-
value == 0
66-
), f"A permutation matrix only contains 1's and 0's, got value {value}."
65+
elif value != 0:
66+
raise ValueError(
67+
f"A permutation matrix only contains 1's and 0's, got {value=}"
68+
)
6769
return p
6870

6971

0 commit comments

Comments
 (0)