-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch_tensorflow.py
More file actions
344 lines (249 loc) · 12.8 KB
/
Copy pathpatch_tensorflow.py
File metadata and controls
344 lines (249 loc) · 12.8 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
def should_patch_file(path: str) -> object:
if path.endswith('/micro/compatibility.h'):
return dict(func=process_compatibility_h)
if path.endswith('/micro/kernels/kernel_util.h'):
return dict(func=process_kernel_util_h, state=0)
if path.endswith('/micro/kernels/kernel_runner.h'):
return dict(func=process_kernel_runner_h, state=0)
if path.endswith('/micro/fake_micro_context.cc'):
return dict(func=process_fake_micro_context_cc, state=0)
if path.endswith('/micro/micro_allocator.cc'):
return dict(func=process_micro_allocator_cc, state=0)
if path.endswith('/micro/arena_allocator/single_arena_buffer_allocator.cc'):
return dict(func=process_single_arena_buffer_allocator_cc, state=0)
if path.endswith('/micro/memory_planner/greedy_memory_planner.cc'):
return dict(func=process_greedy_memory_planner_cc, state=0)
if path.endswith(('/micro/micro_interpreter.h', '/micro/micro_allocator.h')):
return dict(func=process_header_visibility, state=0)
if path.endswith('/kernels/kernel_util.cc'):
return dict(func=process_kernel_util_cc, state=0)
if path.endswith('/kernels/op_macros.h'):
return dict(func=process_op_macros_h, state=0)
if path.endswith('/c/builtin_op_data.h'):
return dict(func=process_builtin_op_data_h, state=0)
if path.endswith('/micro/kernels/conv_common.cc'):
return dict(func=process_conv_common_cc, state=0)
if path.endswith('/micro/kernels/depthwise_conv_common.cc'):
return dict(func=process_depthwise_conv_common_cc, state=0)
if path.endswith('/micro/kernels/fully_connected_common.cc'):
return dict(func=process_fully_connected_common_cc, state=0)
if path.endswith('/micro/kernels/pooling.h'):
return dict(func=process_pooling_h, state=0)
if path.endswith('/micro/micro_interpreter.cc'):
return dict(func=process_micro_interpreter_cc, state=0)
return None
def process_file_line(lineno: int, line: str, arg: object) -> str:
return arg['func'](lineno, line, arg)
def process_compatibility_h(lineno: int, line: str, arg: object) -> str:
if line.strip() == 'void operator delete(void* p) {}':
line = 'public: void operator delete(void* p) {} // Patched by YZLITE to ensure this operator is public\n'
return line
def process_kernel_util_h(lineno: int, line: str, arg: object) -> str:
if line.strip() == 'TFLITE_DCHECK(tensor != nullptr);':
line = ' // Patched by YZLITE\n'
line += ' // TFLITE_DCHECK(tensor != nullptr);\n'
line += ' if(tensor == nullptr){ return nullptr; }\n'
return line
def process_kernel_runner_h(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0 and 'Patched by the YZLITE' in line:
return None
if line.strip() == 'private:':
line = ' public: // private:\n'
if arg['state'] == 0 and 'static constexpr int kKernelRunnerBufferSize_ = 10000;' in line:
arg['state'] = 1
line = '// Patched by the YZLITE\n'
line += '#ifdef __arm__\n'
line += 'static constexpr int kKernelRunnerBufferSize_ = 32*1024;\n'
line += '#else \n'
line += 'static constexpr int kKernelRunnerBufferSize_ = 16*1024*1024;\n'
line += '#endif\n'
return line
def process_fake_micro_context_cc(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0 and 'IsAllTempTfLiteTensorDeallocated()' in line:
arg['state'] = 1
elif arg['state'] == 1:
arg['state'] = 2
if 'Patched by YZLITE' not in line:
line = ' // Patched by YZLITE\n'
line += ' return true; // !allocated_tensor_count_;\n'
return line
def process_micro_allocator_cc(lineno: int, line: str, arg: object) -> str:
if 'if (PopulateTfLiteTensorFromFlatbuffer(model, tensor, tensor_index,' in line:
if 'YZLITE' not in line:
return ' if(tensor == nullptr){ return nullptr; } if (PopulateTfLiteTensorFromFlatbuffer(model, tensor, tensor_index, // Patched by YZLITE\n'
if 'TfLiteTensor* tensor = AllocatePersistentTfLiteTensorInternal()' in line:
if 'YZLITE' not in line:
return " TfLiteTensor* tensor = AllocatePersistentTfLiteTensorInternal(); if(tensor == nullptr){ return nullptr; } // Patched by YZLITE"
if arg['state'] == 0 and 'void MicroAllocator::DeallocateTempTfLiteTensor(' in line:
arg['state'] = 1
elif arg['state'] == 1:
arg['state'] = 2
if 'Patched by YZLITE' not in line:
line = ' // Patched by YZLITE\n'
line += ' if(tensor == nullptr){ return; } // TFLITE_DCHECK(tensor != nullptr);\n'
return line
def process_single_arena_buffer_allocator_cc(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0 and 'SingleArenaBufferAllocator::IsAllTempDeallocated()' in line:
arg['state'] = 1
elif arg['state'] == 1:
arg['state'] = 2
if 'YZLITE' not in line:
return '// Patch by YZLITE\n return true;\n' + line
return line
def process_greedy_memory_planner_cc(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0 and line.strip() == '// Patched by YZLITE':
return None
if arg['state'] == 0 and 'namespace tflite' in line:
arg['state'] = 1
line = '// Patched by YZLITE\n'
line += 'bool yzlite_tflm_force_buffer_overlap = false;\n\n\n'
line += 'namespace tflite {\n'
return line
if arg['state'] == 1 and 'bool GreedyMemoryPlanner::DoesEntryOverlapInTime(' in line:
arg['state'] = 2
if arg['state'] == 2 and '{' in line:
arg['state'] = 3
line += '\n'
line += ' // Patched by YZLITE\n'
line += ' if(yzlite_tflm_force_buffer_overlap) return false;\n\n'
return line
def process_header_visibility(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0 and 'Patched by the YZLITE' in line:
return None
if arg['state'] == 0 and line.strip() == 'private:':
arg['state'] = 1
line = '// Patched by the YZLITE\n'
line += 'public:\n'
return line
def process_kernel_util_cc(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0 and 'const double output_scale = static_cast<double>(output->params.scale);' in line:
arg['state'] = 1
elif arg['state'] == 1 and '// Patched by YZLITE' in line:
arg['state'] = 2
elif arg['state'] == 1 and 'TF_LITE_ENSURE(context, scale_diff / output_scale <= 0.02)' in line:
arg['state'] = 2
line = ' // Patched by YZLITE\n'
line += ' // TF_LITE_ENSURE(context, scale_diff / output_scale <= 0.02);\n'
return line
def process_op_macros_h(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0 and 'Patched by the YZLITE' in line:
return None
if arg['state'] == 0 and line.strip() == '#endif // TENSORFLOW_LITE_KERNELS_OP_MACROS_H_':
arg['state'] = 1
line = '// Patched by the YZLITE\n'
line += '#include <assert.h>\n'
line += '#undef TFLITE_ABORT\n'
line += '#define TFLITE_ABORT assert(!"TF-Lite Micro assertion failed");\n'
line += '\n\n#endif // TENSORFLOW_LITE_KERNELS_OP_MACROS_H_\n'
return line
def process_builtin_op_data_h(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0 and '#endif // __cplusplus' in line:
arg['state'] = 1
elif arg['state'] == 1:
arg['state'] = 2
if '// Patched by YZLITE' not in line:
line = '// Patched by YZLITE\n'
line += '#ifndef __arm__\n'
line += '#pragma pack(push,4)\n'
line += '#define enum enum __attribute__((packed))\n'
line += '#endif\n\n'
elif arg['state'] == 2 and '#endif // __cplusplus' in line:
arg['state'] = 3
elif arg['state'] == 3:
arg['state'] = 4
if '// Patched by YZLITE' not in line:
line = '// Patched by YZLITE\n'
line += '#ifndef __arm__\n'
line += '#pragma pack(pop)\n'
line += '#undef enum\n'
line += '#endif\n\n'
return line
def process_conv_common_cc(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0:
if 'yzlite_tflite_micro_recorder.hpp' in line:
arg['state'] = 1
elif 'namespace tflite {' in line:
arg['state'] = 1
line = '// Patched by YZLITE\n#include "yzlite_tflite_micro_recorder.hpp"\n\n' + line
elif arg['state'] == 1:
if 'op_params.quantized_activation_max = data.output_activation_max;' in line:
arg['state'] = 2
elif arg['state'] == 2:
arg['state'] = 3
if 'TFLITE_MICRO_RECORD_CONV_PARAMS' not in line:
line = ' TFLITE_MICRO_RECORD_CONV_PARAMS(op_params, data.per_channel_output_multiplier, data.per_channel_output_shift, data.padding.height_offset); // Patched by YZLITE\n' + line
elif arg['state'] == 3:
if 'int output_channels = filter->dims->data[kConvQuantizedDimension];' in line:
arg['state'] = 4
elif arg['state'] == 4:
arg['state'] = 5
if '// Patched by YZLITE' not in line:
line = ' data->padding.height_offset = output_channels; // Patched by YZLITE, the height_offset member isnt used, so we hack it to store the number of channels\n' + line
return line
def process_depthwise_conv_common_cc(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0:
if 'yzlite_tflite_micro_recorder.hpp' in line:
arg['state'] = 1
elif 'namespace tflite {' in line:
arg['state'] = 1
line = '// Patched by YZLITE\n#include "yzlite_tflite_micro_recorder.hpp"\n\n' + line
elif arg['state'] == 1:
if 'op_params.quantized_activation_max = data.output_activation_max;' in line:
arg['state'] = 2
elif arg['state'] == 2:
arg['state'] = 3
if 'TFLITE_MICRO_RECORD_DEPTHWISE_CONV_PARAMS' not in line:
line = ' TFLITE_MICRO_RECORD_DEPTHWISE_CONV_PARAMS(op_params, data.per_channel_output_multiplier, data.per_channel_output_shift, data.padding.height_offset); // Patched by YZLITE\n' + line
elif arg['state'] == 3:
if 'int output_channels = filter->dims->data[kDepthwiseConvQuantizedDimension];' in line:
arg['state'] = 4
elif arg['state'] == 4:
arg['state'] = 5
if '// Patched by YZLITE' not in line:
line = ' data->padding.height_offset = output_channels; // Patched by YZLITE, the height_offset member isnt used, so we hack it to store the number of channels\n' + line
return line
def process_fully_connected_common_cc(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0:
if 'yzlite_tflite_micro_recorder.hpp' in line:
arg['state'] = 1
elif 'namespace tflite {' in line:
arg['state'] = 1
line = '// Patched by YZLITE\n#include "yzlite_tflite_micro_recorder.hpp"\n\n' + line
elif arg['state'] == 1:
if 'op_params.quantized_activation_max = op_data.output_activation_max;' in line:
arg['state'] = 2
elif arg['state'] == 2:
arg['state'] = 3
if 'TFLITE_MICRO_RECORD_FULLY_CONNECTED_PARAMS' not in line:
line = ' TFLITE_MICRO_RECORD_FULLY_CONNECTED_PARAMS(op_params); // Patched by YZLITE\n' + line
return line
def process_pooling_h(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0:
if 'yzlite_tflite_micro_recorder.hpp' in line:
arg['state'] = 1
elif 'namespace tflite {' in line:
arg['state'] = 1
line = '// Patched by YZLITE\n#include "yzlite_tflite_micro_recorder.hpp"\n\n' + line
elif arg['state'] == 1 or arg['state'] == 3:
if 'op_params.quantized_activation_max = data->activation_max;' in line:
arg['state'] += 1
elif arg['state'] == 2 or arg['state'] == 4:
arg['state'] += 1
if '// Patched by YZLITE' not in line:
line = '\n op_params.padding_type = tflite::micro::RuntimePaddingType(params->padding); // Patched by YZLITE\n TFLITE_MICRO_RECORD_POOL_PARAMS(op_params);\n' + line
return line
def process_micro_interpreter_cc(lineno: int, line: str, arg: object) -> str:
if arg['state'] == 0 and 'Patched by YZLITE' in line:
return None
if arg['state'] == 0 and 'TfLiteStatus MicroInterpreter::PrepareNodeAndRegistrationDataFromFlatbuffer() {' in line:
arg['state'] = 1
line = '// Patched by YZLITE\n'
line += '#if 0\n'
line += 'TfLiteStatus MicroInterpreter::PrepareNodeAndRegistrationDataFromFlatbuffer() {\n'
elif arg['state'] == 1 and 'return kTfLiteOk;' in line:
arg['state'] = 2
elif arg['state'] == 2 and '}' in line:
arg['state'] = 3
line = '}\n'
line += '#endif // Patched by YZLITE, if 0\n'
return line