Crash when running my TorchScript model from JAVA

Aviais: <p>Hi,<br>
I’m trying to run my TorchScript model in JAVA, but I get an error and my application crashes. I based on the code in the “HelloWorld” example application, which worked fine on my Android device.</p>
<p><strong>code lines:</strong><br>
super.onCreate(savedInstanceState);<br>
setContentView(R.layout.activity_main);</p>
<pre><code>Bitmap bitmap = null;
Module module = null;
try {
  // creating bitmap from packaged into app android asset 'image.jpg',
  Log.d("PytorchHelloWorld", "Loading input image");
  bitmap = BitmapFactory.decodeStream(getAssets().open("image2.jpg"));

  // loading serialized torchscript module from packaged into app android asset model.pt,
  // app/src/model/assets/model.pt
  Log.d("PytorchHelloWorld", "Loading pytorch model");
  module = Module.load(assetFilePath(this, "torchScript_model.pt"));
} catch (IOException e) {
  Log.e("PytorchHelloWorld", "Error reading assets", e);
  finish();
}

// showing image on UI
ImageView imageView = findViewById(R.id.image);
imageView.setImageBitmap(bitmap);

// preparing input tensor
Log.d("PytorchHelloWorld", "Preparing input tensor");
final Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap,
    TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB);

// running the model
Log.d("PytorchHelloWorld", "Running the model");
final Tensor outputTensor = module.forward(IValue.from(inputTensor)).toTensor();
TextView textView = findViewById(R.id.text);
textView.setText("ok");
</code></pre>
<p>}</p>
<p><strong>logs</strong><br>
PytorchHelloWorld:	 Loading input image	<br>
PytorchHelloWorld:	 Loading pytorch model	<br>
AndroidRuntime:	 Shutting down VM	<br>
AndroidRuntime:	 FATAL EXCEPTION: main	<br>
AndroidRuntime:	 Process: org.pytorch.helloworld, PID: 8625	<br>
AndroidRuntime:	 java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.helloworld/org.pytorch.helloworld.MainActivity}: com.facebook.jni.CppException: 	<br>
AndroidRuntime:	 Arguments for call are not valid.	<br>
AndroidRuntime:	 The following variants are available:	<br>
AndroidRuntime:		<br>
AndroidRuntime:	   aten::upsample_bilinear2d(Tensor self, int[2] output_size, bool align_corners) -&gt; (Tensor):	<br>
AndroidRuntime:	   Expected at most 3 arguments but found 5 positional arguments.	<br>
AndroidRuntime:		<br>
AndroidRuntime:	   aten::upsample_bilinear2d.out(Tensor self, int[2] output_size, bool align_corners, *, Tensor(a!) out) -&gt; (Tensor(a!)):	<br>
AndroidRuntime:	   Argument out not provided.</p>
<p>Can you please help me understand what’s the issue?</p>

IvanKobzarev: <p>Hello, <a class="mention" href="/u/aviais">@Aviais</a><br>
Which version of pytorch did you use to get your model.pt file?<br>
(To check the version:  <code>python -c "import torch; print(torch.version.__version)</code> )</p>
<p>Which version of gradle dependencies do you use?</p>
<p>It could be that the model was traced/scripted with different version from gradle dependencies, while between those versions aten::upsample_bilinear2d.out maybe changed.</p>
<p>If you trace/script with nightly build - its better to use android dependencies from nightly channel (<a href="https://github.com/pytorch/pytorch/tree/master/android#nightly" rel="nofollow noopener">https://github.com/pytorch/pytorch/tree/master/android#nightly</a>)</p>
<p>And matching version of gradle dependencies and installed pytorch.</p>

Aviais: <aside class="quote no-group" data-username="IvanKobzarev" data-post="2" data-topic="86415">
<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/ivankobzarev/48/19031_2.png" class="avatar"> IvanKobzarev:</div>
<blockquote>
<p>torch.version.__version</p>
</blockquote>
</aside>
<p>Hi <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a>,<br>
Thank you! You solved my problem!<br>
I used PyTorch 1.5.0 to get my ‘model.pt’ and gradle dependencies of 1.4.0. When I changed my dependencies to 1.5.0, the issues was solved!</p>
<p>Thanks!</p>