SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
backend.h
1 #ifndef _CORE_BACKEND_H_
2 #define _CORE_BACKEND_H_
3 
4 #include <string>
5 
6 #include "smaug/core/datatypes.h"
7 #include "smaug/utility/utils.h"
8 
9 // These are compile-time switches that selectively build a copy of SMAUG with
10 // a particular backend.
11 #define REFERENCE 0
12 #define SMVBACKEND 1
13 
17 namespace smaug {
18 
24  Reference = REFERENCE,
26  Smv = SMVBACKEND,
29 };
30 
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 class Workspace;
33 template <typename Backend> class ConvolutionOp;
34 template <typename Backend> class DataOp;
35 template <typename Backend> class DepthwiseConvolutionOp;
36 template <typename Backend> class MaxPoolingOp;
37 template <typename Backend> class AvgPoolingOp;
38 template <typename Backend> class InnerProductOp;
39 template <typename Backend> class SoftmaxOp;
40 template <typename Backend> class ReorderOp;
41 template <typename Backend> class ConcatOp;
42 template <typename Backend> class SplitOp;
43 template <typename Backend> class ReshapeOp;
44 template <typename Backend> class RepeatOp;
45 template <typename Backend> class FlattenOp;
46 template <typename Backend> class BatchNormOp;
47 template <typename Backend> class EltwiseAddOp;
48 template <typename Backend> class EltwiseMulOp;
49 template <typename Backend> class LessOp;
50 template <typename Backend> class LessEqualOp;
51 template <typename Backend> class GreaterOp;
52 template <typename Backend> class GreaterEqualOp;
53 template <typename Backend> class SwitchOp;
54 template <typename Backend> class MergeOp;
55 template <typename Backend> class ReluOp;
56 template <typename Backend> class SigmoidOp;
57 template <typename Backend> class EluOp;
58 template <typename Backend> class SeluOp;
59 template <typename Backend> class TanhOp;
60 template <typename Backend> class HardTanhOp;
61 template <typename Backend> class PaddingOp;
62 
63 #endif
64 
68 namespace ref {
69 extern const unsigned kConvolutionHw;
70 extern const unsigned kInnerProductHw;
71 extern const unsigned kEltwiseOpHw;
72 extern const unsigned kBatchNormHw;
73 extern const unsigned kPoolingHw;
74 } // namespace ref
75 
84 
85 #define DECL_CREATE_OP(OpType) \
86  static smaug::OpType<ReferenceBackend>* create##OpType( \
87  const std::string& name, Workspace* workspace)
88 
89  public:
90  static const int Alignment = 0;
91  static const bool PrecomputeBNVariance = true;
92  static const bool TransposeFCWeights = false;
93  static const std::string Name;
94  static const DataLayout DefaultInputDataLayout = DataLayout::NCHW;
95 
96  static int SpadSize() { return 0; }
97  static void initGlobals() {}
98  static void freeGlobals() {}
99 
100  DECL_CREATE_OP(ConvolutionOp);
101  DECL_CREATE_OP(DataOp);
102  DECL_CREATE_OP(DepthwiseConvolutionOp);
103  DECL_CREATE_OP(MaxPoolingOp);
104  DECL_CREATE_OP(AvgPoolingOp);
105  DECL_CREATE_OP(InnerProductOp);
106  DECL_CREATE_OP(SoftmaxOp);
107  DECL_CREATE_OP(ReorderOp);
108  DECL_CREATE_OP(ConcatOp);
109  DECL_CREATE_OP(SplitOp);
110  DECL_CREATE_OP(ReshapeOp);
111  DECL_CREATE_OP(RepeatOp);
112  DECL_CREATE_OP(FlattenOp);
113  DECL_CREATE_OP(BatchNormOp);
114  DECL_CREATE_OP(EltwiseAddOp);
115  DECL_CREATE_OP(EltwiseMulOp);
116  DECL_CREATE_OP(LessOp);
117  DECL_CREATE_OP(LessEqualOp);
118  DECL_CREATE_OP(GreaterOp);
119  DECL_CREATE_OP(GreaterEqualOp);
120  DECL_CREATE_OP(SwitchOp);
121  DECL_CREATE_OP(MergeOp);
122  DECL_CREATE_OP(ReluOp);
123  DECL_CREATE_OP(SigmoidOp);
124  DECL_CREATE_OP(EluOp);
125  DECL_CREATE_OP(SeluOp);
126  DECL_CREATE_OP(TanhOp);
127  DECL_CREATE_OP(HardTanhOp);
128  DECL_CREATE_OP(PaddingOp);
129 
130 #undef DECL_CREATE_OP
131 };
132 
136 namespace smv {
137 extern int kSpadSize;
138 extern const unsigned kConvolutionHw;
139 extern const unsigned kInnerProductHw;
140 extern const unsigned kEltwiseOpHw;
141 extern const unsigned kBatchNormHw;
142 extern const unsigned kPoolingHw;
143 extern const unsigned kSystolicArrayHw;
144 // Note that these naked pointers are never to be used except when invoking the
145 // kernels themselves.
146 extern float* spad0;
147 extern float* spad1;
148 extern float* spad2;
149 } // namespace smv
150 
151 #ifndef DOXYGEN_SHOULD_SKIP_THIS
152 class SmvConvolutionOp;
153 class SmvInnerProductOp;
154 class SmvMaxPoolingOp;
155 class SmvAvgPoolingOp;
156 class SmvBatchNormOp;
157 class SmvReluOp;
158 class SmvEluOp;
159 class SmvSeluOp;
160 class SmvTanhOp;
161 class SmvHardTanhOp;
162 class SmvSigmoidOp;
163 class SmvSoftmaxOp;
164 class SmvEltwiseAddOp;
165 class SmvEltwiseMulOp;
166 class SmvLessOp;
167 class SmvLessEqualOp;
168 class SmvGreaterOp;
169 class SmvGreaterEqualOp;
170 #endif
171 
178 class SmvBackend {
179 
180 // The difference between DECL_CREATE_OP and DECL_CREATE_SMV_OP is the latter is
181 // used for a custom operator implementation for the SMV backend, which is not
182 // limited to explicit template specializations.
183 #define DECL_CREATE_OP(OpType) \
184  static smaug::OpType<SmvBackend>* create##OpType( \
185  const std::string& name, Workspace* workspace)
186 #define DECL_CREATE_SMV_OP(OpType) \
187  static smaug::Smv##OpType* create##OpType( \
188  const std::string& name, Workspace* workspace)
189 
190  public:
191  static const int Alignment = 8;
192  static const bool PrecomputeBNVariance = true;
193  static const bool TransposeFCWeights = true;
194  static const std::string Name;
195  static const DataLayout DefaultInputDataLayout = DataLayout::NHWC;
196 
197  static int SpadSize() { return smv::kSpadSize; }
198  static void initGlobals() {
199  // kSpadSize is in terms of float16 data.
200  smv::kSpadSize = 32 * 1024;
201  // In SMV, all tensors store float16 data, but due to the modelling
202  // restriction of Aladdin, we actually store float32 data in the
203  // scratchpads. This why the allocated memory size here is double
204  // kSpadSize.
205  smv::spad0 = (float*)malloc_aligned(smv::kSpadSize * 2);
206  smv::spad1 = (float*)malloc_aligned(smv::kSpadSize * 2);
207  smv::spad2 = (float*)malloc_aligned(smv::kSpadSize * 2);
208  }
209  static void freeGlobals() {
210  free(smv::spad0);
211  free(smv::spad1);
212  free(smv::spad2);
213  }
214 
215  DECL_CREATE_SMV_OP(ConvolutionOp);
216  DECL_CREATE_SMV_OP(InnerProductOp);
217  DECL_CREATE_SMV_OP(MaxPoolingOp);
218  DECL_CREATE_SMV_OP(AvgPoolingOp);
219  DECL_CREATE_SMV_OP(BatchNormOp);
220  DECL_CREATE_SMV_OP(ReluOp);
221  DECL_CREATE_SMV_OP(EluOp);
222  DECL_CREATE_SMV_OP(SeluOp);
223  DECL_CREATE_SMV_OP(TanhOp);
224  DECL_CREATE_SMV_OP(HardTanhOp);
225  DECL_CREATE_SMV_OP(SigmoidOp);
226  DECL_CREATE_SMV_OP(SoftmaxOp);
227  DECL_CREATE_SMV_OP(EltwiseAddOp);
228  DECL_CREATE_SMV_OP(EltwiseMulOp);
229  DECL_CREATE_SMV_OP(LessOp);
230  DECL_CREATE_SMV_OP(LessEqualOp);
231  DECL_CREATE_SMV_OP(GreaterOp);
232  DECL_CREATE_SMV_OP(GreaterEqualOp);
233  DECL_CREATE_OP(DataOp);
234  DECL_CREATE_OP(DepthwiseConvolutionOp);
235  DECL_CREATE_OP(ReorderOp);
236  DECL_CREATE_OP(ConcatOp);
237  DECL_CREATE_OP(SplitOp);
238  DECL_CREATE_OP(ReshapeOp);
239  DECL_CREATE_OP(RepeatOp);
240  DECL_CREATE_OP(FlattenOp);
241  DECL_CREATE_OP(SwitchOp);
242  DECL_CREATE_OP(MergeOp);
243  DECL_CREATE_OP(PaddingOp);
244 
245 #undef DECL_SMV_OP
246 #undef DECL_CREATE_OP
247 };
248 
249 } // namespace smaug
250 
251 #endif
smaug::RepeatOp
Replicates a Tensor's data among all dimensions.
Definition: backend.h:44
smaug::ConcatOp
Concatenates N Tensors along a specified axis.
Definition: backend.h:41
smaug::BackendName
BackendName
The list of all hardware backends in the system.
Definition: backend.h:22
smaug::EltwiseAddOp
Adds two Tensors elementwise.
Definition: backend.h:47
smaug::SmvBackend
SmvBackend implements a set of models of optimized DL kernels that were taped out on a machine learni...
Definition: backend.h:178
smaug::LessEqualOp
Implements an elementwise less-than-or-equal-to operator.
Definition: backend.h:50
smaug::SigmoidOp
Implements the sigmoid operator, defined as 1/(1 + exp(-input)).
Definition: backend.h:56
smaug::SoftmaxOp
Implements the softmax operator.
Definition: backend.h:39
smaug::malloc_aligned
void * malloc_aligned(size_t size, bool zeroOut)
Return heap-allocated cacheline-aligned memory.
Definition: utils.cpp:9
smaug::Smv
@ Smv
SMV backend.
Definition: backend.h:26
smaug::SeluOp
Implements the scaled exponential linear unit function.
Definition: backend.h:58
smaug::HardTanhOp
Implements the hard tanh operator, which bounds the min and max value of the tanh operator.
Definition: backend.h:60
smaug::InnerProductOp
Implements the inner product operator.
Definition: backend.h:38
smaug::TanhOp
Implements the tanh operator.
Definition: backend.h:59
smaug::SplitOp
Implements the split operator, which divides a Tensor into N output Tensors along a specified dimensi...
Definition: backend.h:42
smaug::SwitchOp
Conditionally forwards an input to one of two outputs.
Definition: backend.h:53
smaug::LessOp
Implements an elementwise less-than operator.
Definition: backend.h:49
smaug::Reference
@ Reference
Reference backend.
Definition: backend.h:24
smaug::DepthwiseConvolutionOp
Implements the depthwise convolution operator.
Definition: backend.h:35
smaug::ReferenceBackend
ReferenceBackend provides reference implementations of all operators supported by SMAUG.
Definition: backend.h:83
smaug::BatchNormOp
Implements the batch normalization layer.
Definition: backend.h:46
smaug::GreaterOp
Implements an elementwise greater than operator.
Definition: backend.h:51
smaug::MaxPoolingOp
Implements the max-pooling operator.
Definition: backend.h:36
smaug::EluOp
Implements the exponential linear unit function.
Definition: backend.h:57
smaug::ReluOp
Implements the rectified linear unit operator: max(slope * x, 0).
Definition: backend.h:55
smaug
The smaug namespace is the parent namespace of all C++ code in SMAUG.
Definition: backend.cpp:38
smaug::AvgPoolingOp
Implements the arithmetic-average-pooling operator.
Definition: backend.h:37
smaug::MergeOp
Forwards the first live input to its output.
Definition: backend.h:54
smaug::DataOp
Exposes a Tensor as its only output.
Definition: backend.h:34
smaug::EltwiseMulOp
Multiplies two Tensors elementwise.
Definition: backend.h:48
smaug::PaddingOp
Pad a given tensor in any number of dimensions with arbitrary size.
Definition: backend.h:61
smaug::ConvolutionOp
The base class for all 4D spatial convolution operators.
Definition: backend.h:33
smaug::GreaterEqualOp
Implements an elementwise greater than or equal to operator.
Definition: backend.h:52
smaug::FlattenOp
Flattens each batch of a Tensor.
Definition: backend.h:45
smaug::ReorderOp
Implements a Tensor reordering operation to convert between different DataLayouts.
Definition: backend.h:40
smaug::UnknownBackend
@ UnknownBackend
Invalid backend.
Definition: backend.h:28
smaug::ReshapeOp
Changes the Tensor's shape while retaining the number of elements.
Definition: backend.h:43