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

Commit 3ad88e7

Browse files
author
Theodoros Theodoridis
committed
Move mathematical functions to utils.h
1 parent aa13b92 commit 3ad88e7

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

include/tc/autotuner/utils/utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "tc/core/cuda/cuda.h"
2222
#include "tc/core/cuda/cuda_mapping_options.h"
2323
#include "tc/core/utils/dlpack.h"
24+
#include "tc/lang/tree.h"
2425

2526
#include <llvm/ADT/Optional.h>
2627

@@ -59,6 +60,11 @@ llvm::Optional<CudaMappingOptions> getBestOptions(
5960
const std::vector<const DLTensor*>& inputs,
6061
const std::vector<const DLTensor*>& outputs);
6162

63+
double mean(std::vector<double>& v);
64+
double stdv(std::vector<double>& v, double mean);
65+
void normalizeVector(std::vector<double>& v);
66+
void sigmaScale(std::vector<double>& v);
67+
6268
} // namespace autotune
6369
} // namespace tc
6470

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"
@@ -128,5 +129,38 @@ llvm::Optional<CudaMappingOptions> getBestOptions(
128129
return llvm::Optional<CudaMappingOptions>{};
129130
}
130131

132+
double mean(std::vector<double>& v) {
133+
if (v.empty()) {
134+
throw std::invalid_argument("Cannot compute the mean of an empty vector.");
135+
}
136+
auto sum = std::accumulate(v.begin(), v.end(), 0.0);
137+
return sum / v.size();
138+
}
139+
140+
double stdv(std::vector<double>& v, double mean) {
141+
std::vector<double> diffs(v.size());
142+
std::transform(v.begin(), v.end(), diffs.begin(), [mean](double val) {
143+
return val - mean;
144+
});
145+
146+
auto squareSum =
147+
std::inner_product(diffs.begin(), diffs.end(), diffs.begin(), 0.0);
148+
return std::sqrt(squareSum / v.size());
149+
}
150+
151+
void sigmaScale(std::vector<double>& v) {
152+
auto m = mean(v);
153+
auto s = stdv(v, m);
154+
std::transform(v.begin(), v.end(), v.begin(), [m, s](double val) {
155+
return std::max(val - (m - 2 * s), 0.0);
156+
});
157+
}
158+
159+
void normalizeVector(std::vector<double>& v) {
160+
auto sum = std::accumulate(v.begin(), v.end(), 0.0);
161+
std::transform(
162+
v.begin(), v.end(), v.begin(), [sum](double v) { return v / sum; });
163+
}
164+
131165
} // namespace autotune
132166
} // namespace tc

0 commit comments

Comments
 (0)