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.92】【BUAA】 Add nll_loss op #67884

Merged
merged 6 commits into from
Sep 6, 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 @@ -2041,6 +2041,75 @@ bool MemoryEfficientAttentionOpInferSymbolicShape(

return true;
}

bool NllLossOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const symbol::ShapeOrDataDimExprs &x_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
const std::vector<symbol::DimExpr> &x_shape = x_shape_or_data.shape();
const symbol::ShapeOrDataDimExprs &label_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(1));
const std::vector<symbol::DimExpr> &label_shape = label_shape_or_data.shape();
PADDLE_ENFORCE_EQ(x_shape.size() == 2 || x_shape.size() == 4,
true,
phi::errors::InvalidArgument(
"The tensor rank of Input(X) must be 2 or 4."));
infer_context->AddEqualCstr(x_shape[0], label_shape[0]);

if (op->operand_source(2)) {
const symbol::ShapeOrDataDimExprs &w_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(2));
const std::vector<symbol::DimExpr> &w_shape = w_shape_or_data.shape();
PADDLE_ENFORCE_EQ(
w_shape.size(),
1,
phi::errors::InvalidArgument("Input(Weight) should be a 1D tensor."));

infer_context->AddEqualCstr(x_shape[1], w_shape[0]);
}

const std::string &reduction =
op->attribute<pir::StrAttribute>("reduction").AsString();

std::vector<symbol::DimExpr> out_shape;
if (x_shape.size() == 2) {
if (reduction == "none") {
out_shape = {x_shape[0]};
} else {
out_shape = std::vector<symbol::DimExpr>{};
}
infer_context->SetShapeOrDataForValue(
op->result(0),
symbol::ShapeOrDataDimExprs{
symbol::TensorShapeOrDataDimExprs(out_shape)});
} else if (x_shape.size() == 4) {
PADDLE_ENFORCE_EQ(label_shape.size(),
3,
phi::errors::InvalidArgument(
"Expected Input(Label) dimensions=3, received %d.",
label_shape.size()));

infer_context->AddEqualCstr(x_shape[0], label_shape[0]);
infer_context->AddEqualCstr(x_shape[2], label_shape[1]);
infer_context->AddEqualCstr(x_shape[3], label_shape[2]);

if (reduction == "none") {
out_shape = {x_shape[0], x_shape[2], x_shape[3]};
} else {
out_shape = std::vector<symbol::DimExpr>{};
}
infer_context->SetShapeOrDataForValue(
op->result(0),
symbol::ShapeOrDataDimExprs{
symbol::TensorShapeOrDataDimExprs(out_shape)});
}
infer_context->SetShapeOrDataForValue(
op->result(1),
symbol::ShapeOrDataDimExprs{
symbol::TensorShapeOrDataDimExprs(std::vector<symbol::DimExpr>{})});
return true;
}

bool RoiPoolOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const auto &x_shape =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(MovingAverageAbsMaxScale)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(MovingAverageAbsMaxScale_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(NearestInterp)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Nce)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(NllLoss)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(PsroiPool)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(QuantizeLinear)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(QuantizeLinear_)
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 @@ -3429,6 +3429,7 @@
data_type : input
optional : weight
backward : nll_loss_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : nms
args : (Tensor x, float threshold = 1.0f)
Expand Down