-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[MetaSchedule] Introduce Union and OrderedUnion in Database
#12628
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
junrushao
merged 1 commit into
apache:main
from
junrushao:feature/2022-08-27/merged-database
Sep 1, 2022
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
112 changes: 112 additions & 0 deletions
112
python/tvm/meta_schedule/database/ordered_union_database.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,112 @@ | ||
| # 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. | ||
| """A database consists of multiple databases.""" | ||
| from tvm._ffi import register_object | ||
|
|
||
| from .. import _ffi_api | ||
| from .database import Database | ||
|
|
||
|
|
||
| @register_object("meta_schedule.OrderedUnionDatabase") | ||
| class OrderedUnionDatabase(Database): | ||
| """A database composed of multiple databases, allowing users to guide IR rewriting using | ||
| combined knowledge of those databases. To each query, it returns the record from the first | ||
| database that responds to the query. | ||
|
|
||
| Examples | ||
| -------- | ||
| Examples below demonstrate the usecases of and difference between UnionDatabase and | ||
| OrderDatabase. | ||
|
|
||
| Assumption: | ||
| * db1, db2 do not have tuning records for the target workload. | ||
| * Each of db3, db4, db5 has tuning records r3, r4, r5 for target workload respectively. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| #### Case 1. `UnionDatabase`: | ||
| merged_db = ms.database.UnionDatabase( | ||
| db1, # no record | ||
| db2, # no record | ||
| db3, # has r3 | ||
| db4 # has r4 | ||
| ) | ||
| # returns the better one between r3 and r4 | ||
| merged_db.query_tuning_record(..., target_workload) | ||
|
|
||
| ### Case 2. `OrderedUnionDatabase` | ||
| merged_db = ms.database.OrderedUnionDatabase( | ||
| db1, # no record | ||
| db2, # no record | ||
| db3, # has r3 | ||
| db4 # has r4 | ||
| ) | ||
| # returns r3 | ||
| merged_db.query_tuning_record(..., target_workload) | ||
|
|
||
| ### Case 3. Mix-use scenario | ||
| merged_db = ms.database.UnionDatabase( | ||
| db1, # no record | ||
| db2, # no record | ||
| db3, # has r3 | ||
| ms.database.OrderedUnionDatabase( # returns r4 | ||
| db4, # has r4 | ||
| db5, # has r5 | ||
| ) | ||
| ) | ||
| # returns the better one between r3 and r4 | ||
| merged_db.query_tuning_record(..., target_workload) | ||
|
|
||
| ### Case 4. Another mix-use scenario | ||
| merged_db = ms.database.UnionDatabase( | ||
| db1, # no record | ||
| db2, # no record | ||
| db3, # has r3 | ||
| ms.database.UnionDatabase( # returns best one between r4 and r5 | ||
| db4, # has r4 | ||
| db5, # has r5 | ||
| ) | ||
| ) | ||
| # returns the best one among r3, r4 and r5 | ||
| merged_db.query_tuning_record(..., target_workload) | ||
|
|
||
| ### Case 5. Yet another mix-use scenario | ||
| merged_db = ms.database.OrderedUnionDatabase( | ||
| db1, # no record | ||
| db2, # no record | ||
| ms.database.UnionDatabase( # returns best one between r3 and r4 | ||
| db3, # has r3 | ||
| db4, # has r4 | ||
| ) | ||
| db5, # has r5 | ||
| ) | ||
| # returns the better one between r3 and r4 | ||
| merged_db.query_tuning_record(..., target_workload) | ||
| """ | ||
|
|
||
| def __init__(self, *databases: Database) -> None: | ||
| """Construct a merged database from multiple databases. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| *databases : Database | ||
| The list of databases to combine. | ||
| """ | ||
| self.__init_handle_by_constructor__( | ||
| _ffi_api.DatabaseOrderedUnionDatabase, # type: ignore # pylint: disable=no-member | ||
| databases, | ||
| ) |
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,112 @@ | ||
| # 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. | ||
| """A database consists of multiple databases.""" | ||
| from tvm._ffi import register_object | ||
|
|
||
| from .. import _ffi_api | ||
| from .database import Database | ||
|
|
||
|
|
||
| @register_object("meta_schedule.UnionDatabase") | ||
| class UnionDatabase(Database): | ||
| """A database composed of multiple databases, allowing users to guide IR rewriting using | ||
| combined knowledge of those databases. To each query, it returns the best record among all the | ||
| databases given. | ||
|
|
||
| Examples | ||
| -------- | ||
| Examples below demonstrate the usecases of and difference between UnionDatabase and | ||
| OrderDatabase. | ||
|
|
||
| Assumption: | ||
| * db1, db2 do not have tuning records for the target workload. | ||
| * Each of db3, db4, db5 has tuning records r3, r4, r5 for target workload respectively. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| #### Case 1. `UnionDatabase`: | ||
| merged_db = ms.database.UnionDatabase( | ||
| db1, # no record | ||
| db2, # no record | ||
| db3, # has r3 | ||
| db4 # has r4 | ||
| ) | ||
| # returns the better one between r3 and r4 | ||
| merged_db.query_tuning_record(..., target_workload) | ||
|
|
||
| ### Case 2. `OrderedUnionDatabase` | ||
| merged_db = ms.database.OrderedUnionDatabase( | ||
| db1, # no record | ||
| db2, # no record | ||
| db3, # has r3 | ||
| db4 # has r4 | ||
| ) | ||
| # returns r3 | ||
| merged_db.query_tuning_record(..., target_workload) | ||
|
|
||
| ### Case 3. Mix-use scenario | ||
| merged_db = ms.database.UnionDatabase( | ||
| db1, # no record | ||
| db2, # no record | ||
| db3, # has r3 | ||
| ms.database.OrderedUnionDatabase( # returns r4 | ||
| db4, # has r4 | ||
| db5, # has r5 | ||
| ) | ||
| ) | ||
| # returns the better one between r3 and r4 | ||
| merged_db.query_tuning_record(..., target_workload) | ||
|
|
||
| ### Case 4. Another mix-use scenario | ||
| merged_db = ms.database.UnionDatabase( | ||
| db1, # no record | ||
| db2, # no record | ||
| db3, # has r3 | ||
| ms.database.UnionDatabase( # returns best one between r4 and r5 | ||
| db4, # has r4 | ||
| db5, # has r5 | ||
| ) | ||
| ) | ||
| # returns the best one among r3, r4 and r5 | ||
| merged_db.query_tuning_record(..., target_workload) | ||
|
|
||
| ### Case 5. Yet another mix-use scenario | ||
| merged_db = ms.database.OrderedUnionDatabase( | ||
| db1, # no record | ||
| db2, # no record | ||
| ms.database.UnionDatabase( # returns best one between r3 and r4 | ||
| db3, # has r3 | ||
| db4, # has r4 | ||
| ) | ||
| db5, # has r5 | ||
| ) | ||
| # returns the better one between r3 and r4 | ||
| merged_db.query_tuning_record(..., target_workload) | ||
| """ | ||
|
|
||
| def __init__(self, *databases: Database) -> None: | ||
| """Construct a merged database from multiple databases. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| *databases : Database | ||
| The list of databases to combine. | ||
| """ | ||
| self.__init_handle_by_constructor__( | ||
| _ffi_api.DatabaseUnionDatabase, # type: ignore # pylint: disable=no-member | ||
| databases, | ||
| ) |
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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * 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 OrderedUnionDatabaseNode : public DatabaseNode { | ||
| public: | ||
| Array<Database> databases; | ||
|
|
||
| void VisitAttrs(AttrVisitor* v) { v->Visit("databases", &databases); } | ||
|
|
||
| static constexpr const char* _type_key = "meta_schedule.OrderedUnionDatabase"; | ||
| TVM_DECLARE_FINAL_OBJECT_INFO(OrderedUnionDatabaseNode, DatabaseNode); | ||
|
|
||
| public: | ||
| Optional<TuningRecord> QueryTuningRecord(const IRModule& mod, const Target& target, | ||
| const String& task_name) final { | ||
| for (const Database& db : databases) { | ||
| if (Optional<TuningRecord> record = db->QueryTuningRecord(mod, target, task_name)) { | ||
| return record; | ||
| } | ||
| } | ||
| return NullOpt; | ||
| } | ||
|
|
||
| bool HasWorkload(const IRModule& mod) final { | ||
| LOG(FATAL) << "NotImplementedError: OrderedUnionDatabase.HasWorkload"; | ||
| throw; | ||
| } | ||
|
|
||
| Workload CommitWorkload(const IRModule& mod) final { | ||
| LOG(FATAL) << "NotImplementedError: OrderedUnionDatabase.CommitWorkload"; | ||
| throw; | ||
| } | ||
|
|
||
| void CommitTuningRecord(const TuningRecord& record) final { | ||
| LOG(FATAL) << "NotImplementedError: OrderedUnionDatabase.CommitTuningRecord"; | ||
| throw; | ||
| } | ||
|
|
||
| Array<TuningRecord> GetTopK(const Workload& workload, int top_k) final { | ||
| LOG(FATAL) << "NotImplementedError: OrderedUnionDatabase.GetTopK"; | ||
| throw; | ||
| } | ||
|
|
||
| Array<TuningRecord> GetAllTuningRecords() final { | ||
| LOG(FATAL) << "NotImplementedError: OrderedUnionDatabase.GetAllTuningRecords"; | ||
| throw; | ||
| } | ||
|
|
||
| int64_t Size() final { | ||
| LOG(FATAL) << "NotImplementedError: OrderedUnionDatabase.size"; | ||
| throw; | ||
| } | ||
| }; | ||
|
|
||
| Database Database::OrderedUnionDatabase(Array<Database> databases) { | ||
| ObjectPtr<OrderedUnionDatabaseNode> n = make_object<OrderedUnionDatabaseNode>(); | ||
| n->databases = std::move(databases); | ||
| return Database(n); | ||
| } | ||
|
|
||
| TVM_REGISTER_NODE_TYPE(OrderedUnionDatabaseNode); | ||
| TVM_REGISTER_GLOBAL("meta_schedule.DatabaseOrderedUnionDatabase") | ||
| .set_body_typed(Database::OrderedUnionDatabase); | ||
|
|
||
| } // namespace meta_schedule | ||
| } // namespace tvm |
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.