forked from pulp-platform/dory
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.py
951 lines (923 loc) · 41.1 KB
/
template.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
#
# template.py
# Alessio Burrello <[email protected]>
# Thorir Mar Ingolfsson <[email protected]>
#
# Copyright (C) 2019-2020 University of Bologna
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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 math
from mako.template import Template
import re
from collections import OrderedDict
import numpy as np
import sys
import os
def print_file_list(x):
# This function is used to generate a string with all input files.
s = repr(x).replace("[", "").replace("]", "").replace("'", '"')
return s
def print_template_Makefile(file_list_w, platform, sdk):
# Generate the Makefile, including all files to upload on the hyperflash
tk = OrderedDict([])
tk['build_layers'] = os.listdir('./application/DORY_network/src/')
tk['layers_w'] = file_list_w
tk['platform'] = 'GAP8'
tk['sdk'] = sdk
root = '/'.join(os.getcwd().split('/')[:-1])
tmpl = Template(filename=root + "/templates/Makefile_template")
s = tmpl.render(**tk)
save_string = './application/Makefile'
with open(save_string, "w") as f:
f.write(s)
##CHANGED ALL H TO W
def print_template_layer_1D(x, y_gold, W,
n_in, w_in,
n_out, w_out,
tile_n_in, tile_w_in, tile_w_out,
tile_n_out,
ds_x, ds_y, ds_W, ds_act, type_data,
fs1, padding_left, padding_right,
stride, dilation,
relu, BN,
out_mul, out_shift,
name_layer='layer',
ultra_verbose=True,
test=False,
test_location='L3',
has_bias=True,
conv_order='CHW',
optional='conv',
l1_buffer=44000,
platform='GAP8',
chip='GAP8v2',
optional_type='8bit',
layer_type = 'normal'):
# Generate the Layer management c file.
if w_out * stride + fs1 - 1 - stride + 1 > w_in:
if (w_out * stride + fs1 - 1 - stride + 1 - w_in) == padding_left:
padding_r = 0
padding_l = padding_left
else:
padding_r = padding_right
padding_l = padding_left
# add padding from "regular" tile where necessary
tile_w_in = tile_w_in if w_in > tile_w_in else tile_w_in
name = re.sub(r'\W', '', name_layer).replace("hex", "").replace(".", "").replace("_weights", "")
name_layer = name + '.h'
conv_overlap1 = 2 * (((fs1-1)*dilation+1) // 2) + ((fs1-1)*dilation+1) % 2 - 1 - (stride - 1)
tk = OrderedDict([])
tk['optional_type'] = optional_type
tk['func_name'] = name
tk['layer_type'] = layer_type
tk['optional'] = optional
tk['FLAG_BATCHNORM'] = BN
tk['has_bias'] = has_bias
tk['FLAG_RELU'] = relu
tk['test_location'] = test_location
tk['platform'] = platform
tk['chip'] = chip
tk['dilation'] = dilation
tk['type'] = type_data
tk['nof'] = n_out
tk['nif'] = n_in
tk['conv_overlap1'] = conv_overlap1
tk['padding_left'] = padding_left
tk['padding_right'] = padding_right
tk['stride'] = stride
# x parameters
tk['x_h'] = w_in
tk['x_data_size_byte'] = ds_x
tk['x_tile_size_nif'] = tile_n_in
tk['x_tile_size_h'] = tile_w_in
tk['x_tile_size_byte'] = int(math.ceil(ds_x * tile_n_in * tile_w_in / 8.0))
tk['x_tile_size_nif_byte'] = int(math.ceil(tile_n_in * ds_x / 8.0))
tk['x_stride_c_byte'] = int(math.ceil(n_in * ds_x / 8.0))
# y parameters
tk['y_h'] = w_out
tk['y_data_size_byte'] = ds_y
tk['act_dim_bit'] = ds_act
tk['y_tile_size_nof'] = tile_n_out
tk['y_tile_size_h'] = tile_w_out
tk['y_tile_size_byte'] = int(math.ceil(tile_n_out * tile_w_out * ds_y / 8.0))
tk['y_stride_c_byte'] = int(math.ceil(n_out * ds_y / 8.0))
tk['y_tile_size_nof_byte'] = int(math.ceil(tile_n_out * ds_y / 8.0))
tk['tile_dim_h'] = max(int(math.ceil(float(w_out) / float(tile_w_out))), 1)
tk['tile_dim_nof'] = max(int(math.ceil(float(n_out) / float(tile_n_out))), 1)
tk['tile_dim_nif'] = max(int(math.ceil(float(n_in) / float(tile_n_in))), 1)
# W parameters
tk['fs1'] = fs1
tk['W_data_size_byte'] = ds_W
tk['W_tile_size_nof'] = tile_n_out
tk['b_size_byte'] = int(math.ceil(n_out * ds_W / 8.0))
tk['W_tile_size_nif'] = tile_n_in
tk['W_tile_size_byte'] = int(math.ceil(tile_n_out * tk['W_tile_size_nif'] * fs1 * ds_W / 8.0))
tk['W_stride_nof_byte'] = int(math.ceil(tk['nif'] * fs1 * ds_W / 8.0))
tk['W_stride_hw_byte'] = int(math.ceil(tk['nif'] * ds_W / 8.0))
tk['W_tile_nif_byte'] = int(math.ceil(tk['W_tile_size_nif'] * ds_W / 8.0))
# l2 parameters
if tk['FLAG_BATCHNORM'] == 1:
tk['l2_off_k'] = int(math.ceil(tk['nof'] * tk['nif'] * fs1 * ds_W / 8.0))
tk['l2_off_lambda'] = int(math.ceil((tk['nof'] * tk['nif'] * fs1 * ds_W + tk['nof'] * ds_act) / 8.0))
if has_bias == 1:
tk['l2_off_bias'] = 0
if n_in == tile_n_in and w_in == tile_w_in:
x_buffer_size = int(math.ceil(ds_x * tile_n_in * tile_w_in / 8.0))
else:
x_buffer_size = 2 * int(math.ceil(ds_x * tile_n_in * tile_w_in / 8.0))
if n_in == tile_n_in and w_in == tile_w_in and n_out == tile_n_out:
y_buffer_size = int(math.ceil(ds_y * tile_n_out * tile_w_out / 8.0))
W_buffer_size = int(math.ceil(ds_W * tile_n_out * tile_n_in * fs1 / 8.0))
else:
y_buffer_size = 2 * int(math.ceil(ds_y * tile_n_out * tile_w_out / 8.0))
W_buffer_size = 2 * int(math.ceil(ds_W * tile_n_out * tile_n_in * fs1 / 8.0))
if tk['FLAG_BATCHNORM'] == 1:
k_buffer_size = int(n_out * ds_act / 8.0)
lambd_buffer_size = int(n_out * ds_act / 8.0)
else:
k_buffer_size = 0
lambd_buffer_size = 0
if conv_order == 'PULP-NN-MAX' or conv_order == 'PULP-NN-ADD':
W_buffer_size = 0
# l1 parameters
tk['l1_x_offset'] = 0
tk['l1_y_offset'] = x_buffer_size + 4
if conv_order == 'PULP-NN-ADD':
tk['l1_x2_offset'] = x_buffer_size + 4 + y_buffer_size + 4
if conv_order != 'PULP-NN-MAX' and conv_order != 'PULP-NN-ADD':
tk['l1_W_offset'] = x_buffer_size + 4 + y_buffer_size + 4
if tk['FLAG_BATCHNORM'] == 1:
tk['l1_k_offset'] = x_buffer_size + 4 + y_buffer_size + 4 + W_buffer_size + 4
tk['l1_lambda_offset'] = x_buffer_size + 4 + y_buffer_size + 4 + W_buffer_size + 4 + k_buffer_size + 4
if has_bias == 1:
tk['l1_b_offset'] = x_buffer_size + 4 + y_buffer_size + 4 + W_buffer_size + 4 + k_buffer_size + 4 + lambd_buffer_size + 4
if conv_order != 'PULP-NN-MAX':
if tk['FLAG_BATCHNORM'] == 1:
tk['k_size_byte'] = k_buffer_size
tk['lambda_size_byte'] = k_buffer_size
tk['k_tile_size_byte'] = int(math.ceil(tile_n_out * ds_act / 8.0))
tk['lambda_tile_size_byte'] = int(math.ceil(tile_n_out * ds_act / 8.0))
if has_bias == 1:
tk['bias_tile_size_byte'] = tile_n_out
tk['b_size_byte'] = int(n_out)
# x last
tk['x_tile_size_nif_last'] = n_in % tile_n_in if (n_in % tile_n_in) > 0 else tile_n_in
tk['x_tile_size_nif_byte_last'] = int(math.ceil(tk['x_tile_size_nif_last'] * ds_x / 8.0))
if tk['tile_dim_h'] == 1:
tk['x_tile_size_h_last'] = tk['x_tile_size_h']
elif tk['tile_dim_h'] == 2:
tk['x_tile_size_h_last'] = w_in - tile_w_in + tk['conv_overlap1'] + padding_left
elif tk['tile_dim_h'] == 3:
tk['x_tile_size_h_last'] = w_in - tile_w_in - (tile_w_in - tk['conv_overlap1'] - padding_left) + tk['conv_overlap1']
else:
tk['x_tile_size_h_last'] = w_in - tile_w_in - (tile_w_in - tk['conv_overlap1'] - padding_left) - (tk['tile_dim_h'] - 3) * (tile_w_in - tk['conv_overlap1']) + tk['conv_overlap1']
if tk['x_tile_size_h_last'] > tk['x_tile_size_h']:
tk['x_tile_size_h_last'] = tk['x_tile_size_h']
# W last
if conv_order != 'PULP-NN-MAX' and conv_order != 'PULP-NN-ADD':
tk['W_tile_size_nof_last'] = n_out % tile_n_out if (n_out % tile_n_out) > 0 else tile_n_out
tk['W_tile_size_nif_last'] = tk['W_tile_size_nif']
tk['W_tile_size_nif_byte_last'] = int(math.ceil(tk['W_tile_size_nif_last'] * ds_W / 8.0))
# y last
tk['y_tile_size_nof_last'] = n_out % tile_n_out if (n_out % tile_n_out) > 0 else tile_n_out
tk['y_tile_size_h_last'] = w_out % tile_w_out if (w_out % tile_w_out) > 0 else tile_w_out
#tk['y_tile_size_nof_last'] = n_in
#tk['y_tile_size_h_last'] = w_in
tk['y_length_nof_byte_last'] = int(math.ceil(tk['y_tile_size_nof_last'] * ds_y / 8.0))
l = ""
for k, v in tk.items():
try:
l += "// %s %d\n" % (k.ljust(30), v)
except TypeError:
try:
l += "// %s %d\n" % (k.ljust(30), v[0])
except TypeError:
l += "// %s %s\n" % (k.ljust(30), v)
if conv_order == 'PULP-NN':
buffer_l1_all = W_buffer_size + x_buffer_size + y_buffer_size + k_buffer_size + lambd_buffer_size + 40
elif conv_order == 'PULP-NN-ADD':
buffer_l1_all = x_buffer_size * 2 + y_buffer_size + k_buffer_size + lambd_buffer_size + 40
elif conv_order == 'PULP-NN-MAX':
buffer_l1_all = x_buffer_size + y_buffer_size + k_buffer_size + lambd_buffer_size + 40
tk['buffer_l1_all'] = buffer_l1_all
l2_dim_input = (n_in) * tk['x_h']
l2_dim_output = (tk['nof']) * tk['y_h']
l2_dim_weights = tk['nof'] * tk['nif'] * tk['fs1']
l2_dim_k = k_buffer_size
l2_dim_lambda = lambd_buffer_size
if conv_order == 'PULP-NN':
root = '/'.join(os.getcwd().split('/')[:-1])
tmpl = Template(filename=root + "/templates/layer_templates/layer_template_conv_1D.c")
s = tmpl.render(TEST=test,VERBOSE=False,ULTRA_VERBOSE=ultra_verbose,PULP_TEST=True,verbose_log=l,**tk)
if 'L2' in test_location:
save_string = './application/DORY_network/src/' + name_layer.replace("h", "c")
elif 'L3' in test_location:
save_string = './application/DORY_network/src/' + name_layer.replace("h", "c")
with open(save_string, "w") as f:
f.write(s)
root = '/'.join(os.getcwd().split('/')[:-1])
tmpl = Template(filename=root + "/templates/layer_templates/layer_template_h.h")
s = tmpl.render(
TEST=test,
VERBOSE=False,
ULTRA_VERBOSE=ultra_verbose,
PULP_TEST=True,
verbose_log=l,
**tk)
if 'L2' in test_location:
save_string = './application/DORY_network/inc/' + name_layer
elif 'L3' in test_location:
save_string = './application/DORY_network/inc/' + name_layer
with open(save_string, "w") as f:
f.write(s)
if 'L2' in test_location:
tk['out_mul'] = out_mul
tk['out_shift'] = out_shift
tk['l1_buffer'] = l1_buffer
tk['activation_size_out'] = int(math.ceil(l2_dim_output * ds_y / 8.0))
tk['activation_size_in'] = int(math.ceil(l2_dim_input * ds_x / 8.0))
tk['x_content'] = print_test_vector(x, 'char')
tk['y_expected_content'] = print_test_vector(y_gold, 'char')
tk['check_sum'] = sum(y_gold)
tk['W_content'] = print_test_vector(W, 'char')
tk['buffer_l1_all'] = buffer_l1_all
tk['l2_dim_weights'] = int(math.ceil((l2_dim_weights) * ds_W / 8.0) + (l2_dim_k + l2_dim_lambda))
tk['h_out'] = tk['y_h']
tk['ultra_test'] = True
root = '/'.join(os.getcwd().split('/')[:-1])
tmpl = Template(filename=root+"/templates/test_templateL2.c")
s = tmpl.render(
TEST=test,
VERBOSE=False,
ULTRA_VERBOSE=ultra_verbose,
PULP_TEST=True,
verbose_log=l,
**tk)
save_string = './application/DORY_network/src/main.c'
with open(save_string, "w") as f:
f.write(s)
tk['build_layers'] = os.listdir('./application/DORY_network/src/')
tk['platform'] = 'GAP8'
tmpl = Template(filename=root+"/templates/Makefile_template_L2")
s = tmpl.render(**tk)
save_string = './application/Makefile'
with open(save_string, "w") as f:
f.write(s)
return l2_dim_input, l2_dim_output, l2_dim_weights, l2_dim_k, l2_dim_lambda, tk['nof'], buffer_l1_all, n_out, w_out
def print_template_network(
file_list_w,
PULP_Nodes_Graph,
type_data,
name,
test = True,
has_bias = True,
verbose_level = 'None',
performance_single_layer = 'Yes',
check_layer = 0,
act_compare = 0,
act_size = [0, 0, 0],
class_out = 0,
l1_buffer = 35000,
master_stack = 4096,
slave_stack = 3072,
l2_buffer_size = 400000,
fc_frequency = 100000000,
cl_frequency = 100000000,
MACs = 1,
platform = 'GAP8',
BitIn = 8,
BitW = 8,
BitOut = 8,
sdk = 'gap_sdk',
dma_parallelization = '8-cores',
optional_type = 'conv'
):
# Generate the Network management c file.
tk = OrderedDict([])
if 'Check' in verbose_level:
tk['verbose'] = True
else:
tk['verbose'] = False
i_conv = []
i = 0
for ind, nodes in enumerate(PULP_Nodes_Graph[:-1]):
if ('Gemm' in PULP_Nodes_Graph[ind + 1].name or 'Conv' in PULP_Nodes_Graph[ind + 1].name):
i += 1
i_conv.append(i)
else:
i_conv.append(i)
weights_number = 0
for nodes in PULP_Nodes_Graph:
if 'Gemm' in nodes.name or 'Conv' in nodes.name or 'MatMul' in nodes.name:
weights_number += 1
tk['dma_parallelization'] = dma_parallelization
tk['BitIn'] = BitIn
tk['BitW'] = BitW
tk['BitOut'] = BitOut
tk['weights_number'] = weights_number
tk['i_conv'] = i_conv
tk['verbose_level'] = verbose_level
tk['performance'] = performance_single_layer
tk['l1_buffer'] = l1_buffer
tk['master_stack'] = master_stack
tk['slave_stack'] = slave_stack
tk['l2_buffer_size'] = l2_buffer_size
tk['MACs'] = MACs
tk['files_list'] = print_file_list(file_list_w)
tk['test'] = test
tk['check_layer'] = check_layer
tk['act_size'] = act_size
tk['nof_check'] = act_size[2]
tk['h_out_check'] = act_size[0]
tk['w_out_check'] = act_size[1]
tk['class_out'] = class_out
tk['platform'] = platform
tk['fc_frequency'] = fc_frequency
tk['cl_frequency'] = cl_frequency
tk['sdk'] = sdk
tk['act_compare'] = print_test_vector(act_compare, 'char')
list_h = []
for i, _ in enumerate(name):
list_h.append(name[i] + '.h')
list_h = list(set(list_h))
tk['list_h'] = list_h
list_name = []
for i, _ in enumerate(name):
list_name.append(name[i])
tk['func_name'] = list_name
tk['has_bias'] = has_bias
tk['type'] = type_data
l = ""
for k, v in tk.items():
try:
l += "// %s %d\n" % (k.ljust(30), v)
except TypeError:
try:
l += "// %s %d\n" % (k.ljust(30), v[0])
except TypeError:
l += "// %s %s\n" % (k.ljust(30), v)
root = '/'.join(os.getcwd().split('/')[:-1])
if(optional_type == '1D_Conv'):
tmpl = Template(filename=root + "/templates/network_template_1D.c")
else:
tmpl = Template(filename=root + "/templates/network_template_dronet.c")
tk['PULP_Nodes_Graph'] = PULP_Nodes_Graph
s = tmpl.render(verbose_log=l,**tk)
save_string = './application/DORY_network/src/network.c'
with open(save_string, "w") as f:
f.write(s)
def print_pool_template_layer_L3(X, W, Y, fs1, fs2, padding, stride,
factor_ch_out,
factor_h_out,
factor_h_in,
name,
out_dim1,
in_dim1,
in_dim_full,
w_out,
h_out,
n_out,
w_in,
h_in,
n_in,
full_net,
platform,
data_type_x,
data_type_y,
test_location,
buffer_l1_all,
input_L3
):
# generation of L3 layers. The layers are generated with this infrustructure if an L3 tiling is demanded.
tk = OrderedDict([])
conv_overlap1 = 2 * (fs1 // 2) + fs1 % 2 - 1 - (stride - 1)
conv_overlap2 = 2 * (fs2 // 2) + fs2 % 2 - 1 - (stride - 1)
tk['conv_overlap1'] = conv_overlap1
tk['conv_overlap2'] = conv_overlap2
tk['padding'] = padding
tk['input_L3'] = input_L3
tk['n_tile_W'] = int(factor_ch_out)
tk['n_tile_x'] = int(factor_h_in)
tk['n_tile_y'] = int(factor_h_out)
tk['verbose'] = False
tk['func_name'] = name
tk['func_name_L3'] = name[0] + 'L3'
tk['act_out_dim_partial'] = int(out_dim1)
tk['w_out'] = w_out
tk['h_out'] = h_out
tk['n_out'] = n_out
tk['w_in'] = w_in
tk['h_in'] = h_in
tk['n_in'] = n_in
tk['dim_out'] = out_dim1
tk['dim_in'] = in_dim1
tk['platform'] = platform
tk['y_data_size_byte'] = data_type_y
tk['x_data_size_byte'] = data_type_x
root = '/'.join(os.getcwd().split('/')[:-1])
tmpl = Template(filename=root + "/templates/layer_templates/layer_template_L3.c")
l = ""
s = tmpl.render(verbose_log=l,**tk)
#
save_string = './application/DORY_network/src/' + tk['func_name_L3'] + '.c'
with open(save_string, "w") as f: f.write(s)
tmpl = Template(filename=root + "/templates/layer_templates/layer_template_L3-h.h")
s = tmpl.render(verbose_log=l, **tk)
if full_net == 1:
save_string = './application/DORY_network/inc/' + \
tk['func_name_L3'] + '.h'
else:
save_string = './applicationL3/DORY_network/inc/' + \
tk['func_name_L3'] + '.h'
with open(save_string, "w") as f:
f.write(s)
if 'partial' in test_location:
string_layer = "inputs.hex"
save_s = './application/DORY_network/' + string_layer
with open(save_s, 'wb') as f:
for i in X.astype('uint8').flatten():
f.write(bytes((i,)))
tk['x_content'] = print_test_vector(X, 'char')
if tk['n_tile_W'] == 1:
tk['W_content'] = print_test_vector(W, 'char')
tk['weight_dim'] = W.shape[0]
tk['check_sum'] = sum(Y)
tk['activation_size_out'] = out_dim1
tk['activation_size_in'] = in_dim1
tk['activation_size_in_full'] = in_dim_full
tk['func_nameL3'] = tk['func_name_L3']
tk['file'] = name[0][5:] + '_weights.hex'
tk['buffer_l1_all'] = buffer_l1_all
tmpl = Template(filename=root + "/templates/test_templateL3.c")
s = tmpl.render(**tk)
save_string = './application/DORY_network/src/main.c'
with open(save_string, "w") as f: f.write(s)
def print_template_layer_L3(X, W, Y, fs1, fs2, padding, stride,
factor_ch_out,
factor_h_out,
factor_h_in,
name,
out_dim1,
in_dim1,
in_dim_full,
weight_dim1,
lambda_dim,
k_dim,
w_out,
h_out,
n_out,
w_in,
h_in,
n_in,
full_net,
platform,
data_type_x,
data_type_y,
test_location,
out_mul, out_shift,
buffer_l1_all,
input_L3
):
# generation of L3 layers. The layers are generated with this infrustructure if an L3 tiling is demanded.
tk = OrderedDict([])
conv_overlap1 = 2 * (fs1 // 2) + fs1 % 2 - 1 - (stride - 1)
conv_overlap2 = 2 * (fs2 // 2) + fs2 % 2 - 1 - (stride - 1)
tk['conv_overlap1'] = conv_overlap1
tk['conv_overlap2'] = conv_overlap2
tk['padding'] = padding
tk['input_L3'] = input_L3
tk['n_tile_W'] = int(factor_ch_out)
tk['n_tile_x'] = int(factor_h_in)
tk['n_tile_y'] = int(factor_h_out)
tk['verbose'] = False
tk['func_name'] = name
tk['func_name_L3'] = name[0] + 'L3'
tk['act_out_dim_partial'] = int(out_dim1)
tk['weight_dim'] = int(weight_dim1)
tk['lambda_dim'] = lambda_dim
tk['k_dim'] = k_dim
tk['w_out'] = w_out
tk['h_out'] = h_out
tk['n_out'] = n_out
tk['w_in'] = w_in
tk['h_in'] = h_in
tk['n_in'] = n_in
tk['dim_out'] = out_dim1
tk['dim_in'] = in_dim1
tk['platform'] = platform
tk['y_data_size_byte'] = data_type_y
tk['x_data_size_byte'] = data_type_x
root = '/'.join(os.getcwd().split('/')[:-1])
tmpl = Template(filename=root + "/templates/layer_templates/layer_template_L3.c")
l = ""
s = tmpl.render(verbose_log=l,**tk)
#
save_string = './application/DORY_network/src/' + tk['func_name_L3'] + '.c'
with open(save_string, "w") as f: f.write(s)
tmpl = Template(filename=root + "/templates/layer_templates/layer_template_L3-h.h")
s = tmpl.render(verbose_log=l, **tk)
if full_net == 1:
save_string = './application/DORY_network/inc/' + \
tk['func_name_L3'] + '.h'
else:
save_string = './applicationL3/DORY_network/inc/' + \
tk['func_name_L3'] + '.h'
with open(save_string, "w") as f:
f.write(s)
if 'partial' in test_location:
string_layer = "inputs.hex"
save_s = './application/DORY_network/' + string_layer
with open(save_s, 'wb') as f:
for i in X.astype('uint8').flatten():
f.write(bytes((i,)))
tk['x_content'] = print_test_vector(X, 'char')
if tk['n_tile_W'] == 1:
tk['W_content'] = print_test_vector(W, 'char')
tk['weight_dim'] = W.shape[0]
tk['check_sum'] = sum(Y)
tk['activation_size_out'] = out_dim1
tk['activation_size_in'] = in_dim1
tk['activation_size_in_full'] = in_dim_full
tk['out_mul'] = out_mul
tk['out_shift'] = out_shift
tk['func_nameL3'] = tk['func_name_L3']
tk['file'] = name[0][5:] + '_weights.hex'
tk['buffer_l1_all'] = buffer_l1_all
tmpl = Template(filename=root + "/templates/test_templateL3.c")
s = tmpl.render(**tk)
save_string = './application/DORY_network/src/main.c'
with open(save_string, "w") as f: f.write(s)
def print_template_layer(x, y_gold, W,
n_in, h_in, w_in,
n_out,h_out, w_out,
tile_n_in, tile_h_in, tile_w_in, tile_h_out, tile_w_out,
tile_n_out,
ds_x, ds_y, ds_W, ds_act, type_data,
fs1, fs2, padding_top, padding_bottom, padding_left, padding_right, stride,
relu, BN, DW,
out_mul, out_mul2, out_shift, factor_ch_out, factor_h_out, factor_h_in,
name_layer='layer',
ultra_verbose=True,
test=False,
test_location='L3',
has_bias=True,
conv_order='CHW',
optional='conv',
l1_buffer=44000,
platform='GAP8',
chip='GAP8v2',
optional_type='8bit',
L3_tiling = 0,
sdk = 'gap_sdk',
dma_parallelization = '8-cores'
):
# Generate the Layer management c file.
if h_out * stride + fs1 - 1 - stride + 1 > h_in:
if (h_out * stride + fs1 - 1 - stride + 1 - h_in) == padding_top:
padding_b = 0
padding_t = padding_top
else:
padding_b = padding_bottom
padding_t = padding_top
if w_out * stride + fs2 - 1 - stride + 1 > w_in:
if (w_out * stride + fs2 - 1 - stride + 1 - w_in) == padding_left:
padding_r = 0
padding_l = padding_left
else:
padding_r = padding_right
padding_l = padding_left
# add padding from "regular" tile where necessary
tile_h_in = tile_h_in if h_in > tile_h_in else tile_h_in
tile_w_in = tile_w_in if w_in > tile_w_in else tile_w_in
if w_in > tile_w_in:
tile_w_out = int((tile_w_in - (fs2 - 1) + (stride - 1)) / stride)
else:
tile_w_out = int((tile_w_in + (padding_left + padding_right) - (fs2 - 1) + (stride - 1)) / stride)
name = re.sub(r'\W', '', name_layer).replace("hex", "").replace(".", "").replace("_weights", "")
name_layer = name + '.h'
conv_overlap1 = 2 * (fs1 // 2) + fs1 % 2 - 1 - (stride - 1)
conv_overlap2 = 2 * (fs2 // 2) + fs2 % 2 - 1 - (stride - 1)
tk = OrderedDict([])
tk['sdk'] = sdk
tk['dma_parallelization'] = dma_parallelization
tk['optional_type'] = optional_type
tk['func_name'] = name
tk['flag_DW'] = DW
tk['optional'] = optional
tk['FLAG_BATCHNORM'] = BN
tk['has_bias'] = has_bias
tk['FLAG_RELU'] = relu
tk['test_location'] = test_location
tk['platform'] = platform
if DW == 1:
tk['chip'] = 'GAPv2'
else:
tk['chip'] = chip
tk['type'] = type_data
tk['nof'] = n_out
tk['factor'] = factor_ch_out
if DW == 0:
tk['g'] = 1
else:
tk['g'] = n_in
if DW == 0:
tk['nif'] = n_in
else:
tk['nif'] = 1
tk['conv_overlap1'] = conv_overlap1
tk['conv_overlap2'] = conv_overlap2
tk['padding_top'] = padding_top
tk['padding_bottom'] = padding_bottom
tk['padding_left'] = padding_left
tk['padding_right'] = padding_right
tk['stride'] = stride
# x parameters
tk['x_h'] = h_in
tk['x_w'] = w_in
tk['x_data_size_byte'] = ds_x
tk['x_tile_size_nif'] = tile_n_in
tk['x_tile_size_h'] = tile_h_in
tk['x_tile_size_w'] = tile_w_in
tk['x_tile_size_byte'] = int(math.ceil(ds_x * tile_n_in * tile_h_in * tile_w_in / 8.0))
tk['x_tile_size_nif_byte'] = int(math.ceil(tile_n_in * ds_x / 8.0))
tk['x_stride_w_byte'] = int(math.ceil(w_in * n_in * ds_x / 8.0))
tk['x_stride_c_byte'] = int(math.ceil(n_in * ds_x / 8.0))
# y parameters
tk['y_h'] = h_out
tk['y_w'] = w_out
tk['y_data_size_byte'] = ds_y
tk['act_dim_bit'] = ds_act
tk['y_tile_size_nof'] = tile_n_out if (n_out > tile_n_out) else n_out
tk['y_tile_size_h'] = tile_h_out if (h_out > tile_h_out) > 0 else h_out
tk['y_tile_size_w'] = tile_w_out if (w_out > tile_w_out) > 0 else w_out
tk['y_tile_size_byte'] = int(math.ceil(tk['y_tile_size_nof'] * tk['y_tile_size_h'] * tk['y_tile_size_w'] * ds_y / 8.0))
tk['y_stride_w_byte'] = int(math.ceil(w_out * n_out * factor_ch_out * ds_y / 8.0))
tk['y_stride_c_byte'] = int(math.ceil(n_out * factor_ch_out * ds_y / 8.0))
tk['y_tile_size_nof_byte'] = int(math.ceil(tile_n_out * ds_y / 8.0))
tk['tile_dim_h'] = max(int(math.ceil(float(h_out) / float(tk['y_tile_size_h']))), 1)
tk['tile_dim_w'] = max(int(math.ceil(float(w_out) / float(tk['y_tile_size_w']))), 1)
tk['tile_dim_nof'] = max(int(math.ceil(float(n_out) / float(tk['y_tile_size_nof']))), 1)
tk['tile_dim_nif'] = max(int(math.ceil(float(n_in) / float(tile_n_in))), 1)
# W parameters
tk['fs1'] = fs1
tk['fs2'] = fs2
tk['W_data_size_byte'] = ds_W
if DW == 0:
tk['W_tile_size_nof'] = tile_n_out
else:
tk['W_tile_size_nof'] = int(tile_n_out * ds_W / 8.0)
if tk['has_bias'] == 1:
tk['b_size_byte'] = int(math.ceil(n_out * ds_W / 8.0))
else:
tk['b_size_byte'] = 0
if DW == 0:
tk['W_tile_size_nif'] = tile_n_in
else:
tk['W_tile_size_nif'] = 1
tk['W_tile_size_byte'] = int(math.ceil(tile_n_out * tk['W_tile_size_nif'] * fs1 * fs2 * ds_W / 8.0))
if DW == 0:
tk['W_stride_nof_byte'] = int(math.ceil(tk['nif'] * fs1 * fs2 * ds_W / 8.0))
else:
tk['W_stride_nof_byte'] = int(math.ceil(tk['nif'] * fs1 * fs2))
tk['W_stride_hw_byte'] = int(math.ceil(tk['nif'] * ds_W / 8.0))
tk['W_tile_nif_byte'] = int(math.ceil(tk['W_tile_size_nif'] * ds_W / 8.0))
# l2 parameters
if tk['FLAG_BATCHNORM'] == 1:
tk['l2_off_k'] = int(
math.ceil(tk['nof'] * tk['nif'] * fs1 * fs2 * ds_W / 8.0 + tk['b_size_byte']))
tk['l2_off_lambda'] = int(
math.ceil((tk['nof'] * tk['nif'] * fs1 * fs2 * ds_W + tk['nof'] * ds_act) / 8.0 + tk['b_size_byte']))
if has_bias == 1:
tk['l2_off_bias'] = int(math.ceil(tk['nof'] * tk['nif'] * fs1 * fs2 * ds_W / 8.0 ))
if n_in == tile_n_in and w_in == tile_w_in and h_in == tile_h_in:
x_buffer_size = int(math.ceil(ds_x * tile_n_in * tile_h_in * tile_w_in / 8.0))
else:
x_buffer_size = 2 * int(math.ceil(ds_x * tile_n_in * tile_h_in * tile_w_in / 8.0))
if n_in == tile_n_in and w_in == tile_w_in and h_in == tile_h_in and n_out == tile_n_out:
y_buffer_size = int(math.ceil(ds_y * tk['y_tile_size_nof'] * tk['y_tile_size_h'] * tk['y_tile_size_w'] / 8.0))
if DW == 0:
W_buffer_size = int(math.ceil(ds_W * tk['y_tile_size_nof'] * tile_n_in * fs1 * fs2 / 8.0))
else:
W_buffer_size = int(math.ceil(ds_W * tk['y_tile_size_nof'] * 1 * fs1 * fs2 / 8.0))
else:
y_buffer_size = 2 * int(math.ceil(ds_y * tk['y_tile_size_nof'] * tk['y_tile_size_h'] * tk['y_tile_size_w'] / 8.0))
if DW == 0:
W_buffer_size = 2 * int(math.ceil(ds_W * tk['y_tile_size_nof'] * tile_n_in * fs1 * fs2 / 8.0))
else:
W_buffer_size = 2 * int(math.ceil(ds_W * tk['y_tile_size_nof'] * 1 * fs1 * fs2 / 8.0))
if tk['FLAG_BATCHNORM'] == 1:
k_buffer_size = int(n_out * ds_act / 8.0)
lambd_buffer_size = int(n_out * ds_act / 8.0)
else:
k_buffer_size = 0
lambd_buffer_size = 0
tk['k_tile_size_byte'] = 0
tk['lambda_tile_size_byte'] = 0
tk['k_size_byte'] = 0
tk['lambda_size_byte'] = 0
if conv_order != 'PULP-NN-MAX':
if tk['FLAG_BATCHNORM'] == 1:
tk['k_size_byte'] = k_buffer_size
tk['lambda_size_byte'] = k_buffer_size
tk['k_tile_size_byte_transfer'] = int(math.ceil(tile_n_out * ds_act / 8.0))
tk['lambda_tile_size_byte_transfer'] = int(math.ceil(tile_n_out * ds_act / 8.0))
if n_in == tile_n_in and w_in == tile_w_in and h_in == tile_h_in and n_out == tile_n_out:
tk['k_tile_size_byte'] = int(math.ceil(tile_n_out * ds_act / 8.0))
tk['lambda_tile_size_byte'] = int(math.ceil(tile_n_out * ds_act / 8.0))
else:
tk['k_tile_size_byte'] = int(math.ceil(tile_n_out * ds_act / 8.0 * 2))
tk['lambda_tile_size_byte'] = int(math.ceil(tile_n_out * ds_act / 8.0 * 2))
if has_bias == 1:
tk['bias_tile_size_byte'] = tile_n_out
tk['b_size_byte'] = int(n_out)
else:
tk['bias_tile_size_byte'] = 0
tk['b_size_byte'] = 0
if conv_order == 'PULP-NN-MAX' or conv_order == 'PULP-NN-ADD':
W_buffer_size = 0
# l1 parameters
tk['l1_x_offset'] = 0
tk['l1_y_offset'] = x_buffer_size + 4
if conv_order == 'PULP-NN-ADD':
tk['l1_x2_offset'] = x_buffer_size + 4 + y_buffer_size + 4
if conv_order != 'PULP-NN-MAX' and conv_order != 'PULP-NN-ADD':
tk['l1_W_offset'] = x_buffer_size + 4 + y_buffer_size + 4
if tk['FLAG_BATCHNORM'] == 1:
tk['l1_k_offset'] = x_buffer_size + 4 + y_buffer_size + 4 + W_buffer_size + 4
tk['l1_lambda_offset'] = x_buffer_size + 4 + y_buffer_size + 4 + W_buffer_size + 4 + tk['k_tile_size_byte'] + 4
if has_bias == 1:
tk['l1_b_offset'] = x_buffer_size + 4 + y_buffer_size + 4 + W_buffer_size + 4 + tk['k_tile_size_byte'] + 4 + tk['lambda_tile_size_byte'] + 4
# x last
tk['x_tile_size_nif_last'] = n_in % tile_n_in if (n_in % tile_n_in) > 0 else tile_n_in
tk['x_tile_size_nif_byte_last'] = int(math.ceil(tk['x_tile_size_nif_last'] * ds_x / 8.0))
if tk['tile_dim_h'] == 1:
tk['x_tile_size_h_last'] = tk['x_tile_size_h']
elif tk['tile_dim_h'] == 2:
tk['x_tile_size_h_last'] = h_in - tile_h_in + tk['conv_overlap1'] + padding_top
elif tk['tile_dim_h'] == 3:
tk['x_tile_size_h_last'] = h_in - tile_h_in - (tile_h_in - tk['conv_overlap1'] - padding_top) + tk['conv_overlap1']
else:
tk['x_tile_size_h_last'] = h_in - tile_h_in - (tile_h_in - tk['conv_overlap1'] - padding_top) - (tk['tile_dim_h'] - 3) * (tile_h_in - tk['conv_overlap1']) + tk['conv_overlap1']
if tk['tile_dim_w'] == 1:
tk['x_tile_size_w_last'] = tk['x_tile_size_w']
elif tk['tile_dim_w'] == 2:
tk['x_tile_size_w_last'] = w_in - tile_w_in + tk['conv_overlap2'] + padding_left
elif tk['tile_dim_w'] == 3:
tk['x_tile_size_w_last'] = w_in - tile_w_in - (tile_w_in - tk['conv_overlap2'] - padding_left) + tk['conv_overlap2']
else:
tk['x_tile_size_w_last'] = w_in - tile_w_in - (tile_w_in - tk['conv_overlap2'] - padding_left) - (tk['tile_dim_w'] - 3) * (tile_w_in - tk['conv_overlap2']) + tk['conv_overlap2']
if tk['x_tile_size_h_last'] > tk['x_tile_size_h']:
tk['x_tile_size_h_last'] = tk['x_tile_size_h']
if tk['x_tile_size_w_last'] > tk['x_tile_size_w']:
tk['x_tile_size_w_last'] = tk['x_tile_size_w']
# W last
if conv_order != 'PULP-NN-MAX' and conv_order != 'PULP-NN-ADD':
tk['W_tile_size_nof_last'] = n_out % tile_n_out if (n_out % tile_n_out) > 0 else tile_n_out
if DW == 1:
tk['W_tile_size_nof_last'] = int(tk['W_tile_size_nof_last'] * ds_W / 8.0)
tk['W_tile_size_nif_last'] = tk['W_tile_size_nif']
tk['W_tile_size_nif_byte_last'] = int(math.ceil(tk['W_tile_size_nif_last'] * ds_W / 8.0))
# y last
tk['y_tile_size_nof_last'] = n_out % tile_n_out if (n_out % tile_n_out) > 0 else tile_n_out
tk['y_tile_size_h_last'] = h_out % tile_h_out if (h_out % tile_h_out) > 0 else tile_h_out
tk['y_tile_size_w_last'] = w_out % tile_w_out if (w_out % tile_w_out) > 0 else tile_w_out
tk['y_length_nof_byte_last'] = int(math.ceil(tk['y_tile_size_nof_last'] * ds_y / 8.0))
l = ""
for k, v in tk.items():
try:
l += "// %s %d\n" % (k.ljust(30), v)
except TypeError:
try:
l += "// %s %d\n" % (k.ljust(30), v[0])
except TypeError:
l += "// %s %s\n" % (k.ljust(30), v)
if conv_order == 'PULP-NN':
buffer_l1_all = W_buffer_size + x_buffer_size + y_buffer_size + tk['k_tile_size_byte'] + tk['lambda_tile_size_byte'] + 40 + tk['b_size_byte']
tk['im2col_dim'] = (8 * (fs1 * (tile_h_in + 2 * padding_top) + fs1)) * int( 8 / min(ds_x, ds_y, ds_W))
elif conv_order == 'PULP-NN-ADD':
buffer_l1_all = x_buffer_size * 2 + y_buffer_size + tk['k_tile_size_byte'] + tk['lambda_tile_size_byte'] + 40 + tk['b_size_byte']
elif conv_order == 'PULP-NN-MAX':
buffer_l1_all = x_buffer_size + y_buffer_size + tk['k_tile_size_byte'] + tk['lambda_tile_size_byte'] + 40 + tk['b_size_byte']
tk['buffer_l1_all'] = buffer_l1_all
l2_dim_input = (n_in) * tk['x_h'] * tk['x_w']
l2_dim_output = (tk['nof']) * tk['y_h'] * tk['y_w']
if DW == 0:
l2_dim_weights = int(tk['nof'] * tk['nif'] * tk['fs1'] * tk['fs2'] * ds_W / 8.0)
else:
l2_dim_weights = int(tk['nof'] * 1 * tk['fs1'] * tk['fs2'] * ds_W / 8.0)
l2_dim_k = k_buffer_size
l2_dim_lambda = lambd_buffer_size
root = '/'.join(os.getcwd().split('/')[:-1])
if conv_order == 'PULP-NN':
tmpl = Template(filename=root+"/templates/layer_templates/layer_template.c")
elif conv_order == 'PULP-NN-MAX':
if(optional_type == '1D_Conv'):
tmpl = Template(filename=root+"/templates/layer_templates/pooling_layer_1D_template.c")
else:
tmpl = Template(filename=root+"/templates/layer_templates/pooling_layer_template.c")
elif conv_order == 'PULP-NN-ADD':
if(optional_type == '1D_Conv'):
tmpl = Template(filename=root+"/templates/layer_templates/add_layer_1D_template.c")
else:
tmpl = Template(filename=root+"/templates/layer_templates/add_layer_template.c")
s = tmpl.render(TEST=test,VERBOSE=False,ULTRA_VERBOSE=ultra_verbose,PULP_TEST=True,verbose_log=l,**tk)
if 'L2' in test_location:
save_string = './application/DORY_network/src/' + name_layer.replace("h", "c")
elif 'L3' in test_location:
save_string = './application/DORY_network/src/' + name_layer.replace("h", "c")
with open(save_string, "w") as f:
f.write(s)
tmpl = Template(filename=root+"/templates/layer_templates/layer_template_h.h")
s = tmpl.render(
TEST=test,
VERBOSE=False,
ULTRA_VERBOSE=ultra_verbose,
PULP_TEST=True,
verbose_log=l,
**tk)
if 'L2' in test_location:
save_string = './application/DORY_network/inc/' + name_layer
elif 'L3' in test_location:
save_string = './application/DORY_network/inc/' + name_layer
with open(save_string, "w") as f:
f.write(s)
if 'L2' in test_location and L3_tiling == 0:
tk['out_mul'] = out_mul
tk['out_shift'] = out_shift
tk['l1_buffer'] = l1_buffer
tk['activation_size_out'] = int(math.ceil(l2_dim_output * ds_y / 8.0))
tk['activation_size_in'] = int(math.ceil(l2_dim_input * ds_x / 8.0))
tk['x_content'] = print_test_vector(x, 'char')
tk['y_expected_content'] = print_test_vector(y_gold, 'char')
tk['check_sum'] = sum(y_gold)
tk['W_content'] = print_test_vector(W, 'char')
tk['buffer_l1_all'] = buffer_l1_all
tk['l2_dim_weights'] = int(l2_dim_weights + (l2_dim_k + l2_dim_lambda))
tk['w_out'] = tk['y_w']
tk['h_out'] = tk['y_h']
tk['ultra_test'] = True
root = '/'.join(os.getcwd().split('/')[:-1])
tmpl = Template(filename=root+"/templates/test_templateL2.c")
s = tmpl.render(
TEST=test,
VERBOSE=False,
ULTRA_VERBOSE=ultra_verbose,
PULP_TEST=True,
verbose_log=l,
**tk)
save_string = './application/DORY_network/src/main.c'
with open(save_string, "w") as f:
f.write(s)
tk['build_layers'] = os.listdir('./application/DORY_network/src/')
tk['platform'] = 'GAP8'
tmpl = Template(filename=root+"/templates/Makefile_template_L2")
s = tmpl.render(**tk)
save_string = './application/Makefile'
with open(save_string, "w") as f:
f.write(s)
return l2_dim_input, l2_dim_output, l2_dim_weights, l2_dim_k, l2_dim_lambda, tk['b_size_byte'], buffer_l1_all, n_out, w_out, h_out
def print_test_vector(x, type_data):
# Print the test vector in the c file.
if type_data == 'char':
try:
np.set_printoptions(
threshold=sys.maxsize,
formatter={'int': lambda x: hex(np.uint8(x)) if (
x < 0) else hex(np.uint8(x)), }
)
except TypeError:
np.set_printoptions(threshold=sys.maxsize)
s = repr(x.flatten()).replace("array([", "").replace("]", "").replace("[", "").replace(")", "").replace(",\n dtype=int8)", "").replace(", dtype=uint8", "").replace(",\n dtype=uint8)", "").replace(",\n dtype=uint8", "").replace(",\n dtype=int8", "").replace(", dtype=int8", "").replace(", dtype=int8)", "").replace(", dtype=int8)", "").replace(", dtype=uint8)", "")
elif type_data == 'int16_t':
try:
np.set_printoptions(
threshold=sys.maxsize,
formatter={'int': lambda x: hex(np.uint16(x)) if (
x < 0) else hex(np.int16(x)), }
)
except TypeError:
np.set_printoptions(threshold=sys.maxsize)
s = repr(x.flatten()).replace("array([", "").replace("]", "").replace("[", "").replace(",\n dtype=int16)", "").replace(
", dtype=int16)", "").replace(", dtype=int16)", "").replace(", dtype=uint16)", "").replace(")", "")
else:
try:
np.set_printoptions(
threshold=sys.maxsize,
formatter={'int': lambda x: hex(np.uint32(x)) if (
x < 0) else hex(np.int32(x)), }
)
except TypeError:
np.set_printoptions(threshold=sys.maxsize)
s = repr(x.flatten()).replace("array([", "").replace("]", "").replace("[", "").replace(
",\n dtype=int32)", "").replace(", dtype=int32)", "").replace(", dtype=int32)", "").replace(", dtype=uint32)", "")
return s