forked from Xilinx/finn-hlslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac.hpp
executable file
·206 lines (195 loc) · 8.17 KB
/
mac.hpp
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
/******************************************************************************
* 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 mac.hpp
*
* Library of templated HLS functions for BNN deployment.
* This file lists a set of convenience funtions used to implement
* multipliers with selectable implementation resource
*
* This project has received funding from the European Union's Framework
* Programme for Research and Innovation Horizon 2020 (2014-2020) under
* the Marie Skłodowska-Curie Grant Agreement No. 751339.
*
*****************************************************************************/
/*****************************************************************************
* MAC operation template:
*
* mac<N, T, TC, TD>(T a, TC c[N], TD d[N])
* = a + SUM_{i=0}^{N-1} c(i)*d(i)
*
* All template arguments but N can typically be inferred.
*
* mac<ap_uint<14>>(0, c, d)
*****************************************************************************/
#ifndef MAC_HPP
#define MAC_HPP
#include "utils.hpp"
/**
* \brief Multipliy operation between 2 operands, HLS choose the best resource
*
* The same multiply operation can be implemented using multiple Vivado HLS pragmas to select the
* hardware resource to be used:
* ap_resource_dflt will let HLS choose the best one
* ap_resource_lut will force HLS to implement the multiplier in LUTs
* ap_resource_dsp will force HLS to implement the multiplier in DSP48
*
* \tparam TC First operand datatype (weights)
* \tparam TD Second operand datatype (input)
*
* \param c First operand (array of weights)
* \param d Second operand (array of input activation)
* \param r Resource type for the hardware implementation of the MAC block
*
* \return Result of the multiply operation
*/
template<typename TC, typename TD>
auto mul(TC const &c, TD const &d, ap_resource_dflt const&) -> decltype(c*d) {
#pragma HLS inline
auto r = c*d;
return r;
}
/**
* \brief Multiply operation between 2 operands, implemented in LUT
*
* The same multiply operation can be implemented using multiple Vivado HLS pragmas to select the
* hardware resource to be used:
* ap_resource_dflt will let HLS choose the best one
* ap_resource_lut will force HLS to implement the multiplier in LUTs
* ap_resource_dsp will force HLS to implement the multiplier in DSP48
*
* \tparam TC First operand datatype (weights)
* \tparam TD Second operand datatype (input)
*
* \param c First operand (array of weights)
* \param d Second operand (array of input activation)
* \param r Resource type for the hardware implementation of the MAC block
*
* \return Result of the multiply operation
*/
template<typename TC, typename TD>
auto mul(TC const &c, TD const &d, ap_resource_lut const&) -> decltype(c*d) {
#pragma HLS inline
decltype(c*d) const res = c*d;
#pragma HLS RESOURCE variable=res core=Mul_LUT
return res;
}
/**
* \brief Multipliy operation between 2 operands, implemented in a DSP48
*
* The same multiply operation can be implemented using multiple Vivado HLS pragmas to select the
* hardware resource to be used:
* ap_resource_dflt will let HLS choose the best one
* ap_resource_lut will force HLS to implement the multiplier in LUTs
* ap_resource_dsp will force HLS to implement the multiplier in DSP48
*
* \tparam TC First operand datatype (weights)
* \tparam TD Second operand datatype (input)
*
* \param c First operand (array of weights)
* \param d Second operand (array of input activation)
* \param r Resource type for the hardware implementation of the MAC block
*
* \return Result of the multiply operation
*/
template<typename TC, typename TD>
auto mul(TC const &c, TD const &d, ap_resource_dsp const&) -> decltype(c*d) {
#pragma HLS inline
decltype(c*d) const res = c*d;
#pragma HLS RESOURCE variable=res core=DSP48
return res;
}
/**
* \brief MAC with selectable implementation resource, used by Matrix_Vector_Activate_Batch
*
* \tparam N Number of MAC to be performed (equals to SIMD in mvau)
* \tparam T Accumulator datatype
* \tparam TC First operand datatype (weights)
* \tparam TD Second operand datatype (input)
* \tparam R Datatype for the resource used for FPGA implementation of the MAC - safely deducible from the paramaters
*
* \param a Initialization value of the accumulation
* \param c First operand (array of weights)
* \param d Second operand (array of input activation)
* \param r Resource type for the hardware implementation of the MAC block
* \param mmv MMV value to address accumulator and activation
*
* \return Result of the MAC operation
*/
template<unsigned N, typename T, typename TC, typename TD, typename R>
T mac(T const &a, TC const &c, TD const &d, R const &r, unsigned mmv) {
#pragma HLS inline
T res = a;
for(unsigned i = 0; i < N; i++) {
#pragma HLS unroll
res += mul(c[i], d(i,mmv), r);
}
return res;
}
/**
* \brief MAC with selectable implementation resource, used by Matrix_Vector_Activate_Batch
*
* \tparam N Number of MAC to be performed (equals to SIMD in mvau)
* \tparam T Accumulator datatype
* \tparam TC First operand datatype (weights)
* \tparam TD Second operand datatype (input)
* \tparam R Datatype for the resource used for FPGA implementation of the MAC - safely deducible from the paramaters
*
* \param a Initialization value of the accumulation
* \param c First operand (array of weights)
* \param d Second operand (array of input activation)
* \param r Resource type for the hardware implementation of the MAC block
*
* \return Result of the MAC operation
*/
template<unsigned N, typename T, typename TC, typename TD, typename R>
T mac(T const &a, TC const &c, TD const &d, R const &r) {
#pragma HLS inline
T res = a;
for(unsigned i = 0; i < N; i++) {
#pragma HLS unroll
res += mul(c[i], d[i], r);
}
return res;
}
template<unsigned N, typename T, typename TC, typename TD>
inline T mac(T const &a, TC const &c, TD const &d) {
#pragma HLS inline
return mac<N>(a, c, d, ap_resource_dflt());
}
#endif