cifangyiquan 10 X 10
cifangyiquan:~/blog$ source "xgboost4j 在apple m2芯片上临时使用方法.sh"

cifangyiquan:~/blog$ printenv
CREATED_DATE = 2023-10-01
UPDATED_DATE = 2024-01-24
TAGS = machine learning : xgboost : apple m2
CATEGORIES = machine-learning

cifangyiquan:~/blog$ grep -lr $TAGS post
2023-06-02 ChatGLM-6B 实践分享
2019-03-09 机器学习中的函数总结
2023-10-01 xgboost4j 在apple m2芯片上临时使用方法
2023-08-20 Transformer原理——Open AI 和 DeepMind使用的神经网络
xgboost4j 在apple m2芯片上临时使用方法

xgboost4j 在apple m2芯片上临时使用方法

今天在本地macbook部署服务时遇到一个问题。运行时会报错:org.springframework.beans.factory.BeanCreationException

1
Constructor threw exception; nested exception is java.lang.UnsatisfiedLinkError: /private/var/folders/gf/scjmv0ws6nb6nt6g56nl46bm0000gn/T/libxgboost4j1824573393192019315.dylib: dlopen(/private/var/folders/gf/scjmv0ws6nb6nt6g56nl46bm0000gn/T/libxgboost4j1824573393192019315.dylib, 0x0001): tried: '/private/var/folders/gf/scjmv0ws6nb6nt6g56nl46bm0000gn/T/libxgboost4j1824573393192019315.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/private/var/folders/gf/scjmv0ws6nb6nt6g56nl46bm0000gn/T/libxgboost4j1824573393192019315.dylib' (no such file), '/private/var/folders/gf/scjmv0ws6nb6nt6g56nl46bm0000gn/T/libxgboost4j1824573393192019315.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))
原因主要是找不到arm64版本xgbboost4j

在搜索xgboost项目的issues发现,目前版本的xgboost4j还没有对m2的稳定版本。

这里分享一个可以使用的方法,主要参考这个issue就是本地重新编译一个xgboost4j,然后安装到本地maven库中使用。具体方法如下

1. 源码编译xgboost

安装xgbboost,一定要注意“--recursive”参数,我就是第一次忘记这个参数,导致编译build出错的。

step 1 安装omp库

1
brew install libomp

step 2 下载xgboost源码

1
git clone --recursive https://github.com/dmlc/xgboost

step 3 编译xgboost

1
2
3
4
5
cd xgboost
mkdir build
cd build
cmake ..
make -j$(nproc)

step 4 生产并安装xgboost4j

1
2
3
4
cd ../jvm-packages
mvn install
# 或者跳过测试
mvn -DskipTests install

2.安装到本地maven仓库

安装本地编译好的jar到maven仓库

1
(base) [cifangyiquan@] jvm-packages %  mvn install:install-file -DgroupId=ml.dmlc -DartifactId=xgboost4j -DartifactId=2.12.0 -Dpackaging=jar -Dfile=/Users/cifangyiquan/Workspace/xgboost/jvm-packages/xgboost4j/target/xgboost4j_2.12-2.1.0-SNAPSHOT.jar

我安装到maven仓库生产的路径有问题。又按照0.9版本修改了一下路径。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(base) [cifangyiquan@] xgboost4j %  tree
.
├── 0.90
│   ├── _remote.repositories
│   ├── xgboost4j-0.90.jar
│   ├── xgboost4j-0.90.jar.sha1
│   ├── xgboost4j-0.90.pom
│   └── xgboost4j-0.90.pom.sha1
└── 2.12.0
├── 2.1.0-SNAPSHOT
│   ├── 2.12.0-2.1.0-SNAPSHOT.jar
│   ├── 2.12.0-2.1.0-SNAPSHOT.pom
│   ├── _remote.repositories
│   └── maven-metadata-local.xml
├── maven-metadata-local.xml
├── xgboost4j-2.12.0.jar -> 2.1.0-SNAPSHOT/2.12.0-2.1.0-SNAPSHOT.jar
└── xgboost4j-2.12.0.pom -> 2.1.0-SNAPSHOT/2.12.0-2.1.0-SNAPSHOT.pom

4 directories, 12 files
(base) [cifangyiquan@] xgboost4j % pwd
/Users/cifangyiquan/.m2/repository/ml/dmlc/xgboost4j
(base) [cifangyiquan@] xgboost4j %

3.修改pom.xml

修改pom.xml对应本地下载的版本号

1
2
3
4
5
<dependency>
<groupId>ml.dmlc</groupId>
<artifactId>xgboost4j</artifactId>
<version>2.12.0</version>
</dependency>

再重新build就可以正常运行了。

1
mvn clean package

长线方案

xgboost团队正在通过OpenCollective募集资金解决xgboost4j在apple M2芯片的CI问题。感兴趣的大佬可以通过https://opencollective.com/xgboost/projects/ci-for-apple-silicon?slug=ci-for-apple-silicon 参与支持。

参考