-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[TFLite] Support TFLite FP32 Relay frontend. #2365
Conversation
Seems that our CI Tensorflow has low version. (i.e. smaller than 1.12). It report error: However, flatbuffers / tflite package should be installed in our CI machine as said before. |
@FrozenGene no issues to upgrade with TF 1.12. |
@srkreddy1238 Do you mean I could make a PR to install / upgrade our CI's machine python packages? Even I can |
I mean the PR for docker changes to accommodate the dependencies for TFLite. @FrozenGene I can help with docker changes if you have difficulty. |
Yes. I am not familiar with docker. If you could help to install TFLite dependency, that's very nice! Thanks in advance. |
@FrozenGene I see tensorflow version is already 1.12.
|
@srkreddy1238 I think we should rebuild the docker image. Because I find this latest commit: e8d6d9a, the tensorflow version is 1.10. I have tested tensorflow 1.10, which will raise the same error: |
@FrozenGene I simplified TVM CI recipe by making a python package out of your tflite repository and uploaded it under web-data. |
Thanks @srkreddy1238 I have seen your prs, which helps a lot to resolve dependencies, thanks again! |
@srkreddy1238 I saw your PR #2371 is merged and rebase to the latest code. However, it raise the same error:
Have we upgraded successfully? or we need some time waiting docker image is updated? |
I think docker images should be rebuilt with new changes. cc @tqchen |
@tqchen could you help to rebuild docker image? Then I could update related code. Thanks in advance. |
I have updated the ci docker image, please check again |
@tqchen Thanks. it works now. @srkreddy1238 could you help to review this PR? |
@srkreddy1238 @ajtulloch @junrushao1994 @zhreshold @xqdan please help to review this PR when you have time |
------- | ||
tensor name in UTF-8 encoding | ||
""" | ||
return subgraph.Tensors(tensor_idx).Name().decode("utf-8") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@FrozenGene I am a little bit surprised that this could pass python3 CI, because str.decode
shouldn't exist in Python3...Any ideas? Is the resulted type of Name()
str
or bytearray
in Python3?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The resulted type of Name()
is not str
, it is bytes()
in TFLite. See: https://github.com/FrozenGene/tflite/blob/master/tflite/Tensor.py#L58
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha. So it is reasonable to me. Thanks!
# 3: N H W C, reshape to N H*W C, transpose to N C H*W | ||
# 4: N H W C, transpose to N C H W | ||
# add more if we need target shapes in future | ||
if output_shape_length == 1 or output_shape_length == 2: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I understand the logic here, but I do think it introduces some underlying assumption.
The assumption is: the input of squeeze operator comes out of conv2d with some specific layout, otherwise it will transpose layers crazily.
It doesn't look safe to me, but for now for specific models like MobileNet, it is fine. @tqchen please advise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. When we do handle Reshape
or squeeze
ops we should be very careful if we handle from input layout NHWC to NCHW. So, this is to obey channel first rule, i.e. C is before H, W but after N. Currently we only handle 1-4D, not N-D. This issue is not special for us, other converters will meet the same issue. Please see Tensorflow to CoreML converter, which has similar logic: https://github.com/tf-coreml/tf-coreml/blob/master/tfcoreml/_shape_sensitive_layers.py#L275
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like it is ok for me for now, but probably we could document it somewhere? DL models in other fields like NLP heavily use squeeze, which may violate our rules and crashes every thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the reason why I comment the rule in code. I think another place we could comment is our TFLite frontend tutorial. Any other suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, it looks good to me. @tqchen if you have any suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout is specific to vision operators, bringing in layout specific customization into basic transforms will not work for all models. Here it's only mobilenet hence it works, but I doubt it may get more complex when more models are added. A simple NLP model with one LSTM layer will fail these assumptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Future improvement may include
- multi subgraph support
- documenting how squeeze is handled (see our discussion for details)
In the scope of this PR, I think everything looks good.
Thanks for the reviews, I will leave this PR's management to @srkreddy1238 :) |
# 3: N H W C, reshape to N H*W C, transpose to N C H*W | ||
# 4: N H W C, transpose to N C H W | ||
# add more if we need target shapes in future | ||
if output_shape_length == 1 or output_shape_length == 2: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout is specific to vision operators, bringing in layout specific customization into basic transforms will not work for all models. Here it's only mobilenet hence it works, but I doubt it may get more complex when more models are added. A simple NLP model with one LSTM layer will fail these assumptions.
return tflite_output | ||
|
||
|
||
def compare_tflite_with_tvm(tflite_in_data, tvm_in_data, in_name, input_tensors, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Different input data for TVM and tflite ??
I understand this conversion is to felicitate NCHW across the model, this impose preprocessing.
Again non vision models may have challenges.
cc @tqchen pls advice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is discussed with @junrushao1994 before. As discussed, the issue is not our specific. Like TF-CoreML (https://github.com/tf-coreml/tf-coreml/blob/master/tfcoreml/_shape_sensitive_layers.py#L275), Intel OpenVINO, TensorRT and so on do the same things like us, we will meet the same problem. Yes, I agree there will be challenges, but currently let us start from little things, such as here limited 1-4D transformation and vision models (under internal development, we have tested many other models, doesn't have problems until now). When we want to support more generally ND reshape and NLP, let us see how to handle it better in future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(under internal development, we have tested many other models, doesn't have problems until now).
Have you tested InceptionV1 & V3 together ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I tested Inception V3 just now(https://www.tensorflow.org/lite/models) and no problem, don't find official Inception V1 TFLite model. Maybe I can convert Tensorflow Inception V1 model into TFLite. Inception V3 model has one op named concatenation
not included in this PR, I will supply more ops later. If someone need Inception V3 support, I will provide concatenation
op implementation without any problem. Current PR's main purpose is to provide infrastructure of TFLite frontend.
@FrozenGene |
Yes. I will add the tutorial in the following PR after this is merged like our frontends do. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with the assumption of tflite frontend will be applicable to only vision models and NCHW layout as default for input.
@FrozenGene request to bring in Inception V1/3/4 support soon to have an evidence that this layout conversion generalize for all types. |
Thanks @srkreddy1238 I will add related ops and Inception Models testing soon. |
* Support TFLite FP32 Relay frontend. * Fix lint issue * Remove unnecessary variables and packages * Add method doc string * Fix capital letter of method doc string * Add TFLite FP32 Relay frontend based on latest code * Merge the latest code * Solve cython issue with relay type inference * Modify code based on suggestions
* Support TFLite FP32 Relay frontend. * Fix lint issue * Remove unnecessary variables and packages * Add method doc string * Fix capital letter of method doc string * Add TFLite FP32 Relay frontend based on latest code * Merge the latest code * Solve cython issue with relay type inference * Modify code based on suggestions
* Support TFLite FP32 Relay frontend. * Fix lint issue * Remove unnecessary variables and packages * Add method doc string * Fix capital letter of method doc string * Add TFLite FP32 Relay frontend based on latest code * Merge the latest code * Solve cython issue with relay type inference * Modify code based on suggestions
* Support TFLite FP32 Relay frontend. * Fix lint issue * Remove unnecessary variables and packages * Add method doc string * Fix capital letter of method doc string * Add TFLite FP32 Relay frontend based on latest code * Merge the latest code * Solve cython issue with relay type inference * Modify code based on suggestions
* Support TFLite FP32 Relay frontend. * Fix lint issue * Remove unnecessary variables and packages * Add method doc string * Fix capital letter of method doc string * Add TFLite FP32 Relay frontend based on latest code * Merge the latest code * Solve cython issue with relay type inference * Modify code based on suggestions
This is the first PR of #2351 to support importing exist quantized int8 TFLite model. The base version of Tensorflow / TFLite is 1.12.
This PR support TFLite FP32 Relay frontend. The input layout is NCHW, which is not the same as TFLite's NHWC layout, because TVM's default supporting layout is NCHW and support NCHW very well like AutoTVM on ARM CPU. Don't worry, I have completed the translation layout work in the TFLite FE. Currently, it could run Mobilenet V1 successfully.
Prerequisite of Python packages:
You could install flatbuffers using
pip3 install flatbuffers
For the tflite package, as far as I know, we haven't pip package until now. You could choose two ways to install:
@tqchen To run the TFLite frontend test, we should have
flatbuffers
andtflite
packages in our CI machine. Please let me know if you have any questions.After this PR is merged, I will write the tutorial of how to use TFLite frontend like previous frontends do.