Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

【Infer Symbolic Shape No.168】【BUAA】 Add partial sum op #67888

Merged
merged 7 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 @@ -2359,12 +2359,53 @@ bool PNormOpInferSymbolicShape(pir::Operation *op,
return true;
}

// bool PartialSumOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
// *infer_context) {
// // pass
// return true;
// }
bool PartialSumOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const symbol::TensorListShapeOrDataDimExprs &xs_shapes =
infer_context->GetShapeOrDataForValue(op->operand_source(0))
.dyn_cast<symbol::TensorListShapeOrDataDimExprs>();

int inputs_num = xs_shapes.size();
PADDLE_ENFORCE_GT(inputs_num,
0,
phi::errors::InvalidArgument(
"ShapeError: Input tensors count should > 0. But "
"received inputs' length is 0."));
if (inputs_num == 1) {
VLOG(3) << "Warning: partial_sum op have only one input, may be useless";
}

symbol::DimExpr batch_size = xs_shapes[0].shape()[0];
symbol::DimExpr input_len = xs_shapes[1].shape()[1];

for (int i = 0; i < inputs_num; i++) {
const std::vector<symbol::DimExpr> x_shape = xs_shapes[i].shape();
PADDLE_ENFORCE_EQ(
x_shape.size(),
2,
phi::errors::InvalidArgument("Only support two dimensions input now."));

if (i > 0) {
infer_context->AddEqualCstr(x_shape[0], batch_size);
infer_context->AddEqualCstr(x_shape[1], input_len);
}
}

int start_index = op->attribute<pir::Int32Attribute>("start_index").data();
int length = op->attribute<pir::Int32Attribute>("length").data();

std::vector<symbol::DimExpr> output_shape(2);
output_shape[0] = batch_size;
output_shape[1] = (length == -1) ? input_len - symbol::DimExpr(start_index)
: symbol::DimExpr(length);

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

return true;
}

bool PadOpInferSymbolicShape(pir::Operation *op,
pir::InferSymbolicShapeContext *infer_context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(OverlapAdd)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(PixelShuffle)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(PixelUnshuffle)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(PNorm)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(PartialSum)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(PartialSum)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Pad)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Pad3d)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Pool2d)
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 @@ -3584,6 +3584,7 @@
func : partial_sum
data_type : x
backward : partial_sum_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : pixel_shuffle
args : (Tensor x, int upscale_factor=1, str data_format="NCHW")
Expand Down