[JIT] [Android] Debugging the model

username: zetyquickly
cooked: <p>Hello there,</p>
<p>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.</p>
<p>The main question is how to debug mobile model? At least print <code>Tensor</code> shapes during runtime. As I noticed Python <code>print</code> statements won’t produce outputs on mobile</p>
<p>Excuse me for direct mention. Would you mind to clarify it <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a>?</p>

username: IvanKobzarev
cooked: <p>Helo <a class="mention" href="/u/zetyquickly">@zetyquickly</a><br>
Yes, python “print” does not show up in logcat, (I even tried it with  <code>adb shell setprop log.redirect-stdio true</code> on emulator)<br>
By this moment our understanding is that model is debugged using python and we only run debugged model on mobile.<br>
But we will think how we can expose print and maybe something else to logcat.</p>

username: IvanKobzarev
cooked: <p>Hello <a class="mention" href="/u/zetyquickly">@zetyquickly</a>,</p>
<p>Just update about torchscript print and logcat:<br>
<code>adb shell setprop log.redirect-stdio</code> redirects only java <code>System.out</code><br>
torchscript print by default uses native stdout.</p>
<p>If you desperately need this print in android logcat, you may use this patch:<br>
<aside class="onebox githubcommit">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/commit/a038b15a4d89dada18da55f6218058aaff85cc61" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Commit">
    <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M10.86 7c-.45-1.72-2-3-3.86-3-1.86 0-3.41 1.28-3.86 3H0v2h3.14c.45 1.72 2 3 3.86 3 1.86 0 3.41-1.28 3.86-3H14V7h-3.14zM7 10.2c-1.22 0-2.2-.98-2.2-2.2 0-1.22.98-2.2 2.2-2.2 1.22 0 2.2.98 2.2 2.2 0 1.22-.98 2.2-2.2 2.2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/commit/a038b15a4d89dada18da55f6218058aaff85cc61" target="_blank" rel="nofollow noopener">[NFC][WIP] Android native stdout,stderr to logcat</a>
    </h4>

    <div class="github-info">
      <div class="date">
        committed <span class="discourse-local-date" data-format="ll" data-date="2019-12-17" data-time="18:57:02" data-timezone="UTC">06:57PM - 17 Dec 19 UTC</span>
      </div>

      <div class="user">
        <a href="https://github.com/IvanKobzarev" target="_blank" rel="nofollow noopener">
          <img alt="IvanKobzarev" src="https://avatars3.githubusercontent.com/u/6638825?v=4" class="onebox-avatar-inline" width="20" height="20">
          IvanKobzarev
        </a>
        
      </div>

      <div class="lines" title="changed 2 files with 33 additions and 0 deletions">
        <a href="https://github.com/pytorch/pytorch/commit/a038b15a4d89dada18da55f6218058aaff85cc61" target="_blank" rel="nofollow noopener">
          <span class="added">+33</span>
          <span class="removed">-0</span>
        </a>
      </div>
    </div>

  </div>
</div>




  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>which I copy-pasted from <a href="https://codelab.wordpress.com/2014/11/03/how-to-use-standard-output-streams-for-logging-in-android-apps/" rel="nofollow noopener">https://codelab.wordpress.com/2014/11/03/how-to-use-standard-output-streams-for-logging-in-android-apps/</a></p>
<p>If you build pytorch-android with this patch (<code>gradle -p android assembleDebug</code> and <a href="https://github.com/pytorch/pytorch/tree/master/android#building-pytorch-android-from-source" rel="nofollow noopener">use result aar files directly</a> ) after calling <code>PyTorchAndroid.nativeStdOutErrToLogcat()</code> you will see torchscript print in logcat.</p>
<p>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.</p>

username: zetyquickly
cooked: <p><a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a></p>
<p>That will be very helpful thanks!</p>
<p>BTW to call <code>PyTorchAndroid.nativeStdOutErrToLogcat()</code> should one import this function from some module, or <code>PyTorchAndroid</code> already is an upper level module?</p>

username: IvanKobzarev
cooked: <p>It’s in <code>org.pytorch</code> package</p>
<pre><code class="lang-auto">import org.pytorch.PyTorchAndroid;

...
    PyTorchAndroid.nativeStdOutErrToLogcat()
...
</code></pre>
<p>Importing the whole class should work. It’s packaged in <code>pytorch_android*aar</code></p>
<p>I have added example of its usage to this commit:<br>
<aside class="onebox githubcommit">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/commit/62d94d10466991595d316c7e5ceebbb1e2f18210" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Commit">
    <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M10.86 7c-.45-1.72-2-3-3.86-3-1.86 0-3.41 1.28-3.86 3H0v2h3.14c.45 1.72 2 3 3.86 3 1.86 0 3.41-1.28 3.86-3H14V7h-3.14zM7 10.2c-1.22 0-2.2-.98-2.2-2.2 0-1.22.98-2.2 2.2-2.2 1.22 0 2.2.98 2.2 2.2 0 1.22-.98 2.2-2.2 2.2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/commit/62d94d10466991595d316c7e5ceebbb1e2f18210" target="_blank" rel="nofollow noopener">[NFC][WIP] Android native stdout,stderr to logcat</a>
    </h4>

    <div class="github-info">
      <div class="date">
        committed <span class="discourse-local-date" data-format="ll" data-date="2019-12-17" data-time="18:57:02" data-timezone="UTC">06:57PM - 17 Dec 19 UTC</span>
      </div>

      <div class="user">
        <a href="https://github.com/IvanKobzarev" target="_blank" rel="nofollow noopener">
          <img alt="IvanKobzarev" src="https://avatars3.githubusercontent.com/u/6638825?v=4" class="onebox-avatar-inline" width="20" height="20">
          IvanKobzarev
        </a>
        
      </div>

      <div class="lines" title="changed 3 files with 34 additions and 0 deletions">
        <a href="https://github.com/pytorch/pytorch/commit/62d94d10466991595d316c7e5ceebbb1e2f18210" target="_blank" rel="nofollow noopener">
          <span class="added">+34</span>
          <span class="removed">-0</span>
        </a>
      </div>
    </div>

  </div>
</div>




  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<br>
(the branch is <a href="https://github.com/pytorch/pytorch/compare/ik_wip_android_stdouterr_to_logcat" rel="nofollow noopener">https://github.com/pytorch/pytorch/compare/ik_wip_android_stdouterr_to_logcat</a>)<br>
We already use <code>org.pytorch.PyTorchAndroid</code> in our testapp in repo for loading module from asset:<br>
<a href="https://github.com/pytorch/pytorch/blob/master/android/test_app/app/src/main/java/org/pytorch/testapp/MainActivity.java#L94" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/master/android/test_app/app/src/main/java/org/pytorch/testapp/MainActivity.java#L94</a></p>