Segmentation Fault When Using XGBoost with PyTorch (macOS)ΒΆ

ProblemΒΆ

Import order conflict on macOS: Importing xgboost before torch causes a segmentation fault when calling torch.tensor() on numpy arrays.

This fails (segfaults):

import xgboost as xgb  # XGBoost first
import torch
import numpy as np

t = torch.tensor(np.random.randn(256, 784).astype(np.float32))  # Segfault

This works:

import torch  # PyTorch first
import xgboost as xgb
import numpy as np

t = torch.tensor(np.random.randn(256, 784).astype(np.float32))  # Works

SolutionsΒΆ

Option 1: Import PyTorch first

Ensure torch is imported before xgboost in your code:

import torch  # FIRST
import xgboost as xgb  # After PyTorch

Option 2: Use separate environments

When working with multiple ML frameworks (PyTorch, TensorFlow, XGBoost, etc.) on macOS, consider using separate virtual environments for each framework to avoid import order conflicts.

EnvironmentΒΆ

  • macOS (Apple Silicon / arm64)

  • PyTorch 2.9.1

  • XGBoost 3.1.2

  • Python 3.12

Important NoteΒΆ

macOS users should be particularly careful when installing multiple ML frameworks in the same environment. Import order conflicts between frameworks can cause subtle and hard-to-debug issues like segmentation faults or deadlocks.

StatusΒΆ

Open Issue - Upstream PyTorch bug.

Tracked at: pytorch/pytorch#171323