Skip to content

Commit 4f2c28a

Browse files
committed
Improve metallib fallback when mlx module lacks __file__
1 parent 00e0632 commit 4f2c28a

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ jobs:
202202
203203
echo "::warning::mlx.metallib missing from native artifact; attempting to source from installed mlx package"
204204
python - <<'PY'
205+
import importlib.util
206+
from importlib import resources
205207
import pathlib
206208
import shutil
207209
import sys
@@ -212,9 +214,30 @@ jobs:
212214
print("::error::The 'mlx' Python package is not installed; cannot locate mlx.metallib.")
213215
sys.exit(1)
214216
215-
kernels_dir = pathlib.Path(mlx.__file__).resolve().parent / "backend" / "metal" / "kernels"
216-
if not kernels_dir.exists():
217-
print(f"::error::Could not find MLX metal kernels directory at {kernels_dir}")
217+
kernels_dir = None
218+
219+
spec = importlib.util.find_spec("mlx.backend.metal.kernels")
220+
if spec and spec.origin:
221+
candidate = pathlib.Path(spec.origin).resolve().parent
222+
if candidate.exists():
223+
kernels_dir = candidate
224+
225+
if kernels_dir is None:
226+
try:
227+
resource = resources.files("mlx.backend.metal") / "kernels"
228+
except (ModuleNotFoundError, AttributeError):
229+
resource = None
230+
if resource is not None:
231+
try:
232+
with resources.as_file(resource) as extracted:
233+
extracted_path = pathlib.Path(extracted)
234+
if extracted_path.exists():
235+
kernels_dir = extracted_path
236+
except FileNotFoundError:
237+
pass
238+
239+
if kernels_dir is None or not kernels_dir.exists():
240+
print("::error::Could not locate the MLX metal kernels directory; checked module spec and importlib resources.")
218241
sys.exit(1)
219242
220243
preferred = kernels_dir / "mlx.metallib"

0 commit comments

Comments
 (0)