Lite PTL model: Expected Tensor but got None

sardox: <p>hello, everyone! Im trying to deploy sample text classification model on android device but encountered an issue that unable to figure out.</p>
<p>I followed article “Classifying Names with a Character-Level RNN” where we train model to classify words based on language<br>
<a href="https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial</a></p>
<p><em>(output is a &lt;1 x n_categories&gt; Tensor, where every item is the likelihood of that category)</em></p>
<p>Model was successfully trained and i can run predictions locally on my machine with correct results:</p>
<blockquote>
<p>python predict.py Nasir<br>
(-0.73) Arabic<br>
(-2.03) French<br>
(-2.32) Czech</p>
</blockquote>
<p>To use model on mobile device, it needs to be converted to PTL format using this code:</p>
<blockquote>
<p>import torch<br>
from torch.utils.mobile_optimizer import optimize_for_mobile<br>
model = torch.load(‘names-classification.pt’)<br>
model.eval()<br>
scripted_module = torch.jit.script(model)<br>
optimized_scripted_module = optimize_for_mobile(scripted_module)<br>
optimized_scripted_module._save_for_lite_interpreter(“names-classification-lite.ptl”)</p>
</blockquote>
<p>As a results I have lite ptl model file, that Im trying to use it on device. But at this point im not confident that model was converted properly - was not able to find a way to run predictions on local machine with PTL model.</p>
<p>Next step is android code:</p>
<pre><code class="lang-auto">  	    mModule = LiteModuleLoader.load(liteModelPath)
        val bytes = userInputtedName.toByteArray(Charset.forName("UTF-8"))
        val shape = longArrayOf(1, bytes.size.toLong())
        val inputTensor = Tensor.fromBlobUnsigned(bytes, shape)
        val outputTensor1 = mModule!!.forward(IValue.from(inputTensor))
</code></pre>
<p>It loads the model without errors, but then it crashes on last line where I call “forward”</p>
<p><strong><code>com.facebook.jni.CppException: Expected Tensor but got None</code></strong></p>
<p>Looks like the error is coming from here:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/67e6c76a18597ad5d874e0ff97a174b8224b1741/aten/src/ATen/core/ivalue.cpp#L983">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/67e6c76a18597ad5d874e0ff97a174b8224b1741/aten/src/ATen/core/ivalue.cpp#L983" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/67e6c76a18597ad5d874e0ff97a174b8224b1741/aten/src/ATen/core/ivalue.cpp#L983" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/67e6c76a18597ad5d874e0ff97a174b8224b1741/aten/src/ATen/core/ivalue.cpp#L983</a></h4>



    <pre class="onebox"><code class="lang-cpp">
      <ol class="start lines" start="973" style="counter-reset: li-counter 972 ;">
          <li>  // NB: this doesn't work if an object contains itself, and it may</li>
          <li>  // come up in the future when we expand the object system, we will</li>
          <li>  // have a follow up PR to fix this when it becomes an issue.</li>
          <li>  if (!isAliasOf(copy)) {</li>
          <li>    memo[*this] = copy;</li>
          <li>  }</li>
          <li>  return copy;</li>
          <li>}</li>
          <li></li>
          <li>void IValue::reportToTensorTypeError() const {</li>
          <li class="selected">  TORCH_CHECK(false, "Expected Tensor but got ", tagKind());</li>
          <li>}</li>
          <li></li>
          <li>std::string ivalue::Object::name() const {</li>
          <li>  // NOLINTNEXTLINE(bugprone-unchecked-optional-access)</li>
          <li>  return type()-&gt;name()-&gt;qualifiedName();</li>
          <li>}</li>
          <li></li>
          <li>IValue ivalue::Object::getAttr(const std::string&amp; name) const {</li>
          <li>  const size_t slot = type()-&gt;getAttributeSlot(name);</li>
          <li>  return getSlot(slot);</li>
      </ol>
    </code></pre>



  </article>

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

  <div style="clear: both"></div>
</aside>
<p>
But at this point Im stuck and cant quite tell:</p>
<ol>
<li>what does this error mean?</li>
<li>did i configure input sensor wrong?</li>
<li>is lite version of model is broken?</li>
<li>is there a mismatch between versions of pytorch I used to train model and mobile lite library?</li>
</ol>
<p>More details:</p>
<blockquote>
<p>Example of inputTensor (toString):  Tensor([1, 10], dtype=torch.uint8)</p>
<p>pytorch_android_lite:1.13.1<br>
torch Version: 2.3.1<br>
torchtext Version: 0.18.0<br>
python Version: 3.11.9</p>
</blockquote>

sardox: <p>update: tried different versions on mobile library and different version of local pytorch - same issue</p>