Repository: pytorch
Issue: Failed to load TorchScript of SSDLite in android
Labels: oncall: mobile

### 🐛 Describe the bug

I tried to load SSDLite in Android but always failed with it.

Here is my code for export TorchScript: [sun-jiao/pytorch_ssdlite_export](https://github.com/sun-jiao/pytorch_ssdlite_export).

Use `detection_export.py` to convert the pretrained model to TorchScript. And then use `detection.py` to check if the exported model works fine.

And here is a minimized Android project to reproduce this issue: [sun-jiao/pytorch_detection_example](https://github.com/sun-jiao/pytorch_detection_example).

Copy or move the above exported `ssdlite.pt` to `app/src/main/assets/ssdlite.pt` and run the Android project. 

Here is the UI:
![533C3229CF90B997580730E07CA0F943](https://github.com/user-attachments/assets/dbb556a1-d484-4fe5-a1f1-26b2291ce993)

Click the button "Load model" and it will crash.

Here is the related log:

```
2024-12-19 17:55:53.534  7148-7148  nativeloader            net.sunjiao.pytorchdetectionexample  D  Load /data/app/~~QEZItQNbBIyxcvyQgPi2uQ==/net.sunjiao.pytorchdetectionexample-n9cinNNg6poEN6AyrBDSpA==/base.apk!/lib/arm64-v8a/libpytorch_jni.so using ns clns-7 from class loader (caller=/data/app/~~QEZItQNbBIyxcvyQgPi2uQ==/net.sunjiao.pytorchdetectionexample-n9cinNNg6poEN6AyrBDSpA==/base.apk!classes5.dex): ok
2024-12-19 17:55:53.535  7148-7148  nativeloader            net.sunjiao.pytorchdetectionexample  D  Load libtorch-code-gen.so using ns clns-7 from class loader (caller=/data/app/~~QEZItQNbBIyxcvyQgPi2uQ==/net.sunjiao.pytorchdetectionexample-n9cinNNg6poEN6AyrBDSpA==/base.apk!classes5.dex): dlopen failed: library "libtorch-code-gen.so" not found
2024-12-19 17:55:53.772  7148-7148  AndroidRuntime          net.sunjiao.pytorchdetectionexample  E  FATAL EXCEPTION: main
                                                                                                    Process: net.sunjiao.pytorchdetectionexample, PID: 7148
                                                                                                    com.facebook.jni.CppException: 
                                                                                                    Unknown builtin op: torchvision::nms.
                                                                                                    Could not find any similar ops to torchvision::nms. This op may not exist or may not be currently supported in TorchScript.
                                                                                                    :
                                                                                                      File "code/__torch__/torchvision/ops/boxes.py", line 128
                                                                                                      _55 = __torch__.torchvision.extension._assert_has_ops
                                                                                                      _56 = _55()
                                                                                                      _57 = ops.torchvision.nms(boxes, scores, iou_threshold)
                                                                                                            ~~~~~~~~~~~~~~~~~~~ <--- HERE
                                                                                                      return _57
                                                                                                    
                                                                                                    	at org.pytorch.NativePeer.initHybrid(Native Method)
                                                                                                    	at org.pytorch.NativePeer.<init>(NativePeer.java:27)
                                                                                                    	at org.pytorch.Module.load(Module.java:28)
                                                                                                    	at org.pytorch.Module.load(Module.java:38)
                                                                                                    	at net.sunjiao.pytorchdetectionexample.MainActivityKt.LoadModelButton$lambda$0(MainActivity.kt:64)
                                                                                                    	at net.sunjiao.pytorchdetectionexample.MainActivityKt.$r8$lambda$tsHf2Yc3D2EpbqvM6adjyUQecUc(Unknown Source:0)
                                                                                                    	at net.sunjiao.pytorchdetectionexample.MainActivityKt$$ExternalSyntheticLambda0.invoke(D8$$SyntheticClass:0)
                                                                                                    	at androidx.compose.foundation.ClickablePointerInputNode$pointerInput$3.invoke-k-4lQ0M(Clickable.kt:987)
                                                                                                    	at androidx.compose.foundation.ClickablePointerInputNode$pointerInput$3.invoke(Clickable.kt:981)
                                                                                                    	at androidx.compose.foundation.gestures.TapGestureDetectorKt$detectTapAndPress$2$1.invokeSuspend(TapGestureDetector.kt:255)
                                                                                                    	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                    	at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:179)
                                                                                                    	at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:168)
                                                                                                    	at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:474)
                                                                                                    	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:508)
                                                                                                    	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:497)
                                                                                                    	at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:368)
                                                                                                    	at androidx.compose.ui.input.pointer.SuspendingPointerInputModifierNodeImpl$PointerEventHandlerCoroutine.offerPointerEvent(SuspendingPointerInputFilter.kt:665)
                                                                                                    	at androidx.compose.ui.input.pointer.SuspendingPointerInputModifierNodeImpl.dispatchPointerEvent(SuspendingPointerInputFilter.kt:544)
                                                                                                    	at androidx.compose.ui.input.pointer.SuspendingPointerInputModifierNodeImpl.onPointerEvent-H0pRuoY(SuspendingPointerInputFilter.kt:566)
                                                                                                    	at androidx.compose.foundation.AbstractClickablePointerInputNode.onPointerEvent-H0pRuoY(Clickable.kt:947)
                                                                                                    	at androidx.compose.foundation.AbstractClickableNode.onPointerEvent-H0pRuoY(Clickable.kt:795)
                                                                                                    	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:317)
                                                                                                    	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:303)
                                                                                                    	at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:303)
                                                                                                    	at androidx.compose.ui.input.pointer.NodeParent.dispatchMainEventPass(HitPathTracker.kt:185)
                                                                                                    	at androidx.compose.ui.input.pointer.HitPathTracker.dispatchChanges(HitPathTracker.kt:104)
                                                                                                    	at androidx.compose.ui.input.pointer.PointerInputEventProcessor.process-BIzXfog(PointerInputEventProcessor.kt:113)
                                                                                                    	at androidx.compose.ui.platform.AndroidComposeView.sendMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1576)
                                                                                                    	at androidx.compose.ui.platform.AndroidComposeView.handleMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1527)
                                                                                                    	at androidx.compose.ui.platform.AndroidComposeView.dispatchTouchEvent(AndroidComposeView.android.kt:1466)
                                                                                                    	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3122)
                                                                                                    	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2803)
                                                                                                    	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3122)
                                                                                                    	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2803)
                                                                                                    	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3122)
                                                                                                    	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2803)
2024-12-19 17:55:53.773  7148-7148  AndroidRuntime          net.sunjiao.pytorchdetectionexample  E  	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3122)
                                                                                                    	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2803)
                                                                                                    	at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:458)
                                                                                                    	at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1982)
                                                                                                    	at android.app.Activity.dispatchTouchEvent(Activity.java:4533)
                                                                                                    	at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:416)
                                                                                                    	at android.view.View.dispatchPointerEvent(View.java:16737)
                                                                                                    	at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:7974)
                                                                                                    	at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:7732)
                                                                                                    	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:7128)
                                                                                                    	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:7185)
                                                                                                    	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:7151)
                                                                                                    	at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:7317)
                                                                                                    	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:7159)
                                                                                                    	at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:7374)
                                                                                                    	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:7132)
                                                                                                    	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:7185)
                                                                                                    	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:7151)
                                                                                                    	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:7159)
                                                                                                    	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:7132)
                                                                                                    	at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:10241)
                                                                                                    	at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:10192)
                                                                                                    	at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:10161)
                                                                                                    	at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:10383)
                                                                                                    	at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:295)
                                                                                                    	at android.os.MessageQueue.nativePollOnce(Native Method)
                                                                                                    	at android.os.MessageQueue.next(MessageQueue.java:346)
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:189)
                                                                                                    	at android.os.Looper.loop(Looper.java:317)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:8710)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
                                                                                                    	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [androidx.compose.ui.platform.MotionDurationScaleImpl@c93bdeb, androidx.compose.runtime.BroadcastFrameClock@d87a748, StandaloneCoroutine{Cancelling}@16a59e1, AndroidUiDispatcher@6b10306]
2024-12-19 17:55:53.779  7148-7148  Process                 net.sunjiao.pytorchdetectionexample  I  Sending signal. PID: 7148 SIG: 9
---------------------------- PROCESS ENDED (7148) for package net.sunjiao.pytorchdetectionexample ----------------------------
```

### Versions

Failed to run it so I'll give environment manually:
```
Collecting environment information...
Traceback (most recent call last):
  File "collect_env.py", line 692, in <module>
    main()
  File "collect_env.py", line 675, in main
    output = get_pretty_env_info()
             ^^^^^^^^^^^^^^^^^^^^^
  File "collect_env.py", line 670, in get_pretty_env_info
    return pretty_str(get_env_info())
                      ^^^^^^^^^^^^^^
  File "collect_env.py", line 495, in get_env_info
    pip_version, pip_list_output = get_pip_packages(run_lambda)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "collect_env.py", line 450, in get_pip_packages
    for line in out.splitlines()
                ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'splitlines'
```

System environment:
```
                   -`                    sunjiao@arch-83al 
                  .o+`                   ----------------- 
                 `ooo/                   OS: Arch Linux x86_64 
                `+oooo:                  Host: 83AL XiaoXinPro 14 IRH8 
               `+oooooo:                 Kernel: 6.12.4-zen1-1-zen 
               -+oooooo+:                Uptime: 1 day, 21 hours, 59 mins 
             `/:-:++oooo+:               Packages: 1794 (pacman) 
            `/++++/+++++++:              Shell: bash 5.2.37 
           `/++++++++++++++:             Resolution: 2880x1800 
          `/+++ooooooooooooo/`           DE: Plasma 6.2.4 
         ./ooosssso++osssssso+`          WM: kwin 
        .oossssso-````/ossssss+`         WM Theme: Lavanda-Sea-Light 
       -osssssso.      :ssssssso.        Theme: [Plasma], FRESH-Blueberries [GTK2/3] 
      :osssssss/        osssso+++.       Icons: Fluent [Plasma], Fluent [GTK2/3] 
     /ossssssss/        +ssssooo/-       Terminal: konsole 
   `/ossssso+/:-        -:/+osssso+-     CPU: 13th Gen Intel i5-13500H (16) @ 4.700GHz 
  `+sso+:-`                 `.-/+oso:    GPU: Intel Raptor Lake-P [Iris Xe Graphics] 
 `++:.                           `-/+/   Memory: 23426MiB / 31816MiB 
 .`                                 `/
```
Python version:
```
$ python --version
Python 3.12.7
```
Package version:
```
$ pip freeze | grep torch
pytorch-lightning==2.4.0
torch==2.5.1
torchaudio==2.5.1
torchmetrics==1.6.0
torchvision==0.20.1
```
Pytorch android package:
```
    api("org.pytorch", "pytorch_android", "2.1.0")
    api("org.pytorch", "pytorch_android_torchvision", "2.1.0")
```
```[tasklist]
### Tasks
```