From c051bf80a560748efac15d2186bc81d3ec5d1b6e Mon Sep 17 00:00:00 2001 From: Sebastian Larsson Date: Fri, 25 Apr 2025 15:25:33 +0200 Subject: [PATCH] Arm backend: Replace asserts with error handling in upsample operators Update `op_upsample_bilinear2d.py` and `op_upsample_nearest2d.py` classes to replace `assert` checks with `ValueError` exceptions for improved error handling and code robustness. - Change static shape checks from assertions to conditional raises of `ValueError` for clear runtime error communication. - Replaced `assert` checks for int16 range validation of `scale_n_yx`, `scale_d_yx`, and `border_yx` with explicit value range checks that raise `ValueError` when bounds are breached. Signed-off-by: Sebastian Larsson Change-Id: I0f94182de97ab97abf752f4c618f2d435687ed3a --- backends/arm/operators/op_upsample_bilinear2d.py | 14 ++++++++------ backends/arm/operators/op_upsample_nearest2d.py | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/backends/arm/operators/op_upsample_bilinear2d.py b/backends/arm/operators/op_upsample_bilinear2d.py index 52eebf70900..3a238709223 100644 --- a/backends/arm/operators/op_upsample_bilinear2d.py +++ b/backends/arm/operators/op_upsample_bilinear2d.py @@ -34,9 +34,8 @@ def define_node( inputs: List[TosaArg], output: TosaArg, ) -> None: - assert ( - inputs[0].shape is not None and output.shape is not None - ), "Only static shapes are supported" + if inputs[0].shape is None or output.shape is None: + raise ValueError("Only static shapes are supported") input_dtype = inputs[0].dtype @@ -55,9 +54,12 @@ def define_node( def in_int16_range(x): return torch.all(x >= -(2**15)) and torch.all(x <= 2**15 - 1) - assert in_int16_range(scale_n_yx) - assert in_int16_range(scale_d_yx) - assert in_int16_range(border_yx) + if not in_int16_range(scale_n_yx): + raise ValueError("scale_n_yx is out of the int16 range") + if not in_int16_range(scale_d_yx): + raise ValueError("scale_d_yx is out of the int16 range") + if not in_int16_range(border_yx): + raise ValueError("border_yx is out of the int16 range") attr = ts.TosaSerializerAttribute() attr.ResizeAttribute( diff --git a/backends/arm/operators/op_upsample_nearest2d.py b/backends/arm/operators/op_upsample_nearest2d.py index 63694b715f0..c08896c2cdc 100644 --- a/backends/arm/operators/op_upsample_nearest2d.py +++ b/backends/arm/operators/op_upsample_nearest2d.py @@ -36,9 +36,8 @@ def define_node( ) -> None: import tosa_tools.v0_80.serializer.tosa_serializer as ts # type: ignore - assert ( - inputs[0].shape is not None and output.shape is not None - ), "Only static shapes are supported" + if inputs[0].shape is None or output.shape is None: + raise ValueError("Only static shapes are supported") # tosa_shape output is NHWC, take HW input_size_yx = torch.tensor( @@ -55,9 +54,12 @@ def define_node( def in_int16_range(x): return torch.all(x >= -(2**15)) and torch.all(x <= 2**15 - 1) - assert in_int16_range(scale_n_yx) - assert in_int16_range(scale_d_yx) - assert in_int16_range(border_yx) + if not in_int16_range(scale_n_yx): + raise ValueError("scale_n_yx is out of the int16 range") + if not in_int16_range(scale_d_yx): + raise ValueError("scale_d_yx is out of the int16 range") + if not in_int16_range(border_yx): + raise ValueError("border_yx is out of the int16 range") attr = ts.TosaSerializerAttribute() attr.ResizeAttribute(