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.124】【BUAA】Add bmm, changed 3 files #67431

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,63 @@ bool Binomial_OpInferSymbolicShape(
// return true;
// }

// bool BmmOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext *infer_context) {
// // pass
// return true;
// }
bool BmmOpInferSymbolicShape(pir::Operation *op,
pir::InferSymbolicShapeContext *infer_context) {
const auto &x_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
const auto &y_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(1));

const std::vector<symbol::DimExpr> &x_dims = x_shape_or_data.shape();
Copy link
Contributor

Choose a reason for hiding this comment

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

命名不规范,x_shape

const std::vector<symbol::DimExpr> &y_dims = y_shape_or_data.shape();
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

std::size_t x_ndims = x_dims.size();
std::size_t y_ndims = y_dims.size();

PADDLE_ENFORCE_EQ(
x_ndims,
3,
common::errors::InvalidArgument("Input(X) of BmmOp must be 3-dimensional "
"in BmmOp, but received X's shape: [%d].",
x_ndims));
PADDLE_ENFORCE_EQ(
y_ndims,
3,
common::errors::InvalidArgument("Input(Y) of BmmOp must be 3-dimensional "
"in BmmOp, but received Y's shape: [%d].",
y_ndims));

auto cal_shape_fn = [](const symbol::DimExpr &x,
Copy link
Contributor

Choose a reason for hiding this comment

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

删掉这个lambda表达式,调用cal_shape_fn的地方都使用 addequalcstr

const symbol::DimExpr &y,
const std::string &error_str) -> symbol::DimExpr {
if (x == -1) {
return y;
} else if (y == -1) {
return x;
}
PADDLE_ENFORCE_EQ(x, y, common::errors::InvalidArgument(error_str, x, y));
return x;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

DimExpr不可能为-1,这段逻辑是想给两个DimExpr加约束


cal_shape_fn(x_dims[2],
Copy link
Contributor

Choose a reason for hiding this comment

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

修改为 equal constrain

y_dims[1],
"Input(X)'s width must be equal with Input(Y)'s height in "
"BmmOp, but receive X's width: [%d], Y's height: [%d].");
symbol::DimExpr batch_size = cal_shape_fn(
Copy link
Contributor

@gongshaotian gongshaotian Aug 19, 2024

Choose a reason for hiding this comment

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

同上,batch_size 直接取其中一个Dim就行

x_dims[0],
y_dims[0],
"Input(X) and Input(Y) must have the same batch size in BmmOp, but "
"received X's batch size: [%d], Y's batch size [%d]");
symbol::DimExpr out_height = x_dims[1];
symbol::DimExpr out_width = y_dims[2];

std::vector<symbol::DimExpr> out_dims = {batch_size, out_height, out_width};

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

return true;
}

// bool CholeskySolveOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(BoxClip)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Binomial)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Binomial_)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(Bincount)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(Bmm)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Bmm)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(CholeskySolve)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(CtcAlign)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Conv2d)
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 @@ -644,6 +644,7 @@
kernel :
func : bmm
backward : bmm_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : box_clip
args: (Tensor input, Tensor im_info)
Expand Down