forked from jiuyuan/InfiniTensor
parent
17033fad97
commit
2a147c235d
|
@ -1,3 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include "../../src/mutation.h"
|
||||
#include "../../src/pass/single_operator.h"
|
||||
#include <unordered_set>
|
||||
|
||||
namespace optimization {
|
||||
|
||||
/// @brief Calculates the memory usage of a graph.
|
||||
/// @param arg0 The graph.
|
||||
/// @return The reciprocal of the total memory usage of the graph in bytes.
|
||||
inline float memory_usage(Unigraph const &g) {
|
||||
std::unordered_set<size_t> mark;
|
||||
uintptr_t memory;
|
||||
for (const auto &op : g.operators)
|
||||
for (const auto &t : op.outputs)
|
||||
if (mark.insert(reinterpret_cast<uintptr_t>(t.get())).second)
|
||||
memory += t->size();
|
||||
return 1e6f / static_cast<float>(memory);
|
||||
}
|
||||
|
||||
} // namespace optimization
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
#include "mutation.h"
|
||||
#include <unordered_set>
|
||||
|
||||
using namespace optimization;
|
||||
|
||||
Vec<std::pair<Unigraph, SingleOperator>>
|
||||
optimization::split_each(Unigraph &&g) {
|
||||
Vec<std::pair<Unigraph, SingleOperator>> ans;
|
||||
for (auto &op : g.operators) {
|
||||
auto &[g, t] = ans.emplace_back();
|
||||
g.push_operator(op.op_type, op.inputs, op.outputs);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
float optimization::memory_usage(Unigraph const &g) {
|
||||
std::unordered_set<size_t> mark;
|
||||
uintptr_t memory;
|
||||
for (const auto &op : g.operators)
|
||||
for (const auto &t : op.outputs)
|
||||
if (mark.insert(reinterpret_cast<uintptr_t>(t.get())).second)
|
||||
memory += t->size();
|
||||
return 1e6f / static_cast<float>(memory);
|
||||
}
|
|
@ -147,20 +147,6 @@ template <class PartitionType> class Rating {
|
|||
}
|
||||
};
|
||||
|
||||
/// @brief Partition every operator as a `Unigraph`.
|
||||
struct SingleOperator {};
|
||||
|
||||
/// @brief Splits a graph into subgraphs, where each subgraph contains
|
||||
/// only one operator.
|
||||
/// @param arg0 An unpartitioned graph.
|
||||
/// @return A vector of individual subgraphs.
|
||||
Vec<std::pair<Unigraph, SingleOperator>> split_each(Unigraph &&);
|
||||
|
||||
/// @brief Calculates the memory usage of a graph.
|
||||
/// @param arg0 The graph.
|
||||
/// @return The reciprocal of the total memory usage of the graph in bytes.
|
||||
float memory_usage(Unigraph const &);
|
||||
|
||||
template <class t> Vec<size_t> list_size(Vec<SubGraph<t>> const &list) {
|
||||
Vec<size_t> ans(list.size());
|
||||
std::transform(list.begin(), list.end(), ans.begin(),
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
#include "single_operator.h"
|
||||
|
||||
using namespace optimization;
|
||||
using namespace pass;
|
||||
|
||||
Vec<std::pair<Unigraph, SingleOperator>>
|
||||
optimization::pass::split_each(Unigraph &&g) {
|
||||
Vec<std::pair<Unigraph, SingleOperator>> ans;
|
||||
for (auto &op : g.operators) {
|
||||
auto &[g, t] = ans.emplace_back();
|
||||
g.push_operator(op.op_type, op.inputs, op.outputs);
|
||||
}
|
||||
return ans;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#include "../mutation.h"
|
||||
|
||||
namespace optimization::pass {
|
||||
|
||||
/// @brief Partition every operator as a `Unigraph`.
|
||||
struct SingleOperator {};
|
||||
|
||||
/// @brief Splits a graph into subgraphs, where each subgraph contains
|
||||
/// only one operator.
|
||||
/// @param arg0 An unpartitioned graph.
|
||||
/// @return A vector of individual subgraphs.
|
||||
Vec<std::pair<Unigraph, SingleOperator>> split_each(Unigraph &&);
|
||||
|
||||
} // namespace optimization::pass
|
|
@ -1,4 +1,4 @@
|
|||
#include "../src/mutation.h"
|
||||
#include "../include/optimization/common.h"
|
||||
#include <iostream>
|
||||
#include <unordered_set>
|
||||
|
||||
|
@ -28,11 +28,12 @@ int main() {
|
|||
{c} // outputs
|
||||
);
|
||||
|
||||
auto p = Partition<SingleOperator>(std::move(g), split_each);
|
||||
auto m = Mutation<SingleOperator>(
|
||||
auto p =
|
||||
Partition<pass::SingleOperator>(std::move(g), pass::split_each);
|
||||
auto m = Mutation<pass::SingleOperator>(
|
||||
std::move(p),
|
||||
[](const auto &g, const auto &t) { return Vec<Unigraph>{}; });
|
||||
auto r = Rating<SingleOperator>(std::move(m), memory_usage);
|
||||
auto r = Rating<pass::SingleOperator>(std::move(m), memory_usage);
|
||||
auto ans = r.build(Vec<size_t>(r.size().size(), 0));
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in New Issue