-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
【BUAA】【Infer Symbolic Shape No.152,153】Add max_pool2d_with_index and max_pool3d_with_index #67390
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e6ce04f
update cinn api
Fripping aa09b54
debug
Fripping 720cc77
a
Fripping d835694
update cinn api
Fripping 507a647
Delete paddle/phi/ops/yaml/.ipynb_checkpoints directory
Fripping 8c019bf
Delete paddle/fluid/pir/dialect/operator/interface/infer_symbolic_sha…
Fripping 443b90c
Merge branch 'develop' into cin1
Fripping 459dbf5
Update unary_infer_sym.cc
Fripping 9db7494
Update unary_infer_sym.cc
Fripping ffe51c3
Update unary_infer_sym.cc
Fripping File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1414,6 +1414,155 @@ bool MinOpInferSymbolicShape(pir::Operation *op, | |
return MaxOpInferSymbolicShape(op, infer_context); | ||
} | ||
|
||
bool MaxPoolWithIndexOpInferSymbolicShape( | ||
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) { | ||
const auto &x_shape_or_data = | ||
infer_context->GetShapeOrDataForValue(op->operand_source(0)); | ||
const std::vector<symbol::DimExpr> &x_shape = x_shape_or_data.shape(); | ||
|
||
std::vector<int> paddings_ = | ||
paddle::dialect::details::GetVectorAttr<int>(op, "paddings"); | ||
std::vector<int> strides = | ||
paddle::dialect::details::GetVectorAttr<int>(op, "strides"); | ||
std::vector<int> kernel_sizes_ = | ||
paddle::dialect::details::GetVectorAttr<int>(op, "kernel_sizes"); | ||
|
||
std::vector<symbol::DimExpr> kernel_size_; | ||
int rank_kernel = kernel_sizes_.size(); | ||
for (int i = 0; i < rank_kernel; ++i) { | ||
kernel_size_.push_back(kernel_sizes_[i]); | ||
} | ||
|
||
bool adaptive = op->attribute<pir::BoolAttribute>("adaptive").data(); | ||
bool ceil_mode = op->attribute<pir::BoolAttribute>("ceil_mode").data(); | ||
bool global_pooling = | ||
op->attribute<pir::BoolAttribute>("global_pooling").data(); | ||
|
||
int rank_x = x_shape.size(); | ||
int rank = kernel_size_.size(); | ||
|
||
if (global_pooling) { | ||
kernel_size_.resize(rank_x - 2); | ||
for (int i = 0; i < rank; ++i) { | ||
paddings_[i] = 0; | ||
kernel_size_[i] = x_shape[i + 2]; | ||
} | ||
} | ||
|
||
PADDLE_ENFORCE_EQ( | ||
x_shape.size() - kernel_size_.size(), | ||
2U, | ||
phi::errors::InvalidArgument( | ||
"The input size %d minus the kernel size %d should equal to 2.", | ||
x_shape.size(), | ||
kernel_size_.size())); | ||
|
||
std::vector<symbol::DimExpr> out_shape = {x_shape[0], x_shape[1]}; | ||
|
||
if (adaptive) { | ||
out_shape.insert(out_shape.end(), kernel_size_.begin(), kernel_size_.end()); | ||
} else { | ||
for (int i = 0; i < rank; ++i) { | ||
PADDLE_ENFORCE_NE( | ||
strides[i], | ||
0, | ||
phi::errors::InvalidArgument( | ||
"The stride of MaxPool shall not be 0, but received %d.", | ||
strides[i])); | ||
if (ceil_mode) { | ||
out_shape.push_back( | ||
symbol::DimExpr((x_shape[i + 2] - kernel_size_[i] + | ||
2 * paddings_[i] + strides[i] - 1) / | ||
strides[i] + | ||
1)); | ||
} else { | ||
out_shape.push_back(symbol::DimExpr( | ||
(x_shape[i + 2] - kernel_size_[i] + 2 * paddings_[i]) / strides[i] + | ||
1)); | ||
} | ||
} | ||
} | ||
|
||
infer_context->SetShapeOrDataForValue( | ||
op->result(0), | ||
symbol::ShapeOrDataDimExprs{ | ||
symbol::TensorShapeOrDataDimExprs(out_shape)}); | ||
infer_context->SetShapeOrDataForValue( | ||
op->result(1), | ||
symbol::ShapeOrDataDimExprs{ | ||
symbol::TensorShapeOrDataDimExprs(out_shape)}); | ||
|
||
return true; | ||
} | ||
|
||
bool MaxPool2dWithIndexOpInferSymbolicShape( | ||
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) { | ||
const auto &x_shape_or_data = | ||
infer_context->GetShapeOrDataForValue(op->operand_source(0)); | ||
const std::vector<symbol::DimExpr> &x_shape = x_shape_or_data.shape(); | ||
|
||
PADDLE_ENFORCE_EQ( | ||
x_shape.size(), | ||
4, | ||
phi::errors::InvalidArgument("Pooling intput should be 4-D Tensor" | ||
"but received %dD-Tensor", | ||
x_shape.size())); | ||
|
||
std::vector<int> paddings_ = | ||
paddle::dialect::details::GetVectorAttr<int>(op, "paddings"); | ||
std::vector<int> strides = | ||
paddle::dialect::details::GetVectorAttr<int>(op, "strides"); | ||
|
||
PADDLE_ENFORCE_EQ( | ||
paddings_.size(), | ||
2, | ||
phi::errors::InvalidArgument( | ||
"It is expected paddings_size equals to 2, but got size %d", | ||
paddings_.size())); | ||
PADDLE_ENFORCE_EQ( | ||
strides.size(), | ||
2, | ||
phi::errors::InvalidArgument( | ||
"It is expected strides_size equals to 2, but got size %d", | ||
strides.size())); | ||
|
||
return MaxPoolWithIndexOpInferSymbolicShape(op, infer_context); | ||
} | ||
|
||
bool MaxPool3dWithIndexOpInferSymbolicShape( | ||
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) { | ||
Comment on lines
+1532
to
+1533
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 两个函数有很多相同逻辑,建议抽象点共同操作 |
||
const auto &x_shape_or_data = | ||
infer_context->GetShapeOrDataForValue(op->operand_source(0)); | ||
const std::vector<symbol::DimExpr> &x_shape = x_shape_or_data.shape(); | ||
|
||
PADDLE_ENFORCE_EQ( | ||
x_shape.size(), | ||
5, | ||
phi::errors::InvalidArgument("Pooling intput should be 5-D Tensor" | ||
"but received %dD-Tensor", | ||
x_shape.size())); | ||
|
||
std::vector<int> paddings_ = | ||
paddle::dialect::details::GetVectorAttr<int>(op, "paddings"); | ||
std::vector<int> strides = | ||
paddle::dialect::details::GetVectorAttr<int>(op, "strides"); | ||
|
||
PADDLE_ENFORCE_EQ( | ||
paddings_.size(), | ||
3, | ||
phi::errors::InvalidArgument( | ||
"It is expected paddings_size equals to 3, but got size %d", | ||
paddings_.size())); | ||
PADDLE_ENFORCE_EQ( | ||
strides.size(), | ||
3, | ||
phi::errors::InvalidArgument( | ||
"It is expected strides_size equals to 3, but got size %d", | ||
strides.size())); | ||
|
||
return MaxPoolWithIndexOpInferSymbolicShape(op, infer_context); | ||
} | ||
|
||
bool MeanAllOpInferSymbolicShape( | ||
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) { | ||
const auto &x_shape_or_data = | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kernel_size