Issues with cmdstanpy on Apple Silicon and newest MacOS

Does anybody have problems with getting cmdstanpy to work with macOS 12.3 and Apple Silicon?

Details are here Issue at GitHub but generally it stopped compiling when building cmdstan.

I’ve exhausted all ideas except reinstalling system and colleague has similar issue on his machine.
I thought that I’ll share it also here because not everyone reads issues on GitHub.

Looking at the last error posted and its reference to x86_64:

ld: unsupported tapi file type '!tapi-tbd' in YAML file '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd' for architecture x86_64

This appears to be due to the Rosetta translation layer, since your system is attempting to use x86 components when your architecture is arm64. I would guess that python or one of its components is running through Rosetta, and this is then influencing how cmdstan is built.

Can you install cmdstan manually and set the path within cmdstanpy?

Rosetta thing is probably correct, but cmdstanpy calls the terminal (with subprocess) and should not have an influence how things are running.

Edit. Unless that terminal which is called is also under the emulation state? Is there a way to test this?

If the process calling the terminal process is run with Rosetta, the terminal process will also run with Rosetta by default. At least that is how it works with R and external processes. I would think ita a similar story for Python.

1 Like

Blockquote
Can you install cmdstan manually and set the path within cmdstanpy ?

I can install cmdstan, and it works individually (cmdstanr also works) but when linked to cmdstanpy it returns PCH error.

Can you try to add the following code before import cmdstanpy

import subprocess
subprocess_popen = subprocess.Popen


def popen_wrap(*args, **kwargs):
    args = list(args)
    print(type(args))
    arm_arch = ["env", "/usr/bin/arch", "-arm", "/bin/zsh", "--login"]
    args[0] = arm_arch + list(args[0])
    return subprocess_popen(*args, **kwargs)


subprocess.Popen = popen_wrap

Ok so some varying, yet unsuccessful results.

Your code followed by:

from cmdstanpy import install_cmdstan
install_cmdstan(verbose=True)

resulted in

Installing CmdStan version: 2.29.1
Install directory: /Users/jerzybaranowski/.cmdstan
Downloading CmdStan version 2.29.1
Download successful, file: /var/folders/96/_4tl8xsx4tx9rj0ykk82715h0000gn/T/tmpp0c08kl2
Extracting distribution
Unpacked download as cmdstan-2.29.1
Building version cmdstan-2.29.1, may take several minutes, depending on your system.
<class 'list'>
arch: Unknown architecture: arm
Command "make build" failed
Command ['make', 'build', '-j1']
	error during processing Operation not permitted

so i’ve modified it into

    arm_arch = ["env", "/usr/bin/arch", "-arm64", "/bin/zsh", "--login"]

and that resulted in:

Installing CmdStan version: 2.29.1
Install directory: /Users/jerzybaranowski/.cmdstan
Downloading CmdStan version 2.29.1
Download successful, file: /var/folders/96/_4tl8xsx4tx9rj0ykk82715h0000gn/T/tmp220sad48
Extracting distribution
Unpacked download as cmdstan-2.29.1
Building version cmdstan-2.29.1, may take several minutes, depending on your system.
<class 'list'>
/bin/zsh: can't open input file: make
Command "make build" failed
Command ['make', 'build', '-j1']
	program not found Unknown error: 127

So there is a progress, as different error messages are happening :)

Looking at the Github issue, the code you’re running is:

from cmdstanpy import cmdstan_path, set_cmdstan_path
import os
oldpath = cmdstan_path()
set_cmdstan_path(os.path.join('/Users','jerzybaranowski','Github','cmdstan'))
newpath = cmdstan_path()
from cmdstanpy import rebuild_cmdstan
rebuild_cmdstan()

If you’ve already built cmdstan externally through the terminal, then you don’t need to run rebuild_cmdstan(). Are you able to compile and sample models if you build cmdstan externally and then add the path without calling rebuild_cmdstan()?

The problem is that, separately built cmdstan does not work with cmdstanpy. It returns the PCH errors. Thats why I tried rebuilding.

The PCH issue is definitely due to the use of two different compilers in this case. I would try running conda uninstall clang which should force cmdstanpy to use the system clang and seeing what happens

Unfortunately this is not possible. Uninstalling clang removes cmdstanpy

If you’re not using the conda-vendored version of cmdstan itself, there’s no reason to prefer the conda version of cmdstanpy over a pip-installed one, so if you wanted you could uninstall clang/cmdstan/cmdstanpy from conda and then just use pip and the normal install_cmdstan and see if that resolves this

Unfortunately it still looks for x86_64 version

/bin/sh: x86_64-apple-darwin13.4.0-clang++: command not found

My guess is you need to deactivate and reactivate the conda environment

Ok, so now it got weird. Restarting kernel didnt help, so I did a new clean environment.
Installed cmdsanpy via pip.
Ran

from cmdstanpy import install_cmdstan
install_cmdstan(verbose=True)

And it built stan, but then:

--- CmdStan v2.29.1 built ---
Test model compilation
clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes      -I stan/lib/stan_math/lib/tbb_2020.3/include    -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.75.0 -I stan/lib/stan_math/lib/sundials_6.0.0/include -I stan/lib/stan_math/lib/sundials_6.0.0/src/sundials    -DBOOST_DISABLE_ASSERTS          -c -MT stan/src/stan/model/model_header.hpp.gch -MT stan/src/stan/model/model_header.d -MM -E -MG -MP -MF stan/src/stan/model/model_header.d stan/src/stan/model/model_header.hpp
clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes      -I stan/lib/stan_math/lib/tbb_2020.3/include    -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.75.0 -I stan/lib/stan_math/lib/sundials_6.0.0/include -I stan/lib/stan_math/lib/sundials_6.0.0/src/sundials    -DBOOST_DISABLE_ASSERTS          -c -MT src/cmdstan/main.o -MM -E -MG -MP -MF src/cmdstan/main.d src/cmdstan/main.cpp

--- Translating Stan model to C++ code ---
bin/stanc  --o=examples/bernoulli/bernoulli.hpp examples/bernoulli/bernoulli.stan

--- Compiling, linking C++ code ---
clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes      -I stan/lib/stan_math/lib/tbb_2020.3/include    -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.75.0 -I stan/lib/stan_math/lib/sundials_6.0.0/include -I stan/lib/stan_math/lib/sundials_6.0.0/src/sundials    -DBOOST_DISABLE_ASSERTS          -c -include-pch stan/src/stan/model/model_header.hpp.gch -x c++ -o examples/bernoulli/bernoulli.o examples/bernoulli/bernoulli.hpp
error: PCH file uses an older PCH format that is no longer supported
1 error generated.
make: *** [examples/bernoulli/bernoulli] Error 1
Command "make clean-all" failed
Command ['make', 'examples/bernoulli/bernoulli']
	error during processing No such file or directory

So kinda coming back to square 1.

You could try disabling the precompiled header functionality by adding PRECOMPILED_HEADERS=false to $CMDSTAN/make/local

Sorry that this is such a hassle. I don’t have an M1 mac myself to do this testing

Ok, just for kicks I ran

from cmdstanpy import rebuild_cmdstan
rebuild_cmdstan()

and it worked.
And now it works

Huh, odd. Well, I’m glad it works!

I’ve just requested osx-arm64 support for cmdstan on conda, so hopefully this won’t be an issue in the future!

Hopefuly. I plan to do some testing if this state persists or not :)

I wrote a brief summary of my workaround on github:

3 Likes