ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Dims.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright (C) 2017 The Android Open Source Project
4 * Copyright 2018 The TensorFlow Authors. All Rights Reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef __DIMS_H__
20#define __DIMS_H__
21
22#include "Shape.h"
23#include "Macro.h"
24
25template <int N> struct Dims
26{
27 int sizes[N];
28 int strides[N];
29};
30
31inline Dims<4> convertShapeToDims(const Shape &shape)
32{
33 Dims<4> dims;
34 for (int i = 0; i < 4; i++)
35 {
36 dims.sizes[i] = 1;
37 }
38
39 if (shape.dimensions.size() == 1)
40 {
41 dims.sizes[0] = (int)getSizeOfDimension(shape, 0);
42 }
43 else
44 {
45 for (int i = 0; i < 4; i++)
46 {
47 int src = (int)shape.dimensions.size() - i - 1;
48 if (src >= 0)
49 {
50 dims.sizes[i] = (int)getSizeOfDimension(shape, src);
51 }
52 }
53 }
54
55 dims.strides[0] = 1;
56 for (int i = 1; i < 4; i++)
57 {
58 dims.strides[i] = dims.strides[i - 1] * dims.sizes[i - 1];
59 }
60 return dims;
61}
62
63// From types.h in TensorFlow Lite
64inline int Offset(const Dims<4> &dims, int i0, int i1, int i2, int i3)
65{
66 DCHECK(i0 >= 0 && i0 < dims.sizes[0]);
67 DCHECK(i1 >= 0 && i1 < dims.sizes[1]);
68 DCHECK(i2 >= 0 && i2 < dims.sizes[2]);
69 DCHECK(i3 >= 0 && i3 < dims.sizes[3]);
70 return i0 * dims.strides[0] + i1 * dims.strides[1] + i2 * dims.strides[2] + i3 * dims.strides[3];
71}
72
73// From types.h in TensorFlow Lite
74//
75// Get array size, DCHECKing that the dim index is in range.
76template <int N> int ArraySize(const Dims<N> &array, int index)
77{
78 DCHECK(index >= 0 && index < N);
79 return array.sizes[index];
80}
81
82// From types.h in TensorFlow Lite
83template <int N> inline int FlatSize(const Dims<N> &dims)
84{
85 int flat_size = 1;
86 for (int i = 0; i < N; ++i)
87 {
88 flat_size *= dims.sizes[i];
89 }
90 return flat_size;
91}
92
93// From types.h in TensorFlow Lite
94inline int RequiredBufferSizeForDims(const Dims<4> &dims)
95{
96 int max_offset = 0;
97 for (int i = 0; i < 4; i++)
98 {
99 max_offset += (dims.sizes[i] - 1) * dims.strides[i];
100 }
101 return max_offset + 1;
102}
103
104// From types.h in TensorFlow Lite
105//
106// Flat size calculation, checking that dimensions match with one or more other
107// arrays.
108template <int N> inline int MatchingFlatSize(const Dims<N> &dims, const Dims<N> &check_dims_0)
109{
110 for (int i = 0; i < N; ++i)
111 {
112 DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i));
113 }
114 return FlatSize(dims);
115}
116
117// From types.h in TensorFlow Lite
118template <int N>
119inline int MatchingFlatSize(const Dims<N> &dims, const Dims<N> &check_dims_0,
120 const Dims<N> &check_dims_1)
121{
122 for (int i = 0; i < N; ++i)
123 {
124 DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i));
125 }
126 return MatchingFlatSize(dims, check_dims_1);
127}
128
129// From types.h in TensorFlow Lite
130template <int N>
131inline int MatchingFlatSize(const Dims<N> &dims, const Dims<N> &check_dims_0,
132 const Dims<N> &check_dims_1, const Dims<N> &check_dims_2)
133{
134 for (int i = 0; i < N; ++i)
135 {
136 DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i));
137 }
138 return FlatSize(dims, check_dims_1, check_dims_2);
139}
140
141// From types.h in TensorFlow Lite
142template <int N>
143inline int MatchingFlatSize(const Dims<N> &dims, const Dims<N> &check_dims_0,
144 const Dims<N> &check_dims_1, const Dims<N> &check_dims_2,
145 const Dims<N> &check_dims_3)
146{
147 for (int i = 0; i < N; ++i)
148 {
149 DCHECK_EQ(ArraySize(dims, i), ArraySize(check_dims_0, i));
150 }
151 return FlatSize(dims, check_dims_1, check_dims_2, check_dims_3);
152}
153
154// From types.h in TensorFlow Lite
155template <int N> bool IsPackedWithoutStrides(const Dims<N> &dims)
156{
157 int expected_stride = 1;
158 for (int d = 0; d < N; d++)
159 {
160 if (dims.strides[d] != expected_stride)
161 return false;
162 expected_stride *= dims.sizes[d];
163 }
164 return true;
165}
166
167#endif // __DIMS_H__
int RequiredBufferSizeForDims(const Dims< 4 > &dims)
Definition Dims.h:94
int Offset(const Dims< 4 > &dims, int i0, int i1, int i2, int i3)
Definition Dims.h:64
int ArraySize(const Dims< N > &array, int index)
Definition Dims.h:76
bool IsPackedWithoutStrides(const Dims< N > &dims)
Definition Dims.h:155
int FlatSize(const Dims< N > &dims)
Definition Dims.h:83
int MatchingFlatSize(const Dims< N > &dims, const Dims< N > &check_dims_0)
Definition Dims.h:108
Dims< 4 > convertShapeToDims(const Shape &shape)
Definition Dims.h:31
#define DCHECK_EQ(x, y)
Definition Macro.h:29
#define DCHECK(condition)
Definition Macro.h:25
uint32_t getSizeOfDimension(const Shape &shape, uint32_t dimensionIdx)
Definition Shape.cpp:60
Definition Dims.h:26
int sizes[N]
Definition Dims.h:27
int strides[N]
Definition Dims.h:28
Definition Shape.h:28
std::vector< uint32_t > dimensions
Definition Shape.h:30