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

iOS TensorFlowLiteC App Size impact #272

Open
pkgoogle opened this issue Dec 10, 2024 · 0 comments
Open

iOS TensorFlowLiteC App Size impact #272

pkgoogle opened this issue Dec 10, 2024 · 0 comments

Comments

@pkgoogle
Copy link

Original Issue: tensorflow/tensorflow#62671
Original Author: @omarzl

Issue type

Performance

Have you reproduced the bug with TensorFlow Nightly?

No

Source

source

TensorFlow version

2.15.0

Custom code

Yes

OS platform and distribution

No response

Mobile device

No response

Python version

No response

Bazel version

6.1.0

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

I open this issue to share my findings regarding app size impact of TensorFlowLiteC binary in iOS

Currently we are migrating from a really old version to v2.15.0:
https://github.com/tensorflow/tensorflow/blob/v2.15.0/tensorflow/lite/ios/TensorFlowLiteC.podspec

And we noticed a notable size increment:
Download size: +1.08MB
Install size: +2.51MB

We have a large codebase and the app size is a big concern for us, so I tried to find ways to reduce the impact.
I noticed that there is a macro tflite_ios_xcframework which creates the xcframework and then modfies the object files to hide public symbols.
So I created a new target using the rule apple_static_xcframework directly.
See omarzl/tensorflow@945d7ec (I can send the pull request if it helps)
I named this target TensorFlowLiteC_static_xcframework, then I compiled it:

bazel build --config=ios_fat -c opt --cxxopt=--std=c++17 \
//tensorflow/lite/ios:TensorFlowLiteC_static_xcframework --subcommands

By using the xcframework without hidden symbols I noticed that the size increment was notable less -50%:
Download size: +0.50MB
Install size: +1.24MB

Then I changed the clang optimization to -Os instead of -O3 which is the default one used by Bazel in opt config:

bazel build --config=ios_fat -c opt --cxxopt=--std=c++17 \
//tensorflow/lite/ios:TensorFlowLiteC_static_xcframework --copt=-Os --subcommands

There was a minor improvement using it:
Download size: +0.49MB
Install size: +1.23MB

Finally I tried using the original tflite_ios_xcframework target TensorFlowLiteC_xcframework but with -Os optimization, I was expecting a minor reduction but I was wrong, instead it incremented:
Download size: +1.30MB
Install size: +3.03MB

Here is a summary of my tests:

Size Target TensorFlowLiteC_xcframework Target TensorFlowLiteC_static_xcframework Target TensorFlowLiteC_xcframework & -Os opt Target TensorFlowLiteC_static_xcframework & -Os opt
Download +1.08MB +0.50MB +0.49MB +1.30MB
Install +2.51MB +1.24MB +1.23MB +3.03MB

I understand the reasons behind hiding symbols since they can cause a symbol collision ending in unexpected behaviors or crashes in the app but these tests found out that it can cause an increment in app size.

For our case, we don't have any symbol collision with Tensorflow so we can use an un-hided version of the xcframework which benefit us in less app size impact.
As a side note: The linker ld warns you if there is a symbol collision, we opt to make warnings to errors to catch any issue of this kind by adding the flag -Wl,-fatal_warnings

Finally looking at the code in hide_xcframework_symbols_with_allowlist.sh
https://github.com/tensorflow/tensorflow/blob/6887368d6d46223f460358323c4b76d61d1558a8/tensorflow/lite/ios/hide_xcframework_symbols_with_allowlist.sh#L120
I understand that by using xcrun ld -r you are changing public symbols to private ones to prevent this symbol collision but I don't see you are generating a dynamic executable, I see that the output is still a Mach-O object (MH_OBJECT) so I think that this documentation https://www.tensorflow.org/lite/guide/build_ios is wrong since there isn't a dynamic framework, it is still a static framework but with hidden symbols.

In summary, I don't think there is much that can be done without hiding the symbols, I open this issue in case anyone finds a better solution to reduce the size impact and to document what I have found.

Standalone code to reproduce the issue

The compilation commands were written above.

Relevant log output

No response

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

No branches or pull requests

1 participant