Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metal not found #36

Closed
dc-dc-dc opened this issue Mar 3, 2024 · 16 comments
Closed

Metal not found #36

dc-dc-dc opened this issue Mar 3, 2024 · 16 comments

Comments

@dc-dc-dc
Copy link

dc-dc-dc commented Mar 3, 2024

When installing through SwiftPM and trying to run I get the following error

libc++abi: terminating due to uncaught exception of type std::runtime_error: library not found
Failed to load device library from <default.metallib>
@davidkoski
Copy link
Collaborator

I wonder if you were doing a command line build with SwiftPM? See the note here:

That is exactly what I would expect to see -- it never built the metallib. You can still do command line builds but you need to use xcodebuild.

@dc-dc-dc
Copy link
Author

dc-dc-dc commented Mar 3, 2024

Correct I am just running this through command line, updating xcode right now and will run build and try again, do I need to run xcodebuild everytime?

@davidkoski
Copy link
Collaborator

Bare SwiftPM doesn't have a way to build the shaders -- you have to build it through xcodebuild (or CMake)

@dc-dc-dc
Copy link
Author

dc-dc-dc commented Mar 3, 2024

Is there docs in building using command line tools? Only see cmake build commands in the docs

@davidkoski
Copy link
Collaborator

I can add a note about how to use xcodebuild. This line from the circleci config is what you want:

https://github.com/ml-explore/mlx-swift/blob/main/.circleci/config.yml#L37

xcodebuild test -scheme mlx-swift-Package -destination 'platform=OS X'

That just builds everything. You could use -scheme Tutorial to build the tutorial example.

@davidkoski
Copy link
Collaborator

@dbhathena
Copy link

dbhathena commented Oct 18, 2024

@davidkoski I am seeing something exactly like this but when building in XCode...

MLX error: library not found
Failed to load device library from <default.metallib> or </private/var/containers/Bundle/Application/4A9CD5B9-9B79-48D2-9CD7-4B1A642FF40D/LiquidApp.app/mlx.metallib>. at /Users/<Home>/Library/Developer/Xcode/DerivedData/MyApp-fmvqzzgndlxiykavgnqvrpmmjqrb/SourcePackages/checkouts/mlx-swift/Source/Cmlx/include/mlx/c/array.cpp:61

Do you have any idea why this might be happening? I also get a SIGABRT error and the app crashes whenever I try to run an MLX array function.

@davidkoski
Copy link
Collaborator

It has to be something about how it is built or how it links. You can try building some of the applications from https://github.com/ml-explore/mlx-swift-examples to compare.

You can look at your own build outputs (you will need to adjust the path of course) -- it should look something like this:

ls ~/Library/Developer/Xcode/DerivedData/mlx-swift-examples-eimbjcofifunwybkcvhnzjbqwyri/Build/Products/Debug/mlx-swift_Cmlx.bundle/Contents/Resources/

default.metallib fft              reduction        steel

The default.metallib is what it needs to find and load. This is a resource for the mlx-swift_Cmlx.bundle which ends up getting copied into your app bundle:

ls ~/Library/Developer/Xcode/DerivedData/mlx-swift-examples-eimbjcofifunwybkcvhnzjbqwyri/Build/Products/Debug/MNISTTrainer.app/Contents/Resources/mlx-swift_Cmlx.bundle/Contents/Resources 

default.metallib fft              reduction        steel

All of that should happen automatically, but compare your build to one of the applications in the example code.

@davidkoski
Copy link
Collaborator

From the error message perhaps it doesn't have the mlx-swift_Cmlx.bundle in the app resources (and since it looks like an iOS app the layout is slightly different than the macOS example I showed above)

@dbhathena
Copy link

You're right, when I navigate to /Users/<me>/Library/Developer/Xcode/DerivedData/MyApp-fmvqzzgndlxiykavgnqvrpmmjqrb/Build/Products/Debug-iphoneos there is no mlx-swift_Cmlx.bundle to be found. I actually had gotten this logic working by modifying the mlx-swift-examples repo to start, and now am trying to port over that logic to my new app.

I'm somewhat new to iOS development...how do I make sure this mlx-swift_Cmlx.bundle gets included in the app resources?

@davidkoski
Copy link
Collaborator

If you have it structured in a similar way as mlx-swift-examples you will have an application with the UI and a framework with the model code (it isn't required to be this way, but this was very useful to make both applications and command line tools that use the same model code). Anyway, if your application is structured like that then look at your application target:

image

That should embed your framework. Then your framework should like against the various libraries -- linking against MLX will pull this bundle in:

image

My best guess is that you are missing the Embed on the app -> framework, but it could be something else entirely.

@dbhathena
Copy link

Hmm...I didn't create a framework, I just have an application target (the model code is directly in that target), and so I don't actually see an "embed" option in that section. I will try to create the framework structure, but in the meantime do you know why the embed option doesn't show up in the app target for the MLX packages themselves? Can I not embed the MLX package directly?

Screenshot 2024-10-18 at 6 00 10 PM

@davidkoski
Copy link
Collaborator

Yes, those are "libraries" rather than "frameworks" -- they implicitly donate their resources to a framework or application. If your application links these then I would expect it to include their resources.

@dbhathena
Copy link

Got it, thank you!

I have broken the model functionality into a framework, but am now getting the following warning:
Screenshot 2024-10-18 at 6 46 01 PM

Also, the project fails to build now with the error Command SwiftVerifyEmittedModuleInterface failed with a nonzero exit code - I'll follow up with my progress, but again if you have any idea why this might be, please let me know

@dbhathena
Copy link

Both these issues were resolved by setting "Build Libraries for Distribution" to "No" in the framework - thanks for the help @davidkoski !!

@davidkoski
Copy link
Collaborator

Awesome, glad to hear it. I think the original issue (missing docs) and this follow-on are resolved. We can open new issues as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants