-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_util.py
87 lines (71 loc) · 3.19 KB
/
_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#
# Copyright 2023 Picovoice Inc.
#
# You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
# file accompanying this source.
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
import os
import platform
import subprocess
def _is_64bit():
return '64bit' in platform.architecture()[0]
def _linux_machine() -> str:
machine = platform.machine()
if machine == 'x86_64':
return machine
elif machine in ['aarch64', 'armv7l']:
arch_info = ('-' + machine) if _is_64bit() else ''
else:
raise NotImplementedError("Unsupported CPU architecture: `%s`" % machine)
cpu_info = ''
try:
cpu_info = subprocess.check_output(['cat', '/proc/cpuinfo']).decode('utf-8')
cpu_part_list = [x for x in cpu_info.split('\n') if 'CPU part' in x]
cpu_part = cpu_part_list[0].split(' ')[-1].lower()
except Exception as e:
raise RuntimeError("Failed to identify the CPU with `%s`\nCPU info: `%s`" % (e, cpu_info))
if '0xd03' == cpu_part:
return 'cortex-a53' + arch_info
elif '0xd07' == cpu_part:
return 'cortex-a57' + arch_info
elif '0xd08' == cpu_part:
return 'cortex-a72' + arch_info
elif "0xd0b" == cpu_part:
return "cortex-a76" + arch_info
else:
raise NotImplementedError("Unsupported CPU: `%s`." % cpu_part)
_RASPBERRY_PI_MACHINES = {
"cortex-a53",
"cortex-a72",
"cortex-a76",
"cortex-a53-aarch64",
"cortex-a72-aarch64",
"cortex-a76-aarch64"}
def default_library_path(relative: str = '') -> str:
if platform.system() == 'Darwin':
if platform.machine() == 'x86_64':
return os.path.join(os.path.dirname(__file__), relative, 'lib/mac/x86_64/libpv_koala.dylib')
elif platform.machine() == "arm64":
return os.path.join(os.path.dirname(__file__), relative, 'lib/mac/arm64/libpv_koala.dylib')
elif platform.system() == 'Linux':
linux_machine = _linux_machine()
if linux_machine == 'x86_64':
return os.path.join(os.path.dirname(__file__), relative, 'lib/linux/x86_64/libpv_koala.so')
elif linux_machine in _RASPBERRY_PI_MACHINES:
return os.path.join(
os.path.dirname(__file__),
relative,
'lib/raspberry-pi/%s/libpv_koala.so' % linux_machine)
elif platform.system() == 'Windows':
if platform.machine().lower() == 'amd64':
return os.path.join(os.path.dirname(__file__), relative, 'lib/windows/amd64/libpv_koala.dll')
elif platform.machine().lower() == 'arm64':
return os.path.join(os.path.dirname(__file__), relative, 'lib/windows/arm64/libpv_koala.dll')
raise NotImplementedError('Unsupported platform.')
def default_model_path(relative: str = '') -> str:
return os.path.join(os.path.dirname(__file__), relative, 'lib/common/koala_params.pv')
__all__ = ['default_library_path', 'default_model_path']