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 BUAA No13、66、91、122】Add 4 ops #66877

Merged
merged 26 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5976307
Update unary_infer_sym.h
Guanhuachen2003 Jul 31, 2024
d6fc344
Update unary_infer_sym.cc
Guanhuachen2003 Jul 31, 2024
b132f90
Update ops.yaml
Guanhuachen2003 Jul 31, 2024
c813a54
Update unary_infer_sym.cc
Guanhuachen2003 Jul 31, 2024
ec646ce
Update multiary_infer_sym.cc
Guanhuachen2003 Jul 31, 2024
00b4875
Update multiary_infer_sym.h
Guanhuachen2003 Jul 31, 2024
9c74456
Update ops.yaml
Guanhuachen2003 Jul 31, 2024
edfb8c0
Update unary_infer_sym.h
Guanhuachen2003 Jul 31, 2024
ddfde35
Update unary_infer_sym.cc
Guanhuachen2003 Jul 31, 2024
6bbd633
Update ops.yaml
Guanhuachen2003 Jul 31, 2024
aaf8ebe
Update unary_infer_sym.cc
Guanhuachen2003 Aug 1, 2024
261d7a3
Merge branch 'develop' into cinn-rrelu
Guanhuachen2003 Aug 1, 2024
36266b5
Update multiary_infer_sym.cc
Guanhuachen2003 Aug 1, 2024
c469ecc
Update multiary_infer_sym.h
Guanhuachen2003 Aug 1, 2024
4a67064
Update static_ops.yaml
Guanhuachen2003 Aug 1, 2024
a67bf50
Merge branch 'PaddlePaddle:develop' into cinn-rrelu
Guanhuachen2003 Aug 4, 2024
cb2994c
Update multiary_infer_sym.cc
Guanhuachen2003 Aug 4, 2024
937c558
Merge branch 'PaddlePaddle:develop' into cinn-rrelu
Guanhuachen2003 Aug 4, 2024
8d39977
Update unary_infer_sym.cc
Guanhuachen2003 Aug 4, 2024
6e77d5e
Update multiary_infer_sym.cc
Guanhuachen2003 Aug 4, 2024
afb225a
Merge branch 'PaddlePaddle:develop' into cinn-rrelu
Guanhuachen2003 Aug 5, 2024
1972a11
Update multiary_infer_sym.cc
Guanhuachen2003 Aug 5, 2024
e7553a5
Merge branch 'develop' into cinn-rrelu
Guanhuachen2003 Aug 6, 2024
7b83438
Update check_finite_and_unscale using tensorlist
Guanhuachen2003 Aug 6, 2024
4cb1903
Merge branch 'develop' into cinn-rrelu
Guanhuachen2003 Aug 7, 2024
817594c
delete unnecessary paddle_enforce_eq
Guanhuachen2003 Aug 7, 2024
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 @@ -613,6 +613,36 @@ bool BilinearInterpOpInferSymbolicShape(
return BicubicInterpOpInferSymbolicShape(op, infer_context);
}

bool CheckFiniteAndUnscaleOpInferSymbolicShape(
Guanhuachen2003 marked this conversation as resolved.
Show resolved Hide resolved
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
std::vector<symbol::ShapeOrDataDimExprs> xs_shapes;
for (size_t i = 0; i < op->num_operands() - 1; ++i) {
Guanhuachen2003 marked this conversation as resolved.
Show resolved Hide resolved
xs_shapes.push_back(
infer_context->GetShapeOrDataForValue(op->operand_source(i)));
}
// Ensure the number of inputs (xs) matches the number of outputs (outs)
infer_context->AddEqualCstr(
Guanhuachen2003 marked this conversation as resolved.
Show resolved Hide resolved
symbol::DimExpr(static_cast<int>(xs_shapes.size())),
symbol::DimExpr(static_cast<int>(op->num_results() - 1)));
for (size_t i = 0; i < xs_shapes.size(); ++i) {
symbol::TensorShapeOrDataDimExprs output_shape(xs_shapes[i].shape());
infer_context->SetShapeOrDataForValue(
op->result(i), symbol::ShapeOrDataDimExprs{output_shape});
}

symbol::TensorShapeOrDataDimExprs found_infinite_shape({symbol::DimExpr(1)});
infer_context->SetShapeOrDataForValue(
op->result(op->num_results() - 1),
Guanhuachen2003 marked this conversation as resolved.
Show resolved Hide resolved
symbol::ShapeOrDataDimExprs{found_infinite_shape});

return true;
}

bool CheckFiniteAndUnscale_OpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
return CheckFiniteAndUnscaleOpInferSymbolicShape(op, infer_context);
}

bool CrossEntropyWithSoftmaxOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const symbol::ShapeOrDataDimExprs &input_shape =
Expand Down Expand Up @@ -1102,6 +1132,48 @@ bool MeshgridOpInferSymbolicShape(
return true;
}

bool MovingAverageAbsMaxScaleOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
// Get the symbolic shape of the input tensor x
const symbol::ShapeOrDataDimExprs &x_shape =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
// Get the symbolic shape of the input tensor in_state
const symbol::ShapeOrDataDimExprs &in_state_shape =
infer_context->GetShapeOrDataForValue(op->operand_source(2));
// Get the symbolic shape of the input tensor in_accum
const symbol::ShapeOrDataDimExprs &in_accum_shape =
infer_context->GetShapeOrDataForValue(op->operand_source(1));

// Set the shape for the output tensor out if it exists
if (op->num_results() > 0 && op->result(0) != nullptr) {
Guanhuachen2003 marked this conversation as resolved.
Show resolved Hide resolved
infer_context->SetShapeOrDataForValue(op->result(0), x_shape);
}

