Skip to content

Commit

Permalink
Merge pull request #48 from tc20042008/xk-cinn-trivalop-fuse
Browse files Browse the repository at this point in the history
add group_pattern_util.InferShardableAxesFromSink
  • Loading branch information
tc20042008 authored Mar 10, 2024
2 parents 604afab + 5b7dc57 commit 53ba3ed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
55 changes: 32 additions & 23 deletions paddle/cinn/frontend/group_pattern_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,8 @@ std::unordered_map<pir::Value, ShardableAxes> ReversedInferShardableAxes(
return value2shardable_axes;
}

common::TopoWalker<const pir::Operation*> GetOpsTopoWalker(const std::vector<const pir::Operation*>& ops) {
using Cache = std::unordered_set<const pir::Operation*>;
auto ops_set = std::make_shared<Cache>(ops.begin(), ops.end());
common::TopoWalker<const pir::Operation*> GetOpsTopoWalker(const std::unordered_set<const pir::Operation*>& ops) {
const auto* ops_set = &ops;
const auto VisitUpStreamInOps = [ops_set](const pir::Operation* op, const OpVisitor& DoEach) {
VisitInputOp(op, [&](const auto* input){
if (ops_set->count(input) == 0) return;
Expand All @@ -258,21 +257,26 @@ common::TopoWalker<const pir::Operation*> GetOpsTopoWalker(const std::vector<con
return reversed_walker;
}

std::list<const pir::Operation*> GetStarts(
const common::TopoWalker<const pir::Operation*>& topo_walker,
const std::vector<const pir::Operation*>& ops) {
const auto IsStart = [&](const pir::Operation* op) {
size_t num_prevs = 0;
topo_walker.VisitPrevNodes(op, [&](const auto*){ ++num_prevs; });
return num_prevs == 0;
std::list<const pir::Operation*> GetSinks(
const std::unordered_set<const pir::Operation*>& ops) {
const auto IsSink = [&](const pir::Operation* op) {
for (int i = 0; i < op->num_results(); ++i) {
pir::Value output = op->result(i);
for (auto consumer_it = output.use_begin(); consumer_it != output.use_end(); ++consumer_it) {
const auto* consumer_op = consumer_it->owner();
if (consumer_op->isa<pir::YieldOp>()) continue;
if (ops.count(consumer_op) > 0) return false;
}
}
return true;
};
std::list<const pir::Operation*> starts;
std::list<const pir::Operation*> sinks;
for (const auto* op : ops) {
if (IsStart(op)) {
starts.push_back(op);
if (IsSink(op)) {
sinks.push_back(op);
}
}
return starts;
return sinks;
}

class StmtFusionHelper {
Expand Down Expand Up @@ -617,17 +621,12 @@ class StmtFusionHelper {

ShardableAxesSignature GetShardableAxesSignature(const std::vector<const pir::Operation*>& ops) const {
std::unordered_set<const pir::Operation*> ops_set(ops.begin(), ops.end());
auto reversed_walker = GetOpsTopoWalker(ops);
const pir::Operation* sink = [&]{
const auto& sinks = GetStarts(reversed_walker, ops);
const auto& sinks = GetSinks(ops_set);
CHECK_EQ(sinks.size(), 1) << "ops must have only one sink node.";
return *sinks.begin();
}();
const auto& value2shardable_axes = [&]{
size_t rank = GetRank(sink->result(0));
const auto& init_sa = ShardableAxesUtil::GetFullyShardableAxes(rank);
return ReversedInferShardableAxes(reversed_walker, sink, init_sa);
}();
const auto& value2shardable_axes = InferShardableAxesFromSink(sink, ops_set);
const auto& IsInputOpOperand = [&](const auto* op, int input_idx) {
const auto& defining_op = op->operand_source(input_idx).defining_op();
return IsInThisFusionOp(defining_op) && ops_set.count(defining_op) == 0;
Expand Down Expand Up @@ -678,10 +677,20 @@ GroupPattern GenerateGroupPatternFromFusionOp(const cinn::dialect::FusionOp& fus
return FuseToGroupPattern(fusion_op);
}

std::unordered_map<pir::Value, ShardableAxes> InferShardableAxes(const std::vector<const pir::Operation*>& ops) {
std::unordered_map<pir::Value, ShardableAxes> InferShardableAxesFromSink(
const pir::Operation* sink,
const std::unordered_set<const pir::Operation*>& ops) {
auto reversed_walker = GetOpsTopoWalker(ops);
CHECK_GT(ops.count(sink), 0);
size_t rank = GetRank(sink->result(0));
const auto& init_sa = ShardableAxesUtil::GetFullyShardableAxes(rank);
return ReversedInferShardableAxes(reversed_walker, sink, init_sa);
}

std::unordered_map<pir::Value, ShardableAxes> InferShardableAxes(const std::unordered_set<const pir::Operation*>& ops) {
auto reversed_walker = GetOpsTopoWalker(ops);
const pir::Operation* sink = [&]{
const auto& sinks = GetStarts(reversed_walker, ops);
const auto& sinks = GetSinks(ops);
CHECK_EQ(sinks.size(), 1) << "ops must have only one sink node.";
return *sinks.begin();
}();
Expand Down
6 changes: 5 additions & 1 deletion paddle/cinn/frontend/group_pattern_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
namespace cinn::frontend {

GroupPattern GenerateGroupPatternFromFusionOp(const cinn::dialect::FusionOp&);
std::unordered_map<pir::Value, ShardableAxes> InferShardableAxes(const std::vector<const pir::Operation*>& ops);

std::unordered_map<pir::Value, ShardableAxes> InferShardableAxes(const std::unordered_set<const pir::Operation*>& ops);

std::unordered_map<pir::Value, ShardableAxes> InferShardableAxesFromSink(
const pir::Operation* sink,
const std::unordered_set<const pir::Operation*>& ops);
}

0 comments on commit 53ba3ed

Please sign in to comment.