ONE - On-device Neural Engine
Loading...
Searching...
No Matches
record_minmax Namespace Reference

Data Structures

struct  DataBuffer
 
class  DataSetIterator
 
class  DirectoryIterator
 
class  HDF5Iterator
 
class  ListFileIterator
 
class  MinMaxComputer
 
class  MinMaxMap
 
class  MinMaxObserver
 
struct  MinMaxVectors
 
class  MovingAvgComputer
 
class  PercentileComputer
 
class  RandomIterator
 
class  RecordMinMax
 

Typedefs

using Buffer = std::vector< char >
 
using Output = std::vector< Buffer >
 
using WholeOutput = std::vector< Output >
 

Functions

std::unique_ptr< MinMaxComputermake_percentile_computer (float min_percentile, float max_percentile)
 
std::unique_ptr< MinMaxComputermake_moving_avg_computer (uint32_t batch_size, float moving_avg_const)
 
float getNthPercentile (std::vector< float > &vector, float percentile)
 getNthPercentile calculates the n-th percentile of input vector (0.0 <= n <= 100.0) linear interpolation is used when the desired percentile lies between two data points
 
float getMovingAverage (const std::vector< float > &vector, const float alpha, const uint8_t batch_size, bool is_min)
 getMovingAverage calculates the weighted moving average of input vector The initial value is the minimum (or maximum) value of the first batch of the vector
 
uint32_t numElements (const luci::CircleNode *node)
 
size_t getTensorSize (const luci::CircleNode *node)
 
void readDataFromFile (const std::string &filename, std::vector< char > &data, size_t data_size)
 
void checkInputDimension (const luci::CircleInput *input)
 

Typedef Documentation

◆ Buffer

using record_minmax::Buffer = typedef std::vector<char>

Definition at line 33 of file RecordMinMax.h.

◆ Output

using record_minmax::Output = typedef std::vector<Buffer>

Definition at line 34 of file RecordMinMax.h.

◆ WholeOutput

using record_minmax::WholeOutput = typedef std::vector<Output>

Definition at line 35 of file RecordMinMax.h.

Function Documentation

◆ checkInputDimension()

void record_minmax::checkInputDimension ( const luci::CircleInput input)

Definition at line 29 of file Utils.cpp.

30{
31 assert(input); // FIX_CALLER_UNLESS
32
33 for (uint32_t i = 0; i < input->rank(); i++)
34 if (!input->dim(i).known())
35 throw std::runtime_error(input->name() + " has unknown dimension");
36
37 if (numElements(input) == 0)
38 throw std::runtime_error(input->name() + " is a zero-sized input");
39}
uint32_t numElements(const luci::CircleNode *node)
Definition Utils.cpp:41

References numElements().

Referenced by record_minmax::RecordMinMax::profileData().

◆ getMovingAverage()

float record_minmax::getMovingAverage ( const std::vector< float > &  vector,
const float  alpha,
const uint8_t  batch_size,
bool  is_min 
)

getMovingAverage calculates the weighted moving average of input vector The initial value is the minimum (or maximum) value of the first batch of the vector

Definition at line 60 of file RecordFunction.cpp.

62{
63 assert(!vector.empty());
64 assert(alpha >= 0.0 && alpha <= 1.0);
65 assert(batch_size > 0);
66
67 auto getBatchMinOrMax = [&](uint32_t start_index) {
68 assert(start_index < vector.size());
69
70 float res = is_min ? std::numeric_limits<float>::max() : std::numeric_limits<float>::lowest();
71 for (uint32_t offset = 0; offset < batch_size; offset++)
72 {
73 uint32_t index = start_index + offset;
74 if (index >= vector.size())
75 break;
76
77 if (is_min)
78 {
79 res = vector[index] < res ? vector[index] : res;
80 }
81 else
82 {
83 res = vector[index] > res ? vector[index] : res;
84 }
85 }
86 return res;
87 };
88
89 float curr_avg = getBatchMinOrMax(0);
90 for (uint32_t i = batch_size; i < vector.size(); i += batch_size)
91 {
92 curr_avg = curr_avg * alpha + getBatchMinOrMax(i) * (1.0 - alpha);
93 }
94 return curr_avg;
95}
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540

References offset().

Referenced by record_minmax::MovingAvgComputer::update_qparam().

