diff --git a/tests/caffe_test_runner.py b/tests/caffe_test_runner.py index 817253849f..a0fdfb4300 100644 --- a/tests/caffe_test_runner.py +++ b/tests/caffe_test_runner.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import caffe from test_runner import * import os diff --git a/tests/compare_util.py b/tests/compare_util.py index 598c46488c..5e1eb56ba0 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -1,4 +1,18 @@ -import enum +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import math import os import re @@ -11,7 +25,38 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): - return (gt @ pred) / (np.linalg.norm(gt, 2) * np.linalg.norm(pred, 2)) + # remove the NaN values in the same location. + if np.isnan(gt).any() and np.isnan(pred).any(): + gt_mask = np.isnan(gt) + pred_mask = np.isnan(pred) + mask = gt_mask & pred_mask + gt = gt[~mask] + pred = pred[~mask] + + # remove the INF values in the same location. + if np.isinf(gt).any() and np.isinf(pred).any(): + gt_mask = np.isinf(gt) + pred_mask = np.isinf(pred) + mask = gt_mask & pred_mask + gt = gt[~mask] + pred = pred[~mask] + + # return -1 if the nan/inf value is still in the array. + if np.isnan(gt).any() or np.isnan(pred).any() or np.isinf(gt).any() or np.isinf(pred).any(): + return -1 + + # exclude the situation of all zeros in array. + if compare_arrays(gt, pred): + return 1 + + result = (gt @ pred) / (np.linalg.norm(gt, 2) * np.linalg.norm(pred, 2)) + + # When tensor gt is a multiple of tensor pred, their similarity is also 1. + return -1 if math.isnan(result) else result + + +def compare_arrays(gt: np.ndarray, pred: np.ndarray): + return np.array_equal(gt, pred) def euclidean(gt: np.ndarray, pred: np.ndarray, *args): @@ -86,7 +131,6 @@ def compare_ndarray(expected: np.ndarray, threshold: float = 0.99, dump_hist: bool = True, dump_file: str = 'hist.csv') -> bool: - if expected.size == actual.size: similarity = similarity_func[similarity_name](expected.flatten(), actual.flatten()) else: diff --git a/tests/evaluator.py b/tests/evaluator.py index 149f098ab4..3b36b8abe8 100644 --- a/tests/evaluator.py +++ b/tests/evaluator.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + from typing import List, Dict, Union, Tuple import os import nncase diff --git a/tests/generator.py b/tests/generator.py index bcccb4ad4d..20d7bcd0a2 100644 --- a/tests/generator.py +++ b/tests/generator.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + from typing import Any, Dict, List, Tuple, Union import numpy as np import os diff --git a/tests/importer/tflite_/basic/test_depthwise_conv2d.py b/tests/importer/tflite_/basic/test_depthwise_conv2d.py index 1b416c5236..7521b6a380 100644 --- a/tests/importer/tflite_/basic/test_depthwise_conv2d.py +++ b/tests/importer/tflite_/basic/test_depthwise_conv2d.py @@ -59,7 +59,7 @@ def __call__(self, x): strides = [ [1, 1], [1, 3], - [5, 5] + # [5, 5] ] paddings = [ @@ -69,7 +69,7 @@ def __call__(self, x): dilations = [ [1, 1], - # [2, 2] there is a bug in tf.nn.depthwise_conv2d that produces incorrect output shape + # [2, 2] there is a bug in tf.nn.depthwise_conv2d that produces incorrect output shape. ] diff --git a/tests/inference.py b/tests/inference.py index fdf42925e3..4585ba89a9 100644 --- a/tests/inference.py +++ b/tests/inference.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + from typing import List, Dict, Union, Tuple import os import nncase diff --git a/tests/json2md.py b/tests/json2md.py index a1c2ac01b9..af99050a2a 100644 --- a/tests/json2md.py +++ b/tests/json2md.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import argparse import json import os diff --git a/tests/kernels/test_cum_sum.cpp b/tests/kernels/test_cum_sum.cpp index e6a287e32c..18cf05837f 100644 --- a/tests/kernels/test_cum_sum.cpp +++ b/tests/kernels/test_cum_sum.cpp @@ -73,13 +73,13 @@ TEST_P(CumSumTest, cum_sum) { .expect("create tensor failed"); // actual - float_t exclusive[] = {0}; + float exclusive[] = {0}; auto exclusive_ptr = hrt::create(nncase::dt_float32, {1}, {reinterpret_cast(exclusive), sizeof(exclusive)}, true, host_runtime_tensor::pool_cpu_only) .expect("create tensor failed"); - float_t reverse[] = {0}; + float reverse[] = {0}; auto reverse_ptr = hrt::create(nncase::dt_float32, {1}, {reinterpret_cast(reverse), sizeof(reverse)}, diff --git a/tests/kernels/test_range.cpp b/tests/kernels/test_range.cpp index b9574a0c05..bc9ba8216e 100644 --- a/tests/kernels/test_range.cpp +++ b/tests/kernels/test_range.cpp @@ -40,21 +40,21 @@ class RangeTest : public KernelTest, auto step_value = GetFloatNumber("step"); auto typecode = GetDataType("lhs_type"); - float_t begin_array[] = {begin_value}; + float begin_array[] = {begin_value}; begin = hrt::create(typecode, shape, {reinterpret_cast(begin_array), sizeof(begin_array)}, true, host_runtime_tensor::pool_cpu_only) .expect("create tensor failed"); - float_t end_array[] = {end_value}; + float end_array[] = {end_value}; end = hrt::create( typecode, shape, {reinterpret_cast(end_array), sizeof(end_array)}, true, host_runtime_tensor::pool_cpu_only) .expect("create tensor failed"); - float_t step_array[] = {step_value}; + float step_array[] = {step_value}; step = hrt::create(typecode, shape, {reinterpret_cast(step_array), sizeof(step_array)}, diff --git a/tests/nuc_proxy.py b/tests/nuc_proxy.py index c65e06b8d2..bdc647ef68 100644 --- a/tests/nuc_proxy.py +++ b/tests/nuc_proxy.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import os import argparse import stat diff --git a/tests/onnx_test_runner.py b/tests/onnx_test_runner.py index acbd16ed9a..d1299bb234 100644 --- a/tests/onnx_test_runner.py +++ b/tests/onnx_test_runner.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + from onnx import version_converter, helper import onnxsim import onnxruntime as ort diff --git a/tests/preprocess_utils.py b/tests/preprocess_utils.py index 174e09132c..0ece47e4cd 100644 --- a/tests/preprocess_utils.py +++ b/tests/preprocess_utils.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + def get_source_transpose_index(perm): """ transpose model output with postprocess to framework output diff --git a/tests/test_runner.py b/tests/test_runner.py index 9c2ba46135..60948ac2f2 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import copy import os import re diff --git a/tests/test_utils.py b/tests/test_utils.py index c136800c48..0b0d5d20df 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import os import json import numpy as np diff --git a/tests/tflite_test_runner.py b/tests/tflite_test_runner.py index dffa694b72..59867442b1 100644 --- a/tests/tflite_test_runner.py +++ b/tests/tflite_test_runner.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import tensorflow as tf from test_runner import * import os