forked from OAID/Tengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtengine_ir_pack.c
393 lines (293 loc) · 11.2 KB
/
tengine_ir_pack.c
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "tengine_ir.h"
#define ADDR_ALIGN(a) ((a + 3) & (~0x3))
struct packed_tensor
{
int32_t size;
uint16_t idx;
int16_t producer; /* node idx: -1 means no producer */
int16_t consumer[MAX_CONSUMER_NUM]; /* idx of node */
uint8_t consumer_num;
uint8_t tensor_type; /* const, input, var, dep */
uint8_t data_type; /* int8,uint8,fp32,fp16,int32 */
uint8_t dim_num;
uint8_t elem_size;
uint8_t layout;
int32_t elem_num;
int32_t data_size;
int32_t dims[MAX_SHAPE_DIM_NUM];
char data[0];
};
struct packed_op
{
int32_t size;
uint16_t op_type;
uint8_t op_version;
uint8_t same_shape;
uint16_t param_size;
char param_mem[0];
};
struct packed_node
{
int32_t size;
int32_t input_offset;
int32_t output_offset;
uint16_t idx;
uint8_t dynamic_shape;
uint8_t input_num;
uint8_t output_num;
uint8_t node_type;
struct packed_op op;
/* input > 2 */
/* output > 2 */
};
struct packed_ir_graph
{
int32_t size;
int32_t node_offset;
int32_t tensor_offset;
uint16_t tensor_num;
uint16_t node_num;
uint16_t input_num;
uint16_t output_num;
int8_t graph_layout;
int8_t model_layout;
int8_t model_format;
uint16_t node_io[0]; /* idx of inputs + outputs */
/* node */
/* tensor */
};
static struct packed_tensor* get_first_packed_tensor(struct packed_ir_graph* p_graph)
{
void* addr = p_graph;
return ( struct packed_tensor* )(addr + p_graph->tensor_offset);
}
static struct packed_tensor* get_next_packed_tensor(struct packed_tensor* p_tensor)
{
void* addr = p_tensor;
return ( struct packed_tensor* )(addr + p_tensor->size);
}
static struct packed_node* get_first_packed_node(struct packed_ir_graph* p_graph)
{
void* addr = p_graph;
return ( struct packed_node* )(addr + p_graph->node_offset);
}
static struct packed_node* get_next_packed_node(struct packed_node* p_node)
{
void* addr = p_node;
return ( struct packed_node* )(addr + p_node->size);
}
static struct packed_op* pack_ir_op(struct ir_op* op)
{
int packed_size = sizeof(struct packed_op) + op->param_size;
packed_size = ADDR_ALIGN(packed_size);
struct packed_op* packed = ( struct packed_op* )sys_malloc(packed_size);
packed->size = packed_size;
packed->op_type = op->op_type;
packed->op_version = op->op_version;
packed->same_shape = op->same_shape;
packed->param_size = op->param_size;
if (packed->param_size)
{
memcpy(packed->param_mem, op->param_mem, packed->param_size);
}
return packed;
}
static struct packed_node* pack_ir_node(struct ir_node* node)
{
struct packed_op* p_op = pack_ir_op(&node->op);
int node_size = p_op->size + sizeof(struct packed_node) - sizeof(struct packed_op);
if (node->input_num > 2)
node_size += sizeof(int16_t) * node->input_num;
if (node->output_num > 2)
node_size += sizeof(int16_t) * node->output_num;
node_size = ADDR_ALIGN(node_size);
struct packed_node* packed = ( struct packed_node* )sys_malloc(node_size);
packed->size = node_size;
packed->idx = node->idx;
packed->dynamic_shape = node->dynamic_shape;
packed->input_num = node->input_num;
packed->output_num = node->output_num;
packed->node_type = node->node_type;
memcpy(&packed->op, p_op, p_op->size);
int offset = p_op->size + sizeof(struct packed_node) - sizeof(struct packed_op);
if (node->input_num == 1)
packed->input_offset = node->input_tensors[0];
else
{
packed->input_offset = offset;
int16_t* ptr = ( int16_t* )(( void* )packed + offset);
for (int i = 0; i < node->input_num; i++)
ptr[i] = node->input_tensors[i];
offset += sizeof(int16_t) * node->input_num;
}
if (node->output_num == 1)
packed->output_offset = node->output_tensors[0];
else
{
packed->output_offset = offset;
int16_t* ptr = ( int16_t* )(( void* )packed + offset);
for (int i = 0; i < node->output_num; i++)
ptr[i] = node->output_tensors[i];
}
sys_free(p_op);
return packed;
}
static struct packed_tensor* pack_ir_tensor(struct ir_tensor* tensor)
{
int data_size;
if (tensor->data)
data_size = tensor->elem_size * tensor->elem_num;
else
data_size = 0;
int packed_size = data_size + sizeof(struct packed_tensor);
packed_size = ADDR_ALIGN(packed_size);
struct packed_tensor* packed = ( struct packed_tensor* )sys_malloc(packed_size);
packed->size = packed_size;
packed->idx = tensor->idx;
packed->tensor_type = tensor->tensor_type;
packed->data_type = tensor->data_type;
packed->dim_num = tensor->dim_num;
packed->layout = tensor->layout;
packed->data_size = data_size;
packed->elem_num = tensor->elem_num;
packed->elem_size = tensor->elem_size;
packed->consumer_num = tensor->consumer_num;
packed->producer = tensor->producer;
memcpy(packed->consumer, tensor->consumer, MAX_CONSUMER_NUM);
if (data_size)
memcpy(packed->data, tensor->data, data_size);
for (int i = 0; i < tensor->dim_num; i++)
packed->dims[i] = tensor->dims[i];
return packed;
}
int pack_ir_graph(struct ir_graph* ir_graph, void** mem, int* mem_size)
{
int packed_size = sizeof(struct packed_ir_graph);
struct packed_ir_graph* packed = ( struct packed_ir_graph* )sys_malloc(packed_size);
packed->size = packed_size;
packed->node_offset = 0;
packed->tensor_offset = 0;
packed->tensor_num = ir_graph->tensor_num;
packed->node_num = ir_graph->node_num;
packed->input_num = ir_graph->input_num;
packed->output_num = ir_graph->output_num;
packed->graph_layout = ir_graph->graph_layout;
packed->model_layout = ir_graph->model_layout;
packed->model_format = ir_graph->model_format;
/* packing input/out nodes */
packed_size += sizeof(uint16_t) * (ir_graph->input_num + ir_graph->output_num);
packed_size = ADDR_ALIGN(packed_size);
packed = ( struct packed_ir_graph* )sys_realloc(packed, packed_size);
for (int i = 0; i < ir_graph->input_num; i++)
packed->node_io[i] = ir_graph->input_nodes[i];
for (int i = 0; i < ir_graph->output_num; i++)
packed->node_io[i + ir_graph->input_num] = ir_graph->output_nodes[i];
packed->node_offset = packed_size;
/* packing nodes */
for (int i = 0; i < packed->node_num; i++)
{
struct ir_node* ir_node = get_ir_graph_node(ir_graph, i);
struct packed_node* packed_node = pack_ir_node(ir_node);
packed = sys_realloc(packed, packed_size + packed_node->size);
memcpy(( char* )packed + packed_size, packed_node, packed_node->size);
packed_size += packed_node->size;
sys_free(packed_node);
}
packed->tensor_offset = packed_size;
/*packing tensors*/
for (int i = 0; i < packed->tensor_num; i++)
{
struct ir_tensor* ir_tensor = get_ir_graph_tensor(ir_graph, i);
struct packed_tensor* packed_tensor = pack_ir_tensor(ir_tensor);
packed = sys_realloc(packed, packed_size + packed_tensor->size);
memcpy(( char* )packed + packed_size, packed_tensor, packed_tensor->size);
packed_size += packed_tensor->size;
sys_free(packed_tensor);
}
packed->size = packed_size;
*mem = packed;
*mem_size = packed_size;
return 0;
}
struct ir_graph* unpack_ir_graph(const void* mem, int mem_size)
{
struct packed_ir_graph* packed = ( struct packed_ir_graph* )mem;
if (packed->size != mem_size)
return NULL;
struct ir_graph* ir_graph = create_ir_graph(NULL);
ir_graph->input_num = packed->input_num;
ir_graph->output_num = packed->output_num;
ir_graph->graph_layout = packed->graph_layout;
ir_graph->model_layout = packed->model_layout;
ir_graph->model_format = packed->model_format;
ir_graph->input_nodes = ( int16_t* )sys_malloc(sizeof(int16_t) * ir_graph->input_num);
for (int i = 0; i < ir_graph->input_num; i++)
ir_graph->input_nodes[i] = packed->node_io[i];
ir_graph->output_nodes = ( int16_t* )sys_malloc(sizeof(int16_t) * ir_graph->output_num);
for (int i = 0; i < ir_graph->output_num; i++)
ir_graph->output_nodes[i] = packed->node_io[i + ir_graph->input_num];
struct packed_node* packed_node = NULL;
for (int i = 0; i < packed->node_num; i++)
{
if (packed_node)
packed_node = get_next_packed_node(packed_node);
else
packed_node = get_first_packed_node(packed);
struct ir_node* ir_node = create_ir_node(ir_graph, NULL, packed_node->op.op_type, packed_node->op.op_version);
if (packed_node->op.param_size)
{
/* todo: double check if packed_node->op.param_size == ir_node->op.param_size */
memcpy(ir_node->op.param_mem, packed_node->op.param_mem, packed_node->op.param_size);
}
ir_node->dynamic_shape = packed_node->dynamic_shape;
ir_node->input_num = packed_node->input_num;
ir_node->output_num = packed_node->output_num;
ir_node->node_type = packed_node->node_type;
ir_node->input_tensors = ( int16_t* )sys_malloc(sizeof(int16_t) * packed_node->input_num);
ir_node->output_tensors = ( int16_t* )sys_malloc(sizeof(int16_t) * packed_node->output_num);
if (packed_node->input_num == 1)
ir_node->input_tensors[0] = packed_node->input_offset;
else
{
int16_t* ptr = ( int16_t* )(( void* )packed_node + packed_node->input_offset);
for (int j = 0; j < packed_node->input_num; j++)
ir_node->input_tensors[j] = ptr[j];
}
if (packed_node->output_num == 1)
ir_node->output_tensors[0] = packed_node->output_offset;
else
{
int16_t* ptr = ( int16_t* )(( void* )packed_node + packed_node->output_offset);
for (int j = 0; j < packed_node->output_num; j++)
ir_node->output_tensors[j] = ptr[j];
}
}
struct packed_tensor* packed_tensor = NULL;
for (int i = 0; i < packed->tensor_num; i++)
{
if (packed_tensor)
packed_tensor = get_next_packed_tensor(packed_tensor);
else
packed_tensor = get_first_packed_tensor(packed);
struct ir_tensor* ir_tensor = create_ir_tensor(ir_graph, NULL, packed_tensor->data_type);
ir_tensor->producer = packed_tensor->producer;
ir_tensor->consumer_num = packed_tensor->consumer_num;
for (int j = 0; j < packed_tensor->consumer_num; j++)
ir_tensor->consumer[j] = packed_tensor->consumer[j];
ir_tensor->tensor_type = packed_tensor->tensor_type;
ir_tensor->data_type = packed_tensor->data_type;
ir_tensor->elem_size = packed_tensor->elem_size;
ir_tensor->elem_num = packed_tensor->elem_num;
ir_tensor->dim_num = packed_tensor->dim_num;
for (int j = 0; j < ir_tensor->dim_num; j++)
ir_tensor->dims[j] = packed_tensor->dims[j];
ir_tensor->free_host_mem = 1;
ir_tensor->internal_allocated = 0;
if (packed_tensor->data_size)
{
ir_tensor->data = sys_malloc(packed_tensor->data_size);
memcpy(ir_tensor->data, packed_tensor->data, packed_tensor->data_size);
}
}
return ir_graph;
}