Crash when running my TorchScript model from JAVA

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.

code lines:

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,
  // app/src/model/assets/
  Log.d("PytorchHelloWorld", "Loading pytorch model");
  module = Module.load(assetFilePath(this, ""));
} catch (IOException e) {
  Log.e("PytorchHelloWorld", "Error reading assets", e);

// showing image on UI
ImageView imageView = findViewById(;

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

// running the model
Log.d("PytorchHelloWorld", "Running the model");
final Tensor outputTensor = module.forward(IValue.from(inputTensor)).toTensor();
TextView textView = findViewById(;


PytorchHelloWorld: Loading input image
PytorchHelloWorld: Loading pytorch model
AndroidRuntime: Shutting down VM
AndroidRuntime: FATAL EXCEPTION: main
AndroidRuntime: Process: org.pytorch.helloworld, PID: 8625
AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.helloworld/org.pytorch.helloworld.MainActivity}: com.facebook.jni.CppException:
AndroidRuntime: Arguments for call are not valid.
AndroidRuntime: The following variants are available:
AndroidRuntime: aten::upsample_bilinear2d(Tensor self, int[2] output_size, bool align_corners) -> (Tensor):
AndroidRuntime: Expected at most 3 arguments but found 5 positional arguments.
AndroidRuntime: aten::upsample_bilinear2d.out(Tensor self, int[2] output_size, bool align_corners, *, Tensor(a!) out) -> (Tensor(a!)):
AndroidRuntime: Argument out not provided.

Can you please help me understand what’s the issue?

Hello, @Aviais
Which version of pytorch did you use to get your file?
(To check the version: python -c "import torch; print(torch.version.__version) )

Which version of gradle dependencies do you use?

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.

If you trace/script with nightly build - its better to use android dependencies from nightly channel (

And matching version of gradle dependencies and installed pytorch.

Hi @IvanKobzarev,
Thank you! You solved my problem!
I used PyTorch 1.5.0 to get my ‘’ and gradle dependencies of 1.4.0. When I changed my dependencies to 1.5.0, the issues was solved!