forked from Xilinx/finn-hlslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamtools.h
1008 lines (944 loc) · 34.9 KB
/
streamtools.h
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Copyright (c) 2019, Xilinx, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION). HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
/******************************************************************************
*
* Authors: Giulio Gambardella <[email protected]>
* Thomas B. Preusser <[email protected]>
* Marie-Curie Fellow, Xilinx Ireland, Grant Agreement No. 751339
* Christoph Doehring <[email protected]>
*
* @file stream-tools.h
*
* Library of templated HLS functions for BNN deployment.
* This file lists a set of convenience funtions used to adapt stream size,
* remove unnecessary streams (padding) and casting
*
******************************************************************************/
#ifndef STREAMTOOLS_H
#define STREAMTOOLS_H
#include "ap_axi_sdata.h"
/**
* \brief Stream limiter - limits the number of stream packets
*
* The block only let the first NumAllowed elements of a stream to pass through, the remainder
* (NumTotal-NumAllowed) are consumed from input but not re-emitted from the output.
* Useful to remove padding
*
* \tparam DataWidth Width, in number of bits, of the input and output stream
* \tparam NumAllowed Number of words to pass through
* \tparam NumTotal Total number of words (NumAllowed+NumDropped)
*
* \param in Input stream
* \param out Output stream
*
*/
template<unsigned int DataWidth,
unsigned int NumAllowed,
unsigned int NumTotal
>
void StreamLimiter(hls::stream<ap_uint<DataWidth> > & in,
hls::stream<ap_uint<DataWidth> > & out) {
static_assert(NumTotal >= NumAllowed, "");
unsigned int numLeft = NumAllowed;
for (unsigned int i = 0; i < NumTotal; i++) {
#pragma HLS pipeline style=flp II=1
ap_uint<DataWidth> e = in.read();
if (numLeft > 0) {
out.write(e);
numLeft--;
}
}
}
/**
* \brief Stream limiter batch - limits the number of stream packets multiple times
*
* The block only let the first NumAllowed elements of a stream to pass through, the remainder
* (NumTotal-NumAllowed) are consumed from input but not re-emitted from the output.
* Useful to remove padding on multiple images (numReps)
*
* \tparam DataWidth Width, in number of bits, of the input and output stream
* \tparam NumAllowed Number of words to pass through
* \tparam NumTotal Total number of words (NumAllowed+NumDropped)
*
* \param in Input stream
* \param out Output stream
* \param numReps Number of times the StreamLimiter function has to be called
*
*/
template<unsigned int DataWidth,
unsigned int NumAllowed,
unsigned int NumTotal
>
void StreamLimiter_Batch(hls::stream<ap_uint<DataWidth> > & in,
hls::stream<ap_uint<DataWidth> > & out, unsigned int numReps) {
for (unsigned int rep = 0; rep < numReps; rep++) {
StreamLimiter<DataWidth, NumAllowed, NumTotal>(in, out);
}
}
/**
* \brief Stream Padding - Padds the input with zeroes for when the sliding window is
* centered on border pixels
*
* Used to add padding to the input with zeroes in case the sliding window is
* centered on border pixels
*
* \tparam ImgDim Size of the input feature map
* \tparam KernelDim Size of the sliding window
* \tparam Stride Stride of the sliding window
* \tparam NumChannels Amount of channels of the input feature map
* \tparam In_t Input datatype
* \tparam PaddingStyle Type of padding that will be applied
*
* \param in Input stream
* \param out Output stream
*
*/
template< unsigned int ImgDim,
unsigned int KernelDim,
unsigned int Stride,
unsigned int NumChannels,
typename In_t,
unsigned int PaddingStyle=2>
void SameResize(hls::stream<ap_uint<NumChannels* In_t::width> > &in,
hls::stream<ap_uint<NumChannels* In_t::width> > &out){
// Number of "same" windows over the input data
constexpr unsigned int SameWindows = (ImgDim) / Stride + ((ImgDim % Stride) > 0);
// Number of elements to generate as output per dimension
constexpr unsigned int OutputDim = KernelDim + Stride * (SameWindows - 1);
// Padding
constexpr unsigned int Padding = OutputDim - ImgDim;
// Padding Up and Left
constexpr unsigned int PaddingUp = Padding/2 + ((PaddingStyle == 2) ? ((Padding % 2) > 0) : 0);
constexpr unsigned int PaddingLeft = Padding/2 + ((PaddingStyle == 2) ? ((Padding % 2) > 0) : 0);
// Padding Down and Right (might be 1 element more than up and left in case of odd padding)
constexpr unsigned int PaddingDown = Padding - PaddingUp;
constexpr unsigned int PaddingRight = Padding - PaddingLeft;
ap_uint<NumChannels* In_t::width> outData, inData;
for(unsigned int y = 0; y<OutputDim; y++){
for(unsigned int x=0; x < OutputDim; x++){
#pragma HLS pipeline style=flp II=1
// Padding Rows
if(y < PaddingUp || y >= (OutputDim - PaddingDown)){
outData = 0;
}
// Padding Cols
else if(x < PaddingLeft || x >= (OutputDim - PaddingRight)){
outData = 0;
}
// No Padding
else{
inData = in.read();
outData = inData;
}
out.write(outData);
}
}
}
/**
* \brief Stream Padding - Padds the input of multiple frames with zeroes
* for when the sliding window is centered on border pixels
*
* Used to add padding with zeroes to multiple inputs in case the sliding window is
* centered on border pixels
*
* \tparam ImgDim Size of the input feature map
* \tparam KernelDim Size of the sliding window
* \tparam Stride Stride of the sliding window
* \tparam NumChannels Amount of channels of the input feature map
* \tparam In_t Input datatype
* \tparam PaddingStyle Type of padding that will be applied
*
* \param in Input stream
* \param out Output stream
* \param numReps Amount of frames / images
*
*/
template< unsigned int ImgDim,
unsigned int KernelDim,
unsigned int Stride,
unsigned int NumChannels,
typename In_t,
unsigned int PaddingStyle=2>
void SameResize_Batch(hls::stream<ap_uint<NumChannels* In_t::width> > &in,
hls::stream<ap_uint<NumChannels* In_t::width> > &out,
const unsigned int numReps) {
for (unsigned int rep = 0; rep < numReps; rep++) {
SameResize<ImgDim, KernelDim, Stride, NumChannels, In_t, PaddingStyle>(in, out);
}
}
/**
* \brief Stream cast - Casts the input stream to a different datatype (OutT)
*
* Used to upscale or downscale a stream, enabling loss of information for downscaling or
* 0 padding for upscaling
*
* \tparam InT Width, in number of bits, of the input and output stream
* \tparam OutT Number of words to pass through
*
* \param in Input stream
* \param out Output stream
* \param numReps Number of times the StreamLimiter function has to be called
*
*/
template<typename InT, typename OutT>
void StreamingCast(hls::stream<InT> & in, hls::stream<OutT> & out, unsigned int numReps) {
for(unsigned int i = 0; i < numReps; i++) {
#pragma HLS pipeline style=flp II=1
out.write((OutT) in.read());
}
}
/**
* \brief FM Padding - Padds the input with zeroes for when the sliding window is
* centered on border pixels
*
* Used to add padding with zeroes to multiple inputs in case the sliding window is
* centered on border pixels - working on non-square images and padding
*
* \tparam OutputDim_x Padded width of the output feature map
* \tparam OutputDim_y Padded height of the output feature map
* \tparam PaddingLeft Left image padding on x-axis
* \tparam PaddingRight Right image padding on x-axis
* \tparam PaddingTop Top image padding on y-axis
* \tparam PaddingBottom Bottom image padding on y-axis
* \tparam NumChannels Number of channels of the input feature map
* \tparam SIMD Input parallelism
* \tparam In_t Input datatype
*
* \param in Input stream
* \param out Output stream
*/
template<
unsigned OutputDim_x,
unsigned OutputDim_y,
unsigned PaddingLeft,
unsigned PaddingRight,
unsigned PaddingTop,
unsigned PaddingBottom,
unsigned NumChannels,
unsigned SIMD,
typename In_t
>
void FMPadding_nonsquare(
hls::stream<ap_uint<SIMD*In_t::width>> &in,
hls::stream<ap_uint<SIMD*In_t::width>> &out
){
static_assert(NumChannels%SIMD == 0, "Channel count must be a SIMD multiple.");
constexpr unsigned Folding = NumChannels/SIMD;
for(unsigned y = 0; y < OutputDim_y; y++) {
for(unsigned x = 0; x < OutputDim_x; x++) {
for(unsigned sf = 0; sf < Folding; sf++) {
#pragma HLS pipeline style=flp II=1
ap_uint<SIMD*In_t::width> outData = 0;
// Read & forward real data only for non-padding image kernel
if(
/* rows */ (PaddingTop <= y) && (y < OutputDim_y - PaddingBottom) &&
/* cols */ (PaddingLeft <= x) && (x < OutputDim_x - PaddingRight)
) {
outData = in.read();
}
out.write(outData);
}
}
}
}
/**
* \brief FM Padding Non Square - Padds the input of multiple frames with zeroes
* for when the sliding window is centered on border pixels
*
* Used to add padding with zeroes to multiple inputs in case the sliding window is
* centered on border pixels - working on non-square images and padding
*
* \tparam OutputDim_x Padded width of the output feature map
* \tparam OutputDim_y Padded height of the output feature map
* \tparam PaddingLeft Left image padding on x-axis
* \tparam PaddingRight Right image padding on x-axis
* \tparam PaddingTop Top image padding on y-axis
* \tparam PaddingBottom Bottom image padding on y-axis
* \tparam NumChannels Number of channels of the input feature map
* \tparam SIMD Input parallelism
* \tparam In_t Input datatype
*
* \param in Input stream
* \param out Output stream
* \param numReps Number of frames / images
*/
template<
unsigned OutputDim_x,
unsigned OutputDim_y,
unsigned PaddingLeft,
unsigned PaddingRight,
unsigned PaddingTop,
unsigned PaddingBottom,
unsigned NumChannels,
unsigned SIMD,
typename In_t
>
void FMPadding_nonsquare_Batch(
hls::stream<ap_uint<SIMD*In_t::width>> &in,
hls::stream<ap_uint<SIMD*In_t::width>> &out,
unsigned const numReps
) {
for (unsigned int rep = 0; rep < numReps; rep++) {
FMPadding_nonsquare<
OutputDim_x, OutputDim_y,
PaddingLeft, PaddingRight, PaddingTop, PaddingBottom,
NumChannels, SIMD, In_t
>(in, out);
}
}
/**
* \brief FM Padding - Padds the input with zeroes for when the sliding window is
* centered on border pixels
*
* Used to add padding to the input with zeroes in case the sliding window is
* centered on border pixels
*
* \tparam ImgDim <ignored>
* \tparam OutputDim Size of the output feature map
* \tparam PaddingBefore Top / left padding
* \tparam PaddingBehind Bottom / right padding
* \tparam NumChannels Number of channels of the input feature map
* \tparam SIMD Input parallelism
* \tparam In_t Input datatype
*
* \param in Input stream
* \param out Output stream
*
*/
template<
unsigned ImgDim,
unsigned OutputDim,
unsigned PaddingBefore,
unsigned PaddingBehind,
unsigned NumChannels,
unsigned SIMD,
typename In_t
>
void FMPadding(
hls::stream<ap_uint<SIMD*In_t::width>> &in,
hls::stream<ap_uint<SIMD*In_t::width>> &out
){
#pragma HLS inline
FMPadding_nonsquare<
OutputDim, OutputDim,
PaddingBefore, PaddingBehind, PaddingBefore, PaddingBehind,
NumChannels, SIMD, In_t
>(in, out);
}
/**
* \brief FM Padding - Padds the input of multiple frames with zeroes
* for when the sliding window is centered on border pixels
*
* Used to add padding with zeroes to multiple inputs in case the sliding window is
* centered on border pixels
*
* \tparam ImgDim <ignored>
* \tparam OutputDim Size of the output feature map
* \tparam PaddingBefore Top / left padding
* \tparam PaddingBehind Bottom / right padding
* \tparam NumChannels Number of channels of the input feature map
* \tparam SIMD Input parallelism
* \tparam In_t Input datatype
*
* \param in Input stream
* \param out Output stream
* \param numReps Number of frames / images
*/
template<
unsigned ImgDim,
unsigned OutputDim,
unsigned PaddingBefore,
unsigned PaddingBehind,
unsigned NumChannels,
unsigned SIMD,
typename In_t
>
void FMPadding_Batch(
hls::stream<ap_uint<SIMD*In_t::width>> &in,
hls::stream<ap_uint<SIMD*In_t::width>> &out,
unsigned const numReps
) {
for (unsigned int rep = 0; rep < numReps; rep++) {
FMPadding<ImgDim, OutputDim, PaddingBefore, PaddingBehind, NumChannels, SIMD, In_t>(in, out);
}
}
/**
* \brief Stream Data Width Converter - Converts the width of the input stream in the output stream
*
* Used to upscale or downscale a stream, without any loss of data in the procedure.
* For downscaling (InWidth > OutWidth), InWidth has to be a multiple of OutWidth.
* For upscaling (InWidth < OutWidth), OutWidth has to be a multiple of InWidth.
*
* \tparam InWidth Width, in number of bits, of the input stream
* \tparam OutWidth Width, in number of bits, of the output stream
* \tparam NumInWords Number of input words to process
*
* \param in Input stream
* \param out Output stream
* \param numReps Number of times the function has to be called
*
*/
template<unsigned int InWidth,
unsigned int OutWidth,
unsigned int NumInWords
>
void StreamingDataWidthConverter_Batch(hls::stream<ap_uint<InWidth> > & in,
hls::stream<ap_uint<OutWidth> > & out, const unsigned int numReps) {
static_assert((InWidth % OutWidth == 0) || (OutWidth % InWidth == 0), "");
if (InWidth > OutWidth) {
// emit multiple output words per input word read
const unsigned int outPerIn = InWidth / OutWidth;
const unsigned int totalIters = NumInWords * outPerIn * numReps;
unsigned int o = 0;
ap_uint<InWidth> ei = 0;
for (unsigned int t = 0; t < totalIters; t++) {
#pragma HLS pipeline style=flp II=1
// read new input word if current out count is zero
if (o == 0) {
ei = in.read();
}
// pick output word from the rightmost position
ap_uint<OutWidth> eo = ei(OutWidth - 1, 0);
out.write(eo);
// shift input to get new output word for next iteration
ei = ei >> OutWidth;
// increment written output count
o++;
// wraparound indices to recreate the nested loop structure
if (o == outPerIn) {
o = 0;
}
}
} else if (InWidth == OutWidth) {
// straight-through copy
for (unsigned int i = 0; i < NumInWords * numReps; i++) {
#pragma HLS pipeline style=flp II=1
ap_uint<InWidth> e = in.read();
out.write(e);
}
} else { // InWidth < OutWidth
// read multiple input words per output word emitted
const unsigned int inPerOut = OutWidth / InWidth;
const unsigned int totalIters = NumInWords * numReps;
unsigned int i = 0;
ap_uint<OutWidth> eo = 0;
for (unsigned int t = 0; t < totalIters; t++) {
#pragma HLS pipeline style=flp II=1
// read input and shift into output buffer
ap_uint<InWidth> ei = in.read();
eo = eo >> InWidth;
eo(OutWidth - 1, OutWidth - InWidth) = ei;
// increment read input count
i++;
// wraparound logic to recreate nested loop functionality
if (i == inPerOut) {
i = 0;
out.write(eo);
}
}
}
}
/**
* \brief Stream Data Width Converter No Multiple -
* Converts the width of the input stream in the output stream for no multiple dimensions
*
* Used to downscale a stream, without any loss of data in the procedure.
* For downscaling (InWidth > OutWidth), InWidth has to be a multiple of OutWidth.
*
* \tparam InWidth Width, in number of bits, of the input stream
* \tparam OutWidth Width, in number of bits, of the output stream
*
* \param in Input stream
* \param out Output stream
*
*/
template<
unsigned int InWidth,
unsigned int OutWidth
>
void StreamingDataWidthConverterNoMultiple(
hls::stream<ap_uint<InWidth> > & in,
hls::stream<ap_uint<OutWidth> > & out) {
static_assert((InWidth % 2) == 0, "");
static_assert((OutWidth % 2) == 0, "");
static_assert(InWidth != OutWidth, "");
static unsigned int offset = 0;
if (InWidth > OutWidth){
static ap_uint<OutWidth> remainder = 0;
ap_uint<InWidth> valueIn = in.read();
if(offset !=0) {
ap_uint<OutWidth> valueOut = 0;
valueOut = (valueIn(offset-1,0),remainder(OutWidth-offset-1,0));
valueIn = valueIn(InWidth-1,offset); // leave the next part prepared
out.write(valueOut);
}
for (; offset <= (InWidth-OutWidth) ; offset+=OutWidth){
ap_uint<OutWidth> valueOut = valueIn(OutWidth-1,0);
valueIn = valueIn(InWidth-1,OutWidth); // leave the next part prepared
out.write(valueOut);
}
remainder = valueIn;
if (offset == InWidth)
offset = 0;
else
offset = offset + OutWidth - InWidth;
}
else {
/*OutWidth > InWidth*/
static ap_uint<InWidth> remainder = 0;
ap_uint<OutWidth> value = 0;
if (offset !=0) {
value(offset-1,0) = remainder(InWidth-1,InWidth-offset);
}
for (; offset <= (OutWidth-InWidth); offset+=InWidth){
ap_uint<InWidth> aux = in.read();
value(offset+InWidth-1,offset) = aux;
}
if (offset != OutWidth){
ap_uint<InWidth> aux = in.read();
value(OutWidth-1,offset) = aux(OutWidth-offset-1,0);
remainder = aux;
offset = offset + InWidth - OutWidth;
}
else
offset = 0;
out.write(value);
}
}
/**
* \brief Stream Duplicator - Reads in a stream and writes the data into two identical streams
*
* Used to generate the inputs to the bypass and convolutional branches in Resnet-50
*
* \tparam DataWidth Width, in number of bits, of the streams
* \tparam NumTotal Total number of words in the input stream
*
* \param in Input stream
* \param out1 Output stream I
* \param out2 Output stream II
*
*/
template<unsigned int DataWidth,
unsigned int NumTotal
>
void DuplicateStreams(hls::stream<ap_uint<DataWidth> > & in, hls::stream<ap_uint<DataWidth> > & out1,
hls::stream<ap_uint<DataWidth> > & out2) {
for (unsigned int i = 0; i < NumTotal; i++) {
#pragma HLS pipeline style=flp II=1
ap_uint<DataWidth> e = in.read();
out1.write(e);
out2.write(e);
}
}
/**
* \brief Batch Stream Duplicator - Reads in a stream multiple times and writes the data into two identical streams
*
* Used to generate the inputs to the bypass and convolutional branches in Resnet-50 when dealing with multiple 'frames'
*
* \tparam DataWidth Width, in number of bits, of the streams
* \tparam NumTotal Total number of words in the input stream
*
* \param in Input stream
* \param out1 Output stream I
* \param out2 Output stream II
* \param numReps Number of frames / images
*
*/
template<unsigned int DataWidth,
unsigned int NumTotal
>
void DuplicateStreams_Batch(hls::stream<ap_uint<DataWidth> > & in, hls::stream<ap_uint<DataWidth> > & out1,
hls::stream<ap_uint<DataWidth> > & out2, const unsigned int numReps) {
for (unsigned int image = 0; image < numReps; image++) {
DuplicateStreams<DataWidth, NumTotal>(in, out1, out2);
}
}
/**
* \brief Element-Wise Addition - Reads in data elements from two streams and writes the sum of these elements to an output
*
* \tparam NumChannels Amount of channels of the streams
* \tparam In1_t First operand datatype
* \tparam In2_t Second operand datatype
* \tparam Out_t Datatype of the accumulation output
* \tparam NumTotal Total number of words in the input streams
* \tparam offset Offset value for the accumulation
*
* \param in1 Input stream I
* \param in2 Input stream II
* \param out Output stream
*
*/
template <unsigned int NumChannels,
typename In1_t,
typename In2_t,
typename Out_t,
unsigned int NumTotal,
int offset = 0>
void AddStreams(hls::stream<ap_uint<NumChannels * In1_t::width>> &in1, hls::stream<ap_uint<NumChannels * In2_t::width>> &in2,
hls::stream<ap_uint<NumChannels * Out_t::width>> &out) {
for (unsigned int i = 0; i < NumTotal; i++) {
#pragma HLS pipeline style=flp II=1
ap_uint<NumChannels * In1_t::width> e1 = in1.read();
ap_uint<NumChannels * In2_t::width> e2 = in2.read();
ap_uint<NumChannels * Out_t::width> e;
for (unsigned int j = 0; j < NumChannels; j++) {
#pragma HLS UNROLL
In1_t op1 = e1((j + 1) * In1_t::width - 1, j * In1_t::width);
In2_t op2 = e2((j + 1) * In2_t::width - 1, j * In2_t::width);
Out_t sum = op1 + op2 + offset;
e((j + 1) * Out_t::width - 1, j * Out_t::width) = sum;
}
out.write(e);
}
}
/**
* \brief
*
* Used to implement point-wise addition in Resnet-50 for multiple images
*
* \tparam NumChannels Amount of channels of the streams
* \tparam In1_t First operand datatype
* \tparam In2_t Second operand datatype
* \tparam Out_t Datatype of the accumulation output
* \tparam NumTotal Total number of words in the input streams
* \tparam offset Offset value for the accumulation
*
* \param in1 Input stream I
* \param in2 Input stream II
* \param out Output stream
* \param numReps Number of frames / images
*
*/
template <unsigned int NumChannels,
typename In1_t,
typename In2_t,
typename Out_t,
unsigned int NumTotal,
int offset = 0>
void AddStreams_Batch(hls::stream<ap_uint<NumChannels * In1_t::width>> &in1, hls::stream<ap_uint<NumChannels * In2_t::width>> &in2,
hls::stream<ap_uint<NumChannels * Out_t::width>> &out, const unsigned int numReps) {
for (unsigned int image = 0; image < numReps; image++) {
AddStreams<NumChannels, In1_t, In2_t, Out_t, NumTotal, offset>(in1, in2, out);
}
}
/**
* \brief Addition Layer - Reads in two streams and writes the sum of these streams to an output
*
* Used to merge the outputs of the bypass and convolutional branches in Resnet-50
*
* \tparam NumChannels Amount of channels of the streams
* \tparam In1_t First operand datatype
* \tparam In2_t Second operand datatype
* \tparam Out_t Datatype of the accumulation output * \tparam NumTotal Total number of words in the input streams
* \tparam PECount Amount of processing elements working in parallel
* \tparam offset Offset value for the accumulation
*
* \param in1 Input stream I
* \param in2 Input stream II
* \param out Output stream
* \param numReps Number of frames / images
*
*/
template <unsigned int NumChannels,
typename In1_t,
typename In2_t,
typename Out_t,
unsigned int NumTotal,
unsigned int PECount,
int offset = 0>
void AddStreamsLayer_Batch(hls::stream<ap_uint<NumChannels * In1_t::width>> &in1, hls::stream<ap_uint<NumChannels * In2_t::width>> &in2,
hls::stream<ap_uint<NumChannels * Out_t::width>> &out, const unsigned int numReps) {
#pragma HLS INLINE
static_assert(NumChannels % PECount == 0, "");
hls::stream<ap_uint<PECount * In1_t::width>> in_folded1;
hls::stream<ap_uint<PECount * In2_t::width>> in_folded2;
hls::stream<ap_uint<PECount * Out_t::width>> out_folded;
StreamingDataWidthConverter_Batch<NumChannels * In1_t::width, PECount * In1_t::width, NumTotal>(in1, in_folded1, numReps);
StreamingDataWidthConverter_Batch<NumChannels * In2_t::width, PECount * In2_t::width, NumTotal>(in2, in_folded2, numReps);
AddStreams_Batch<PECount, In1_t, In2_t, Out_t, NumTotal *(NumChannels / PECount),offset>(in_folded1, in_folded2, out_folded, numReps);
StreamingDataWidthConverter_Batch<PECount * Out_t::width, NumChannels * Out_t::width, NumTotal *(NumChannels / PECount)>(out_folded, out, numReps);
}
/**
* \brief Stream Multi Chan Data Width Converter - Converts the width of the input stream in the output stream, working on multiple parallel streams
*
* Used to upscale or downscale a stream, without any loss of data in the procedure.
* For downscaling (InWidth > OutWidth), InWidth has to be a multiple of OutWidth.
* For upscaling (InWidth < OutWidth), OutWidth has to be a multiple of InWidth.
* This version works on the MMV structure, with multiple parallel streams
*
* \tparam InWidth Width, in number of bits, of the input stream
* \tparam OutWidth Width, in number of bits, of the output stream
* \tparam NumInWords Number of input words to process
* \tparam NumVecs Number of parallel vectors MMV
*
* \param in Input stream
* \param out Output stream
* \param numReps Number of times the function has to be called
*
*/
template<unsigned int InWidth, // width of input stream
unsigned int OutWidth, // width of output stream
unsigned int NumInWords, // number of input words to process
unsigned int NumVecs
>
void MultiChanDataWidthConverter_Batch(
hls::stream<MultiChanData<NumVecs, InWidth> > & in,
hls::stream<MultiChanData<NumVecs, OutWidth> > & out,
const unsigned int numReps) {
static_assert((InWidth % OutWidth == 0) || (OutWidth % InWidth == 0), "");
if (InWidth > OutWidth) {
// emit multiple output words per input word read
const unsigned int outPerIn = InWidth / OutWidth;
const unsigned int totalIters = NumInWords * outPerIn * numReps;
unsigned int o = 0;
MultiChanData<NumVecs, InWidth> ei;
for (unsigned int t = 0; t < totalIters; t++) {
#pragma HLS pipeline style=flp II=1
// read new input word if current out count is zero
if (o == 0)
ei = in.read();
// pick output word from the rightmost position
MultiChanData<NumVecs, OutWidth> eo;
for(unsigned int v = 0; v < NumVecs; v++) {
#pragma HLS UNROLL
eo.data[v] = (ei.data[v])(OutWidth - 1, 0);
// shift input to get new output word for next iteration
ei.data[v] = ei.data[v] >> OutWidth;
}
out.write(eo);
// increment written output count
o++;
// wraparound indices to recreate the nested loop structure
if (o == outPerIn) {
o = 0;
}
}
} else if (InWidth == OutWidth) {
// straight-through copy
for (unsigned int i = 0; i < NumInWords * numReps; i++) {
#pragma HLS pipeline style=flp II=1
MultiChanData<NumVecs, InWidth> e = in.read();
MultiChanData<NumVecs, OutWidth> eo;
// we don't support typecasting between templated types, so explicitly
// transfer vector-by-vector here
for(unsigned int v=0; v < NumVecs; v++) {
#pragma HLS UNROLL
eo.data[v] = e.data[v];
}
out.write(eo);
}
} else { // InWidth < OutWidth
// read multiple input words per output word emitted
const unsigned int inPerOut = OutWidth / InWidth;
const unsigned int totalIters = NumInWords * numReps;
unsigned int i = 0;
MultiChanData<NumVecs, OutWidth> eo;
for (unsigned int t = 0; t < totalIters; t++) {
#pragma HLS pipeline style=flp II=1
// read input and shift into output buffer
MultiChanData<NumVecs, InWidth> ei = in.read();
for(unsigned int v = 0; v < NumVecs; v++) {
#pragma HLS UNROLL
eo.data[v] = eo.data[v] >> InWidth;
(eo.data[v])(OutWidth - 1, OutWidth - InWidth) = ei.data[v];
}
// increment read input count
i++;
// wraparound logic to recreate nested loop functionality
if (i == inPerOut) {
i = 0;
out.write(eo);
}
}
}
}
/**
* \brief Flatten Multi Chan Data - Converts the parallel input stream in a flatten output stream
*
* Used to pach a flattened stream into a structure with multiple parallel streams
*
* \tparam NumChannels Number of channels flattened in the input stream
* \tparam DataWidth Width, in number of bits, of each stream
*
* \param in Input parallel stream
* \param out Output stream
* \param numReps Number of times the function has to be called
*
*/
template <unsigned int NumChannels, unsigned int DataWidth>
void FlattenMultiChanData(
hls::stream<MultiChanData<NumChannels, DataWidth> > & in,
hls::stream<ap_uint<NumChannels*DataWidth> > & out,
const unsigned int numReps
) {
for(unsigned int r = 0; r < numReps; r++) {
#pragma HLS pipeline style=flp II=1
MultiChanData<NumChannels, DataWidth> e = in.read();
ap_uint<NumChannels*DataWidth> o = 0;
for(unsigned int v = 0; v < NumChannels; v++) {
#pragma HLS UNROLL
o(DataWidth*(v+1)-1, DataWidth*v) = e.data[v];
}
out.write(o);
}
}
/**
* \brief Pack Multi Chan Data - Converts the flatten input stream into a parallel output stream
*
* Used to pach a flattened stream into a structure with multiple parallel streams
*
* \tparam NumChannels Number of channels flattened in the input stream
* \tparam DataWidth Width, in number of bits, of each stream
*
* \param in Input stream
* \param out Output parallel stream
* \param numReps Number of times the function has to be called
*
*/
template <unsigned int NumChannels, unsigned int DataWidth>
void PackMultiChanData(
hls::stream<ap_uint<NumChannels*DataWidth> > & in,
hls::stream<MultiChanData<NumChannels, DataWidth> > & out,
const unsigned int numReps
) {
for(unsigned int r = 0; r < numReps; r++) {
#pragma HLS pipeline style=flp II=1
ap_uint<NumChannels*DataWidth> e = in.read();
MultiChanData<NumChannels, DataWidth> o;
for(unsigned int v = 0; v < NumChannels; v++) {
#pragma HLS UNROLL
o.data[v] = e(DataWidth*(v+1)-1, DataWidth*v);
}
out.write(o);
}
}
template<unsigned IW, unsigned OW, unsigned N>
class WidthAdjustedInputStream {
hls::stream<ap_uint<OW>> m_target;
public:
WidthAdjustedInputStream(hls::stream<ap_uint<IW> >& source, unsigned const reps) {
StreamingDataWidthConverter_Batch<IW, OW, N>(source, m_target, reps);
}
~WidthAdjustedInputStream() {}
public:
operator hls::stream<ap_uint<OW> >&() {
return m_target;
}
};
template<unsigned W, unsigned N>
class WidthAdjustedInputStream<W, W, N> {
hls::stream<ap_uint<W>> &m_source;
public:
WidthAdjustedInputStream(hls::stream<ap_uint<W> >& source, __attribute__((unused)) unsigned const reps) : m_source(source) {}
~WidthAdjustedInputStream() {}
public:
operator hls::stream<ap_uint<W> >&() {
return m_source;
}
};
template<unsigned IW, unsigned OW, unsigned N>
class WidthAdjustedOutputStream {
hls::stream<ap_uint<IW>> m_buffer;
hls::stream<ap_uint<OW>> &m_target;
unsigned const m_reps;
public:
WidthAdjustedOutputStream(hls::stream<ap_uint<OW> >& target, unsigned const reps) : m_target(target), m_reps(reps) {}
~WidthAdjustedOutputStream() {
StreamingDataWidthConverter_Batch<IW, OW, N>(m_buffer, m_target, m_reps);
}
public:
operator hls::stream<ap_uint<IW> >&() {
return m_buffer;
}
};
template<unsigned W, unsigned N>
class WidthAdjustedOutputStream<W, W, N> {
hls::stream<ap_uint<W>> &m_target;
public:
WidthAdjustedOutputStream(hls::stream<ap_uint<W> >& target, __attribute__((unused)) unsigned const reps)
: m_target(target) {}
~WidthAdjustedOutputStream() {}
public:
operator hls::stream<ap_uint<W> >&() {
return m_target;
}
};
/**
* \brief QDMA stream to normal stream conversion - Reads in a QDMA stream and strips metadata (TLAST, TKEEP)
*
* Used as an adapter when connecting blocks through top-level Vitis streams (kernel to kernel or host to plaform streaming)
*
* \tparam DataWidth Width, in number of bits, of the data on streams
* \tparam NumTotal Total number of words in the input stream
*
* \param in Input stream
* \param out Output stream
* \param numReps Number of frames / images
*
*/
template<unsigned int DataWidth, unsigned int NumTotal>
void Qdma2Stream_Batch(hls::stream<qdma_axis<DataWidth,0,0,0> > & in, hls::stream<ap_uint<DataWidth> > & out, const unsigned int numReps){
//TODO: static_assert to ensure DataWidth is power of 2 between 8 and 512
for (unsigned int image = 0; image < numReps; image++) {
for (unsigned int word = 0; word < NumTotal; word++) {
#pragma HLS pipeline style=flp II=1
out.write(in.read().get_data());
}
}
}
/**
* \brief Normal stream to QDMA stream conversion - Reads in a stream and outputs a QDMA stream including metadata (TLAST, TKEEP)
*
* Used as an adapter when connecting blocks through top-level Vitis streams (kernel to kernel or host to plaform streaming)
*
* \tparam DataWidth Width, in number of bits, of the data on streams
* \tparam NumTotal Total number of words in the input stream
*
* \param in Input stream
* \param out Output stream
* \param numReps Number of frames / images
*
*/
template<unsigned int DataWidth, unsigned int NumTotal>
void Stream2Qdma_Batch(hls::stream<ap_uint<DataWidth> > & in, hls::stream<qdma_axis<DataWidth,0,0,0> > & out, const unsigned int numReps){
for (unsigned int image = 0; image < numReps; image++) {
for (unsigned int word = 0; word < NumTotal; word++) {
#pragma HLS pipeline style=flp II=1
qdma_axis<DataWidth,0,0,0> temp;
temp.set_data(in.read());