com.facebook.jni.CppException: Method 'forward' is not defined

Lumi: <p>My Code in Python</p>
<pre><code class="lang-auto">import torch
import torchvision
from torch.utils.mobile_optimizer import optimize_for_mobile

model = torchvision.models.mobilenet_v3_small(pretrained=True)
model.eval()
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
optimized_traced_model = optimize_for_mobile(traced_script_module)
optimized_traced_model._save_for_lite_interpreter("model.ptl")
</code></pre>
<p><strong>torch version : 2.1.0+cu118</strong><br>
<strong>torchvision version : 0.16.0+cu118</strong><br>
get file “model.ptl”</p>
<p>then Android Dependencies</p>
<pre><code class="lang-auto">dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    implementation 'org.pytorch:pytorch_android_lite:2.1.0'
    implementation 'org.pytorch:pytorch_android_torchvision_lite:2.1.0'
}
</code></pre>
<p>MainActivity.java</p>
<pre><code class="lang-auto">package com.example.ptmobilewalkthrough;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.pytorch.LiteModuleLoader;
import org.pytorch.MemoryFormat;
import org.pytorch.IValue;
import org.pytorch.Module;
import org.pytorch.Tensor;
import org.pytorch.torchvision.TensorImageUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {

    private Bitmap bitmap = null;
    private Module module = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            bitmap = BitmapFactory.decodeStream(getAssets().open("sample_card.jpg"));
            module = LiteModuleLoader.load(assetFilePath(this, "model.ptl"));
        }
        catch (IOException e){
            Log.e("PTRTDryRun", "Error reading assets", e);
            finish();
        }

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


        final Button button = findViewById(R.id.inferButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View w) {

                final Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap, TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, MemoryFormat.CHANNELS_LAST);
                Log.i("MyTag", "shape tensor image : " + inputTensor);

                // running the model
                final Tensor outputTensor = module.forward(IValue.from(inputTensor)).toTensor();


                // getting tensor content as java array of floats
                final float[] scores = outputTensor.getDataAsFloatArray();

                // searching for the index with maximum score
                float maxScore = -Float.MAX_VALUE;
                int maxScoreIdx = -1;
                for (int i = 0; i &lt; scores.length; i++) {
                    if(scores[i] &gt; maxScore) {
                        maxScore = scores[i];
                        maxScoreIdx = i;
                    }
                }
                String className = ImageNetClasses.IMAGENET_CLASSES[maxScoreIdx];

                TextView textView = findViewById(R.id.textView);
                textView.setText(className);
            }
        });
    }

    public static String assetFilePath(Context context, String assetName) throws IOException {
        File file = new File(context.getFilesDir(), assetName);
        if (file.exists() &amp;&amp; file.length() &gt; 0) {
            return file.getAbsolutePath();
        }

        try (InputStream is = context.getAssets().open(assetName)) {
            try (OutputStream os = new FileOutputStream(file)) {
                byte[] buffer = new byte[4 * 1024];
                int read;
                while ((read = is.read(buffer)) != -1) {
                    os.write(buffer, 0, read);
                }
                os.flush();
            }
            return file.getAbsolutePath();
        }
    }

}
</code></pre>
<p>When build test app, and click button Inference<br>
get error :</p>
<pre><code class="lang-auto">com.facebook.jni.CppException: Method 'forward' is not defined.
</code></pre>
<p>error code (MainActivity.java):</p>
<pre><code class="lang-auto">final Tensor outputTensor = module.forward(IValue.from(inputTensor)).toTensor();
</code></pre>
<p>but I defined ‘forward’ function in my model <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>
<p>i try change version <a href="https://mvnrepository.com/artifact/org.pytorch/pytorch_android_lite" rel="noopener nofollow ugc">https://mvnrepository.com/artifact/org.pytorch/pytorch_android_lite</a> 2.1.0 till 1.9.0</p>
<p>i saw discuss<br>
<a href="https://discuss.pytorch.org/t/pytorch-android-i-got-the-error-method-forward-is-not-defined-but-i-defined-forward-function-in-my-model/123247">https://discuss.pytorch.org/t/pytorch-android-i-got-the-error-method-forward-is-not-defined-but-i-defined-forward-function-in-my-model/123247</a><br>
<strong>but not solve about forward is not defined</strong> <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>
<p>anyone help me ?<br>
im sorry choosing word very bad <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>