// Set the shape for the output tensor out_scale as a scalar if it exists
if (op->num_results() > 1 && op->result(1) != nullptr) {
symbol::TensorShapeOrDataDimExprs scalar_shape(
std::vector<symbol::DimExpr>{symbol::DimExpr(1)});
infer_context->SetShapeOrDataForValue(op->result(1), scalar_shape);
}

// Set the shape for the output tensor out_state if it exists
if (op->num_results() > 2 && op->result(2) != nullptr) {
Guanhuachen2003 marked this conversation as resolved.
Show resolved Hide resolved
infer_context->SetShapeOrDataForValue(op->result(2), in_state_shape);
}

// Set the shape for the output tensor out_accum if it exists
if (op->num_results() > 3 && op->result(3) != nullptr) {
infer_context->SetShapeOrDataForValue(op->result(3), in_accum_shape);
}

return true;
}

bool MovingAverageAbsMaxScale_OpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
return MovingAverageAbsMaxScaleOpInferSymbolicShape(op, infer_context);
}

bool StackOpInferSymbolicShape(pir::Operation *op,
pir::InferSymbolicShapeContext *infer_context) {
pir::Value operand_source = op->operand_source(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(BatchNorm_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(BicubicInterp)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Bilinear)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(BilinearInterp)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(CheckFiniteAndUnscale)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(CheckFiniteAndUnscale_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Concat)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(CrossEntropyWithSoftmax)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(CrossEntropyWithSoftmax_)
Expand All @@ -42,6 +44,8 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(LinearInterp)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Logspace)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(MemoryEfficientAttention)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Meshgrid)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(MovingAverageAbsMaxScale)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(MovingAverageAbsMaxScale_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(NearestInterp)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(RoiAlign)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Stack)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,22 @@ bool KthvalueOpInferSymbolicShape(
return true;
}

bool L1NormOpInferSymbolicShape(pir::Operation *op,
pir::InferSymbolicShapeContext *infer_context) {
// The output is a scalar, set the output shape accordingly
std::vector<symbol::DimExpr> output_shape;
infer_context->SetShapeOrDataForValue(
op->result(0),
symbol::ShapeOrDataDimExprs{
symbol::TensorShapeOrDataDimExprs(output_shape)});
return true;
}

bool L1Norm_OpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
return L1NormOpInferSymbolicShape(op, infer_context);
}

bool LpPool2dOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const auto &kernel_size = [&]() -> std::vector<symbol::DimExpr> {
Expand Down Expand Up @@ -1351,6 +1367,52 @@ bool ShapeOpInferSymbolicShape(pir::Operation *op,
return true;
}

bool RreluOpInferSymbolicShape(pir::Operation *op,
pir::InferSymbolicShapeContext *infer_context) {
float lower = op->attribute<pir::FloatAttribute>("lower").data();
float upper = op->attribute<pir::FloatAttribute>("upper").data();
const auto &x_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
const auto &x_shape = x_shape_or_data.shape();

// Check constraints for the attributes lower and upper
PADDLE_ENFORCE_GE(lower,
0,
phi::errors::InvalidArgument(
"The lower value should be greater than or equal to 0. "
"But received lower value = %f.",
lower));
PADDLE_ENFORCE_LE(upper,
1,
phi::errors::InvalidArgument(
"The upper value should be less than or equal to 1. "
"But received upper value = %f.",
upper));
PADDLE_ENFORCE_GE(
upper,
lower,
phi::errors::InvalidArgument(
"The upper value should be greater than or equal to lower value. "
"But received upper value = %f, lower value = %f.",
upper,
lower));

// Set the shape for the output tensor out
infer_context->SetShapeOrDataForValue(
op->result(0),
symbol::ShapeOrDataDimExprs{symbol::TensorShapeOrDataDimExprs(x_shape)});

// Set the shape for the output tensor noise if it exists
if (op->num_results() > 1 && op->result(1) != nullptr) {
infer_context->SetShapeOrDataForValue(
op->result(1),
symbol::ShapeOrDataDimExprs{
symbol::TensorShapeOrDataDimExprs(x_shape)});
}

return true;
}

bool ShapeSrOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
return ShapeOpInferSymbolicShape(op, infer_context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(Flatten_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(IdentityLoss)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(IdentityLoss_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Kthvalue)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(L1Norm)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(L1Norm_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(LpPool2d)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Logcumsumexp)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Logsumexp)
Expand All @@ -72,6 +74,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(Prod)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(RepeatInterleave)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Reshape)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Reshape_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Rrelu)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Shape)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(ShapeSr)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Slice)
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/ops/yaml/inconsistent/static_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@
param: [x, in_accum, in_state, moving_rate, is_test]
optional : in_accum, in_state, out, out_state, out_accum
inplace : (in_accum -> out_accum), (in_state -> out_state)
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op: nce
args: (Tensor input, Tensor label, Tensor weight, Tensor bias, Tensor sample_weight, Tensor custom_dist_probs, Tensor custom_dist_alias, Tensor custom_dist_alias_probs, int num_total_classes, int[] custom_neg_classes={}, int num_neg_samples=10, int sampler=0, int seed=0, bool is_sparse=false, bool remote_prefetch=false, bool is_test=false)
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/ops/yaml/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@
param : [x, scale]
data_type : x
inplace : (x -> out)
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : check_numerics
args : (Tensor tensor, str op_type = "", str var_name = "", int check_nan_inf_level = 0, int stack_height_limit = -1, str output_dir = "")
Expand Down Expand Up @@ -2623,6 +2624,7 @@
data_type : x
inplace: (x -> out)
backward : l1_norm_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : label_smooth
args : (Tensor label, Tensor prior_dist, float epsilon = 0.0f)
Expand Down Expand Up @@ -3921,6 +3923,7 @@
data_type : x
intermediate : noise
backward : rrelu_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

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