-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add align iter space tactic (#60498)
Add align iter space tactic
- Loading branch information
Showing
14 changed files
with
400 additions
and
30 deletions.
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
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
core_gather_headers() | ||
|
||
gather_srcs(cinnapi_src SRCS arrange_storage_tactic.cc) | ||
gather_srcs(cinnapi_src SRCS align_iter_space_tactic.cc) | ||
gather_srcs(cinnapi_src SRCS compute_inline_tactic.cc) | ||
gather_srcs(cinnapi_src SRCS arrange_storage_tactic.cc) |
87 changes: 87 additions & 0 deletions
87
paddle/cinn/ir/group_schedule/tactic/align_iter_space_tactic.cc
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) 2023 CINN Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "paddle/cinn/ir/group_schedule/tactic/align_iter_space_tactic.h" | ||
#include "paddle/cinn/common/cas.h" | ||
#include "paddle/cinn/common/integer_set.h" | ||
#include "paddle/cinn/ir/ir.h" | ||
#include "paddle/cinn/ir/ir_analyzer/ir_analyzer.h" | ||
#include "paddle/cinn/ir/op/ir_operators.h" | ||
#include "paddle/cinn/ir/utils/ir_copy.h" | ||
|
||
namespace cinn { | ||
namespace ir { | ||
|
||
void AlignIterSpaceTactic::Init(ScheduleContext* context) { | ||
context_ = context; | ||
} | ||
|
||
void AlignIterSpaceTactic::Apply(ir::IRSchedule* sch, | ||
const std::string& block_id) { | ||
ir::Expr block = sch->GetBlock(block_id); | ||
if (analyzer::IsReductionSBlock(block)) { | ||
return; | ||
} | ||
|
||
std::vector<ir::Expr> loops = sch->GetLoops(block_id); | ||
ir::Expr src_fused_loop = sch->Fuse(loops); | ||
ir::Expr src_total_extent = src_fused_loop.As<ir::For>()->extent; | ||
|
||
ir::Expr target_sp_extent{1}; | ||
for (const auto& iter : context_->iter_space_info.sp_space) { | ||
target_sp_extent = target_sp_extent * std::get<0>(iter); | ||
} | ||
ir::Expr target_total_extent = ir_utils::IRCopy(target_sp_extent); | ||
for (const auto& iter : context_->iter_space_info.rb_space) { | ||
target_total_extent = target_total_extent * std::get<0>(iter); | ||
} | ||
|
||
common::cas_intervals_t var_intervals; | ||
common::SymbolicExprAnalyzer symbolic_expr_analyzer(var_intervals); | ||
std::optional<bool> total_extent_eq = | ||
symbolic_expr_analyzer.ProveEQ(src_total_extent, target_total_extent); | ||
bool need_reorder = false; | ||
for (int i = 0; i < context_->iter_space_info.rb_last_order.size(); ++i) { | ||
if (context_->iter_space_info.rb_last_order[i] != i) { | ||
need_reorder = true; | ||
break; | ||
} | ||
} | ||
|
||
if (total_extent_eq.has_value() && total_extent_eq.value()) { | ||
sch->Split(src_fused_loop, | ||
context_->iter_space_info.memory_consistent_order_space); | ||
loops = sch->GetLoops(block_id); | ||
if (need_reorder) { | ||
sch->Reorder(block_id, context_->iter_space_info.rb_last_order); | ||
} | ||
if (context_->iter_space_info.sp_space.size() < loops.size() - 1) { | ||
loops = sch->GetLoops(block_id); | ||
std::vector<ir::Expr> rb_loops( | ||
loops.begin() + context_->iter_space_info.sp_space.size(), | ||
loops.end()); | ||
sch->Fuse(rb_loops); | ||
} | ||
if (context_->iter_space_info.sp_space.size() > 1) { | ||
loops = sch->GetLoops(block_id); | ||
std::vector<ir::Expr> sp_loops( | ||
loops.begin(), | ||
loops.begin() + context_->iter_space_info.sp_space.size()); | ||
sch->Fuse(sp_loops); | ||
} | ||
} | ||
} | ||
|
||
} // namespace ir | ||
} // namespace cinn |
37 changes: 37 additions & 0 deletions
37
paddle/cinn/ir/group_schedule/tactic/align_iter_space_tactic.h
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2023 CINN Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <unordered_set> | ||
#include "paddle/cinn/ir/group_schedule/tactic/schedule_tactic.h" | ||
|
||
namespace cinn { | ||
namespace ir { | ||
|
||
class AlignIterSpaceTactic final : public ScheduleTactic { | ||
public: | ||
void Init(ScheduleContext* context) override; | ||
|
||
void Apply(ir::IRSchedule* sch, const std::string& block_id) override; | ||
|
||
std::string TacticName() const override { return "AlignIterSpaceTactic"; } | ||
|
||
private: | ||
ScheduleContext* context_; | ||
}; | ||
|
||
} // namespace ir | ||
} // namespace cinn |
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
Oops, something went wrong.