forked from Xilinx/finn-hlslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.hpp
228 lines (216 loc) · 7.2 KB
/
pool.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/******************************************************************************
* 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]>
* Tobias Alonso <[email protected]>
* \file pool.hpp
*
* This file defines the pool activations
*
******************************************************************************/
#ifndef POOL_HPP
#define POOL_HPP
#include "interpret.hpp"
/*!
* \brief PoolFunction: General contract for pool functions.
*
* This class itself has no formal significance for the implementation
* of the pool function. It provides a guidence for specific pool function to be used in Pool_batch
*
* \tparam TA Datatype of the internal accumulation in the pool function
* \tparam TO Datatype of the output generated by the pool function
* \tparam size Additional optional unsigned parameter to be used in pool or activate
*
*/
template<typename TA, typename TO, unsigned size>
class PoolFunction {
public:
/*!
* \brief init: initialization function returning the datatype for the accumulators
*/
TA init(void) const {
#pragma HLS inline
return TA(0);
}
/*!
* \brief pool: computes the pooling algorithm (e.g., max, avg, sum)
*
* \param input Input value to be used in the pool function
* \param accu Value already computed in previous iterations
*/
TA pool(TA const &input, TA const &accu) const;
/*!
* \brief activate: compute the output of pooling algorithm (e.g., max, avg, sum)
*
* \param accu Value already computed in previous iterations
*/
TO activate(TA const &accu) const;
};
/*!
* \brief MaxPoolFunction: Implementing max pool
*
* This class inherits from the generic Poolfunction to implement Max Pool
*
* \tparam T Datatype of the input value and the accu value containing the previously computed max
* \tparam size Unused
*
*/
template<typename T, unsigned size>
class MaxPoolFunction : public PoolFunction<T, T, size> {
public:
T init(void) const {
#pragma HLS inline
const T T_MIN_VAL = (T(-1)<0)? 1<<(T::width-1) : 0;
return T_MIN_VAL;
}
/*!
* \brief pool: computes the max value
*
* \param input Input value to be used in the max pool function
* \param accu Max value already computed in previous iterations
*/
T pool(T const &input, T const &accu) const{
#pragma HLS inline
return std::max(input,accu);
}
/*!
* \brief activate: compute the output of the max pooling algorithm
*
* \param accu Max value already computed and returned
*/
T activate(T const &accu) const {
#pragma HLS inline
return accu;
}
};
/*!
* \brief AvgPoolFunction: Implementing avg pool
*
* This class inherits from the generic Poolfunction to implement Average Pool
*
* \tparam TA Datatype of the internal accumulation in the avg pool function
* \tparam TO Datatype of the output generated by the avg pool function
* \tparam size Value used as divisor on the accumulator to generate output
*
*/
template<typename TA, typename TO, unsigned size>
class AvgPoolFunction : public PoolFunction<TA, TO, size> {
public:
/*!
* \brief pool: computes the sum
*
* \param input Input value to be used in the avg pool function
* \param accu Accumulation value already computed in previous iterations
*/
TA pool(TA const &input, TA const &accu) const{
#pragma HLS inline
return std::plus<TA>()(input,accu);
}
/*!
* \brief activate: compute the output of the avg pooling algorithm
*
* \param accu Accumulation value already computed in previous iterations
*/
TO activate(TA const &accu) const {
#pragma HLS inline
return (accu/size);
}
};
/*!
* \brief AccPoolFunction: Implementing accumulation pool
*
* This class inherits from the generic Poolfunction to implement accumulation Pool
*
* \tparam TA Datatype of the internal accumulation in the avg pool function
* \tparam size Unused
*
*/
template<typename TA, unsigned size>
class AccPoolFunction : public PoolFunction<TA, TA, size> {
public:
/*!
* \brief pool: computes the sum
*
* \param input Input value to be used in the avg pool function
* \param accu Accumulation value already computed in previous iterations
*/
TA pool(TA const &input, TA const &accu) const{
#pragma HLS inline
return std::plus<TA>()(input,accu);
}
/*!
* \brief activate: compute the output of the max pooling algorithm
*
* \param accu Accumulation value already computed in previous iterations
*/
TA activate(TA const &accu) const {
#pragma HLS inline
return accu;
}
};
/*!
* \brief QuantAvgPoolFunction: Implementing avg pool with shift instead of
* division
*
* This class inherits from the generic Poolfunction to implement Average Pool
* with shift instead of division
*
* \tparam TA Datatype of the internal accumulation in the quant avg pool function
* \tparam TO Datatype of the output generated by the quant avg pool function
* \tparam size Number of right shifts applied to generate output
*
*/
template<typename TA, typename TO, unsigned size>
class QuantAvgPoolFunction : public PoolFunction<TA, TO, size> {
public:
/*!
* \brief pool: computes the sum
*
* \param input Input value to be used in the avg pool function
* \param accu Accumulation value already computed in previous iterations
*/
TA pool(TA const &input, TA const &accu) const{
#pragma HLS inline
return input + accu;
}
/*!
* \brief activate: compute the output of the quant avg pooling algorithm
*
* \param accu Accumulation value already computed in previous iterations
*/
TO activate(TA const &accu) const {
#pragma HLS inline
return TO(accu>>size);
}
};
#endif