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

[frontend][keras] Add support for TimeDistributed #7006

Merged
merged 20 commits into from
Jan 21, 2022

Conversation

slyubomirsky
Copy link
Contributor

This PR is an attempt to support the TimeDistributed layer in the Keras frontend. Because TimeDistributed takes a layer as an argument, I had to modify the existing conversion functions to take parameters instead of making assumptions about the Keras model or etab data structure. Perhaps it might be better not to attempt to reuse the existing conversion functions and special-case the different possible arguments to TimeDistributed -- I would be glad to take any suggestions on this.

I would also be interested to know what you might advise as to further test cases for TimeDistributed; I went by the example from the documentation and another use-case I found that used TimeDistributed with Dense.

@jroesch I am not entirely sure who would be the best person to review a change to the Keras frontend and would appreciate any advice

@jroesch
Copy link
Member

jroesch commented Dec 1, 2020

cc @mbrookhart @tkonolige @jwfromm

@slyubomirsky
Copy link
Contributor Author

Thanks, Jared!

)

conversion_func = lambda expr: _convert_map[inner_layer_op_name](
expr, inner_layer, etab, input_shape=inner_input_shape, data_layout=inner_data_layout
Copy link
Contributor

Choose a reason for hiding this comment

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

it seems like you could have just make a copy of etab but set a new 'input_shape' and 'data_layout' here. This would remove the need to change every other convert function. Am I missing a reason that wouldnt work? It's unclear to me what other assumptions are being made about etab.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would work for data_layout. input_shape is a field in the Keras layer and is not mutable, unfortunately

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another problem is that the etab is used in the end to produce the new Relay module. Copying the etab would be okay if I move all the definitions over to the original one and make sure there's no chance of a name conflict (that is probably doable).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://www.tensorflow.org/api_docs/python/tf/keras/models/clone_model I might be able to copy the inner layer to TimeDistributed and set its input_shape but I'm not sure whether it's possible. I will check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After reading over the ExprTable implementation, I do not think it was a good idea to attach the data layout (a property that has nothing to do with the ExprTable specifically) to the etab in the first place -- it seems like unnecessary coupling. Maybe there should be a different way of handling the shared state across these conversions.

Fwiw, I could just overwrite the data_layout field in the ExprTable when handling the TimeDistributed case and avoid having to modify the other conversion functions. I am not sure whether that would be a good idea from the standpoint of maintainability, though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok that seems reasonable. I think its fine to remove data_layout from etab and use explicit inputs.

@slyubomirsky
Copy link
Contributor Author

The test failure seems spurious (in an operator test file I didn't modify).

@tkonolige
Copy link
Contributor

This is a flakey test we've been seeing recently. Push a new commit to force the ci to rerun.

@slyubomirsky
Copy link
Contributor Author

Should be up to date, thanks for the advice

@tqchen
Copy link
Member

tqchen commented Mar 10, 2021

@jroesch @slyubomirsky @jwfromm please followup on this PR

@jwfromm
Copy link
Contributor

jwfromm commented Mar 18, 2021

@slyubomirsky can you resolve the conflicts this PR has with the current main branch? Once you do I think we can merge this as it LGTM.

@tqchen tqchen added the status: need update need update based on feedbacks label Mar 18, 2021
@slyubomirsky
Copy link
Contributor Author

Oh I'm terribly sorry, I got side tracked and haven't seen these updates. I will make the requested changes

@slyubomirsky
Copy link
Contributor Author

@jwfromm Rebased, terribly sorry about the long delay

@jroesch
Copy link
Member

jroesch commented Aug 27, 2021

@jwfromm @mbrookhart can you guys try to land this?

@jroesch jroesch added the needs-triage PRs or issues that need to be investigated by maintainers to find the right assignees to address it label Aug 27, 2021
@slyubomirsky
Copy link
Contributor Author

Should be back up to date, @jroesch @jwfromm et al

@jroesch jroesch merged commit 25c8f4c into apache:main Jan 21, 2022
yuanfz98 pushed a commit to yuanfz98/tvm that referenced this pull request Jan 24, 2022
* First pass on modifying Keras importer to handle TimeDistributed

* Use squeeze inside TimeDistributed, add tests

* linter fixes

* More linting

* Even more linting

* Fix unused argument annotations

* Forgot one pylint annotation

* Forgot to set up data layout in _convert_activation

* Decouple data_layout from etab

* Linting fix

* Forgot to set data_layout argument

* Missed an etab.data_format, also test_conv1d was not in the test file's main

* Rebase fixes

* Linting fix

* _convert_lambda needs a data layout argument too

* linting fix too

* Lint the test file too

* Redundant variables

* Simplify further

* Another simplification

Co-authored-by: Steven Lyubomirsky <slyubomirsky@octoml.ai>
ylc pushed a commit to ylc/tvm that referenced this pull request Feb 16, 2022
* First pass on modifying Keras importer to handle TimeDistributed

* Use squeeze inside TimeDistributed, add tests

* linter fixes

* More linting

* Even more linting

* Fix unused argument annotations

* Forgot one pylint annotation

* Forgot to set up data layout in _convert_activation

* Decouple data_layout from etab

* Linting fix

* Forgot to set data_layout argument

* Missed an etab.data_format, also test_conv1d was not in the test file's main

* Rebase fixes

* Linting fix

* _convert_lambda needs a data layout argument too

* linting fix too

* Lint the test file too

* Redundant variables

* Simplify further

* Another simplification

Co-authored-by: Steven Lyubomirsky <slyubomirsky@octoml.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage PRs or issues that need to be investigated by maintainers to find the right assignees to address it status: need review status: need update need update based on feedbacks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants