Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2020 Parichay Kapoor <pk.kapoor@samsung.com>
4 : *
5 : * @file layer.h
6 : * @date 14 October 2020
7 : * @see https://github.com/nnstreamer/nntrainer
8 : * @author Parichay Kapoor <pk.kapoor@samsung.com>
9 : * @author Debadri Samaddar <s.debadri@samsung.com>
10 : * @author Niket Agarwal <niket.a@samsung.com>
11 : * @bug No known bugs except for NYI items
12 : * @brief This is layers interface for c++ API
13 : *
14 : * @note This is experimental API and not stable.
15 : */
16 :
17 : #ifndef __ML_TRAIN_LAYER_H__
18 : #define __ML_TRAIN_LAYER_H__
19 :
20 : #if __cplusplus < MIN_CPP_VERSION
21 : #error "CPP versions c++17 or over are only supported"
22 : #endif // __cpluscplus
23 :
24 : #include <memory>
25 : #include <string>
26 : #include <tensor_dim.h>
27 : #include <vector>
28 :
29 : #include <common.h>
30 :
31 : namespace ml {
32 : namespace train {
33 :
34 : /**
35 : * @brief Enumeration of layer type
36 : */
37 : enum LayerType {
38 : LAYER_IN = ML_TRAIN_LAYER_TYPE_INPUT, /**< Input Layer type */
39 : LAYER_WEIGHT = ML_TRAIN_LAYER_TYPE_WEIGHT, /**< Weight Layer type */
40 : LAYER_TENSOR = ML_TRAIN_LAYER_TYPE_TENSOR, /**< Weight Layer type */
41 : LAYER_ADD = ML_TRAIN_LAYER_TYPE_ADD, /**< Add Layer type */
42 : LAYER_SUBTRACT = ML_TRAIN_LAYER_TYPE_SUBTRACT, /**< Subtract Layer type */
43 : LAYER_MULTIPLY = ML_TRAIN_LAYER_TYPE_MULTIPLY, /**< Multiply Layer type */
44 : LAYER_DIVIDE = ML_TRAIN_LAYER_TYPE_DIVIDE, /**< Divide Layer type */
45 : LAYER_POW = ML_TRAIN_LAYER_TYPE_POW, /**< Pow Layer type */
46 : LAYER_SQRT = ML_TRAIN_LAYER_TYPE_SQRT, /**< SQRT Layer type */
47 : LAYER_SINE = ML_TRAIN_LAYER_TYPE_SINE, /**< Sine Layer type */
48 : LAYER_COSINE = ML_TRAIN_LAYER_TYPE_COSINE, /**< Cosine Layer type */
49 : LAYER_TANGENT = ML_TRAIN_LAYER_TYPE_TANGENT, /**< Tangent Layer type */
50 : LAYER_MATMUL =
51 : ML_TRAIN_LAYER_TYPE_MATMUL, /**< Matrix multiplication Layer type */
52 : LAYER_CAST = ML_TRAIN_LAYER_TYPE_CAST, /**< Cast Layer type */
53 : LAYER_GATHER = ML_TRAIN_LAYER_TYPE_GATHER, /**< Gather Layer type */
54 : LAYER_SLICE = ML_TRAIN_LAYER_TYPE_SLICE, /**< Slice Layer type */
55 : LAYER_NEG = ML_TRAIN_LAYER_TYPE_NEG, /**< Negative Layer type */
56 : LAYER_FC = ML_TRAIN_LAYER_TYPE_FC, /**< Fully Connected Layer type */
57 : LAYER_SWIGLU = ML_TRAIN_LAYER_TYPE_SWIGLU, /**< Swiglu Layer type */
58 : LAYER_BN = ML_TRAIN_LAYER_TYPE_BN, /**< Batch Normalization Layer type */
59 : LAYER_CONV2D = ML_TRAIN_LAYER_TYPE_CONV2D, /**< Convolution 2D Layer type */
60 : LAYER_CONV2D_TRANSPOSE =
61 : ML_TRAIN_LAYER_TYPE_CONV2D_TRANSPOSE, /**< Convolution 2D Transpose Layer
62 : type */
63 : LAYER_POOLING2D = ML_TRAIN_LAYER_TYPE_POOLING2D, /**< Pooling 2D Layer type */
64 : LAYER_FLATTEN = ML_TRAIN_LAYER_TYPE_FLATTEN, /**< Flatten Layer type */
65 : LAYER_ACTIVATION =
66 : ML_TRAIN_LAYER_TYPE_ACTIVATION, /**< Activation Layer type */
67 : LAYER_ADDITION = ML_TRAIN_LAYER_TYPE_ADDITION, /**< Addition Layer type */
68 : LAYER_CONCAT = ML_TRAIN_LAYER_TYPE_CONCAT, /**< Concat Layer type */
69 : LAYER_MULTIOUT = ML_TRAIN_LAYER_TYPE_MULTIOUT, /**< Multi Output Layer type */
70 : LAYER_EMBEDDING = ML_TRAIN_LAYER_TYPE_EMBEDDING, /**< Embedding Layer type */
71 : LAYER_RNN = ML_TRAIN_LAYER_TYPE_RNN, /**< RNN Layer type */
72 : LAYER_LSTM = ML_TRAIN_LAYER_TYPE_LSTM, /**< LSTM Layer type */
73 : LAYER_SPLIT = ML_TRAIN_LAYER_TYPE_SPLIT, /**< Splite Layer type */
74 : LAYER_GRU = ML_TRAIN_LAYER_TYPE_GRU, /**< GRU Layer type */
75 : LAYER_PERMUTE = ML_TRAIN_LAYER_TYPE_PERMUTE, /**< Permute layer */
76 : LAYER_DROPOUT = ML_TRAIN_LAYER_TYPE_DROPOUT, /**< DropOut Layer type */
77 : LAYER_BACKBONE_NNSTREAMER =
78 : ML_TRAIN_LAYER_TYPE_BACKBONE_NNSTREAMER, /**< Backbone using NNStreamer */
79 : LAYER_CENTROID_KNN =
80 : ML_TRAIN_LAYER_TYPE_CENTROID_KNN, /**< Centroid KNN Layer */
81 : LAYER_CONV1D = ML_TRAIN_LAYER_TYPE_CONV1D, /**< Convolution 1D Layer type */
82 : LAYER_LSTMCELL = ML_TRAIN_LAYER_TYPE_LSTMCELL, /**< LSTM Cell Layer type */
83 : LAYER_GRUCELL = ML_TRAIN_LAYER_TYPE_GRUCELL, /**< GRU Cell Layer type */
84 : LAYER_RNNCELL = ML_TRAIN_LAYER_TYPE_RNNCELL, /**< RNN Cell Layer type */
85 : LAYER_ZONEOUT_LSTMCELL =
86 : ML_TRAIN_LAYER_TYPE_ZONEOUTLSTMCELL, /**< Zoneout LSTM Cell Layer type */
87 : LAYER_ATTENTION = ML_TRAIN_LAYER_TYPE_ATTENTION, /**< Attention Layer type */
88 : LAYER_MOL_ATTENTION =
89 : ML_TRAIN_LAYER_TYPE_MOL_ATTENTION, /**< MoL Attention Layer type */
90 : LAYER_MULTI_HEAD_ATTENTION =
91 : ML_TRAIN_LAYER_TYPE_MULTI_HEAD_ATTENTION, /**< Multi Head Attention Layer
92 : type */
93 : LAYER_LAYER_NORMALIZATION =
94 : ML_TRAIN_LAYER_TYPE_LAYER_NORMALIZATION, /**< Layer Normalization Layer type
95 : */
96 : LAYER_POSITIONAL_ENCODING =
97 : ML_TRAIN_LAYER_TYPE_POSITIONAL_ENCODING, /**< Positional Encoding Layer type
98 : */
99 : LAYER_IDENTITY = ML_TRAIN_LAYER_TYPE_IDENTITY, /**< Identity Layer type */
100 : LAYER_PREPROCESS_FLIP =
101 : ML_TRAIN_LAYER_TYPE_PREPROCESS_FLIP, /**< Preprocess flip Layer type */
102 : LAYER_PREPROCESS_TRANSLATE =
103 : ML_TRAIN_LAYER_TYPE_PREPROCESS_TRANSLATE, /**< Preprocess translate Layer
104 : type */
105 : LAYER_PREPROCESS_L2NORM =
106 : ML_TRAIN_LAYER_TYPE_PREPROCESS_L2NORM, /**< Preprocess l2norm Layer type */
107 : LAYER_LOSS_MSE =
108 : ML_TRAIN_LAYER_TYPE_LOSS_MSE, /**< Mean Squared Error Loss Layer type */
109 : LAYER_LOSS_CROSS_ENTROPY_SIGMOID =
110 : ML_TRAIN_LAYER_TYPE_LOSS_CROSS_ENTROPY_SIGMOID, /**< Cross Entropy with
111 : Sigmoid Loss Layer type
112 : */
113 : LAYER_LOSS_CROSS_ENTROPY_SOFTMAX =
114 : ML_TRAIN_LAYER_TYPE_LOSS_CROSS_ENTROPY_SOFTMAX, /**< Cross Entropy with
115 : Softmax Loss Layer type
116 : */
117 : LAYER_RMSNORM = ML_TRAIN_LAYER_TYPE_RMSNORM, /**<RMS NORM Layer */
118 : LAYER_TRANSPOSE = ML_TRAIN_LAYER_TYPE_TRANSPOSE, /**< Transpose Layer type */
119 : LAYER_CHANNEL_SHUFFLE =
120 : ML_TRAIN_LAYER_TYPE_CHANNEL_SHUFFLE, /**< Channel Shuffle Layer type */
121 : LAYER_REDUCE_SUM =
122 : ML_TRAIN_LAYER_TYPE_REDUCE_SUM, /**< Reduce sum Layer type */
123 : LAYER_REDUCE_MEAN =
124 : ML_TRAIN_LAYER_TYPE_REDUCE_MEAN, /**< Reduce mean Layer type */
125 : LAYER_RESHAPE = ML_TRAIN_LAYER_TYPE_RESHAPE, /**< Reshape Layer type */
126 : LAYER_UPSAMPLE2D =
127 : ML_TRAIN_LAYER_TYPE_UPSAMPLE2D, /**< Upsample 2D Layer type */
128 :
129 : LAYER_UNKNOWN = ML_TRAIN_LAYER_TYPE_UNKNOWN, /**< Unknown */
130 :
131 : LAYER_TIME_DIST =
132 : ML_TRAIN_LAYER_EXPERIMENTAL, /**< Time Distributed Layer type */
133 : LAYER_BACKBONE_TFLITE =
134 : ML_TRAIN_LAYER_EXPERIMENTAL + 1, /**< Backbone using TFLite */
135 : LAYER_LOSS_CONSTANT_DERIVATIVE =
136 : ML_TRAIN_LAYER_EXPERIMENTAL + 2, /**< Synthetic loss layer to feed
137 : constant derivative */
138 : };
139 :
140 : /**
141 : * @class Layer Base class for layers
142 : * @brief Base class for all layers
143 : */
144 : class Layer {
145 : public:
146 : /**
147 : * @brief Destructor of Layer Class
148 : */
149 : virtual ~Layer() = default;
150 :
151 : /**
152 : * @brief Get the layer type
153 : * @return const std::string type representation
154 : */
155 : virtual const std::string getType() const = 0;
156 :
157 : /**
158 : * @brief Get the layer type
159 : */
160 : virtual void initialize() = 0;
161 :
162 : /**
163 : * @brief Default allowed properties
164 : * - input shape : string
165 : * - bias zero : bool
166 : * - normalization : bool
167 : * - standardization : bool
168 : * - activation : string (type)
169 : * - epsilon : float
170 : * - weight_regularizer : string (type)
171 : * - weight_regularizer_constant : float
172 : * - unit : int
173 : * - weight_initializer : string (type)
174 : * - filter_size : int
175 : * - kernel_size : ( n , m )
176 : * - stride : ( n, m )
177 : * - padding : ( n, m )
178 : * - pool_size : ( n,m )
179 : * - pooling : max, average, global_max, global_average
180 : * - flatten : bool
181 : * - name : string (type)
182 : * - momentum : float,
183 : * - moving_mean_initializer : string (type),
184 : * - moving_variance_initializer : string (type),
185 : * - gamma_initializer : string (type),
186 : * - beta_initializer" : string (type)
187 : * - modelfile : model file for loading config for backbone layer
188 : * - input_layers : string (type)
189 : * - output_layers : string (type)
190 : * - trainable :
191 : * - flip_direction
192 : * - random_translate
193 : * - in_dim : int ( input dimension for embedding layer )
194 : * - out_dim : int ( output dimesion for embedding layer )
195 : * - recurrent_activation : string (type) - used only in lstm
196 : * - return_sequences : bool (type) - used only in lstm
197 : * - distribute : bool
198 : * - hidden_state_activation : string (type) - used only in lstm
199 : * - drop_out : float (type) - drop out rate
200 : */
201 : /**
202 : * @brief set Property of layer
203 : * @todo change the function signature
204 : * @param[in] values values of property
205 : * @retval #ML_ERROR_NONE Successful.
206 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
207 : * @details This function accepts vector of properties in the format -
208 : * { std::string property_name, void * property_val, ...}
209 : */
210 : virtual void setProperty(const std::vector<std::string> &values) = 0;
211 :
212 : /**
213 : * @brief Get property value of layer
214 : * @param[in] key Property key to retrieve
215 : * @retval std::string Property value as string
216 : * @retval Empty string if property not found
217 : * @details For layers derived from layer_impl: Property lookup is handled
218 : * automatically.
219 : * @details For layers derived from layer_devel: The getProperty() function
220 : * must be overridden to enable property retrieval.
221 : */
222 : virtual std::string getProperty(const std::string &key) = 0;
223 :
224 : /**
225 : * @brief Get name of the layer
226 : * @retval name of the layer
227 : * @note This name is unique to this layer in a model
228 : * @note This name might be changed once this layer is added to the model
229 : * to keep the name unique to the model
230 : */
231 : virtual const std::string getName() const = 0;
232 :
233 : /**
234 : * @brief Get the Weight object name
235 : *
236 : * @param idx Identifier of the weight
237 : * @return const std::string &Name of the weight
238 : */
239 : virtual const std::string &getWeightName(unsigned int idx) = 0;
240 :
241 : /**
242 : * @brief Get weight data of the layer
243 : * @retval weight data of the layer
244 : * @note nntrainer assign the vector and if there is no weights, the size
245 : * of vector is zero
246 : * @note layer needs to be finalized before called.
247 : */
248 : virtual const std::vector<float *> getWeights() = 0;
249 :
250 : /**
251 : * @brief Get weight data of the layer
252 : * @retval weights : float * arrary to store weight data
253 : * @retval weights_dim : TensorDim for each weights
254 : * @note nntrainer assign the vector and if there is no weights, the size
255 : * of vector is zero
256 : * @note layer needs to be finalized before called.
257 : */
258 : virtual void getWeights(std::vector<float *> &weights,
259 : std::vector<ml::train::TensorDim> &weights_dim) = 0;
260 :
261 : #ifdef ENABLE_FP16
262 : /**
263 : * @brief Get weight data of the layer
264 : * @retval weight data of the layer
265 : * @note nntrainer assign the vector and if there is no weights, the size
266 : * of vector is zero
267 : * @note layer needs to be finalized before called.
268 : */
269 : virtual const std::vector<_FP16 *> getFP16Weights() = 0;
270 :
271 : /**
272 : * @brief Get weight data of the layer
273 : * @retval weights : float * arrary to store weight data
274 : * @retval weights_dim : TensorDim for each weights
275 : * @note nntrainer assign the vector and if there is no weights, the size
276 : * of vector is zero
277 : * @note layer needs to be finalized before called.
278 : */
279 : virtual void
280 : getFP16Weights(std::vector<_FP16 *> &weights,
281 : std::vector<ml::train::TensorDim> &weights_dim) = 0;
282 : #endif
283 : /**
284 : * @brief Set weight data of the layer
285 : * @note Size of vector must be the same with number of weights.
286 : * @note layer needs to be finalized before called.
287 : */
288 : virtual void setWeights(const std::vector<float *>) = 0;
289 : };
290 :
291 : /**
292 : * @brief Factory creator with constructor for layer type
293 : */
294 : std::unique_ptr<Layer>
295 : createLayer(const LayerType &type,
296 : const std::vector<std::string> &properties = {});
297 :
298 : /**
299 : * @brief Factory creator with constructor for layer
300 : */
301 : std::unique_ptr<Layer>
302 : createLayer(const std::string &type,
303 : const std::vector<std::string> &properties = {});
304 :
305 : /**
306 : * @brief General Layer Factory function to register Layer
307 : *
308 : * @param props property representation
309 : * @return std::unique_ptr<ml::train::Layer> created object
310 : */
311 : template <typename T,
312 : std::enable_if_t<std::is_base_of<Layer, T>::value, T> * = nullptr>
313 : std::unique_ptr<Layer> createLayer(const std::vector<std::string> &props = {}) {
314 : std::unique_ptr<Layer> ptr = std::make_unique<T>();
315 :
316 : ptr->setProperty(props);
317 : return ptr;
318 : }
319 :
320 : /**
321 : * Aliases for various layers and losses
322 : */
323 : namespace layer {
324 :
325 : /**
326 : * @brief Helper function to create input layer
327 : */
328 : inline std::unique_ptr<Layer>
329 : Input(const std::vector<std::string> &properties = {}) {
330 7 : return createLayer(LayerType::LAYER_IN, properties);
331 : }
332 :
333 : /**
334 : * @brief Helper function to create weight layer
335 : */
336 : inline std::unique_ptr<Layer>
337 : WeightLayer(const std::vector<std::string> &properties = {}) {
338 1 : return createLayer(LayerType::LAYER_WEIGHT, properties);
339 : }
340 :
341 : /**
342 : * @brief Helper function to create add layer
343 : */
344 : inline std::unique_ptr<Layer>
345 : AddLayer(const std::vector<std::string> &properties = {}) {
346 1 : return createLayer(LayerType::LAYER_ADD, properties);
347 : }
348 :
349 : /**
350 : * @brief Helper function to create subtract layer
351 : */
352 : inline std::unique_ptr<Layer>
353 : SubtractLayer(const std::vector<std::string> &properties = {}) {
354 1 : return createLayer(LayerType::LAYER_SUBTRACT, properties);
355 : }
356 :
357 : /**
358 : * @brief Helper function to create multiply layer
359 : */
360 : inline std::unique_ptr<Layer>
361 : MultiplyLayer(const std::vector<std::string> &properties = {}) {
362 1 : return createLayer(LayerType::LAYER_MULTIPLY, properties);
363 : }
364 :
365 : /**
366 : * @brief Helper function to create divide layer
367 : */
368 : inline std::unique_ptr<Layer>
369 : DivideLayer(const std::vector<std::string> &properties = {}) {
370 : return createLayer(LayerType::LAYER_DIVIDE, properties);
371 : }
372 :
373 : /**
374 : * @brief Helper function to create pow layer
375 : */
376 : inline std::unique_ptr<Layer>
377 : PowLayer(const std::vector<std::string> &properties = {}) {
378 : return createLayer(LayerType::LAYER_POW, properties);
379 : }
380 :
381 : /**
382 : * @brief Helper function to create SQRT layer
383 : */
384 : inline std::unique_ptr<Layer>
385 : SQRTLayer(const std::vector<std::string> &properties = {}) {
386 : return createLayer(LayerType::LAYER_SQRT, properties);
387 : }
388 :
389 : /**
390 : * @brief Helper function to create sine layer
391 : */
392 : inline std::unique_ptr<Layer>
393 : SineLayer(const std::vector<std::string> &properties = {}) {
394 : return createLayer(LayerType::LAYER_SINE, properties);
395 : }
396 :
397 : /**
398 : * @brief Helper function to create cosine layer
399 : */
400 : inline std::unique_ptr<Layer>
401 : CosineLayer(const std::vector<std::string> &properties = {}) {
402 : return createLayer(LayerType::LAYER_COSINE, properties);
403 : }
404 :
405 : /**
406 : * @brief Helper function to create tangent layer
407 : */
408 : inline std::unique_ptr<Layer>
409 : TangentLayer(const std::vector<std::string> &properties = {}) {
410 : return createLayer(LayerType::LAYER_TANGENT, properties);
411 : }
412 :
413 : /**
414 : * @brief Helper function to create matmul layer
415 : */
416 : inline std::unique_ptr<Layer>
417 : MatMulLayer(const std::vector<std::string> &properties = {}) {
418 : return createLayer(LayerType::LAYER_MATMUL, properties);
419 : }
420 :
421 : /**
422 : * @brief Helper function to create cast layer
423 : */
424 : inline std::unique_ptr<Layer>
425 : CastLayer(const std::vector<std::string> &properties = {}) {
426 : return createLayer(LayerType::LAYER_CAST, properties);
427 : }
428 :
429 : /**
430 : * @brief Helper function to create gather layer
431 : */
432 : inline std::unique_ptr<Layer>
433 : GatherLayer(const std::vector<std::string> &properties = {}) {
434 : return createLayer(LayerType::LAYER_GATHER, properties);
435 : }
436 :
437 : /**
438 : * @brief Helper function to create Slice layer
439 : */
440 : inline std::unique_ptr<Layer>
441 : SliceLayer(const std::vector<std::string> &properties = {}) {
442 : return createLayer(LayerType::LAYER_SLICE, properties);
443 : }
444 :
445 : /**
446 : * @brief Helper function to create Negative layer
447 : */
448 : inline std::unique_ptr<Layer>
449 : NegativeLayer(const std::vector<std::string> &properties = {}) {
450 : return createLayer(LayerType::LAYER_NEG, properties);
451 : }
452 :
453 : /**
454 : * @brief Helper function to create fully connected layer
455 : */
456 : inline std::unique_ptr<Layer>
457 : FullyConnected(const std::vector<std::string> &properties = {}) {
458 10 : return createLayer(LayerType::LAYER_FC, properties);
459 : }
460 :
461 : /**
462 : * @brief Helper function to create Swiglu layer
463 : */
464 : inline std::unique_ptr<Layer>
465 : Swiglu(const std::vector<std::string> &properties = {}) {
466 : return createLayer(LayerType::LAYER_SWIGLU, properties);
467 : }
468 :
469 : // /**
470 : // * @brief Helper function to create RMS normalization layer for GPU
471 : // */
472 : // inline std::unique_ptr<Layer>
473 : // RMSNormCl(const std::vector<std::string> &properties = {},
474 : // const LayerComputeEngine &compute_engine =
475 : // LayerComputeEngine::GPU)
476 : // {
477 : // return createLayer(LayerType::LAYER_RMSNORM, properties, compute_engine);
478 : // }
479 :
480 : /**
481 : * @brief Helper function to create Transpose layer
482 : */
483 : inline std::unique_ptr<Layer>
484 : Transpose(const std::vector<std::string> &properties = {}) {
485 : return createLayer(LayerType::LAYER_TRANSPOSE, properties);
486 : }
487 :
488 : /**
489 : * @brief Helper function to create Channel Shuffle layer
490 : */
491 : inline std::unique_ptr<Layer>
492 : ChannelShuffle(const std::vector<std::string> &properties = {}) {
493 : return createLayer(LayerType::LAYER_CHANNEL_SHUFFLE, properties);
494 : }
495 :
496 : /**
497 : * @brief Helper function to create batch normalization layer
498 : */
499 : inline std::unique_ptr<Layer>
500 : BatchNormalization(const std::vector<std::string> &properties = {}) {
501 1 : return createLayer(LayerType::LAYER_BN, properties);
502 : }
503 :
504 : /**
505 : * @brief Helper function to create layer normalization layer
506 : */
507 : inline std::unique_ptr<Layer>
508 : LayerNormalization(const std::vector<std::string> &properties = {}) {
509 : return createLayer(LayerType::LAYER_LAYER_NORMALIZATION, properties);
510 : }
511 :
512 : /**
513 : * @brief Helper function to create convolution 2d layer
514 : */
515 : inline std::unique_ptr<Layer>
516 : Convolution2D(const std::vector<std::string> &properties = {}) {
517 1 : return createLayer(LayerType::LAYER_CONV2D, properties);
518 : }
519 :
520 : /**
521 : * @brief Helper function to create convolution 1d layer
522 : */
523 : inline std::unique_ptr<Layer>
524 : Convolution1D(const std::vector<std::string> &properties = {}) {
525 : return createLayer(LayerType::LAYER_CONV1D, properties);
526 : }
527 :
528 : /**
529 : * @brief Helper function to create pooling 2d layer
530 : */
531 : inline std::unique_ptr<Layer>
532 : Pooling2D(const std::vector<std::string> &properties = {}) {
533 1 : return createLayer(LayerType::LAYER_POOLING2D, properties);
534 : }
535 :
536 : /**
537 : * @brief Helper function to create flatten layer
538 : */
539 : inline std::unique_ptr<Layer>
540 : Flatten(const std::vector<std::string> &properties = {}) {
541 1 : return createLayer(LayerType::LAYER_FLATTEN, properties);
542 : }
543 :
544 : /**
545 : * @brief Helper function to create reshape layer
546 : */
547 : inline std::unique_ptr<Layer>
548 : Reshape(const std::vector<std::string> &properties = {}) {
549 : return createLayer(LayerType::LAYER_RESHAPE, properties);
550 : }
551 :
552 : /**
553 : * @brief Helper function to create addition layer
554 : */
555 : inline std::unique_ptr<Layer>
556 : Addition(const std::vector<std::string> &properties = {}) {
557 1 : return createLayer(LayerType::LAYER_ADDITION, properties);
558 : }
559 :
560 : /**
561 : * @brief Helper function to create concat layer
562 : */
563 : inline std::unique_ptr<Layer>
564 : Concat(const std::vector<std::string> &properties = {}) {
565 1 : return createLayer(LayerType::LAYER_CONCAT, properties);
566 : }
567 :
568 : /**
569 : * @brief Helper function to create multi-out layer
570 : */
571 : inline std::unique_ptr<Layer>
572 : MultiOut(const std::vector<std::string> &properties = {}) {
573 1 : return createLayer(LayerType::LAYER_MULTIOUT, properties);
574 : }
575 :
576 : /**
577 : * @brief Helper function to create nnstreamer backbone layer
578 : */
579 : inline std::unique_ptr<Layer>
580 : BackboneNNStreamer(const std::vector<std::string> &properties = {}) {
581 : return createLayer(LayerType::LAYER_BACKBONE_NNSTREAMER, properties);
582 : }
583 :
584 : /**
585 : * @brief Helper function to create tflite backbone layer
586 : */
587 : inline std::unique_ptr<Layer>
588 : BackboneTFLite(const std::vector<std::string> &properties = {}) {
589 2 : return createLayer(LayerType::LAYER_BACKBONE_TFLITE, properties);
590 : }
591 :
592 : /**
593 : * @brief Helper function to create Embedding layer
594 : */
595 : inline std::unique_ptr<Layer>
596 : Embedding(const std::vector<std::string> &properties = {}) {
597 : return createLayer(LayerType::LAYER_EMBEDDING, properties);
598 : }
599 :
600 : /**
601 : * @brief Helper function to create RNN layer
602 : */
603 : inline std::unique_ptr<Layer>
604 : RNN(const std::vector<std::string> &properties = {}) {
605 : return createLayer(LayerType::LAYER_RNN, properties);
606 : }
607 :
608 : /**
609 : * @brief Helper function to create RNNCell layer
610 : */
611 : inline std::unique_ptr<Layer>
612 : RNNCell(const std::vector<std::string> &properties = {}) {
613 : return createLayer(LayerType::LAYER_RNNCELL, properties);
614 : }
615 :
616 : /**
617 : * @brief Helper function to create LSTM layer
618 : */
619 : inline std::unique_ptr<Layer>
620 : LSTM(const std::vector<std::string> &properties = {}) {
621 : return createLayer(LayerType::LAYER_LSTM, properties);
622 : }
623 :
624 : /**
625 : * @brief Helper function to create LSTMCell layer
626 : */
627 : inline std::unique_ptr<Layer>
628 : LSTMCell(const std::vector<std::string> &properties = {}) {
629 : return createLayer(LayerType::LAYER_LSTMCELL, properties);
630 : }
631 :
632 : /**
633 : * @brief Helper function to create ZoneoutLSTMCell layer
634 : */
635 : inline std::unique_ptr<Layer>
636 : ZoneoutLSTMCell(const std::vector<std::string> &properties = {}) {
637 : return createLayer(LayerType::LAYER_ZONEOUT_LSTMCELL, properties);
638 : }
639 :
640 : /**
641 : * @brief Helper function to create GRU layer
642 : */
643 : inline std::unique_ptr<Layer>
644 : GRU(const std::vector<std::string> &properties = {}) {
645 : return createLayer(LayerType::LAYER_GRU, properties);
646 : }
647 :
648 : /**
649 : * @brief Helper function to create GRUCell layer
650 : */
651 : inline std::unique_ptr<Layer>
652 : GRUCell(const std::vector<std::string> &properties = {}) {
653 : return createLayer(LayerType::LAYER_GRUCELL, properties);
654 : }
655 :
656 : /**
657 : * @brief Helper function to create DropOut layer
658 : */
659 : inline std::unique_ptr<Layer>
660 : DropOut(const std::vector<std::string> &properties = {}) {
661 : return createLayer(LayerType::LAYER_DROPOUT, properties);
662 : }
663 :
664 : /**
665 : * @brief Helper function to create Time Distributed layer
666 : */
667 : inline std::unique_ptr<Layer>
668 : TimeDistLayer(const std::vector<std::string> &properties = {}) {
669 : return createLayer(LayerType::LAYER_TIME_DIST, properties);
670 : }
671 :
672 : /**
673 : * @brief Helper function to create Centroid KNN Layer
674 : */
675 : inline std::unique_ptr<Layer>
676 : CentroidKNN(const std::vector<std::string> &properties = {}) {
677 : return createLayer(LayerType::LAYER_CENTROID_KNN, properties);
678 : }
679 :
680 : /**
681 : * @brief Helper function to create Attention Layer
682 : */
683 : inline std::unique_ptr<Layer>
684 : Attention(const std::vector<std::string> &properties = {}) {
685 : return createLayer(LayerType::LAYER_ATTENTION, properties);
686 : }
687 :
688 : /**
689 : * @brief Helper function to create MoL Attention Layer
690 : */
691 : inline std::unique_ptr<Layer>
692 : MoLAttention(const std::vector<std::string> &properties = {}) {
693 1 : return createLayer(LayerType::LAYER_MOL_ATTENTION, properties);
694 : }
695 :
696 : /**
697 : * @brief Helper function to create Multi Head Attention Layer
698 : */
699 : inline std::unique_ptr<Layer>
700 : MultiHeadAttention(const std::vector<std::string> &properties = {}) {
701 : return createLayer(LayerType::LAYER_MULTI_HEAD_ATTENTION, properties);
702 : }
703 :
704 : /**
705 : * @brief Helper function to create Positional Encoding Layer
706 : */
707 : inline std::unique_ptr<Layer>
708 : PositionalEncoding(const std::vector<std::string> &properties = {}) {
709 : return createLayer(LayerType::LAYER_POSITIONAL_ENCODING, properties);
710 : }
711 :
712 : /**
713 : * @brief Helper function to create Permute Layer
714 : */
715 : inline std::unique_ptr<Layer>
716 : Permute(const std::vector<std::string> &properties = {}) {
717 : return createLayer(LayerType::LAYER_PERMUTE, properties);
718 : }
719 :
720 : /**
721 : * @brief Helper function to create Reduce Mean Layer
722 : */
723 : inline std::unique_ptr<Layer>
724 : ReduceMean(const std::vector<std::string> &properties = {}) {
725 1 : return createLayer(LayerType::LAYER_REDUCE_MEAN, properties);
726 : }
727 :
728 : /**
729 : * @brief Helper function to create Reduce Sum Layer
730 : */
731 : inline std::unique_ptr<Layer>
732 : ReduceSum(const std::vector<std::string> &properties = {}) {
733 : return createLayer(LayerType::LAYER_REDUCE_SUM, properties);
734 : }
735 :
736 : /**
737 : * @brief Helper function to create Identity layer
738 : */
739 : inline std::unique_ptr<Layer>
740 : Identity(const std::vector<std::string> &properties = {}) {
741 : return createLayer(LayerType::LAYER_IDENTITY, properties);
742 : }
743 :
744 : /**
745 : * @brief Helper function to create Upsample2d layer
746 : */
747 : inline std::unique_ptr<Layer>
748 : Upsample2D(const std::vector<std::string> &properties = {}) {
749 : return createLayer(LayerType::LAYER_UPSAMPLE2D, properties);
750 : }
751 :
752 : /**
753 : * @brief Helper function to create activation layer
754 : */
755 : inline std::unique_ptr<Layer>
756 4 : Activation(const std::string &act,
757 : const std::vector<std::string> &properties = {}) {
758 4 : std::vector<std::string> props(properties);
759 4 : props.push_back(act);
760 8 : return createLayer(LayerType::LAYER_ACTIVATION, props);
761 4 : }
762 :
763 : /**
764 : * @brief Helper function to create ReLU activation layer
765 : */
766 : inline std::unique_ptr<Layer>
767 1 : ReLU(const std::vector<std::string> &properties = {}) {
768 2 : return Activation("Activation=relu", properties);
769 : }
770 :
771 : /**
772 : * @brief Helper function to create swish activation layer
773 : */
774 : inline std::unique_ptr<Layer>
775 : Swish(const std::vector<std::string> &properties = {}) {
776 : return Activation("Activation=swish", properties);
777 : }
778 :
779 : /**
780 : * @brief Helper function to create gelu activation layer
781 : */
782 : inline std::unique_ptr<Layer>
783 : GeLU(const std::vector<std::string> &properties = {}) {
784 : return Activation("Activation=gelu", properties);
785 : }
786 :
787 : /**
788 : * @brief Helper function to create Tanh layer
789 : */
790 : inline std::unique_ptr<Layer>
791 1 : Tanh(const std::vector<std::string> &properties = {}) {
792 2 : return Activation("Activation=tanh", properties);
793 : }
794 :
795 : /**
796 : * @brief Helper function to create sigmoid layer
797 : */
798 : inline std::unique_ptr<Layer>
799 1 : Sigmoid(const std::vector<std::string> &properties = {}) {
800 2 : return Activation("Activation=sigmoid", properties);
801 : }
802 :
803 : /**
804 : * @brief Helper function to create softmax layer
805 : */
806 : inline std::unique_ptr<Layer>
807 1 : Softmax(const std::vector<std::string> &properties = {}) {
808 2 : return Activation("Activation=softmax", properties);
809 : }
810 :
811 : /**
812 : * @brief Helper function to create elu activation layer
813 : */
814 : inline std::unique_ptr<Layer>
815 : ELU(const std::vector<std::string> &properties = {}) {
816 : return Activation("Activation=elu", properties);
817 : }
818 :
819 : /**
820 : * @brief Helper function to create selu activation layer
821 : */
822 : inline std::unique_ptr<Layer>
823 : SELU(const std::vector<std::string> &properties = {}) {
824 : return Activation("Activation=selu", properties);
825 : }
826 :
827 : /**
828 : * @brief Helper function to create mish activation layer
829 : */
830 : inline std::unique_ptr<Layer>
831 : Mish(const std::vector<std::string> &properties = {}) {
832 : return Activation("Activation=mish", properties);
833 : }
834 :
835 : } // namespace layer
836 :
837 : namespace loss {
838 : /**
839 : * @brief Helper function to create mse layer
840 : */
841 : inline std::unique_ptr<Layer>
842 : MSE(const std::vector<std::string> &properties = {}) {
843 1 : return createLayer(LayerType::LAYER_LOSS_MSE, properties);
844 : }
845 :
846 : /**
847 : * @brief Helper function to create cross entropy with sigmoid layer
848 : */
849 : inline std::unique_ptr<Layer>
850 : CrossEntropySigmoid(const std::vector<std::string> &properties = {}) {
851 1 : return createLayer(LayerType::LAYER_LOSS_CROSS_ENTROPY_SIGMOID, properties);
852 : }
853 :
854 : /**
855 : * @brief Helper function to create cross entropy with softmax layer
856 : */
857 : inline std::unique_ptr<Layer>
858 : CrossEntropySoftmax(const std::vector<std::string> &properties = {}) {
859 1 : return createLayer(LayerType::LAYER_LOSS_CROSS_ENTROPY_SOFTMAX, properties);
860 : }
861 :
862 : } // namespace loss
863 :
864 : } // namespace train
865 : } // namespace ml
866 : #endif // __ML_TRAIN_LAYER_H__
|