[JIT] [Android] Debugging the model

Hello there,

Recently I worked a lot with mobile deployment of PyTorch models and figured out that having debugged TorchScript model on PC sometimes isn’t enough to run it without errors on Android.

The main question is how to debug mobile model? At least print Tensor shapes during runtime. As I noticed Python print statements won’t produce outputs on mobile

Excuse me for direct mention. Would you mind to clarify it @IvanKobzarev?

Helo @zetyquickly
Yes, python “print” does not show up in logcat, (I even tried it with adb shell setprop log.redirect-stdio true on emulator)
By this moment our understanding is that model is debugged using python and we only run debugged model on mobile.
But we will think how we can expose print and maybe something else to logcat.

Hello @zetyquickly,

Just update about torchscript print and logcat:
adb shell setprop log.redirect-stdio redirects only java System.out
torchscript print by default uses native stdout.

If you desperately need this print in android logcat, you may use this patch:

which I copy-pasted from https://codelab.wordpress.com/2014/11/03/how-to-use-standard-output-streams-for-logging-in-android-apps/

If you build pytorch-android with this patch (gradle -p android assembleDebug and use result aar files directly ) after calling PyTorchAndroid.nativeStdOutErrToLogcat() you will see torchscript print in logcat.

But we are not going to merge this :), we think about custom handling of print on mobile and print it to logcat. But doing it might take some time.


That will be very helpful thanks!

BTW to call PyTorchAndroid.nativeStdOutErrToLogcat() should one import this function from some module, or PyTorchAndroid already is an upper level module?

It’s in org.pytorch package

import org.pytorch.PyTorchAndroid;


Importing the whole class should work. It’s packaged in pytorch_android*aar

I have added example of its usage to this commit:

(the branch is https://github.com/pytorch/pytorch/compare/ik_wip_android_stdouterr_to_logcat)
We already use org.pytorch.PyTorchAndroid in our testapp in repo for loading module from asset: