-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[TIR][Schedule] enhance compute_at and reverse_compute_at primitive to choose possible position #12450
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
[TIR][Schedule] enhance compute_at and reverse_compute_at primitive to choose possible position #12450
Changes from all commits
Commits
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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -129,15 +129,19 @@ class NotInSameScopeError : public ScheduleError { | |
| * \param producer_srefs The producer blocks | ||
| * \param consumer_srefs The consumer blocks | ||
| * \param block2realize A cache that maps a block to its realize | ||
| * \return The last position the new block can be inserted onto, and the | ||
| * \param index The block index of the loop body subtree blocks: | ||
| * - `index = -1` means inserted into the last possible insertion point; | ||
| * - `index = -2` means inserted into the first possible insertion point; | ||
| * - Otherwise, `index` is a nonnegative number that indicates the insertion point | ||
| * \return The possible position the new block can be inserted into, and the | ||
| * producer-consumer-relationship is still satisfied. | ||
| * \throws ScheduleError if there is no such insertion point found | ||
| */ | ||
| template <bool require_all_producers_visited, bool require_all_consumers_visited> | ||
| int FindInsertionPoint( | ||
| const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs, | ||
| const Array<StmtSRef>& consumer_srefs, | ||
| std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) { | ||
| int FindInsertionPoint(const ScheduleState& self, const Array<Stmt>& subtrees, | ||
| const Array<StmtSRef>& producer_srefs, const Array<StmtSRef>& consumer_srefs, | ||
| std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize, | ||
| int index) { | ||
| ProducerConsumerSplit split = | ||
| ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize); | ||
| // Step 1. Check if all the producers are visited in the subtrees, if required to | ||
|
|
@@ -159,8 +163,22 @@ int FindInsertionPoint( | |
| // Step 3. Check if there is at least one index of the position can be inserted into | ||
| // The valid indices are: (last_producer_position, first_consumer_position] | ||
| ICHECK(split.last_producer_position < split.first_consumer_position); | ||
| // Step 4. Return the last valid insertion point | ||
| return split.first_consumer_position; | ||
| // Step 4. Return the possible insertion point according to index | ||
| int insert_position; | ||
| if (index == -1) { | ||
| insert_position = split.first_consumer_position; | ||
| } else if (index == -2) { | ||
| insert_position = split.last_producer_position + 1; | ||
| } else if (index >= 0 && index >= split.last_producer_position + 1 && | ||
| index <= split.first_consumer_position) { | ||
| insert_position = index; | ||
| } else { | ||
| LOG(FATAL) << "Valid index:(-1, -2, [" << split.last_producer_position + 1 << ", " | ||
| << split.first_consumer_position << "]), " | ||
| << "current index=" << index; | ||
| throw; | ||
| } | ||
| return insert_position; | ||
|
Comment on lines
+166
to
+181
Member
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. Thanks for updating the logic here! It's much clearer now! |
||
| } | ||
|
|
||
| /*! | ||
|
|
@@ -556,7 +574,8 @@ void CalculateProvidedRequiredRegions( | |
| template <bool is_compute_at> | ||
| void ComputeAtOrReverseComputeAtImpl(ScheduleState self, const StmtSRef& block_sref, | ||
| const StmtSRef& loop_sref, bool preserve_unit_loops, | ||
| arith::Analyzer* analyzer, bool check_only = false) { | ||
| arith::Analyzer* analyzer, bool check_only = false, | ||
| int index = -1) { | ||
| const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref); | ||
| const ForNode* loop = TVM_SREF_TO_FOR(loop, loop_sref); | ||
| // Step 1. Bunch of checks | ||
|
|
@@ -588,7 +607,8 @@ void ComputeAtOrReverseComputeAtImpl(ScheduleState self, const StmtSRef& block_s | |
| /*self=*/self, | ||
| /*subtrees=*/AsArray(loop->body), | ||
| /*producer_srefs=*/producer_srefs, | ||
| /*consumer_srefs=*/consumer_srefs, /*block2realize=*/&block2realize); | ||
| /*consumer_srefs=*/consumer_srefs, /*block2realize=*/&block2realize, | ||
| /*index=*/index); | ||
| // Step 4. Calculate the region provided by a single execution instance of `block`, | ||
| // as well as the region required by dependent blocks under `loop`. | ||
| // Here is the definition of `provide` and `require`: | ||
|
|
@@ -626,17 +646,17 @@ void ComputeAtOrReverseComputeAtImpl(ScheduleState self, const StmtSRef& block_s | |
| } | ||
|
|
||
| void ComputeAt(ScheduleState self, const StmtSRef& block_sref, const StmtSRef& loop_sref, | ||
| bool preserve_unit_loops) { | ||
| bool preserve_unit_loops, int index) { | ||
| arith::Analyzer analyzer; | ||
| ComputeAtOrReverseComputeAtImpl<true>(self, block_sref, loop_sref, preserve_unit_loops, | ||
| &analyzer); | ||
| ComputeAtOrReverseComputeAtImpl<true>(self, block_sref, loop_sref, preserve_unit_loops, &analyzer, | ||
| false, index); | ||
| } | ||
|
|
||
| void ReverseComputeAt(ScheduleState self, const StmtSRef& block_sref, const StmtSRef& loop_sref, | ||
| bool preserve_unit_loops) { | ||
| bool preserve_unit_loops, int index) { | ||
| arith::Analyzer analyzer; | ||
| ComputeAtOrReverseComputeAtImpl<false>(self, block_sref, loop_sref, preserve_unit_loops, | ||
| &analyzer); | ||
| &analyzer, false, index); | ||
| } | ||
|
|
||
| bool CanComputeAt(const ScheduleState& self, const StmtSRef& block_sref, const StmtSRef& loop_sref, | ||
|
|
@@ -671,20 +691,21 @@ struct ComputeAtTraits : public UnpackedInstTraits<ComputeAtTraits> { | |
|
|
||
| private: | ||
| static constexpr size_t kNumInputs = 2; | ||
| static constexpr size_t kNumAttrs = 1; | ||
| static constexpr size_t kNumAttrs = 2; | ||
| static constexpr size_t kNumDecisions = 0; | ||
|
|
||
| static void UnpackedApplyToSchedule(Schedule sch, BlockRV block_rv, LoopRV loop_rv, | ||
| Bool preserve_unit_loops) { | ||
| return sch->ComputeAt(block_rv, loop_rv, preserve_unit_loops.operator bool()); | ||
| Bool preserve_unit_loops, IntImm index) { | ||
| return sch->ComputeAt(block_rv, loop_rv, preserve_unit_loops.operator bool(), index->value); | ||
| } | ||
|
|
||
| static String UnpackedAsPython(Array<String> outputs, String block_rv, String loop_rv, | ||
| Bool preserve_unit_loops) { | ||
| Bool preserve_unit_loops, IntImm index) { | ||
| PythonAPICall py("compute_at"); | ||
| py.Input("block", block_rv); | ||
| py.Input("loop", loop_rv); | ||
| py.Input("preserve_unit_loops", preserve_unit_loops.operator bool()); | ||
| py.Input("index", index); | ||
| return py.Str(); | ||
| } | ||
|
|
||
|
|
@@ -698,20 +719,22 @@ struct ReverseComputeAtTraits : public UnpackedInstTraits<ReverseComputeAtTraits | |
|
|
||
| private: | ||
| static constexpr size_t kNumInputs = 2; | ||
| static constexpr size_t kNumAttrs = 1; | ||
| static constexpr size_t kNumAttrs = 2; | ||
| static constexpr size_t kNumDecisions = 0; | ||
|
|
||
| static void UnpackedApplyToSchedule(Schedule sch, BlockRV block_rv, LoopRV loop_rv, | ||
| Bool preserve_unit_loops) { | ||
| return sch->ReverseComputeAt(block_rv, loop_rv, preserve_unit_loops.operator bool()); | ||
| Bool preserve_unit_loops, IntImm index) { | ||
| return sch->ReverseComputeAt(block_rv, loop_rv, preserve_unit_loops.operator bool(), | ||
| index->value); | ||
| } | ||
|
|
||
| static String UnpackedAsPython(Array<String> outputs, String block_rv, String loop_rv, | ||
| Bool preserve_unit_loops) { | ||
| Bool preserve_unit_loops, IntImm index) { | ||
| PythonAPICall py("reverse_compute_at"); | ||
| py.Input("block", block_rv); | ||
| py.Input("loop", loop_rv); | ||
| py.Input("preserve_unit_loops", preserve_unit_loops.operator bool()); | ||
| py.Input("index", index); | ||
| return py.Str(); | ||
| } | ||
|
|
||
|
|
||
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
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
Oops, something went wrong.
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.