Skip to content

Lint #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 31, 2024
Merged

Lint #89

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions _doc/examples/plot_benchmark_rf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ def skl2onnx_convert_lightgbm(scope, operator, container):
)

options = scope.get_options(operator.raw_operator)
if "split" in options:
operator.split = options["split"]
else:
operator.split = None
operator.split = options.get("split", None)
convert_lightgbm(scope, operator, container)


Expand Down Expand Up @@ -103,7 +100,7 @@ def measure_inference(fct, X, repeat, max_time=5, quantile=1):
:return: number of runs, sum of the time, average, median
"""
times = []
for n in range(repeat):
for _n in range(repeat):
perf = time.perf_counter()
fct(X)
delta = time.perf_counter() - perf
Expand Down Expand Up @@ -241,7 +238,10 @@ def measure_inference(fct, X, repeat, max_time=5, quantile=1):
# onnxruntime
bar.set_description(f"J={n_j} E={n_estimators} D={max_depth} predictO")
r, t, mean, med = measure_inference(
lambda x: sess.run(None, {"X": x}), X, repeat=repeat, max_time=max_time
lambda x, sess=sess: sess.run(None, {"X": x}),
X,
repeat=repeat,
max_time=max_time,
)
o2 = obs.copy()
o2.update(dict(avg=mean, med=med, n_runs=r, ttime=t, name="ort_"))
Expand Down
4 changes: 2 additions & 2 deletions _doc/examples/plot_onnxruntime.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ def loop(n=1000):
x = np.random.randn(n, 2).astype(np.float32)
y = np.random.randn(n, 2).astype(np.float32)

obs = measure_time(lambda: myloss(x, y))
obs = measure_time(lambda x=x, y=y: myloss(x, y))
obs["name"] = "numpy"
obs["n"] = n
data.append(obs)

xort = OrtTensor.from_array(x)
yort = OrtTensor.from_array(y)
obs = measure_time(lambda: ort_myloss(xort, yort))
obs = measure_time(lambda xort=xort, yort=yort: ort_myloss(xort, yort))
obs["name"] = "ort"
obs["n"] = n
data.append(obs)
Expand Down
16 changes: 13 additions & 3 deletions _unittests/ut_array_api/test_hypothesis_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import warnings
from os import getenv
from functools import reduce
import packaging.version as pv
import numpy as np
from operator import mul
from hypothesis import given
Expand Down Expand Up @@ -44,9 +45,12 @@ class TestHypothesisArraysApis(ExtTestCase):

@classmethod
def setUpClass(cls):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
from numpy import array_api as xp
try:
import array_api_strict as xp
except ImportError:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
from numpy import array_api as xp

api_version = getenv(
"ARRAY_API_TESTS_VERSION",
Expand All @@ -63,6 +67,9 @@ def test_strategies(self):
self.assertNotEmpty(self.xps)
self.assertNotEmpty(self.onxps)

@unittest.skipIf(
pv.Version(np.__version__) >= pv.Version("2.0"), reason="abandonned"
)
def test_scalar_strategies(self):
dtypes = dict(
integer_dtypes=self.xps.integer_dtypes(),
Expand Down Expand Up @@ -139,6 +146,9 @@ def fctonx(x, kw):
fctonx()
self.assertEqual(len(args_onxp), len(args_np))

@unittest.skipIf(
pv.Version(np.__version__) >= pv.Version("2.0"), reason="abandonned"
)
def test_square_sizes_strategies(self):
dtypes = dict(
integer_dtypes=self.xps.integer_dtypes(),
Expand Down
26 changes: 22 additions & 4 deletions _unittests/ut_light_api/test_backend_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import packaging.version as pv
import numpy
from numpy.testing import assert_allclose
from onnx.defs import onnx_opset_version
import onnx.backend.base
import onnx.backend.test
import onnx.shape_inference
Expand All @@ -31,7 +32,6 @@

class ReferenceImplementationError(RuntimeError):
"Fails, export cannot be compared."
pass


class ExportWrapper:
Expand Down Expand Up @@ -64,7 +64,8 @@ def run(
expected = self.expected_sess.run(names, feeds)
except (RuntimeError, AssertionError, TypeError, KeyError) as e:
raise ReferenceImplementationError(
f"ReferenceImplementation fails with {onnx_simple_text_plot(self.model)}"
f"ReferenceImplementation fails with "
f"{onnx_simple_text_plot(self.model)}"
f"\n--RAW--\n{self.model}"
) from e

Expand All @@ -85,7 +86,7 @@ def run(
new_code = "\n".join(
[f"{i+1:04} {line}" for i, line in enumerate(code.split("\n"))]
)
raise AssertionError(f"ERROR {e}\n{new_code}")
raise AssertionError(f"ERROR {e}\n{new_code}") # noqa: B904

locs = {
"np": numpy,
Expand Down Expand Up @@ -154,7 +155,8 @@ def run(
):
if a.tolist() != b.tolist():
raise AssertionError(
f"Text discrepancies for api {api!r} with a.dtype={a.dtype} "
f"Text discrepancies for api {api!r} "
f"with a.dtype={a.dtype} "
f"and b.dtype={b.dtype}"
f"\n--BASE--\n{onnx_simple_text_plot(self.model)}"
f"\n--EXP[{api}]--\n{onnx_simple_text_plot(export_model)}"
Expand Down Expand Up @@ -275,6 +277,22 @@ def run_node(cls, node, inputs, device=None, outputs_info=None, **kwargs):
")"
)

if onnx_opset_version() < 22:
backend_test.exclude(
"("
"test_dft_inverse_cpu"
"|test_dft_inverse_opset19_cpu"
"|test_lppool_1d_default_cpu"
"|test_lppool_2d_default_cpu"
"|test_lppool_2d_dilations_cpu"
"|test_lppool_2d_pads_cpu"
"|test_lppool_2d_same_lower_cpu"
"|test_lppool_2d_same_upper_cpu"
"|test_lppool_2d_strides_cpu"
"|test_lppool_3d_default_cpu"
")"
)

if pv.Version(onnx_version) < pv.Version("1.16.0"):
backend_test.exclude("(test_strnorm|test_range_)")

Expand Down
2 changes: 1 addition & 1 deletion _unittests/ut_light_api/test_light_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def g(self):
def ah(self):
return True

setattr(A, "h", ah)
setattr(A, "h", ah) # noqa: B010

self.assertTrue(A().h())
self.assertIn("(self)", str(inspect.signature(A.h)))
Expand Down
1 change: 0 additions & 1 deletion _unittests/ut_plotting/test_dot_plot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
import os
import unittest

Expand Down
2 changes: 1 addition & 1 deletion _unittests/ut_plotting/test_text_plot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
import os
import textwrap
import unittest
Expand Down Expand Up @@ -95,6 +94,7 @@ def test_onnx_text_plot_tree_cls_2(self):
+f 0:1 1:0 2:0
"""
).strip(" \n\r")
res = res.replace("np.float32(", "").replace(")", "")
self.assertEqual(expected, res.strip(" \n\r"))

