xref: /aosp_15_r20/external/ComputeLibrary/src/common/utils/LegacySupport.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "src/common/utils/LegacySupport.h"
25 
26 namespace arm_compute
27 {
28 namespace detail
29 {
30 namespace
31 {
convert_to_legacy_data_type(AclDataType data_type)32 DataType convert_to_legacy_data_type(AclDataType data_type)
33 {
34     switch(data_type)
35     {
36         case AclDataType::AclFloat32:
37             return DataType::F32;
38         case AclDataType::AclFloat16:
39             return DataType::F16;
40         case AclDataType::AclBFloat16:
41             return DataType::BFLOAT16;
42         default:
43             return DataType::UNKNOWN;
44     }
45 }
46 
convert_to_c_data_type(DataType data_type)47 AclDataType convert_to_c_data_type(DataType data_type)
48 {
49     switch(data_type)
50     {
51         case DataType::F32:
52             return AclDataType::AclFloat32;
53         case DataType::F16:
54             return AclDataType::AclFloat16;
55         case DataType::BFLOAT16:
56             return AclDataType::AclBFloat16;
57         default:
58             return AclDataType::AclDataTypeUnknown;
59     }
60 }
61 
create_legacy_tensor_shape(int32_t ndims,int32_t * shape)62 TensorShape create_legacy_tensor_shape(int32_t ndims, int32_t *shape)
63 {
64     TensorShape legacy_shape{};
65     for(int32_t d = 0; d < ndims; ++d)
66     {
67         legacy_shape.set(d, shape[d], false);
68     }
69     return legacy_shape;
70 }
create_tensor_shape_array(const TensorInfo & info)71 int32_t *create_tensor_shape_array(const TensorInfo &info)
72 {
73     const auto num_dims = info.num_dimensions();
74     if(num_dims <= 0)
75     {
76         return nullptr;
77     }
78 
79     int32_t *shape_array = new int32_t[num_dims];
80 
81     for(size_t d = 0; d < num_dims; ++d)
82     {
83         shape_array[d] = info.tensor_shape()[d];
84     }
85 
86     return shape_array;
87 }
88 } // namespace
89 
convert_to_legacy_tensor_info(const AclTensorDescriptor & desc)90 TensorInfo convert_to_legacy_tensor_info(const AclTensorDescriptor &desc)
91 {
92     TensorInfo legacy_desc;
93     legacy_desc.init(create_legacy_tensor_shape(desc.ndims, desc.shape), 1, convert_to_legacy_data_type(desc.data_type));
94     return legacy_desc;
95 }
96 
convert_to_descriptor(const TensorInfo & info)97 AclTensorDescriptor convert_to_descriptor(const TensorInfo &info)
98 {
99     const auto          num_dims = info.num_dimensions();
100     AclTensorDescriptor desc
101     {
102         static_cast<int32_t>(num_dims),
103         create_tensor_shape_array(info),
104         convert_to_c_data_type(info.data_type()),
105         nullptr,
106         0
107     };
108     return desc;
109 }
110 
convert_to_activation_info(const AclActivationDescriptor & desc)111 ActivationLayerInfo convert_to_activation_info(const AclActivationDescriptor &desc)
112 {
113     ActivationLayerInfo::ActivationFunction act;
114     switch(desc.type)
115     {
116         case AclActivationType::AclIdentity:
117             act = ActivationLayerInfo::ActivationFunction::IDENTITY;
118             break;
119         case AclActivationType::AclLogistic:
120             act = ActivationLayerInfo::ActivationFunction::LOGISTIC;
121             break;
122         case AclActivationType::AclTanh:
123             act = ActivationLayerInfo::ActivationFunction::TANH;
124             break;
125         case AclActivationType::AclRelu:
126             act = ActivationLayerInfo::ActivationFunction::RELU;
127             break;
128         case AclActivationType::AclBoundedRelu:
129             act = ActivationLayerInfo::ActivationFunction::BOUNDED_RELU;
130             break;
131         case AclActivationType::AclLuBoundedRelu:
132             act = ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU;
133             break;
134         case AclActivationType::AclLeakyRelu:
135             act = ActivationLayerInfo::ActivationFunction::LEAKY_RELU;
136             break;
137         case AclActivationType::AclSoftRelu:
138             act = ActivationLayerInfo::ActivationFunction::SOFT_RELU;
139             break;
140         case AclActivationType::AclElu:
141             act = ActivationLayerInfo::ActivationFunction::ELU;
142             break;
143         case AclActivationType::AclAbs:
144             act = ActivationLayerInfo::ActivationFunction::ABS;
145             break;
146         case AclActivationType::AclSquare:
147             act = ActivationLayerInfo::ActivationFunction::SQUARE;
148             break;
149         case AclActivationType::AclSqrt:
150             act = ActivationLayerInfo::ActivationFunction::SQRT;
151             break;
152         case AclActivationType::AclLinear:
153             act = ActivationLayerInfo::ActivationFunction::LINEAR;
154             break;
155         case AclActivationType::AclHardSwish:
156             act = ActivationLayerInfo::ActivationFunction::HARD_SWISH;
157             break;
158         default:
159             return ActivationLayerInfo();
160     }
161 
162     return ActivationLayerInfo(act, desc.a, desc.b);
163 }
164 } // namespace detail
165 } // namespace arm_compute
166