Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / arm_compute / runtime / misc / functions / Utils.h
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 /**
18  * @file utils.h
19  * @ingroup COM_AI_RUNTIME
20  * @brief This file contains utils for arm compute library
21  */
22 #ifndef __ARM_COMPUTE_MISC_UTILS_H__
23 #define __ARM_COMPUTE_MISC_UTILS_H__
24
25 #include <string>
26 #include <cassert>
27 #include <arm_compute/runtime/CL/CLTensor.h>
28
29 #include <arm_compute/core/Coordinates.h>
30 #include <arm_compute/core/TensorInfo.h>
31 #include <arm_compute/core/TensorShape.h>
32 #include <arm_compute/core/Types.h>
33
34 // TODO : It should be extracted to independent module.
35
36 namespace arm_compute
37 {
38 namespace misc
39 {
40 namespace utils
41 {
42
43 /**
44  * @brief Check if this runtime runs on GPU or NEON
45  * @return @c true if GPU mode, otherwise @c false
46  */
47 bool isGpuMode();
48
49 #ifndef CAST_CL
50 #define CAST_CL(tensor) static_cast<::arm_compute::CLTensor *>(tensor)
51 #endif
52
53 #ifndef CAST_NE
54 #define CAST_NE(tensor) static_cast<::arm_compute::Tensor *>(tensor)
55 #endif
56
57 /**
58 * @brief      Generate arm compute permutation vector from runtime permutation vector
59 * @param[in]  rank                 Rank number supported upto 4
60 * @param[in]  runtime_pv           Integer array for runtime permutation vector
61 * @return     Permutation vector of arm compute
62 */
63 arm_compute::PermutationVector getARMComputePermutationVector(uint32_t rank,
64                                                               const int32_t *runtime_pv);
65
66 /**
67  * @brief       Set value to arm compute tensor with casting
68  * @param[in]   value Value to set
69  * @param[out]  to    Target tensor of arm compute
70  * @param[in]   id    Position of element
71  * @return      N/A
72  */
73 template <typename FromT>
74 void copyCast(const FromT value, arm_compute::ITensor *to, const arm_compute::Coordinates &id)
75 {
76   switch (to->info()->data_type())
77   {
78     case arm_compute::DataType::F32:
79     {
80       *reinterpret_cast<float *>(to->ptr_to_element(id)) = static_cast<float>(value);
81       break;
82     }
83     case arm_compute::DataType::S32:
84     {
85       *reinterpret_cast<int32_t *>(to->ptr_to_element(id)) = static_cast<int32_t>(value);
86       break;
87     }
88     case arm_compute::DataType::U32:
89     {
90       *reinterpret_cast<uint32_t *>(to->ptr_to_element(id)) = static_cast<uint32_t>(value);
91       break;
92     }
93     case arm_compute::DataType::QASYMM8:
94     {
95       float realValue = static_cast<float>(value);
96       // NOTE We haven't known the policy of rounding for quantization.
97       //      So this is set to a temporary value.
98       *(to->ptr_to_element(id)) = quantize_qasymm8(realValue, to->info()->quantization_info(),
99                                                    arm_compute::RoundingPolicy::TO_ZERO);
100       break;
101     }
102     default:
103       throw std::runtime_error("Not supported, yet");
104       break;
105   }
106 }
107
108 } // namespace utils
109 } // namespace misc
110 } // namespace arm_compute
111
112 #endif // __ARM_COMPUTE_MISC_UTILS_H__