Mac基于LLaMA Factory微调模型导入Ollama踩坑记录
Mac基于LLaMA Factory微调模型导入Ollama踩坑记录
记录下完成大模型微调demo遇到的问题及最终效果
一、llama Factory安装
# 通过git下载项目git clone https://github.com/hiyouga/LLaMA-Factory.git # 进入目录cd LLaMA-Factory # 安装 pip install -e ".[torch,metrics]"遇到问题1:ERROR: Package 'llamafactory' requires a different Python: 3.9.6 not in '>=3.11.0'。然后升级到python最高版本3.14解决(不建议选最新版本,后面使用数据集遇到新的问题)
问题2:
error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try brew install xyz, where xyz is the package you are trying to install. note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification. 选择安装虚拟环境
# 安装anaconda brew install --cask anaconda # 创建虚拟环境 lora conda create -n lora python=3.14# 激活该环境 conda activate lora 再执行pip install -e ".[torch,metrics]" 依然有同样问题,添加对应参数pip install --break-system-packages -e ".[torch,metrics]"
成功安装后在目录执行llamafactory-cli webui ,会创建一个web服务,能访问http://localhost:7860/ 说明安装成功

二、模型下载&验证
在modelscope找一个较小的模型: DeepSeek-R1-Distill-Qwen-1.5B,可以通过git方式下载
# 因为存在大文件下载,需要安装 lfsgit lfs install# 下载模型git clone https://www.modelscope.cn/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B.git 下载完成后可通过页面配置模型名称、路径,然后在Chat中加载模型,进行测试

我这边在初次加载抛出异常
self.model = load_model( ~~~~~~~~~~^ self.tokenizer, model_args, finetuning_args, is_trainable=False, add_valuehead=(not self.can_generate) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) # must after fixing tokenizer to resize vocab ^ File "/Users/xxx/Documents/workspace/python/LLaMA-Factory/src/llamafactory/model/loader.py", line 178, in load_model model = load_class.from_pretrained(**init_kwargs) File "/opt/homebrew/lib/python3.14/site-packages/transformers/models/auto/auto_factory.py", line 372, in from_pretrained return model_class.from_pretrained( ~~~~~~~~~~~~~~~~~~~~~~~~~~~^ pretrained_model_name_or_path, *model_args, config=config, **hub_kwargs, **kwargs ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "/opt/homebrew/lib/python3.14/site-packages/transformers/modeling_utils.py", line 4109, in from_pretrained load_info = cls._load_pretrained_model(model, state_dict, checkpoint_files, load_config) File "/opt/homebrew/lib/python3.14/site-packages/transformers/modeling_utils.py", line 4218, in _load_pretrained_model file_pointer = safe_open(file, framework="pt", device="cpu") safetensors_rust.SafetensorError: Error while deserializing header: header too large 原因是git下载大文件不全,可以检测下模型DeepSeek-R1-Distill-Qwen-1.5B 目录下 model.safetensors的文件大小是不是和下载页面大小一致。可以通过页面上手动下载然后移动到文件夹。

加载成功后可以通过聊天测试

三、数据集准备
微调需要导入一份数据集,同样在modelscope中下载一份小的 甄嬛1M数据集,将huanhuan.json 移动到${dir}/LLaMA-Factory/data下,然后在修改该目录下的文件dataset_info.json ,在末尾中添加下面配置
{ // 原本的不动 ,"huanhuan": { "file_name": "huanhuan.json" } } 在页面Train中数据集能搜到文件名huanhuan

预览有数据则配置成功
四、训练&验证效果
直接用默认的配置点击开始

我这边之前使用python3.14遇到的问题,导入数据集函数不兼容。
File "/opt/homebrew/lib/python3.14/site-packages/dill/_dill.py", line 1217, in save_module_dict StockPickler.save_dict(pickler, obj) ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/pickle.py", line 1064, in save_dict self._batch_setitems(obj.items(), obj) ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^ TypeError: Pickler._batch_setitems() takes 2 positional arguments but 3 were given 后换成3.12版本调通微调,训练完成的日志,用了46分钟。
***** train metrics ***** epoch = 3.0 num_input_tokens_seen = 776520 total_flos = 6738458GF train_loss = 4.5287 train_runtime = 0:46:36.50 train_samples_per_second = 4.0 train_steps_per_second = 0.251 Figure saved at: saves/DeepSeek-R1-1.5B-Distill/lora/train_2026-02-12-16-14-47/training_loss.png [WARNING|2026-02-12 17:01:53] llamafactory.extras.ploting:149 >> No metric eval_loss to plot. [WARNING|2026-02-12 17:01:53] llamafactory.extras.ploting:149 >> No metric eval_accuracy to plot. 在检查点路径选择我们自己训练后输出的数据集,然后需要先卸载模型,再重新加载。最后在聊天测试“我是谁”,它的答案就不是之前的 deepseek深度求索了。

五、导出ollama
ollama可以提供API接口,给到后台服务直接调用。 这里通过Export,配置对应的导出路径,就可以导出镜像

然后通过ollama的创建指令加载刚才导出的目录后面下的Modelfile,名称test_DeepSeek 可自定义
# 安装导出的模型 ollama create test_DeepSeek -f /Users/xxx/Documents/workspace/model/Modelfile # 查看 ollama list # 运行模型, 名称默认带上:latest标签 ollama run test_DeepSeek:latest 后台就可以通过接口来调用ollama的服务