突破Colab束缚:轻松在服务器上用Unsloth微调Llama 3.1大模型!

引言

你是否还在为在colab微调大模型训练时GPU资源受限和空间不足,或者模型文件过大导出费时又费力而发愁?那么这篇文章将教你如何在自己的服务器上部署,用unsloth 去微调训练llama3.1大模型 ,打破colab的局限性!

1. 创建unsloth所需的conda虚拟环境

  • 这里以要求python3.10pytorch2.3cuda12.1为例。

1.1 创建python3.10版本的conda虚拟环境

conda create -n unsloth  python=3.10
1.1.1 补充:conda的一些常用命令
环境管理(最常用)
  1. 创建新环境

    conda create --name env_name python=3.10
    
  2. 激活环境

    conda activate env_name
    
  3. 停用当前环境

    conda deactivate
    
  4. 列出所有环境

    conda env list
    
  5. 删除环境

    conda remove --name env_name --all
    
包管理
  1. 安装包

    conda install package_name
    
  2. 更新包

    conda update package_name
    
  3. 卸载包

    conda remove package_name
    
  4. 列出已安装的包

    conda list
    
  5. 查找包

    conda search package_name
    
其他命令
  1. 更新 conda 本身

    conda update conda
    
  2. 导出环境配置

    conda env export > environment.yml
    
  3. 从环境配置文件创建环境

    conda env create -f environment.yml
    
  4. 清理未使用的包和缓存

    conda clean --all
    

1.2 激活创建的虚拟环境,并安装cuda12.1版本的pytorch2.3

  • 激活环境
    conda activate unsloth
    
  • 进入pytorch官网,找寻对应的版本
    在这里插入图片描述
    在这里插入图片描述
  • 命令如下
conda install pytorch==2.3.0 torchvision==0.18.0 torchaudio==2.3.0 pytorch-cuda=12.1 -c pytorch -c nvidia

注意:环境中如果没有TensorFlow,记得安装因为pytorchtensorflow具有关联性

2. 安装unsloth及其所需的包

pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
pip install --no-deps "xformers==0.0.27" trl peft accelerate bitsandbytes

3. 执行训练所需的相关代码

  • 执行训练代码之前,需要先执行这三条代码。

python程序默认使用的是0号GPU,所以可以先参考GPU的情况(nvidia-smi),然后选择要用的GPU

export CUDA_VISIBLE_DEVICES="4"
export NCCL_P2P_DISABLE="1"
export NCCL_IB_DISABLE="1"
  • 补充:如果没开科学上网,需要打开,命令如下(没sudo权限,找管理员
sudo systemctl start v2raya.service
  • 训练所需的代码比如叫run.py
    • 运行run.py
      • python run.py
# 帅小柏

import os
import torch
from unsloth import FastLanguageModel, is_bfloat16_supported
from transformers import TextStreamer, TrainingArguments
from datasets import load_dataset
from trl import SFTTrainer

# 1. Configuration
max_seq_length = 2048
dtype = None
load_in_4bit = True 
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

### Instruction:
{}

### Input:
{}

### Response:
{}"""

instruction =  "以下是一首唐詩的第一句話,請用你的知識判斷並完成整首詩。" # instruction
        
input = "长风破浪会有时,直挂云帆济沧海。" 
# huggingface_model_name = "poem"

# 2. Before Training
model, tokenizer = FastLanguageModel.from_pretrained(
    # model_name = "unsloth/Meta-Llama-3.1-8B",
    # model_name = "unsloth/llama-3-8b-bnb-4bit",
     model_name = "unsloth/Meta-Llama-3.1-8B-Instruct-bnb-4bit",
    max_seq_length = max_seq_length,
    dtype = dtype,
    load_in_4bit = load_in_4bit,
    # token = os.getenv("HF_TOKEN")
)

FastLanguageModel.for_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅小柏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值