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