File size of dynamic libraries for android are very large

leigao97: <p>The file size of dynamic libraries for android generated under <code>pytorch/build_android/install/lib</code> are very large. Here is the detail:</p>
<blockquote>
<p>-rw-r–r-- 1 ultraz ultraz 5.7M Jun 30 23:17 build_android/install/lib/libc10.so<br>
-rw-r–r-- 1 ultraz ultraz  82M Jun 30 23:19 build_android/install/lib/libtorch_cpu.so<br>
-rw-r–r-- 1 ultraz ultraz 7.4K Jun 30 23:17 build_android/install/lib/libtorch_global_deps.so<br>
-rw-r–r-- 1 ultraz ultraz 7.4K Jun 30 23:19 build_android/install/lib/libtorch.so</p>
</blockquote>
<p>For libtorch_cpu, it is 82M, which is considerably large once integrated into a mobile app. I see the <code>script/build_android.sh</code> has already disabled a lot of features. I would like to ask if it is possible to further reduce the size of libs?</p>

chaoyanghe: <p>I also met this issue when compiling mobile version. 100M+ is too big for smartphones…</p>

Linbin: <p>Use selective build to only include the used operators in the final library, see</p>
<p><a href="https://pytorch.org/tutorials/recipes/mobile_interpreter.html#how-to-use-mobile-interpreter-custom-build" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/recipes/mobile_interpreter.html#how-to-use-mobile-interpreter-custom-build</a><br>
and<br>
<a href="https://pytorch.org/tutorials/prototype/tracing_based_selective_build.html" class="inline-onebox" rel="noopener nofollow ugc">(prototype) Tracing-based Selective Build Mobile Interpreter in Android and iOS — PyTorch Tutorials 1.12.0+cu102 documentation</a> (even smaller size)</p>

leigao97: <p>Thank you and I will try it.</p>

cccclai: <pre><code class="lang-auto">[quote="leigao97, post:1, topic:155576"]
-rw-r–r-- 1 ultraz ultraz 82M Jun 30 23:19 build_android/install/lib/libtorch_cpu.so
[/quote]
</code></pre>
<ol>
<li>
<p>Looks like it’s the size of an old interpreter. The size should be smaller if you check <code>build_android_lite/install/lib/libtorch_cpu.so</code>. See <a href="https://pytorch.org/tutorials/recipes/mobile_interpreter.html" class="inline-onebox" rel="noopener nofollow ugc">(beta) Efficient mobile interpreter in Android and iOS — PyTorch Tutorials 1.12.0+cu102 documentation</a></p>
</li>
<li>
<p>From the number, this <code>libtorch_cpu.so</code> includes all 4 android abis (armeabi-v7a, arm64-v8a, x86, x86_64). For one device, only one abi is needed and size should be much less. Select build is another way to bring down. the size.</p>
</li>
</ol>

leigao97: <p>I have tested a couple of cases for ANDROID_ABI=arm64-v8a, and here is the result:</p>
<ol>
<li>
<code>BUILD_LITE_INTERPRETER=0 ./script/build_android.sh</code>  → <code>libtorch_cpu.so </code> is 89M</li>
<li>
<code>BUILD_LITE_INTERPRETER=1 ./script/build_android.sh</code>  → <code>libtorch_cpu.so </code> is 81M</li>
<li>
<code>BUILD_LITE_INTERPRETER=1 .SELECTED_OP_LIST=scripted_lenet_model.yaml /script/build_android.sh</code> →  <code>libtorch_cpu.so </code> is 75M</li>
</ol>
<p><a class="mention" href="/u/cccclai">@cccclai</a> Can you clarify that I am using the new interpreter? For your second point, I think it only builds one specific ABI because I have declared before compiling.</p>

cccclai: <p>Try to use</p>
<pre><code class="lang-auto">BUILD_LITE_INTERPRETER=1 ./scripts/build_pytorch_android.sh x86
</code></pre>
<p>to build for x86.</p>
<p>The one with smaller size is the new interpreter. <code>BUILD_LITE_INTERPRETER=1</code> is the correct environement setting.</p>

Cagatay_Bilgin: <p>Hey there! Just wanted to let you know that we’ve been using ExecuTorch for a bit now, and the old stack is no longer supported. You can find ExecuTorch on GitHub: <a href="https://github.com/pytorch/executorch" class="inline-onebox" rel="noopener nofollow ugc">GitHub - pytorch/executorch: On-device AI across mobile, embedded and edge for PyTorch</a>. If you have any questions or want to stay in the loop, join our Discord server at <a href="https://discord.gg/Wyn9PWqP" class="inline-onebox" rel="noopener nofollow ugc">PyTorch Edge</a>.</p>