Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit ef0ecd7

Browse files
author
Theodoros Theodoridis
committed
Move mathematical functions to utils.h
1 parent 0336ede commit ef0ecd7

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

include/tc/autotuner/utils/utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ llvm::Optional<MappingOptions> getBestOptions(
5454
const std::vector<const DLTensor*>& inputs,
5555
const std::vector<const DLTensor*>& outputs);
5656

57+
double mean(std::vector<double>& v);
58+
double stdv(std::vector<double>& v, double mean);
59+
void normalizeVector(std::vector<double>& v);
60+
void sigmaScale(std::vector<double>& v);
61+
5762
} // namespace autotune
5863
} // namespace tc
5964

src/autotuner/genetic_search.cc

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <random>
2222
#include <sstream>
2323

24+
#include "tc/autotuner/utils/utils.h"
25+
2426
namespace tc {
2527
namespace autotune {
2628

@@ -74,39 +76,6 @@ void mutate(
7476
}
7577
}
7678

77-
double mean(std::vector<double>& v) {
78-
if (v.empty()) {
79-
throw std::invalid_argument("Cannot compute the mean of an empty vector.");
80-
}
81-
auto sum = std::accumulate(v.begin(), v.end(), 0.0);
82-
return sum / v.size();
83-
}
84-
85-
double stdv(std::vector<double>& v, double mean) {
86-
std::vector<double> diffs(v.size());
87-
std::transform(v.begin(), v.end(), diffs.begin(), [mean](double val) {
88-
return val - mean;
89-
});
90-
91-
auto squareSum =
92-
std::inner_product(diffs.begin(), diffs.end(), diffs.begin(), 0.0);
93-
return std::sqrt(squareSum / v.size());
94-
}
95-
96-
void sigmaScale(std::vector<double>& v) {
97-
auto m = mean(v);
98-
auto s = stdv(v, m);
99-
std::transform(v.begin(), v.end(), v.begin(), [m, s](double val) {
100-
return std::max(val - (m - 2 * s), 0.0);
101-
});
102-
}
103-
104-
void normalizeVector(std::vector<double>& v) {
105-
auto sum = std::accumulate(v.begin(), v.end(), 0.0);
106-
std::transform(
107-
v.begin(), v.end(), v.begin(), [sum](double v) { return v / sum; });
108-
}
109-
11079
std::vector<double> computeNormalizedFitness(
11180
const GeneticSearch::Population& population) {
11281
std::vector<double> fitness;

src/autotuner/utils/utils.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#include <algorithm>
1717
#include <cmath>
18+
#include <numeric>
1819

1920
#include "tc/aten/aten_compiler.h"
2021
#include "tc/autotuner/utils/utils.h"
@@ -108,5 +109,38 @@ llvm::Optional<MappingOptions> getBestOptions(
108109
return llvm::Optional<MappingOptions>{};
109110
}
110111

112+
double mean(std::vector<double>& v) {
113+
if (v.empty()) {
114+
throw std::invalid_argument("Cannot compute the mean of an empty vector.");
115+
}
116+
auto sum = std::accumulate(v.begin(), v.end(), 0.0);
117+
return sum / v.size();
118+
}
119+
120+
double stdv(std::vector<double>& v, double mean) {
121+
std::vector<double> diffs(v.size());
122+
std::transform(v.begin(), v.end(), diffs.begin(), [mean](double val) {
123+
return val - mean;
124+
});
125+
126+
auto squareSum =
127+
std::inner_product(diffs.begin(), diffs.end(), diffs.begin(), 0.0);
128+
return std::sqrt(squareSum / v.size());
129+
}
130+
131+
void sigmaScale(std::vector<double>& v) {
132+
auto m = mean(v);
133+
auto s = stdv(v, m);
134+
std::transform(v.begin(), v.end(), v.begin(), [m, s](double val) {
135+
return std::max(val - (m - 2 * s), 0.0);
136+
});
137+
}
138+
139+
void normalizeVector(std::vector<double>& v) {
140+
auto sum = std::accumulate(v.begin(), v.end(), 0.0);
141+
std::transform(
142+
v.begin(), v.end(), v.begin(), [sum](double v) { return v / sum; });
143+
}
144+
111145
} // namespace autotune
112146
} // namespace tc

0 commit comments

Comments
 (0)