Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ShapeOrDataDimExpr simplify unwork #62376

Merged
merged 10 commits into from
Mar 7, 2024
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
6 changes: 3 additions & 3 deletions paddle/cinn/hlir/dialect/operator/transforms/add_cinn_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ void ApplyCinnPreprocessPass(
if (has_dynamic_shape) {
pass_manager->AddPass(cinn::dialect::ir::CreateConvert0DTo1DPass());
pass_manager->AddPass(pir::CreateShapeOptimizationPass());
pass_manager->AddPass(cinn::dialect::ir::CreateSimplifyDimExprPass());
pass_manager->AddPass(
cinn::dialect::ir::CreateSubstituteDimExprBasedOnConstraintsPass());
pass_manager->AddPass(cinn::dialect::ir::CreateConvert0DTo1DPass());
pass_manager->AddPass(
cinn::dialect::ir::CreateFuseShapeOpsIntoGenerateShapeOpPass());
Expand Down Expand Up @@ -130,6 +127,9 @@ void ApplyGroupOpPass(::pir::Program* program,
cinn::dialect::ir::CreateFuseShapeOpsIntoGenerateShapeOpPass());
pass_manager->AddPass(
cinn::dialect::ir::CreateMoveGenerateShapeOpsToProloguePass());
pass_manager->AddPass(
cinn::dialect::ir::CreateSubstituteDimExprBasedOnConstraintsPass());
pass_manager->AddPass(cinn::dialect::ir::CreateSimplifyDimExprPass());
}

pass_manager->AddPass(cinn::dialect::ir::CreateDynamicReshapeOpPass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ namespace ir {
namespace {

template <typename DoEachT>
void VisitEachOp(pir::ModuleOp module_op, const DoEachT& DoEach) {
for (uint32_t i = 0; i < module_op->num_regions(); i++) {
for (pir::Block& block : module_op->region(i)) {
for (pir::Operation& op : block) {
DoEach(op);
void VisitEachOp(pir::Operation* op, const DoEachT& DoEach) {
for (uint32_t i = 0; i < op->num_regions(); i++) {
for (pir::Block& block : op->region(i)) {
for (pir::Operation& sub_op : block) {
DoEach(sub_op);
if (sub_op.num_regions() > 0) {
VisitEachOp(&sub_op, DoEach);
}
}
}
}
Expand Down Expand Up @@ -90,24 +93,36 @@ symbol::ShapeOrDataDimExprs SimplifyShapeOrData(
return std::visit(lambdas, shape_or_data.variant());
}

void SimplifyDimExpr(pir::ModuleOp module_op) {
void SimplifyDimExpr(pir::Operation* module_op) {
VLOG(4) << "SimplifyDimExpr start";
pir::ShapeConstraintIRAnalysis shape_analysis =
pir::ShapeAnalysisManager::Instance().Get(module_op.program());
pir::ShapeConstraintIRAnalysis* shape_analysis =
&pir::ShapeAnalysisManager::Instance().Get(
module_op->dyn_cast<pir::ModuleOp>().program());

VisitEachOp(module_op, [&](pir::Operation& op) {
VisitEachValue(op, [&](pir::Value value) {
if (!shape_analysis.HasShapeOrDataForValue(value)) {
if (!shape_analysis->HasShapeOrDataForValue(value)) {
VLOG(4) << "SimplifyDimExpr: shape_analysis can't find ShapeOrData for "
"value of the op:"
<< op.name();
} else {
const symbol::ShapeOrDataDimExprs& shape_or_data =
shape_analysis.GetShapeOrDataForValue(value);
shape_analysis->GetShapeOrDataForValue(value);
VLOG(8) << op.name() << " origin_shape_or_data: " << shape_or_data;
symbol::ShapeOrDataDimExprs simplified_shape_or_data =
SimplifyShapeOrData(shape_or_data);
shape_analysis.SetShapeOrDataForValue(value, simplified_shape_or_data);
VLOG(8) << op.name()
<< " simplified_shape_or_data: " << simplified_shape_or_data;
shape_analysis->SetShapeOrDataForValue(value, simplified_shape_or_data);
}
});
if (op.num_results() > 0) {
pir::shape::SetShapeAttrForOp(
&op, shape_analysis->GetShapeOrDataForValue(op.result(0)));
} else {
pir::shape::SetShapeAttrForOp(
&op, shape_analysis->GetShapeOrDataForValue(op.operand_source(0)));
}
// TODO(JiaWenxuan): simplify the attribute "sym_shape_str" of the op
});
VLOG(4) << "SimplifyDimExpr end";
Expand All @@ -117,10 +132,7 @@ class SimplifyDimExprPass : public pir::Pass {
public:
SimplifyDimExprPass() : pir::Pass("simplify_dim_expr_pass", 1) {}

void Run(pir::Operation* op) override {
pir::ModuleOp module_op = op->dyn_cast<pir::ModuleOp>();
SimplifyDimExpr(module_op);
}
void Run(pir::Operation* op) override { SimplifyDimExpr(op); }

bool CanApplyOn(pir::Operation* op) const override {
return op->isa<pir::ModuleOp>() && op->num_regions() > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "paddle/cinn/common/dim_expr_util.h"
#include "paddle/cinn/common/union_find.h"
#include "paddle/pir/include/dialect/shape/ir/shape_attribute.h"

namespace cinn {
namespace dialect {
Expand All @@ -26,11 +27,14 @@ namespace ir {
namespace {

template <typename DoEachT>
void VisitEachOp(pir::ModuleOp module_op, const DoEachT& DoEach) {
for (uint32_t i = 0; i < module_op->num_regions(); i++) {
for (pir::Block& block : module_op->region(i)) {
for (pir::Operation& op : block) {
DoEach(op);
void VisitEachOp(pir::Operation* op, const DoEachT& DoEach) {
for (uint32_t i = 0; i < op->num_regions(); i++) {
for (pir::Block& block : op->region(i)) {
for (pir::Operation& sub_op : block) {
DoEach(sub_op);
if (sub_op.num_regions() > 0) {
VisitEachOp(&sub_op, DoEach);
}
}
}
}
Expand Down Expand Up @@ -133,25 +137,39 @@ std::unordered_map<symbol::DimExpr, symbol::DimExpr> GetDimExprSubstitution(
return substitution_pattern;
}

void SubstituteDimExprBasedOnConstraints(pir::ModuleOp module_op) {
void SubstituteDimExprBasedOnConstraints(pir::Operation* module_op) {
VLOG(4) << "SubstituteDimExprBasedOnConstraints start";
pir::ShapeConstraintIRAnalysis shape_analysis =
pir::ShapeAnalysisManager::Instance().Get(module_op.program());
pir::ShapeConstraintIRAnalysis* shape_analysis =
&pir::ShapeAnalysisManager::Instance().Get(
module_op->dyn_cast<pir::ModuleOp>().program());
const std::unordered_map<symbol::DimExpr, symbol::DimExpr>&
substitution_pattern = GetDimExprSubstitution(&shape_analysis);
substitution_pattern = GetDimExprSubstitution(shape_analysis);

VisitEachOp(module_op, [&](pir::Operation& op) {
VisitEachValue(op, [&](pir::Value value) {
if (!shape_analysis.HasShapeOrDataForValue(value)) {
if (!shape_analysis->HasShapeOrDataForValue(value)) {
VLOG(4) << "Can not find ShapeOrData for value of op(" << op.name()
<< ") in shape_analysis";
} else {
const symbol::ShapeOrDataDimExprs& origin_shape_or_data =
shape_analysis.GetShapeOrDataForValue(value);
shape_analysis->GetShapeOrDataForValue(value);
VLOG(8) << op.name()
<< " origin_shape_or_data: " << origin_shape_or_data;
const symbol::ShapeOrDataDimExprs& substituted_shape_or_data =
SubstituteShapeOrData(origin_shape_or_data, substitution_pattern);
shape_analysis.SetShapeOrDataForValue(value, substituted_shape_or_data);
VLOG(8) << op.name()
<< " substituted_shape_or_data: " << substituted_shape_or_data;
shape_analysis->SetShapeOrDataForValue(value,
substituted_shape_or_data);
}
});
if (op.num_results() > 0) {
pir::shape::SetShapeAttrForOp(
&op, shape_analysis->GetShapeOrDataForValue(op.result(0)));
} else {
pir::shape::SetShapeAttrForOp(
&op, shape_analysis->GetShapeOrDataForValue(op.operand_source(0)));
}
// TODO(JiaWenxuan): substitute the attribute "sym_shape_str" of the op
});
VLOG(4) << "SubstituteDimExprBasedOnConstraints end";
Expand All @@ -163,8 +181,7 @@ class SubstituteDimExprBasedOnConstraintsPass : public pir::Pass {
: pir::Pass("substitute_dim_expr_based_on_constraints_pass", 1) {}

void Run(pir::Operation* op) override {
pir::ModuleOp module_op = op->dyn_cast<pir::ModuleOp>();
SubstituteDimExprBasedOnConstraints(module_op);
SubstituteDimExprBasedOnConstraints(op);
}

bool CanApplyOn(pir::Operation* op) const override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ bool ConcatOpInferSymbolicShape(
out_dims[axis] = out_dims[axis] + operand_shape_or_data.shape()[axis];
}

for (size_t i = 1; i < rank; ++i) {
for (size_t i = 0; i < rank; ++i) {
if (i == static_cast<size_t>(axis)) continue;
paddle::dialect::details::BuildCstrEqForTensorListAlongAxis(
shape_analysis, input_values, i);
Expand Down