xmos-ai-tools 1.3.2.dev40__py3-none-win_amd64.whl → 1.3.2.dev82__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- xmos_ai_tools/__init__.py +7 -7
- xmos_ai_tools/io_server/__init__.py +151 -151
- xmos_ai_tools/runtime/buildfiles/aitoolslib.cmake +13 -13
- xmos_ai_tools/runtime/buildfiles/aitoolslib.make +8 -8
- xmos_ai_tools/runtime/lib/host_xtflitemicro.lib +0 -0
- xmos_ai_tools/runtime/lib/libxtflitemicro.a +0 -0
- xmos_ai_tools/xformer/__init__.py +60 -60
- xmos_ai_tools/xformer/flash.py +190 -190
- xmos_ai_tools/xinterpreters/__init__.py +1 -1
- xmos_ai_tools/xinterpreters/exceptions.py +38 -38
- xmos_ai_tools/xinterpreters/host_interpreter.py +652 -652
- xmos_ai_tools/xinterpreters/libs/windows/xtflm_python.dll +0 -0
- {xmos_ai_tools-1.3.2.dev40.data → xmos_ai_tools-1.3.2.dev82.data}/data/Scripts/xcore-opt.exe +0 -0
- {xmos_ai_tools-1.3.2.dev40.dist-info → xmos_ai_tools-1.3.2.dev82.dist-info}/METADATA +3 -5
- {xmos_ai_tools-1.3.2.dev40.dist-info → xmos_ai_tools-1.3.2.dev82.dist-info}/RECORD +17 -17
- {xmos_ai_tools-1.3.2.dev40.dist-info → xmos_ai_tools-1.3.2.dev82.dist-info}/WHEEL +0 -0
- {xmos_ai_tools-1.3.2.dev40.dist-info → xmos_ai_tools-1.3.2.dev82.dist-info}/top_level.txt +0 -0
xmos_ai_tools/xformer/flash.py
CHANGED
@@ -1,190 +1,190 @@
|
|
1
|
-
BYTES_FOR_MAGIC_PATTERN = 32
|
2
|
-
BYTES_FOR_VERSION = 4
|
3
|
-
BYTES_PER_ENGINE_HEADER = 16
|
4
|
-
VERSION_MAJOR = 1
|
5
|
-
VERSION_MINOR = 2
|
6
|
-
|
7
|
-
|
8
|
-
class FlashBuilder:
|
9
|
-
class Header:
|
10
|
-
"""
|
11
|
-
Class that stores a header for a flash file system
|
12
|
-
The header comprises the addresses of the model, parameters, and operators
|
13
|
-
relative to the start address
|
14
|
-
"""
|
15
|
-
|
16
|
-
def __init__(self, model_bytes, params_bytes, ops_bytes, xip_bytes, start):
|
17
|
-
self.model_start = start
|
18
|
-
self.parameters_start = self.model_start + model_bytes + 4 # len
|
19
|
-
self.operators_start = self.parameters_start + params_bytes # no len
|
20
|
-
self.xip_start = self.operators_start + ops_bytes # no len
|
21
|
-
new_start = self.xip_start + xip_bytes # no len
|
22
|
-
self.length = new_start - start
|
23
|
-
|
24
|
-
def __init__(self, engines=1):
|
25
|
-
self.engines = engines
|
26
|
-
self.models = [bytes([])] * engines
|
27
|
-
self.params = [bytes([])] * engines
|
28
|
-
self.ops = [bytes([])] * engines
|
29
|
-
self.xips = [bytes([])] * engines
|
30
|
-
|
31
|
-
@staticmethod
|
32
|
-
def read_whole_binary_file(filename):
|
33
|
-
"""
|
34
|
-
Reads a whole binary file in and returns bytes(). If the file to be read is called '-'
|
35
|
-
then an empty bytes is returned.
|
36
|
-
"""
|
37
|
-
if filename == "-":
|
38
|
-
return bytes([])
|
39
|
-
try:
|
40
|
-
with open(filename, "rb") as input_fd:
|
41
|
-
contents = input_fd.read()
|
42
|
-
return contents
|
43
|
-
except:
|
44
|
-
print('File "%s" is not a readable file' % (filename))
|
45
|
-
return None
|
46
|
-
|
47
|
-
@staticmethod
|
48
|
-
def create_params_image(params=None, filename=None):
|
49
|
-
if params is None:
|
50
|
-
params = FlashBuilder.read_whole_binary_file(filename)
|
51
|
-
return params
|
52
|
-
|
53
|
-
@staticmethod
|
54
|
-
def create_model_image(model=None, filename=None):
|
55
|
-
if model is None:
|
56
|
-
model = FlashBuilder.read_whole_binary_file(filename)
|
57
|
-
return model
|
58
|
-
|
59
|
-
@staticmethod
|
60
|
-
def create_params_file(params_filename, params=None, input_filename=None):
|
61
|
-
image = FlashBuilder.create_params_image(params=params, filename=input_filename)
|
62
|
-
with open(params_filename, "wb") as output_fd:
|
63
|
-
output_fd.write(image)
|
64
|
-
|
65
|
-
@staticmethod
|
66
|
-
def tobytes(integr):
|
67
|
-
"""Converts an int to a LSB first quad of bytes"""
|
68
|
-
data = []
|
69
|
-
for i in range(4):
|
70
|
-
data.append((integr >> (8 * i)) & 0xFF)
|
71
|
-
return bytes(data)
|
72
|
-
|
73
|
-
@staticmethod
|
74
|
-
def swap_nibbles(x):
|
75
|
-
return (x & 0x0F) << 4 | (x & 0xF0) >> 4
|
76
|
-
|
77
|
-
def add_params(self, engine, params=None, filename=None):
|
78
|
-
image = FlashBuilder.create_params_image(params, filename)
|
79
|
-
self.params[engine] = image
|
80
|
-
|
81
|
-
def add_model(self, engine, model=None, filename=None):
|
82
|
-
image = FlashBuilder.create_model_image(model, filename)
|
83
|
-
self.models[engine] = image
|
84
|
-
|
85
|
-
def flash_image(self):
|
86
|
-
"""
|
87
|
-
Builds a flash image out of a collection of models and parameter blobs.
|
88
|
-
This function returns a bytes comprising the header, models, parameters, etc.
|
89
|
-
The whole thing should be written as is to flash
|
90
|
-
"""
|
91
|
-
headers = [None] * self.engines
|
92
|
-
start = (
|
93
|
-
BYTES_FOR_MAGIC_PATTERN
|
94
|
-
+ BYTES_FOR_VERSION
|
95
|
-
+ BYTES_PER_ENGINE_HEADER * self.engines
|
96
|
-
)
|
97
|
-
for i in range(self.engines):
|
98
|
-
headers[i] = FlashBuilder.Header(
|
99
|
-
len(self.models[i]),
|
100
|
-
len(self.params[i]),
|
101
|
-
len(self.ops[i]),
|
102
|
-
len(self.xips[i]),
|
103
|
-
start,
|
104
|
-
)
|
105
|
-
start += headers[i].length
|
106
|
-
|
107
|
-
# We add the magic fast flash pattern of 32 bytes at the very beginning
|
108
|
-
# After that comes the version
|
109
|
-
output = bytes(
|
110
|
-
[
|
111
|
-
0xFF,
|
112
|
-
0x00,
|
113
|
-
0x0F,
|
114
|
-
0x0F,
|
115
|
-
0x0F,
|
116
|
-
0x0F,
|
117
|
-
0x0F,
|
118
|
-
0x0F,
|
119
|
-
0xFF,
|
120
|
-
0x00,
|
121
|
-
0xFF,
|
122
|
-
0x00,
|
123
|
-
0xFF,
|
124
|
-
0x00,
|
125
|
-
0xFF,
|
126
|
-
0x00,
|
127
|
-
0x31,
|
128
|
-
0xF7,
|
129
|
-
0xCE,
|
130
|
-
0x08,
|
131
|
-
0x31,
|
132
|
-
0xF7,
|
133
|
-
0xCE,
|
134
|
-
0x08,
|
135
|
-
0x9C,
|
136
|
-
0x63,
|
137
|
-
0x9C,
|
138
|
-
0x63,
|
139
|
-
0x9C,
|
140
|
-
0x63,
|
141
|
-
0x9C,
|
142
|
-
0x63,
|
143
|
-
]
|
144
|
-
)
|
145
|
-
|
146
|
-
output += bytes(
|
147
|
-
[VERSION_MAJOR, VERSION_MINOR, 0xFF ^ VERSION_MAJOR, 0xFF ^ VERSION_MINOR]
|
148
|
-
)
|
149
|
-
|
150
|
-
for i in range(self.engines):
|
151
|
-
output += FlashBuilder.tobytes(
|
152
|
-
headers[i].model_start
|
153
|
-
) # encode start of model
|
154
|
-
output += FlashBuilder.tobytes(
|
155
|
-
headers[i].parameters_start
|
156
|
-
) # encode start of params
|
157
|
-
output += FlashBuilder.tobytes(
|
158
|
-
headers[i].operators_start
|
159
|
-
) # encode start of ops
|
160
|
-
output += FlashBuilder.tobytes(headers[i].xip_start) # encode start of xip
|
161
|
-
|
162
|
-
for i in range(self.engines):
|
163
|
-
output += FlashBuilder.tobytes(len(self.models[i])) # encode len of model
|
164
|
-
output += self.models[i] # add model image
|
165
|
-
output += self.params[i] # add params image
|
166
|
-
output += self.ops[i] # add operators image
|
167
|
-
output += self.xips[i] # add exec in place image
|
168
|
-
return output
|
169
|
-
|
170
|
-
def flash_file(self, filename):
|
171
|
-
"""
|
172
|
-
Builds a file for the host system that comprises a single parameter blob.
|
173
|
-
"""
|
174
|
-
output = self.flash_image()
|
175
|
-
# swap nibbles around
|
176
|
-
swapped_output = bytes([FlashBuilder.swap_nibbles(byte) for byte in output])
|
177
|
-
with open(filename, "wb") as output_fd:
|
178
|
-
output_fd.write(swapped_output)
|
179
|
-
|
180
|
-
|
181
|
-
def generate_flash(*, output_file, model_files, param_files):
|
182
|
-
assert len(model_files) == len(
|
183
|
-
param_files
|
184
|
-
), "The number of provided model files must match the number of param files!"
|
185
|
-
num_of_engines = len(model_files)
|
186
|
-
fb = FlashBuilder(engines=num_of_engines)
|
187
|
-
for i in range(num_of_engines):
|
188
|
-
fb.add_model(i, filename=model_files[i])
|
189
|
-
fb.add_params(i, filename=param_files[i])
|
190
|
-
fb.flash_file(output_file)
|
1
|
+
BYTES_FOR_MAGIC_PATTERN = 32
|
2
|
+
BYTES_FOR_VERSION = 4
|
3
|
+
BYTES_PER_ENGINE_HEADER = 16
|
4
|
+
VERSION_MAJOR = 1
|
5
|
+
VERSION_MINOR = 2
|
6
|
+
|
7
|
+
|
8
|
+
class FlashBuilder:
|
9
|
+
class Header:
|
10
|
+
"""
|
11
|
+
Class that stores a header for a flash file system
|
12
|
+
The header comprises the addresses of the model, parameters, and operators
|
13
|
+
relative to the start address
|
14
|
+
"""
|
15
|
+
|
16
|
+
def __init__(self, model_bytes, params_bytes, ops_bytes, xip_bytes, start):
|
17
|
+
self.model_start = start
|
18
|
+
self.parameters_start = self.model_start + model_bytes + 4 # len
|
19
|
+
self.operators_start = self.parameters_start + params_bytes # no len
|
20
|
+
self.xip_start = self.operators_start + ops_bytes # no len
|
21
|
+
new_start = self.xip_start + xip_bytes # no len
|
22
|
+
self.length = new_start - start
|
23
|
+
|
24
|
+
def __init__(self, engines=1):
|
25
|
+
self.engines = engines
|
26
|
+
self.models = [bytes([])] * engines
|
27
|
+
self.params = [bytes([])] * engines
|
28
|
+
self.ops = [bytes([])] * engines
|
29
|
+
self.xips = [bytes([])] * engines
|
30
|
+
|
31
|
+
@staticmethod
|
32
|
+
def read_whole_binary_file(filename):
|
33
|
+
"""
|
34
|
+
Reads a whole binary file in and returns bytes(). If the file to be read is called '-'
|
35
|
+
then an empty bytes is returned.
|
36
|
+
"""
|
37
|
+
if filename == "-":
|
38
|
+
return bytes([])
|
39
|
+
try:
|
40
|
+
with open(filename, "rb") as input_fd:
|
41
|
+
contents = input_fd.read()
|
42
|
+
return contents
|
43
|
+
except:
|
44
|
+
print('File "%s" is not a readable file' % (filename))
|
45
|
+
return None
|
46
|
+
|
47
|
+
@staticmethod
|
48
|
+
def create_params_image(params=None, filename=None):
|
49
|
+
if params is None:
|
50
|
+
params = FlashBuilder.read_whole_binary_file(filename)
|
51
|
+
return params
|
52
|
+
|
53
|
+
@staticmethod
|
54
|
+
def create_model_image(model=None, filename=None):
|
55
|
+
if model is None:
|
56
|
+
model = FlashBuilder.read_whole_binary_file(filename)
|
57
|
+
return model
|
58
|
+
|
59
|
+
@staticmethod
|
60
|
+
def create_params_file(params_filename, params=None, input_filename=None):
|
61
|
+
image = FlashBuilder.create_params_image(params=params, filename=input_filename)
|
62
|
+
with open(params_filename, "wb") as output_fd:
|
63
|
+
output_fd.write(image)
|
64
|
+
|
65
|
+
@staticmethod
|
66
|
+
def tobytes(integr):
|
67
|
+
"""Converts an int to a LSB first quad of bytes"""
|
68
|
+
data = []
|
69
|
+
for i in range(4):
|
70
|
+
data.append((integr >> (8 * i)) & 0xFF)
|
71
|
+
return bytes(data)
|
72
|
+
|
73
|
+
@staticmethod
|
74
|
+
def swap_nibbles(x):
|
75
|
+
return (x & 0x0F) << 4 | (x & 0xF0) >> 4
|
76
|
+
|
77
|
+
def add_params(self, engine, params=None, filename=None):
|
78
|
+
image = FlashBuilder.create_params_image(params, filename)
|
79
|
+
self.params[engine] = image
|
80
|
+
|
81
|
+
def add_model(self, engine, model=None, filename=None):
|
82
|
+
image = FlashBuilder.create_model_image(model, filename)
|
83
|
+
self.models[engine] = image
|
84
|
+
|
85
|
+
def flash_image(self):
|
86
|
+
"""
|
87
|
+
Builds a flash image out of a collection of models and parameter blobs.
|
88
|
+
This function returns a bytes comprising the header, models, parameters, etc.
|
89
|
+
The whole thing should be written as is to flash
|
90
|
+
"""
|
91
|
+
headers = [None] * self.engines
|
92
|
+
start = (
|
93
|
+
BYTES_FOR_MAGIC_PATTERN
|
94
|
+
+ BYTES_FOR_VERSION
|
95
|
+
+ BYTES_PER_ENGINE_HEADER * self.engines
|
96
|
+
)
|
97
|
+
for i in range(self.engines):
|
98
|
+
headers[i] = FlashBuilder.Header(
|
99
|
+
len(self.models[i]),
|
100
|
+
len(self.params[i]),
|
101
|
+
len(self.ops[i]),
|
102
|
+
len(self.xips[i]),
|
103
|
+
start,
|
104
|
+
)
|
105
|
+
start += headers[i].length
|
106
|
+
|
107
|
+
# We add the magic fast flash pattern of 32 bytes at the very beginning
|
108
|
+
# After that comes the version
|
109
|
+
output = bytes(
|
110
|
+
[
|
111
|
+
0xFF,
|
112
|
+
0x00,
|
113
|
+
0x0F,
|
114
|
+
0x0F,
|
115
|
+
0x0F,
|
116
|
+
0x0F,
|
117
|
+
0x0F,
|
118
|
+
0x0F,
|
119
|
+
0xFF,
|
120
|
+
0x00,
|
121
|
+
0xFF,
|
122
|
+
0x00,
|
123
|
+
0xFF,
|
124
|
+
0x00,
|
125
|
+
0xFF,
|
126
|
+
0x00,
|
127
|
+
0x31,
|
128
|
+
0xF7,
|
129
|
+
0xCE,
|
130
|
+
0x08,
|
131
|
+
0x31,
|
132
|
+
0xF7,
|
133
|
+
0xCE,
|
134
|
+
0x08,
|
135
|
+
0x9C,
|
136
|
+
0x63,
|
137
|
+
0x9C,
|
138
|
+
0x63,
|
139
|
+
0x9C,
|
140
|
+
0x63,
|
141
|
+
0x9C,
|
142
|
+
0x63,
|
143
|
+
]
|
144
|
+
)
|
145
|
+
|
146
|
+
output += bytes(
|
147
|
+
[VERSION_MAJOR, VERSION_MINOR, 0xFF ^ VERSION_MAJOR, 0xFF ^ VERSION_MINOR]
|
148
|
+
)
|
149
|
+
|
150
|
+
for i in range(self.engines):
|
151
|
+
output += FlashBuilder.tobytes(
|
152
|
+
headers[i].model_start
|
153
|
+
) # encode start of model
|
154
|
+
output += FlashBuilder.tobytes(
|
155
|
+
headers[i].parameters_start
|
156
|
+
) # encode start of params
|
157
|
+
output += FlashBuilder.tobytes(
|
158
|
+
headers[i].operators_start
|
159
|
+
) # encode start of ops
|
160
|
+
output += FlashBuilder.tobytes(headers[i].xip_start) # encode start of xip
|
161
|
+
|
162
|
+
for i in range(self.engines):
|
163
|
+
output += FlashBuilder.tobytes(len(self.models[i])) # encode len of model
|
164
|
+
output += self.models[i] # add model image
|
165
|
+
output += self.params[i] # add params image
|
166
|
+
output += self.ops[i] # add operators image
|
167
|
+
output += self.xips[i] # add exec in place image
|
168
|
+
return output
|
169
|
+
|
170
|
+
def flash_file(self, filename):
|
171
|
+
"""
|
172
|
+
Builds a file for the host system that comprises a single parameter blob.
|
173
|
+
"""
|
174
|
+
output = self.flash_image()
|
175
|
+
# swap nibbles around
|
176
|
+
swapped_output = bytes([FlashBuilder.swap_nibbles(byte) for byte in output])
|
177
|
+
with open(filename, "wb") as output_fd:
|
178
|
+
output_fd.write(swapped_output)
|
179
|
+
|
180
|
+
|
181
|
+
def generate_flash(*, output_file, model_files, param_files):
|
182
|
+
assert len(model_files) == len(
|
183
|
+
param_files
|
184
|
+
), "The number of provided model files must match the number of param files!"
|
185
|
+
num_of_engines = len(model_files)
|
186
|
+
fb = FlashBuilder(engines=num_of_engines)
|
187
|
+
for i in range(num_of_engines):
|
188
|
+
fb.add_model(i, filename=model_files[i])
|
189
|
+
fb.add_params(i, filename=param_files[i])
|
190
|
+
fb.flash_file(output_file)
|
@@ -1 +1 @@
|
|
1
|
-
from .host_interpreter import TFLMHostInterpreter
|
1
|
+
from .host_interpreter import TFLMHostInterpreter
|
@@ -1,38 +1,38 @@
|
|
1
|
-
# Copyright 2021 XMOS LIMITED. This Software is subject to the terms of the
|
2
|
-
# XMOS Public License: Version 1
|
3
|
-
|
4
|
-
|
5
|
-
class InterpreterError(Exception):
|
6
|
-
pass
|
7
|
-
|
8
|
-
|
9
|
-
class AllocateTensorsError(InterpreterError):
|
10
|
-
pass
|
11
|
-
|
12
|
-
|
13
|
-
class InvokeError(InterpreterError):
|
14
|
-
pass
|
15
|
-
|
16
|
-
|
17
|
-
class SetTensorError(InterpreterError):
|
18
|
-
pass
|
19
|
-
|
20
|
-
|
21
|
-
class GetTensorError(InterpreterError):
|
22
|
-
pass
|
23
|
-
|
24
|
-
|
25
|
-
class ModelSizeError(InterpreterError):
|
26
|
-
pass
|
27
|
-
|
28
|
-
|
29
|
-
class ArenaSizeError(InterpreterError):
|
30
|
-
pass
|
31
|
-
|
32
|
-
|
33
|
-
class DeviceTimeoutError(InterpreterError):
|
34
|
-
pass
|
35
|
-
|
36
|
-
|
37
|
-
class GetProfilerTimesError(InterpreterError):
|
38
|
-
pass
|
1
|
+
# Copyright 2021 XMOS LIMITED. This Software is subject to the terms of the
|
2
|
+
# XMOS Public License: Version 1
|
3
|
+
|
4
|
+
|
5
|
+
class InterpreterError(Exception):
|
6
|
+
pass
|
7
|
+
|
8
|
+
|
9
|
+
class AllocateTensorsError(InterpreterError):
|
10
|
+
pass
|
11
|
+
|
12
|
+
|
13
|
+
class InvokeError(InterpreterError):
|
14
|
+
pass
|
15
|
+
|
16
|
+
|
17
|
+
class SetTensorError(InterpreterError):
|
18
|
+
pass
|
19
|
+
|
20
|
+
|
21
|
+
class GetTensorError(InterpreterError):
|
22
|
+
pass
|
23
|
+
|
24
|
+
|
25
|
+
class ModelSizeError(InterpreterError):
|
26
|
+
pass
|
27
|
+
|
28
|
+
|
29
|
+
class ArenaSizeError(InterpreterError):
|
30
|
+
pass
|
31
|
+
|
32
|
+
|
33
|
+
class DeviceTimeoutError(InterpreterError):
|
34
|
+
pass
|
35
|
+
|
36
|
+
|
37
|
+
class GetProfilerTimesError(InterpreterError):
|
38
|
+
pass
|