Skip to content

Find SwiftPM metallib bundle near test binary#3562

Closed
RNT56 wants to merge 1 commit into
ml-explore:mainfrom
RNT56:pr/swiftpm-metallib-bundle-lookup
Closed

Find SwiftPM metallib bundle near test binary#3562
RNT56 wants to merge 1 commit into
ml-explore:mainfrom
RNT56:pr/swiftpm-metallib-bundle-lookup

Conversation

@RNT56

@RNT56 RNT56 commented May 19, 2026

Copy link
Copy Markdown

Summary

Fixes SwiftPM package/test layouts where default.metallib is copied to the generated SWIFTPM_BUNDLE.bundle next to the test binary, but MLX only checks nested bundle locations.

This keeps the existing lookup order and adds:

  • direct lookup for SWIFTPM_BUNDLE.bundle next to the current binary, walking a few parent directories
  • direct lookup when NSBundle.allBundles() already contains the SwiftPM resource bundle

Validation

  • pre-commit run --all-files passed
  • validated through the mlx-swift SwiftPM packaging branch:
    • swift test --filter StreamTests/testDeviceType passes after applying this MLX loader patch and generating default.metallib
    • the same test failed before this patch with Failed to load the default metallib

@RNT56 RNT56 force-pushed the pr/swiftpm-metallib-bundle-lookup branch from 93232ea to 1a75e1a Compare May 19, 2026 00:15
@zcbenz

zcbenz commented May 21, 2026

Copy link
Copy Markdown
Collaborator

Ping @davidkoski for review.

@zcbenz

zcbenz commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

At the moment I prefer adding an API to set the path to metallib directly, i.e. #3597, and I plan to close this PR, please let me know if the API would not be able to solve your use case.

@davidkoski

Copy link
Copy Markdown
Contributor

I think API to set a path would be fine, but I don't know how early it needs to be called. IIRC the worker threads start via static initializers and there is no way to guarantee being able to get in before that (perhaps that has changed?). Anyway I think the automatic scheme we have is ok and having an override would be useful.

@davidkoski

Copy link
Copy Markdown
Contributor

Ah, it looks like it is changed -- it happens when you first ask for Stream default_stream(Device d) / scheduler::scheduler().get_default_stream(d)

@zcbenz

zcbenz commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

Yeah the metal initialization has been delayed until a stream is actually created, it was to solve issues like #3148. So the API only needs to be called before the stream was explicitly or implicitly created.

I think we should keep current metallib discovery behavior unchanged, and ask users to use the API if they have some unusual bundle layout, do you think whether it is a good idea?

@davidkoski

Copy link
Copy Markdown
Contributor

That seems like a good plan -- then if somebody has custom lookup needs they can implement it in application code.

@zcbenz zcbenz closed this Jun 5, 2026
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

Successfully merging this pull request may close these issues.

3 participants