xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/operators/CpuGemmLowpOutputStage.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/cpu/operators/CpuGemmLowpOutputStage.h"
25 
26 #include "arm_compute/core/ITensor.h"
27 #include "arm_compute/core/Validate.h"
28 #include "arm_compute/runtime/NEON/NEScheduler.h"
29 #include "src/common/utils/Log.h"
30 #include "src/cpu/kernels/CpuGemmLowpQuantizeDownInt32ScaleKernel.h"
31 #include "src/cpu/kernels/CpuGemmLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel.h"
32 #include "src/cpu/kernels/CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel.h"
33 #include "src/cpu/kernels/CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel.h"
34 
35 namespace arm_compute
36 {
37 namespace cpu
38 {
configure(ITensorInfo * src,ITensorInfo * bias,ITensorInfo * dst,const GEMMLowpOutputStageInfo & info)39 void CpuGemmLowpOutputStage::configure(ITensorInfo *src, ITensorInfo *bias, ITensorInfo *dst, const GEMMLowpOutputStageInfo &info)
40 {
41     // Perform validate step
42     ARM_COMPUTE_ERROR_THROW_ON(CpuGemmLowpOutputStage::validate(src, bias, dst, info));
43     ARM_COMPUTE_LOG_PARAMS(src, bias, dst, info);
44 
45     switch(info.type)
46     {
47         case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
48         {
49             switch(info.output_data_type)
50             {
51                 case DataType::QASYMM8:
52                 {
53                     auto k = std::make_unique<kernels::CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel>();
54                     k->configure(src, bias, dst, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_offset, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
55                     _kernel = std::move(k);
56                     break;
57                 }
58                 case DataType::QASYMM8_SIGNED:
59                 {
60                     auto k = std::make_unique<kernels::CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel>();
61                     k->configure(src, bias, dst, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_offset, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
62                     _kernel = std::move(k);
63                     break;
64                 }
65                 case DataType::QSYMM16:
66                 {
67                     auto k = std::make_unique<kernels::CpuGemmLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel>();
68                     k->configure(src, bias, dst, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
69                     _kernel = std::move(k);
70                     break;
71                 }
72                 default:
73                 {
74                     ARM_COMPUTE_ERROR("Unsupported output data type.");
75                     break;
76                 }
77             }
78             break;
79         }
80         case GEMMLowpOutputStageType::QUANTIZE_DOWN:
81         {
82             switch(info.output_data_type)
83             {
84                 case DataType::QASYMM8:
85                 case DataType::QASYMM8_SIGNED:
86                 {
87                     auto k = std::make_unique<kernels::CpuGemmLowpQuantizeDownInt32ScaleKernel>();
88                     k->configure(src, bias, dst, &info);
89                     _kernel = std::move(k);
90                     break;
91                 }
92                 default:
93                 {
94                     ARM_COMPUTE_ERROR("Unsupported output data type.");
95                     break;
96                 }
97             }
98             break;
99         }
100         default:
101             ARM_COMPUTE_ERROR("Unsupported GEMMLowpOutputStage type.");
102     }
103 }
104 
validate(const ITensorInfo * src,const ITensorInfo * bias,const ITensorInfo * dst,const GEMMLowpOutputStageInfo & info)105 Status CpuGemmLowpOutputStage::validate(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, const GEMMLowpOutputStageInfo &info)
106 {
107     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
108     ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::UNKNOWN, "CpuGemmLowpOutputStage cannot be used with UNKNOWN output data type.");
109     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16);
110     ARM_COMPUTE_RETURN_ERROR_ON((info.type != GEMMLowpOutputStageType::QUANTIZE_DOWN) && (info.type != GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT));
111 
112     switch(info.type)
113     {
114         case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
115         {
116             switch(dst->data_type())
117             {
118                 case DataType::QASYMM8:
119                     return kernels::CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(src, bias, dst, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
120                 case DataType::QASYMM8_SIGNED:
121                     return kernels::CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(src, bias, dst, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
122                 case DataType::QSYMM16:
123                     return kernels::CpuGemmLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::validate(src, bias, dst, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
124                 default:
125                     return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported output data type.");
126             }
127         }
128         case GEMMLowpOutputStageType::QUANTIZE_DOWN:
129         {
130             switch(dst->data_type())
131             {
132                 case DataType::QASYMM8:
133                 case DataType::QASYMM8_SIGNED:
134                     return kernels::CpuGemmLowpQuantizeDownInt32ScaleKernel::validate(src, bias, dst, &info);
135                 default:
136                     return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported output data type.");
137             }
138         }
139         default:
140             return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported GEMMLowpOutputStage type.");
141     }
142 }
143 
run(ITensorPack & tensors)144 void CpuGemmLowpOutputStage::run(ITensorPack &tensors)
145 {
146     NEScheduler::get().schedule_op(_kernel.get(), Window::DimY, _kernel->window(), tensors);
147 }
148 } // namespace cpu
149 } // namespace arm_compute