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.166][BUAA] rms_norm #67294

Merged
merged 2 commits into from
Aug 13, 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 @@ -1257,12 +1257,46 @@ bool MeshgridOpInferSymbolicShape(
// return true;
// }

// bool RmsNormOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
// *infer_context) {
// // pass
// return true;
// }
bool RmsNormOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const symbol::ShapeOrDataDimExprs &x_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
std::vector<symbol::DimExpr> x_shape = x_shape_or_data.shape();
size_t x_shape_size = x_shape.size();

symbol::DimExpr normalized_dims(1);
int begin_norm_axis =
op->attribute<pir::Int32Attribute>("begin_norm_axis").data();
for (size_t i = begin_norm_axis; i < x_shape_size; ++i) {
normalized_dims = normalized_dims * x_shape[i];
}

const auto &norm_weight_shape =
infer_context->GetShapeOrDataForValue(op->operand_source(3));
const std::vector<symbol::DimExpr> &norm_weight_dims =
norm_weight_shape.shape();

infer_context->AddEqualCstr(normalized_dims, norm_weight_dims[0]);

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

if (op->result(2)) {
std::vector<symbol::DimExpr> inv_var_dims(
x_shape.begin(), x_shape.begin() + begin_norm_axis);
infer_context->SetShapeOrDataForValue(
op->result(2),
symbol::ShapeOrDataDimExprs{
symbol::TensorShapeOrDataDimExprs(inv_var_dims)});
}

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

return true;
}

// bool RoiPoolOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(Meshgrid)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(NearestInterp)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(Nce)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(PsroiPool)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(RmsNorm)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(RmsNorm)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(RoiPool)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(RoiAlign)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Stack)
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 @@ -3845,6 +3845,7 @@
optional : bias, residual, norm_bias, residual_out
intermediate : inv_var
backward : rms_norm_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : rmsprop_
args : (Tensor param, Tensor mean_square, Tensor grad, Tensor moment, Tensor learning_rate, Tensor mean_grad, Tensor master_param, float epsilon = 1.0e-10f, float decay = 0.9f, float momentum = 0.0f, bool centered = false, bool multi_precision = false)
Expand Down