ONE - On-device Neural Engine
Loading...
Searching...
No Matches
make_cmd.py
Go to the documentation of this file.
1#!/usr/bin/env python
2
3# Copyright (c) 2022 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 os
18import sys
19
20import onelib.constant as _constant
21
22
23def is_valid_attr(args, attr):
24 return hasattr(args, attr) and getattr(args, attr)
25
26
27def make_tf2tfliteV2_cmd(args, driver_path, input_path, output_path):
28 """make a command for running tf2tfliteV2.py"""
29 cmd = [sys.executable, os.path.expanduser(driver_path)]
30 # verbose
31 if is_valid_attr(args, 'verbose'):
32 cmd.append('--verbose')
33 # model_format
34 if is_valid_attr(args, 'model_format_cmd'):
35 cmd.append(getattr(args, 'model_format_cmd'))
36 elif is_valid_attr(args, 'model_format'):
37 cmd.append('--' + getattr(args, 'model_format'))
38 else:
39 cmd.append('--graph_def') # default value
40 # converter version
41 if is_valid_attr(args, 'converter_version_cmd'):
42 cmd.append(getattr(args, 'converter_version_cmd'))
43 elif is_valid_attr(args, 'converter_version'):
44 cmd.append('--' + getattr(args, 'converter_version'))
45 else:
46 cmd.append('--v1') # default value
47 # input_path
48 if is_valid_attr(args, 'input_path'):
49 cmd.append('--input_path')
50 cmd.append(os.path.expanduser(input_path))
51 # output_path
52 if is_valid_attr(args, 'output_path'):
53 cmd.append('--output_path')
54 cmd.append(os.path.expanduser(output_path))
55 # input_arrays
56 if is_valid_attr(args, 'input_arrays'):
57 cmd.append('--input_arrays')
58 cmd.append(getattr(args, 'input_arrays'))
59 # input_shapes
60 if is_valid_attr(args, 'input_shapes'):
61 cmd.append('--input_shapes')
62 cmd.append(getattr(args, 'input_shapes'))
63 # output_arrays
64 if is_valid_attr(args, 'output_arrays'):
65 cmd.append('--output_arrays')
66 cmd.append(getattr(args, 'output_arrays'))
67
68 # experimental options
69 if is_valid_attr(args, 'experimental_disable_batchmatmul_unfold'):
70 cmd.append('--experimental_disable_batchmatmul_unfold')
71
72 return cmd
73
74
75def make_tflite2circle_cmd(driver_path, input_path, output_path):
76 """make a command for running tflite2circle"""
77 cmd = [driver_path, input_path, output_path]
78 return [os.path.expanduser(c) for c in cmd]
79
80
81def make_circle2circle_cmd(args, driver_path, input_path, output_path):
82 """make a command for running circle2circle"""
83 cmd = [os.path.expanduser(c) for c in [driver_path, input_path, output_path]]
84 # profiling
85 if is_valid_attr(args, 'generate_profile_data'):
86 cmd.append('--generate_profile_data')
87 # optimization pass(only true/false options)
88 # TODO support options whose number of arguments is more than zero
89 for opt in _constant.CONSTANT.OPTIMIZATION_OPTS:
90 if is_valid_attr(args, opt[0]):
91 # ./driver --opt[0]
92 if type(getattr(args, opt[0])) is bool:
93 cmd.append('--' + opt[0])
94 """
95 This condition check is for config file interface, usually would be
96 SomeOption=True
97 but user can write as follows while development
98 SomeOption=False
99 instead of removing SomeOption option
100 """
101 if type(getattr(args, opt[0])) is str and not getattr(
102 args, opt[0]).lower() in ['false', '0', 'n']:
103 cmd.append('--' + opt[0])
104
105 return cmd
is_valid_attr(args, attr)
Definition make_cmd.py:23
make_circle2circle_cmd(args, driver_path, input_path, output_path)
Definition make_cmd.py:81
make_tf2tfliteV2_cmd(args, driver_path, input_path, output_path)
Definition make_cmd.py:27
make_tflite2circle_cmd(driver_path, input_path, output_path)
Definition make_cmd.py:75