e6ac008a5297f7ae7fde2625950f000029dd1a12
[platform/core/ml/nnfw.git] / compute / cker / include / cker / operation / Helper / Tensor.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2015 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef __NNFW_CKER_HELPER_TENSOR_H__
19 #define __NNFW_CKER_HELPER_TENSOR_H__
20
21 #include "cker/Shape.h"
22 #include "cker/eigen/EigenSupport.h"
23
24 namespace nnfw
25 {
26 namespace cker
27 {
28 template <typename T, int NDIMS = 1, typename IndexType = Eigen::DenseIndex> struct TTypes
29 {
30   // Rank-<NDIMS> tensor of scalar type T.
31   typedef Eigen::TensorMap<Eigen::Tensor<T, NDIMS, Eigen::RowMajor, IndexType>, Eigen::Aligned>
32       Tensor;
33   typedef Eigen::TensorMap<Eigen::Tensor<const T, NDIMS, Eigen::RowMajor, IndexType>,
34                            Eigen::Aligned>
35       ConstTensor;
36
37   // Unaligned Rank-<NDIMS> tensor of scalar type T.
38   typedef Eigen::TensorMap<Eigen::Tensor<T, NDIMS, Eigen::RowMajor, IndexType>> UnalignedTensor;
39   typedef Eigen::TensorMap<Eigen::Tensor<const T, NDIMS, Eigen::RowMajor, IndexType>>
40       UnalignedConstTensor;
41
42   typedef Eigen::TensorMap<Eigen::Tensor<T, NDIMS, Eigen::RowMajor, int>, Eigen::Aligned>
43       Tensor32Bit;
44
45   // Scalar tensor (implemented as a rank-0 tensor) of scalar type T.
46   typedef Eigen::TensorMap<Eigen::TensorFixedSize<T, Eigen::Sizes<>, Eigen::RowMajor, IndexType>,
47                            Eigen::Aligned>
48       Scalar;
49   typedef Eigen::TensorMap<
50       Eigen::TensorFixedSize<const T, Eigen::Sizes<>, Eigen::RowMajor, IndexType>, Eigen::Aligned>
51       ConstScalar;
52
53   // Unaligned Scalar tensor of scalar type T.
54   typedef Eigen::TensorMap<Eigen::TensorFixedSize<T, Eigen::Sizes<>, Eigen::RowMajor, IndexType>>
55       UnalignedScalar;
56   typedef Eigen::TensorMap<
57       Eigen::TensorFixedSize<const T, Eigen::Sizes<>, Eigen::RowMajor, IndexType>>
58       UnalignedConstScalar;
59
60   // Rank-1 tensor (vector) of scalar type T.
61   typedef Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, IndexType>, Eigen::Aligned> Flat;
62   typedef Eigen::TensorMap<Eigen::Tensor<const T, 1, Eigen::RowMajor, IndexType>, Eigen::Aligned>
63       ConstFlat;
64   typedef Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, IndexType>, Eigen::Aligned> Vec;
65   typedef Eigen::TensorMap<Eigen::Tensor<const T, 1, Eigen::RowMajor, IndexType>, Eigen::Aligned>
66       ConstVec;
67
68   // Unaligned Rank-1 tensor (vector) of scalar type T.
69   typedef Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, IndexType>> UnalignedFlat;
70   typedef Eigen::TensorMap<Eigen::Tensor<const T, 1, Eigen::RowMajor, IndexType>>
71       UnalignedConstFlat;
72   typedef Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, IndexType>> UnalignedVec;
73   typedef Eigen::TensorMap<Eigen::Tensor<const T, 1, Eigen::RowMajor, IndexType>> UnalignedConstVec;
74
75   // Rank-2 tensor (matrix) of scalar type T.
76   typedef Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, IndexType>, Eigen::Aligned> Matrix;
77   typedef Eigen::TensorMap<Eigen::Tensor<const T, 2, Eigen::RowMajor, IndexType>, Eigen::Aligned>
78       ConstMatrix;
79
80   // Unaligned Rank-2 tensor (matrix) of scalar type T.
81   typedef Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, IndexType>> UnalignedMatrix;
82   typedef Eigen::TensorMap<Eigen::Tensor<const T, 2, Eigen::RowMajor, IndexType>>
83       UnalignedConstMatrix;
84 };
85
86 typedef typename TTypes<float, 1>::Tensor32Bit::Index Index32;
87
88 template <typename T> struct InputTensor
89 {
90   Shape shape;
91   const T *buffer;
92 };
93
94 struct Tensor
95 {
96   Shape shape;
97   void *buffer;
98
99 public:
100   bool copyFrom(const Tensor &other, const Shape &new_shape)
101   {
102     if (other.shape.FlatSize() != new_shape.FlatSize())
103       return false;
104
105     this->shape.ReplaceWith(new_shape.DimensionsCount(), new_shape.DimsData());
106     this->buffer = other.buffer;
107
108     return true;
109   }
110
111   template <typename T> T *base() const
112   {
113     return buffer == nullptr ? nullptr : reinterpret_cast<T *>(buffer);
114   }
115
116   template <typename T, size_t NDIMS>
117   typename TTypes<T, NDIMS>::Tensor shaped(const std::vector<int32_t> &new_sizes)
118   {
119     Eigen::array<Eigen::DenseIndex, NDIMS> dims;
120     for (size_t d = 0; d < NDIMS; d++)
121     {
122       dims[d] = new_sizes[d];
123     }
124     return typename TTypes<T, NDIMS>::Tensor(base<T>(), dims);
125   }
126
127   template <typename T> typename TTypes<T>::Flat flat() { return shaped<T, 1>({shape.FlatSize()}); }
128
129   template <typename T, size_t NDIMS>
130   typename TTypes<T, NDIMS>::ConstTensor shaped(const std::vector<int32_t> new_sizes) const
131   {
132     Eigen::array<Eigen::DenseIndex, NDIMS> dims;
133     for (size_t d = 0; d < NDIMS; d++)
134     {
135       dims[d] = new_sizes[d];
136     }
137     return typename TTypes<T, NDIMS>::ConstTensor(base<T>(), dims);
138   }
139
140   // Create Eigen Tensor with current shape
141   template <typename T, size_t NDIMS> typename TTypes<T, NDIMS>::Tensor shaped() const
142   {
143     Eigen::array<Eigen::DenseIndex, NDIMS> dims;
144     for (size_t d = 0; d < NDIMS; d++)
145     {
146       dims[d] = shape.Dims(d);
147     }
148     return typename TTypes<T, NDIMS>::Tensor(base<T>(), dims);
149   }
150
151   template <typename T> typename TTypes<T>::ConstFlat flat() const
152   {
153     return shaped<T, 1>({shape.FlatSize()});
154   }
155
156   template <typename T> typename TTypes<T>::ConstScalar scalar() const
157   {
158     return typename TTypes<T>::ConstScalar(base<T>());
159   }
160 }; // Tensor
161
162 template <typename DSizes> Eigen::DSizes<Index32, DSizes::count> To32BitDims(const DSizes &in)
163 {
164   Eigen::DSizes<Index32, DSizes::count> out;
165   for (int i = 0; i < DSizes::count; ++i)
166   {
167     out[i] = in[i];
168   }
169   return out;
170 }
171
172 template <typename TensorType>
173 typename TTypes<typename TensorType::Scalar, TensorType::NumIndices>::Tensor32Bit
174 To32Bit(TensorType in)
175 {
176   typedef typename TTypes<typename TensorType::Scalar, TensorType::NumIndices>::Tensor32Bit RetType;
177   return RetType(in.data(), To32BitDims(in.dimensions()));
178 }
179
180 } // namespace cker
181 } // namespace nnfw
182
183 #endif // __NNFW_CKER_HELPER_TENSOR_H__