Compute Library
 21.02
CPPTopKVKernel.cpp
Go to the documentation of this file.
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  */
25 
29 
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>
39 inline bool greater_than(T a, T b)
40 {
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 >
47 inline bool greater_than(T a, T b)
48 {
49  return (a > b);
50 }
51 
52 Status validate_arguments(const ITensorInfo *predictions, const ITensorInfo *targets, ITensorInfo *output, const unsigned int k)
53 {
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());
66  }
67 
68  return Status{};
69 }
70 } // namespace
71 
72 template <typename T>
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 
95  : _predictions(nullptr), _targets(nullptr), _output(nullptr), _k(), _batch_size(), _num_classes()
96 {
97 }
98 
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 
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 
125 {
126  return false;
127 }
128 
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;
147  run_topkv<int8_t>();
148  break;
149  default:
150  ARM_COMPUTE_ERROR("Not supported");
151  }
152 }
153 } // namespace arm_compute
CPPTopKVKernel()
Default constructor.
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
SimpleTensor< float > b
Definition: DFT.cpp:157
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 U8 per channel
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
decltype(strategy::transforms) typedef type
Interface for Neon tensor.
Definition: ITensor.h:36
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
bool is_parallelisable() const override
Indicates whether or not the kernel is parallelisable.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
1 channel, 1 U32 per channel
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
void configure(const ITensor *predictions, const ITensor *targets, ITensor *output, const unsigned int k)
Set the input and output of the kernel.
static Status validate(const ITensorInfo *predictions, const ITensorInfo *targets, ITensorInfo *output, const unsigned int k)
Static function to check if given info will lead to a valid configuration of CPPTopKVKernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
quantized, asymmetric fixed-point 8-bit number signed
Describe a multidimensional execution window.
Definition: Window.h:39