-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MetaSchedule] Introduce
MergedDatabase
Following up #12520 and #12626, this PR introduces `MergedDatabase`, which allow users to compose multiple databases so that the high-level IR could select the best tuning records among them. The `MergedDatabase` also comes with an extra field `preferred` to allow users to override tuning records from other databases. A classic usecase of the `preferred` parameter is through handcrafted schedule functions: ```python def schedule_fn(sch: tir.Schedule) -> bool: if "nn_conv2d" in sch.mod.attrs["task_name"]: handcrafted_scheduling(sch) return True return False with ms.database.MergedDatabase( preferred=ms.database.ScheduleFn(schedule_fn), # ^^^^ override scheduling decisions databases=[database], fallback=libtorch_database, # ^^^^ fallback to libtorch ): lib = relay.build(...) ```
- Loading branch information
Showing
8 changed files
with
287 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# 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 typing import List, Union | ||
|
||
from tvm._ffi import register_object | ||
|
||
from .. import _ffi_api | ||
from .database import Database | ||
|
||
|
||
@register_object("meta_schedule.MergedDatabase") | ||
class MergedDatabase(Database): | ||
"""A database composed of multiple databases, allowing users to guide IR rewriting using | ||
combined knowledge of those databases. | ||
Besides querying from all databases and picking the best running time, this database also | ||
comes with two extra sets of databases: | ||
- preferred: If the preferred database responds to a query, all responses of other databases | ||
will be overridden and ignored. | ||
- fallback: If all databases don't respond to a query, the fallback databases will be used. | ||
Examples | ||
-------- | ||
An example of using the merged database: | ||
.. code-block:: python | ||
def schedule_conv2d(sch: tir.Schedule) -> bool: | ||
if "nn_conv2d" in sch.mod.attrs["task_name"]: | ||
handcrafted_scheduling(sch) | ||
return True | ||
return False | ||
with ms.database.MergedDatabase( | ||
preferred=ScheduleFnDatabase(schedule_conv2d), # override schedule for conv2d | ||
databases=[existing_db0, existing_db1, existing_db2], # use existing databases | ||
fallback=libtorch, # fallback to libtorch | ||
): | ||
lib = relay.build(...) | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
preferred: Union[None, Database, List[Database]] = None, | ||
databases: Union[None, Database, List[Database]] = None, | ||
fallback: Union[None, Database, List[Database]] = None, | ||
) -> None: | ||
"""Construct a merged database from multiple databases. | ||
Parameters | ||
---------- | ||
preferred : Union[None, Database, List[Database]] = None | ||
The preferred databases. If one of the preferred database responses to a | ||
query, all other databases will be ignored. | ||
databases : Union[None, Database, List[Database]] = None | ||
The list of databases to merge. | ||
fallback : Union[None, Database, List[Database]] = None | ||
The fallback databases. If all the databases didn't answer a query, | ||
the response from the first fallback database that responds will be used. | ||
""" | ||
if preferred is None: | ||
preferred = [] | ||
elif isinstance(preferred, Database): | ||
preferred = [preferred] | ||
if databases is None: | ||
databases = [] | ||
elif isinstance(databases, Database): | ||
databases = [databases] | ||
if fallback is None: | ||
fallback = [] | ||
elif isinstance(fallback, Database): | ||
fallback = [fallback] | ||
self.__init_handle_by_constructor__( | ||
_ffi_api.DatabaseMergedDatabase, # type: ignore # pylint: disable=no-member | ||
preferred, | ||
databases, | ||
fallback, | ||
) |
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 |
---|---|---|
@@ -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. | ||
*/ | ||
#include "../utils.h" | ||
|
||
namespace tvm { | ||
namespace meta_schedule { | ||
|
||
class MergedDatabaseNode : public DatabaseNode { | ||
public: | ||
Array<Database> preferred; | ||
Array<Database> databases; | ||
Array<Database> fallback; | ||
|
||
void VisitAttrs(AttrVisitor* v) { | ||
v->Visit("preferred", &preferred); | ||
v->Visit("databases", &databases); | ||
v->Visit("fallback", &fallback); | ||
} | ||
|
||
static constexpr const char* _type_key = "meta_schedule.MergedDatabase"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(MergedDatabaseNode, DatabaseNode); | ||
|
||
public: | ||
Optional<TuningRecord> QueryTuningRecord(const IRModule& mod, const Target& target, | ||
const String& task_name) final { | ||
for (const Database& db : preferred) { | ||
if (Optional<TuningRecord> record = db->QueryTuningRecord(mod, target, task_name)) { | ||
return record; | ||
} | ||
} | ||
std::vector<TuningRecord> results; | ||
results.reserve(databases.size()); | ||
for (const Database& db : databases) { | ||
if (Optional<TuningRecord> record = db->QueryTuningRecord(mod, target, task_name)) { | ||
ICHECK(record.value()->run_secs.defined()); | ||
results.push_back(record.value()); | ||
} | ||
} | ||
std::sort(results.begin(), results.end(), SortTuningRecordByMeanRunSecs()); | ||
if (!results.empty()) { | ||
return results[0]; | ||
} | ||
for (const Database& db : fallback) { | ||
if (Optional<TuningRecord> record = db->QueryTuningRecord(mod, target, task_name)) { | ||
return record; | ||
} | ||
} | ||
return NullOpt; | ||
} | ||
|
||
bool HasWorkload(const IRModule& mod) final { | ||
LOG(FATAL) << "NotImplementedError: MergedDatabase.HasWorkload"; | ||
throw; | ||
} | ||
|
||
Workload CommitWorkload(const IRModule& mod) final { | ||
LOG(FATAL) << "NotImplementedError: MergedDatabase.CommitWorkload"; | ||
throw; | ||
} | ||
|
||
void CommitTuningRecord(const TuningRecord& record) final { | ||
LOG(FATAL) << "NotImplementedError: MergedDatabase.CommitTuningRecord"; | ||
throw; | ||
} | ||
|
||
Array<TuningRecord> GetTopK(const Workload& workload, int top_k) final { | ||
LOG(FATAL) << "NotImplementedError: MergedDatabase.GetTopK"; | ||
throw; | ||
} | ||
|
||
Array<TuningRecord> GetAllTuningRecords() final { | ||
LOG(FATAL) << "NotImplementedError: MergedDatabase.GetAllTuningRecords"; | ||
throw; | ||
} | ||
|
||
int64_t Size() final { | ||
LOG(FATAL) << "NotImplementedError: MergedDatabase.size"; | ||
throw; | ||
} | ||
}; | ||
|
||
Database Database::MergedDatabase(Array<Database> preferred, Array<Database> databases, | ||
Array<Database> fallback) { | ||
ObjectPtr<MergedDatabaseNode> n = make_object<MergedDatabaseNode>(); | ||
n->preferred = std::move(preferred); | ||
n->databases = std::move(databases); | ||
n->fallback = std::move(fallback); | ||
return Database(n); | ||
} | ||
|
||
TVM_REGISTER_NODE_TYPE(MergedDatabaseNode); | ||
TVM_REGISTER_GLOBAL("meta_schedule.DatabaseMergedDatabase") | ||
.set_body_typed(Database::MergedDatabase); | ||
|
||
} // namespace meta_schedule | ||
} // namespace tvm |
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