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

[CINN] Add infer_symbol_shape for some ops #68166

Merged
merged 3 commits into from
Sep 23, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,45 @@ bool IndexPut_OpInferSymbolicShape(
return IndexPutOpInferSymbolicShape(op, infer_context);
}

bool LogLossOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const symbol::ShapeOrDataDimExprs &input_shape_or_data =
infer_context->GetShapeOrDataForValue(
op->operand_source(0)),

&label_shape_or_data =
infer_context->GetShapeOrDataForValue(
op->operand_source(1));

const auto &input_shape = input_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.

这里可以直接写成
const auto &input_shape = infer_context->GetShapeOrDataForValue(op->operand_source(0)).shape();

Copy link
Contributor

Choose a reason for hiding this comment

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

这里可以直接写成
const auto &input_shape = infer_context->GetShapeOrDataForValue(op->operand_source(0)).shape();

@GoldenStain 下一个PR改一下哈

const auto &label_shape = label_shape_or_data.shape();

PADDLE_ENFORCE_EQ(
input_shape.size() == 2 && label_shape.size() == 2,
true,
common::errors::InvalidArgument(
"The rank of input and label should both be 2, but received: "
"input: %d, label: %d\n",
input_shape.size(),
label_shape.size()));

for (int i = 0; i < 2; i++) {
infer_context->AddEqualCstr(input_shape[i], label_shape[i]);
}

Copy link
Contributor

@gongshaotian gongshaotian Sep 18, 2024

Choose a reason for hiding this comment

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

麻烦补充一个equalcstr(input_shape[1], dimexpr(1)); 的约束,对于动态维度,能添加约束信息就尽量添加

symbol::DimExpr one_dim = symbol::DimExpr{1};

infer_context->AddEqualCstr(input_shape[1], one_dim);
infer_context->AddEqualCstr(label_shape[1], one_dim);
Copy link
Contributor

Choose a reason for hiding this comment

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

这里其实添加一个就可以,上面已经添加了input_shape[1]和label_shape[1]的约束。可以放到下个pr修改


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

return true;
}

} // namespace paddle::dialect

namespace cinn::dialect {
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(IndexSample)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(IndexSelectStrided)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(KldivLoss)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Kron)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(LogLoss)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(Lstsq)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(LuUnpack)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(MatrixRankTol)
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 @@ -2889,6 +2889,7 @@
kernel :
func : log_loss
backward : log_loss_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : log_softmax
args : (Tensor x, int axis = -1)
Expand Down