From d042b8b050a04d9121323af44fac31ccf5d9ba02 Mon Sep 17 00:00:00 2001 From: Jack Atkinson Date: Thu, 13 Feb 2025 14:57:18 +0000 Subject: [PATCH] Add instructions for a conda environment on mac that can successfully build FTorch and utilise the MPS backend on Apple Silicon. Co-authored-by: Karl Harrison --- README.md | 9 ++++++--- conda/README.md | 26 ++++++++++++++++++++++++++ conda/environment_mac.yaml | 13 +++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 conda/environment_mac.yaml diff --git a/README.md b/README.md index 393f20ab..0d33212f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/conda/README.md b/conda/README.md index 9090210a..a8b988ef 100644 --- a/conda/README.md +++ b/conda/README.md @@ -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 diff --git a/conda/environment_mac.yaml b/conda/environment_mac.yaml new file mode 100644 index 00000000..439cf074 --- /dev/null +++ b/conda/environment_mac.yaml @@ -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