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

Commit a05e0cc

Browse files
author
Theodoros Theodoridis
committed
Move mathematical functions to utils.h
1 parent ea3453c commit a05e0cc

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

tc/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;

tc/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"
@@ -109,5 +110,38 @@ llvm::Optional<CudaMappingOptions> getBestOptions(
109110
return llvm::Optional<CudaMappingOptions>{};
110111
}
111112

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

tc/autotuner/utils/utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ struct OptionsWithMedianTime {
5959
std::vector<OptionsWithMedianTime> getOptionsAndMedianRuntimes(
6060
const lang::CanonicalTcString& id,
6161
const std::vector<const DLTensor*>& inputs);
62+
double mean(std::vector<double>& v);
63+
double stdv(std::vector<double>& v, double mean);
64+
void normalizeVector(std::vector<double>& v);
65+
void sigmaScale(std::vector<double>& v);
6266

6367
} // namespace autotune
6468
} // namespace tc

0 commit comments

Comments
 (0)