2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __LOCO_IR_FEATURE_CODEC_H__
18 #define __LOCO_IR_FEATURE_CODEC_H__
20 #include "loco/IR/FeatureShape.h"
21 #include "loco/IR/FeatureIndex.h"
23 #include "loco/IR/TensorShape.h"
24 #include "loco/IR/TensorIndex.h"
26 #include "loco/IR/CastHelpers.h"
34 * @brief Decribe how to build a (convolution) feature map from a tensor
36 * Let us assume that "enc" is a feature encoder.
38 * Given a tensor "inp" and its shape "inp.shape", "enc" builds a feature map
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))
46 virtual ~FeatureEncoder() = default;
48 virtual FeatureShape shape(const TensorShape &shape) const = 0;
49 virtual TensorIndex value(const FeatureIndex &index) const = 0;
51 virtual std::unique_ptr<FeatureEncoder> clone(void) const = 0;
55 * @brief Describe how to build a tensor from a (convolution) feature map
57 * Let us assume that "dec" is a feature decoder.
59 * Given a feature map "inp" and its shape "inp.shape", "dec" builds a tensor
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))
65 * NOTE "inp" is a feature value and "out" is a tensor value in this example.
69 virtual ~FeatureDecoder() = default;
71 virtual TensorShape shape(const FeatureShape &) const = 0;
72 virtual FeatureIndex value(const TensorIndex &) const = 0;
74 virtual std::unique_ptr<FeatureDecoder> clone(void) const = 0;
78 * @brief A helper dynamic_cast that throws when failed
80 template <typename T> T must_cast(FeatureEncoder *node)
82 return _must_cast<T, FeatureEncoder *>(node);
85 template <typename T> T must_cast(const FeatureEncoder *node)
87 return _must_cast<T, const FeatureEncoder *>(node);
90 template <typename T> T must_cast(FeatureDecoder *node)
92 return _must_cast<T, FeatureDecoder *>(node);
95 template <typename T> T must_cast(const FeatureDecoder *node)
97 return _must_cast<T, const FeatureDecoder *>(node);
102 #endif // __LOCO_IR_FEATURE_CODEC_H__