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.86, 89】[buaa]Add multiplex number_count op #67387

Merged
merged 9 commits into from
Sep 2, 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 @@ -2376,6 +2376,71 @@ bool Where_OpInferSymbolicShape(pir::Operation *op,
return WhereOpInferSymbolicShape(op, infer_context);
}

bool MultiplexOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const auto &inputs_shape_or_data_list =
infer_context->GetShapeOrDataForValue(op->operand_source(0))
.dyn_cast<symbol::TensorListShapeOrDataDimExprs>();

PADDLE_ENFORCE_NE(
inputs_shape_or_data_list.empty(),
true,
common::errors::InvalidArgument("MultiInput(X) shouldn't be empty."));

const auto &ids_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(1));

PADDLE_ENFORCE_EQ(ids_shape_or_data.shape().size(),
2,
common::errors::PreconditionNotMet(
"The index tensor must be a vector with 2 dimensions"));

infer_context->AddEqualCstr(ids_shape_or_data.shape()[1], symbol::DimExpr(1));

PADDLE_ENFORCE_GT(
inputs_shape_or_data_list.size(),
1,
common::errors::InvalidArgument("multiplex operator should have more "
"than one candidate input tensors."));

size_t num_inputs = inputs_shape_or_data_list.size();
std::vector<symbol::DimExpr> first_input_shape =
inputs_shape_or_data_list[0].shape();
PADDLE_ENFORCE_GE(
first_input_shape.size(),
2,
common::errors::InvalidArgument(
"The rank of candidate tensors must be not less than 2."));

for (size_t i = 1; i < num_inputs; ++i) {
std::vector<symbol::DimExpr> element_shape =
inputs_shape_or_data_list[i].shape();

PADDLE_ENFORCE_EQ(first_input_shape.size(),
element_shape.size(),
common::errors::PreconditionNotMet(
"All the candidate tensors must have the same dim."));

for (size_t j = 0; j < first_input_shape.size(); ++j)
infer_context->AddEqualCstr(first_input_shape[j], element_shape[j]);
// all of the input Tensors should have the same shape
}

if (first_input_shape[0].isa<int64_t>() &&
ids_shape_or_data.shape()[0].isa<int64_t>()) {
PADDLE_ENFORCE_GE(first_input_shape[0].dyn_cast<int64_t>(),
ids_shape_or_data.shape()[0].dyn_cast<int64_t>(),
common::errors::InvalidArgument(
"The 2nd-dim of input cannot be smaller than "
"batchSize of the index tensor."));
}
std::vector<symbol::DimExpr> &output_shape = first_input_shape;
output_shape[0] = ids_shape_or_data.shape()[0];
infer_context->SetShapeOrDataForValue(
op->result(0),
symbol::ShapeOrData{symbol::TensorShapeOrDataDimExprs(output_shape)});
return true;
}
bool YoloLossOpInferSymbolicShape(
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 @@ -81,6 +81,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(Logspace)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(MergedMomentum)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(MergedMomentum_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(MulticlassNms3)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Multiplex)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(MemoryEfficientAttention)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Meshgrid)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Moe)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ OP_SAME_OPERANDS_AND_RESULT(Logsigmoid)
OP_SAME_OPERANDS_AND_RESULT(Logsigmoid_)
OP_SAME_OPERANDS_AND_RESULT(Memcpy)
OP_SAME_OPERANDS_AND_RESULT(Mish)
OP_SAME_OPERANDS_AND_RESULT(NumberCount)
OP_SAME_OPERANDS_AND_RESULT(Pow)
OP_SAME_OPERANDS_AND_RESULT(Poisson)
OP_SAME_OPERANDS_AND_RESULT(Pow_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(Logsigmoid)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Logsigmoid_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Memcpy)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Mish)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(NumberCount)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Poisson)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Pow)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Pow_)
Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/ops/yaml/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3346,6 +3346,7 @@
backward : multiplex_grad
data_transform :
skip_transform : index
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : mv
args : (Tensor x, Tensor vec)
Expand Down Expand Up @@ -5211,3 +5212,4 @@
kernel:
func: number_count
data_type: numbers
interfaces : paddle::dialect::InferSymbolicShapeInterface