Skip to content

Compatibility with pip-installed openmpi#2741

Merged
angeloskath merged 7 commits intoml-explore:mainfrom
pcuenca:mpilib
Nov 8, 2025
Merged

Compatibility with pip-installed openmpi#2741
angeloskath merged 7 commits intoml-explore:mainfrom
pcuenca:mpilib

Conversation

@pcuenca
Copy link
Copy Markdown
Contributor

@pcuenca pcuenca commented Nov 7, 2025

Proposed changes

When installing openmpi using pip in a virtual environment, the installation looks like this (incomplete):

.venv
├── bin
│   ├── mpirun
│   ├── ompi_info
├── lib
│   ├── libmpi.40.dylib  # <- not libmpi.dylib

However, mpi.cpp uses a hardcoded name, libmpi.dylib, to dlopen(). The installation does not provide a symlink.

On the Python side, distributed_run was already special-casing homebrew installations by explicitly passing DYLD_LIBRARY_PATH to mpirun. This PR:

  • Extends the existing mechanism so DYLD_LIBRARY_PATH is sent in all cases, as long as an mpi library is found.
  • Derives the library name by examining ompi_info with otool. Other common tools, such as mpirun, are not linked against the shared library.
  • Sends the library name as an envvar for mpi.cpp to use.

This should result in a more robust environment detection and increased compatibility.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

Manual Tests

  • macOS with pip
  • macOS with homebrew
  • macOS with conda
  • Linux (CPU) with pip
  • Linux (CPU) with conda (conda install openmpi; conda install conda-forge::openmpi did not put an mpirun executable anywhere in my environment)

@pcuenca
Copy link
Copy Markdown
Contributor Author

pcuenca commented Nov 7, 2025

If this solution is acceptable, I can tweak the docs a bit.

@madrob
Copy link
Copy Markdown
Contributor

madrob commented Nov 7, 2025

Does this suggest that instead of brew install openmpi in the CI setup we should python install it?

@pcuenca
Copy link
Copy Markdown
Contributor Author

pcuenca commented Nov 7, 2025

This works with brew, pip, and conda. Needs some adjustments for Linux (cpu backend).

@pcuenca
Copy link
Copy Markdown
Contributor Author

pcuenca commented Nov 7, 2025

As updated in the initial description, I tested the following combinations:

  • macOS with pip
  • macOS with homebrew
  • macOS with conda
  • Linux (CPU) with pip
  • Linux (CPU) with conda (conda install openmpi; conda install conda-forge::openmpi did not put an mpirun executable anywhere in my environment)

Copy link
Copy Markdown
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks great!

I 'll merge after the tests clear.

@angeloskath
Copy link
Copy Markdown
Member

Some random thought. Perhaps we should change the environment variable to MLX_MPI_LIBNAME since I don't think it is standard and we could avoid collisions. I am fine with MPI_LIBNAME too though.

@angeloskath angeloskath merged commit eba6a9d into ml-explore:main Nov 8, 2025
7 checks passed
@pcuenca
Copy link
Copy Markdown
Contributor Author

pcuenca commented Nov 8, 2025

change the environment variable to MLX_MPI_LIBNAME

Good call, thank you!

@pcuenca pcuenca deleted the mpilib branch November 8, 2025 13:17
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