提交 bd7f885a 编写于 作者: M Megvii Engine Team

fix(mge/pytest): remove __init__ in py-test

GitOrigin-RevId: e16eae9cbcb4038bd1abbf4c6089d9cbb7cd4afc
上级 06041f8a
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), "helpers"))
import numpy as np
from megengine import tensor
def _default_compare_fn(x, y):
np.testing.assert_allclose(x.numpy(), y, rtol=1e-6)
def opr_test(cases, func, compare_fn=_default_compare_fn, ref_fn=None, **kwargs):
"""
:param cases: the list which have dict element, the list length should be 2 for dynamic shape test.
and the dict should have input,
and should have output if ref_fn is None.
should use list for multiple inputs and outputs for each case.
:param func: the function to run opr.
:param compare_fn: the function to compare the result and expected, use assertTensorClose if None.
:param ref_fn: the function to generate expected data, should assign output if None.
Examples:
.. code-block::
dtype = np.float32
cases = [{"input": [10, 20]}, {"input": [20, 30]}]
opr_test(cases,
F.eye,
ref_fn=lambda n, m: np.eye(n, m).astype(dtype),
dtype=dtype)
"""
def check_results(results, expected):
if not isinstance(results, (tuple, list)):
results = (results,)
for r, e in zip(results, expected):
compare_fn(r, e)
def get_param(cases, idx):
case = cases[idx]
inp = case.get("input", None)
outp = case.get("output", None)
if inp is None:
raise ValueError("the test case should have input")
if not isinstance(inp, (tuple, list)):
inp = (inp,)
if ref_fn is not None and callable(ref_fn):
outp = ref_fn(*inp)
if outp is None:
raise ValueError("the test case should have output or reference function")
if not isinstance(outp, (tuple, list)):
outp = (outp,)
return inp, outp
if len(cases) == 0:
raise ValueError("should give one case at least")
if not callable(func):
raise ValueError("the input func should be callable")
inp, outp = get_param(cases, 0)
inp_tensor = [tensor(inpi) for inpi in inp]
results = func(*inp_tensor, **kwargs)
check_results(results, outp)
...@@ -13,9 +13,9 @@ else ...@@ -13,9 +13,9 @@ else
fi fi
pushd $(dirname "${BASH_SOURCE[0]}")/.. >/dev/null pushd $(dirname "${BASH_SOURCE[0]}")/.. >/dev/null
PYTHONPATH="." PY_IGNORE_IMPORTMISMATCH=1 python3 -m pytest $test_dirs -m 'not isolated_distributed' PYTHONPATH="." python3 -m pytest $test_dirs -m 'not isolated_distributed'
if [[ "$TEST_PLAT" == cuda ]]; then if [[ "$TEST_PLAT" == cuda ]]; then
echo "test GPU pytest now" echo "test GPU pytest now"
PYTHONPATH="." PY_IGNORE_IMPORTMISMATCH=1 python3 -m pytest $test_dirs -m 'isolated_distributed' PYTHONPATH="." python3 -m pytest $test_dirs -m 'isolated_distributed'
fi fi
popd >/dev/null popd >/dev/null
# -*- coding: utf-8 -*-
# MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...@@ -10,6 +10,7 @@ import itertools ...@@ -10,6 +10,7 @@ import itertools
import numpy as np import numpy as np
import pytest import pytest
from utils import opr_test
import megengine.core.ops.builtin as builtin import megengine.core.ops.builtin as builtin
import megengine.core.tensor.dtype as dtype import megengine.core.tensor.dtype as dtype
...@@ -21,68 +22,6 @@ from megengine.core.tensor.utils import make_shape_tuple ...@@ -21,68 +22,6 @@ from megengine.core.tensor.utils import make_shape_tuple
from megengine.test import assertTensorClose from megengine.test import assertTensorClose
def _default_compare_fn(x, y):
assertTensorClose(x.numpy(), y)
def opr_test(cases, func, compare_fn=_default_compare_fn, ref_fn=None, **kwargs):
"""
func: the function to run opr.
compare_fn: the function to compare the result and expected, use assertTensorClose if None.
ref_fn: the function to generate expected data, should assign output if None.
cases: the list which have dict element, the list length should be 2 for dynamic shape test.
and the dict should have input,
and should have output if ref_fn is None.
should use list for multiple inputs and outputs for each case.
kwargs: The additional kwargs for opr func.
simple examples:
dtype = np.float32
cases = [{"input": [10, 20]}, {"input": [20, 30]}]
opr_test(cases,
F.eye,
ref_fn=lambda n, m: np.eye(n, m).astype(dtype),
dtype=dtype)
"""
def check_results(results, expected):
if not isinstance(results, (tuple, list)):
results = (results,)
for r, e in zip(results, expected):
compare_fn(r, e)
def get_param(cases, idx):
case = cases[idx]
inp = case.get("input", None)
outp = case.get("output", None)
if inp is None:
raise ValueError("the test case should have input")
if not isinstance(inp, (tuple, list)):
inp = (inp,)
if ref_fn is not None and callable(ref_fn):
outp = ref_fn(*inp)
if outp is None:
raise ValueError("the test case should have output or reference function")
if not isinstance(outp, (tuple, list)):
outp = (outp,)
return inp, outp
if len(cases) == 0:
raise ValueError("should give one case at least")
if not callable(func):
raise ValueError("the input func should be callable")
inp, outp = get_param(cases, 0)
inp_tensor = [tensor(inpi) for inpi in inp]
results = func(*inp_tensor, **kwargs)
check_results(results, outp)
def test_where(): def test_where():
maskv0 = np.array([[1, 0], [0, 1]], dtype=np.bool_) maskv0 = np.array([[1, 0], [0, 1]], dtype=np.bool_)
xv0 = np.array([[1, np.inf], [np.nan, 4]], dtype=np.float32) xv0 = np.array([[1, np.inf], [np.nan, 4]], dtype=np.float32)
......
...@@ -9,78 +9,13 @@ ...@@ -9,78 +9,13 @@
from functools import partial from functools import partial
import numpy as np import numpy as np
from utils import opr_test
import megengine.functional as F import megengine.functional as F
from megengine import tensor from megengine import tensor
from megengine.test import assertTensorClose from megengine.test import assertTensorClose
def _default_compare_fn(x, y):
assertTensorClose(x.numpy(), y)
def opr_test(cases, func, compare_fn=_default_compare_fn, ref_fn=None, **kwargs):
"""
func: the function to run opr.
compare_fn: the function to compare the result and expected, use assertTensorClose if None.
ref_fn: the function to generate expected data, should assign output if None.
cases: the list which have dict element, the list length should be 2 for dynamic shape test.
and the dict should have input,
and should have output if ref_fn is None.
should use list for multiple inputs and outputs for each case.
kwargs: The additional kwargs for opr func.
simple examples:
dtype = np.float32
cases = [{"input": [10, 20]}, {"input": [20, 30]}]
opr_test(cases,
F.eye,
ref_fn=lambda n, m: np.eye(n, m).astype(dtype),
dtype=dtype)
"""
def check_results(results, expected):
if not isinstance(results, tuple):
results = (results,)
for r, e in zip(results, expected):
compare_fn(r, e)
def get_param(cases, idx):
case = cases[idx]
inp = case.get("input", None)
outp = case.get("output", None)
if inp is None:
raise ValueError("the test case should have input")
if not isinstance(inp, list):
inp = (inp,)
else:
inp = tuple(inp)
if ref_fn is not None and callable(ref_fn):
outp = ref_fn(*inp)
if outp is None:
raise ValueError("the test case should have output or reference function")
if not isinstance(outp, list):
outp = (outp,)
else:
outp = tuple(outp)
return inp, outp
if len(cases) == 0:
raise ValueError("should give one case at least")
if not callable(func):
raise ValueError("the input func should be callable")
inp, outp = get_param(cases, 0)
inp_tensor = [tensor(inpi) for inpi in inp]
results = func(*inp_tensor, **kwargs)
check_results(results, outp)
def common_test_reduce(opr, ref_opr): def common_test_reduce(opr, ref_opr):
data1_shape = (5, 6, 7) data1_shape = (5, 6, 7)
data2_shape = (2, 9, 12) data2_shape = (2, 9, 12)
......
...@@ -6,10 +6,12 @@ ...@@ -6,10 +6,12 @@
# Unless required by applicable law or agreed to in writing, # Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an # software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import os
import platform import platform
import numpy as np import numpy as np
import pytest import pytest
from utils import opr_test
import megengine.functional as F import megengine.functional as F
from megengine import tensor from megengine import tensor
...@@ -19,72 +21,6 @@ from megengine.distributed.helper import get_device_count_by_fork ...@@ -19,72 +21,6 @@ from megengine.distributed.helper import get_device_count_by_fork
from megengine.test import assertTensorClose from megengine.test import assertTensorClose
def _default_compare_fn(x, y):
assertTensorClose(x.numpy(), y)
def opr_test(cases, func, compare_fn=_default_compare_fn, ref_fn=None, **kwargs):
"""
func: the function to run opr.
compare_fn: the function to compare the result and expected, use assertTensorClose if None.
ref_fn: the function to generate expected data, should assign output if None.
cases: the list which have dict element, the list length should be 2 for dynamic shape test.
and the dict should have input,
and should have output if ref_fn is None.
should use list for multiple inputs and outputs for each case.
kwargs: The additional kwargs for opr func.
simple examples:
dtype = np.float32
cases = [{"input": [10, 20]}, {"input": [20, 30]}]
opr_test(cases,
F.eye,
ref_fn=lambda n, m: np.eye(n, m).astype(dtype),
dtype=dtype)
"""
def check_results(results, expected):
if not isinstance(results, tuple):
results = (results,)
for r, e in zip(results, expected):
compare_fn(r, e)
def get_param(cases, idx):
case = cases[idx]
inp = case.get("input", None)
outp = case.get("output", None)
if inp is None:
raise ValueError("the test case should have input")
if not isinstance(inp, list):
inp = (inp,)
else:
inp = tuple(inp)
if ref_fn is not None and callable(ref_fn):
outp = ref_fn(*inp)
if outp is None:
raise ValueError("the test case should have output or reference function")
if not isinstance(outp, list):
outp = (outp,)
else:
outp = tuple(outp)
return inp, outp
if len(cases) == 0:
raise ValueError("should give one case at least")
if not callable(func):
raise ValueError("the input func should be callable")
inp, outp = get_param(cases, 0)
inp_tensor = [tensor(inpi) for inpi in inp]
results = func(*inp_tensor, **kwargs)
check_results(results, outp)
def test_eye(): def test_eye():
dtype = np.float32 dtype = np.float32
cases = [{"input": [10, 20]}, {"input": [20, 30]}] cases = [{"input": [10, 20]}, {"input": [20, 30]}]
...@@ -265,37 +201,37 @@ def test_flatten(): ...@@ -265,37 +201,37 @@ def test_flatten():
data1 = np.random.random(data1_shape).astype(np.float32) data1 = np.random.random(data1_shape).astype(np.float32)
def compare_fn(x, y): def compare_fn(x, y):
assert x.numpy().shape == y[0] assert x.shape[0] == y
output0 = (2 * 3 * 4 * 5,) output0 = (2 * 3 * 4 * 5,)
output1 = (4 * 5 * 6 * 7,) output1 = (4 * 5 * 6 * 7,)
cases = [ cases = [
{"input": data0, "output": (output0,)}, {"input": data0, "output": output0},
{"input": data1, "output": (output1,)}, {"input": data1, "output": output1},
] ]
opr_test(cases, F.flatten, compare_fn=compare_fn) opr_test(cases, F.flatten, compare_fn=compare_fn)
output0 = (2, 3 * 4 * 5) output0 = (2, 3 * 4 * 5)
output1 = (4, 5 * 6 * 7) output1 = (4, 5 * 6 * 7)
cases = [ cases = [
{"input": data0, "output": (output0,)}, {"input": data0, "output": output0},
{"input": data1, "output": (output1,)}, {"input": data1, "output": output1},
] ]
opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=1) opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=1)
output0 = (2, 3, 4 * 5) output0 = (2, 3, 4 * 5)
output1 = (4, 5, 6 * 7) output1 = (4, 5, 6 * 7)
cases = [ cases = [
{"input": data0, "output": (output0,)}, {"input": data0, "output": output0},
{"input": data1, "output": (output1,)}, {"input": data1, "output": output1},
] ]
opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=2) opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=2)
output0 = (2, 3 * 4, 5) output0 = (2, 3 * 4, 5)
output1 = (4, 5 * 6, 7) output1 = (4, 5 * 6, 7)
cases = [ cases = [
{"input": data0, "output": (output0,)}, {"input": data0, "output": output0},
{"input": data1, "output": (output1,)}, {"input": data1, "output": output1},
] ]
opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=1, end_axis=2) opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=1, end_axis=2)
...@@ -310,7 +246,7 @@ def test_broadcast(): ...@@ -310,7 +246,7 @@ def test_broadcast():
data2 = np.random.random(input2_shape).astype(np.float32) data2 = np.random.random(input2_shape).astype(np.float32)
def compare_fn(x, y): def compare_fn(x, y):
assert x.numpy().shape == y assert x.shape[0] == y
cases = [ cases = [
{"input": [data1, output1_shape], "output": output1_shape}, {"input": [data1, output1_shape], "output": output1_shape},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册