ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw::rt::optimized_ops::TopContainer< T > Class Template Reference

class to define TopK operation More...

#include <topk_v2.h>

Public Member Functions

 TopContainer ()=delete
 Prevent default constructor of of this class.
 
 TopContainer (int32 k, int32 row_size)
 Constructor with params.
 
 TopContainer (const TopContainer &)=delete
 Prevent instances of this class from being copied (As this class contains pointers)
 
TopContaineroperator= (const TopContainer &)=delete
 
void start_collecting (const T *values)
 Start collecting.
 
void push (int32 a)
 Push a value to be compared for topk.
 
const std::vector< int32 > & sorted_result ()
 Get sorted result from pushed values.
 

Detailed Description

template<typename T>
class nnfw::rt::optimized_ops::TopContainer< T >

class to define TopK operation

Note
The follwing codes are impemented and modified while referring to TFLite topk_v2.cc file. TopK_v2 of NN Runtime supports TENSOR_FLOAT32, TENSOR_QUANT8_ASYMM, TENSOR_INT32 other than TFLite. (TFLite additionaly supports kTfLiteInt64.)

The class that collects top indexes of k values. Based on template tensorflow::gtl::TopN<> but, for optimization, it re-uses the same container.

Definition at line 46 of file topk_v2.h.

Constructor & Destructor Documentation

◆ TopContainer() [1/3]

template<typename T >
nnfw::rt::optimized_ops::TopContainer< T >::TopContainer ( )
delete

Prevent default constructor of of this class.

◆ TopContainer() [2/3]

template<typename T >
nnfw::rt::optimized_ops::TopContainer< T >::TopContainer ( int32  k,
int32  row_size 
)
inline

Constructor with params.

Parameters
[in]row_sizeSize of row in data
[in]kThe top k predictions

Definition at line 58 of file topk_v2.h.

58 : k_(k), container_(), values_(nullptr)
59 {
60 container_.reserve(std::min(k, row_size) + 1);
61 }

◆ TopContainer() [3/3]

template<typename T >
nnfw::rt::optimized_ops::TopContainer< T >::TopContainer ( const TopContainer< T > &  )
delete

Prevent instances of this class from being copied (As this class contains pointers)

Parameters
[in]topContainerTo copy

Member Function Documentation

◆ operator=()

template<typename T >
TopContainer & nnfw::rt::optimized_ops::TopContainer< T >::operator= ( const TopContainer< T > &  )
delete

◆ push()

template<typename T >
void nnfw::rt::optimized_ops::TopContainer< T >::push ( int32  a)
inline

Push a value to be compared for topk.

Parameters
[in]aA value to compare
Returns
N/A

Definition at line 91 of file topk_v2.h.

92 {
93 auto comparator = [this](int32 a, int32 b) { return compare_fun(a, b); };
94 if (container_.size() <= (size_t)k_)
95 {
96 container_.push_back(a);
97 if (container_.size() == (size_t)(k_ + 1))
98 {
99 std::make_heap(container_.begin(), container_.end(), comparator);
100 std::pop_heap(container_.begin(), container_.end(), comparator);
101 }
102 }
103 else if (comparator(a, container_.front()))
104 {
105 container_.back() = a;
106 std::push_heap(container_.begin(), container_.end(), comparator);
107 std::pop_heap(container_.begin(), container_.end(), comparator);
108 }
109 }
int32_t int32
Definition topk_v2.h:27

Referenced by nnfw::rt::optimized_ops::TopK().

◆ sorted_result()

template<typename T >
const std::vector< int32 > & nnfw::rt::optimized_ops::TopContainer< T >::sorted_result ( )
inline

Get sorted result from pushed values.

Returns
Reference of vector with sorted values

Definition at line 115 of file topk_v2.h.

116 {
117 auto comparator = [this](int32 a, int32 b) { return compare_fun(a, b); };
118 if (container_.size() <= (size_t)(k_))
119 {
120 std::sort(container_.begin(), container_.end(), comparator);
121 }
122 else
123 {
124 std::sort_heap(container_.begin(), container_.end() - 1, comparator);
125 container_.resize(k_);
126 }
127 return container_;
128 }

Referenced by nnfw::rt::optimized_ops::TopK().

◆ start_collecting()

template<typename T >
void nnfw::rt::optimized_ops::TopContainer< T >::start_collecting ( const T *  values)
inline

Start collecting.

Parameters
[in]valuesTo set as values
Returns
N/A

Definition at line 80 of file topk_v2.h.

81 {
82 values_ = values;
83 container_.clear();
84 }

Referenced by nnfw::rt::optimized_ops::TopK().


The documentation for this class was generated from the following file: