forked from Xilinx/finn-hlslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivations.hpp
executable file
·205 lines (190 loc) · 7.3 KB
/
activations.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
/******************************************************************************
* 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 activations.hpp
*
* Library of templated HLS classes for BNN deployment.
* This file lists a set of classes used to implement
* threshold memory in neural network.
*
* 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.
*
*******************************************************************************/
#ifndef ACTIVATIONS_HPP
#define ACTIVATIONS_HPP
#include "interpret.hpp"
/**
* General contract for activation functions.
*
* This class itself has no formal significance for the implementation
* of the MVAU. Implementations of activation functions are encouraged
* to implement it nonetheless to guarantee appropriate function
* signatures.
*/
template<typename TA, typename TO>
class Activation {
public:
TA init(unsigned const nf, unsigned const pe) const {
#pragma HLS inline
return TA(0);
}
/**
* Compute the activation of the passed accumulator value accu in row idx.
*/
TO activate(unsigned const nf, unsigned const pe, TA const &accu) const;
};
/**
* A no-op activation that simply outputs the computed accumulator
* output as the final result.
*/
template<typename T>
class PassThroughActivation : public Activation<T, T> {
public:
T activate(unsigned const nf, unsigned const pe, T const &accu) const {
#pragma HLS inline
return accu;
}
};
/**
* Use a simple global threshold comparison as activation function.
*
* The constant threshold is initialized at construction.
* The default comparison returns true if the threshold value is
* smaller than the passed accumulator value.
*/
template<typename TA, typename Compare = std::less<TA>>
class ThresholdActivation : public Activation<TA, bool> {
TA const m_threshold;
public:
ThresholdActivation(TA const &threshold) : m_threshold(threshold) {
#pragma HLS inline
}
public:
bool activate(unsigned const nf, unsigned const pe, TA const &accu) const {
#pragma HLS inline
return Compare()(m_threshold, accu);
}
};
/**
* Use a simple per-row threshold comparison as activation function.
*
* The thresholds are taken from an array indexed by output row.
* It is currently public to allow direct initialization and
* to make its name accessible for top-level HLS pragmas.
*
* The default comparison returns true if the threshold value defined for
* the indexed row is smaller than the passed accumulator value.
*/
template<unsigned NF, unsigned PE, unsigned NumTH,
typename TA, typename TR, int ActVal = 0, typename Compare = std::less<TA>>
class ThresholdsActivation {
public:
TA m_thresholds[PE][NF][NumTH];
public:
TA init(unsigned const nf, unsigned const pe) const {
#pragma HLS inline
return TA(0);
}
public:
TR activate(unsigned const nf, unsigned const pe, TA const &accu) const {
#pragma HLS inline
TR result=ActVal;
for(unsigned int i=0; i< NumTH; i++){
#pragma HLS unroll
result+=Compare()(m_thresholds[pe][nf][i], accu);
}
return result;
}
};
/**
* \brief Thresholding function for multiple images
*
* The function performs thresholds comparison with input activation vector,
* and generating output based on the comparison results
*
* \tparam ImgDim Width and Heigth of the Input Feature Map (assumed square)
* \tparam NumChannels Heigth of the input matrix
* \tparam PE Number of output rows computed in parallel
* \tparam TSrcI DataType of the input activation (as used in the MAC)
* \tparam TDstI DataType of the output activation (as generated by the activation)
* \tparam TI DataType of the input stream - safely deducible from the paramaters
* \tparam TO DataType of the output stream - safely deducible from the paramaters
* \tparam TA DataType of the activation class (e.g. thresholds) - safely deducible from the paramaters
*
* \param in Input stream
* \param out Output stream
* \param activation Activation class
* \param reps Number of time the function has to be repeatedly executed (e.g. number of images)
*/
template <
unsigned ImgDim, unsigned NumChannels, unsigned PE,
typename TSrcI = Identity, typename TDstI = Identity,
typename TI, typename TO, typename TA>
void Thresholding_Batch(hls::stream<TI> &in,
hls::stream<TO> &out,
TA const &activation,
int const reps)
{
// how many different rows each neuron will compute
// alternatively: number of vertical matrix chunks
unsigned const NF = NumChannels / PE;
unsigned nf = 0;
unsigned tile = 0; // invariant: tile = nf*SF + sf
// everything merged into a common iteration space (one "big" loop instead
// of smaller nested loops) to get the pipelinening the way we want
for (unsigned i = 0; i < reps * ImgDim * ImgDim * NF; i++)
{
TI inElem;
inElem = in.read();
auto outElem = TDstI().template operator()<TO>();
for (unsigned pe = 0; pe < PE; pe++)
{
#pragma HLS UNROLL
auto const act = TSrcI()(inElem);
outElem(pe,0,1) = activation.activate(nf, pe, act(pe,0));
}
out.write(outElem);
if (++nf == NF)
{
nf = 0;
}
}
}
#endif