-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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.161,162][BUAA] norm
, p_norm
#67136
Merged
luotao1
merged 8 commits into
PaddlePaddle:develop
from
MufanColin:cinn_medium_norm_pnorm
Aug 13, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2a2ca54
Finished norm and p_norm
MufanColin 407ee47
Fixed name errors
MufanColin 85ce4b3
Removed unused variables
MufanColin 5ffe790
Updated norm and pnorm in unary_infer_sym.cc according to suggested c…
MufanColin 9919dbd
Removed comments
MufanColin 19af842
Fixed errors returned by CI
MufanColin 08663f1
Put some old code back
MufanColin 595694c
Use a bool variable to make life easier
MufanColin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1158,12 +1158,32 @@ bool MeanAllOpInferSymbolicShape( | |
// return true; | ||
// } | ||
|
||
// bool NormOpInferSymbolicShape(pir::Operation *op, | ||
// pir::InferSymbolicShapeContext *infer_context) | ||
// { | ||
// // pass | ||
// return true; | ||
// } | ||
bool NormOpInferSymbolicShape(pir::Operation *op, | ||
pir::InferSymbolicShapeContext *infer_context) { | ||
auto x_shape_or_data = | ||
infer_context->GetShapeOrDataForValue(op->operand_source(0)); | ||
const auto &x_shape = x_shape_or_data.shape(); | ||
|
||
infer_context->SetShapeOrDataForValue( | ||
op->result(0), | ||
symbol::ShapeOrDataDimExprs{symbol::TensorShapeOrDataDimExprs(x_shape)}); | ||
|
||
int axis = op->attribute<pir::Int32Attribute>("axis").data(); | ||
bool is_test = op->attribute<pir::BoolAttribute>("is_test").data(); | ||
|
||
if (!is_test) { | ||
if (axis < 0) axis += x_shape.size(); | ||
|
||
auto norm_shape = x_shape; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为什么要copy一份 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我后来看了一下好像是因为 x_shape 被加了 const 标记,所以需要 copy 一份才能进行相关修改,泓清老师说这里保留原样不用修改了。 |
||
norm_shape[axis] = symbol::DimExpr(1); | ||
infer_context->SetShapeOrDataForValue( | ||
op->result(1), | ||
symbol::ShapeOrDataDimExprs{ | ||
symbol::TensorShapeOrDataDimExprs(norm_shape)}); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool NonzeroOpInferSymbolicShape( | ||
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) { | ||
|
@@ -1198,12 +1218,70 @@ bool NumelOpInferSymbolicShape(pir::Operation *op, | |
|
||
return true; | ||
} | ||
// bool P_NormOpInferSymbolicShape(pir::Operation *op, | ||
// pir::InferSymbolicShapeContext | ||
// *infer_context) { | ||
// // pass | ||
// return true; | ||
// } | ||
|
||
bool PNormOpInferSymbolicShape(pir::Operation *op, | ||
pir::InferSymbolicShapeContext *infer_context) { | ||
const auto &x_shape_or_data = | ||
infer_context->GetShapeOrDataForValue(op->operand_source(0)); | ||
const auto &x_shape = x_shape_or_data.shape(); | ||
int x_rank = x_shape.size(); | ||
|
||
int axis = op->attribute<pir::Int32Attribute>("axis").data(); | ||
bool keepdim = op->attribute<pir::BoolAttribute>("keepdim").data(); | ||
bool asvector = op->attribute<pir::BoolAttribute>("asvector").data(); | ||
|
||
if (axis < 0) { | ||
axis += x_rank; | ||
} | ||
|
||
bool axis_valid = (axis >= 0) && (axis < x_rank); | ||
|
||
PADDLE_ENFORCE_EQ( | ||
axis_valid, | ||
true, | ||
common::errors::InvalidArgument( | ||
"Attr(axis) value should be in range [-R, R-1], R is the rank of " | ||
"Input(X). " | ||
"But received axis: %d, R: %d. Current Input(X)'s shape is=[%s].", | ||
axis, | ||
x_rank, | ||
x_shape)); | ||
|
||
std::vector<symbol::DimExpr> out_shape; | ||
|
||
if (asvector) { | ||
if (keepdim) { | ||
for (int i = 0; i < x_rank; ++i) { | ||
out_shape.emplace_back(symbol::DimExpr(1)); | ||
} | ||
} else { | ||
out_shape = {}; | ||
} | ||
} else { | ||
if (keepdim) { | ||
for (int i = 0; i < x_rank; ++i) { | ||
if (i == axis) { | ||
out_shape.emplace_back(symbol::DimExpr(1)); | ||
} else { | ||
out_shape.emplace_back(x_shape[i]); | ||
} | ||
} | ||
} else { | ||
for (int i = 0; i < x_rank; ++i) { | ||
if (i != axis) { | ||
out_shape.emplace_back(x_shape[i]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
infer_context->SetShapeOrDataForValue( | ||
op->result(0), | ||
symbol::ShapeOrDataDimExprs{ | ||
symbol::TensorShapeOrDataDimExprs(out_shape)}); | ||
|
||
return true; | ||
} | ||
|
||
// bool PartialSumOpInferSymbolicShape(pir::Operation *op, | ||
// pir::InferSymbolicShapeContext | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉恭喜遇到一个不明参数,这个参数在api文档里没有,但yaml文件里声明了,一般是用来区分训练和推理的,is_test开着代表是跑推理,麻烦下周会跟同学们分享下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
收到