Error occurs when LiteModuleLoader.load is called

username: mmz211
cooked: <p>Hi,</p>
<pre><code>I had run pytorch [HelloWorldApp]
</code></pre>
<p>(<a href="https://github.com/pytorch/android-demo-app/tree/master/HelloWorldApp" rel="noopener nofollow ugc">https://github.com/pytorch/android-demo-app/tree/master/HelloWorldApp</a>)<br>
successfully on Android phone.</p>
<pre><code>but when I replace the HelloWorldApp modle file 
</code></pre>
<p>with our trained modle file “ScriptMod.pth”</p>
<p>Error ocurs when I call<br>
<code>Module module = LiteModuleLoader.load(assetFilePath(this, "ScriptMod.pth"));</code><br>
I had also tried<br>
<code>Module module = Module.load(assetFilePath(this, "ScriptMod.pth"));</code></p>
<p>It complains</p>
<pre><code class="lang-auto">com.facebook.jni.CppException: PytorchStreamReader failed locating file bytecode.pkl: file not found ()  Exception raised from valid at ../caffe2/serialize/inline_container.cc:157 (most recent call first):
        (no backtrace available)
</code></pre>
<p>I have no idea about it. anyone could help?</p>
<p>the modle file “ScriptMod.pth” is about 270kbs, I had doubted its size. but AI team explains that the model size<br>
of HelloWorldApp is large due to image process.</p>
<p>the modle is generated this way:</p>
<pre><code>        dummpy_input = copy.deepcopy(trans_tensor(x_test[:256, :]))
        ScriptMod = torch.jit.trace(Mod, dummpy_input)
        MobOptMod = optimize_for_mobile(ScriptMod)

        # Mobile lite
        MobOptMod._save_for_lite_interpreter('./online/MobScriptMod.ptl')

        # Mobile
        ScriptMod.save('./online/ScriptMod.pth')
</code></pre>
<p>I had tried .ptl file too.</p>
<p>Thanks</p>

username: Linbin
cooked: <p>There’s difference between regular (full jit) and lite interpreter models:</p>
<p>regular model:<br>
save with “module.save” → no “bytecode.pkl” in the model → load with “Module.load”</p>
<p>lite interpreter model:<br>
save with “_save_for_lite_interpreter” → has “bytecode.pkl” → load with “LiteModuleLoader.load”</p>
<p>You said you tried the “.ptl” file which is a lite interpreter model, what was the error message for this one?</p>

username: mmz211
cooked: <p>Thanks Linbin for your reply.</p>
<p>It reports:</p>
<pre><code class="lang-auto">
2022-02-15 16:36:00.492 29338-29338/org.pytorch.helloworld E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.pytorch.helloworld, PID: 29338
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.helloworld/org.pytorch.helloworld.MainActivity}: com.facebook.jni.CppException: PytorchStreamReader failed locating file bytecode.pkl: file not found ()
    Exception raised from valid at ../caffe2/serialize/inline_container.cc:157 (most recent call first):
    (no backtrace available)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4060)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4247)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2613)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:219)
        at android.app.ActivityThread.main(ActivityThread.java:8668)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)
     Caused by: com.facebook.jni.CppException: PytorchStreamReader failed locating file bytecode.pkl: file not found ()
    Exception raised from valid at ../caffe2/serialize/inline_container.cc:157 (most recent call first):
    (no backtrace available)
        at org.pytorch.LiteNativePeer.initHybrid(Native Method)
        at org.pytorch.LiteNativePeer.&lt;init&gt;(LiteNativePeer.java:23)
        at org.pytorch.LiteModuleLoader.load(LiteModuleLoader.java:29)
        at org.pytorch.helloworld.MainActivity.onCreate(MainActivity.java:213)
        at android.app.Activity.performCreate(Activity.java:8214)
        at android.app.Activity.performCreate(Activity.java:8202)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4033)
          ... 11 more
</code></pre>
<p>and if I debug in single step. It stops at IoUtils.closeQuietly(info.fd); in handleDumpActivity function, in ActivityThread.java.</p>
<p>I captured the screen when it stops.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/6/564a8137b1c5650b94f65b7e1eae7da9689a4305.png" data-download-href="https://discuss.pytorch.org/uploads/default/564a8137b1c5650b94f65b7e1eae7da9689a4305" title="pytorch0"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/564a8137b1c5650b94f65b7e1eae7da9689a4305_2_690x388.png" alt="pytorch0" data-base62-sha1="cjmGFilQNx91vcRExlRdifXFnSd" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/564a8137b1c5650b94f65b7e1eae7da9689a4305_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/564a8137b1c5650b94f65b7e1eae7da9689a4305_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/564a8137b1c5650b94f65b7e1eae7da9689a4305_2_1380x776.png 2x" data-dominant-color="393B3D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">pytorch0</span><span class="informations">1920×1080 317 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Linbin
cooked: <aside class="quote no-group" data-username="mmz211" data-post="3" data-topic="143620">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mmz211/48/46456_2.png" class="avatar"> mmz211:</div>
<blockquote>
<p><code>failed locating file bytecode.pkl</code></p>
</blockquote>
</aside>
<p>This error means the model is not saved in the lite interpreter format. Is it saved using _save_for_lite_interpreter, and without error?</p>

username: mmz211
cooked: <p>Thanks, it is OK now.<br>
It is caused by module file, and our AI team supplied a new module file.</p>
<p><a href="https://pytorch.org/tutorials/recipes/mobile_interpreter.html?highlight=bytecode" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/recipes/mobile_interpreter.html?highlight=bytecode</a></p>
<p>Thanks a lot.</p>