Error occurs when LiteModuleLoader.load is called

Hi,

I had run pytorch [HelloWorldApp]

(https://github.com/pytorch/android-demo-app/tree/master/HelloWorldApp)
successfully on Android phone.

but when I replace the HelloWorldApp modle file 

with our trained modle file “ScriptMod.pth”

Error ocurs when I call
Module module = LiteModuleLoader.load(assetFilePath(this, "ScriptMod.pth"));
I had also tried
Module module = Module.load(assetFilePath(this, "ScriptMod.pth"));

It complains

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)

I have no idea about it. anyone could help?

the modle file “ScriptMod.pth” is about 270kbs, I had doubted its size. but AI team explains that the model size
of HelloWorldApp is large due to image process.

the modle is generated this way:

        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')

I had tried .ptl file too.

Thanks

There’s difference between regular (full jit) and lite interpreter models:

regular model:
save with “module.save” → no “bytecode.pkl” in the model → load with “Module.load”

lite interpreter model:
save with “_save_for_lite_interpreter” → has “bytecode.pkl” → load with “LiteModuleLoader.load”

You said you tried the “.ptl” file which is a lite interpreter model, what was the error message for this one?

Thanks Linbin for your reply.

It reports:


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.<init>(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

and if I debug in single step. It stops at IoUtils.closeQuietly(info.fd); in handleDumpActivity function, in ActivityThread.java.

I captured the screen when it stops.

This error means the model is not saved in the lite interpreter format. Is it saved using _save_for_lite_interpreter, and without error?

Thanks, it is OK now.
It is caused by module file, and our AI team supplied a new module file.

https://pytorch.org/tutorials/recipes/mobile_interpreter.html?highlight=bytecode

Thanks a lot.