Skip to content

Commit

Permalink
Fix/pytest (#1021)
Browse files Browse the repository at this point in the history
* fix pytest result

* fix typo in pytest

* remove config.yam anf check_result()

* fix trilu

* fix build

---------

Co-authored-by: FusionBolt <RealFusionBolt@gmail.com>
Co-authored-by: zhangyang2057 <zhangyang@canaan-creative.com>
Co-authored-by: Curio Yang <curioyhq@gmail.com>
  • Loading branch information
4 people committed Jul 28, 2023
1 parent 84a4648 commit fde7b77
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 164 deletions.
2 changes: 1 addition & 1 deletion src/Native/src/kernels/stackvm/reference/ref_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ transpose(datatype_t type, const gsl::byte *src, gsl::byte *dest,
NNCASE_API result<void>
trilu(datatype_t type, const gsl::byte *input, gsl::byte *output,
gsl::span<const size_t> in_shape, gsl::span<const size_t> in_strides,
gsl::span<const size_t> out_strides, long k, bool upper) noexcept;
gsl::span<const size_t> out_strides, int64_t k, bool upper) noexcept;

NNCASE_API result<void>
unary(typecode_t dtype, runtime::stackvm::unary_op_t op, const gsl::byte *input,
Expand Down
16 changes: 10 additions & 6 deletions src/Native/src/kernels/stackvm/reference/trilu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ template <typename T>
result<void>
trilu_impl(const T *input, T *output, gsl::span<const size_t> in_shape,
gsl::span<const size_t> in_strides,
gsl::span<const size_t> out_strides, long k, bool upper) {
gsl::span<const size_t> out_strides, int64_t k, bool upper) {
return apply(in_shape, [&](gsl::span<const size_t> index) -> result<void> {
auto h = index.size() - 2;
auto w = index.size() - 1;
int64_t h = index[index.size() - 2];
int64_t w = index[index.size() - 1];

if (upper) {
auto value = w >= (h + k) ? 0 : input[offset(in_strides, index)];
auto wV = h + k;
auto value = wV <= w ? input[offset(in_strides, index)] : 0;
output[offset(out_strides, index)] = value;
} else {
auto value = w >= (h + k) ? input[offset(in_strides, index)] : 0;
auto wV = h + k + 1;
wV = wV < 0 ? 0 : wV;
auto value = w < wV ? input[offset(in_strides, index)] : 0;
output[offset(out_strides, index)] = value;
}
return ok();
Expand All @@ -50,7 +54,7 @@ trilu_impl(const T *input, T *output, gsl::span<const size_t> in_shape,
result<void> nncase::kernels::stackvm::reference::trilu(
datatype_t type, const gsl::byte *input, gsl::byte *output,
gsl::span<const size_t> in_shape, gsl::span<const size_t> in_strides,
gsl::span<const size_t> out_strides, long k, bool upper) noexcept {
gsl::span<const size_t> out_strides, int64_t k, bool upper) noexcept {
switch (runtime::get_bytes(type)) {
TRILU_IMPL(1, uint8_t);
TRILU_IMPL(2, uint16_t);
Expand Down
30 changes: 15 additions & 15 deletions tests/compare_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def segment_close(gt: np.ndarray, pred: np.ndarray):
return ret


simarity_func = {
similarity_func = {
'cosine': cosine,
'euclidean': euclidean,
'allclose': np.allclose,
Expand All @@ -48,7 +48,7 @@ def segment_close(gt: np.ndarray, pred: np.ndarray):
def compare_binfile(result_path: Tuple[str, str],
ground_truth_path: Tuple[str, str],
dtype,
simarity_name: str = 'cosine',
similarity_name: str = 'cosine',
threshold: float = 0.99,
hist: bool = True) -> bool:
# NOTE the result_path is Tuple[ bin_path, txt_path ]
Expand All @@ -62,46 +62,46 @@ def compare_binfile(result_path: Tuple[str, str],
gt_arr = np.fromfile(ground_truth_path_bin, dtype).astype(np.float32)
pred_arr = np.fromfile(result_path_bin, dtype).astype(np.float32)
if gt_arr.size == pred_arr.size:
simarity = simarity_func[simarity_name](gt_arr, pred_arr)
similarity = similarity_func[similarity_name](gt_arr, pred_arr)
else:
raise ValueError("The number of elements in gt and result not match\n")
if hist and not test_utils.in_ci:
y, x = np.histogram(gt_arr - pred_arr, 100)
p = Path(result_path_bin)
np.savetxt(str(p.parent / (p.stem + '_hist.csv')),
np.stack((x[:-1], y)).T, fmt='%f', delimiter=',')
simarity_info = f"\n{simarity_name} similarity = {simarity}, threshold = {threshold}\n"
if simarity_name in ['cosine', 'euclidean', 'segment']:
similarity_info = f"\n{similarity_name} similarity = {similarity}, threshold = {threshold}\n"
if similarity_name in ['cosine', 'euclidean', 'segment']:
compare_op = lt
else:
compare_op = gt
if compare_op(simarity, threshold):
return False, simarity_info
return True, simarity_info
if compare_op(similarity, threshold):
return False, similarity_info
return True, similarity_info


def compare_ndarray(expected: np.ndarray,
actual: np.ndarray,
simarity_name: str = 'cosine',
similarity_name: str = 'cosine',
threshold: float = 0.99,
dump_hist: bool = True,
dump_file: str = 'hist.csv') -> bool:

if expected.size == actual.size:
simarity = simarity_func[simarity_name](expected.flatten(), actual.flatten())
similarity = similarity_func[similarity_name](expected.flatten(), actual.flatten())
else:
raise ValueError("The number of elements in gt and result not match\n")

if dump_hist:
y, x = np.histogram(expected - actual, 100)
np.savetxt(dump_file, np.stack((x[:-1], y)).T, fmt='%f', delimiter=',')
simarity_info = f"\n{simarity_name} similarity = {simarity}, threshold = {threshold}\n"
similarity_info = f"\n{similarity_name} similarity = {similarity}, threshold = {threshold}\n"

if simarity_name in ['cosine', 'euclidean', 'segment']:
if similarity_name in ['cosine', 'euclidean', 'segment']:
compare_op = lt
else:
compare_op = gt

if compare_op(simarity, threshold):
return False, simarity_info
return True, simarity_info
if compare_op(similarity, threshold):
return False, similarity_info
return True, similarity_info
6 changes: 3 additions & 3 deletions tests/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ args = []
[target.cpu]
eval = true
infer = true
simarity_name = 'cosine'
similarity_name = 'cosine'

[target.cpu.mode.noptq]
enabled = false
Expand All @@ -95,7 +95,7 @@ threshold = 0.98
[target.k510]
eval = true
infer = true
simarity_name = 'cosine'
similarity_name = 'cosine'

[target.k510.mode.noptq]
enabled = false
Expand All @@ -108,7 +108,7 @@ threshold = 0.98
[target.k230]
eval = true
infer = true
simarity_name = 'cosine'
similarity_name = 'cosine'

[target.k230.mode.noptq]
enabled = false
Expand Down
124 changes: 0 additions & 124 deletions tests/config.yml

This file was deleted.

5 changes: 3 additions & 2 deletions tests/importer/onnx_/basic/test_trilu.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def _make_module(in_shape, kvalue, upper):
]

ks = [
0,
1,
2,
-1,
Expand All @@ -83,7 +84,7 @@ def _make_module(in_shape, kvalue, upper):
@pytest.mark.parametrize('in_shape', in_shapes)
@pytest.mark.parametrize('k', ks)
@pytest.mark.parametrize('upper', uppers)
def test_erf(in_shape, upper, k, request):
def test_trilu(in_shape, upper, k, request):
model_def = _make_module(in_shape, k, upper)

runner = OnnxTestRunner(request.node.name)
Expand All @@ -92,4 +93,4 @@ def test_erf(in_shape, upper, k, request):


if __name__ == "__main__":
pytest.main(['-vv', 'test_trilu.py'])
pytest.main(['-vv', 'test_trilu.py', '-s'])
4 changes: 2 additions & 2 deletions tests/importer/tflite_/model/test_densenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def _make_module(in_shape):
# - matchs:
# target: k210
# ptq: true
# simarity_name: segment
# similarity_name: segment
# threshold: true
# - matchs:
# target: k510
# ptq: true
# simarity_name: segment
# similarity_name: segment
# threshold: true
# """
# runner = TfliteTestRunner(request.node.name, overwirte_configs=overwrite_cfg)
Expand Down
17 changes: 6 additions & 11 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ def run(self, model_file: Union[List[str], str]):
mode_dir = os.path.join(target_dir, k_mode)
shutil.move(tmp_dir, mode_dir)
judge, result = self.compare_results(
expected, actual, stage, k_target, v_target['simarity_name'], k_mode, v_mode['threshold'], self.cfg['dump_hist'], mode_dir)
expected, actual, stage, k_target, v_target['similarity_name'], k_mode, v_mode['threshold'], self.cfg['dump_hist'], mode_dir)

if not judge:
if test_utils.in_ci():
self.clear(self.case_dir)
assert f"Fault result in {stage} + {result}"
assert (judge), f"Fault result in {stage} + {result}"

if test_utils.in_ci():
self.clear(self.case_dir)
Expand All @@ -269,11 +269,6 @@ def translate_shape(self, shape):
def set_shape_var(self, dict: Dict[str, int]):
self.shape_vars = dict

def check_result(self, expected, actual, stage, target, simarity_name, mode, threshold, dump_dir):
judge, result = self.compare_results(
expected, actual, stage, target, simarity_name, mode, threshold, self.cfg['dump_hist'], dump_dir)
assert(judge), f"Fault result in {stage} + {result}"

def set_quant_opt(self, compiler: nncase.Compiler):
compile_opt = self.cfg['compile_opt']
ptq_opt = self.cfg['ptq_opt']
Expand Down Expand Up @@ -409,18 +404,18 @@ def generate_data(self, name: str, inputs: List[Dict], compile_opt, generator_cf
def compare_results(self,
ref_ouputs: List[np.ndarray],
test_outputs: List[np.ndarray],
stage, target, simarity_name, mode, threshold, dump_hist, dump_dir) -> Tuple[bool, str]:
stage, target, similarity_name, mode, threshold, dump_hist, dump_dir) -> Tuple[bool, str]:
i = 0
judges = []
for expected, actual in zip(ref_ouputs, test_outputs):
expected = expected.astype(np.float32)
actual = actual.astype(np.float32)
dump_file = os.path.join(dump_dir, 'nncase_result_{0}_hist.csv'.format(i))
judge, simarity_info = compare_ndarray(
expected, actual, simarity_name, threshold, dump_hist, dump_file)
judge, similarity_info = compare_ndarray(
expected, actual, similarity_name, threshold, dump_hist, dump_file)
result_info = "\n{0} [ {1} {2} {3} ] Output: {4}!!\n".format(
'Pass' if judge else 'Fail', stage, target, mode, i)
result = simarity_info + result_info
result = similarity_info + result_info
with open(os.path.join(self.case_dir, 'test_result.txt'), 'a+') as f:
f.write(result)
i = i + 1
Expand Down

0 comments on commit fde7b77

Please sign in to comment.