◆ getNthPercentile()

float record_minmax::getNthPercentile ( std::vector< float > &  vector,
float  percentile 
)

getNthPercentile calculates the n-th percentile of input vector (0.0 <= n <= 100.0) linear interpolation is used when the desired percentile lies between two data points

Definition at line 30 of file RecordFunction.cpp.

31{
32 if (percentile < 0 || percentile > 100)
33 throw std::runtime_error("Percentile must be ranged from 0 to 100");
34
35 if (vector.empty())
36 throw std::runtime_error("Percentile must take a non-empty vector as an argument");
37
38 if (vector.size() == 1)
39 return vector[0];
40
41 std::vector<float> copy;
42 copy.assign(vector.begin(), vector.end());
43 std::sort(copy.begin(), copy.end());
44
45 if (percentile == 0.0)
46 return copy.front();
47
48 if (percentile == 100.0)
49 return copy.back();
50
51 int index = static_cast<int>(std::floor((copy.size() - 1) * percentile / 100.0));
52
53 float percent_i = static_cast<float>(index) / static_cast<float>(copy.size() - 1);
54 float fraction =
55 (percentile / 100.0 - percent_i) / ((index + 1.0) / (copy.size() - 1.0) - percent_i);
56 float res = copy[index] + fraction * (copy[index + 1] - copy[index]);
57 return res;
58}

Referenced by record_minmax::PercentileComputer::update_qparam().

◆ getTensorSize()

size_t record_minmax::getTensorSize ( const luci::CircleNode node)

Definition at line 57 of file Utils.cpp.

58{
59 assert(node); // FIX_CALLER_UNLESS
60
61 uint32_t elem_size = luci::size(node->dtype());
62 return numElements(node) * elem_size;
63}
uint32_t size(loco::DataType data_type)
Returns the size of the data type.

References numElements(), and luci::size().

Referenced by record_minmax::DirectoryIterator::next(), record_minmax::HDF5Iterator::next(), record_minmax::ListFileIterator::next(), and record_minmax::RandomIterator::next().

◆ make_moving_avg_computer()

std::unique_ptr< MinMaxComputer > record_minmax::make_moving_avg_computer ( uint32_t  batch_size,
float  moving_avg_const 
)

Definition at line 80 of file MinMaxComputer.cpp.

82{
83 return std::make_unique<MovingAvgComputer>(batch_size, moving_avg_const);
84}

◆ make_percentile_computer()

std::unique_ptr< MinMaxComputer > record_minmax::make_percentile_computer ( float  min_percentile,
float  max_percentile 
)

Definition at line 75 of file MinMaxComputer.cpp.

76{
77 return std::make_unique<PercentileComputer>(min_percentile, max_percentile);
78}

◆ numElements()

uint32_t record_minmax::numElements ( const luci::CircleNode node)

Definition at line 41 of file Utils.cpp.

42{
43 assert(node); // FIX_CALLER_UNLESS
44
45 uint32_t num_elements = 1;
46 for (uint32_t i = 0; i < node->rank(); i++)
47 {
48 if (not node->dim(i).known())
49 throw std::runtime_error("Unknown dimension found in " + node->name());
50
51 num_elements *= node->dim(i).value();
52 }
53
54 return num_elements;
55}
NodeName name(void) const

References luci::CircleNode::name().

Referenced by checkInputDimension(), getTensorSize(), and record_minmax::RandomIterator::next().

◆ readDataFromFile()

void record_minmax::readDataFromFile ( const std::string &  filename,
std::vector< char > &  data,
size_t  data_size 
)

Definition at line 65 of file Utils.cpp.

66{
67 assert(data.size() == data_size); // FIX_CALLER_UNLESS
68
69 std::ifstream fs(filename, std::ifstream::binary);
70 if (fs.fail())
71 throw std::runtime_error("Cannot open file \"" + filename + "\".\n");
72 if (fs.read(data.data(), data_size).fail())
73 throw std::runtime_error("Failed to read data from file \"" + filename + "\".\n");
74 if (fs.peek() != EOF)
75 throw std::runtime_error("Input tensor size mismatches with \"" + filename + "\".\n");
76}

Referenced by record_minmax::DirectoryIterator::next(), and record_minmax::ListFileIterator::next().