xref: /aosp_15_r20/external/ComputeLibrary/src/core/CPP/kernels/CPPTopKVKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019-2020 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/CPP/kernels/CPPTopKVKernel.h"
25 
26 #include "arm_compute/core/Helpers.h"
27 #include "arm_compute/core/TensorInfo.h"
28 #include "arm_compute/core/utils/misc/Traits.h"
29 
30 #include "src/core/helpers/AutoConfiguration.h"
31 #include "src/core/helpers/WindowHelpers.h"
32 
33 namespace arm_compute
34 {
35 namespace
36 {
37 template <typename T,
38           typename std::enable_if<utils::traits::is_floating_point<T>::value, int>::type = 0>
greater_than(T a,T b)39 inline bool greater_than(T a, T b)
40 {
41     const T epsilon = std::numeric_limits<T>::epsilon();
42     return (a - b > epsilon);
43 }
44 
45 template < typename T,
46            typename std::enable_if < !utils::traits::is_floating_point<T>::value, int >::type = 0 >
greater_than(T a,T b)47 inline bool greater_than(T a, T b)
48 {
49     return (a > b);
50 }
51 
validate_arguments(const ITensorInfo * predictions,const ITensorInfo * targets,ITensorInfo * output,const unsigned int k)52 Status validate_arguments(const ITensorInfo *predictions, const ITensorInfo *targets, ITensorInfo *output, const unsigned int k)
53 {
54     ARM_COMPUTE_UNUSED(k);
55     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(predictions, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::S32, DataType::F16, DataType::F32);
56     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(targets, 1, DataType::U32);
57 
58     ARM_COMPUTE_RETURN_ERROR_ON(predictions->num_dimensions() > 2);
59     ARM_COMPUTE_RETURN_ERROR_ON(targets->num_dimensions() > 1);
60     ARM_COMPUTE_RETURN_ERROR_ON(targets->dimension(0) != predictions->dimension(1));
61     // Validate configured output
62     if(output->total_size() != 0)
63     {
64         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), targets->tensor_shape());
65         ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
66     }
67 
68     return Status{};
69 }
70 } // namespace
71 
72 template <typename T>
run_topkv()73 void CPPTopKVKernel::run_topkv()
74 {
75     for(unsigned int i = 0; i < _batch_size; ++i)
76     {
77         const auto target_class_id = *reinterpret_cast<uint32_t *>(_targets->ptr_to_element(Coordinates{ i }));
78         const auto predicted_value = *reinterpret_cast<T *>(_predictions->ptr_to_element(Coordinates{ target_class_id, i }));
79 
80         // The variable rank indicates how many values there are before the target_class_id
81         unsigned int rank = 0;
82         for(unsigned int j = 0; (j < _num_classes) && (rank < _k); ++j)
83         {
84             const auto current_prediction = *reinterpret_cast<T *>(_predictions->ptr_to_element(Coordinates{ j, i }));
85             if(greater_than(current_prediction, predicted_value))
86             {
87                 rank++;
88             }
89         }
90         *(_output->ptr_to_element(Coordinates{ i })) = static_cast<uint8_t>(rank < _k);
91     }
92 }
93 
CPPTopKVKernel()94 CPPTopKVKernel::CPPTopKVKernel()
95     : _predictions(nullptr), _targets(nullptr), _output(nullptr), _k(), _batch_size(), _num_classes()
96 {
97 }
98 
configure(const ITensor * predictions,const ITensor * targets,ITensor * output,const unsigned int k)99 void CPPTopKVKernel::configure(const ITensor *predictions, const ITensor *targets, ITensor *output, const unsigned int k)
100 {
101     ARM_COMPUTE_ERROR_ON_NULLPTR(predictions, targets, output);
102 
103     // Perform validation step
104     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(predictions->info(), targets->info(), output->info(), k));
105     auto_init_if_empty(*output->info(), targets->info()->tensor_shape(), 1, DataType::U8);
106 
107     _predictions = predictions;
108     _targets     = targets;
109     _output      = output;
110 
111     _k           = k;
112     _batch_size  = predictions->info()->dimension(1);
113     _num_classes = predictions->info()->dimension(0);
114 
115     ICPPKernel::configure(Window()); // Default 1 iteration window
116 }
117 
validate(const ITensorInfo * predictions,const ITensorInfo * targets,ITensorInfo * output,const unsigned int k)118 Status CPPTopKVKernel::validate(const ITensorInfo *predictions, const ITensorInfo *targets, ITensorInfo *output, const unsigned int k)
119 {
120     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(predictions, targets, output, k));
121     return Status{};
122 }
123 
is_parallelisable() const124 bool CPPTopKVKernel::is_parallelisable() const
125 {
126     return false;
127 }
128 
run(const Window & window,const ThreadInfo & info)129 void CPPTopKVKernel::run(const Window &window, const ThreadInfo &info)
130 {
131     ARM_COMPUTE_UNUSED(window, info);
132     switch(_predictions->info()->data_type())
133     {
134         case DataType::F32:
135             run_topkv<float>();
136             break;
137         case DataType::F16:
138             run_topkv<half>();
139             break;
140         case DataType::S32:
141             run_topkv<int>();
142             break;
143         case DataType::QASYMM8:
144             run_topkv<uint8_t>();
145             break;
146         case DataType::QASYMM8_SIGNED:
147             run_topkv<int8_t>();
148             break;
149         default:
150             ARM_COMPUTE_ERROR("Not supported");
151     }
152 }
153 } // namespace arm_compute
154