ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Tensor.h
Go to the documentation of this file.
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"
23
24namespace nnfw
25{
26namespace cker
27{
28template <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>
33 typedef Eigen::TensorMap<Eigen::Tensor<const T, NDIMS, Eigen::RowMajor, IndexType>,
34 Eigen::Aligned>
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>>
41
42 typedef Eigen::TensorMap<Eigen::Tensor<T, NDIMS, Eigen::RowMajor, int>, Eigen::Aligned>
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>
49 typedef Eigen::TensorMap<
50 Eigen::TensorFixedSize<const T, Eigen::Sizes<>, Eigen::RowMajor, IndexType>, Eigen::Aligned>
52
53 // Unaligned Scalar tensor of scalar type T.
54 typedef Eigen::TensorMap<Eigen::TensorFixedSize<T, Eigen::Sizes<>, Eigen::RowMajor, IndexType>>
56 typedef Eigen::TensorMap<
57 Eigen::TensorFixedSize<const T, Eigen::Sizes<>, Eigen::RowMajor, IndexType>>
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>
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>
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>>
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>
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>>
84};
85
87
88template <typename T> struct InputTensor
89{
91 const T *buffer;
92};
93
94struct Tensor
95{
97 void *buffer;
98
99public:
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
161 template <typename T> typename TTypes<T>::Vec vec() { return shaped<T, 1>(); }
162
163 template <typename T> typename TTypes<T>::Matrix matrix() { return shaped<T, 2>(); }
164}; // Tensor
165
166template <typename DSizes> Eigen::DSizes<Index32, DSizes::count> To32BitDims(const DSizes &in)
167{
168 Eigen::DSizes<Index32, DSizes::count> out;
169 for (int i = 0; i < DSizes::count; ++i)
170 {
171 out[i] = in[i];
172 }
173 return out;
174}
175
176template <typename TensorType>
178To32Bit(TensorType in)
179{
181 return RetType(in.data(), To32BitDims(in.dimensions()));
182}
183
184} // namespace cker
185} // namespace nnfw
186
187#endif // __NNFW_CKER_HELPER_TENSOR_H__
int32_t DimensionsCount() const
Definition Shape.h:91
void ReplaceWith(int dimensions_count, const int32_t *dims_data)
Definition Shape.h:130
int32_t Dims(int i) const
Definition Shape.h:92
int FlatSize() const
Definition Shape.h:181
int32_t * DimsData()
Definition Shape.h:112
TTypes< typenameTensorType::Scalar, TensorType::NumIndices >::Tensor32Bit To32Bit(TensorType in)
Definition Tensor.h:178
TTypes< float, 1 >::Tensor32Bit::Index Index32
Definition Tensor.h:86
Eigen::DSizes< Index32, DSizes::count > To32BitDims(const DSizes &in)
Definition Tensor.h:166
Definition topk_v2.h:30
Eigen::TensorMap< Eigen::Tensor< T, 1, Eigen::RowMajor, IndexType > > UnalignedFlat
Definition Tensor.h:69
Eigen::TensorMap< Eigen::TensorFixedSize< T, Eigen::Sizes<>, Eigen::RowMajor, IndexType > > UnalignedScalar
Definition Tensor.h:55
Eigen::TensorMap< Eigen::TensorFixedSize< const T, Eigen::Sizes<>, Eigen::RowMajor, IndexType >, Eigen::Aligned > ConstScalar
Definition Tensor.h:51
Eigen::TensorMap< Eigen::Tensor< const T, 1, Eigen::RowMajor, IndexType > > UnalignedConstVec
Definition Tensor.h:73
Eigen::TensorMap< Eigen::Tensor< const T, NDIMS, Eigen::RowMajor, IndexType > > UnalignedConstTensor
Definition Tensor.h:40
Eigen::TensorMap< Eigen::Tensor< const T, NDIMS, Eigen::RowMajor, IndexType >, Eigen::Aligned > ConstTensor
Definition Tensor.h:35
Eigen::TensorMap< Eigen::TensorFixedSize< const T, Eigen::Sizes<>, Eigen::RowMajor, IndexType > > UnalignedConstScalar
Definition Tensor.h:58
Eigen::TensorMap< Eigen::Tensor< T, NDIMS, Eigen::RowMajor, IndexType > > UnalignedTensor
Definition Tensor.h:38
Eigen::TensorMap< Eigen::Tensor< const T, 1, Eigen::RowMajor, IndexType >, Eigen::Aligned > ConstFlat
Definition Tensor.h:63
Eigen::TensorMap< Eigen::TensorFixedSize< T, Eigen::Sizes<>, Eigen::RowMajor, IndexType >, Eigen::Aligned > Scalar
Definition Tensor.h:48
Eigen::TensorMap< Eigen::Tensor< T, 2, Eigen::RowMajor, IndexType > > UnalignedMatrix
Definition Tensor.h:81
Eigen::TensorMap< Eigen::Tensor< const T, 1, Eigen::RowMajor, IndexType > > UnalignedConstFlat
Definition Tensor.h:71
Eigen::TensorMap< Eigen::Tensor< T, 1, Eigen::RowMajor, IndexType >, Eigen::Aligned > Flat
Definition Tensor.h:61
Eigen::TensorMap< Eigen::Tensor< T, NDIMS, Eigen::RowMajor, int >, Eigen::Aligned > Tensor32Bit
Definition Tensor.h:43
Eigen::TensorMap< Eigen::Tensor< T, 1, Eigen::RowMajor, IndexType > > UnalignedVec
Definition Tensor.h:72
Eigen::TensorMap< Eigen::Tensor< const T, 2, Eigen::RowMajor, IndexType > > UnalignedConstMatrix
Definition Tensor.h:83
Eigen::TensorMap< Eigen::Tensor< T, 1, Eigen::RowMajor, IndexType >, Eigen::Aligned > Vec
Definition Tensor.h:64
Eigen::TensorMap< Eigen::Tensor< T, 2, Eigen::RowMajor, IndexType >, Eigen::Aligned > Matrix
Definition Tensor.h:76
Eigen::TensorMap< Eigen::Tensor< T, NDIMS, Eigen::RowMajor, IndexType >, Eigen::Aligned > Tensor
Definition Tensor.h:32
Eigen::TensorMap< Eigen::Tensor< const T, 1, Eigen::RowMajor, IndexType >, Eigen::Aligned > ConstVec
Definition Tensor.h:66
Eigen::TensorMap< Eigen::Tensor< const T, 2, Eigen::RowMajor, IndexType >, Eigen::Aligned > ConstMatrix
Definition Tensor.h:78
TTypes< T, NDIMS >::Tensor shaped(const std::vector< int32_t > &new_sizes)
Definition Tensor.h:117
TTypes< T >::Vec vec()
Definition Tensor.h:161
TTypes< T >::Matrix matrix()
Definition Tensor.h:163
TTypes< T >::ConstScalar scalar() const
Definition Tensor.h:156
T * base() const
Definition Tensor.h:111
TTypes< T, NDIMS >::Tensor shaped() const
Definition Tensor.h:141
bool copyFrom(const Tensor &other, const Shape &new_shape)
Definition Tensor.h:100
TTypes< T, NDIMS >::ConstTensor shaped(const std::vector< int32_t > new_sizes) const
Definition Tensor.h:130
TTypes< T >::Flat flat()
Definition Tensor.h:127
TTypes< T >::ConstFlat flat() const
Definition Tensor.h:151