Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / acl_common / IACLTensor.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "IACLTensor.h"
18 #include "Convert.h"
19 #include "Swizzle.h"
20
21 namespace onert
22 {
23 namespace backend
24 {
25 namespace acl_common
26 {
27
28 size_t IACLTensor::dimension(size_t index) const
29 {
30   // Assume that the front is higher dimensional.
31   // i.g. N: 0, C: 1, H: 2, W: 3 for NCHW layout
32   // NOTE This tensor must not be applied dim correction
33   auto rank = num_dimensions();
34   rank = rank == 0 ? 1 : rank;
35   assert(rank > index);
36   const ARMComputeAxis reversed{(static_cast<uint32_t>(rank - index) - 1)};
37   return info()->dimension(reversed.value());
38 }
39
40 size_t IACLTensor::calcOffset(const ir::Coordinates &coords) const
41 {
42   auto rank = num_dimensions();
43   rank = rank == 0 ? 1 : rank;
44   assert(rank == coords.size());
45
46   ::arm_compute::Coordinates acl_coords;
47   for (uint32_t i = 0; i < rank; ++i)
48   {
49     const ARMComputeAxis reversed{static_cast<uint32_t>((rank - i) - 1)};
50     acl_coords.set(reversed.value(), coords[i]);
51   }
52
53   return info()->offset_element_in_bytes(acl_coords);
54 }
55
56 ir::Layout IACLTensor::layout() const { return acl_common::asRuntimeLayout(info()->data_layout()); }
57
58 ir::DataType IACLTensor::data_type() const
59 {
60   return acl_common::asRuntimeDataType(info()->data_type());
61 }
62
63 float IACLTensor::data_scale() const
64 {
65   // FIXME What if quantization info is non-uniform?
66   return info()->quantization_info().uniform().scale;
67 }
68
69 int32_t IACLTensor::data_offset() const
70 {
71   // FIXME What if quantization info is non-uniform?
72   return info()->quantization_info().uniform().offset;
73 }
74
75 } // namespace acl_common
76 } // namespace backend
77 } // namespace onert