pythonmacospymcpytensor

Pytensor compilation failed during linking stage on macOS


When trying to run a simple PyMC example on ARM macOS 15.4 using a fresh conda-forge conda-environment the run fails with a compilation error: pytensor.link.c.exceptions.CompileError: Compilation failed

import pymc as pm

with pm.Model() as model:
    alpha = pm.Normal('alpha', mu=0, sigma=1)
    pm.Normal('Est', mu=alpha, sigma=1, observed=0)
    pm.sample()

Relevant logs:

pytensor.link.c.exceptions.CompileError: Compilation failed (return status=1):
/opt/homebrew/Cellar/micromamba/2.0.8/envs/chyby/bin/clang++ -dynamiclib -g -O3 -fno-math-errno -Wno-unused-label -Wno-unused-variable -Wno-write-strings -Wno-c++11-narrowing -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION -fPIC -undefined dynamic_lookup -ld64 -I/opt/homebrew/Cellar/micromamba/2.0.8/envs/chyby/lib/python3.13/site-packages/numpy/_core/include -I/opt/homebrew/Cellar/micromamba/2.0.8/envs/chyby/include/python3.13 -I/opt/homebrew/Cellar/micromamba/2.0.8/envs/chyby/lib/python3.13/site-packages/pytensor/link/c/c_code -L/opt/homebrew/Cellar/micromamba/2.0.8/envs/chyby/lib -fvisibility=hidden -o /Users/cr/.pytensor/compiledir_macOS-15.4-arm64-arm-64bit-Mach-O-arm-3.13.2-64/tmphttorckn/m25516502502c211c050fc4b6a6d2e1108dee165ceb9a6715069ad05cf2e6f5c3.so /Users/cr/.pytensor/compiledir_macOS-15.4-arm64-arm-64bit-Mach-O-arm-3.13.2-64/tmphttorckn/mod.cpp
ld: -lto_library library filename must be 'libLTO.dylib'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

Apply node that caused the error: Subtensor{i}([1 3], -1)
Toposort index: 0
Inputs types: [TensorType(int64, shape=(2,)), ScalarType(int64)]

Backtrace when the node is created (use PyTensor flag traceback__limit=N to make it longer):
  File "/Users/cr/code/autotracer_chyby/bayesian/latent.py", line 149, in <module>
    trace = pm.sample(1000, tune=2000, chains=2, cores=1, target_accept=0.95)
  File "/opt/homebrew/Cellar/micromamba/2.0.8/envs/chyby/lib/python3.13/site-packages/pymc/sampling/mcmc.py", line 789, in sample
    provided_steps, selected_steps = assign_step_methods(model, step, methods=pm.STEP_METHODS)
  File "/opt/homebrew/Cellar/micromamba/2.0.8/envs/chyby/lib/python3.13/site-packages/pymc/sampling/mcmc.py", line 261, in assign_step_methods
    selected = max(
  File "/opt/homebrew/Cellar/micromamba/2.0.8/envs/chyby/lib/python3.13/site-packages/pymc/sampling/mcmc.py", line 263, in <lambda>
    key=lambda method, var=rv_var, has_gradient=has_gradient: method._competence(  # type: ignore[misc]
  File "/opt/homebrew/Cellar/micromamba/2.0.8/envs/chyby/lib/python3.13/site-packages/pymc/step_methods/compound.py", line 219, in _competence
    competences.append(cls.competence(var))
  File "/opt/homebrew/Cellar/micromamba/2.0.8/envs/chyby/lib/python3.13/site-packages/pymc/step_methods/metropolis.py", line 533, in competence
    k = var.owner.inputs[-1].shape[-1].eval()

HINT: Use a linker other than the C linker to print the inputs' shapes and strides.
HINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.

How do I fix this?


Solution

  • This appears to be a bug in macOS 15.4. There are at least 2 solutions

    Option 1: Add config line in each file

    Add pytensor.config.cxx = '/usr/bin/clang++' at the top of the file after imports, e.g.:

    import pytensor
    pytensor.config.cxx = "/usr/bin/clang++"
    

    Option 2: Add a single global config file

    Creating a ~/.pytensorrc file that contains:

    [global]
    cxx=/usr/bin/clang++ 
    

    There might be more solutions in this forum post: https://discourse.pymc.io/t/pytensor-fails-to-compile-model-after-upgrading-to-mac-os-15-4/16796/4