unsloth安装踩坑记录

安装前环境

  • centos 7
  • cmake 3.18.0
  • CUDA 12.4
  • pytorch 2.0.0

unsloth简介

Unsloth AI - Open Source Fine-Tuning for LLMs

安装过程

pip install unsloth

    步骤一:升级pytorch

    unsloth库会默认安装pytorch2.6.0-cpu,无法识别已安装的pytorch2.0.0-cuda124,因为unsloth不支持pytorch2.0.0版本。

    参考:Pip Install | Unsloth Documentation

    尝试升级pytorch到pytorch2.6.0-cuda124,参考pytorch官网Previous PyTorch Versions | PyTorch

    。由于官网给出的安装命令仅支持到2.5.0版本,魔改命令如下:

    pip install torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 --index-url https://download.pytorch.org/whl/cu124

     note:torchvision和torchaudio版本要查看https://download.pytorch.org/whl/cu124的支持情况(墙内推荐使用pip安装,不要用conda)。

    步骤二:解决import unsloth的bitsandbytes报错

    pytorch升级后unsloth安装“成功”,但import报错:

    >>> from unsloth import FastLanguageModel
    False

    ===================================BUG REPORT===================================
    /data/software/anaconda3/envs/py39/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:167: UserWarning: Welcome to bitsandbytes. For bug reports, please run

    python -m bitsandbytes


      warn(msg)
    ================================================================================
    /data/software/anaconda3/envs/py39/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:167: UserWarning: /data/software/anaconda3/envs/py39 did not contain ['libcudart.so', 'libcudart.so.11.0', 'libcudart.so.12.0'] as expected! Searching further paths...
      warn(msg)

    怀疑bitsandbytes库和pytorch不匹配导致。实验import bitsandbytes:

     参考博客Bitsandbytes最新版本编译安装_bitsandbytes安装-CSDN博客,可能是因为bitsandbytes0.42.0版本过旧,不支持CUDA12.4。尝试升级bitsandbytes,发现事情没那么简单。。。

    尝试1:安装whl

    pip install bitsandbytes-0.45.2-py3-none-manylinux_2_24_x86_64.whl

    报错不适用本平台,需要GLIBC2.24+。

    尝试2:源码安装

    参考https://huggingface.co/docs/bitsandbytes/main/en/installation官网源码安装指令

    git clone https://github.com/bitsandbytes-foundation/bitsandbytes.git && cd bitsandbytes/
    cmake -DCOMPUTE_BACKEND=cuda -S .
    make
    pip install -e . 

    第一步报错cmake版本低,需要>=3.22.1。 

     更新cmake,参考博客Linux下安装 CMake_linux下载cmake-CSDN博客下载编译好的cmake3.22.1。

    经过漫长地make,bitsandbytes0.45.2安装成功。但是安装成功后没有生成缺失的libbitsandbytes_cuda124.so,只有libbitsandbytes_cuda118.so,怀疑要求gcc13,陷入绝望。。。

    博主灵机一动,能不能拷一个 libbitsandbytes_cuda124.so到目录下?

    https://huggingface.co/eltorio/IDEFICS3_ROCOv2/blob/main/libbitsandbytes_cuda124.so万幸找到了。

    import bitsandbytes成功,但import unsloth失败:

    from unsloth_zoo.patching_utils import (
    ImportError: cannot import name 'patch_compiled_autograd' from 'unsloth_zoo.patching_utils' (/data/software/anaconda3/envs/py39/lib/python3.9/site-packages/unsloth_zoo/patching_utils.py)

    步骤三:解决import unsloth的unsloth_zoo报错

    依然怀疑是unsloth_zoo版本不匹配导致,github上搜到了同样的问题

    patch_compiled_autograd · Issue #16 · unslothai/unsloth-zoo · GitHub

    升级unsloth_zoo至最新:

    pip uninstall unsloth unsloth-zoo -y && pip install --upgrade --no-cache-dir --no-deps git+https://github.com/unslothai/unsloth.git git+https://github.com/unslothai/unsloth-zoo.git

    import unsloth后不再报unsloth_zoo相关错误,安装成功!

    note:如果遇到xformer版本不匹配,可参考GitHub - facebookresearch/xformers: Hackable and optimized Transformers building blocks, supporting a composable construction.重装xformer。

    加载模型

    from unsloth import FastLanguageModel
    import wandb
    
    max_seq_length = 2048 
    dtype = None 
    load_in_4bit = False
    model, tokenizer = FastLanguageModel.from_pretrained(
        model_name = "./DeepSeek-R1-Distill-Qwen-1.5B",
        max_seq_length = max_seq_length,
        dtype = dtype,
        load_in_4bit = load_in_4bit,
    )

    加载成功!

    打印模型信息

    踩坑总结

    1. 轻易不要装CUDA12.4,对于很多python库来说大部分版本只支持到CUDA11.8。
    2. 当pip install失效时,尝试拉github源码编译,参考github ReadMe。
      pip install -v -U git+xxx
      
      -v:启用详细输出(verbose),显示安装过程中的详细信息。
      -U:升级包到最新版本(如果已安装)。
      git+xxx:指定从 Git 仓库安装包,xxx 是 Git 仓库的 URL。
    3. 遇到报错使用deepseek + Google + CSDN组合拳。
    评论 2
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值