Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2022 Hyeonseok Lee <hs89.lee@samsung.com>
4 : *
5 : * @file positional_encoding_layer.h
6 : * @date 16 August 2022
7 : * @brief This file contains the positional encoding layer in transformer
8 : * @see https://github.com/nnstreamer/nntrainer
9 : * https://arxiv.org/abs/1607.06450
10 : * @author Hyeonseok Lee <hs89.lee@samsung.com>
11 : * @bug No known bugs except for NYI items
12 : *
13 : */
14 :
15 : #ifndef __POSITIONAL_ENCODING_LAYER_H__
16 : #define __POSITIONAL_ENCODING_LAYER_H__
17 : #ifdef __cplusplus
18 :
19 : #include <base_properties.h>
20 : #include <layer_context.h>
21 : #include <layer_devel.h>
22 : #include <node_exporter.h>
23 :
24 : namespace nntrainer {
25 :
26 : /**
27 : * @class Positional encoding Layer
28 : * @brief Implementation of positional encoding layer which is described in
29 : * paper "Attention is all you need"
30 : */
31 : class PositionalEncodingLayer : public Layer {
32 : public:
33 : /**
34 : * @brief Constructor of PositionalEncodingLayer
35 : */
36 : PositionalEncodingLayer();
37 :
38 : /**
39 : * @brief Destructor of PositionalEncodingLayer
40 : */
41 : ~PositionalEncodingLayer();
42 :
43 : /**
44 : * @brief Move constructor of PositionalEncodingLayer.
45 : * @param[in] PositionalEncodingLayer &&
46 : */
47 : PositionalEncodingLayer(PositionalEncodingLayer &&rhs) noexcept = default;
48 :
49 : /**
50 : * @brief Move assignment operator.
51 : * @parma[in] rhs PositionalEncodingLayer to be moved.
52 : */
53 : PositionalEncodingLayer &operator=(PositionalEncodingLayer &&rhs) = default;
54 :
55 : /**
56 : * @copydoc Layer::finalize(InitLayerContext &context)
57 : */
58 : void finalize(InitLayerContext &context) override;
59 :
60 : /**
61 : * @copydoc Layer::forwarding(RunLayerContext &context, bool training)
62 : */
63 : void forwarding(RunLayerContext &context, bool training) override;
64 :
65 : /**
66 : * @copydoc Layer::calcDerivative(RunLayerContext &context)
67 : */
68 : void calcDerivative(RunLayerContext &context) override;
69 :
70 : /**
71 : * @copydoc bool supportBackwarding() const
72 : */
73 2 : bool supportBackwarding() const override { return true; };
74 :
75 : /**
76 : * @copydoc Layer::exportTo(Exporter &exporter, ml::train::ExportMethods
77 : * method)
78 : */
79 : void exportTo(Exporter &exporter,
80 : const ml::train::ExportMethods &method) const override;
81 :
82 : /**
83 : * @copydoc Layer::setProperty(const std::vector<std::string> &values)
84 : */
85 : void setProperty(const std::vector<std::string> &values) override;
86 :
87 : /**
88 : * @copydoc Layer::getType()
89 : */
90 117 : const std::string getType() const override {
91 117 : return PositionalEncodingLayer::type;
92 : };
93 :
94 : static constexpr const char *type = "positional_encoding";
95 :
96 : private:
97 : bool isPEcalculated; // bool value to check positional encoding is already
98 : // calculated
99 : std::tuple<props::MaxTimestep> positional_encoding_props;
100 : std::array<unsigned int, 1> weight_idx;
101 :
102 : /**
103 : * @brief calculate positional encoding
104 : * @param context Context of the layer
105 : */
106 : void calculatePositionalEncoding(RunLayerContext &context);
107 : };
108 :
109 : } // namespace nntrainer
110 :
111 : #endif /* __cplusplus */
112 : #endif /* __MULTI_HEAD_ATTENTION_LAYER_H__ */
|