Skip to content

Commit 8f59153

Browse files
committed
replaces RuntimeError by make_sure
1 parent b43f6d0 commit 8f59153

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

tests/test_backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3387,7 +3387,7 @@ def func_neg(x):
33873387
x_val = np.random.random(size=[4, 3]).astype(np.float32) * 2048. - 1024 * 3.
33883388
try:
33893389
self._run_test_case(func_neg, [_OUTPUT], {_INPUT: x_val}, rtol=1e-6, atol=1e-4)
3390-
except RuntimeError:
3390+
except ValueError:
33913391
pass
33923392

33933393

tf2onnx/onnx_opset/quantize.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from tf2onnx import utils
1818
from tf2onnx.handler import tf_op
19+
from tf2onnx.utils import make_sure
1920

2021
logger = logging.getLogger(__name__)
2122

@@ -34,14 +35,13 @@ def version_10(cls, ctx, node, **kwargs):
3435
narrow_range = node.get_attr("narrow_range").i
3536
num_bits = node.get_attr("num_bits").i
3637

37-
if narrow_range:
38-
raise RuntimeError(
39-
"Unable to convert node FakeQuantWithMinMaxArgs with "
40-
"narrow_range=%r" % narrow_range)
41-
if num_bits != 8:
42-
raise RuntimeError(
43-
"Unable to convert node FakeQuantWithMinMaxArgs with "
44-
"num_bits=%r" % num_bits)
38+
make_sure(
39+
not narrow_range,
40+
"Unable to convert node FakeQuantWithMinMaxArgs with narrow_range=%r",
41+
narrow_range)
42+
make_sure(num_bits == 8,
43+
"Unable to convert node FakeQuantWithMinMaxArgs with "
44+
"num_bits=%r", num_bits)
4545

4646
scale = (amax - amin) / (2 ** num_bits - 1)
4747
min_adj = np.around(amin / scale)
@@ -55,12 +55,11 @@ def version_10(cls, ctx, node, **kwargs):
5555
utils.make_name("{}_scaley".format(node.name)),
5656
np.array(scale, dtype=np.float32))
5757
zero = np.array(-min_adj, dtype=np.uint8)
58-
if zero != -min_adj:
59-
raise RuntimeError(
60-
"Cannot convert FakeQuantWithMinMaxArgs with "
61-
"min={} max={} numbits={} because zero_scale={} "
62-
"is outside uint8 boundary".format(
63-
amin, amax, num_bits, -min_adj))
58+
make_sure(zero == -min_adj,
59+
"Cannot convert FakeQuantWithMinMaxArgs with "
60+
"min={} max={} numbits={} because zero_scale={} "
61+
"is outside uint8 boundary",
62+
amin, amax, num_bits, -min_adj)
6463
zero_point = ctx.make_const(
6564
utils.make_name("{}_zpy".format(node.name)), zero)
6665

0 commit comments

Comments
 (0)