Model Loading issue in iOS

kennywalker: <p>I get the following error when trying to initialize a traced model with the iOS framework:<br>
PyTorchDemo[25132:6735005] false CHECK FAILED at /Users/distiller/project/c10/core/Backend.h (tensorTypeIdToBackend at /Users/distiller/project/c10/core/Backend.h:106)<br>
(no backtrace available)</p>
<p>The error appears similar to these Android issues:</p><aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/android-demo-app/issues/19">
  <header class="source">

      <a href="https://github.com/pytorch/android-demo-app/issues/19" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/android-demo-app</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/android-demo-app/issues/19" target="_blank" rel="noopener nofollow ugc">Failed to Load Custom Model in Android</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2019-10-16" data-time="01:37:23" data-timezone="UTC">01:37AM - 16 Oct 19 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2019-10-24" data-time="16:40:35" data-timezone="UTC">04:40PM - 24 Oct 19 UTC</span>
        </div>

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

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">Hello,

I'm trying to deploy a custom model in python to Android using pytorch<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden"> mobile. I used torch.jit.trace to trace and then save the model. The saved model is working correctly when I load it in both C++ and python. However, when using pytorch_android, the following error occurred when loading the module (inside Module.load(modelFileAbsolutePath) function). The only thing different from the PyTorchDemoApp is the model file path. And I've confirmed that the model file path is correct and the file exists. I'm using pytorch 1.3.0 in both python and android.

