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
Use rewrites to convert relay to glenside #162
Use rewrites to convert relay to glenside #162
Conversation
Current problem I'm facing: when we go to add, for example, the conv2d expression we construct with the In the future, this won't be an issue, as the helper function will no longer add RelayOperatorCalls. |
Running into a bug while implementing the rewrite for conv2d. I think it has to do with how I'm using the existing |
I need to update egg to use add_instantiation. I'm seeing if I can just update egg to vanilla 0.7.1 without Mike's changes and get away with it. Otherwise, we can add the add_instantiation impl on top of Mike's changes, or (harder way) rebase Mike's changes on top of 0.7.1. |
Okay, conv2d rewrite is tentatively working. Who knows what else I broke in the process, though. |
I added add_instantiation manually. We should update egg when we get the chance. Specifically, we should stop using a fork of egg; I'm not sure it's needed! |
I'd like to use the existing test machinery to test the new code. Specifically, the |
I'm working on this now. I'm making it so that the from_relay function adds just a RelayOperatorCall to the expression, and nothing more. Then, I'll make a new version of the test macro which is nearly the same, except it accepts a list of rewrites to run over the egraph (which do the compilation). I'm doing this for conv2d first. |
This change copies the test! macro from the from_relay mod and modifies it for use for the new relay-to-glenside-via- rewrites pipeline. It also modifies from_relay for conv2d so that it's able to be tested using the new test! macro. Specifically, we make it so that from_relay simply constructs a RelayOperatorCall for conv2d, which is then rewritten by the rewrite.
Seems to be working for conv2d. Great! Now we can proceed by: For a given Relay operation (e.g. conv1d):
The tests should work out of the box! |
conv1d done, proceeding as normal. had to add some fields to conv1d. We make a lot of assumptions about what kinds of conv1ds we can handle, and those are still explicitly set out in from_relay(), but they're not necessarily made explicit anywhere else. |
Previously we would convert the axis value from -1 to an unsigned value. Now we keep the raw value.
I'm having an issue writing a test for expand_dims, as I can't write a string for a pattern that will match an Int64 literal. it just parses as a usize. I'm not sure what the right way to fix this is. perhaps just convert everything to int64? But expand_dims is working, fwiw. |
I'm having an issue with the concatenate rewrite. Specifically, I need an additional rewrite which simplifies (tuple-get-item (construct-tuple ?a ...) 0) --> ?a. I have written the rewrite, and it tentatively works. But currently, when I run the rewrite (with the concatenate relay->glenside rewrite), the concatenate rewrite fires, introduces a (tuple-get-item (construct-tuple ...) ...) pattern, but then the tuple-simplification rewrite doesn't fire for some reason. Specifically, the rewrite runner claims that things saturate, which doesn't make sense to me. |
Okay, the issue seemed to be that I was doing a union() inside of the applier for the concatenate rewrite, and that was breaking things. The problem is that the egg API updated and soon enough i think we will need to union() in the applier. So i'll have to make sure I update the code when I update egg. |
All operators implemented, unless I missed some! |
I'm going to go ahead and merge this in, as I just want to move on with it! |
* add comment * Update comment * conv2d relay->glenside rewrite * Update src/language/from_relay/mod.rs * Add macro for relay-to-glenside tests, enable it for conv2d This change copies the test! macro from the from_relay mod and modifies it for use for the new relay-to-glenside-via- rewrites pipeline. It also modifies from_relay for conv2d so that it's able to be tested using the new test! macro. Specifically, we make it so that from_relay simply constructs a RelayOperatorCall for conv2d, which is then rewritten by the rewrite. * conv1d relay->glenside rewrite * Use "real" axis when putting relay softmax in egraph Previously we would convert the axis value from -1 to an unsigned value. Now we keep the raw value. * Update comment * Softmax relay->glenside rewrite * Relu relay->glenside rewrite * negative and sqrt relay to glenside rewrites * max pool 2d NCHW relay->glenside rewrite * global_avg_pool2d_nchw relay->glenside rewrite * expand_dims relay->glenside rewrites * WIP pad operator * update tvm * pad WIP * Kill warning * Pad WIP (need to run the test) * Rename test * Allow dense to be created only as opaque op * Dense relay->glenside rewrite * Compile multiply and divide to opaque ops * Disable some tests * Add relay->glenside rewrite * Multiply and divide relay->glenside rewrites * batch_flatten relay->glenside rewrite * bias_add relay->glenside rewrite * Concatenate WIP * Finish concatenate relay->glenside * Remove usages of union() * Transpose relay->glenside * interpret access reshape * reshape relay->glenside * Squeeze relay->glenside
* add comment * Update comment * conv2d relay->glenside rewrite * Update src/language/from_relay/mod.rs * Add macro for relay-to-glenside tests, enable it for conv2d This change copies the test! macro from the from_relay mod and modifies it for use for the new relay-to-glenside-via- rewrites pipeline. It also modifies from_relay for conv2d so that it's able to be tested using the new test! macro. Specifically, we make it so that from_relay simply constructs a RelayOperatorCall for conv2d, which is then rewritten by the rewrite. * conv1d relay->glenside rewrite * Use "real" axis when putting relay softmax in egraph Previously we would convert the axis value from -1 to an unsigned value. Now we keep the raw value. * Update comment * Softmax relay->glenside rewrite * Relu relay->glenside rewrite * negative and sqrt relay to glenside rewrites * max pool 2d NCHW relay->glenside rewrite * global_avg_pool2d_nchw relay->glenside rewrite * expand_dims relay->glenside rewrites * WIP pad operator * update tvm * pad WIP * Kill warning * Pad WIP (need to run the test) * Rename test * Allow dense to be created only as opaque op * Dense relay->glenside rewrite * Compile multiply and divide to opaque ops * Disable some tests * Add relay->glenside rewrite * Multiply and divide relay->glenside rewrites * batch_flatten relay->glenside rewrite * bias_add relay->glenside rewrite * Concatenate WIP * Finish concatenate relay->glenside * Remove usages of union() * Transpose relay->glenside * interpret access reshape * reshape relay->glenside * Squeeze relay->glenside
* add comment * Update comment * conv2d relay->glenside rewrite * Update src/language/from_relay/mod.rs * Add macro for relay-to-glenside tests, enable it for conv2d This change copies the test! macro from the from_relay mod and modifies it for use for the new relay-to-glenside-via- rewrites pipeline. It also modifies from_relay for conv2d so that it's able to be tested using the new test! macro. Specifically, we make it so that from_relay simply constructs a RelayOperatorCall for conv2d, which is then rewritten by the rewrite. * conv1d relay->glenside rewrite * Use "real" axis when putting relay softmax in egraph Previously we would convert the axis value from -1 to an unsigned value. Now we keep the raw value. * Update comment * Softmax relay->glenside rewrite * Relu relay->glenside rewrite * negative and sqrt relay to glenside rewrites * max pool 2d NCHW relay->glenside rewrite * global_avg_pool2d_nchw relay->glenside rewrite * expand_dims relay->glenside rewrites * WIP pad operator * update tvm * pad WIP * Kill warning * Pad WIP (need to run the test) * Rename test * Allow dense to be created only as opaque op * Dense relay->glenside rewrite * Compile multiply and divide to opaque ops * Disable some tests * Add relay->glenside rewrite * Multiply and divide relay->glenside rewrites * batch_flatten relay->glenside rewrite * bias_add relay->glenside rewrite * Concatenate WIP * Finish concatenate relay->glenside * Remove usages of union() * Transpose relay->glenside * interpret access reshape * reshape relay->glenside * Squeeze relay->glenside
* add comment * Update comment * conv2d relay->glenside rewrite * Update src/language/from_relay/mod.rs * Add macro for relay-to-glenside tests, enable it for conv2d This change copies the test! macro from the from_relay mod and modifies it for use for the new relay-to-glenside-via- rewrites pipeline. It also modifies from_relay for conv2d so that it's able to be tested using the new test! macro. Specifically, we make it so that from_relay simply constructs a RelayOperatorCall for conv2d, which is then rewritten by the rewrite. * conv1d relay->glenside rewrite * Use "real" axis when putting relay softmax in egraph Previously we would convert the axis value from -1 to an unsigned value. Now we keep the raw value. * Update comment * Softmax relay->glenside rewrite * Relu relay->glenside rewrite * negative and sqrt relay to glenside rewrites * max pool 2d NCHW relay->glenside rewrite * global_avg_pool2d_nchw relay->glenside rewrite * expand_dims relay->glenside rewrites * WIP pad operator * update tvm * pad WIP * Kill warning * Pad WIP (need to run the test) * Rename test * Allow dense to be created only as opaque op * Dense relay->glenside rewrite * Compile multiply and divide to opaque ops * Disable some tests * Add relay->glenside rewrite * Multiply and divide relay->glenside rewrites * batch_flatten relay->glenside rewrite * bias_add relay->glenside rewrite * Concatenate WIP * Finish concatenate relay->glenside * Remove usages of union() * Transpose relay->glenside * interpret access reshape * reshape relay->glenside * Squeeze relay->glenside
This is a long-needed change which implements the Relay-to-Glenside compiler using egg rewrites.
In the past, the Relay-to-Glenside compiler would read in Relay programs using the Relay Rust bindings, and then construct Glenside expressions out of those expressions. There were no Relay expressions in Glenside at the time, and so everything needed to be converted directly to Glenside. It was only later that we began adding Relay expressions into Glenside. Now, all Relay constructs of interest are explicitly representable in Glenside using RelayOperatorCall and other constructs. With that in mind, the cleaner way to implement a Relay to Glenside compiler would be by importing a Relay program as RelayOperatorCalls, and then using rewrites to rewrite the various subexpressions to their Glenside equivalents. We can implement the Glenside-to-Relay compiler the same way. This has multiple benefits:
My plan for doing this:
for each Relay operator supported in Glenside, add a rewrite which rewrites from a Relay operator invocation to a Glenside expression. How should we test, however?
Operators remaining to be implemented:
maximum(we only implement this op opaquely in Glenside at the moment)minimum(we only implement this op opaquely in Glenside at the moment)