pytorch/ios-demo-app

Build succeeded but "Fatal error: Can't find the model file!"

Opened this issue ยท 9 comments

I have just ran the Hello World and Speech Recognition code, following all of the steps. The build was successful but after that the Hello World code did not show up the picture and the Speech Recognition code did not return the word I said (although the interface with "Start" and "Listening" still showed up)

I also checked the model file and it was at the right folder with the ViewController

The full output is "SpeechRecognition[47748:2353702] SpeechRecognition/ViewController.swift:36: Fatal error: Can't find the model file!"

Fixed!!!

[Speech Recognition]
I have just replaced the model file with the file from the quantized scripted wav2vec2 model file here, then drag and drop to the project.

But the HelloWorld code still remains bugged!

I'm also having this problem with the HelloWorld project: I can produce model.pt by running trace_model.py and can verify that it is at HelloWorld/HelloWorld/model/model.pt, but when I build the iOS app and it attempts to run, I get HelloWorld/ViewController.swift:11: Fatal error: Can't find the model file!

EDIT: I can also confirm that model.pt is listed under Copy Bundle Resources:

image

I've tried modifying ViewController.swift:8 to a few things:

if let filePath = Bundle.main.path(forResource: "model", ofType: "pt", inDirectory: "model"),
if let filePath = Bundle.main.path(forResource: "model", ofType: "pt", inDirectory: "HelloWorld/model"),
if let filePath = Bundle.main.path(forResource: "model", ofType: "pt", inDirectory: "/HelloWorld/model"),

But didn't have any luck.

Has anyone else been able to reproduce this? Very excited to try PyTorch mobile development, but I can't really move beyond the HelloWorld at this point!

Just tried on Xcode 14.0.1 on a M1 Mac. Same error.

I do see this warning though, am not sure they are related.

error: module importing failed: invalid pathname Warning: Error creating LLDB target at path '/Users/pbanavara/Library/Developer/Xcode/DerivedData/HelloWorld-cquprczqztzljbcqwusrrzphtrlq/Build/Products/Debug-iphonesimulator/HelloWorld.app'- using an empty LLDB target which can cause slow memory reads from remote devices: the specified architecture 'arm64-*-*' is not compatible with 'x86_64-apple-ios12.0.0-simulator' in '/Users/pbanavara/Library/Developer/Xcode/DerivedData/HelloWorld-cquprczqztzljbcqwusrrzphtrlq/Build/Products/Debug-iphonesimulator/HelloWorld.app/HelloWorld' 2022-10-03 18:31:58.194924+0530 HelloWorld[33606:5500414] [Assert] UINavigationBar decoded as unlocked for UINavigationController, or navigationBar delegate set up incorrectly. Inconsistent configuration may cause problems. navigationController=<UINavigationController: 0x7fd30c80b400>, navigationBar=<UINavigationBar: 0x7fd30c3644e0; frame = (0 44; 0 50); opaque = NO; autoresize = W; layer = <CALayer: 0x600003797980>> delegate=0x7fd30c80b400

use python 3.6.x and install torch 1.10.x then run python trace_model.py

The Error message is actually kind of misleading. It's trying to do two things at once:

  1. Find the resource model.pt
  2. Load the model into TorchModule

It throws the fatalError("Can't find the model file!") if either of those two fail, but the problem may not be finding the model file, but is somehow happening inside TorchModule. I suggest to split up the two statements to find out what is really the root of the problem, like this:

    private lazy var module: TorchModule = {
        
        let modelName = "model"
        
        if let filePath = Bundle.main.path(forResource: modelName, ofType: "ptl") {
            // Ok, found the model file
        } else {
            fatalError("Can't find the model file!")
        }
        
        if let filePath = Bundle.main.path(forResource: modelName, ofType: "ptl"),
            let module = TorchModule(fileAtPath: filePath) {
            return module
        } else {
            fatalError("Can't load TorchModule!")
        }
    }()

If now you get a different error message, the problem is the structure of the model. For example, for me I got
"Lite Interpreter version number does not match. The model version must be between 3 and 7 but the model version is 8"
Clearly this is a different issue. After installing a different pytorch version and running the python script again, it worked ๐ŸŽ‰
I used the following command to create a python environment with a older version:
conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 -c pytorch

If you still get the same error message, you have to add the file to Xcode.(XCode uses a it's internal filesystem that is not the same as the native filesystem)

  1. Click on the project root on the left side (blue symbol)
  2. Go to the tab build phases
  3. Go to "Copy Bundle Resources"
  4. Click the Plus sign
  5. Click "Add Other" and select the model.pt file
  6. Uncheck the boxes like this:

Screenshot 2022-10-31 at 17 22 55

I read several answers here, and realized the reason is the different version between LibTorch-Lite and Pytorch.

In my case, the Pytorch version is 2.0.
So, I changed the LibTorch-Lite in the Podfile to 1.13.0.1, and then ran "pod install" again.

Both Pytorch 2.0 and LibTorch-Lite 1.13.0.1 are the latest version at this moment.

Thanks @ho0-kim! PyTorch versioning has given me a lot of pain ๐Ÿ˜…

I was hitting the same issue (that the model file exists, but it can't be parsed by TorchModule). As @cyrillkuettel and @ho0-kim pointed out, this issue is due to the version mismatch between LibTorch-Lite and Pytorch.

Here is my solution.

  1. Using the nightly PyTorch iOS library in Cocoapods by following this link
    Basically, what I did is, first change the following line in Podfile to use nightly LibTorch-Lite:
    pod 'LibTorch-Lite-Nightly'
    Then run "pod install".
    And then install the nightly torch and torchvision by:
    pip install --pre torch torchvision -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
  2. Edit line 2 in HelloWorld/TorchBridge/TorchModule.mm to:
    #import <LibTorch-Lite-Nightly/LibTorch-Lite.h>

Then cmd+R to run it, and the app runs successfully.