Any suggestions on why the model loading would fail? Would this be related to [this issue](https://github.com/pytorch/pytorch/issues/26764)? Any suggestions on how to get more debugging information? Thank you!

```
2019-10-15 21:25:36.585 24959-25880/org.pytorch.demo E/PyTorchDemo: Error during image analysis
    com.facebook.jni.CppException: false CHECK FAILED at ../c10/core/Backend.h (tensorTypeIdToBackend at ../c10/core/Backend.h:106)
    (no backtrace available)
        at org.pytorch.Module$NativePeer.initHybrid(Native Method)
        at org.pytorch.Module$NativePeer.&lt;init&gt;(Module.java:70)
        at org.pytorch.Module.&lt;init&gt;(Module.java:25)
        at org.pytorch.Module.load(Module.java:21)
        at org.pytorch.demo.vision.ImageClassificationActivity.analyzeImage(ImageClassificationActivity.java:168)
        at org.pytorch.demo.vision.ImageClassificationActivity.analyzeImage(ImageClassificationActivity.java:31)
        at org.pytorch.demo.vision.AbstractCameraXActivity.lambda$setupCameraX$2$AbstractCameraXActivity(AbstractCameraXActivity.java:90)
        at org.pytorch.demo.vision.-$$Lambda$AbstractCameraXActivity$t0OjLr-l_M0-_0_dUqVE4yqEYnE.analyze(lambda)
        at androidx.camera.core.ImageAnalysisAbstractAnalyzer.analyzeImage(ImageAnalysisAbstractAnalyzer.java:57)
        at androidx.camera.core.ImageAnalysisNonBlockingAnalyzer$1.run(ImageAnalysisNonBlockingAnalyzer.java:135)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.os.HandlerThread.run(HandlerThread.java:61)
```</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<aside class="quote quote-modified" data-post="1" data-topic="60990">
  <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/mohit7/48/17896_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/model-loading-issue-in-android/60990">Model Loading issue in android</a> <a class="badge-category__wrapper " href="/c/mobile/18"><span data-category-id="18" data-drop-close="true" class="badge-category " title="This category is dedicated for iOS and Android issues, new features and general discussion of PyTorch Mobile."><span class="badge-category__name">Mobile</span></span></a>
  </div>
  <blockquote>
    My model contains nn.Functional.interpolate layer and it is converted properly to .pt model using jit. 
But Loading same model in android giving issues. 
This is happening in case of interpolate layer only. 
Edit - Error trace 
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.custom_model/org.pytorch.custom_model.MainActivity}: com.facebook.jni.CppException: false CHECK FAILED at …/c10/core/Backend.h (tensorTypeIdToBackend at …/c10/core/Backend.h:106) 
(no backtrace…
  </blockquote>
</aside>

<p>The second linked issue states that there’s an Android update with a fix --is there a fix for the iOS framework?</p>
<p>The model I’m using was traced with PyTorch 1.3.1, and I’m using LibTorch 1.3.1</p>

ljk53: <p>Looks like the fix was landed to master as mentioned on <span class="hashtag">#29806</span> so it will be part of 1.4 release. You can test the nightly build of iOS. <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> could you please confirm?</p>

David_Reiss: <p>Yeah, I’m pretty sure this is resolved.  If you want to verify, the steps for testing with the nightly build are in the first half of this comment: <a href="https://discuss.pytorch.org/t/incomprehensible-behaviour/61710/4" class="inline-onebox">Incomprehensible behaviour</a></p>

xta0: <p>Hi <a class="mention" href="/u/kennywalker">@kennywalker</a>, are you using cocoapods on iOS? if so, the correct version should be 1.3.1. If the error still exist, try the iOS nightly build - <a href="https://ossci-ios-build.s3.amazonaws.com/libtorch_ios_nightly_build.zip" rel="nofollow noopener">https://ossci-ios-build.s3.amazonaws.com/libtorch_ios_nightly_build.zip</a>. Let me know if you have any questions.</p>

kennywalker: <p>The model loads with the nightly build.</p>
<p>Thank you for the help <a class="mention" href="/u/ljk53">@ljk53</a>, <a class="mention" href="/u/david_reiss">@David_Reiss</a>, <a class="mention" href="/u/xta0">@xta0</a></p>

kennywalker: <p>Hi <a class="mention" href="/u/xta0">@xta0</a></p>
<p>I was using cocoapods 1.3.1. Can the 1.4 podspec be published?.</p>

xta0: <p>Hi <a class="mention" href="/u/kennywalker">@kennywalker</a>, we’re going to publish 1.4 in Jan (Hopefully). Could you give a solution mark on this?  If there is any questions, feel free to leave a comment.</p>

thoval: <p>Hi <a class="mention" href="/u/xta0">@xta0</a>, is there an update about the release date of 1.4 for iOS?</p>

xta0: <p>I think it’ll be in the middle of Jan. <a class="mention" href="/u/jspisak">@jspisak</a> do you know the exact date?</p>

jspisak: <p>Yes, we are targeting mid next week to release 1.4. Look for the blog post on <a href="http://pytorch.org">pytorch.org</a>. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

fabricionarcizo: <p>Dear <a class="mention" href="/u/xta0">@xta0</a>, I tried to use the current iOS nightly build, but it doesn’t work. I’ve noticed that the file <code>libtorch.a</code> has only 944 bytes.</p>

fabricionarcizo: <p>Dear <a class="mention" href="/u/kennywalker">@kennywalker</a>, could you share with us the version you downloaded on December 19th, please?</p>

xta0: <p><a class="mention" href="/u/fabricionarcizo">@fabricionarcizo</a> the nightly build is from master branch. We’ve recently split the <code>libtorch.a</code> into multiple libraries. If you unzip the nightly build, you should see this - <code>libtorch_cpu.a</code> which is the old <code>libtorch.a</code>.</p>
<ul>
<li>PR regarding the spliting - <a href="https://github.com/pytorch/pytorch/pull/30315" rel="nofollow noopener">https://github.com/pytorch/pytorch/pull/30315</a>
</li>
<li>Nightly build - <a href="https://ossci-ios-build.s3.amazonaws.com/libtorch_ios_nightly_build.zip" rel="nofollow noopener">https://ossci-ios-build.s3.amazonaws.com/libtorch_ios_nightly_build.zip</a>
</li>
</ul>

fabricionarcizo: <p><a class="mention" href="/u/xta0">@xta0</a> I just tried to compile the <code>libtorch.a</code> using the branch <code>v1.4.0</code> with the following command:</p>
<pre><code class="lang-auto">SELECTED_OP_LIST=predictor.yaml BUILD_PYTORCH_MOBILE=1 IOS_ARCH=arm64 ./scripts/build_ios.sh
</code></pre>
<p>It compiles correctly, but I got some additional error when I try to link the <code>libtorch.a</code> on Xcode.</p>
<pre><code class="lang-auto">Undefined symbol: c10::NonVariableTypeMode::is_enabled()
Undefined symbol: torch::jit::script::Module::module_object() const
Undefined symbol: c10::NonVariableTypeMode::set_enabled(bool)
Undefined symbol: caffe2::detail::_typeMetaDataInstance_preallocated_6
Undefined symbol: at::TypeDefault::to(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool)
</code></pre>

xta0: <p><a class="mention" href="/u/fabricionarcizo">@fabricionarcizo</a>  <code>SELECTED_OP_LIST</code> is used for the custom build. Also, the build is in arm64, were you running in simulator?</p>
<p>Since I don’t have your code, my recommendation is to follow the tutorials</p>
<ul>
<li>1.4.0 non-custom build - <a href="https://github.com/pytorch/ios-demo-app" rel="nofollow noopener">the HelloWorld example</a>
</li>
<li>1.4.0 custom build - <a href="https://pytorch.org/mobile/ios/" rel="nofollow noopener">https://pytorch.org/mobile/ios/</a>
</li>
</ul>