summaryrefslogtreecommitdiff
path: root/compiler/record-minmax/include/RecordFunction.h
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/record-minmax/include/RecordFunction.h')
-rw-r--r--compiler/record-minmax/include/RecordFunction.h77
1 files changed, 8 insertions, 69 deletions
diff --git a/compiler/record-minmax/include/RecordFunction.h b/compiler/record-minmax/include/RecordFunction.h
index d5da01ce0..745ee2282 100644
--- a/compiler/record-minmax/include/RecordFunction.h
+++ b/compiler/record-minmax/include/RecordFunction.h
@@ -14,12 +14,11 @@
* limitations under the License.
*/
+#ifndef __RECORD_MINMAX_RECORD_FUNCTION_H__
+#define __RECORD_MINMAX_RECORD_FUNCTION_H__
+
#include <vector>
-#include <cassert>
-#include <algorithm>
-#include <cmath>
-#include <limits>
-#include <stdexcept>
+#include <cstdint>
namespace record_minmax
{
@@ -28,75 +27,15 @@ namespace record_minmax
* @brief 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 getNthPercentile(std::vector<float> &vector, float percentile)
-{
- if (percentile < 0 || percentile > 100)
- throw std::runtime_error("Percentile must be ranged from 0 to 100");
-
- if (vector.empty())
- throw std::runtime_error("Percentile must take a non-empty vector as an argument");
-
- if (vector.size() == 1)
- return vector[0];
-
- std::vector<float> copy;
- copy.assign(vector.begin(), vector.end());
- std::sort(copy.begin(), copy.end());
-
- if (percentile == 0.0)
- return copy.front();
-
- if (percentile == 100.0)
- return copy.back();
-
- int index = static_cast<int>(std::floor((copy.size() - 1) * percentile / 100.0));
-
- float percent_i = static_cast<float>(index) / static_cast<float>(copy.size() - 1);
- float fraction =
- (percentile / 100.0 - percent_i) / ((index + 1.0) / (copy.size() - 1.0) - percent_i);
- float res = copy[index] + fraction * (copy[index + 1] - copy[index]);
- return res;
-}
+float getNthPercentile(std::vector<float> &vector, float percentile);
/**
* @brief 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
*/
float getMovingAverage(const std::vector<float> &vector, const float alpha,
- const uint8_t batch_size, bool is_min)
-{
- assert(!vector.empty());
- assert(alpha >= 0.0 && alpha <= 1.0);
- assert(batch_size > 0);
-
- auto getBatchMinOrMax = [&](uint32_t start_index) {
- assert(start_index < vector.size());
-
- float res = is_min ? std::numeric_limits<float>::max() : std::numeric_limits<float>::lowest();
- for (uint32_t offset = 0; offset < batch_size; offset++)
- {
- uint32_t index = start_index + offset;
- if (index >= vector.size())
- break;
-
- if (is_min)
- {
- res = vector[index] < res ? vector[index] : res;
- }
- else
- {
- res = vector[index] > res ? vector[index] : res;
- }
- }
- return res;
- };
-
- float curr_avg = getBatchMinOrMax(0);
- for (uint32_t i = batch_size; i < vector.size(); i += batch_size)
- {
- curr_avg = curr_avg * alpha + getBatchMinOrMax(i) * (1.0 - alpha);
- }
- return curr_avg;
-}
+ const uint8_t batch_size, bool is_min);
} // namespace record_minmax
+
+#endif // __RECORD_MINMAX_RECORD_FUNCTION_H__