perf: 没有变体就不用评分

Signed-off-by: YdrMaster <ydrml@hotmail.com>
This commit is contained in:
YdrMaster 2023-04-23 16:22:47 +08:00
parent 2a147c235d
commit 7d7d923e8d
4 changed files with 49 additions and 18 deletions

View File

@ -13,14 +13,18 @@ struct Mutant {
/// @brief A score representing the quality of the mutant.
float score;
Mutant(Unigraph &&g) : graph(std::move(g)) {}
Mutant(Unigraph &&g) : graph(std::move(g)), score(1.0f) {}
Mutant(Mutant const &) = delete;
Mutant(Mutant &&others) : graph(std::move(others.graph)) {}
Mutant(Mutant &&others)
: graph(std::move(others.graph)),
score(std::exchange(others.score, 1.0f)) {}
Mutant &operator=(Mutant const &) = delete;
Mutant &operator=(Mutant &&others) {
if (this != &others)
if (this != &others) {
this->graph = std::move(others.graph);
this->score = std::exchange(others.score, 1.0f);
}
return *this;
}
};
@ -113,17 +117,20 @@ template <class PartitionType> class Rating {
/// and returns its score.
Rating(Mutation<PartitionType> &&m, Func const &f)
: parts(std::move(m.parts)) {
for (auto &sub : parts) {
auto sum = 0.0f;
for (auto &c : sub.mutants)
sum += (c.score = f(c.graph));
sum = std::abs(sum);
for (auto &c : sub.mutants)
c.score /= sum;
std::sort(
sub.mutants.begin(), sub.mutants.end(),
[](auto const &a, auto const &b) { return a.score > b.score; });
}
for (auto &sub : parts)
if (sub.mutants.size() > 1) {
auto sum = 0.0f;
for (auto &c : sub.mutants)
sum += (c.score = f(c.graph));
sum = std::abs(sum);
for (auto &c : sub.mutants)
c.score /= sum;
std::sort(sub.mutants.begin(), sub.mutants.end(),
[](auto const &a, auto const &b) {
return a.score > b.score;
});
}
}
/// @brief Returns mutant vector size.

View File

@ -4,7 +4,7 @@ using namespace optimization;
using namespace pass;
Vec<std::pair<Unigraph, SingleOperator>>
optimization::pass::split_each(Unigraph &&g) {
optimization::pass::partition(Unigraph &&g) {
Vec<std::pair<Unigraph, SingleOperator>> ans;
for (auto &op : g.operators) {
auto &[g, t] = ans.emplace_back();
@ -12,3 +12,21 @@ optimization::pass::split_each(Unigraph &&g) {
}
return ans;
}
Vec<Unigraph> optimization::pass::mutate( // fmt: new line
Unigraph const &g, //
SingleOperator const & //
) {
Vec<Unigraph> ans;
switch (g.operators.front().op_type) {
case OpType::Conv:
/* code */
break;
default:
break;
}
return ans;
}

View File

@ -8,9 +8,15 @@ namespace optimization::pass {
struct SingleOperator {};
/// @brief Splits a graph into subgraphs, where each subgraph contains
/// only one operator.
/// only a single operator.
/// @param arg0 An unpartitioned graph.
/// @return A vector of individual subgraphs.
Vec<std::pair<Unigraph, SingleOperator>> split_each(Unigraph &&);
Vec<std::pair<Unigraph, SingleOperator>> partition(Unigraph &&);
/// @brief Mutates the single operator graph.
/// @param g The subgraph.
/// @param arg1 Never used.
/// @return Mutants.
Vec<Unigraph> mutate(Unigraph const &g, SingleOperator const &);
} // namespace optimization::pass

View File

@ -29,7 +29,7 @@ int main() {
);
auto p =
Partition<pass::SingleOperator>(std::move(g), pass::split_each);
Partition<pass::SingleOperator>(std::move(g), pass::partition);
auto m = Mutation<pass::SingleOperator>(
std::move(p),
[](const auto &g, const auto &t) { return Vec<Unigraph>{}; });