# \[JIT\] \[Android\] Debugging the model

**URL:** <https://discuss.pytorch.org/t/jit-android-debugging-the-model/63950>\
**Category:** Mobile\
**Created:** [December 13, 2019, 9:43am UTC](https://discuss.pytorch.org/t/jit-android-debugging-the-model/63950 "2019-12-13T09:43:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zetyquickly](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zetyquickly/32/22489_2.png) [@zetyquickly](https://discuss.pytorch.org/u/zetyquickly)\
**Post date:** [December 13, 2019, 9:43am UTC](https://discuss.pytorch.org/t/jit-android-debugging-the-model/63950/1 "2019-12-13T09:43:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![IvanKobzarev](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ivankobzarev/32/19031_2.png) [@IvanKobzarev](https://discuss.pytorch.org/u/IvanKobzarev)\
**Post date:** [December 16, 2019, 9:28pm UTC](https://discuss.pytorch.org/t/jit-android-debugging-the-model/63950/2 "2019-12-16T21:28:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![IvanKobzarev](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ivankobzarev/32/19031_2.png) [@IvanKobzarev](https://discuss.pytorch.org/u/IvanKobzarev)\
**Post date:** [December 17, 2019, 7:07pm UTC](https://discuss.pytorch.org/t/jit-android-debugging-the-model/63950/3 "2019-12-17T19:07:21Z")

</div>

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:

> <https://github.com/pytorch/pytorch/commit/a038b15a4d89dada18da55f6218058aaff85cc61>

which I copy-pasted from [https://codelab.wordpress.com/2014/11/03/how-to-use-standard-output-streams-for-logging-in-android-apps/](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](https://github.com/pytorch/pytorch/tree/master/android#building-pytorch-android-from-source) ) 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.

---

<div class="post-metadata">

**Author:** ![zetyquickly](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zetyquickly/32/22489_2.png) [@zetyquickly](https://discuss.pytorch.org/u/zetyquickly)\
**Post date:** [December 17, 2019, 7:49pm UTC](https://discuss.pytorch.org/t/jit-android-debugging-the-model/63950/4 "2019-12-17T19:49:25Z")

</div>

@IvanKobzarev

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?

---

<div class="post-metadata">

**Author:** ![IvanKobzarev](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ivankobzarev/32/19031_2.png) [@IvanKobzarev](https://discuss.pytorch.org/u/IvanKobzarev)\
**Post date:** [December 17, 2019, 8:54pm UTC](https://discuss.pytorch.org/t/jit-android-debugging-the-model/63950/5 "2019-12-17T20:54:57Z")

</div>

It’s in `org.pytorch` package

```auto
import org.pytorch.PyTorchAndroid;

...
    PyTorchAndroid.nativeStdOutErrToLogcat()
...

```

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

I have added example of its usage to this commit:

> <https://github.com/pytorch/pytorch/commit/62d94d10466991595d316c7e5ceebbb1e2f18210>

  
(the branch is [https://github.com/pytorch/pytorch/compare/ik\_wip\_android\_stdouterr\_to\_logcat](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:  
[https://github.com/pytorch/pytorch/blob/master/android/test\_app/app/src/main/java/org/pytorch/testapp/MainActivity.java#L94](https://github.com/pytorch/pytorch/blob/master/android/test_app/app/src/main/java/org/pytorch/testapp/MainActivity.java#L94)
