ONE - On-device Neural Engine
Loading...
Searching...
No Matches
backends.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
17import glob
18import ntpath
19import os
20
21import onelib.utils as oneutils
22"""
23[one hierarchy]
24one
25├── backends
26├── bin
27├── doc
28├── include
29├── lib
30├── optimization
31├── target
32└── test
33
34The list where `one-XXXX` finds its backends
35- `bin` folder where `one-XXXX` exists
36- `backends` folder
37
38NOTE If there are backends of the same name in different places,
39 the closer to the top in the list, the higher the priority.
40
41[About TARGET and BACKEND]
42 "Target" refers to an instance from the core of the system and
43 "Backend" refers to an architecture. Say there is a NPU that has
44 multiple cores. Its cores may have different global buffer
45 size, DSPM size and clock rate, etc, which are described in
46 each configuration file of "Target". Even though they
47 are different target, they may follow same architecture, which means
48 they have same "Backend".
49
50[Path for TARGET configuration]
51 - /usr/share/one/target/${TARGET}.ini
52
53[Path for BACKEND tools]
54 - /usr/share/one/backends/${BACKEND}
55"""
56
57
58def get_list(cmdname):
59 dir_path = os.path.dirname(os.path.realpath(__file__))
60 backend_set = set()
61
62 # bin folder
63 files = [f for f in glob.glob(dir_path + '/../' + cmdname)]
64 # backends folder
65 files += [
66 f for f in glob.glob(dir_path + '/../../backends/**/' + cmdname, recursive=True)
67 ]
68 # TODO find backends in `$PATH`
69
70 backends_list = []
71 for cand in files:
72 base = ntpath.basename(cand)
73 if (not base in backend_set) and os.path.isfile(cand) and os.access(
74 cand, os.X_OK):
75 backend_set.add(base)
76 backends_list.append(cand)
77
78 return backends_list
79
80
81def get_value_from_target_conf(target: str, key: str):
82 dir_path = os.path.dirname(os.path.realpath(__file__))
83 target_conf_path = dir_path + f'/../../target/{target}.ini'
84 if not os.path.isfile(target_conf_path):
85 raise FileNotFoundError(f"Not found given target configuration: {target}")
86
87 # target config doesn't have section.
88 # but, configparser needs configs to have one or more sections.
89 DUMMY_SECTION = 'dummy_section'
90 with open(target_conf_path, 'r') as f:
91 config_str = f'[{DUMMY_SECTION}]\n' + f.read()
92 parser = oneutils.get_config_parser()
93 parser.read_string(config_str)
94 assert parser.has_section(DUMMY_SECTION)
95
96 # Check if target file is valid
97 TARGET_KEY = 'TARGET'
98 assert TARGET_KEY in parser[DUMMY_SECTION]
99 if target != parser[DUMMY_SECTION][TARGET_KEY]:
100 raise RuntimeError("Invalid target file.")
101
102 if key in parser[DUMMY_SECTION]:
103 return parser[DUMMY_SECTION][key]
104
105 raise RuntimeError(f"Not found '{key}' key in target configuration.")
106
107
108def search_driver(driver):
109 dir_path = os.path.dirname(os.path.realpath(__file__))
110
111 # CASE 1: one/bin/{driver} is found
112 driver_path = dir_path + '/../' + driver
113 if os.path.isfile(driver_path) and os.access(driver_path, os.X_OK):
114 return driver_path
115
116 # CASE 2: one/backends/**/bin/{driver} is found
117 for driver_path in glob.glob(dir_path + '/../../backends/**/bin/' + driver,
118 recursive=True):
119 if os.path.isfile(driver_path) and os.access(driver_path, os.X_OK):
120 return driver_path
121
122 # CASE 3: {driver} is found in nowhere
123 return None
get_list(cmdname)
Definition backends.py:58
search_driver(driver)
Definition backends.py:108
get_value_from_target_conf(str target, str key)
Definition backends.py:81