diff --git a/src/Native/src/kernels/stackvm/reference/ref_ops.h b/src/Native/src/kernels/stackvm/reference/ref_ops.h index 85746e256a..c6bc7b2d7d 100644 --- a/src/Native/src/kernels/stackvm/reference/ref_ops.h +++ b/src/Native/src/kernels/stackvm/reference/ref_ops.h @@ -455,7 +455,7 @@ transpose(datatype_t type, const gsl::byte *src, gsl::byte *dest, NNCASE_API result trilu(datatype_t type, const gsl::byte *input, gsl::byte *output, gsl::span in_shape, gsl::span in_strides, - gsl::span out_strides, long k, bool upper) noexcept; + gsl::span out_strides, int64_t k, bool upper) noexcept; NNCASE_API result unary(typecode_t dtype, runtime::stackvm::unary_op_t op, const gsl::byte *input, diff --git a/src/Native/src/kernels/stackvm/reference/trilu.cpp b/src/Native/src/kernels/stackvm/reference/trilu.cpp index ddbc15bb58..758a160772 100644 --- a/src/Native/src/kernels/stackvm/reference/trilu.cpp +++ b/src/Native/src/kernels/stackvm/reference/trilu.cpp @@ -27,15 +27,19 @@ template result trilu_impl(const T *input, T *output, gsl::span in_shape, gsl::span in_strides, - gsl::span out_strides, long k, bool upper) { + gsl::span out_strides, int64_t k, bool upper) { return apply(in_shape, [&](gsl::span index) -> result { - 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(); @@ -50,7 +54,7 @@ trilu_impl(const T *input, T *output, gsl::span in_shape, result nncase::kernels::stackvm::reference::trilu( datatype_t type, const gsl::byte *input, gsl::byte *output, gsl::span in_shape, gsl::span in_strides, - gsl::span out_strides, long k, bool upper) noexcept { + gsl::span out_strides, int64_t k, bool upper) noexcept { switch (runtime::get_bytes(type)) { TRILU_IMPL(1, uint8_t); TRILU_IMPL(2, uint16_t); diff --git a/tests/compare_util.py b/tests/compare_util.py index d5f5c5f2d9..6398bf65b4 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -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, @@ -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 ] @@ -62,7 +62,7 @@ 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: @@ -70,38 +70,38 @@ def compare_binfile(result_path: Tuple[str, str], 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 diff --git a/tests/config.toml b/tests/config.toml index 7d27973767..c4cb6ac0c2 100644 --- a/tests/config.toml +++ b/tests/config.toml @@ -82,7 +82,7 @@ args = [] [target.cpu] eval = true infer = true -simarity_name = 'cosine' +similarity_name = 'cosine' [target.cpu.mode.noptq] enabled = false @@ -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 @@ -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 diff --git a/tests/config.yml b/tests/config.yml deleted file mode 100644 index c43d0d2f30..0000000000 --- a/tests/config.yml +++ /dev/null @@ -1,124 +0,0 @@ -setup: # 整个runner期间的超参数配置 - root: tests_output - numworkers: 8 - log_txt: true -running: # 每个case运行时的处理配置 - preprocess: null - postprocess: null -case: # case的配置,应该是一个多层次的 - preprocess_opt: - - name: preprocess - values: - - false - - name: swapRB - values: - - false - - name: mean - values: - - [0,0,0] - - name: std - values: - - [1,1,1] - - name: input_range - values: - - [0,255] - - name: input_type - values: - - uint8 - - name: input_shape - values: - - [1,224,224,3] - - name: input_layout - values: - - NHWC - - name: output_layout - values: - - NHWC - - name: model_layout - values: - - NHWC - - name: letterbox_value - values: - - 0. - importer_opt: - kwargs: null - compile_opt: - tcu_num: 0 - is_fpga: false - dump_asm: true - dump_ir: false - dump_import_op_range: false - quant_type: 'uint8' - w_quant_type: 'uint8' - use_mse_quant_w: true - calibrate_method: 'Kld' #options: 'NoClip', 'Kld' - finetune_weights_method: 'NoFineTuneWeights' #options: 'NoFineTuneWeights', 'UseSquant', 'UseAdaRound' - use_mix_quant: false - quant_scheme: "" - export_quant_scheme: false - export_weight_range_by_channel: true - dump_quant_error: false - dump_quant_error_symmetric_for_signed: true - shape_bucket_enable: false - shape_bucket_range_info: {} - shape_bucket_segments_count: 2 - shape_bucket_fix_var_map: {} - input_file: "" - - ptq_opt: - kwargs: - input_mean: 0.5 - input_std: 0.5 - generate_inputs: - name: generate_random - kwargs: null - numbers: 1 - batch_size: 1 - generate_calibs: - name: generate_random - kwargs: null - numbers: 5 - batch_size: 1 - generate_dump_range_data: - name: generate_random - kwargs: null - numbers: 1 - batch_size: 1 - eval: - - name: target - values: - - cpu - #- vulkan -# - k210 -# - k510 -# - k230 - - name: ptq - values: - - true - infer: - - name: target - values: - - cpu - #- vulkan -# - k210 -# - k510 -# - k230 - - name: ptq - values: - - true -judge: - common: &judge_common - simarity_name: cosine - threshold: 0.999 - log_hist: true - matchs: null - specifics: - - matchs: - #target: [cpu, vulkan, k210, k510] - target: [cpu, k210, k510] - ptq: true - threshold: 0.98 - - matchs: - target: [k510] - ptq: false - threshold: 0.99 diff --git a/tests/importer/onnx_/basic/test_trilu.py b/tests/importer/onnx_/basic/test_trilu.py index 17ef9997b3..cb4f31a148 100644 --- a/tests/importer/onnx_/basic/test_trilu.py +++ b/tests/importer/onnx_/basic/test_trilu.py @@ -68,6 +68,7 @@ def _make_module(in_shape, kvalue, upper): ] ks = [ + 0, 1, 2, -1, @@ -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) @@ -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']) diff --git a/tests/importer/tflite_/model/test_densenet.py b/tests/importer/tflite_/model/test_densenet.py index 783b632c52..6d9f910767 100644 --- a/tests/importer/tflite_/model/test_densenet.py +++ b/tests/importer/tflite_/model/test_densenet.py @@ -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) diff --git a/tests/test_runner.py b/tests/test_runner.py index 335b22e2c9..0829dd213e 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -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) @@ -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'] @@ -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