ONE - On-device Neural Engine
Loading...
Searching...
No Matches
q_implant_qparam_test.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3# Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import argparse
18import subprocess
19import os
20import importlib
21
22from test_utils import TestRunner
23from q_implant_validator import validate
24
25parser = argparse.ArgumentParser()
26parser.add_argument('--input_dir', type=str, required=True)
27parser.add_argument('--output_dir', type=str, required=True)
28parser.add_argument('--driver', type=str, required=True)
29parser.add_argument('--dump', type=str, required=True)
30parser.add_argument('--model', type=str, required=True)
31args = parser.parse_args()
32
33input_dir = args.input_dir
34output_dir = args.output_dir
35driver = args.driver
36dump = args.dump
37model = args.model
38
39module = importlib.import_module('qparam.' + model)
40
41input_circle = input_dir + '.circle'
42output_circle = output_dir + f'/{module._name_}/output.circle'
43qparam_dir = output_dir + f'/{module._name_}/qparam.json'
44h5_path = output_dir + f'/{module._name_}/output.h5'
45
46if not os.path.exists(input_circle):
47 print('fail to load input circle')
48 quit(255)
49
50# if the previous test dummy file exist, remove it.
51if os.path.exists(output_circle):
52 os.remove(output_circle)
53
54if os.path.exists(h5_path):
55 os.remove(h5_path)
56
57# generate qparam.json and numpys
58test_runner = TestRunner(output_dir)
59
60test_runner.register(module._test_case_)
61
62test_runner.run()
63
64if not os.path.exists(qparam_dir):
65 print('qparam generate fail')
66 quit(255)
67
68# run q-implant
69process = subprocess.run([driver, input_circle, qparam_dir, output_circle], check=True)
70
71try:
72 process.check_returncode()
73except:
74 print('q-implant run failed')
75 quit(255)
76
77if not os.path.exists(output_circle):
78 print('output circle generate fail')
79 quit(255)
80
81# dump circle to h5
82process = subprocess.run([dump, '--tensors_to_hdf5', h5_path, output_circle], check=True)
83
84try:
85 process.check_returncode()
86except:
87 print('circle-tensordump run failed')
88 quit(255)
89
90if not os.path.exists(h5_path):
91 print('h5 dump failed')
92 quit(255)
93
94if not validate(h5_path, output_dir + f'/{module._name_}', qparam_dir):
95 quit(255)
96
97quit(0)