Skip to content
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] Introduce Union and OrderedUnion in Database #12628

Merged

Conversation

junrushao
Copy link
Member

@junrushao junrushao commented Aug 28, 2022

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.
#### 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>

cc @Hzfengsy @junrushao1994

@junrushao
Copy link
Member Author

junrushao commented Aug 28, 2022

Pending #12626

@junrushao junrushao force-pushed the feature/2022-08-27/merged-database branch 4 times, most recently from d1d3f4b to 4544cd0 Compare August 30, 2022 01:14
@junrushao junrushao marked this pull request as ready for review August 30, 2022 01:15
@junrushao
Copy link
Member Author

This is ready for review! CC: @sunggg @zxybazh @vinx13

Copy link
Member

@zxybazh zxybazh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Nits on the comments.

include/tvm/meta_schedule/database.h Outdated Show resolved Hide resolved
include/tvm/meta_schedule/database.h Outdated Show resolved Hide resolved
Copy link
Contributor

@MasterJH5574 MasterJH5574 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @junrushao for the continuous hard work! Looks very nice to me :-)

include/tvm/meta_schedule/database.h Outdated Show resolved Hide resolved
include/tvm/meta_schedule/database.h Outdated Show resolved Hide resolved
src/meta_schedule/database/merged_database.cc Outdated Show resolved Hide resolved
tests/python/unittest/test_link_params.py Show resolved Hide resolved
@junrushao junrushao force-pushed the feature/2022-08-27/merged-database branch from 4544cd0 to aec10ac Compare August 30, 2022 07:11
@github-actions github-actions bot requested a review from Hzfengsy August 30, 2022 07:15
Copy link
Contributor

@sunggg sunggg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!
LGTM. One clarification question.

python/tvm/meta_schedule/database/merged_database.py Outdated Show resolved Hide resolved
@junrushao
Copy link
Member Author

Might need some deliberation on the API design

@junrushao junrushao changed the title [MetaSchedule] Introduce MergedDatabase [MetaSchedule] Introduce Union and OrderedUnion in Database Aug 31, 2022
@junrushao junrushao force-pushed the feature/2022-08-27/merged-database branch 3 times, most recently from 3ed6cea to 370d247 Compare August 31, 2022 06:55
@junrushao
Copy link
Member Author

Significantly amended the APIs and docs according to feedbacks from @sunggg @MasterJH5574 @spectrometerHBH @Kathryn-cat @tqchen. Please re-review :-)

@junrushao junrushao requested review from masahi, zxybazh, sunggg and MasterJH5574 and removed request for sunggg August 31, 2022 06:57
@junrushao junrushao force-pushed the feature/2022-08-27/merged-database branch from 370d247 to 2170231 Compare August 31, 2022 08:42
@github-actions github-actions bot removed the request for review from sunggg August 31, 2022 08:43
Copy link
Contributor

@sunggg sunggg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for reflecting the feedback. :)
One nit.

include/tvm/meta_schedule/database.h Show resolved Hide resolved
@junrushao junrushao force-pushed the feature/2022-08-27/merged-database branch 3 times, most recently from 2388106 to 3e83d11 Compare September 1, 2022 19:49
@junrushao junrushao force-pushed the feature/2022-08-27/merged-database branch from 3e83d11 to ccbd64e Compare September 1, 2022 19:54
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>
@junrushao junrushao force-pushed the feature/2022-08-27/merged-database branch from ccbd64e to 10d0192 Compare September 1, 2022 19:55
@junrushao
Copy link
Member Author

Thank you all for super valuable suggestions!

@junrushao junrushao merged commit eecb7fd into apache:main Sep 1, 2022
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 25, 2022
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants