Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion paddle/cinn/common/broadcast_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ using Pattern2Placement = std::unordered_map<symbol::DimExpr, symbol::DimExpr>;
Pattern2Placement ConstructCstrLhsEqRhsReplacement(
const symbol::Broadcastable<symbol::DimExpr>& broadcastable_condition) {
auto [lhs, rhs] = *broadcastable_condition;
if (lhs.isa<std::string>()) return Pattern2Placement{{lhs, rhs}};
if (rhs.isa<std::string>()) return Pattern2Placement{{rhs, lhs}};
if (lhs.isa<std::string>()) return Pattern2Placement{{lhs, rhs}};
return Pattern2Placement{{lhs, rhs}};
}

Expand Down Expand Up @@ -295,4 +295,54 @@ BroadcastTree ConstructBroadcastTree(const BroadcastLeaf& leaves) {
return ConstructBroadcastBranch(broadcastable_condition.value(), leaves);
}

namespace {

std::string ToTxtStringImpl(const BroadcastBranch<BroadcastTree>& branch) {
std::stringstream ss;
const auto& [cstr, lhs_eq_rhs, lhs_eq_one, rhs_eq_one] = branch.tuple();
const auto& [lhs, rhs] = *cstr;
const auto& Put = [&](const std::string& key, const auto& value) {
ss << "\"" << key << "\": ";
ss << ToTxtString(value);
ss << ",\n ";
};
ss << "{";
ss << "\"$lhs\": " << lhs << ",\n ";
ss << "\"$rhs\": " << rhs << ",\n ";
Put("$lhs == $rhs", lhs_eq_rhs);
Put("$lhs == 1", lhs_eq_one);
Put("$rhs == 1", rhs_eq_one);
ss << "}";
return ss.str();
}

std::string ToTxtStringImpl(const BroadcastLeaf& leaf) {
std::stringstream ss;
ss << "[";
for (const auto& dim_exprs : *leaf) {
ss << "[";
int j = 0;
for (const auto& dim_expr : dim_exprs) {
if (j++) {
ss << ",";
}
ss << dim_expr;
}
ss << "]";
}
ss << "]";
return ss.str();
}

} // namespace

std::string ToTxtString(const BroadcastTree& tree) {
return std::visit([&](const auto& impl) { return ToTxtStringImpl(impl); },
tree.variant());
}

std::ostream& operator<<(std::ostream& os, const BroadcastTree& tree) {
os << ToTxtString(tree);
}

} // namespace cinn::common
4 changes: 4 additions & 0 deletions paddle/cinn/common/broadcast_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ using BroadcastTree = adt::Tree<BroadcastBranch, BroadcastLeaf>;

BroadcastTree ConstructBroadcastTree(const BroadcastLeaf& leaves);

std::string ToTxtString(const BroadcastTree&);

std::ostream& operator<<(std::ostream& os, const BroadcastTree& tree);

} // namespace cinn::common