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

[Transform] Operator legalizer V0 #96

Merged

Conversation

MasterJH5574
Copy link
Member

@MasterJH5574 MasterJH5574 commented Jan 11, 2023

This PR is the very first version of operator legalizer, which leverages CallTE of Relax BlockBuilder and the existing TOPI functions or newly written TE functions to lower high-level operator calls down to CallTIRs with TIR PrimFuncs.

The legalizer can pass the existing unit tests, which are mostly written in October, 2022. Those unit tests guarantee the correctness on static shapes as much as possible. So for static shape cases, the legalizer is expected to work properly.

However, the test cases are far from enough, especially the support and robustness on symbolic shapes and other cases need to be further confirmed as a recent-future work. Another to-do is to well-document the pass.

@MasterJH5574
Copy link
Member Author

MasterJH5574 commented Jan 11, 2023

[Update.] Dependency cleared.

Depends on tlc-pack#352, tlc-pack#353, and tlc-pack#354 for them to be merged and cherry-pick to our fork. Those PRs fix a few bugs that were revealed during my work on the legalizer unit tests. A sync is preferably needed.

@MasterJH5574 MasterJH5574 force-pushed the mlc-dev/2023-01-11-legalizer-v0 branch from 86a609f to 93310e0 Compare January 13, 2023 15:43
@MasterJH5574 MasterJH5574 marked this pull request as draft January 15, 2023 18:00

