Skip to content

Commit

Permalink
[MetaSchedule] Union and OrderedUnion Database
Browse files Browse the repository at this point in the history
Following up apache#12520 and apache#12626, this PR introduces two database classes:
`UnionDatabase` and `OrderedUnionDatabase`, both of which allow users to
organically compose multiple databases together, so that the high-level
IR (Relay, Relax) could select the best tuning records according to
running time or a preferred order given by users.

To each query, `UnionDatabase` returns the best record among all the
databases given; Instead, `OrderedUnionDatabase` returns he record from
the first database that responds to the query.

Used together, users may specify complicated dispatching patterns like
below:

```python
def schedule_fn(sch: tir.Schedule) -> bool:
  if "nn_conv2d" in sch.mod.attrs["task_name"]:
    if some_other_tir_conditions(sch.mod):
      handcrafted_scheduling(sch)
      return True
  return False

with ms.database.OrderedUnionDatabase(
  ms.database.ScheduleFn(schedule_fn),  # hand-override some scheduling
  ms.database.Union(                    # existing databases
    db_for_matmul,
    db_for_conv2d,
    db_for_softmax,
  )
  libtorch_database,                    # fallback to libtorch
):
  lib = relay.build(...)
```
  • Loading branch information
junrushao committed Aug 31, 2022
1 parent f114d55 commit 2170231
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 28 deletions.
16 changes: 16 additions & 0 deletions include/tvm/meta_schedule/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,22 @@ class Database : public runtime::ObjectRef {
*/
TVM_DLL static Database JSONDatabase(String path_workload, String path_tuning_record,
bool allow_missing);
/*!
* \brief 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.
* \param databases The list of databases to be combined.
* \return The combined database.
*/
TVM_DLL static Database UnionDatabase(Array<Database, void> databases);
/*!
* \brief 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.
* \param databases The database to be subsetted.
* \return The subsetted database.
*/
TVM_DLL static Database OrderedUnionDatabase(Array<Database, void> databases);
/*!
* \brief Create a database with customized methods on the python-side.
* \param f_has_workload The packed function of `HasWorkload`.
Expand Down
2 changes: 2 additions & 0 deletions python/tvm/meta_schedule/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@
from .database import Database, PyDatabase, TuningRecord, Workload
from .json_database import JSONDatabase
from .memory_database import MemoryDatabase
from .ordered_union_database import OrderedUnionDatabase
from .schedule_fn_database import ScheduleFnDatabase
from .union_database import UnionDatabase
60 changes: 60 additions & 0 deletions python/tvm/meta_schedule/database/ordered_union_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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
--------
An example of using the union:
.. 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.OrderedUnionDatabase(
ScheduleFnDatabase(schedule_conv2d), # override schedule for conv2d
existing_database, # use existing database
mocked_libtorch_database, # use mocked libtorch database for fallback
):
lib = relay.build(...)
"""

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,
)
54 changes: 54 additions & 0 deletions python/tvm/meta_schedule/database/union_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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
--------
An example of using the union:
.. code-block:: python
with ms.database.UnionDatabase(
db_for_matmul,
db_for_conv2d,
db_for_softmax,
):
lib = relay.build(...)
"""

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,
)
22 changes: 0 additions & 22 deletions src/meta_schedule/database/json_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,6 @@
namespace tvm {
namespace meta_schedule {

/*! \brief The struct defining comparison function of sorting by mean run seconds. */
struct SortTuningRecordByMeanRunSecs {
static const constexpr double kMaxMeanTime = 1e10;

static double Mean(const Array<FloatImm>& a) {
if (a.empty()) {
return kMaxMeanTime;
}
double sum = 0.0;
for (const FloatImm& i : a) {
sum += i->value;
}
return sum / a.size();
}

bool operator()(const TuningRecord& a, const TuningRecord& b) const {
double a_time = Mean(a->run_secs.value_or({}));
double b_time = Mean(b->run_secs.value_or({}));
return a_time < b_time;
}
};

/*!
* \brief Read lines from a json file.
* \param path The path to the json file.
Expand Down
86 changes: 86 additions & 0 deletions src/meta_schedule/database/ordered_union_database.cc
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
88 changes: 88 additions & 0 deletions src/meta_schedule/database/union_database.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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 UnionDatabaseNode : public DatabaseNode {
public:
Array<Database> databases;

void VisitAttrs(AttrVisitor* v) { v->Visit("databases", &databases); }

static constexpr const char* _type_key = "meta_schedule.UnionDatabase";
TVM_DECLARE_FINAL_OBJECT_INFO(UnionDatabaseNode, DatabaseNode);

public:
Optional<TuningRecord> QueryTuningRecord(const IRModule& mod, const Target& target,
const String& task_name) final {
std::vector<TuningRecord> results;
results.reserve(databases.size());
for (const Database& db : databases) {
if (Optional<TuningRecord> record = db->QueryTuningRecord(mod, target, task_name)) {
results.push_back(record.value());
}
}
std::stable_sort(results.begin(), results.end(), SortTuningRecordByMeanRunSecs());
return results.empty() ? Optional<TuningRecord>(NullOpt) : results[0];
}

bool HasWorkload(const IRModule& mod) final {
LOG(FATAL) << "NotImplementedError: UnionDatabase.HasWorkload";
throw;
}

Workload CommitWorkload(const IRModule& mod) final {
LOG(FATAL) << "NotImplementedError: UnionDatabase.CommitWorkload";
throw;
}

void CommitTuningRecord(const TuningRecord& record) final {
LOG(FATAL) << "NotImplementedError: UnionDatabase.CommitTuningRecord";
throw;
}

Array<TuningRecord> GetTopK(const Workload& workload, int top_k) final {
LOG(FATAL) << "NotImplementedError: UnionDatabase.GetTopK";
throw;
}

Array<TuningRecord> GetAllTuningRecords() final {
LOG(FATAL) << "NotImplementedError: UnionDatabase.GetAllTuningRecords";
throw;
}

int64_t Size() final {
LOG(FATAL) << "NotImplementedError: UnionDatabase.size";
throw;
}
};

Database Database::UnionDatabase(Array<Database> databases) {
ObjectPtr<UnionDatabaseNode> n = make_object<UnionDatabaseNode>();
n->databases = std::move(databases);
return Database(n);
}

TVM_REGISTER_NODE_TYPE(UnionDatabaseNode);
TVM_REGISTER_GLOBAL("meta_schedule.DatabaseUnionDatabase").set_body_typed(Database::UnionDatabase);

} // namespace meta_schedule
} // namespace tvm
22 changes: 22 additions & 0 deletions src/meta_schedule/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,28 @@ inline Array<Integer> AsIntArray(const ObjectRef& obj) {
return results;
}

/*! \brief The struct defining comparison function of sorting by mean run seconds. */
struct SortTuningRecordByMeanRunSecs {
static const constexpr double kMaxMeanTime = 1e10;

static double Mean(const Array<FloatImm>& a) {
if (a.empty()) {
return kMaxMeanTime;
}
double sum = 0.0;
for (const FloatImm& i : a) {
sum += i->value;
}
return sum / a.size();
}

bool operator()(const TuningRecord& a, const TuningRecord& b) const {
double a_time = Mean(a->run_secs.value_or({}));
double b_time = Mean(b->run_secs.value_or({}));
return a_time < b_time;
}
};

} // namespace meta_schedule
} // namespace tvm

Expand Down
7 changes: 1 addition & 6 deletions tests/python/unittest/test_link_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,10 @@ def schedule_fn(sch):
return True
return False

link_params = True

with StringIO() as stderr_buf, redirect_stderr(stderr_buf):
with ms.database.ScheduleFnDatabase(schedule_fn), tvm.transform.PassContext(
opt_level=3,
config={
"relay.backend.use_meta_schedule": True,
"relay.FuseOps.link_params": link_params,
},
config={"relay.backend.use_meta_schedule": True},
):
executor = Executor("graph", {"link-params": link_params})
lib = relay.build(relay_mod, target=target, executor=executor)
Expand Down
Loading

0 comments on commit 2170231

Please sign in to comment.