Skip to content

Commit

Permalink
Add instructions for a conda environment on mac that can successfully
Browse files Browse the repository at this point in the history
build FTorch and utilise the MPS backend on Apple Silicon.

Co-authored-by: Karl Harrison <[email protected]>
  • Loading branch information
jatkinson1000 and kh296 committed Feb 13, 2025
1 parent e0727a7 commit d042b8b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,12 @@ Note that LibTorch is not supported for the GNU Fortran compiler with MinGW.

#### Apple Silicon Support

At the time of writing, LibTorch is only officially available for x86 architectures
(according to https://pytorch.org/). However, the version of PyTorch provided by
`pip install torch` uses an ARM binary for LibTorch which works on Apple Silicon.
At the time of writing [there are issues](https://github.com/pytorch/pytorch/issues/143571)
building FTorch on apple silicon when linking to downloaded `LibTorch` binaries or
pip-installed PyTorch.
FTorch can successfully be built, including utilising the MPS backend, from inside a
conda environment using the environment files and instructions in
[`conda/`](https://github.com/Cambridge-ICCS/FTorch/tree/main/conda).

#### Conda Support

Expand Down
26 changes: 26 additions & 0 deletions conda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ cmake --build . --target install
Note: There is the option of using `--parallel` to speed this up as described in
the main documentation.

### Mac and Mps

At the time of writing [there are issues](https://github.com/pytorch/pytorch/issues/143571)
building FTorch when linking to downloaded `LibTorch` binaries or pip-installed PyTorch.
FTorch can successfully be built, including utilising the MPS backend, from inside a
conda environment using the environment files provided here.

From a conda base environment run:
```sh
conda env create -f environment_mac.yaml
```
from this directory to create the environment and install dependencies.
We install PyTorch using `pip` from within the conda environment which should include
the Mps backend.

FTorch can then be build with a CMake command similar to the following:
```sh
cmake \
-DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX \
-DCMAKE_PREFIX_PATH=$(python -c 'import torch;print(torch.utils.cmake_prefix_path)') \
-DCMAKE_BUILD_TYPE=Release \
..
cmake --build . --target install
```
Note: There is the option of using `--parallel` to speed this up as described in
the main documentation.

### Other Backends

Expand Down
13 changes: 13 additions & 0 deletions conda/environment_mac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: ftorch-mac
channels:
- conda-forge
- nodefaults
dependencies:
- fortran-compiler
- cxx-compiler
- cmake >=3.15
- openmpi >=5.0.6 # For pFUnit
- pip
- pip: # We install PyTorch using pip as recommended by PyTorch
- torch
- torchvision

0 comments on commit d042b8b

Please sign in to comment.