租界GPU平台 趋动云虚拟服务器中新建 Conda 环境 持久化配置环境
一些GPU租界平台,虚拟服务器,重启后 配置环境会重置.程序包会丢失
在趋动云虚拟服务器中新建 Conda 环境并基于 base 环境复刻(指定安装目录到 /gemini/code/python311_unsloth),是解决 unsloth 安装环境隔离、路径问题的最优方案。以下是完整、可直接执行的步骤,兼顾环境复刻、路径指定和 unsloth 安装验证:
一、核心前提(趋动云环境适配)
- 确保 Conda 已初始化(趋动云默认 miniconda 已配置,若未初始化执行
conda init bash并重启终端); /gemini/code/目录有读写权限(root 用户默认有权限,普通用户需执行chmod -R 755 /gemini/code)。
二、完整操作步骤
步骤 1:新建 Conda 环境(指定 Python 版本+安装目录)
bash
# 1. 创建新环境,指定安装目录+Python 3.11(和 base 环境一致)
conda create --prefix /gemini/code/python311_unsloth python=3.11 -y
# 2. 激活新环境(关键:后续所有操作在新环境中执行)
conda activate /gemini/code/python311_unsloth
# 验证环境激活:终端前缀会变成 (/{gemini/code/python311_unsloth})
echo $CONDA_PREFIX # 应输出 /gemini/code/python311_unsloth
步骤 2:复刻 base 环境的包(可选,按需复刻)
如果需要完全复用 base 环境的包(避免重复下载),分两种方式:
方式 A:轻量复刻(仅复刻已安装包列表)
bash
# 1. 导出 base 环境的包列表
conda list -n base --export > /gemini/code/base_packages.txt
# 2. 在新环境中安装这些包(自动适配 Python 3.11)
conda install --prefix /gemini/code/python311_unsloth --file /gemini/code/base_packages.txt -y
# 3. 补充 pip 包(base 环境的 pip 包)
pip freeze > /gemini/code/base_pip_packages.txt
pip install --no-cache-dir -r /gemini/code/base_pip_packages.txt
方式 B:快速克隆(直接复制 base 环境,推荐)
bash
# 直接克隆 base 环境到指定目录(最快,保留所有包)
conda create --prefix /gemini/code/python311_unsloth --clone base -y
步骤 3:在新环境中安装 unsloth(核心)
bash
# 1. 确保激活新环境(若已激活可跳过)
conda activate /gemini/code/python311_unsloth
# 2. 升级 pip(避免旧版本问题)
pip install --upgrade pip
# 3. 安装 unsloth(禁用缓存,确保下载完整包)
pip install --no-cache-dir unsloth
# 【可选】安装旧版(保留 FastLanguageModel)
# pip install --no-cache-dir unsloth==2024.5
步骤 4:验证新环境+unsloth 安装
bash
# 1. 验证环境路径
which python # 应输出 /gemini/code/python311_unsloth/bin/python
which pip # 应输出 /gemini/code/python311_unsloth/bin/pip
# 2. 验证 unsloth 核心类
python -c "
import unsloth
# 新版验证
print('LanguageModel:', 'LanguageModel' in dir(unsloth))
# 旧版验证(若装2024.5)
# print('FastLanguageModel:', 'FastLanguageModel' in dir(unsloth))
"
# 3. 验证依赖版本
python -c "
import xformers, bitsandbytes, triton
print('xformers:', xformers.__version__)
print('bitsandbytes:', bitsandbytes.__version__)
print('triton:', triton.__version__)
"
✅ 预期正常输出:
LanguageModel: True
xformers: 0.0.35
bitsandbytes: 0.49.2
triton: 3.0.0
三、关键注意事项(趋动云环境适配)
-
环境激活简化 :后续每次使用时,直接执行:
bashconda activate /gemini/code/python311_unsloth -
避免权限问题 :全程用 root 用户执行(趋动云默认 root),无需加
sudo(否则可能导致 Conda 路径错乱); -
磁盘空间 :新环境+unsloth 依赖约需 5-8GB 空间,趋动云
/gemini/code/目录默认有足够空间; -
GPU 适配:趋动云虚拟服务器已预装 CUDA,unsloth 会自动识别,无需额外配置。
四、问题排查(若新环境安装仍失败)
1. Conda 创建环境失败
bash
# 清理 Conda 缓存
conda clean -y --all
# 重新创建环境
conda create --prefix /gemini/code/python311_unsloth python=3.11 -y
2. unsloth 仍无核心类
bash
# 查看新环境中 unsloth 安装目录
ls -l $CONDA_PREFIX/lib/python3.11/site-packages/unsloth/
# 正常应看到:__init__.py、language_model.py、chat_templates/ 等文件
# 若为空,重新安装:
pip uninstall -y unsloth
pip install --no-cache-dir --force-reinstall unsloth
总结
- 核心操作 :通过
conda create --prefix指定安装目录,克隆 base 环境实现依赖复用,在新环境中纯净安装 unsloth; - 验证标准 :新环境路径正确,能导入
LanguageModel/FastLanguageModel,依赖版本满足要求; - 优势:环境隔离避免 base 环境依赖冲突,指定目录便于趋动云的文件管理和持久化。
完成后,你就可以在这个独立环境中正常使用 unsloth 进行大模型微调,无需担心和 base 环境的依赖冲突问题。