Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FastSurfer on Apple M4 does not run #668

Open
gregordoerl opened this issue Mar 3, 2025 · 10 comments
Open

FastSurfer on Apple M4 does not run #668

gregordoerl opened this issue Mar 3, 2025 · 10 comments
Labels
needs-fix A reproducable bug that needs to be fixed question Further information is requested

Comments

@gregordoerl
Copy link

Hello,
Thanks for the nice tool and good documentation! Unfortunately, I was unable to get FastSurfer to run on my Mac. Here are some details:

MacBook Pro, M4 Pro Chip, 24GB mem, macOS 15.3.1
Native installation ran fine, I created a Conda environment with python3.10 without issues.

When trying to run a subject (here, a longitudinal analysis with two timepoints, one T1 each, 0.8 mm resolution), I soon got an error. My command was:

bash ./long_fastsurfer.sh --tid s001 --t1s $dataDir/m1/s001_T1.nii $dataDir/m2/s001_T1.nii --tpids m1 m2 --sd $dataDir --py "python3.10 -s" --device mps --3T

Here a part of the error:
/AppleInternal/Library/BuildRoots/d187755d-b9a3-11ef-83e5-aabfac210453/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:829: failed assertion `[MPSNDArray initWithDevice:descriptor:isTextureBacked:] Error: NDArray dimension length > INT_MAX'
/Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/multiprocessing/resource_tracker.py:224: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
warnings.warn('resource_tracker: There appear to be %d '

I'll attach the log file:
long_fastsurfer.log

Environment

When running run_fastsurfer.sh --version all I get:
Setting ENV variable FASTSURFER_HOME to script directory /Users/gregor/FastSurfer.
Change via environment to location of your choice if this is undesired (export FASTSURFER_HOME=/dir/to/FastSurfer)
2.4.2+7e53343

  • Installation type: native
  • FreeSurfer Version: 7.4.1/
  • OS: macOS, MacBook Pro, M4 Pro Chip, 24GB mem, macOS 15.3.1
  • GPU: I tried the flag '--device mps' as well as without it, got the same result

I appreciate any help, thanks in advance.

Best, gregor

@gregordoerl gregordoerl added the question Further information is requested label Mar 3, 2025
@dkuegler
Copy link
Member

dkuegler commented Mar 3, 2025

On first sight, this seems to be an error outside of the FastSurfer toolbox. Maybe @m-reuter has seen something like this before.

Also, if you run run_fastsurfer.sh --version all you are supposed to get a lot more output...

Can you run python -m pip list --verbose --no-cache-dir from where you otherwise would get these errors and report the results?

Thanks!

@gregordoerl
Copy link
Author

Thanks for the quick response! Maybe installation did not go as well even though I didn't notice any errors..?

here is what i get from running python3.10 -m pip list --verbose --no-cache-dir

Package Version Location Installer


absl-py 2.1.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
asttokens 2.0.5 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
certifi 2025.1.31 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
charset-normalizer 3.4.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
click 8.1.8 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
contourpy 1.3.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
cycler 0.12.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
decorator 5.1.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
Deprecated 1.2.18 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
exceptiongroup 1.2.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
executing 0.8.3 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
filelock 3.17.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
fonttools 4.56.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
fsspec 2025.2.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
grpcio 1.70.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
h5py 3.13.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
humanize 4.12.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
idna 3.10 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
imageio 2.37.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
importlib_resources 6.5.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
ipython 8.30.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
jedi 0.19.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
Jinja2 3.1.5 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
joblib 1.4.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
kiwisolver 1.4.8 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
lapy 1.2.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
lazy_loader 0.4 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
Markdown 3.7 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
markdown-it-py 3.0.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
MarkupSafe 3.0.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
matplotlib 3.10.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
matplotlib-inline 0.1.6 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
mdurl 0.1.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
mpmath 1.3.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
narwhals 1.29.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
networkx 3.4.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
nibabel 5.3.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
numpy 1.26.4 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
packaging 24.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
pandas 2.2.3 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
parso 0.8.4 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
pexpect 4.8.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
pillow 11.1.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
pip 25.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
plotly 6.0.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
prompt-toolkit 3.0.43 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
protobuf 5.29.3 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
psutil 7.0.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
ptyprocess 0.7.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
pure-eval 0.2.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
Pygments 2.15.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
pyparsing 3.2.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
python-dateutil 2.9.0.post0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
pytz 2025.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
PyYAML 6.0.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
requests 2.32.3 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
rich 13.9.4 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
scikit-image 0.25.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
scikit-learn 1.6.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
scipy 1.15.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
setuptools 75.8.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages
shellingham 1.5.4 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
SimpleITK 2.4.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
six 1.16.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
stack-data 0.2.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
sympy 1.13.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
tensorboard 2.19.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
tensorboard-data-server 0.7.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
threadpoolctl 3.5.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
tifffile 2025.2.18 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
torch 2.6.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
torchio 0.20.4 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
torchvision 0.21.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
tqdm 4.67.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
traitlets 5.14.3 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
typer 0.15.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
typing_extensions 4.12.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
tzdata 2025.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
urllib3 2.3.0 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
wcwidth 0.2.5 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages conda
Werkzeug 3.1.3 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
wheel 0.45.1 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages
wrapt 1.17.2 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip
yacs 0.1.8 /Users/gregor/anaconda3/envs/FastSurfer/lib/python3.10/site-packages pip

@dkuegler dkuegler added the needs-repro Still needs to be reproduced label Mar 4, 2025
@dkuegler
Copy link
Member

dkuegler commented Mar 4, 2025

For reference

Chiming in here to echo @atiorh apple/ml-stable-diffusion#349 (comment), from what I can gather the leaked semaphore log is actually just an artifact of the process being killed, the multiprocessing library can output that log message when killed unexpectedly. The root of the issue is that the process is getting killed in the first place, which is generally due to out of memory, but could be from other issues.

Like people here have mentioned, simplest solution is to make sure your mac has enough RAM (ideally 2-3x the model size), make sure to disable --check-output-correctness, as well as freeing up as much memory as possible during the conversion process by closing any running apps to prevent swap usage. A longer-term fix would be a fine-grained review of memory usage throughout the conversion script to free up memory that is no longer needed and eliminate unnecessary model copies.

Ursprünglich gepostet von @ZachNagengast in apple/ml-stable-diffusion#8 (comment)

@m-reuter
Copy link
Member

m-reuter commented Mar 5, 2025

Hi, I just checked my system (where it is working). I have not tried yet on macOS 15.3 (only 15.2). Also I have older versions of most dependencies , for example I am running torch 2.4.
If I find the time, I will try to replicate your dependency versions and then also upgrade macOS at some point.

@m-reuter
Copy link
Member

m-reuter commented Mar 5, 2025

I upgraded requirements (still according to requirements.mac.txt) using
pip install --upgrade -r requirements.mac.txt

absl-py                 2.1.0
certifi                 2024.7.4
charset-normalizer      3.3.2
click                   8.1.7
contourpy               1.2.1
cycler                  0.12.1
Deprecated              1.2.14
filelock                3.15.4
fonttools               4.53.1
fsspec                  2024.6.1
grpcio                  1.66.0
h5py                    3.13.0
humanize                4.10.0
idna                    3.8
imageio                 2.35.1
importlib_resources     6.5.2
Jinja2                  3.1.4
joblib                  1.4.2
kiwisolver              1.4.5
lapy                    1.2.0
lazy_loader             0.4
Markdown                3.7
markdown-it-py          3.0.0
MarkupSafe              2.1.5
matplotlib              3.10.1
mdurl                   0.1.2
mpmath                  1.3.0
networkx                3.3
nibabel                 5.3.2
numpy                   1.26.4
packaging               24.1
pandas                  2.2.3
pillow                  10.4.0
pip                     25.0.1
plotly                  5.23.0
protobuf                5.27.3
psutil                  6.0.0
Pygments                2.18.0
pyparsing               3.1.2
python-dateutil         2.9.0.post0
pytz                    2024.1
PyYAML                  6.0.2
requests                2.32.3
rich                    13.7.1
scikit-image            0.25.2
scikit-learn            1.6.1
scipy                   1.15.2
setuptools              65.5.0
shellingham             1.5.4
SimpleITK               2.4.1
six                     1.16.0
sympy                   1.13.1
tenacity                9.0.0
tensorboard             2.19.0
tensorboard-data-server 0.7.2
threadpoolctl           3.5.0
tifffile                2024.8.10
torch                   2.6.0
torchio                 0.20.4
torchvision             0.21.0
tqdm                    4.67.1
typer                   0.12.4
typing_extensions       4.12.2
tzdata                  2024.1
urllib3                 2.2.2
Werkzeug                3.0.4
wrapt                   1.16.0
yacs                    0.1.8

which are now similar to what you have, but still different. Maybe try installing with pip and using the requirements file.
It works for me on Macos 15.2. I am getting some issues due to the older bash 3:

time command failing, not using time...
stat: illegal option -- c
usage: stat [-FLnq] [-f format | -l | -r | -s | -x] [-t timefmt] [file ...]

and

./FastSurfer/run_fastsurfer.sh: line 841: mapfile: command not found

but it continues and segments the images (did not try the surface stream yet).

@gregordoerl
Copy link
Author

Thanks again for the input. I tried several things:

I got it to run a bit further when using a different T1 (larger voxels, different to my 0.8mm isotropic) and using only a single measurement with the following command: ./run_fastsurfer.sh --t1 $dataDir2/s002_T1.nii --sid s002 --sd $fastSurferDir --3T --device mps

It ran part of the recon-surf until I got the following error:

================== Creating orig and rawavg from input =========================

python3.10 -s /Users/gregor/FastSurfer/FastSurferCNN/data_loader/conform.py -i /Users/gregor/Documents/_Projects/2023_Hypothalameth/01_data/testFastSurfer/s001/FastSurfer/s002/mri/orig.mgz --check_only --vox_size min --verbose --log /Users/gregor/Documents/_Projects/2023_Hypothalameth/01_data/testFastSurfer/s001/FastSurfer/s002/scripts/conform.log
Reading input: /Users/gregor/Documents/_Projects/2023_Hypothalameth/01_data/testFastSurfer/s001/FastSurfer/s002/mri/orig.mgz ...
[INFO: conform.py: 826]: A min-conformedconformed image must satisfy the following criteria:
[INFO: conform.py: 828]: - Number of Dimensions 3 : True
[INFO: conform.py: 828]: - Dimensions 295x295x295 : True
[INFO: conform.py: 828]: - Voxel Size 0.85x0.85x0.85 : True
[INFO: conform.py: 828]: - Orientation LIA : True
[INFO: conform.py: 828]: - Dtype uint8 : True
Input /Users/gregor/Documents/_Projects/2023_Hypothalameth/01_data/testFastSurfer/s001/FastSurfer/s002/mri/orig.mgz is already conformed! Exiting.

grep: invalid option -- P
usage: grep [-abcdDEFGHhIiJLlMmnOopqRSsUVvwXxZz] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
python3.10 -s /Users/gregor/FastSurfer/FastSurferCNN/data_loader/conform.py -i /Users/gregor/Documents/_Projects/2023_Hypothalameth/01_data/testFastSurfer/s001/FastSurfer/s002/mri/aparc.DKTatlas+aseg.deep.mgz --check_only --vox_size --dtype any --verbose
usage:
Script to conform an MRI brain image to UCHAR, RAS orientation,
and 1mm or minimal isotropic voxels

USAGE:
conform.py -i -o
OR
conform.py -i --check_only
Dependencies:
Python 3.8+
Numpy
https://www.numpy.org
Nibabel to read and write FreeSurfer data
https://nipy.org/nibabel/
Original Author: Martin Reuter
Date: Jul-09-2019
conform.py: error: argument --vox_size: expected one argument

Here is the log.
recon-surf.log

I tried FastSurfer on a single measurement of my other high-resolution T1, but it exited with the previous error of the leaked semaphore object.

I also wanted to note, that my default shell is not bash, but zsh (because Apple). Might that be a problem?

@m-reuter
Copy link
Member

m-reuter commented Mar 6, 2025

So basically what you changed is:

  • lower image resolution (1mm instead of .8mm isotropic)? That would indicate that memory was an issue.
  • single time point instead of longitudinal stream, is always good to test that first. Once it works here, it should also work in the longitudinal stream.

The error you get is strange as conform.py is around for a while and the it is unclear why the flag --vox_size does not have an argument. Are you using the current dev branch or did you checkout stable?

I will try to run the surface pipeline to see if I can replicate that on my Mac.

@dkuegler
Copy link
Member

dkuegler commented Mar 6, 2025

This seems to be an error that is supported in Ubuntu 22.04's grep, but not in macOS's grep (the -P option).

The error you are getting is then because recon-surf.sh cannot extract the voxel size from the conform output.
I assume this incompatibilities arises from #652.

@dkuegler
Copy link
Member

dkuegler commented Mar 6, 2025

@gregordoerl maybe you can report you grep version grep --version.

@m-reuter
Copy link
Member

m-reuter commented Mar 6, 2025

on my Mac it is
grep (BSD grep, GNU compatible) 2.6.0-FreeBSD
probably we need to work around the -P option issue. And we need to test at least one full run on the Mac before future releases. Hopefully via CI.

@m-reuter m-reuter added needs-fix A reproducable bug that needs to be fixed and removed needs-repro Still needs to be reproduced labels Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-fix A reproducable bug that needs to be fixed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants