forked from jiuyuan/InfiniTensor
feat: 实现子图合并
Signed-off-by: YdrMaster <ydrml@hotmail.com>
This commit is contained in:
parent
bd61cf4533
commit
72788e8e0a
|
@ -21,4 +21,4 @@ tensor.h 提供了这个图表示中张量的定义。张量的结构由形状
|
|||
|
||||
`Partition`、`Mutation` 和 `Rating` 三个类用于支持图的规则优化。这三个类本质是一样的,这种定义是为了对优化的不同阶段实现编译时的约束——一轮优化必须按划分→突变→评价的顺序依次执行每个操作一次。
|
||||
|
||||
这些类中保存的是一个 `Mutant` 的二维数组。每个 `Mutant` 是子图的一种突变体,存储了子图结构和评分。内层数组表示每个子图的多个变体,外层数组表示每张图的多个子图。显然,`Partition` 输入完整的图,并构建外层数组的结构,`Mutation` 将填充内层数组。`Rating` 填充每个突变体的得分,然后从低到高排序。这个操作会将得分最高的变体移动到 `vector` 的末尾,方便将其移出 `vector`。
|
||||
这些类中保存的是一个 `Mutant` 的二维数组。每个 `Mutant` 是子图的一种突变体,存储了子图结构和评分。内层数组表示每个子图的多个变体,外层数组表示每张图的多个子图。显然,`Partition` 输入完整的图,并构建外层数组的结构,`Mutation` 将填充内层数组。`Rating` 填充每个突变体的得分,然后从高到低排序。接下来可以用序号向量指导图的重建。
|
||||
|
|
|
@ -82,26 +82,50 @@ Mutant &Mutant::operator=(Mutant &&others) {
|
|||
return *this;
|
||||
}
|
||||
|
||||
Partition::Partition(Unigraph &&g, Func const &f) {
|
||||
auto graph = f(std::move(g));
|
||||
for (auto &sub : graph)
|
||||
this->graph.emplace_back().emplace_back(std::move(sub));
|
||||
template <class t> Vec<size_t> list_size(Vec<Vec<t>> const &list) {
|
||||
Vec<size_t> ans(list.size());
|
||||
std::transform(list.begin(), list.end(), ans.begin(),
|
||||
[](const auto &e) { return e.size(); });
|
||||
return ans;
|
||||
}
|
||||
|
||||
Mutation::Mutation(Partition &&p, Func const &f) : graph(std::move(p.graph)) {
|
||||
for (auto &sub : graph)
|
||||
Partition::Partition(Unigraph &&g, Func const &f) {
|
||||
auto mutant = f(std::move(g));
|
||||
for (auto &sub : mutant)
|
||||
this->mutant.emplace_back().emplace_back(std::move(sub));
|
||||
}
|
||||
|
||||
Vec<size_t> Partition::size() const { return list_size(mutant); }
|
||||
|
||||
Mutation::Mutation(Partition &&p, Func const &f) : mutant(std::move(p.mutant)) {
|
||||
for (auto &sub : mutant)
|
||||
for (auto &m : f(sub.front().graph))
|
||||
sub.emplace_back(std::move(m));
|
||||
}
|
||||
|
||||
Rating::Rating(Mutation &&m, Func const &f) : graph(std::move(m.graph)) {
|
||||
for (auto &sub : graph) {
|
||||
Vec<size_t> Mutation::size() const { return list_size(mutant); }
|
||||
|
||||
Rating::Rating(Mutation &&m, Func const &f) : mutant(std::move(m.mutant)) {
|
||||
for (auto &sub : mutant) {
|
||||
for (auto &c : sub)
|
||||
c.score = f(c.graph);
|
||||
std::sort(sub.begin(), sub.end());
|
||||
std::sort(sub.begin(), sub.end(), std::greater<Mutant>());
|
||||
}
|
||||
}
|
||||
|
||||
Vec<size_t> Rating::size() const { return list_size(mutant); }
|
||||
|
||||
Unigraph Rating::build(Vec<size_t> const &indices) const {
|
||||
const auto size = indices.size();
|
||||
if (size != mutant.size())
|
||||
throw "indices size wrong";
|
||||
Unigraph ans;
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
for (const auto &op : mutant.at(i).at(indices[i]).graph.operators)
|
||||
ans.push_operator(op.op_type, op.inputs, op.outputs);
|
||||
return ans;
|
||||
}
|
||||
|
||||
Vec<Unigraph> split_each(Unigraph &&g) {
|
||||
Vec<Unigraph> ans;
|
||||
for (auto &op : g.operators)
|
||||
|
|
|
@ -76,8 +76,8 @@ class Rating;
|
|||
|
||||
/// @brief Partitioned subgraphs.
|
||||
struct Partition {
|
||||
/// @brief 2D vector of Mutant instances for each partitioned subgraph.
|
||||
Vec<Vec<Mutant>> graph;
|
||||
/// @brief 2D vector of `Mutant` instances for each partitioned subgraph.
|
||||
Vec<Vec<Mutant>> mutant;
|
||||
|
||||
friend Mutation;
|
||||
|
||||
|
@ -92,12 +92,16 @@ struct Partition {
|
|||
/// @param arg1 A function that takes an unpartitioned graph as input
|
||||
/// and returns a vector of partitioned subgraphs.
|
||||
Partition(Unigraph &&, Func const &);
|
||||
|
||||
/// @brief Returns mutant vector size.
|
||||
/// @return 2D vector size;
|
||||
Vec<size_t> size() const;
|
||||
};
|
||||
|
||||
/// @brief Generates mutants for every subgraph.
|
||||
class Mutation {
|
||||
/// @brief 2D vector of Mutant instances for each partitioned subgraph.
|
||||
Vec<Vec<Mutant>> graph;
|
||||
/// @brief 2D vector of `Mutant` instances for each partitioned subgraph.
|
||||
Vec<Vec<Mutant>> mutant;
|
||||
|
||||
friend Rating;
|
||||
|
||||
|
@ -111,12 +115,16 @@ class Mutation {
|
|||
/// @param arg1 A function that takes a subgraph as input
|
||||
/// and returns a vector of mutated graphs.
|
||||
Mutation(Partition &&, Func const &);
|
||||
|
||||
/// @brief Returns mutant vector size.
|
||||
/// @return 2D vector size;
|
||||
Vec<size_t> size() const;
|
||||
};
|
||||
|
||||
/// @brief Rates each subgraph mutant.
|
||||
class Rating {
|
||||
/// @brief 2D vector of Mutant instances for each partitioned subgraph.
|
||||
Vec<Vec<Mutant>> graph;
|
||||
/// @brief 2D vector of `Mutant` instances for each partitioned subgraph.
|
||||
Vec<Vec<Mutant>> mutant;
|
||||
|
||||
public:
|
||||
/// @brief A functional object that takes a mutated subgraph as input
|
||||
|
@ -128,6 +136,16 @@ class Rating {
|
|||
/// @param arg1 A function that takes a mutated subgraph as input
|
||||
/// and returns its score.
|
||||
Rating(Mutation &&, Func const &);
|
||||
|
||||
/// @brief Returns mutant vector size.
|
||||
/// @return 2D vector size;
|
||||
Vec<size_t> size() const;
|
||||
|
||||
/// @brief Builds `Unigraph` from the subgraphs
|
||||
/// with specified indices.
|
||||
/// @param arg0 Subgraph indices.
|
||||
/// @return Merged `Unigraph`.
|
||||
Unigraph build(Vec<size_t> const &) const;
|
||||
};
|
||||
|
||||
/// @brief Splits a graph into subgraphs, where each subgraph contains
|
||||
|
|
|
@ -30,6 +30,7 @@ int main() {
|
|||
auto m = Mutation(std::move(p),
|
||||
[](const auto &g) { return Vec<Unigraph>{}; });
|
||||
auto r = Rating(std::move(m), memory_usage);
|
||||
auto ans = r.build(Vec<size_t>(r.size().size(), 0));
|
||||
|
||||
return 0;
|
||||
} catch (const char *e) {
|
||||
|
|
Loading…
Reference in New Issue