ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2018 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_EIGEN_UTILS_H__
19#define __NNFW_CKER_EIGEN_UTILS_H__
20
21#include <Eigen/Core>
22#include <type_traits>
23#include "cker/Shape.h"
24
25namespace nnfw
26{
27namespace cker
28{
29
30// Make a local VectorMap typedef allowing to map a float array
31// as a Eigen vector expression. The std::conditional here is to
32// construct the suitable Eigen type for the constness of the
33// data. Indeed, for const data, we need to produce
34// Eigen::Map<const Eigen::Matrix<float, ...>>
35// and not the more straightforward
36// Eigen::Map<Eigen::Matrix<const float, ...>>
37template <typename Scalar>
38using VectorMap = typename std::conditional<
39 std::is_const<Scalar>::value,
40 Eigen::Map<const Eigen::Matrix<typename std::remove_const<Scalar>::type, Eigen::Dynamic, 1>>,
41 Eigen::Map<Eigen::Matrix<Scalar, Eigen::Dynamic, 1>>>::type;
42
43template <typename Scalar> VectorMap<Scalar> MapAsVector(Scalar *data, const Shape &shape)
44{
45 const int size = shape.FlatSize();
46 return VectorMap<Scalar>(data, size, 1);
47}
48
49// Make a local VectorMap typedef allowing to map a float array
50// as a Eigen matrix expression. The same explanation as for VectorMap
51// above also applies here.
52template <typename Scalar>
53using MatrixMap = typename std::conditional<
54 std::is_const<Scalar>::value,
55 Eigen::Map<
56 const Eigen::Matrix<typename std::remove_const<Scalar>::type, Eigen::Dynamic, Eigen::Dynamic>>,
57 Eigen::Map<Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>>>::type;
58
59template <typename Scalar>
61{
62 const int dims_count = shape.DimensionsCount();
63 const int rows = shape.Dims(dims_count - 1);
64 const int cols = FlatSizeSkipDim(shape, dims_count - 1);
65 return MatrixMap<Scalar>(data, rows, cols);
66}
67
68} // namespace cker
69} // namespace nnfw
70
71#endif // __NNFW_CKER_EIGEN_UTILS_H__
int32_t DimensionsCount() const
Definition Shape.h:91
int32_t Dims(int i) const
Definition Shape.h:92
int FlatSize() const
Definition Shape.h:181
MatrixMap< Scalar > MapAsMatrixWithLastDimAsRows(Scalar *data, const Shape &shape)
Definition Utils.h:60
int FlatSizeSkipDim(const Shape &shape, int skip_dim)
Definition Shape.h:253
typename std::conditional< std::is_const< Scalar >::value, Eigen::Map< const Eigen::Matrix< typename std::remove_const< Scalar >::type, Eigen::Dynamic, 1 > >, Eigen::Map< Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > > >::type VectorMap
Definition Utils.h:41
typename std::conditional< std::is_const< Scalar >::value, Eigen::Map< const Eigen::Matrix< typename std::remove_const< Scalar >::type, Eigen::Dynamic, Eigen::Dynamic > >, Eigen::Map< Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > > >::type MatrixMap
Definition Utils.h:57
VectorMap< Scalar > MapAsVector(Scalar *data, const Shape &shape)
Definition Utils.h:43
Definition topk_v2.h:30
int32_t size[5]
Definition Slice.cpp:35