如何将adapters.npz融合到Mistral 7B?融合时遇多类错误求助
大模型LoRA微调后融合适配器遇到的问题
已完成的步骤:LoRA适配器测试成功
首次进行大模型微调尝试,已成功创建并测试adapters.npz文件,测试代码及结果如下:
python3 lora.py \ --model mistralai/Mistral-7B-Instruct-v0.2 \ --max-tokens 8 \ --adapter-file /Users/sambrand/mlx-examples-main/lora/adapters.npz \ --prompt "What is the sentiment of this movie review? Respond with a sentiment score between 1 and 10, where 1 is hate and 10 is love. The movie review is enclosed in brackets. This is the movie review: [This apology for a movie is about absolutely nothing! Rachel Griffiths must have needed the money.]"
输出结果:
The sentiment score is: 1
融合适配器时遇到的两类错误
需将adapters.npz融合回Mistral-7B-Instruct-v0.2模型以实现本地使用,但执行融合命令时出现以下两类错误:
错误1:找不到mlx_lm模块
执行命令:
python3 -m mlx_lm.fuse \ --model mistralai/Mistral-7B-Instruct-v0.2 \ --adapter-file /Users/sambrand/mlx-examples-main/lora/adapters.npz \
返回错误:
/Library/Developer/CommandLineTools/usr/bin/python3: Error while finding module specification for 'mlx_lm.fuse' (ModuleNotFoundError: No module named 'mlx_lm')
错误2:fuse.py参数识别异常
尝试执行命令:
python3 fuse.py \ --model mistralai/Mistral-7B-Instruct-v0.2 \ --adapter-file /Users/sambrand/mlx-examples-main/lora/adapters.npz \
返回错误:
usage: fuse.py [-h] [--model MODEL] [--save-path SAVE_PATH] [--adapter-file ADAPTER_FILE] [--hf-path HF_PATH] [--upload-name UPLOAD_NAME] [-d] fuse.py: error: unrecognized arguments: zsh: command not found: --adapter-file
当前困惑点
- 遵循Andy Peatling的教程操作,此前步骤均正常,但不确定本地待融合的模型位置
- 不确定是否已完成HF认证,不清楚如何通过终端下载HF模型
- 此前通过Ollama命令
ollama run mistral下载模型,推测模型存储在.ollama/models/blobs目录下的sha开头文件中
解决步骤
1. 修复mlx_lm模块缺失问题
确保安装最新版mlx-lm,如果使用虚拟环境需先激活:
pip install --upgrade mlx-lm
2. 修复fuse.py参数错误
错误源于命令格式问题+缺少必填参数,将命令改为单行并添加--save-path(指定融合后模型的保存路径):
python3 fuse.py --model mistralai/Mistral-7B-Instruct-v0.2 --adapter-file /Users/sambrand/mlx-examples-main/lora/adapters.npz --save-path ./merged-mistral-model
3. 处理本地模型位置与HF下载问题
Ollama下载的模型为封装格式,无法直接被MLX工具调用,需单独下载HF格式的模型:
- 完成HF认证:执行以下命令,按提示输入HF个人设置中生成的access token:
huggingface-cli login
- 安装git-lfs(用于大文件下载):
brew install git-lfs git lfs install
- 下载HF格式模型:
git clone https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2
或用mlx-lm下载命令:
python -m mlx_lm.download --model mistralai/Mistral-7B-Instruct-v0.2
下载完成后,模型会存放在本地mistralai/Mistral-7B-Instruct-v0.2目录中。
4. 正确执行融合命令
使用本地模型路径执行融合,以mlx_lm.fuse为例:
python3 -m mlx_lm.fuse --model ./mistralai/Mistral-7B-Instruct-v0.2 --adapter-file /Users/sambrand/mlx-examples-main/lora/adapters.npz --save-path ./merged-mistral-model
内容的提问来源于stack exchange,提问作者samthebrand
相关产品推荐
相关产品推荐

