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

【Infer Symbolic Shape No.120】[BUAA] affine_grid #67364

Merged
merged 12 commits into from
Aug 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ std::vector<symbol::DimExpr> GetRealPadding(
return real_padding;
}

// bool AffineGridOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
// *infer_context) {
// // pass
// return true;
// }

symbol::ShapeOrDataDimExprs Pool2dRawInferSymbolicShape(
pir::Operation *op,
const std::vector<symbol::DimExpr> &kernel_size,
Expand Down Expand Up @@ -228,6 +221,42 @@ symbol::ShapeOrDataDimExprs Pool2dRawInferSymbolicShape(
namespace paddle::dialect {
using paddle::dialect::details::CreateShapeOrDataForXShape;

bool AffineGridOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const auto &input_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
std::vector<symbol::DimExpr> input_dims = input_shape_or_data.shape();

const auto &output_shape_or_data =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output_shape 是IntArray类型有可能从attribute传过来,加一个逻辑判断

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

infer_context->GetShapeOrDataForValue(op->operand_source(1));

const int64_t output_shape_size =
static_cast<int64_t>(output_shape_or_data.shape().size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

非必要cast,为啥不直接用int接收

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


std::vector<symbol::DimExpr> output_dims;
output_dims.push_back(input_dims[0]); // N

if (output_shape_size == 4) {
// N * H * W * 2
output_dims.push_back(infer_context->GetNextSymName()); // H
output_dims.push_back(infer_context->GetNextSymName()); // W
output_dims.push_back(symbol::DimExpr(2)); // 2
} else {
// N * D * H * W * 3
output_dims.push_back(infer_context->GetNextSymName()); // D
output_dims.push_back(infer_context->GetNextSymName()); // H
output_dims.push_back(infer_context->GetNextSymName()); // W
output_dims.push_back(symbol::DimExpr(3)); // 3
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D, H, W可以从output_shape中得到,再确认下吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


infer_context->SetShapeOrDataForValue(
op->result(0),
symbol::ShapeOrDataDimExprs{
symbol::TensorShapeOrDataDimExprs(output_dims)});

return true;
}

bool AllOpInferSymbolicShape(pir::Operation *op,
pir::InferSymbolicShapeContext *infer_context) {
const auto &axis = details::GetVectorAttr(op, "axis");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "paddle/pir/include/dialect/shape/utils/shape_analysis.h"

namespace paddle::dialect {
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(AffineGrid)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(AffineGrid)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(All)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Amax)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Amin)
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/ops/yaml/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
param : [input, output_shape, align_corners]
data_type : input
backward : affine_grid_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : all
args : (Tensor x, int64_t[] axis={}, bool keepdim=false)
Expand Down
2 changes: 1 addition & 1 deletion test/legacy_test/test_affine_grid_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def setUp(self):
}

def test_check_output(self):
self.check_output(check_pir=True)
self.check_output(check_pir=True, check_symbol_infer=False)

def test_check_grad_normal(self):
self.check_grad(
Expand Down