Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / loco / include / loco / IR / FeatureCodec.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __LOCO_IR_FEATURE_CODEC_H__
18 #define __LOCO_IR_FEATURE_CODEC_H__
19
20 #include "loco/IR/FeatureShape.h"
21 #include "loco/IR/FeatureIndex.h"
22
23 #include "loco/IR/TensorShape.h"
24 #include "loco/IR/TensorIndex.h"
25
26 #include "loco/IR/CastHelpers.h"
27
28 #include <memory>
29
30 namespace loco
31 {
32
33 /**
34  * @brief Decribe how to build a (convolution) feature map from a tensor
35  *
36  * Let us assume that "enc" is a feature encoder.
37  *
38  * Given a tensor "inp" and its shape "inp.shape", "enc" builds a feature map
39  * "out" as follows:
40  *
41  * for each valid feature index (referred to as feature_idx below) for enc.shape(inp.shape)
42  *   out.at(feature_index) = inp.at(enc.value(feature_index))
43  */
44 struct FeatureEncoder
45 {
46   virtual ~FeatureEncoder() = default;
47
48   virtual FeatureShape shape(const TensorShape &shape) const = 0;
49   virtual TensorIndex value(const FeatureIndex &index) const = 0;
50
51   virtual std::unique_ptr<FeatureEncoder> clone(void) const = 0;
52 };
53
54 /**
55  * @brief Describe how to build a tensor from a (convolution) feature map
56  *
57  * Let us assume that "dec" is a feature decoder.
58  *
59  * Given a feature map "inp" and its shape "inp.shape", "dec" builds a tensor
60  * "out" as follows:
61  *
62  * for each valid tensor index (referred to as tensor_index below) for dec.shape(inp.shape)
63  *   out.at(tensor_index) = inp.at(dec.value(tensor_index))
64  *
65  * NOTE "inp" is a feature value and "out" is a tensor value in this example.
66  */
67 struct FeatureDecoder
68 {
69   virtual ~FeatureDecoder() = default;
70
71   virtual TensorShape shape(const FeatureShape &) const = 0;
72   virtual FeatureIndex value(const TensorIndex &) const = 0;
73
74   virtual std::unique_ptr<FeatureDecoder> clone(void) const = 0;
75 };
76
77 /**
78  * @brief A helper dynamic_cast that throws when failed
79  */
80 template <typename T> T must_cast(FeatureEncoder *node)
81 {
82   return _must_cast<T, FeatureEncoder *>(node);
83 }
84
85 template <typename T> T must_cast(const FeatureEncoder *node)
86 {
87   return _must_cast<T, const FeatureEncoder *>(node);
88 }
89
90 template <typename T> T must_cast(FeatureDecoder *node)
91 {
92   return _must_cast<T, FeatureDecoder *>(node);
93 }
94
95 template <typename T> T must_cast(const FeatureDecoder *node)
96 {
97   return _must_cast<T, const FeatureDecoder *>(node);
98 }
99
100 } // namespace loco
101
102 #endif // __LOCO_IR_FEATURE_CODEC_H__