mirror of https://github.com/kendryte/nncase.git
Refactor test compare module (#283)
* speed up test * add pep8 config and fix ci metric threshold * fix indent * fix autopep8 yaml * lower resnet50 threshold * reduce resnet50 thresh againpull/289/head
parent
61928eab17
commit
80fd5a9d96
|
@ -0,0 +1,36 @@
|
|||
name: Nncase Presubmit Checks
|
||||
on:
|
||||
# We don't want 'edited' (that's basically just the description, title, etc)
|
||||
# We don't want 'review_requested' (that's redundant to the ones below for our purposes)
|
||||
pull_request:
|
||||
types: [opened, synchronize, reopened]
|
||||
paths:
|
||||
- '**.h'
|
||||
- '**.c'
|
||||
- '**.cpp'
|
||||
|
||||
jobs:
|
||||
check_clang_format:
|
||||
name: Check clang-format
|
||||
runs-on: ubuntu-18.04
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: DoozyX/clang-format-lint-action@v0.11
|
||||
with:
|
||||
source: '.'
|
||||
exclude: './third_party'
|
||||
extensions: 'h,c,cc,cxx,cpp,hpp,cppm'
|
||||
clangFormatVersion: 11
|
||||
check_autopep8_format:
|
||||
name: Check autopep8-format
|
||||
runs-on: ubuntu-18.04
|
||||
steps:
|
||||
- name: autopep8
|
||||
id: autopep8
|
||||
uses: peter-evans/autopep8@v1
|
||||
with:
|
||||
args: --recursive --in-place tests python
|
||||
|
||||
- name: Fail if autopep8 made changes
|
||||
if: steps.autopep8.outputs.exit-code == 2
|
||||
run: exit 1
|
|
@ -13,7 +13,7 @@ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 40
|
|||
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 40
|
||||
```
|
||||
|
||||
- cmake >=3.16
|
||||
- cmake >=3.17
|
||||
- python >= 3.6
|
||||
- libgtk2.0
|
||||
|
||||
|
@ -21,10 +21,10 @@ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 40
|
|||
sudo apt install libgtk2.0-dev -y
|
||||
```
|
||||
|
||||
2. Install conan
|
||||
2. Install conan and cmake
|
||||
|
||||
```bash
|
||||
pip install conan
|
||||
pip install conan cmake
|
||||
```
|
||||
|
||||
3. Clone source
|
||||
|
@ -45,13 +45,16 @@ cmake --install . --prefix ../install
|
|||
|
||||
Install dependencies (MacOS)
|
||||
```bash
|
||||
pip install six==1.12 conan==1.19.2 tensorflow==2.4.1 matplotlib pillow pytest onnxruntime torch torchvision
|
||||
pip install tensorflow==2.5.0 matplotlib pillow onnx==1.9.0 onnx-simplifier==0.3.6 onnxoptimizer==0.2.6 onnxruntime==1.8.0
|
||||
pip install torch==1.9.0 torchvision==0.10.0 -f https://download.pytorch.org/whl/torch_stable.html
|
||||
pip install pytest
|
||||
```
|
||||
|
||||
Install dependencies
|
||||
```bash
|
||||
pip install conan tensorflow==2.4.1 matplotlib pillow onnxruntime
|
||||
pip install torch==1.4.0+cpu torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
|
||||
pip install tensorflow==2.5.0 matplotlib pillow onnx==1.9.0 onnx-simplifier==0.3.6 onnxoptimizer==0.2.6 onnxruntime==1.8.0
|
||||
pip install torch==1.9.0+cpu torchvision==0.10.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
|
||||
pip install pytest
|
||||
```
|
||||
|
||||
Export environment
|
||||
|
@ -71,12 +74,12 @@ pytest tests
|
|||
### Windows
|
||||
1. Install dependencies
|
||||
- Visual Studio 2019
|
||||
- cmake >=3.8
|
||||
- cmake >=3.17
|
||||
- python >= 3.6
|
||||
|
||||
2. Install conan
|
||||
2. Install conan cmake
|
||||
```cmd
|
||||
pip install conan==1.21.1
|
||||
pip install conan cmake
|
||||
```
|
||||
3. Clone source
|
||||
```cmd
|
||||
|
@ -95,7 +98,9 @@ msbuild nncase.sln
|
|||
|
||||
Install dependencies
|
||||
```cmd
|
||||
pip install six==1.12 conan==1.19.2 tensorflow==2.0.0 matplotlib pillow pytest
|
||||
pip install conan tensorflow==2.5.0 matplotlib pillow onnx==1.9.0 onnx-simplifier==0.3.6 onnxoptimizer==0.2.6 onnxruntime==1.8.0
|
||||
pip install torch==1.9.0+cpu torchvision==0.10.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
|
||||
pip install pytest
|
||||
```
|
||||
Run tests
|
||||
```cmd
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import os
|
||||
import sys
|
||||
|
||||
os.environ['KMP_DUPLICATE_LIB_OK']='True'
|
||||
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
|
||||
import _nncase
|
||||
|
|
|
@ -182,7 +182,7 @@ class BuildCMakeExt(build_ext):
|
|||
bin_dir = os.path.abspath(os.path.join(self.build_temp, 'install'))
|
||||
cmake_args = ['-G', 'Ninja']
|
||||
if os.getenv('CI', False):
|
||||
cmake_args += ['-DPython3_ROOT_DIR='+os.environ['pythonLocation']]
|
||||
cmake_args += ['-DPython3_ROOT_DIR=' + os.environ['pythonLocation']]
|
||||
|
||||
cfg = 'Debug' if self.debug else 'Release'
|
||||
build_args = ['--config', cfg]
|
||||
|
@ -200,13 +200,13 @@ class BuildCMakeExt(build_ext):
|
|||
# Change your cmake arguments below as necessary
|
||||
# Below is just an example set of arguments for building Blender as a Python module
|
||||
|
||||
self.spawn(['cmake', '-S' + ext.sourcedir, '-B'+self.build_temp] +
|
||||
self.spawn(['cmake', '-S' + ext.sourcedir, '-B' + self.build_temp] +
|
||||
cmake_args)
|
||||
|
||||
self.announce("Building binaries", level=3)
|
||||
|
||||
self.spawn(["cmake", "--build", self.build_temp]+build_args)
|
||||
self.spawn(["cmake", "--install", self.build_temp]+install_args)
|
||||
self.spawn(["cmake", "--build", self.build_temp] + build_args)
|
||||
self.spawn(["cmake", "--install", self.build_temp] + install_args)
|
||||
|
||||
# Build finished, now copy the files into the copy directory
|
||||
# The copy directory is the parent directory of the extension (.pyd)
|
||||
|
@ -235,7 +235,7 @@ class BuildCMakeExt(build_ext):
|
|||
|
||||
|
||||
setup(name='nncase',
|
||||
version='1.0.0'+os.getenv('NNCASE_VERSION_SUFFIX',''),
|
||||
version='1.0.0' + os.getenv('NNCASE_VERSION_SUFFIX', ''),
|
||||
packages=['nncase'],
|
||||
package_dir={'': '..'},
|
||||
ext_modules=[CMakeExtension(name="_nncase", sourcedir='../..')],
|
||||
|
|
|
@ -4,289 +4,49 @@ import os
|
|||
import re
|
||||
import struct
|
||||
import numpy as np
|
||||
|
||||
use_cosine_to_double_check = True
|
||||
from typing import List, Tuple
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def dot(v1, v2):
|
||||
return sum(x * y for x, y in zip(v1, v2))
|
||||
def cosine(gt: np.ndarray, pred: np.ndarray):
|
||||
return (gt @ pred) / (np.linalg.norm(gt, 2) * np.linalg.norm(pred, 2))
|
||||
|
||||
|
||||
def length(v):
|
||||
return math.sqrt(dot(v, v))
|
||||
def euclidean(gt: np.ndarray, pred: np.ndarray):
|
||||
return np.linalg.norm(gt - pred, 2)**2
|
||||
|
||||
|
||||
def cosine_similarity(v1, v2):
|
||||
return dot(v1, v2) / (length(v1) * length(v2))
|
||||
simarity_func = {
|
||||
'cosine': cosine,
|
||||
'euclidean': euclidean
|
||||
}
|
||||
|
||||
|
||||
DiffType = enum.Enum('DIFFTYPE', 'ABS REL')
|
||||
|
||||
|
||||
class DiffState(enum.IntEnum):
|
||||
BAD = 0
|
||||
GOOD = 1
|
||||
|
||||
|
||||
class VerboseType(enum.IntEnum):
|
||||
SILENT = 0
|
||||
PRINT_RESULT = 1
|
||||
PRINT_BAD = 2
|
||||
PRINT_EVERY = 3
|
||||
|
||||
|
||||
class SegmentTolerance:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
seg_min,
|
||||
seg_max,
|
||||
diff_thresh,
|
||||
diff_type: DiffType,
|
||||
verbose_type: VerboseType):
|
||||
|
||||
self.seg_min = seg_min
|
||||
self.seg_max = seg_max
|
||||
self.diff_thresh = diff_thresh
|
||||
self.diff_type = diff_type
|
||||
self.diff_state = DiffState.GOOD
|
||||
self.verbose_type = verbose_type
|
||||
|
||||
self.accumulated_diff = 0.0
|
||||
self.n_diff = 0
|
||||
|
||||
def add_diff(self, diff, print_prefix=""):
|
||||
self.accumulated_diff += diff
|
||||
self.n_diff += 1
|
||||
|
||||
current_state = DiffState.GOOD if diff <= self.diff_thresh else DiffState.BAD
|
||||
if current_state == DiffState.BAD:
|
||||
self.diff_state = current_state
|
||||
|
||||
if self.verbose_type == VerboseType.PRINT_EVERY or \
|
||||
self.verbose_type == VerboseType.PRINT_BAD and current_state == DiffState.BAD:
|
||||
print(print_prefix + "{}".format(diff))
|
||||
|
||||
def __str__(self):
|
||||
return "[{}, {}) thr={} type={} -> {} / {} = {}, {}".format(
|
||||
self.seg_min,
|
||||
self.seg_max,
|
||||
self.diff_thresh,
|
||||
self.diff_type,
|
||||
self.accumulated_diff,
|
||||
self.n_diff,
|
||||
self.accumulated_diff / self.n_diff if self.n_diff != 0 else 0,
|
||||
self.diff_state)
|
||||
|
||||
|
||||
class Judge:
|
||||
|
||||
def __init__(self, tolerances: [SegmentTolerance]):
|
||||
tolerances.sort(key=lambda x: x.seg_min)
|
||||
self.tolerances = tolerances
|
||||
self.n_outlier = 0
|
||||
self.cosine_similarity = 0
|
||||
|
||||
def judge(self, gt, pred, print_prefix=""):
|
||||
for tol in self.tolerances:
|
||||
if tol.seg_min <= abs(gt) < tol.seg_max:
|
||||
diff = abs(gt - pred)
|
||||
if tol.diff_type == DiffType.REL:
|
||||
diff /= gt
|
||||
|
||||
tol.add_diff(diff, print_prefix)
|
||||
break
|
||||
def compare(result_path: Tuple[str, str],
|
||||
ground_truth_path: Tuple[str, str],
|
||||
simarity_name: str = 'cosine',
|
||||
threshold: float = 0.99,
|
||||
hist: bool = True) -> bool:
|
||||
# NOTE the result_path is Tuple[ bin_path, txt_path ]
|
||||
ground_truth_path_bin, ground_truth_path_txt = result_path
|
||||
result_path_bin, result_path_txt = ground_truth_path
|
||||
if 'npy' in ground_truth_path_bin: # bfloat16
|
||||
# gt, pred = bytes.fromhex(gt.strip()), bytes.fromhex(pred.strip())
|
||||
# gt, pred = struct.unpack('>H', gt)[0], struct.unpack('>H', pred)[0]
|
||||
raise NotImplemented("need support bfloat16 judge!")
|
||||
else: # float
|
||||
gt_arr = np.fromfile(ground_truth_path_bin, np.float32)
|
||||
pred_arr = np.fromfile(result_path_bin, np.float32)
|
||||
if gt_arr.size == pred_arr.size:
|
||||
simarity = simarity_func[simarity_name](gt_arr, pred_arr)
|
||||
else:
|
||||
# raise ValueError
|
||||
self.n_outlier += 1
|
||||
|
||||
def is_good(self):
|
||||
if self.cosine_similarity > 0.98:
|
||||
return True
|
||||
|
||||
for tol in self.tolerances:
|
||||
if tol.diff_state == DiffState.BAD:
|
||||
return False
|
||||
return True
|
||||
|
||||
def __str__(self):
|
||||
s = ' ' + '\n '.join([str(tol) for tol in self.tolerances]) + ' '
|
||||
if self.n_outlier:
|
||||
s += '\n n_outlier: %d' % self.n_outlier + ' '
|
||||
return s
|
||||
|
||||
|
||||
class Index(object):
|
||||
|
||||
def __init__(self, shape: [int]):
|
||||
self.product_shape = self._get_product_shape(shape)
|
||||
|
||||
def _get_product_shape(self, shape: [int]):
|
||||
cumprod = np.cumprod(shape[-1:0:-1]).tolist()
|
||||
cumprod = cumprod[::-1]
|
||||
cumprod.append(1)
|
||||
return cumprod
|
||||
|
||||
def flatten_index_shape_index(self, flatten_index: int):
|
||||
shape_index = []
|
||||
for s in self.product_shape:
|
||||
index = flatten_index // s
|
||||
shape_index.append(index)
|
||||
flatten_index %= s
|
||||
|
||||
return shape_index
|
||||
|
||||
|
||||
def compare(
|
||||
ground_truth_path,
|
||||
result_path,
|
||||
verbose=VerboseType.PRINT_EVERY,
|
||||
judge=None,
|
||||
cfg=None):
|
||||
|
||||
first_threshold = 0.6 if hasattr(
|
||||
cfg, 'op') and cfg.op == 'tf.reduce_prod' else 0.5
|
||||
first_threshold = 1.0 if hasattr(
|
||||
cfg, 'op') and cfg.op == 'random_connections' else first_threshold
|
||||
|
||||
if judge is None:
|
||||
judge = Judge([
|
||||
SegmentTolerance(0, 64, first_threshold, DiffType.ABS, verbose),
|
||||
SegmentTolerance(64, 128, 2, DiffType.ABS, verbose),
|
||||
SegmentTolerance(128, 10 ** 18, 8 / 128, DiffType.REL, verbose),
|
||||
SegmentTolerance(10 ** 18, float('inf'), 44 / 128, DiffType.REL, verbose)])
|
||||
|
||||
gt_num_lines = sum(1 for line in open(ground_truth_path)) - 1
|
||||
res_num_lines = sum(1 for line in open(result_path)) - 1
|
||||
|
||||
with open(ground_truth_path, 'r') as fgt, open(result_path, 'r') as fpred:
|
||||
shape = fgt.readline()
|
||||
fpred.readline()
|
||||
shape = re.findall(r'(\d+)', shape)
|
||||
shape = [int(dim) for dim in shape]
|
||||
|
||||
index = Index(shape)
|
||||
|
||||
if gt_num_lines == res_num_lines:
|
||||
for i, (gt, pred) in enumerate(zip(fgt, fpred)):
|
||||
if i % 1000 == 0:
|
||||
#print("Compare %d..." % i, end='\r')
|
||||
pass
|
||||
try:
|
||||
gt, pred = float(gt), float(pred)
|
||||
except ValueError:
|
||||
gt, pred = bytes.fromhex(
|
||||
gt.strip()), bytes.fromhex(pred.strip())
|
||||
gt, pred = struct.unpack(
|
||||
'>H', gt)[0], struct.unpack('>H', pred)[0]
|
||||
|
||||
shape_index = index.flatten_index_shape_index(i)
|
||||
|
||||
print_prefix = '[' + ','.join(map(str, shape_index)) + \
|
||||
'] {} -> *{} != {} by '.format(i, gt, pred)
|
||||
judge.judge(gt, pred, print_prefix)
|
||||
|
||||
elif gt_num_lines == 9 * res_num_lines:
|
||||
gt_elements = fgt.read().split('\n')[:-1]
|
||||
res_elements = fpred.read().split('\n')[:-1]
|
||||
|
||||
for i, pred in enumerate(res_elements):
|
||||
if i % 1000 == 0:
|
||||
#print("Compare %d..." % i, end='\r')
|
||||
pass
|
||||
nearest_answer = 0
|
||||
nearest_diff = 10000
|
||||
for gt_index in range(9):
|
||||
gt, pred = float(
|
||||
gt_elements[gt_index * res_num_lines + i]), float(pred)
|
||||
|
||||
shape_index = index.flatten_index_shape_index(i)
|
||||
|
||||
print_prefix = '[' + ','.join(map(str, shape_index)) + \
|
||||
'] {} -> *{} != {} by '.format(i, gt, pred)
|
||||
|
||||
if abs(gt - pred) < nearest_diff:
|
||||
nearest_answer = gt
|
||||
nearest_diff = abs(gt - pred)
|
||||
|
||||
gt = nearest_answer
|
||||
judge.judge(gt, pred, print_prefix)
|
||||
|
||||
else:
|
||||
print('# of elements in gt and result not match\n')
|
||||
raise ValueError
|
||||
|
||||
#print("Pass!!" if judge.is_good() else "Fail..")
|
||||
print(judge)
|
||||
if judge.is_good() == False:
|
||||
with open(ground_truth_path, 'r') as fgt, open(result_path, 'r') as fpred:
|
||||
gt = fgt.readlines()[1:]
|
||||
pred = fpred.readlines()[1:]
|
||||
judge.cosine_similarity = cosine_similarity(
|
||||
[float(i) for i in gt], [float(i) for i in pred])
|
||||
print("cosine_similarity is: {}".format(judge.cosine_similarity))
|
||||
return judge
|
||||
|
||||
|
||||
def compare_with_ground_truth(result_path, ground_truth_path, state, verbose):
|
||||
if not isinstance(ground_truth_path, list):
|
||||
if os.path.exists(result_path) and state == 0:
|
||||
ok = compare(ground_truth_path, result_path, verbose)
|
||||
else:
|
||||
|
||||
class FailObject:
|
||||
|
||||
def __init__(self, result_path, state):
|
||||
self.result_path = result_path
|
||||
self.state = state
|
||||
|
||||
def is_good(self):
|
||||
return False
|
||||
|
||||
def __str__(self):
|
||||
return '%s, return state: %d' % (self.result_path, self.state)
|
||||
|
||||
ok = FailObject(result_path, state)
|
||||
else:
|
||||
for i in range(len(result_path)):
|
||||
if os.path.exists(result_path[0]) and state == 0:
|
||||
|
||||
ok = compare(ground_truth_path[i], result_path[i], verbose)
|
||||
|
||||
else:
|
||||
|
||||
class FailObject:
|
||||
|
||||
def __init__(self, result_path, state):
|
||||
self.result_path = result_path
|
||||
self.state = state
|
||||
|
||||
def is_good(self):
|
||||
return False
|
||||
|
||||
def __str__(self):
|
||||
return '%s, return state: %d' % (self.result_path, self.state)
|
||||
|
||||
ok = FailObject(result_path[i], state)
|
||||
|
||||
return ok
|
||||
|
||||
|
||||
def compare_results(case_dir, out_len, targets, enable_ptq, is_evaluation):
|
||||
for i in range(out_len):
|
||||
gt_file = os.path.join(case_dir, 'cpu_result{0}.txt'.format(i))
|
||||
for target in targets:
|
||||
nncase_file = os.path.join(case_dir, target)
|
||||
nncase_file = os.path.join(
|
||||
nncase_file, 'eval' if is_evaluation else 'infer')
|
||||
nncase_file = os.path.join(
|
||||
nncase_file, 'ptq' if enable_ptq else 'no_ptq', 'nncase_result{0}.txt'.format(i))
|
||||
|
||||
judge = compare_with_ground_truth(
|
||||
nncase_file, gt_file, state=0, verbose=VerboseType.PRINT_RESULT)
|
||||
if judge.is_good():
|
||||
print("Pass {0}.{1}!!\n".format(target, i))
|
||||
else:
|
||||
print("Fail {0}.{1}..\n".format(target, i))
|
||||
return False
|
||||
return True
|
||||
raise ValueError("The number of elements in gt and result not match\n")
|
||||
if hist:
|
||||
y, x = np.histogram(gt_arr - pred_arr, 100)
|
||||
p = Path(result_path_bin)
|
||||
np.savetxt(p.parent / (p.stem + '_hist.csv'),
|
||||
np.stack((x[:-1], y)).T, fmt='%f', delimiter=',')
|
||||
simarity_info = f"\n{simarity_name} similarity = {simarity:.4f}, threshold = {threshold:.4f}\n"
|
||||
if simarity < threshold:
|
||||
return False, simarity_info
|
||||
return True, simarity_info
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
setup: # 整个runner期间的超参数配置
|
||||
root: tests_output
|
||||
numworkers: 8
|
||||
log_txt: true
|
||||
running: # 每个case运行时的处理配置
|
||||
preprocess: null
|
||||
postprocess: null
|
||||
|
@ -43,3 +44,7 @@ case: # case的配置,应该是一个多层次的
|
|||
values:
|
||||
- false
|
||||
- true
|
||||
judge:
|
||||
simarity_name: cosine
|
||||
threshold: 0.98
|
||||
log_hist: true
|
|
@ -30,7 +30,7 @@ def _make_module():
|
|||
|
||||
@tf.function(input_signature=[tf.TensorSpec([1, 4, 8, 3], tf.float32)])
|
||||
def __call__(self, x):
|
||||
out = tf.reshape(x, [1,4,4,6])
|
||||
out = tf.reshape(x, [1, 4, 4, 6])
|
||||
out1 = tf.nn.conv2d(out, self.w1, [1, 1], 'SAME')
|
||||
out2 = tf.nn.conv2d(out, self.w2, [1, 1], 'SAME')
|
||||
c1 = tf.concat([out1, out2], axis=3)
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(num, esp, momentum):
|
||||
|
||||
class BatchNormModule(torch.nn.Module):
|
||||
|
@ -30,6 +31,7 @@ def _make_module(num, esp, momentum):
|
|||
|
||||
return BatchNormModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1, 2, 16, 16],
|
||||
[1, 8, 224, 224]
|
||||
|
@ -45,6 +47,7 @@ momentums = [
|
|||
0.9
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('eps', epses)
|
||||
@pytest.mark.parametrize('momentum', momentums)
|
||||
|
@ -55,5 +58,6 @@ def test_batchnorm(in_shape, eps, momentum, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_batchnorm.py'])
|
||||
pytest.main(['-vv', 'test_batchnorm.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(min, max):
|
||||
|
||||
class ClipModule(torch.nn.Module):
|
||||
|
@ -29,6 +30,7 @@ def _make_module(min, max):
|
|||
|
||||
return ClipModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1],
|
||||
[8, 8],
|
||||
|
@ -47,6 +49,7 @@ maxs = [
|
|||
6
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('min', mins)
|
||||
@pytest.mark.parametrize('max', maxs)
|
||||
|
@ -58,5 +61,6 @@ def test_clip(in_shape, min, max, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_clip.py'])
|
||||
pytest.main(['-vv', 'test_clip.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, dim):
|
||||
|
||||
class ConcatModule(torch.nn.Module):
|
||||
|
@ -29,6 +30,7 @@ def _make_module(in_shape, dim):
|
|||
|
||||
return ConcatModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1],
|
||||
[3, 4],
|
||||
|
@ -43,6 +45,7 @@ axes = [
|
|||
3
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('axis', axes)
|
||||
def test_concat(in_shape, axis, request):
|
||||
|
@ -53,5 +56,6 @@ def test_concat(in_shape, axis, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_concat.py'])
|
||||
pytest.main(['-vv', 'test_concat.py'])
|
||||
|
|
|
@ -17,12 +17,14 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(i_channel, k_size, o_channel, stride, padding, padding_mode, dilation):
|
||||
|
||||
class Conv2dModule(torch.nn.Module):
|
||||
def __init__(self):
|
||||
super(Conv2dModule, self).__init__()
|
||||
self.conv = torch.nn.Conv2d(i_channel, o_channel, k_size, stride=stride, padding=padding, padding_mode=padding_mode, dilation=dilation)
|
||||
self.conv = torch.nn.Conv2d(i_channel, o_channel, k_size, stride=stride,
|
||||
padding=padding, padding_mode=padding_mode, dilation=dilation)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.conv(x)
|
||||
|
@ -30,6 +32,7 @@ def _make_module(i_channel, k_size, o_channel, stride, padding, padding_mode, di
|
|||
|
||||
return Conv2dModule()
|
||||
|
||||
|
||||
n = [
|
||||
1,
|
||||
3
|
||||
|
@ -41,8 +44,8 @@ i_channels = [
|
|||
]
|
||||
|
||||
i_sizes = [
|
||||
[224, 224],
|
||||
[112, 65]
|
||||
[12, 24],
|
||||
[38, 65]
|
||||
]
|
||||
|
||||
k_sizes = [
|
||||
|
@ -52,7 +55,7 @@ k_sizes = [
|
|||
|
||||
o_channels = [
|
||||
1,
|
||||
16
|
||||
8
|
||||
]
|
||||
|
||||
strides = [
|
||||
|
@ -74,6 +77,7 @@ dilations = [
|
|||
[2, 2]
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('n', n)
|
||||
@pytest.mark.parametrize('i_channel', i_channels)
|
||||
@pytest.mark.parametrize('i_size', i_sizes)
|
||||
|
@ -93,5 +97,6 @@ def test_conv2d(n, i_channel, i_size, k_size, o_channel, stride, padding, paddin
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_conv2d.py'])
|
||||
pytest.main(['-vv', 'test_conv2d.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ from onnx import helper
|
|||
from onnx import AttributeProto, TensorProto, GraphProto
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, ratio):
|
||||
x = helper.make_tensor_value_info('x', TensorProto.FLOAT, in_shape)
|
||||
y = helper.make_tensor_value_info('y', TensorProto.FLOAT, in_shape)
|
||||
|
@ -50,6 +51,7 @@ def _make_module(in_shape, ratio):
|
|||
|
||||
return model_def
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1, 3, 60, 72],
|
||||
[1, 3, 224, 224]
|
||||
|
@ -61,6 +63,7 @@ ratios = [
|
|||
0.5
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('ratio', ratios)
|
||||
def test_dropout(in_shape, ratio, request):
|
||||
|
@ -70,5 +73,6 @@ def test_dropout(in_shape, ratio, request):
|
|||
model_file = runner.from_onnx_helper(model_def)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_lrn.py'])
|
||||
pytest.main(['-vv', 'test_lrn.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, axis):
|
||||
|
||||
class FlattenModule(torch.nn.Module):
|
||||
|
@ -32,6 +33,7 @@ def _make_module(in_shape, axis):
|
|||
|
||||
return FlattenModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1, 3, 224, 224]
|
||||
]
|
||||
|
@ -42,6 +44,7 @@ axes = [
|
|||
3
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('axis', axes)
|
||||
def test_flatten(in_shape, axis, request):
|
||||
|
@ -52,5 +55,6 @@ def test_flatten(in_shape, axis, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_flatten.py'])
|
||||
pytest.main(['-vv', 'test_flatten.py'])
|
||||
|
|
|
@ -21,16 +21,20 @@ from onnx import AttributeProto, TensorProto, GraphProto
|
|||
from onnx_test_runner import OnnxTestRunner
|
||||
import numpy as np
|
||||
|
||||
|
||||
def result_shape(p_shape, i_shape, axis=0):
|
||||
if axis < 0:
|
||||
axis = len(p_shape) + axis
|
||||
return p_shape[:axis] + i_shape + p_shape[axis + 1:]
|
||||
|
||||
|
||||
def _make_module(in_shape, indices, axis):
|
||||
input = helper.make_tensor_value_info('input', TensorProto.FLOAT, in_shape)
|
||||
i_shape = list(np.array(indices).shape)
|
||||
indices = helper.make_tensor('indices', TensorProto.INT64, np.array(indices).shape, np.array(indices).flatten().tolist())
|
||||
output = helper.make_tensor_value_info('output', TensorProto.FLOAT, result_shape(in_shape, i_shape, axis))
|
||||
indices = helper.make_tensor('indices', TensorProto.INT64, np.array(
|
||||
indices).shape, np.array(indices).flatten().tolist())
|
||||
output = helper.make_tensor_value_info(
|
||||
'output', TensorProto.FLOAT, result_shape(in_shape, i_shape, axis))
|
||||
initializers = []
|
||||
initializers.append(indices)
|
||||
|
||||
|
@ -50,6 +54,7 @@ def _make_module(in_shape, indices, axis):
|
|||
|
||||
return helper.make_model(graph_def, producer_name='kendryte')
|
||||
|
||||
|
||||
in_shapes_indices_dim = [
|
||||
([11], [1, 3, 10, 0, 2], 0),
|
||||
([11], [[2, 4], [1, 3]], 0),
|
||||
|
@ -64,6 +69,7 @@ in_shapes_indices_dim = [
|
|||
([2, 3, 5, 7], [[1, 1], [1, 2]], -1)
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape,indices,dim', in_shapes_indices_dim)
|
||||
def test_gather(in_shape, indices, dim, request):
|
||||
model_def = _make_module(in_shape, indices, dim)
|
||||
|
|
|
@ -21,16 +21,20 @@ from onnx import AttributeProto, TensorProto, GraphProto
|
|||
from onnx_test_runner import OnnxTestRunner
|
||||
import numpy as np
|
||||
|
||||
|
||||
def result_shape(p_shape, i_shape, batch_dims=0):
|
||||
if batch_dims < 0:
|
||||
batch_dims = len(p_shape) + batch_dims
|
||||
return i_shape[:-1] + p_shape[i_shape[-1] + batch_dims:]
|
||||
|
||||
|
||||
def _make_module(in_shape, indices, batch_dims):
|
||||
input = helper.make_tensor_value_info('input', TensorProto.FLOAT, in_shape)
|
||||
i_shape = list(np.array(indices).shape)
|
||||
indices = helper.make_tensor('indices', TensorProto.INT64, np.array(indices).shape, np.array(indices).flatten().tolist())
|
||||
output = helper.make_tensor_value_info('output', TensorProto.FLOAT, result_shape(in_shape, i_shape, batch_dims))
|
||||
indices = helper.make_tensor('indices', TensorProto.INT64, np.array(
|
||||
indices).shape, np.array(indices).flatten().tolist())
|
||||
output = helper.make_tensor_value_info(
|
||||
'output', TensorProto.FLOAT, result_shape(in_shape, i_shape, batch_dims))
|
||||
initializers = []
|
||||
initializers.append(indices)
|
||||
|
||||
|
@ -51,6 +55,7 @@ def _make_module(in_shape, indices, batch_dims):
|
|||
|
||||
return helper.make_model(graph_def, producer_name='kendryte')
|
||||
|
||||
|
||||
in_shapes_indices_dim = [
|
||||
([11], [[0], [7], [5]], 0),
|
||||
([3, 5], [[[0, 2], [0, 4]]], 0),
|
||||
|
@ -71,6 +76,7 @@ in_shapes_indices_dim = [
|
|||
[[[3], [1], [4]], [[1], [0], [2]], [[3], [2], [4]]]], 3)
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape,indices,dim', in_shapes_indices_dim)
|
||||
def test_gather_nd(in_shape, indices, dim, request):
|
||||
model_def = _make_module(in_shape, indices, dim)
|
||||
|
|
|
@ -20,14 +20,15 @@ from onnx import AttributeProto, TensorProto, GraphProto
|
|||
import numpy as np
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module():
|
||||
input_A = helper.make_tensor_value_info('A', TensorProto.FLOAT, [112, 224])
|
||||
input_B = helper.make_tensor("B", TensorProto.FLOAT,
|
||||
dims=(56, 224),
|
||||
vals=np.random.randn(56, 224).astype(np.float32).flatten().tolist())
|
||||
dims=(56, 224),
|
||||
vals=np.random.randn(56, 224).astype(np.float32).flatten().tolist())
|
||||
input_C = helper.make_tensor("C", TensorProto.FLOAT,
|
||||
dims=(56,),
|
||||
vals=np.random.randn(56,).astype(np.float32).flatten().tolist())
|
||||
dims=(56,),
|
||||
vals=np.random.randn(56,).astype(np.float32).flatten().tolist())
|
||||
initializers = []
|
||||
initializers.append(input_B)
|
||||
initializers.append(input_C)
|
||||
|
@ -56,6 +57,7 @@ def _make_module():
|
|||
|
||||
return model_def
|
||||
|
||||
|
||||
def test_gemm(request):
|
||||
model_def = _make_module()
|
||||
|
||||
|
@ -63,5 +65,6 @@ def test_gemm(request):
|
|||
model_file = runner.from_onnx_helper(model_def)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_gemm.py'])
|
||||
pytest.main(['-vv', 'test_gemm.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ from onnx import helper
|
|||
from onnx import AttributeProto, TensorProto, GraphProto
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape):
|
||||
x = helper.make_tensor_value_info('x', TensorProto.FLOAT, in_shape)
|
||||
y = helper.make_tensor_value_info('y', TensorProto.FLOAT, in_shape)
|
||||
|
@ -47,10 +48,12 @@ def _make_module(in_shape):
|
|||
|
||||
return model_def
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1, 3, 224, 224]
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
def test_identity(in_shape, request):
|
||||
model_def = _make_module(in_shape)
|
||||
|
@ -59,5 +62,6 @@ def test_identity(in_shape, request):
|
|||
model_file = runner.from_onnx_helper(model_def)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_identity.py'])
|
||||
pytest.main(['-vv', 'test_identity.py'])
|
||||
|
|
|
@ -20,6 +20,7 @@ from onnx import AttributeProto, TensorProto, GraphProto
|
|||
from onnx_test_runner import OnnxTestRunner
|
||||
import numpy as np
|
||||
|
||||
|
||||
def _make_module(in_shape, epsilon):
|
||||
|
||||
input = helper.make_tensor_value_info('input', TensorProto.FLOAT, in_shape)
|
||||
|
@ -53,6 +54,7 @@ def _make_module(in_shape, epsilon):
|
|||
|
||||
return model_def
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1, 3, 56, 56]
|
||||
]
|
||||
|
@ -62,6 +64,7 @@ epsilons = [
|
|||
1e-2
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('epsilon', epsilons)
|
||||
def test_instancenorm(in_shape, epsilon, request):
|
||||
|
@ -71,5 +74,6 @@ def test_instancenorm(in_shape, epsilon, request):
|
|||
model_file = runner.from_onnx_helper(model_def)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_instancenorm.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(negative_slope):
|
||||
|
||||
class LeakyReluModule(torch.nn.Module):
|
||||
|
@ -30,6 +31,7 @@ def _make_module(negative_slope):
|
|||
|
||||
return LeakyReluModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1],
|
||||
[1, 3, 224, 224]
|
||||
|
@ -42,6 +44,7 @@ negative_slopes = [
|
|||
0.8
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('negative_slope', negative_slopes)
|
||||
def test_leakyrelu(in_shape, negative_slope, request):
|
||||
|
@ -51,5 +54,6 @@ def test_leakyrelu(in_shape, negative_slope, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_leakyrelu.py'])
|
||||
pytest.main(['-vv', 'test_leakyrelu.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ from onnx import helper
|
|||
from onnx import AttributeProto, TensorProto, GraphProto
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, alpha, beta, bias, size):
|
||||
input = helper.make_tensor_value_info('input', TensorProto.FLOAT, in_shape)
|
||||
output = helper.make_tensor_value_info('output', TensorProto.FLOAT, in_shape)
|
||||
|
@ -44,6 +45,7 @@ def _make_module(in_shape, alpha, beta, bias, size):
|
|||
|
||||
return model_def
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1, 3, 60, 72],
|
||||
[1, 3, 224, 224]
|
||||
|
@ -69,6 +71,7 @@ sizes = [
|
|||
5
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('alpha', alphas)
|
||||
@pytest.mark.parametrize('beta', betas)
|
||||
|
@ -81,5 +84,6 @@ def test_lrn(in_shape, alpha, beta, bias, size, request):
|
|||
model_file = runner.from_onnx_helper(model_def)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_lrn.py'])
|
||||
pytest.main(['-vv', 'test_lrn.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, padding, value):
|
||||
|
||||
class PadModule(torch.nn.Module):
|
||||
|
@ -33,6 +34,7 @@ def _make_module(in_shape, padding, value):
|
|||
|
||||
return PadModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1, 3, 60, 72],
|
||||
[1, 3, 224, 224]
|
||||
|
@ -49,6 +51,7 @@ values = [
|
|||
0
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('padding', paddings)
|
||||
@pytest.mark.parametrize('value', values)
|
||||
|
@ -59,5 +62,6 @@ def test_pad(in_shape, padding, value, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_pad.py'])
|
||||
pytest.main(['-vv', 'test_pad.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(kernel_size, stride, padding):
|
||||
|
||||
class PoolModule(torch.nn.Module):
|
||||
|
@ -33,12 +34,13 @@ def _make_module(kernel_size, stride, padding):
|
|||
outs.append(self.avgpool2d(x))
|
||||
outs.append(self.global_avgpool(x))
|
||||
outs.append(self.maxpool2d(x))
|
||||
outs.append(self.global_maxpool(x)) # maxpool2d in fact
|
||||
outs.append(self.global_maxpool(x)) # maxpool2d in fact
|
||||
|
||||
return outs
|
||||
|
||||
return PoolModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1, 3, 60, 72],
|
||||
[1, 3, 224, 224]
|
||||
|
@ -62,6 +64,7 @@ paddings = [
|
|||
(2, 3)
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('kernel_size', kernel_sizes)
|
||||
@pytest.mark.parametrize('stride', strides)
|
||||
|
@ -74,5 +77,6 @@ def test_pool(in_shape, kernel_size, stride, padding, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_pool.py'])
|
||||
pytest.main(['-vv', 'test_pool.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(num, init):
|
||||
|
||||
class PReluModule(torch.nn.Module):
|
||||
|
@ -30,6 +31,7 @@ def _make_module(num, init):
|
|||
|
||||
return PReluModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1],
|
||||
[1, 3, 224, 224]
|
||||
|
@ -40,6 +42,7 @@ inits = [
|
|||
0.25,
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('init', inits)
|
||||
def test_prelu(in_shape, init, request):
|
||||
|
@ -50,5 +53,6 @@ def test_prelu(in_shape, init, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_prelu.py'])
|
||||
pytest.main(['-vv', 'test_prelu.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(dim, keepdim):
|
||||
|
||||
class ReduceModule(torch.nn.Module):
|
||||
|
@ -36,6 +37,7 @@ def _make_module(dim, keepdim):
|
|||
|
||||
return ReduceModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1],
|
||||
[3, 4],
|
||||
|
@ -55,16 +57,18 @@ keepdims = [
|
|||
True
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('dim', dims)
|
||||
@pytest.mark.parametrize('keepdim', keepdims)
|
||||
def test_reduce(in_shape, dim, keepdim, request):
|
||||
if len(in_shape) > dim :
|
||||
if len(in_shape) > dim:
|
||||
module = _make_module(dim, keepdim)
|
||||
|
||||
runner = OnnxTestRunner(request.node.name)
|
||||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_reduce.py'])
|
||||
pytest.main(['-vv', 'test_reduce.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module():
|
||||
|
||||
class ReluModule(torch.nn.Module):
|
||||
|
@ -30,6 +31,7 @@ def _make_module():
|
|||
|
||||
return ReluModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1],
|
||||
[8, 8],
|
||||
|
@ -37,6 +39,7 @@ in_shapes = [
|
|||
[1, 3, 224, 224]
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
def test_relu(in_shape, request):
|
||||
module = _make_module()
|
||||
|
@ -45,5 +48,6 @@ def test_relu(in_shape, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_relu.py'])
|
||||
pytest.main(['-vv', 'test_relu.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, out_channel, kernel_size):
|
||||
|
||||
class ReshapeModule(torch.nn.Module):
|
||||
|
@ -41,6 +42,7 @@ def _make_module(in_shape, out_channel, kernel_size):
|
|||
|
||||
return ReshapeModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1, 4, 60, 72],
|
||||
[1, 3, 224, 224]
|
||||
|
@ -58,6 +60,7 @@ kernel_sizes = [
|
|||
5
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('out_channel', out_channels)
|
||||
@pytest.mark.parametrize('kernel_size', kernel_sizes)
|
||||
|
@ -68,5 +71,6 @@ def test_reshape(in_shape, out_channel, kernel_size, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_reshape.py'])
|
||||
pytest.main(['-vv', 'test_reshape.py'])
|
||||
|
|
|
@ -20,6 +20,7 @@ import sys
|
|||
import torchvision.transforms.functional as F
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(size, mode):
|
||||
|
||||
class ResizeModule(torch.nn.Module):
|
||||
|
@ -28,11 +29,12 @@ def _make_module(size, mode):
|
|||
|
||||
def forward(self, x):
|
||||
# x = torch.nn.functional.interpolate(x, size=size, scale_factor=scale_factor, mode=mode, align_corners=None, recompute_scale_factor=None)
|
||||
x = F.resize(x, size = size, interpolation = mode)
|
||||
x = F.resize(x, size=size, interpolation=mode)
|
||||
return x
|
||||
|
||||
return ResizeModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
# [1, 3, 224, 224]
|
||||
[2, 3, 32, 32]
|
||||
|
@ -47,10 +49,11 @@ sizes = [
|
|||
]
|
||||
|
||||
modes = [
|
||||
0, # PIL.Image.NEAREST
|
||||
0, # PIL.Image.NEAREST
|
||||
2, # PIL.Image.BILINEAR
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('size', sizes)
|
||||
@pytest.mark.parametrize('mode', modes)
|
||||
|
@ -61,5 +64,6 @@ def test_resize(in_shape, size, mode, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_resize.py'])
|
||||
pytest.main(['-vv', 'test_resize.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module():
|
||||
|
||||
class SigmoidModule(torch.nn.Module):
|
||||
|
@ -29,11 +30,13 @@ def _make_module():
|
|||
|
||||
return SigmoidModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1],
|
||||
[1, 3, 224, 224]
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
def test_sigmoid(in_shape, request):
|
||||
module = _make_module()
|
||||
|
@ -42,5 +45,6 @@ def test_sigmoid(in_shape, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_sigmoid.py'])
|
||||
pytest.main(['-vv', 'test_sigmoid.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, out_channel, kernel_size, dim, start, length):
|
||||
|
||||
class SqueezeModule(torch.nn.Module):
|
||||
|
@ -31,6 +32,7 @@ def _make_module(in_shape, out_channel, kernel_size, dim, start, length):
|
|||
|
||||
return SqueezeModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1, 4, 60, 72],
|
||||
[1, 3, 224, 224]
|
||||
|
@ -52,6 +54,7 @@ axes = [
|
|||
3
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('out_channel', out_channels)
|
||||
@pytest.mark.parametrize('kernel_size', kernel_sizes)
|
||||
|
@ -64,5 +67,6 @@ def test_slice(in_shape, out_channel, kernel_size, axis, request):
|
|||
# model_file = runner.from_torch(module, in_shape)
|
||||
# runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_slice.py'])
|
||||
pytest.main(['-vv', 'test_slice.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module():
|
||||
|
||||
class SoftmaxModule(torch.nn.Module):
|
||||
|
@ -30,6 +31,7 @@ def _make_module():
|
|||
|
||||
return SoftmaxModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1],
|
||||
[1, 1001],
|
||||
|
@ -37,6 +39,7 @@ in_shapes = [
|
|||
[1, 3, 224, 224]
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
def test_softmax(in_shape, request):
|
||||
module = _make_module()
|
||||
|
@ -45,5 +48,6 @@ def test_softmax(in_shape, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_softmax.py'])
|
||||
pytest.main(['-vv', 'test_softmax.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, out_channel, kernel_size, dim):
|
||||
|
||||
class SqueezeModule(torch.nn.Module):
|
||||
|
@ -34,6 +35,7 @@ def _make_module(in_shape, out_channel, kernel_size, dim):
|
|||
|
||||
return SqueezeModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[1, 4, 60, 72],
|
||||
[1, 3, 224, 224]
|
||||
|
@ -56,12 +58,14 @@ axes = [
|
|||
3
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('out_channel', out_channels)
|
||||
@pytest.mark.parametrize('kernel_size', kernel_sizes)
|
||||
@pytest.mark.parametrize('axis', axes)
|
||||
def test_squeeze(in_shape, out_channel, kernel_size, axis, request):
|
||||
out_shape = [in_shape[0], out_channel, in_shape[2] - kernel_size + 1, in_shape[3] - kernel_size + 1]
|
||||
out_shape = [in_shape[0], out_channel, in_shape[2] -
|
||||
kernel_size + 1, in_shape[3] - kernel_size + 1]
|
||||
dim = axis if out_shape[axis] == 1 else None
|
||||
module = _make_module(in_shape, out_channel, kernel_size, dim)
|
||||
|
||||
|
@ -69,5 +73,6 @@ def test_squeeze(in_shape, out_channel, kernel_size, axis, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_squeeze.py'])
|
||||
pytest.main(['-vv', 'test_squeeze.py'])
|
||||
|
|
|
@ -20,11 +20,12 @@ from onnx import AttributeProto, TensorProto, GraphProto
|
|||
from onnx_test_runner import OnnxTestRunner
|
||||
import numpy as np
|
||||
|
||||
|
||||
def _make_module(in_shape_list):
|
||||
|
||||
input_names = []
|
||||
input_nodes = []
|
||||
out=np.ones(1)
|
||||
out = np.ones(1)
|
||||
for i, in_shape in enumerate(in_shape_list):
|
||||
input_name = 'x{0}'.format(i)
|
||||
input_names.append(input_name)
|
||||
|
@ -50,6 +51,7 @@ def _make_module(in_shape_list):
|
|||
|
||||
return model_def
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[[224]],
|
||||
[[224], [1]],
|
||||
|
@ -57,10 +59,11 @@ in_shapes = [
|
|||
[[224, 224], [224, 224]],
|
||||
[[224, 224], [224]],
|
||||
[[224], [224, 224]],
|
||||
[[224], [224, 224],[3, 224, 224],],
|
||||
[[224], [224, 224],[1, 3, 224, 224]],
|
||||
[[224], [224, 224], [3, 224, 224], ],
|
||||
[[224], [224, 224], [1, 3, 224, 224]],
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
def test_sum(in_shape, request):
|
||||
model_def = _make_module(in_shape)
|
||||
|
@ -69,5 +72,6 @@ def test_sum(in_shape, request):
|
|||
model_file = runner.from_onnx_helper(model_def)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_sum.py'])
|
||||
pytest.main(['-vv', 'test_sum.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
import torch
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module(dim0, dim1):
|
||||
|
||||
class TransposeModule(torch.nn.Module):
|
||||
|
@ -29,6 +30,7 @@ def _make_module(dim0, dim1):
|
|||
|
||||
return TransposeModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[3, 4],
|
||||
[3, 4, 5],
|
||||
|
@ -44,6 +46,7 @@ axes = [
|
|||
[2, 3]
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('axis', axes)
|
||||
def test_transpose(in_shape, axis, request):
|
||||
|
@ -54,5 +57,6 @@ def test_transpose(in_shape, axis, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_transpose.py'])
|
||||
pytest.main(['-vv', 'test_transpose.py'])
|
||||
|
|
|
@ -18,6 +18,7 @@ import torch
|
|||
# import test_util
|
||||
from onnx_test_runner import OnnxTestRunner
|
||||
|
||||
|
||||
def _make_module():
|
||||
class UnaryModule(torch.nn.Module):
|
||||
def __init__(self):
|
||||
|
@ -55,6 +56,7 @@ def _make_module():
|
|||
|
||||
return UnaryModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[3],
|
||||
[64, 3],
|
||||
|
@ -62,6 +64,7 @@ in_shapes = [
|
|||
[8, 6, 16, 3]
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
def test_unary(in_shape, request):
|
||||
module = _make_module()
|
||||
|
@ -70,5 +73,6 @@ def test_unary(in_shape, request):
|
|||
model_file = runner.from_torch(module, in_shape)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_unary.py'])
|
||||
|
|
|
@ -20,6 +20,7 @@ from onnx import AttributeProto, TensorProto, GraphProto
|
|||
from onnx_test_runner import OnnxTestRunner
|
||||
import numpy as np
|
||||
|
||||
|
||||
def _make_module(in_shape, axes, op_version):
|
||||
|
||||
x = helper.make_tensor_value_info('x', TensorProto.FLOAT, in_shape)
|
||||
|
@ -49,7 +50,8 @@ def _make_module(in_shape, axes, op_version):
|
|||
# unsqueeze-13
|
||||
axes_len = []
|
||||
axes_len.append(len(axes))
|
||||
axes = onnx.helper.make_tensor('axes', onnx.TensorProto.INT64, axes_len, np.array(axes).astype(np.int64))
|
||||
axes = onnx.helper.make_tensor('axes', onnx.TensorProto.INT64,
|
||||
axes_len, np.array(axes).astype(np.int64))
|
||||
initializers.append(axes)
|
||||
unsqueeze = onnx.helper.make_node(
|
||||
'Unsqueeze',
|
||||
|
@ -71,6 +73,7 @@ def _make_module(in_shape, axes, op_version):
|
|||
|
||||
return model_def
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[224],
|
||||
[224, 224],
|
||||
|
@ -98,6 +101,7 @@ op_versions = [
|
|||
13
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('axes', axes_list)
|
||||
@pytest.mark.parametrize('op_version', op_versions)
|
||||
|
@ -109,5 +113,6 @@ def test_unsqueeze(in_shape, axes, op_version, request):
|
|||
model_file = runner.from_onnx_helper(model_def)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_unsqueeze.py'])
|
||||
pytest.main(['-vv', 'test_unsqueeze.py'])
|
||||
|
|
|
@ -20,12 +20,13 @@ import numpy as np
|
|||
import sys
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(batch_coff, in_shape, block_shape, crops):
|
||||
class BatchToSpaceModule(tf.Module):
|
||||
def __init__(self):
|
||||
super(BatchToSpaceModule).__init__()
|
||||
|
||||
@tf.function(input_signature=[tf.TensorSpec([batch_coff*np.prod(block_shape), *in_shape], tf.float32)])
|
||||
@tf.function(input_signature=[tf.TensorSpec([batch_coff * np.prod(block_shape), *in_shape], tf.float32)])
|
||||
def __call__(self, x):
|
||||
return tf.batch_to_space(x, block_shape, crops)
|
||||
return BatchToSpaceModule()
|
||||
|
@ -64,5 +65,6 @@ def test_batch_to_space(batch_coff, in_shape, block_shape, crops, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_batch_to_space.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, v_shape):
|
||||
class BinaryModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -37,6 +38,7 @@ def _make_module(in_shape, v_shape):
|
|||
return outs
|
||||
return BinaryModule()
|
||||
|
||||
|
||||
lhs_shapes = [
|
||||
[3],
|
||||
[64, 3],
|
||||
|
@ -69,5 +71,6 @@ def test_binary(lhs_shape, rhs_shape, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_binary.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, out_shape):
|
||||
class BroadcastModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -45,5 +46,6 @@ def test_broadcast(in_shape, out_shape, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_broadcast.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shapes, axis):
|
||||
class ConcatModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -53,5 +54,6 @@ def test_concat(in_shapes, axis, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_concat.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(n, i_channels, i_size, k_size, o_channels, strides, padding, dilations):
|
||||
class Conv2DModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -33,6 +34,7 @@ def _make_module(n, i_channels, i_size, k_size, o_channels, strides, padding, di
|
|||
return out
|
||||
return Conv2DModule()
|
||||
|
||||
|
||||
n = [
|
||||
1,
|
||||
3
|
||||
|
@ -93,5 +95,6 @@ def test_conv2d(n, i_channels, i_size, k_size, o_channels, strides, padding, dil
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_conv2d.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, dst_type):
|
||||
class ConvertModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -29,6 +30,7 @@ def _make_module(in_shape, dst_type):
|
|||
return tf.cast(x, dst_type)
|
||||
return ConvertModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[3],
|
||||
[64, 3],
|
||||
|
@ -51,5 +53,6 @@ def test_convert(in_shape, dst_type, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_convert.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(n, i_channels, i_size, k_size, strides, padding, dilations):
|
||||
class DepthwiseConv2DModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -29,10 +30,11 @@ def _make_module(n, i_channels, i_size, k_size, strides, padding, dilations):
|
|||
@tf.function(input_signature=[tf.TensorSpec([n, *i_size, i_channels], tf.float32)])
|
||||
def __call__(self, x):
|
||||
out = tf.nn.depthwise_conv2d(x, self.w, [1, *strides, 1], padding,
|
||||
dilations=dilations)
|
||||
dilations=dilations)
|
||||
return out
|
||||
return DepthwiseConv2DModule()
|
||||
|
||||
|
||||
n = [
|
||||
1,
|
||||
3
|
||||
|
@ -67,7 +69,7 @@ paddings = [
|
|||
|
||||
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
|
||||
]
|
||||
|
||||
|
||||
|
@ -87,5 +89,6 @@ def test_depthwise_conv2d(n, i_channels, i_size, k_size, strides, padding, dilat
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_depthwise_conv2d.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, indice, axis, batch_dims):
|
||||
class GatherModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -44,6 +45,7 @@ in_shape_indice_axis_batch_dims = [
|
|||
([2, 3, 4, 7], [[1, 1], [1, 2]], -1, 0),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape,indice,axis,batch_dims', in_shape_indice_axis_batch_dims)
|
||||
def test_gather(in_shape, indice, axis, batch_dims, request):
|
||||
module = _make_module(in_shape, indice, axis, batch_dims)
|
||||
|
@ -51,5 +53,6 @@ def test_gather(in_shape, indice, axis, batch_dims, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_gather.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, indice, batch_dims):
|
||||
class GatherModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -50,6 +51,7 @@ in_shape_indices_batch_dims = [
|
|||
[[[3], [1], [4]], [[1], [0], [2]], [[3], [2], [4]]]], 3)
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape,indices,batch_dims', in_shape_indices_batch_dims)
|
||||
def test_gather_nd(in_shape, indices, batch_dims, request):
|
||||
module = _make_module(in_shape, indices, batch_dims)
|
||||
|
@ -57,5 +59,6 @@ def test_gather_nd(in_shape, indices, batch_dims, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_gather_nd.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, paddings, mode, const):
|
||||
class PadModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -71,5 +72,6 @@ def test_pad(in_shape, paddings, mode, constant, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_pad.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, axis, keep_dims):
|
||||
class ReduceModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -34,6 +35,7 @@ def _make_module(in_shape, axis, keep_dims):
|
|||
return outs
|
||||
return ReduceModule()
|
||||
|
||||
|
||||
in_shape_axis = [
|
||||
([3], [0]),
|
||||
([64, 3], [0]),
|
||||
|
@ -60,6 +62,7 @@ keep_dims = [
|
|||
False
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape,axis', in_shape_axis)
|
||||
@pytest.mark.parametrize('keep_dims', keep_dims)
|
||||
def test_reduce(in_shape, axis, keep_dims, request):
|
||||
|
@ -69,5 +72,6 @@ def test_reduce(in_shape, axis, keep_dims, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_reduce.py'])
|
||||
|
|
|
@ -35,6 +35,7 @@ def _make_module(n, i_channels, i_size, k_size, strides, padding):
|
|||
return outs
|
||||
return ReduceWindow2DModule()
|
||||
|
||||
|
||||
n = [
|
||||
1,
|
||||
3
|
||||
|
@ -83,5 +84,6 @@ def test_reduce_window2d(n, i_channels, i_size, k_size, strides, padding, reques
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_reduce_window2d.py'])
|
||||
pytest.main(['-vv', 'test_reduce_window2d.py'])
|
||||
|
|
|
@ -17,6 +17,7 @@ import pytest
|
|||
from tflite_test_runner import TfliteTestRunner
|
||||
import tensorflow as tf
|
||||
|
||||
|
||||
def _make_module(in_shape, size, align_corners, half_pixel_centers, mode):
|
||||
class ResizeModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -30,6 +31,7 @@ def _make_module(in_shape, size, align_corners, half_pixel_centers, mode):
|
|||
return tf.compat.v1.image.resize_nearest_neighbor(x, size, align_corners=align_corners, half_pixel_centers=half_pixel_centers)
|
||||
return ResizeModule()
|
||||
|
||||
|
||||
in_shape = [
|
||||
[2, 32, 32, 3]
|
||||
]
|
||||
|
@ -56,12 +58,12 @@ modes = [
|
|||
tf.image.ResizeMethod.NEAREST_NEIGHBOR
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shape)
|
||||
@pytest.mark.parametrize('size', sizes)
|
||||
@pytest.mark.parametrize('align_corners', align_corners)
|
||||
@pytest.mark.parametrize('half_pixel_centers', half_pixel_centers)
|
||||
@pytest.mark.parametrize('mode', modes)
|
||||
|
||||
def test_resize(in_shape, size, align_corners, half_pixel_centers, mode, request):
|
||||
if mode == tf.image.ResizeMethod.BILINEAR and align_corners and half_pixel_centers:
|
||||
return
|
||||
|
@ -70,5 +72,6 @@ def test_resize(in_shape, size, align_corners, half_pixel_centers, mode, request
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_resize.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, begin, size):
|
||||
class SliceModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -48,5 +49,6 @@ def test_slice(in_shape, begin, size, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_slice.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, begin, end, strides):
|
||||
class StridedSliceModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -45,5 +46,6 @@ def test_strided_slice(in_shape, begin, end, strides, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_strided_slice.py'])
|
||||
pytest.main(['-vv', 'test_strided_slice.py'])
|
||||
|
|
|
@ -19,6 +19,7 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, perm):
|
||||
class TransposeModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -29,6 +30,7 @@ def _make_module(in_shape, perm):
|
|||
return tf.transpose(x, perm=perm)
|
||||
return TransposeModule()
|
||||
|
||||
|
||||
in_shapes = [
|
||||
[3, 2],
|
||||
[64, 3],
|
||||
|
@ -60,5 +62,6 @@ def test_transpose(in_shape, perm, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_transpose.py'])
|
||||
pytest.main(['-vv', 'test_transpose.py'])
|
||||
|
|
|
@ -21,6 +21,7 @@ import numpy as np
|
|||
import sys
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(n, i_channels, i_size, k_size, o_channels, strides, padding, dilations, act):
|
||||
class Conv2DActModule(tf.Module):
|
||||
def __init__(self):
|
||||
|
@ -103,5 +104,6 @@ def test_conv2d_act(n, i_channels, i_size, k_size, o_channels, strides, padding,
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_conv2d_act.py'])
|
||||
|
|
|
@ -17,10 +17,12 @@
|
|||
import pytest
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def test_20classes_yolo(request):
|
||||
runner = TfliteTestRunner(request.node.name)
|
||||
model_file = 'examples/20classes_yolo/model/20classes_yolo.tflite'
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_20classes_yolo.py'])
|
||||
pytest.main(['-vv', 'test_20classes_yolo.py'])
|
||||
|
|
|
@ -19,13 +19,16 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape):
|
||||
return tf.keras.applications.DenseNet121(input_shape=in_shape)
|
||||
|
||||
|
||||
in_shapes = [
|
||||
(224, 224, 3)
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
def test_densenet(in_shape, request):
|
||||
module = _make_module(in_shape)
|
||||
|
@ -34,5 +37,6 @@ def test_densenet(in_shape, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_densenet.py'])
|
||||
pytest.main(['-vv', 'test_densenet.py'])
|
||||
|
|
|
@ -23,7 +23,8 @@ from tflite_test_runner import TfliteTestRunner
|
|||
|
||||
|
||||
def _make_module(in_shape, alpha):
|
||||
return tf.keras.applications.MobileNet(in_shape, alpha, include_top=True)
|
||||
return tf.keras.applications.MobileNet(in_shape, alpha, include_top=False)
|
||||
|
||||
|
||||
in_shapes = [
|
||||
(224, 224, 3)
|
||||
|
@ -33,14 +34,16 @@ alphas = [
|
|||
1.0
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('alpha', alphas)
|
||||
def test_mobilenetv1(in_shape, alpha, request):
|
||||
module = _make_module(in_shape, alpha)
|
||||
|
||||
runner = TfliteTestRunner(request.node.name)
|
||||
overwrite_cfg = {'judge': {'threshold': 0.95}}
|
||||
runner = TfliteTestRunner(request.node.name, overwirte_configs=overwrite_cfg)
|
||||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_mobilenetv1.py'])
|
||||
pytest.main(['-vv', 'test_mobilenetv1.py'])
|
||||
|
|
|
@ -21,9 +21,11 @@ import numpy as np
|
|||
import sys
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape, alpha):
|
||||
return tf.keras.applications.MobileNetV2(in_shape, alpha)
|
||||
|
||||
|
||||
in_shapes = [
|
||||
(224, 224, 3)
|
||||
]
|
||||
|
@ -32,6 +34,7 @@ alphas = [
|
|||
1.0
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
@pytest.mark.parametrize('alpha', alphas)
|
||||
def test_mobilenetv2(in_shape, alpha, request):
|
||||
|
@ -41,5 +44,6 @@ def test_mobilenetv2(in_shape, alpha, request):
|
|||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_mobilenetv2.py'])
|
||||
pytest.main(['-vv', 'test_mobilenetv2.py'])
|
||||
|
|
|
@ -19,20 +19,24 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape):
|
||||
return tf.keras.applications.ResNet50V2(input_shape=in_shape)
|
||||
return tf.keras.applications.ResNet50(input_shape=in_shape, include_top=False)
|
||||
|
||||
|
||||
in_shapes = [
|
||||
(224, 224, 3)
|
||||
]
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
def test_resnet50v2(in_shape, request):
|
||||
module = _make_module(in_shape)
|
||||
|
||||
runner = TfliteTestRunner(request.node.name)
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
def test_resnet50(in_shape, request):
|
||||
module = _make_module(in_shape)
|
||||
overwrite_cfg = {'judge': {'threshold': 0.92}}
|
||||
runner = TfliteTestRunner(request.node.name, overwirte_configs=overwrite_cfg)
|
||||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_resnet50v2.py'])
|
||||
pytest.main(['-vv', 'test_resnet50.py'])
|
||||
|
|
|
@ -19,20 +19,24 @@ import tensorflow as tf
|
|||
import numpy as np
|
||||
from tflite_test_runner import TfliteTestRunner
|
||||
|
||||
|
||||
def _make_module(in_shape):
|
||||
return tf.keras.applications.ResNet50(input_shape=in_shape)
|
||||
return tf.keras.applications.ResNet50V2(input_shape=in_shape)
|
||||
|
||||
|
||||
in_shapes = [
|
||||
(224, 224, 3)
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('in_shape', in_shapes)
|
||||
def test_resnet50(in_shape, request):
|
||||
def test_resnet50v2(in_shape, request):
|
||||
module = _make_module(in_shape)
|
||||
|
||||
runner = TfliteTestRunner(request.node.name)
|
||||
model_file = runner.from_tensorflow(module)
|
||||
runner.run(model_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(['-vv', 'test_resnet50.py'])
|
||||
pytest.main(['-vv', 'test_resnet50v2.py'])
|
||||
|
|
|
@ -8,9 +8,10 @@ import os
|
|||
import numpy as np
|
||||
from test_runner import *
|
||||
|
||||
|
||||
class OnnxTestRunner(TestRunner):
|
||||
def __init__(self, case_name, targets=None):
|
||||
super().__init__(case_name, targets)
|
||||
def __init__(self, case_name, targets=None, overwirte_configs: dict = None):
|
||||
super().__init__(case_name, targets, overwirte_configs)
|
||||
|
||||
def from_torch(self, module, in_shape, opset_version=11):
|
||||
# export model
|
||||
|
@ -76,7 +77,7 @@ class OnnxTestRunner(TestRunner):
|
|||
curret_version = onnx_model.opset_import[0].version
|
||||
for i in range(curret_version, 8):
|
||||
onnx_model = version_converter.convert_version(
|
||||
onnx_model, i+1)
|
||||
onnx_model, i + 1)
|
||||
|
||||
if simplify:
|
||||
onnx_model = onnx.shape_inference.infer_shapes(onnx_model)
|
||||
|
@ -145,8 +146,8 @@ class OnnxTestRunner(TestRunner):
|
|||
text_file = os.path.join(case_dir, f'cpu_result_{i}.txt')
|
||||
self.output_paths.append((bin_file, text_file))
|
||||
output.tofile(bin_file)
|
||||
save_array_as_txt(text_file, output)
|
||||
self.totxtfile(text_file, output)
|
||||
i += 1
|
||||
|
||||
def import_model(self, compiler, model_content, import_options):
|
||||
compiler.import_onnx(model_content, import_options)
|
||||
compiler.import_onnx(model_content, import_options)
|
||||
|
|
|
@ -9,7 +9,7 @@ import shutil
|
|||
from abc import ABCMeta, abstractmethod
|
||||
import nncase
|
||||
import struct
|
||||
from compare_util import compare_with_ground_truth, VerboseType
|
||||
from compare_util import compare
|
||||
|
||||
|
||||
class Edict:
|
||||
|
@ -47,6 +47,14 @@ class Edict:
|
|||
s += '\n'
|
||||
return s.rstrip('\n')
|
||||
|
||||
def update(self, d):
|
||||
for name, value in d.items():
|
||||
if name in self.keys():
|
||||
if isinstance(value, dict):
|
||||
getattr(self, name).update(value)
|
||||
else:
|
||||
self.__dict__[name] = value
|
||||
|
||||
|
||||
def generate_random(shape: List[int], dtype: np.dtype) -> np.ndarray:
|
||||
if dtype is np.uint8:
|
||||
|
@ -59,25 +67,12 @@ def generate_random(shape: List[int], dtype: np.dtype) -> np.ndarray:
|
|||
return data
|
||||
|
||||
|
||||
def save_array_as_txt(save_path, value_np, bit_16_represent=False):
|
||||
if bit_16_represent:
|
||||
np.save(save_path, _cast_bfloat16_then_float32(value_np))
|
||||
else:
|
||||
with open(save_path, 'w') as f:
|
||||
shape_info = "shape: (" + ",".join(str(dim)
|
||||
for dim in value_np.shape) + ")\n"
|
||||
f.write(shape_info)
|
||||
|
||||
for val in value_np.reshape([-1]):
|
||||
f.write("%f\n" % val)
|
||||
print("----> %s" % save_path)
|
||||
|
||||
|
||||
def _cast_bfloat16_then_float32(values: np.array):
|
||||
shape = values.shape
|
||||
values = values.reshape([-1])
|
||||
for i, value in enumerate(values):
|
||||
value = float(value)
|
||||
value = 1
|
||||
packed = struct.pack('!f', value)
|
||||
integers = [c for c in packed][:2] + [0, 0]
|
||||
value = struct.unpack('!f', bytes(integers))[0]
|
||||
|
@ -93,11 +88,13 @@ Fuc = {
|
|||
|
||||
|
||||
class TestRunner(metaclass=ABCMeta):
|
||||
def __init__(self, case_name, targets=None) -> None:
|
||||
def __init__(self, case_name, targets=None, overwirte_configs: dict = None) -> None:
|
||||
config_root = os.path.dirname(__file__)
|
||||
with open(os.path.join(config_root, 'config.yml'), encoding='utf8') as f:
|
||||
cfg = yaml.safe_load(f)
|
||||
cfg: dict = yaml.safe_load(f)
|
||||
config = Edict(cfg)
|
||||
if overwirte_configs:
|
||||
config.update(overwirte_configs)
|
||||
|
||||
self.cfg = self.validte_config(config)
|
||||
|
||||
|
@ -105,15 +102,7 @@ class TestRunner(metaclass=ABCMeta):
|
|||
self.case_dir = os.path.join(self.cfg.setup.root, case_name)
|
||||
self.clear(self.case_dir)
|
||||
|
||||
if targets is None:
|
||||
self.cfg.case.eval[0].values = self.validate_targets(
|
||||
self.cfg.case.eval[0].values)
|
||||
self.cfg.case.infer[0].values = self.validate_targets(
|
||||
self.cfg.case.infer[0].values)
|
||||
else:
|
||||
targets = self.validate_targets(targets)
|
||||
self.cfg.case.eval[0].values = targets
|
||||
self.cfg.case.infer[0].values = targets
|
||||
self.validate_targets(targets)
|
||||
|
||||
self.inputs: List[Dict] = []
|
||||
self.calibs: List[Dict] = []
|
||||
|
@ -124,19 +113,28 @@ class TestRunner(metaclass=ABCMeta):
|
|||
self.num_pattern = re.compile("(\d+)")
|
||||
|
||||
def validte_config(self, config):
|
||||
in_ci = os.getenv('CI', False)
|
||||
if in_ci:
|
||||
config.judge.log_hist = False
|
||||
config.setup.log_txt = False
|
||||
return config
|
||||
|
||||
def validate_targets(self, targets):
|
||||
new_targets = []
|
||||
for t in targets:
|
||||
if nncase.test_target(t):
|
||||
new_targets.append(t)
|
||||
else:
|
||||
print("WARN: target[{0}] not found".format(t))
|
||||
return new_targets
|
||||
def validate_targets(self, targets: List[str]):
|
||||
def _validate_targets(old_targets: List[str]):
|
||||
new_targets = []
|
||||
for t in old_targets:
|
||||
if nncase.test_target(t):
|
||||
new_targets.append(t)
|
||||
else:
|
||||
print("WARN: target[{0}] not found".format(t))
|
||||
return new_targets
|
||||
self.cfg.case.eval[0].values = _validate_targets(
|
||||
targets if targets else self.cfg.case.eval[0].values)
|
||||
self.cfg.case.infer[0].values = _validate_targets(
|
||||
targets if targets else self.cfg.case.infer[0].values)
|
||||
|
||||
def run(self, model_path: str):
|
||||
# 这里开多线程池去跑
|
||||
# TODO add mulit process pool
|
||||
# case_name = self.process_model_path_name(model_path)
|
||||
# case_dir = os.path.join(self.cfg.setup.root, case_name)
|
||||
# if not os.path.exists(case_dir):
|
||||
|
@ -272,7 +270,7 @@ class TestRunner(metaclass=ABCMeta):
|
|||
os.path.join(eval_dir, f'nncase_result_{i}.bin'),
|
||||
os.path.join(eval_dir, f'nncase_result_{i}.txt')))
|
||||
result.tofile(eval_output_paths[-1][0])
|
||||
save_array_as_txt(eval_output_paths[-1][1], result)
|
||||
self.totxtfile(eval_output_paths[-1][1], result)
|
||||
return eval_output_paths
|
||||
|
||||
def nncase_infer(self, cfg, case_dir: str,
|
||||
|
@ -288,7 +286,8 @@ class TestRunner(metaclass=ABCMeta):
|
|||
self.import_model(compiler, model_content, import_options)
|
||||
if kwargs['ptq']:
|
||||
ptq_options = nncase.PTQTensorOptions()
|
||||
ptq_options.set_tensor_data(np.asarray([sample['data'] for sample in self.calibs]).tobytes())
|
||||
ptq_options.set_tensor_data(np.asarray(
|
||||
[sample['data'] for sample in self.calibs]).tobytes())
|
||||
ptq_options.samples_count = cfg.generate_calibs.batch_size
|
||||
ptq_options.input_mean = cfg.ptq_opt.kwargs['input_mean']
|
||||
ptq_options.input_std = cfg.ptq_opt.kwargs['input_std']
|
||||
|
@ -313,7 +312,7 @@ class TestRunner(metaclass=ABCMeta):
|
|||
os.path.join(infer_dir, f'nncase_result_{i}.bin'),
|
||||
os.path.join(infer_dir, f'nncase_result_{i}.txt')))
|
||||
result.tofile(infer_output_paths[-1][0])
|
||||
save_array_as_txt(infer_output_paths[-1][1], result)
|
||||
self.totxtfile(infer_output_paths[-1][1], result)
|
||||
return infer_output_paths
|
||||
|
||||
def on_test_start(self) -> None:
|
||||
|
@ -331,7 +330,7 @@ class TestRunner(metaclass=ABCMeta):
|
|||
(os.path.join(case_dir, f'{name}_{n}_{i}.bin'),
|
||||
os.path.join(case_dir, f'{name}_{n}_{i}.txt')))
|
||||
data.tofile(path_list[-1][0])
|
||||
save_array_as_txt(path_list[-1][1], data)
|
||||
self.totxtfile(path_list[-1][1], data)
|
||||
i += 1
|
||||
input['data'] = data
|
||||
|
||||
|
@ -349,22 +348,28 @@ class TestRunner(metaclass=ABCMeta):
|
|||
test_outputs: List[Tuple[str]],
|
||||
kwargs: Dict[str, str]):
|
||||
for ref_file, test_file in zip(ref_ouputs, test_outputs):
|
||||
judge = compare_with_ground_truth(test_file[1],
|
||||
ref_file[1],
|
||||
state=0,
|
||||
verbose=VerboseType.PRINT_RESULT)
|
||||
|
||||
judge, simarity_info = compare(test_file, ref_file,
|
||||
self.cfg.judge.simarity_name,
|
||||
self.cfg.judge.threshold,
|
||||
self.cfg.judge.log_hist)
|
||||
name_list = test_file[1].split('/')
|
||||
kw_names = ' '.join(name_list[-len(kwargs) - 2:-1])
|
||||
i = self.num_pattern.findall(name_list[-1])
|
||||
if judge.is_good():
|
||||
result = "\nPass [ {0} ] Output: {1}!!\n".format(kw_names, i)
|
||||
print(result)
|
||||
with open(os.path.join(self.case_dir, 'test_result.txt'), 'a+') as f:
|
||||
f.write(result)
|
||||
else:
|
||||
result = "\nFail [ {0} ] Output: {1}!!\n".format(kw_names, i)
|
||||
print(result)
|
||||
with open(os.path.join(self.case_dir, 'test_result.txt'), 'a+') as f:
|
||||
f.write(result)
|
||||
result_info = "\n{0} [ {1} ] Output: {2}!!\n".format(
|
||||
'Pass' if judge else 'Fail', kw_names, i)
|
||||
result = simarity_info + result_info
|
||||
print(result)
|
||||
with open(os.path.join(self.case_dir, 'test_result.txt'), 'a+') as f:
|
||||
f.write(result)
|
||||
if not judge:
|
||||
return False
|
||||
return True
|
||||
|
||||
def totxtfile(self, save_path, value_np: np.array, bit_16_represent=False):
|
||||
if self.cfg.setup.log_txt:
|
||||
if bit_16_represent:
|
||||
np.save(save_path, _cast_bfloat16_then_float32(value_np))
|
||||
else:
|
||||
np.savetxt(save_path, value_np.flatten(), fmt='%f', header=str(value_np.shape))
|
||||
print("----> %s" % save_path)
|
||||
|
|
|
@ -3,9 +3,10 @@ from test_runner import *
|
|||
import os
|
||||
import shutil
|
||||
|
||||
|
||||
class TfliteTestRunner(TestRunner):
|
||||
def __init__(self, case_name, targets=None):
|
||||
super().__init__(case_name, targets)
|
||||
def __init__(self, case_name, targets=None, overwirte_configs: dict = None):
|
||||
super().__init__(case_name, targets, overwirte_configs)
|
||||
|
||||
def from_tensorflow(self, module):
|
||||
# export model
|
||||
|
@ -63,9 +64,8 @@ class TfliteTestRunner(TestRunner):
|
|||
os.path.join(case_dir, f'cpu_result_{i}.bin'),
|
||||
os.path.join(case_dir, f'cpu_result_{i}.txt')))
|
||||
data.tofile(self.output_paths[-1][0])
|
||||
save_array_as_txt(self.output_paths[-1][1], data)
|
||||
self.totxtfile(self.output_paths[-1][1], data)
|
||||
i += 1
|
||||
# output['data'] = data
|
||||
|
||||
def import_model(self, compiler, model_content, import_options):
|
||||
compiler.import_tflite(model_content, import_options)
|
||||
compiler.import_tflite(model_content, import_options)
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
#!/bin/bash
|
||||
autopep8 tests python -r --in-place
|
Loading…
Reference in New Issue