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.27][BUAA]edit_distance #67117

Merged
merged 17 commits into from
Aug 12, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,67 @@ bool ConcatOpInferSymbolicShape(pir::Operation *op,
return true;
}

bool EditDistanceOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const auto &hyps_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
const auto &refs_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(1));

const auto &hyps_dims = hyps_shape_or_data.shape();
const auto &refs_dims = refs_shape_or_data.shape();

PADDLE_ENFORCE_EQ(
hyps_dims.size(),
2,
phi::errors::InvalidArgument(
"Input(Hyps) must be a 2-D Tensor, but received rank %u.",
hyps_dims.size()));
PADDLE_ENFORCE_EQ(
refs_dims.size(),
2,
phi::errors::InvalidArgument(
"Input(Refs) must be a 2-D Tensor, but received rank %u.",
refs_dims.size()));

infer_context->AddEqualCstr(hyps_dims[0], refs_dims[0]);

bool has_lengths = op->operand_source(2) && op->operand_source(3);
if (has_lengths) {
const auto &hypslength_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(2));
const auto &refslength_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(3));

infer_context->AddEqualCstr(hypslength_shape_or_data.shape()[0],
hyps_dims[0]);

Copy link
Contributor

Choose a reason for hiding this comment

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

这个分支里这四个值的0维都是相等的,是不是少了一个约束

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我再添加一个 infer_context->AddEqualCstr(hypslength_shape_or_data, refslength_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.

可以

infer_context->AddEqualCstr(refslength_shape_or_data.shape()[0],
refs_dims[0]);

infer_context->AddEqualCstr(hypslength_shape_or_data.shape()[0],
refslength_shape_or_data.shape()[0]);
} else {
symbol::DimExpr one = symbol::DimExpr(1);

infer_context->AddEqualCstr(hyps_dims[1], one);
infer_context->AddEqualCstr(refs_dims[1], one);
}

symbol::ShapeOrDataDimExprs refs_shape_or_data_exprs(
symbol::TensorShapeOrDataDimExprs(
std::vector<symbol::DimExpr>{refs_dims}));
infer_context->SetShapeOrDataForValue(op->result(0),
refs_shape_or_data_exprs);
Comment on lines +832 to +836
Copy link
Contributor

Choose a reason for hiding this comment

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

这里命名最好用out_shape_or_data_exprs,out和refs的shape相同,你这里的操作相当于是把refs的shape区取出来给out的符号表达初始化,可以下一个PR顺带修改下

Copy link
Contributor Author

Choose a reason for hiding this comment

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

收到,我下一个 PR 改一下。


symbol::ShapeOrDataDimExprs single_dim_expr(symbol::TensorShapeOrDataDimExprs(
std::vector<symbol::DimExpr>{symbol::DimExpr(1)}));
infer_context->SetShapeOrDataForValue(
op->result(1), symbol::ShapeOrDataDimExprs({single_dim_expr}));

return true;
}

bool FakeQuantizeMovingAverageAbsMaxOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const symbol::ShapeOrDataDimExprs &x_shape =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(FakeQuantizeMovingAverageAbsMax)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(FakeQuantizeMovingAverageAbsMax_)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(CoalesceTensor)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(CoalesceTensor_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(EditDistance)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(FullWithTensor)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(FlashAttn)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(FlashAttnUnpadded)
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 @@ -1467,6 +1467,7 @@
func : edit_distance
data_type : DataType::FLOAT32
optional : hypslength, refslength
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : eig
args: (Tensor x)
Expand Down