-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[MetaSchedule][UX] Make Database
with-able
#12520
[MetaSchedule][UX] Make Database
with-able
#12520
Conversation
d2b1e90
to
e506714
Compare
Also CC @sunggg @YuchenJin for updates on Relax side |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some tiny nits :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another problem I’m thinking about is.. What will happen if we have code
with database_0, database_1:
...
If we query the best record in the “with” body, looks like the current impl will only looking for records in database_2
, rather than looking for record from both databases?
I think if we have a way to merge several databases, we can agree that we only support |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like this change! Modulo the issue raised by @MasterJH5574, LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally LGTM and we may want to add function to distinguish between targets, which should be a minor problem for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the great work!
Overall, LGTM. A few questions and nits.
e506714
to
2221492
Compare
@MasterJH5574 @masahi Great discussion here! We will introduced |
`ApplyHistoryBest` right now plays a role as the database adaptor to query inside the database. In fact, the logic could be simplified and users only have to deal with `Database` instead of this extra object. - [x] Add `EnterWithScope`/`ExitWithScope`/`Current` to Database - [x] Migrate `te_filter_func` => "tir_filter" in Relay's pass context - [x] Migrate `f_take_tuning_record` => "Database.query_tuning_record" - [x] Migrate `TECompiler` to use `Database` - [x] Remove apply-history-best Next PR: - Migrate `f_direct_dispatch` (potentially unify with `apply_fixed_schedule`?)
2221492
to
8f74866
Compare
@junrushao Sounds a great idea. Thanks in advance! |
One minor nit on naming :) Join have a separate meaning and in this case perhaps concat(or merge) is more appropriate. Try lookup the terminology in pandas |
Following apache#12520, this PR introduces `ScheduleFnDatabase`, a mocked database to allow injecting handcrafted schedules provided by a schedule function. The schedule function comes with the following signature: ```python def schedule_fn( sch: tir.Schedule, ) -> bool: task_name = sch.mod.attrs["task_name"] # ^^^ provides an optional name of the task queried ... ``` This mocked database helps incorporate the existing testing utility `apply_fixed_schedule` more formally into the MetaSchedule-Relay build pipeline, and allows further extension to Relax with the same interface. Next as another follow-up, we will introduce ConcatDatabase that allows mixing multiple databases, including the mocked and ones from JSON files.
@tqchen Sure |
Following up apache#12520 and apache#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( databases=[database], preferred=ms.database.ScheduleFn(schedule_fn), ): lib = relay.build(...) ```
Following #12520, this PR introduces `ScheduleFnDatabase`, a mocked database to allow injecting handcrafted schedules provided by a schedule function. The schedule function comes with the following signature: ```python def schedule_fn( sch: tir.Schedule, ) -> bool: task_name = sch.mod.attrs["task_name"] # ^^^ provides an optional name of the task queried ... ``` This mocked database helps incorporate the existing testing utility `apply_fixed_schedule` more formally into the MetaSchedule-Relay build pipeline, and allows further extension to Relax with the same interface. Next as another follow-up, we will introduce ConcatDatabase that allows mixing multiple databases, including the mocked and ones from JSON files.
Following up apache#12520 and apache#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( databases=[database], preferred=ms.database.ScheduleFn(schedule_fn), ): lib = relay.build(...) ```
Following up apache#12520 and apache#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(...) ```
Following up apache#12520 and apache#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(...) ```
Following up apache#12520 and apache#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(...) ```
Following up apache#12520 and apache#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(...) ```
Following up apache#12520 and apache#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(...) ```
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(...) ```
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(...) ```
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(...) ```
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(...) ```
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: 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. ```python merged_db = ms.database.UnionDatabase( db1, # no record db2, # no record db3, # has r3 db4 # has r4 ) merged_db.query_tuning_record(..., target_workload) merged_db = ms.database.OrderedUnionDatabase( db1, # no record db2, # no record db3, # has r3 db4 # has r4 ) merged_db.query_tuning_record(..., target_workload) 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 ) ) merged_db.query_tuning_record(..., target_workload) 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 ) ) merged_db.query_tuning_record(..., target_workload) 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 ) merged_db.query_tuning_record(..., target_workload) ``` Co-authored-by: sunggg <49998730+sunggg@users.noreply.github.com>
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: 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. ```python merged_db = ms.database.UnionDatabase( db1, # no record db2, # no record db3, # has r3 db4 # has r4 ) merged_db.query_tuning_record(..., target_workload) merged_db = ms.database.OrderedUnionDatabase( db1, # no record db2, # no record db3, # has r3 db4 # has r4 ) merged_db.query_tuning_record(..., target_workload) 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 ) ) merged_db.query_tuning_record(..., target_workload) 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 ) ) merged_db.query_tuning_record(..., target_workload) 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 ) merged_db.query_tuning_record(..., target_workload) ``` Co-authored-by: sunggg <49998730+sunggg@users.noreply.github.com>
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: 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. ```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 the better 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 the better 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) ``` Co-authored-by: sunggg <49998730+sunggg@users.noreply.github.com> # Please enter the commit message for your changes. Lines starting # with '#' will be kept; you may remove them yourself if you want to. # An empty message aborts the commit. # # Date: Sat Aug 27 17:36:54 2022 -0700 # # On branch feature/2022-08-27/merged-database # Your branch and 'origin/feature/2022-08-27/merged-database' have diverged, # and have 1 and 1 different commits each, respectively. # (use "git pull" to merge the remote branch into yours) # # Changes to be committed: # modified: include/tvm/meta_schedule/database.h # modified: python/tvm/meta_schedule/database/__init__.py # new file: python/tvm/meta_schedule/database/ordered_union_database.py # new file: python/tvm/meta_schedule/database/union_database.py # modified: src/meta_schedule/database/json_database.cc # new file: src/meta_schedule/database/ordered_union_database.cc # new file: src/meta_schedule/database/union_database.cc # modified: src/meta_schedule/utils.h # modified: tests/python/unittest/test_link_params.py # modified: tests/python/unittest/test_meta_schedule_database.py # # Please enter the commit message for your changes. Lines starting # with '#' will be kept; you may remove them yourself if you want to. # An empty message aborts the commit. # # Date: Sat Aug 27 17:36:54 2022 -0700 # # On branch feature/2022-08-27/merged-database # Your branch and 'origin/feature/2022-08-27/merged-database' have diverged, # and have 1 and 1 different commits each, respectively. # (use "git pull" to merge the remote branch into yours) # # Changes to be committed: # modified: include/tvm/meta_schedule/database.h # modified: python/tvm/meta_schedule/database/__init__.py # new file: python/tvm/meta_schedule/database/ordered_union_database.py # new file: python/tvm/meta_schedule/database/union_database.py # modified: src/meta_schedule/database/json_database.cc # new file: src/meta_schedule/database/ordered_union_database.cc # new file: src/meta_schedule/database/union_database.cc # modified: src/meta_schedule/utils.h # modified: tests/python/unittest/test_link_params.py # modified: tests/python/unittest/test_meta_schedule_database.py # # Please enter the commit message for your changes. Lines starting # with '#' will be kept; you may remove them yourself if you want to. # An empty message aborts the commit. # # Date: Sat Aug 27 17:36:54 2022 -0700 # # On branch feature/2022-08-27/merged-database # Your branch and 'origin/feature/2022-08-27/merged-database' have diverged, # and have 1 and 1 different commits each, respectively. # (use "git pull" to merge the remote branch into yours) # # Changes to be committed: # modified: include/tvm/meta_schedule/database.h # modified: python/tvm/meta_schedule/database/__init__.py # new file: python/tvm/meta_schedule/database/ordered_union_database.py # new file: python/tvm/meta_schedule/database/union_database.py # modified: src/meta_schedule/database/json_database.cc # new file: src/meta_schedule/database/ordered_union_database.cc # new file: src/meta_schedule/database/union_database.cc # modified: src/meta_schedule/utils.h # modified: tests/python/unittest/test_link_params.py # modified: tests/python/unittest/test_meta_schedule_database.py #
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: 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. ```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 the better 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 the better 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) ``` Co-authored-by: sunggg <49998730+sunggg@users.noreply.github.com>
Following up #12520 and #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: 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. ```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 the better 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 the better 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) ``` Co-authored-by: sunggg <49998730+sunggg@users.noreply.github.com>
`ApplyHistoryBest` right now plays a role as the database adaptor to query inside the database. In fact, the logic could be simplified and users only have to deal with `Database` instead of this extra object. - [x] Add `EnterWithScope`/`ExitWithScope`/`Current` to Database - [x] Migrate `te_filter_func` => "tir_filter" in Relay's pass context - [x] Migrate `f_take_tuning_record` => "Database.query_tuning_record" - [x] Migrate `TECompiler` to use `Database` - [x] Remove apply-history-best Next PR: - Migrate `f_direct_dispatch` (potentially unify with `apply_fixed_schedule`?)
Following apache#12520, this PR introduces `ScheduleFnDatabase`, a mocked database to allow injecting handcrafted schedules provided by a schedule function. The schedule function comes with the following signature: ```python def schedule_fn( sch: tir.Schedule, ) -> bool: task_name = sch.mod.attrs["task_name"] # ^^^ provides an optional name of the task queried ... ``` This mocked database helps incorporate the existing testing utility `apply_fixed_schedule` more formally into the MetaSchedule-Relay build pipeline, and allows further extension to Relax with the same interface. Next as another follow-up, we will introduce ConcatDatabase that allows mixing multiple databases, including the mocked and ones from JSON files.
…he#12628) 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: 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. ```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 the better 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 the better 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) ``` Co-authored-by: sunggg <49998730+sunggg@users.noreply.github.com>
ApplyHistoryBest
right now plays a role as the database adaptor to query inside the database. In fact, the logic could be simplified and users only have to deal withDatabase
instead of this extra object.EnterWithScope
/ExitWithScope
/Current
to Databasete_filter_func
=> "tir_filter" in Relay's pass contextf_take_tuning_record
=> "Database.query_tuning_record"TECompiler
to useDatabase
Next PR:
f_direct_dispatch
(potentially unify withapply_fixed_schedule
?)cc @Hzfengsy @junrushao1994