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

【BUAA】【Infer Symbolic Shape】add set_value_with_tensor, sigmoid_cross_entropy_with_logits,swiglu #67098

Merged
merged 9 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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 @@ -759,12 +759,32 @@ bool SearchsortedOpInferSymbolicShape(
// return true;
// }

// bool SwigluOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
// *infer_context) {
// // pass
// return true;
// }
bool SwigluOpInferSymbolicShape(pir::Operation *op,
pir::InferSymbolicShapeContext *infer_context) {
const auto &x_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
size_t rank = x_shape_or_data.shape().size();
if (op->operand_source(1)) {
const auto &y_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(1));
for (size_t i = 0; i < rank; ++i) {
infer_context->AddEqualCstr(x_shape_or_data.shape()[i],
y_shape_or_data.shape()[i]);
}
infer_context->SetShapeOrDataForValue(op->result(0), x_shape_or_data);
} else {
std::vector<symbol::DimExpr> x_shape = x_shape_or_data.shape();
int x_last = static_cast<int>(x_shape[rank - 1].Get<std::int64_t>());
infer_context->AddEqualCstr(symbol::DimExpr{x_last % 2},
symbol::DimExpr{0});
x_shape[rank - 1] = symbol::DimExpr{x_last / 2};
infer_context->SetShapeOrDataForValue(
Copy link
Contributor

Choose a reason for hiding this comment

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

直接对DimExpr添加约束,取int就默认是静态shape了

op->result(0),
symbol::ShapeOrDataDimExprs{
symbol::TensorShapeOrDataDimExprs(x_shape)});
}
return true;
}

bool IscloseOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(Matmul)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(ReduceAs)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Searchsorted)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(SequenceMask)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(Swiglu)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Swiglu)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(TakeAlongAxis)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(TopPSampling)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(TdmChild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1346,19 +1346,43 @@ bool StackOpInferSymbolicShape(pir::Operation *op,
// return true;
// }

// bool SigmoidCrossEntropyWithLogitsOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
// *infer_context) {
// // pass
// return true;
// }
bool SigmoidCrossEntropyWithLogitsOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const auto &input_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
const auto &label_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(1));

// bool SigmoidCrossEntropyWithLogits_OpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
// *infer_context) {
// return SigmoidCrossEntropyWithLogitsOpInferSymbolicShape(op,
// infer_context);
// }
size_t rank = input_shape_or_data.shape().size();
PADDLE_ENFORCE_EQ(rank,
label_shape_or_data.shape().size(),
common::errors::InvalidArgument(
"Input(X) and Input(Label) shall have the same rank."
"But received: the rank of Input(X) is [%d], "
"the rank of Input(Label) is [%d].",
rank,
label_shape_or_data.shape().size()));

for (size_t i = 0; i < rank; ++i) {
infer_context->AddEqualCstr(input_shape_or_data.shape()[i],
label_shape_or_data.shape()[i]);
}
if (op->operand_source(2)) {
const auto &pos_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(2));
for (size_t i = 0; i < rank; ++i) {
infer_context->AddEqualCstr(input_shape_or_data.shape()[i],
pos_shape_or_data.shape()[i]);
}
}
infer_context->SetShapeOrDataForValue(op->result(0), input_shape_or_data);
Copy link
Contributor

Choose a reason for hiding this comment

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

最好将Shape取出来单独设置,这里输入输出的data区应该不一样吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

return true;
}

bool SigmoidCrossEntropyWithLogits_OpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
return SigmoidCrossEntropyWithLogitsOpInferSymbolicShape(op, infer_context);
}

// bool SyncBatchNormOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(NearestInterp)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(RoiPool)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(RoiAlign)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Stack)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(SigmoidCrossEntropyWithLogits)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(SigmoidCrossEntropyWithLogits_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(SigmoidCrossEntropyWithLogits)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(SigmoidCrossEntropyWithLogits_)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(SyncBatchNorm)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(SyncBatchNorm_)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(SaveCombine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1728,12 +1728,36 @@ bool SumOpInferSymbolicShape(pir::Operation *op,

return true;
}
// bool SetValueWithTensorOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
// *infer_context) {
// // pass
// return true;
// }

bool SetValueOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const auto &input_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
PADDLE_ENFORCE_LT(
input_shape_or_data.shape().size(),
7,
common::errors::InvalidArgument("The SetValueOp's rank of input should "
"be less than 7, but received %d.",
input_shape_or_data.shape().size()));

infer_context->SetShapeOrDataForValue(op->result(0), input_shape_or_data);
return true;
}

bool SetValue_OpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
return SetValueOpInferSymbolicShape(op, infer_context);
}

bool SetValueWithTensorOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
return SetValueOpInferSymbolicShape(op, infer_context);
}

bool SetValueWithTensor_OpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
return SetValueOpInferSymbolicShape(op, infer_context);
}

// bool TraceOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext *infer_context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(SplitWithNum)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Squeeze)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Squeeze_)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Sum)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(SetValueWithTensor)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(SetValue)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(SetValue_)
Comment on lines -102 to +103
Copy link
Contributor

Choose a reason for hiding this comment

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

这个PR没有给set_value在yaml中加接口,下一个PR可以顺带加一下(或者给加set_value的同学说一声)

OP_DECLARE_INFER_SYMBOLIC_SHAPE(SetValueWithTensor)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(SetValueWithTensor_)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(Trace)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Tile)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Topk)
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 @@ -4092,6 +4092,7 @@
kernel:
func: set_value_with_tensor
backward: set_value_with_tensor_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : sgd_
args : (Tensor param, Tensor learning_rate, Tensor grad, Tensor master_param, bool multi_precision=false)
Expand Down Expand Up @@ -4183,6 +4184,7 @@
inplace : (x -> out)
backward : sigmoid_cross_entropy_with_logits_grad
optional : pos_weight
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : sign
args : (Tensor x)
Expand Down Expand Up @@ -4464,6 +4466,7 @@
func : swiglu
optional : y
backward: swiglu_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

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