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

[TF][Relay] TensorFlow Frontend support with shared params #5042

Merged
merged 5 commits into from
Mar 30, 2020

Conversation

lfengad
Copy link
Contributor

@lfengad lfengad commented Mar 11, 2020

We observe that in lots of tensorflow models used in our production environment, some operators share the same node as their attributes. For example, there might be two "tf.reduce_mean" operators each with its own "axis" attribute. Their "axis" attributes can take the same "tf.constant" node. In this case, different operators share the same node as attributes.

However, in the current tensorflow frontend of relay, such sharing of the same param among different operators is not supported. For example, in the current implementation of "_get_param" function in "python/tvm/relay/frontend/tensorflow.py", the corresponding param will be deleted permanently using "pop" from the "_params" dictionary after being checked in "_get_param" function. In this case, if another operator needs to check the same param as its attribute, that param will not be found. Hence, the current implementation of the tensorflow frontend cannot support param sharing among different operators.

In order to support the case of param sharing among operators, we modify the tensorflow frontend. We leverage on the python decorator method. Everytime "_get_param" is called, instead of deleting the corresponding visited param as in the current implementation, we record it into a "used_params" dictionary and keep it in the original "_params" dictionary, so that another operator can still visit it in the following process. Then after the whole relay graph completes building, we delete the params have been visited in "used_params" from "_params" to generate the final "_params" output.

Compared to the old implementation ,our new implementation doesn't delete the visited params immediately in "_get_param", but records them and deletes them at the final step. In this way, the case where different operators share the same param can be supported.

Any suggestions are welcome! @tqchen @FrozenGene

@srkreddy1238
Copy link
Contributor

yes, this is an issue I too see recently while working on TF 2.0 upgrade. Few suggestions on this.

  • I think we can just remove the pop of used param and leave it assuming relay build process removes unused params automatically. cc @tqchen or @jroesch to confirm this behaviour.
  • You may move the test case to default test_forward.py instead of a new file for it.

@@ -77,10 +77,21 @@ def _dim_check(attrs):
return False
return _dim_check, "Only 2d or 3d kernel supported."

# Build count_used to record params have been converted to attrs
used_params = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Only need set?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree with @srkreddy1238 if relay build can remove unused params, we don't need this here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the suggestions! Then I think the thing to do is simple and we can just remove the pop operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, I have already modified the code according to your suggestions! Thank you for the help!

@kevinthesun kevinthesun added the status: need update need update based on feedbacks label Mar 12, 2020
@lfengad
Copy link
Contributor Author

lfengad commented Mar 13, 2020

yes, this is an issue I too see recently while working on TF 2.0 upgrade. Few suggestions on this.

  • I think we can just remove the pop of used param and leave it assuming relay build process removes unused params automatically. cc @tqchen or @jroesch to confirm this behaviour.
  • You may move the test case to default test_forward.py instead of a new file for it.

Thank you for the suggestions! I can try it then!

mod, params = relay.frontend.from_tensorflow(constant_graph,
outputs=['output'])
with relay.build_config(opt_level=3):
graph, lib, params = relay.build(mod,
Copy link
Contributor

Choose a reason for hiding this comment

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

You may use the same test case to verify the relay behaviour dropping unused params if frontend just ignores pop. In this case final params after relay build shouldn't contain param '''axis'''

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah! I think the axis will be removed then. Thank you for the information!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, I have already modified the code according to both suggestions from you two! Thank you so much!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi I have already done the changes as your suggestions! Could you please update the status of this pr so that it could be merged? Thank you so much!

@lfengad lfengad force-pushed the master branch 3 times, most recently from 8e0f034 to 824f930 Compare March 13, 2020 05:04
Copy link
Contributor

@kevinthesun kevinthesun left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@FrozenGene FrozenGene left a comment

Choose a reason for hiding this comment

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

LGTM

@FrozenGene
Copy link
Member

ping @srkreddy1238 Please review it again. If approve, we should follow the instruction to set the status: https://docs.tvm.ai/contribute/code_review.html#approve-and-request-changes-explicitly

@FrozenGene
Copy link
Member

ping @srkreddy1238

@FrozenGene FrozenGene dismissed srkreddy1238’s stale review March 30, 2020 07:20

Has waited 7 days and ping 2 times. The reviewer's review has been solved

@FrozenGene
Copy link
Member

Thanks @lfengad's pr and @srkreddy1238 @kevinthesun 's review. Considering @srkreddy1238 's review comment is fixed and wait @srkreddy1238 7 days / ping two times, additionally two reviewers have approved, so let us merge it firstly. If @srkreddy1238 has other comments, we could update or revert this pr easily.

@FrozenGene FrozenGene merged commit c38f2f3 into apache:master Mar 30, 2020
@srkreddy1238
Copy link
Contributor

Sorry @lfengad. Was a bit disconnected few days over pandemic situations.
Have no comments and this PR is good. @FrozenGene thanks for merging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: need update need update based on feedbacks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants