: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 func
(
cases,
func,
compare_fn=_default_compare_fn,
ref_fn=None,
test_trace=True,
network=None,
**kwargs
)
| 38 | |
| 39 | |
| 40 | def opr_test( |
| 41 | cases, |
| 42 | func, |
| 43 | compare_fn=_default_compare_fn, |
| 44 | ref_fn=None, |
| 45 | test_trace=True, |
| 46 | network=None, |
| 47 | **kwargs |
| 48 | ): |
| 49 | """ |
| 50 | :param cases: the list which have dict element, the list length should be 2 for dynamic shape test. |
| 51 | and the dict should have input, |
| 52 | and should have output if ref_fn is None. |
| 53 | should use list for multiple inputs and outputs for each case. |
| 54 | :param func: the function to run opr. |
| 55 | :param compare_fn: the function to compare the result and expected, use |
| 56 | ``np.testing.assert_allclose`` if None. |
| 57 | :param ref_fn: the function to generate expected data, should assign output if None. |
| 58 | |
| 59 | Examples: |
| 60 | |
| 61 | .. code-block:: |
| 62 | |
| 63 | dtype = np.float32 |
| 64 | cases = [{"input": [10, 20]}, {"input": [20, 30]}] |
| 65 | opr_test(cases, |
| 66 | F.eye, |
| 67 | ref_fn=lambda n, m: np.eye(n, m).astype(dtype), |
| 68 | dtype=dtype) |
| 69 | |
| 70 | """ |
| 71 | |
| 72 | def check_results(results, expected, check_shape=True): |
| 73 | if not isinstance(results, (tuple, list)): |
| 74 | results = (results,) |
| 75 | for r, e in zip(results, expected): |
| 76 | if not isinstance(r, (tensor, VarNode)): |
| 77 | r = tensor(r) |
| 78 | if check_shape: |
| 79 | r_shape = r.numpy().shape |
| 80 | e_shape = e.shape if isinstance(e, np.ndarray) else () |
| 81 | assert r_shape == e_shape |
| 82 | compare_fn(r, e) |
| 83 | |
| 84 | def get_param(cases, idx): |
| 85 | case = cases[idx] |
| 86 | inp = case.get("input", None) |
| 87 | outp = case.get("output", None) |
| 88 | if inp is None: |
| 89 | raise ValueError("the test case should have input") |
| 90 | if not isinstance(inp, (tuple, list)): |
| 91 | inp = (inp,) |
| 92 | if ref_fn is not None and callable(ref_fn): |
| 93 | outp = ref_fn(*inp) |
| 94 | if outp is None: |
| 95 | raise ValueError("the test case should have output or reference function") |
| 96 | if not isinstance(outp, (tuple, list)): |
| 97 | outp = (outp,) |