ONE - On-device Neural Engine
Loading...
Searching...
No Matches
export_constant.py
Go to the documentation of this file.
1#!/usr/bin/env python
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
17from constant import CONSTANT
18
19import argparse
20import configparser
21
22
23def main():
24 parser = argparse.ArgumentParser(
25 description='Export CONSTANT value with given file format.')
26 parser.add_argument('-c',
27 '--constant',
28 type=str,
29 required=True,
30 help='Constant name to export')
31 parser.add_argument(
32 '-f',
33 '--format',
34 type=str,
35 required=True,
36 choices=['cfg', 'txt'],
37 help=
38 'File format to export. The created cfg file contains CONSTANT under the one-optimize section.'
39 )
40 parser.add_argument(
41 '--exclusive',
42 action='store_true',
43 help='Exports the rest of the options except for the given constant')
44 parser.add_argument('-o',
45 '--output_path',
46 type=str,
47 required=True,
48 help='Path to output')
49
50 args = parser.parse_args()
51
52 if not hasattr(CONSTANT, args.constant):
53 raise NameError('Not found given constant name')
54
55 if args.exclusive:
56 constant_to_exclude = getattr(CONSTANT, args.constant)
57 constant_to_export = []
58 for opt in CONSTANT.OPTIMIZATION_OPTS:
59 if opt[0] in constant_to_exclude:
60 continue
61 constant_to_export.append(opt[0])
62 else:
63 constant_to_export = getattr(CONSTANT, args.constant)
64
65 if args.format == 'cfg':
66 SECTION_TO_EXPORT = 'one-optimize'
67 config = configparser.ConfigParser()
68 config[SECTION_TO_EXPORT] = dict()
69 for constant in constant_to_export:
70 config[SECTION_TO_EXPORT][constant] = 'True'
71
72 with open(args.output_path, 'w') as f:
73 config.write(f)
74
75 if args.format == 'txt':
76 with open(args.output_path, 'w') as f:
77 for constant in constant_to_export:
78 f.write(f"{constant}\n")
79
80
81if __name__ == '__main__':
82 main()