Skip to content

Importing xgboost before torch + openmp causes seg fault #11500

Open
@ikrommyd

Description

@ikrommyd

I'm on apple sillicon in a fresh environment that I just created.

uv venv --python 3.12
source .venv/bin/activate
uv pip install xgboost torch

Running the following seg faults:

import xgboost
import torch

M = torch.tensor([[2.0, 0.5],
                  [0.5, 1.0]],
                 dtype=torch.double, device="cpu")

L = torch.linalg.cholesky(M)

print("Cholesky succeeded:", L)

Switching the import order (torch before xgboost) fixes the problem. Also doing export OMP_NUM_THREADS=1 fixes the problem.

From my quick debugging session I think it's that xgboost loads libomp from homebrew first while then torch loads it's own version. I don't know if torch or xgboost should do something differently here, I'm just opening the issue to raise awareness.

Activity

ikrommyd

ikrommyd commented on Jun 5, 2025

@ikrommyd
Author
-> otool -L .venv/lib/python3.12/site-packages/xgboost/lib/libxgboost.dylib | grep libomp
        @rpath/libomp.dylib (compatibility version 5.0.0, current version 5.0.0)

-> otool -L .venv/lib/python3.12/site-packages/torch/lib/libomp.dylib | grep libomp

.venv/lib/python3.12/site-packages/torch/lib/libomp.dylib:
        /opt/homebrew/opt/libomp/lib/libomp.dylib (compatibility version 5.0.0, current version 5.0.0)

and indeed when loading

-> DYLD_PRINT_LIBRARIES=1 python3 - <<EOF 2>&1 | grep libomp.dylib
import xgboost
import torch
EOF


dyld[58427]: <AE900456-D54F-3657-A54C-21B02B390E20> /opt/homebrew/Cellar/libomp/20.1.5/lib/libomp.dylib
dyld[58427]: <2448B10B-E7F4-3091-9302-765D47BFB0B8> /Users/iason/.venv/lib/python3.12/site-packages/torch/lib/libomp.dylib

while with the opposite import order

-> DYLD_PRINT_LIBRARIES=1 python3 - <<EOF 2>&1 | grep libomp.dylib
import torch
import xgboost
EOF

dyld[59185]: <2448B10B-E7F4-3091-9302-765D47BFB0B8> /Users/iason/.venv/lib/python3.12/site-packages/torch/lib/libomp.dylib
matthewfeickert

matthewfeickert commented on Jun 5, 2025

@matthewfeickert

I think you're right about different symbols getting used, given that if everything is installed from conda-forge (on linux-64) then there's no issue

$ docker run --rm -ti ghcr.io/prefix-dev/pixi:latest 
root@31162fd0e8eb:/# pixi init example
Created /example/pixi.toml
root@31162fd0e8eb:/# cd example/
root@31162fd0e8eb:/example# pixi add xgboost pytorch
Added xgboost >=3.0.2,<4
Added pytorch >=2.7.0,<3
root@31162fd0e8eb:/example# pixi global install vim
Global environments as specified in '/root/.pixi/manifests/pixi-global.toml'
└── vim: 9.1.1397 (installed)
    └─ exposes: ex, rview, rvim, view, vim, vimdiff, vimtutor, xxd
root@31162fd0e8eb:/example# vim example.py
root@31162fd0e8eb:/example# cat example.py 
import xgboost
import torch

M = torch.tensor([[2.0, 0.5],
                  [0.5, 1.0]],
                 dtype=torch.double, device="cpu")

L = torch.linalg.cholesky(M)

print("Cholesky succeeded:", L)
root@31162fd0e8eb:/example# pixi run python example.py 
Cholesky succeeded: tensor([[1.4142, 0.0000],
        [0.3536, 0.9354]], dtype=torch.float64)
root@31162fd0e8eb:/example# find .pixi/ -type f -iname "libomp.*"
.pixi/envs/default/lib/libomp.so

Same as for osx-arm64:

% pixi init /tmp/example
✔ Created /private/tmp/example/pixi.toml
% cd /tmp/example
% pixi add xgboost pytorch
✔ Added xgboost >=3.0.2,<4
✔ Added pytorch >=2.7.0,<3
% vim example.py
% pixi run python example.py
Cholesky succeeded: tensor([[1.4142, 0.0000],
        [0.3536, 0.9354]], dtype=torch.float64)
ikrommyd

ikrommyd commented on Jun 5, 2025

@ikrommyd
Author

Yeah installing the packages from conda-forge is totally fine cause there is an openmp version coming with them from conda-forge. I'm talking about plain pypi installation here.

trivialfis

trivialfis commented on Jun 6, 2025

@trivialfis
Member

There is ongoing work on the Python wheel side for more flexible dependencies. cc @hcho3 https://github.com/wheelnext

For now, Python projects must package all dependencies into a wheel, which can create conflicts between packages. Until the wheel format starts supporting native dependencies, you should use conda/mamba to resolve these kinds of issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @matthewfeickert@trivialfis@ikrommyd

        Issue actions

          Importing xgboost before torch + openmp causes seg fault · Issue #11500 · dmlc/xgboost