@ignore_warnings((UserWarning, FutureWarning))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,25 @@ def run_node(cls, node, inputs, device=None, outputs_info=None, **kwargs):
# The following tests fail due to a type mismatch.
backend_test.exclude("(test_eyelike_without_dtype)")

if onnx_opset_version() < 22:
backend_test.exclude(
"("
"test_adagrad_cpu"
"|test_adagrad_multiple_cpu"
"|test_dft_inverse_cpu"
"|test_dft_inverse_opset19_cpu"
"|test_lppool_1d_default_cpu"
"|test_lppool_2d_default_cpu"
"|test_lppool_2d_dilations_cpu"
"|test_lppool_2d_pads_cpu"
"|test_lppool_2d_same_lower_cpu"
"|test_lppool_2d_same_upper_cpu"
"|test_lppool_2d_strides_cpu"
"|test_lppool_3d_default_cpu"
")"
)


# The following tests fail due to discrepancies (small but still higher than 1e-7).
backend_test.exclude("test_adam_multiple") # 1e-2

Expand Down
10 changes: 8 additions & 2 deletions _unittests/ut_translate_api/test_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,14 @@ def test_export_if(self):
self.assertEqualArray(np.array([1], dtype=np.int64), got[0])

code = translate(onx)
selse = "g().cst(np.array([0], dtype=np.int64)).rename('Z').bring('Z').vout(elem_type=TensorProto.FLOAT)"
sthen = "g().cst(np.array([1], dtype=np.int64)).rename('Z').bring('Z').vout(elem_type=TensorProto.FLOAT)"
selse = (
"g().cst(np.array([0], dtype=np.int64)).rename('Z')."
"bring('Z').vout(elem_type=TensorProto.FLOAT)"
)
sthen = (
"g().cst(np.array([1], dtype=np.int64)).rename('Z')."
"bring('Z').vout(elem_type=TensorProto.FLOAT)"
)
expected = dedent(
f"""
(
Expand Down
2 changes: 1 addition & 1 deletion _unittests/ut_translate_api/test_translate_classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def test_fft(self):
new_code = "\n".join(
[f"{i+1:04} {line}" for i, line in enumerate(code.split("\n"))]
)
raise AssertionError(f"ERROR {e}\n{new_code}")
raise AssertionError(f"ERROR {e}\n{new_code}") # noqa: B904

def test_aionnxml(self):
onx = (
Expand Down
24 changes: 11 additions & 13 deletions _unittests/ut_validation/test_f8.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_fe5m2_to_float32_paper(self):
self.assertEqual(fe5m2_to_float32(int("11111100", 2)), -numpy.inf)

def test_fe4m3fn_to_float32_all(self):
for i in range(0, 256):
for i in range(256):
a = fe4m3_to_float32_float(i)
b = fe4m3_to_float32(i)
if numpy.isnan(a):
Expand All @@ -97,7 +97,7 @@ def test_fe4m3fn_to_float32_all(self):
self.assertEqual(a, b)

def test_fe4m3fn_to_float32_all_ml_types(self):
for i in range(0, 256):
for i in range(256):
a = fe4m3_to_float32_float(i)
b = fe4m3_to_float32(i)
c = new_cvt_float32_to_e4m3fn(b)
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_search_float32_into_fe5m2_simple(self):
self.assertEqual(b1, b2)

def test_search_float32_into_fe4m3fn_equal(self):
values = [(fe4m3_to_float32_float(i), i) for i in range(0, 256)]
values = [(fe4m3_to_float32_float(i), i) for i in range(256)]
values.sort()

for value, expected in values:
Expand All @@ -208,7 +208,7 @@ def test_search_float32_into_fe4m3fn_equal(self):
self.assertIn(nf, (0, 128))

def test_search_float32_into_fe5m2_equal(self):
values = [(fe5m2_to_float32_float(i), i) for i in range(0, 256)]
values = [(fe5m2_to_float32_float(i), i) for i in range(256)]
values.sort()

for value, expected in values:
Expand All @@ -233,7 +233,7 @@ def test_search_float32_into_fe5m2_equal(self):
self.assertEqual(fe5m2_to_float32(nf), float(cf))

def test_search_float32_into_fe4m3fn(self):
values = [(fe4m3_to_float32_float(i), i) for i in range(0, 256)]
values = [(fe4m3_to_float32_float(i), i) for i in range(256)]
values.sort()

obs = []
Expand Down Expand Up @@ -308,7 +308,7 @@ def test_search_float32_into_fe4m3fn(self):
)

def test_search_float32_into_fe5m2(self):
values = [(fe5m2_to_float32_float(i), i) for i in range(0, 256)]
values = [(fe5m2_to_float32_float(i), i) for i in range(256)]
values.sort()

obs = []
Expand Down Expand Up @@ -651,7 +651,7 @@ def test_search_float32_into_fe5m2fnuz_simple(self):
self.assertEqual(expected, got)

def test_fe4m3fnuz_to_float32_all(self):
for i in range(0, 256):
for i in range(256):
a = fe4m3_to_float32_float(i, uz=True)
b = fe4m3_to_float32(i, uz=True)
if numpy.isnan(a):
Expand All @@ -660,7 +660,7 @@ def test_fe4m3fnuz_to_float32_all(self):
self.assertEqual(a, b)

def test_fe5m2fnuz_to_float32_all(self):
for i in range(0, 256):
for i in range(256):
a = fe5m2_to_float32_float(i, fn=True, uz=True)
b = fe5m2_to_float32(i, fn=True, uz=True)
if numpy.isnan(a):
Expand All @@ -669,7 +669,7 @@ def test_fe5m2fnuz_to_float32_all(self):
self.assertEqual(a, b)

def test_search_float32_into_fe4m3fnuz(self):
values = [(fe4m3_to_float32_float(i, uz=True), i) for i in range(0, 256)]
values = [(fe4m3_to_float32_float(i, uz=True), i) for i in range(256)]
values.sort()

obs = []
Expand Down Expand Up @@ -715,9 +715,7 @@ def test_search_float32_into_fe4m3fnuz(self):
)

def test_search_float32_into_fe5m2fnuz(self):
values = [
(fe5m2_to_float32_float(i, fn=True, uz=True), i) for i in range(0, 256)
]
values = [(fe5m2_to_float32_float(i, fn=True, uz=True), i) for i in range(256)]
values.sort()

obs = []
Expand Down Expand Up @@ -1235,7 +1233,7 @@ def test_nan(self):
expected,
)
]
for i in range(0, 23):
for i in range(23):
v = 0x7F800000 | (1 << i)
f = numpy.uint32(v).view(numpy.float32)
values.append((i, v, f, expected))
Expand Down
2 changes: 1 addition & 1 deletion _unittests/ut_xrun_doc/test_documentation_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def run_test(self, fold: str, name: str, verbose=0) -> int:
if verbose:
print(f"failed: {name!r} due to missing dot.")
return 0
raise AssertionError(
raise AssertionError( # noqa: B904
"Example '{}' (cmd: {} - exec_prefix='{}') "
"failed due to\n{}"
"".format(name, cmds, sys.exec_prefix, st)
Expand Down
Loading
Loading