-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[MetaSchedule][M4a] Schedule Rule: Random-Compute-Location #9940
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 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
31 changes: 31 additions & 0 deletions
31
python/tvm/meta_schedule/schedule_rule/random_compute_location.py
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| """Rule that randomly select a compute-at location for a free block""" | ||
| from tvm._ffi import register_object | ||
|
|
||
| from .. import _ffi_api | ||
| from .schedule_rule import ScheduleRule | ||
|
|
||
|
|
||
| @register_object("meta_schedule.RandomComputeLocation") | ||
| class RandomComputeLocation(ScheduleRule): | ||
| """A rule that randomly select a compute-at location for a free block""" | ||
|
|
||
| def __init__(self) -> None: | ||
| self.__init_handle_by_constructor__( | ||
| _ffi_api.ScheduleRuleRandomComputeLocation, # type: ignore # pylint: disable=no-member | ||
| ) |
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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| # pylint: disable=missing-module-docstring,missing-function-docstring,missing-class-docstring | ||
| from typing import List | ||
|
|
||
| from tvm.tir import Schedule | ||
| from tvm.tir.schedule import Trace | ||
|
|
||
|
|
||
| def check_trace(spaces: List[Schedule], expected: List[List[str]]): | ||
| expected_traces = {"\n".join(t) for t in expected} | ||
| actual_traces = set() | ||
| for space in spaces: | ||
| trace = Trace(space.trace.insts, {}) | ||
| trace = trace.simplified(remove_postproc=True) | ||
| str_trace = "\n".join(str(trace).strip().splitlines()) | ||
| actual_traces.add(str_trace) | ||
| assert str_trace in expected_traces, "\n" + str_trace | ||
| assert len(expected_traces) == len(actual_traces) |
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
123 changes: 123 additions & 0 deletions
123
src/meta_schedule/schedule_rule/random_compute_location.cc
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 |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 "../utils.h" | ||
|
|
||
| namespace tvm { | ||
| namespace meta_schedule { | ||
|
|
||
| class RandomComputeLocationNode : public ScheduleRuleNode { | ||
| public: | ||
| // Inherited from ScheduleRuleNode | ||
| void InitializeWithTuneContext(const TuneContext& context) final {} | ||
|
|
||
| // Inherited from ScheduleRuleNode | ||
| Array<tir::Schedule> Apply(const tir::Schedule& sch, const tir::BlockRV& block_rv) final { | ||
| if (!CheckConditions(sch, block_rv)) { | ||
| return {sch}; | ||
| } | ||
|
|
||
| // Step 1. If the producer of the input block needs a random compute-at location (specified by | ||
| // the annotation), we collect the producer first, and transform the producer block later. | ||
| // - The reason we collect the producer before transforming the input block is that, if the | ||
| // decision of Sample-Compute-Location is "compute-inline" for the input block, we can no longer | ||
| // access the input block. Hence we collect its producer ahead of time. | ||
| // - Note that only single producer is allowed in this case. | ||
| Array<tir::BlockRV> producers{nullptr}; | ||
| if (tir::HasAnn(sch->GetSRef(block_rv), tir::attr::meta_schedule_random_compute_producer, | ||
| true)) { | ||
| producers = sch->GetProducers(block_rv); | ||
| sch->Unannotate(block_rv, tir::attr::meta_schedule_random_compute_producer); | ||
| ICHECK_EQ(producers.size(), 1); | ||
| } | ||
|
|
||
| // Step 2. Transform the input block. | ||
| tir::Schedule res = RandomlyComputeAt(sch, block_rv); | ||
|
|
||
| // Step 3. Transform the producer block if compute-location sampling is needed. | ||
| if (producers.defined()) { | ||
| res = RandomlyComputeAt(res, producers[0]); | ||
| } | ||
|
junrushao marked this conversation as resolved.
|
||
|
|
||
| return {res}; | ||
| } | ||
|
|
||
| private: | ||
| bool CheckConditions(const tir::Schedule sch, const tir::BlockRV& block_rv) const { | ||
| tir::StmtSRef block_sref = sch->GetSRef(block_rv); | ||
| const tir::BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref); | ||
|
|
||
| // Cond 1. The block is not the root block. | ||
| if (block_sref->parent == nullptr) { | ||
| return false; | ||
| } | ||
| // Cond 2. The block should be the direct child block of the root block. | ||
| if (GetScopeRoot(sch->state(), block_sref, // | ||
| /*require_stage_pipeline=*/false, // | ||
| /*require_subtree_compact_dataflow=*/false) | ||
| ->parent != nullptr) { | ||
| return false; | ||
| } | ||
| // Cond 3 & 4. The block has at least one outer loop, and the outermost loop has only one child | ||
| // block. | ||
| Array<tir::StmtSRef> loop_srefs = tir::GetLoops(block_sref); | ||
| if (loop_srefs.empty()) { | ||
| return false; | ||
| } | ||
| if (tir::GetChildBlockSRefOnSRefTree(sch->state(), loop_srefs[0]).size() > 1) { | ||
| return false; | ||
| } | ||
| // Cond 5. The block is not tiled. We check this condition by examine the block's annotation. | ||
| if (tir::HasBeenMultiLevelTiled(block_sref)) { | ||
| return false; | ||
| } | ||
| // Cond 6. The block has at lease one consumer. | ||
| if (tir::GetConsumers(sch->state(), sch->GetSRef(block_rv)).empty()) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /*! | ||
| * \brief Keep sampling a compute-at location for the input block until success. | ||
| * \param sch The TIR schedule | ||
| * \param block_rv The block whose compute-at location is to be sampled | ||
| * \return The TIR schedule after transformation | ||
| */ | ||
| tir::Schedule RandomlyComputeAt(const tir::Schedule& sch, const tir::BlockRV& block_rv) { | ||
| tir::LoopRV compute_at_loc = sch->SampleComputeLocation(block_rv); | ||
| sch->ComputeAt(block_rv, compute_at_loc, true); | ||
| return sch; | ||
| } | ||
|
|
||
| public: | ||
| void VisitAttrs(tvm::AttrVisitor* v) {} | ||
|
|
||
| static constexpr const char* _type_key = "meta_schedule.RandomComputeLocation"; | ||
| TVM_DECLARE_FINAL_OBJECT_INFO(RandomComputeLocationNode, ScheduleRuleNode); | ||
| }; | ||
|
|
||
| ScheduleRule ScheduleRule::RandomComputeLocation() { | ||
| return ScheduleRule(make_object<RandomComputeLocationNode>()); | ||
| } | ||
|
|
||
| TVM_REGISTER_NODE_TYPE(RandomComputeLocationNode); | ||
| TVM_REGISTER_GLOBAL("meta_schedule.ScheduleRuleRandomComputeLocation") | ||
| .set_body_typed(ScheduleRule::RandomComputeLocation); | ||
| } // namespace meta_schedule | ||
| } // namespace tvm | ||
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.