-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[CINN] uniform all the 0 and reduce deleted axis #61608
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
65dc09e
uniform all the 0 and reduce deleted axis
2742195759 d22fbfa
remove one shape for keepdim cases.
2742195759 4ca0b25
fix by code review
2742195759 1c6c65f
Merge remote-tracking branch 'origin/develop' into uniform-cinn
2742195759 a84a8c5
fix some error in 0d format
2742195759 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 hidden or 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 |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| #include "paddle/cinn/optim/replace_var_with_expr.h" | ||
|
|
||
| PD_DECLARE_bool(cinn_new_group_scheduler); | ||
| PD_DECLARE_bool(group_schedule_tiling_first); | ||
| PD_DECLARE_bool(cinn_bucket_compile); | ||
|
|
||
| namespace cinn { | ||
|
|
@@ -93,9 +94,46 @@ ir::Expr AstGen::Build(const ir::Tensor& tensor, TensorGroup* tensor_group) { | |
| std::vector<ir::Expr> iter_values; | ||
| // reduce body and reduce init schedule block should have different objects | ||
| // for same axis so we re-create objects | ||
| VLOG(4) << "FLAGS_group_schedule_tiling_first = " | ||
| << FLAGS_group_schedule_tiling_first; | ||
| std::vector<Var> axis_vars = cinn::common::GenDefaultAxis(axis_len); | ||
| const std::vector<ir::Var>& reduce_axis = tensor->reduce_axis; | ||
| const auto reduce_axis_position = [&reduce_axis, | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| &tensor]() -> std::vector<int> { | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| VLOG(4) << "start calculus reduce_axis_position: "; | ||
| std::vector<int> res; | ||
| auto fn_body = tensor->operation.ptr()->as<ir::ComputeOp>()->body[0]; | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (fn_body.defined() && fn_body.As<ir::Reduce>()) { | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| auto& reduce_body = | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fn_body.As<ir::Reduce>()->body; // reduce body is a tensor store. | ||
| auto& load_indices = reduce_body.As<ir::Load>()->indices; | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| int position = -1; | ||
| for (auto& obj : load_indices) { | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| position += 1; | ||
| for (auto& reduce_var : reduce_axis) { | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (obj.as_var_ref() == reduce_var) { | ||
| res.push_back(position); | ||
| } | ||
| } | ||
| } | ||
| for (auto i : res) { | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| VLOG(4) << "reduce axis position is " << i; | ||
| } | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return res; | ||
| } | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }(); | ||
| for (int i = 0; i < shape.size(); ++i) { | ||
| if (FLAGS_cinn_new_group_scheduler && shape[i] == Expr(1)) { | ||
| if (FLAGS_group_schedule_tiling_first && | ||
| std::find(reduce_axis_position.begin(), | ||
| reduce_axis_position.end(), | ||
| i) != reduce_axis_position.end()) { | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // if tiling first, we need to replace the reduce axis with 0, but don't | ||
| // deal with the non-reduce axis | ||
| optim::ReplaceVarWithExpr(&init_body, axis[i], Expr(0)); | ||
| continue; | ||
| } | ||
| if (!FLAGS_group_schedule_tiling_first && | ||
| FLAGS_cinn_new_group_scheduler && shape[i] == Expr(1)) { | ||
| optim::ReplaceVarWithExpr(&init_body, axis[i], Expr(0)); | ||
| continue; | ||
| } | ||
|
|
@@ -105,7 +143,7 @@ ir::Expr AstGen::Build(const ir::Tensor& tensor, TensorGroup* tensor_group) { | |
| /*is_reduce = */ false)); | ||
| optim::ReplaceVarWithExpr(&init_body, axis[i], block_vars.back()); | ||
| axis_vars[i]->is_reduce_axis = false; | ||
| if (shape[i] == Expr(1)) { | ||
| if (!FLAGS_group_schedule_tiling_first && shape[i] == Expr(1)) { | ||
| iter_values.push_back(Expr(0)); | ||
| } else { | ||
| iter_values.push_back(axis_vars[i]); | ||
|
|
@@ -117,7 +155,6 @@ ir::Expr AstGen::Build(const ir::Tensor& tensor, TensorGroup* tensor_group) { | |
| block_vars, {}, {}, reduce_init_name, init_body)); | ||
|
|
||
| // For the remaining reduce axis, make reduce body | ||
| const std::vector<ir::Var>& reduce_axis = tensor->reduce_axis; | ||
| ir::Expr reduce_body = | ||
| ConvertReduceBody(tensor->body(), tensor, axis_exprs); | ||
| // create schedule block itervars, i0,i1... | ||
|
|
@@ -127,7 +164,17 @@ ir::Expr AstGen::Build(const ir::Tensor& tensor, TensorGroup* tensor_group) { | |
| // for same axis so we re-create objects | ||
| std::vector<Var> reduce_axis_vars = cinn::common::GenDefaultAxis(axis_len); | ||
| for (int i = 0; i < shape.size(); ++i) { | ||
| if (FLAGS_cinn_new_group_scheduler && shape[i] == Expr(1)) { | ||
| if (FLAGS_group_schedule_tiling_first && | ||
| std::find(reduce_axis_position.begin(), | ||
| reduce_axis_position.end(), | ||
| i) != reduce_axis_position.end()) { | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // if tiling first, we need to replace the reduce axis with 0, but don't | ||
| // deal with the non-reduce axis | ||
| optim::ReplaceVarWithExpr(&reduce_body, axis[i], Expr(0)); | ||
| continue; | ||
| } | ||
| if (!FLAGS_group_schedule_tiling_first && | ||
| FLAGS_cinn_new_group_scheduler && shape[i] == Expr(1)) { | ||
| optim::ReplaceVarWithExpr(&reduce_body, axis[i], Expr(0)); | ||
| continue; | ||
| } | ||
|
|
@@ -136,7 +183,7 @@ ir::Expr AstGen::Build(const ir::Tensor& tensor, TensorGroup* tensor_group) { | |
| cinn::UniqName("i" + std::to_string(i)), | ||
| /*is_reduce = */ false)); | ||
| reduce_axis_vars[i]->is_reduce_axis = false; | ||
| if (shape[i] == Expr(1)) { | ||
| if (!FLAGS_group_schedule_tiling_first && shape[i] == Expr(1)) { | ||
| reduce_iter_values.push_back(Expr(0)); | ||
| } else { | ||
| reduce_iter_values.push_back(axis_vars[i]); | ||
|
|
@@ -156,13 +203,18 @@ ir::Expr AstGen::Build(const ir::Tensor& tensor, TensorGroup* tensor_group) { | |
|
|
||
| int non_zero_axis_size = 0; | ||
| for (int i = 0; i < axis.size(); ++i) { | ||
| if (FLAGS_cinn_new_group_scheduler && shape[i] == Expr(1)) { | ||
| if (!FLAGS_group_schedule_tiling_first && | ||
| FLAGS_cinn_new_group_scheduler && shape[i] == Expr(1)) { | ||
| continue; | ||
| } | ||
| optim::ReplaceVarWithExpr( | ||
| &reduce_body, axis[i], reduce_block_vars[non_zero_axis_size]); | ||
| ++non_zero_axis_size; | ||
| } | ||
|
|
||
| if (FLAGS_group_schedule_tiling_first) { | ||
| non_zero_axis_size = axis.size() - reduce_axis.size(); | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| for (int i = non_zero_axis_size; i < reduce_block_vars.size(); ++i) { | ||
| optim::ReplaceVarWithExpr(&reduce_body, | ||
| reduce_axis[i - non_zero_axis_size], | ||
|
|
@@ -185,7 +237,14 @@ ir::Expr AstGen::Build(const ir::Tensor& tensor, TensorGroup* tensor_group) { | |
| // Put the two parts together | ||
| ir::Expr body = ir::Block::Make({init_body, reduce_body}); | ||
| for (int i = static_cast<int>(axis_len) - 1; i >= 0; --i) { | ||
| if (!FLAGS_cinn_bucket_compile && shape[i] == Expr(1)) { | ||
| if (FLAGS_group_schedule_tiling_first && | ||
| std::find(reduce_axis_position.begin(), | ||
| reduce_axis_position.end(), | ||
| i) != reduce_axis_position.end()) { | ||
2742195759 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| continue; | ||
| } | ||
| if (!FLAGS_group_schedule_tiling_first && !FLAGS_cinn_bucket_compile && | ||
|
Contributor
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. (!FLAGS_group_schedule_tiling_first || !FLAGS_cinn_bucket_compile) && shape[i] == Expr(1) |
||
| shape[i] == Expr(1)) { | ||
| continue; | ||
| } | ||
| ir::Var loop_var = axis[i]; | ||
|
|
@@ -210,7 +269,7 @@ ir::Expr AstGen::Build(const ir::Tensor& tensor, TensorGroup* tensor_group) { | |
| Expr(0), shape[i], cinn::UniqName("i" + std::to_string(i)), false)); | ||
| optim::ReplaceVarWithExpr(&body, axis[i], block_vars[i]); | ||
| axis_vars[i]->is_reduce_axis = false; | ||
| if (shape[i] == Expr(1)) { | ||
| if (!FLAGS_group_schedule_tiling_first && shape[i] == Expr(1)) { | ||
| iter_values.push_back(Expr(0)); | ||
| } else { | ||
| iter_values.push_back(axis_vars[i]); | ||
|
|
||
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.