[Android] Large model 4GB on Android app is crashing

brainchip_india: <p>Hello all,</p>
<p>I have a working LLM Model of size 4GB, and I’m able to run it successfully on an x86 CPU with Pytorch, and when I try to load the same model from the Android application its crashing.</p>
<p>I have followed the instructions exactly from this website: <a href="https://pytorch.org/mobile/android/" rel="noopener nofollow ugc">https://pytorch.org/mobile/android/</a><br>
Currently, I’m loading the model explicitly from the internal storage, not from the assets, as the model is too large to save as a binary.</p>
<p>Here is my code snippet of the initialization:</p>
<pre><code class="lang-auto">private fun initialize(modelPath: String) {
val modelPath = "/storage/emulated/0/Download/model.ptl"
    if (initialized) return
    Logger.appendLog(" Loading model from: $modelPath")
   // module = LiteModuleLoader.load(modelPath)
    module = Module.load(modelPath)
    initialized = true
    Logger.appendLog(" Model loaded successfully.")
}
</code></pre>
<p><strong>build.gradle:</strong><br>
implementation(“org.pytorch:pytorch_android_lite:1.13.1”)<br>
implementation(“org.pytorch:pytorch_android_torchvision_lite:1.13.1”)</p>

cbilgin: <p>Hey <strong><a href="https://discuss.pytorch.org/u/brainchip_india">brainchip_india</a>,</strong></p>
<p>I don’t know a good way to run LLMs on LiteInterpreter. We support LLMs and in general way more GenAI functionality with ExecuTorch. The link you are referencing is now updated and points to ExecuTorch as well.</p>
<p>Please give that a try, and if you are still running into issues, let us know. We are also on Discord if you want to hang out with the team there.</p>

GerryMan: <p>Could be hitting android’s memory hit. Quantize or split the model or you might wanna test with <code>pytorch_android</code> instead of the lite version</p>

brainchip_india: <p>Hey <a class="mention" href="/u/cbilgin">@cbilgin</a> and <a class="mention" href="/u/gerryman">@GerryMan</a></p>
<p>I have another variant of the LLM model, which is 2GB in size, and I’m able to run it successfully on an Android device. It appears that the crash is due to a memory issue.</p>
<p>I tried cleaning up the cache memory using the lines below, but still no luck. Do you know any legit methods to clean up the RAM during the Model load?</p>
<p><a class="mention" href="/u/cbilgin">@cbilgin</a> Could you please point me to the Pytorch Discord channel link?</p>
<p><code>System.gc()  PyTorchAndroid.setNumThreads(Runtime.getRuntime().availableProcessors() / 2)</code></p>

brainchip_india: <p>Later, I tried to check the adb logs, and there I found that the crash is due to unsupported op[rms norm], see the detailed logs below.</p>
<p>E  FATAL EXCEPTION: main Process: com.example.pytorch_new_rag_llm, PID: 29995 com.facebook.jni.CppException: <strong>Following ops cannot be found: [aten::rms_norm]</strong>. Please check if the operator library is included in the build. If built with selected ops, check if these ops are in the list. If you are a Meta employee, please see <a href="http://fburl.com/missing_ops" rel="noopener nofollow ugc">fburl.com/missing_ops</a> for a fix. Or post it in <a href="https://discuss.pytorch.org/c/mobile/" title="https://discuss.pytorch.org/c/mobile/">https://discuss.pytorch.org/c/mobile/</a> ()Exception raised from initialize_operators at /home/agunapal/pytorch/torch/csrc/jit/mobile/function.cpp:90</p>

olokobayusuf: <p>Have you tried using ExecuTorch instead?</p>

cbilgin: <p>Here you go! <a href="http://discord.com/invite/74dmqtAQQs" class="inline-onebox" rel="noopener nofollow ugc">PyTorch Edge</a></p>