xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/operators/CpuAddMulAdd.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2023 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 "arm_compute/core/experimental/Types.h"
25 #include "arm_compute/runtime/NEON/NEScheduler.h"
26 
27 #include "src/common/utils/Log.h"
28 #include "src/core/helpers/MemoryHelpers.h"
29 #include "src/cpu/kernels/CpuAddMulAddKernel.h"
30 #include "src/cpu/operators/CpuAddMulAdd.h"
31 #include "src/cpu/utils/CpuAuxTensorHandler.h"
32 
33 namespace arm_compute
34 {
35 namespace cpu
36 {
configure(const ITensorInfo * input1,const ITensorInfo * input2,const ITensorInfo * bn_mul,const ITensorInfo * bn_add,ITensorInfo * add_output,ITensorInfo * final_output,ConvertPolicy policy,const ActivationLayerInfo & act_info)37 void CpuAddMulAdd::configure(const ITensorInfo *input1, const ITensorInfo *input2,
38                              const ITensorInfo *bn_mul, const ITensorInfo *bn_add,
39                              ITensorInfo *add_output, ITensorInfo *final_output,
40                              ConvertPolicy policy, const ActivationLayerInfo &act_info)
41 {
42     ARM_COMPUTE_LOG_PARAMS(input1, input2, bn_mul, bn_add, add_output, final_output, policy, act_info);
43 
44     auto k = std::make_unique<kernels::CpuAddMulAddKernel>();
45 
46     const DataType data_type = input1->data_type();
47     if(is_data_type_quantized(data_type))
48     {
49         _dequantize_bn_mul.configure(bn_mul, &_dequantized_bn_mul);
50         _dequantize_bn_add.configure(bn_add, &_dequantized_bn_add);
51 
52         k->configure(input1, input2, &_dequantized_bn_mul, &_dequantized_bn_add, add_output, final_output, policy, act_info);
53 
54         // Save auxilary memory requirements after configuration
55         _aux_mem[DequantizedBnMul] = experimental::MemoryInfo(offset_int_vec(DequantizedBnMul), experimental::MemoryLifetime::Temporary, _dequantized_bn_mul.total_size());
56         _aux_mem[DequantizedBnAdd] = experimental::MemoryInfo(offset_int_vec(DequantizedBnAdd), experimental::MemoryLifetime::Temporary, _dequantized_bn_add.total_size());
57     }
58     else
59     {
60         k->configure(input1, input2, bn_mul, bn_add, add_output, final_output, policy, act_info);
61     }
62 
63     _kernel = std::move(k);
64 }
65 
validate(const ITensorInfo * input1,const ITensorInfo * input2,const ITensorInfo * bn_mul,const ITensorInfo * bn_add,const ITensorInfo * add_output,const ITensorInfo * final_output,ConvertPolicy policy,const ActivationLayerInfo & act_info)66 Status CpuAddMulAdd::validate(const ITensorInfo *input1, const ITensorInfo *input2,
67                               const ITensorInfo *bn_mul, const ITensorInfo *bn_add,
68                               const ITensorInfo *add_output, const ITensorInfo *final_output,
69                               ConvertPolicy policy, const ActivationLayerInfo &act_info)
70 {
71     const DataType data_type = input1->data_type();
72     if(is_data_type_quantized(data_type))
73     {
74         TensorInfo dequantized_bn_mul;
75         TensorInfo dequantized_bn_add;
76 
77         ARM_COMPUTE_RETURN_ON_ERROR(CpuDequantize::validate(bn_mul, &dequantized_bn_mul));
78         ARM_COMPUTE_RETURN_ON_ERROR(CpuDequantize::validate(bn_add, &dequantized_bn_add));
79 
80         return kernels::CpuAddMulAddKernel::validate(input1, input2, &dequantized_bn_mul, &dequantized_bn_add, add_output, final_output, policy, act_info);
81     }
82     else
83     {
84         return kernels::CpuAddMulAddKernel::validate(input1, input2, bn_mul, bn_add, add_output, final_output, policy, act_info);
85     }
86 }
87 
run(ITensorPack & tensors)88 void CpuAddMulAdd::run(ITensorPack &tensors)
89 {
90     const DataType data_type       = tensors.get_const_tensor(TensorType::ACL_SRC_0)->info()->data_type();
91 
92     if(is_data_type_quantized(data_type))
93     {
94         const ITensor *bn_mul = tensors.get_const_tensor(TensorType::ACL_SRC_2);
95         const ITensor *bn_add = tensors.get_const_tensor(TensorType::ACL_SRC_3);
96 
97         CpuAuxTensorHandler dequantized_bn_mul_handler(offset_int_vec(DequantizedBnMul), _dequantized_bn_mul, tensors, true);
98         CpuAuxTensorHandler dequantized_bn_add_handler(offset_int_vec(DequantizedBnAdd), _dequantized_bn_add, tensors, true);
99 
100         ITensorPack dequantize_mul_pack =
101         {
102             { TensorType::ACL_SRC_0, bn_mul },
103             { TensorType::ACL_DST_0, dequantized_bn_mul_handler.get() }
104         };
105 
106         ITensorPack dequantize_add_pack =
107         {
108             { TensorType::ACL_SRC_0, bn_add },
109             { TensorType::ACL_DST_0, dequantized_bn_add_handler.get() }
110         };
111 
112         _dequantize_bn_mul.run(dequantize_mul_pack);
113         _dequantize_bn_add.run(dequantize_add_pack);
114 
115         ITensorPack add_mul_add_pack =
116         {
117             { TensorType::ACL_SRC_0, tensors.get_const_tensor(TensorType::ACL_SRC_0) },
118             { TensorType::ACL_SRC_1, tensors.get_const_tensor(TensorType::ACL_SRC_1) },
119             { TensorType::ACL_SRC_2, dequantized_bn_mul_handler.get() },
120             { TensorType::ACL_SRC_3, dequantized_bn_add_handler.get() },
121             { TensorType::ACL_DST_0, tensors.get_tensor(TensorType::ACL_DST_0) },
122             { TensorType::ACL_DST_1, tensors.get_tensor(TensorType::ACL_DST_1) },
123         };
124 
125         NEScheduler::get().schedule_op(_kernel.get(), Window::DimY, _kernel->window(), add_mul_add_pack);
126     }
127     else
128     {
129         NEScheduler::get().schedule_op(_kernel.get(), Window::DimY, _kernel->window(), tensors);
130     }
131 }
132 
workspace() const133 experimental::MemoryRequirements CpuAddMulAdd::workspace() const
134 {
135     return _aux_mem;
136 }
137 
138 } // namespace cpu
139 } // namespace arm_compute
140