@T.prim_func
def sum(
rxplaceholder: T.Buffer[(T.int64(1), T.int64(2), T.int64(3), T.int64(4)), "float32"],
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps a good motivational pt for meta-programming later cc @MasterJH5574 @Hzfengsy

MasterJH5574 pushed a commit that referenced this pull request Jan 16, 2023
* [VM] Refactor and improve vm.

- Have a separate function for RunInstCall.
- Cache func_index lookup by table to avoid repeative lookup by str.
- Move PackedFunc call arg stack to Frame to increase locality and avoid re-allocation in repeative calls.
- Make frame stack of unique_ptr to avoid frame re-allocation and copy during frame.resize.
- Pass curr_frame as arguments into sub-functions to make it explicit.

* address review comments
@MasterJH5574 MasterJH5574 force-pushed the mlc-dev/2023-01-11-legalizer-v0 branch from 93310e0 to 103c51e Compare January 16, 2023 02:04
@MasterJH5574 MasterJH5574 marked this pull request as ready for review January 16, 2023 02:05
@MasterJH5574 MasterJH5574 force-pushed the mlc-dev/2023-01-11-legalizer-v0 branch 6 times, most recently from 32f215c to 2853e30 Compare January 16, 2023 02:26
@MasterJH5574 MasterJH5574 force-pushed the mlc-dev/2023-01-11-legalizer-v0 branch 3 times, most recently from 050d52e to 3a4977e Compare January 16, 2023 20:35
@MasterJH5574 MasterJH5574 force-pushed the mlc-dev/2023-01-11-legalizer-v0 branch from 3a4977e to cb58430 Compare January 16, 2023 20:36
@spectrometerHBH spectrometerHBH merged commit 99d5afc into mlc-ai:relax Jan 16, 2023
spectrometerHBH added a commit that referenced this pull request Jan 16, 2023
MasterJH5574 pushed a commit that referenced this pull request Jan 19, 2023
This PR migrates #46 to new struct
info infra, as part of our AD migration.

Because we need do numerical testing for gradients, this PR depends on
the operator legalizer #96. Also
because the original version of legalizer did not handle the negative
indexing case of `relax.mean`, this PR fixes it.

To lower `collapse_sum_to`, `collapse_sum_like` properly, this PR
migrates a previous patch #43 which
introduces `collapse_sum` in topi. Now we can remove the skip marker in
the legalizer test for `collapse_sum_to` and `collapse_sum_like`.

The gradients of `cross_entropy` and `softmax_cross_entropy` are
removed. And the former will be added back and adjust to new
`cross_entropy` introduced in #96.

Further plan in this PR:
- [x] Add gradients for `log_softmax` and `nll_loss` once
#94 is merged.
- [x] Gradients for some tuple related operators such as `split` and
`concat`. It can help us to test the correctness of AD when there are
Tuple-I/O operators.
- (Not in this PR) "Undefined Gradient" representation. As we know, the
gradients of some operators w.r.t. specified inputs are undefined or
meaningless, such as the partial gradient of `indices` in `take(x,
indices)`. Relay directly uses `zeros_like` in this case as it won't
affect gradient propagation. Another choice is to introduce a dummy Expr
named `UndefinedGradient` to represent it. How do we handle this case in
relax?
MasterJH5574 added a commit that referenced this pull request Jan 28, 2023
* [TIR][Fix] Buffer slicing using index dtype as extent (#13788)

[Fix] Buffer slicing using index dtype as extent

* [TIR][Fix] IndexDataTypeNormalizer not unwrapping float casting (#13789)

* [Fix][TVMScript] Parse and print `tir_vars` of `call_tir` properly (tlc-pack#361)

* [Transform] Operator legalizer

* Documentation
MasterJH5574 pushed a commit that referenced this pull request Jan 28, 2023
This PR migrates #46 to new struct
info infra, as part of our AD migration.

Because we need do numerical testing for gradients, this PR depends on
the operator legalizer #96. Also
because the original version of legalizer did not handle the negative
indexing case of `relax.mean`, this PR fixes it.

To lower `collapse_sum_to`, `collapse_sum_like` properly, this PR
migrates a previous patch #43 which
introduces `collapse_sum` in topi. Now we can remove the skip marker in
the legalizer test for `collapse_sum_to` and `collapse_sum_like`.

The gradients of `cross_entropy` and `softmax_cross_entropy` are
removed. And the former will be added back and adjust to new
`cross_entropy` introduced in #96.

Further plan in this PR:
- [x] Add gradients for `log_softmax` and `nll_loss` once
#94 is merged.
- [x] Gradients for some tuple related operators such as `split` and
`concat`. It can help us to test the correctness of AD when there are
Tuple-I/O operators.
- (Not in this PR) "Undefined Gradient" representation. As we know, the
gradients of some operators w.r.t. specified inputs are undefined or
meaningless, such as the partial gradient of `indices` in `take(x,
indices)`. Relay directly uses `zeros_like` in this case as it won't
affect gradient propagation. Another choice is to introduce a dummy Expr
named `UndefinedGradient` to represent it. How do we handle this case in
relax?
MasterJH5574 added a commit that referenced this pull request Jan 31, 2023
* [TIR][Fix] Buffer slicing using index dtype as extent (#13788)

[Fix] Buffer slicing using index dtype as extent

* [TIR][Fix] IndexDataTypeNormalizer not unwrapping float casting (#13789)

* [Fix][TVMScript] Parse and print `tir_vars` of `call_tir` properly (tlc-pack#361)

* [Transform] Operator legalizer

* Documentation
MasterJH5574 pushed a commit that referenced this pull request Jan 31, 2023
This PR migrates #46 to new struct
info infra, as part of our AD migration.

Because we need do numerical testing for gradients, this PR depends on
the operator legalizer #96. Also
because the original version of legalizer did not handle the negative
indexing case of `relax.mean`, this PR fixes it.

To lower `collapse_sum_to`, `collapse_sum_like` properly, this PR
migrates a previous patch #43 which
introduces `collapse_sum` in topi. Now we can remove the skip marker in
the legalizer test for `collapse_sum_to` and `collapse_sum_like`.

The gradients of `cross_entropy` and `softmax_cross_entropy` are
removed. And the former will be added back and adjust to new
`cross_entropy` introduced in #96.

Further plan in this PR:
- [x] Add gradients for `log_softmax` and `nll_loss` once
#94 is merged.
- [x] Gradients for some tuple related operators such as `split` and
`concat`. It can help us to test the correctness of AD when there are
Tuple-I/O operators.
- (Not in this PR) "Undefined Gradient" representation. As we know, the
gradients of some operators w.r.t. specified inputs are undefined or
meaningless, such as the partial gradient of `indices` in `take(x,
indices)`. Relay directly uses `zeros_like` in this case as it won't
affect gradient propagation. Another choice is to introduce a dummy Expr
named `UndefinedGradient` to represent it. How do we handle this case in
relax?
MasterJH5574 pushed a commit that referenced this pull request Feb 8, 2023
* [VM] Refactor and improve vm.

- Have a separate function for RunInstCall.
- Cache func_index lookup by table to avoid repeative lookup by str.
- Move PackedFunc call arg stack to Frame to increase locality and avoid re-allocation in repeative calls.
- Make frame stack of unique_ptr to avoid frame re-allocation and copy during frame.resize.
- Pass curr_frame as arguments into sub-functions to make it explicit.

* address review comments
MasterJH5574 added a commit that referenced this pull request Feb 8, 2023
* [TIR][Fix] Buffer slicing using index dtype as extent (#13788)

[Fix] Buffer slicing using index dtype as extent

* [TIR][Fix] IndexDataTypeNormalizer not unwrapping float casting (#13789)

* [Fix][TVMScript] Parse and print `tir_vars` of `call_tir` properly (tlc-pack#361)

* [Transform] Operator legalizer

* Documentation
MasterJH5574 pushed a commit that referenced this pull request Feb 8, 2023
This PR migrates #46 to new struct
info infra, as part of our AD migration.

Because we need do numerical testing for gradients, this PR depends on
the operator legalizer #96. Also
because the original version of legalizer did not handle the negative
indexing case of `relax.mean`, this PR fixes it.

To lower `collapse_sum_to`, `collapse_sum_like` properly, this PR
migrates a previous patch #43 which
introduces `collapse_sum` in topi. Now we can remove the skip marker in
the legalizer test for `collapse_sum_to` and `collapse_sum_like`.

The gradients of `cross_entropy` and `softmax_cross_entropy` are
removed. And the former will be added back and adjust to new
`cross_entropy` introduced in #96.

Further plan in this PR:
- [x] Add gradients for `log_softmax` and `nll_loss` once
#94 is merged.
- [x] Gradients for some tuple related operators such as `split` and
`concat`. It can help us to test the correctness of AD when there are
Tuple-I/O operators.
- (Not in this PR) "Undefined Gradient" representation. As we know, the
gradients of some operators w.r.t. specified inputs are undefined or
meaningless, such as the partial gradient of `indices` in `take(x,
indices)`. Relay directly uses `zeros_like` in this case as it won't
affect gradient propagation. Another choice is to introduce a dummy Expr
named `UndefinedGradient` to represent it. How do we handle this case in
relax?
spectrometerHBH pushed a commit to spectrometerHBH/relax that referenced this pull request Feb 9, 2023
* [VM] Refactor and improve vm.

- Have a separate function for RunInstCall.
- Cache func_index lookup by table to avoid repeative lookup by str.
- Move PackedFunc call arg stack to Frame to increase locality and avoid re-allocation in repeative calls.
- Make frame stack of unique_ptr to avoid frame re-allocation and copy during frame.resize.
- Pass curr_frame as arguments into sub-functions to make it explicit.

* address review comments
MasterJH5574 pushed a commit to MasterJH5574/tlc-relax that referenced this pull request Feb 12, 2023
This PR migrates mlc-ai/relax#46 to new struct
info infra, as part of our AD migration.

Because we need do numerical testing for gradients, this PR depends on
the operator legalizer mlc-ai/relax#96. Also
because the original version of legalizer did not handle the negative
indexing case of `relax.mean`, this PR fixes it.

To lower `collapse_sum_to`, `collapse_sum_like` properly, this PR
migrates a previous patch mlc-ai/relax#43 which
introduces `collapse_sum` in topi. Now we can remove the skip marker in
the legalizer test for `collapse_sum_to` and `collapse_sum_like`.

The gradients of `cross_entropy` and `softmax_cross_entropy` are
removed. And the former will be added back and adjust to new
`cross_entropy` introduced in mlc-ai/relax#96.

Further plan in this PR:
- [x] Add gradients for `log_softmax` and `nll_loss` once
mlc-ai/relax#94 is merged.
- [x] Gradients for some tuple related operators such as `split` and
`concat`. It can help us to test the correctness of AD when there are
Tuple-I/O operators.
- (Not in this PR) "Undefined Gradient" representation. As we know, the
gradients of some operators w.r.t. specified inputs are undefined or
meaningless, such as the partial gradient of `indices` in `take(x,
indices)`. Relay directly uses `zeros_like` in this case as it won't
affect gradient propagation. Another choice is to introduce a dummy Expr
named `UndefinedGradient` to represent it. How do we handle this case in
relax?
MasterJH5574 pushed a commit to MasterJH5574/tlc-relax that referenced this pull request Feb 12, 2023
This PR migrates mlc-ai/relax#46 to new struct
info infra, as part of our AD migration.

Because we need do numerical testing for gradients, this PR depends on
the operator legalizer mlc-ai/relax#96. Also
because the original version of legalizer did not handle the negative
indexing case of `relax.mean`, this PR fixes it.

To lower `collapse_sum_to`, `collapse_sum_like` properly, this PR
migrates a previous patch mlc-ai/relax#43 which
introduces `collapse_sum` in topi. Now we can remove the skip marker in
the legalizer test for `collapse_sum_to` and `collapse_sum_like`.

The gradients of `cross_entropy` and `softmax_cross_entropy` are
removed. And the former will be added back and adjust to new
`cross_entropy` introduced in mlc-ai/relax#96.

Further plan in this PR:
- [x] Add gradients for `log_softmax` and `nll_loss` once
mlc-ai/relax#94 is merged.
- [x] Gradients for some tuple related operators such as `split` and
`concat`. It can help us to test the correctness of AD when there are
Tuple-I/O operators.
- (Not in this PR) "Undefined Gradient" representation. As we know, the
gradients of some operators w.r.t. specified inputs are undefined or
meaningless, such as the partial gradient of `indices` in `take(x,
indices)`. Relay directly uses `zeros_like` in this case as it won't
affect gradient propagation. Another choice is to introduce a dummy Expr
named `UndefinedGradient` to represent it. How do we handle this case in
relax?
MasterJH5574 pushed a commit that referenced this pull request Feb 12, 2023
This PR migrates #46 to new struct
info infra, as part of our AD migration.

Because we need do numerical testing for gradients, this PR depends on
the operator legalizer #96. Also
because the original version of legalizer did not handle the negative
indexing case of `relax.mean`, this PR fixes it.

To lower `collapse_sum_to`, `collapse_sum_like` properly, this PR
migrates a previous patch #43 which
introduces `collapse_sum` in topi. Now we can remove the skip marker in
the legalizer test for `collapse_sum_to` and `collapse_sum_like`.

The gradients of `cross_entropy` and `softmax_cross_entropy` are
removed. And the former will be added back and adjust to new
`cross_entropy` introduced in #96.

Further plan in this PR:
- [x] Add gradients for `log_softmax` and `nll_loss` once
#94 is merged.
- [x] Gradients for some tuple related operators such as `split` and
`concat`. It can help us to test the correctness of AD when there are
Tuple-I/O operators.
- (Not in this PR) "Undefined Gradient" representation. As we know, the
gradients of some operators w.r.t. specified inputs are undefined or
meaningless, such as the partial gradient of `indices` in `take(x,
indices)`. Relay directly uses `zeros_like` in this case as it won't
affect gradient propagation. Another choice is to introduce a dummy Expr
named `UndefinedGradient` to represent it. How do we handle this case in
relax?
MasterJH5574 pushed a commit that referenced this pull request Feb 17, 2023
@MasterJH5574 brings the very first version of legalizer for relax
high-level operators in this PR:
#96. This PR refactors the
`LegalizeOps` pass according to the suggestions mentioned in the
comments of the PR. In details, it includes:
- Change the default legalization map to the attribute of operators. By
this way we can make better use of op attribute and move the main body
of the Pass to C++, making `LegalizeOps` a more formal Pass and bringing
opportunities that we can register legalization without Python in the
future.
- Use decorator to register legalization instead of writing it in the
Pass. It is more elegant and brings extensibility. Also by this way we
can have a clear code structure since now we separate the implementation
in different files. As we will have more and more legalization for
operators, putting them in a single file is not promising.
- Preserve the customize map part. It looks good for now.
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