2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2015 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef __NNFW_CKER_HELPER_TENSOR_H__
19 #define __NNFW_CKER_HELPER_TENSOR_H__
21 #include "cker/Shape.h"
22 #include "cker/eigen/EigenSupport.h"
28 template <typename T, int NDIMS = 1, typename IndexType = Eigen::DenseIndex> struct TTypes
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>,
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>>
42 typedef Eigen::TensorMap<Eigen::Tensor<T, NDIMS, Eigen::RowMajor, int>, Eigen::Aligned>
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>,
49 typedef Eigen::TensorMap<
50 Eigen::TensorFixedSize<const T, Eigen::Sizes<>, Eigen::RowMajor, IndexType>, Eigen::Aligned>
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>>
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>
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;
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>
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>>
86 typedef typename TTypes<float, 1>::Tensor32Bit::Index Index32;
88 template <typename T> struct InputTensor
100 bool copyFrom(const Tensor &other, const Shape &new_shape)
102 if (other.shape.FlatSize() != new_shape.FlatSize())
105 this->shape.ReplaceWith(new_shape.DimensionsCount(), new_shape.DimsData());
106 this->buffer = other.buffer;
111 template <typename T> T *base() const
113 return buffer == nullptr ? nullptr : reinterpret_cast<T *>(buffer);
116 template <typename T, size_t NDIMS>
117 typename TTypes<T, NDIMS>::Tensor shaped(const std::vector<int32_t> &new_sizes)
119 Eigen::array<Eigen::DenseIndex, NDIMS> dims;
120 for (size_t d = 0; d < NDIMS; d++)
122 dims[d] = new_sizes[d];
124 return typename TTypes<T, NDIMS>::Tensor(base<T>(), dims);
127 template <typename T> typename TTypes<T>::Flat flat() { return shaped<T, 1>({shape.FlatSize()}); }
129 template <typename T, size_t NDIMS>
130 typename TTypes<T, NDIMS>::ConstTensor shaped(const std::vector<int32_t> new_sizes) const
132 Eigen::array<Eigen::DenseIndex, NDIMS> dims;
133 for (size_t d = 0; d < NDIMS; d++)
135 dims[d] = new_sizes[d];
137 return typename TTypes<T, NDIMS>::ConstTensor(base<T>(), dims);
140 // Create Eigen Tensor with current shape
141 template <typename T, size_t NDIMS> typename TTypes<T, NDIMS>::Tensor shaped() const
143 Eigen::array<Eigen::DenseIndex, NDIMS> dims;
144 for (size_t d = 0; d < NDIMS; d++)
146 dims[d] = shape.Dims(d);
148 return typename TTypes<T, NDIMS>::Tensor(base<T>(), dims);
151 template <typename T> typename TTypes<T>::ConstFlat flat() const
153 return shaped<T, 1>({shape.FlatSize()});
156 template <typename T> typename TTypes<T>::ConstScalar scalar() const
158 return typename TTypes<T>::ConstScalar(base<T>());
162 template <typename DSizes> Eigen::DSizes<Index32, DSizes::count> To32BitDims(const DSizes &in)
164 Eigen::DSizes<Index32, DSizes::count> out;
165 for (int i = 0; i < DSizes::count; ++i)
172 template <typename TensorType>
173 typename TTypes<typename TensorType::Scalar, TensorType::NumIndices>::Tensor32Bit
174 To32Bit(TensorType in)
176 typedef typename TTypes<typename TensorType::Scalar, TensorType::NumIndices>::Tensor32Bit RetType;
177 return RetType(in.data(), To32BitDims(in.dimensions()));
183 #endif // __NNFW_CKER_HELPER_TENSOR_H__