作者归档:admin

Qwen2.5-1.5B MLX LORA微调

下载模型:
export HF_ENDPOINT=https://hf-mirror.com

huggingface-cli download –resume-download Qwen/Qwen2.5-1.5B-Instruct –local-dir qwen2.5-1.5B

训练数据

{"prompt": "什么狼不吃羊?", "completion": "黄鼠狼"}
{"prompt": "什么人怕太阳?", "completion": "雪人"}
{"prompt": "什么时候做的事别人看不到?", "completion": "梦里做事"}
{"prompt": "农夫养了10头牛,为什么只有19只角?", "completion": "因为一只是犀牛"}
{"prompt": "什么东西没脚走天下?", "completion": "船"}
{"prompt": "什么桥下没水?", "completion": "立交桥"}
{"prompt": "小呆骑在大牛身上,为什么大牛不吃草?", "completion": "大牛是人"}
{"prompt": "在什么时候1加2不等于3?", "completion": "在算错了的时候。"}
{"prompt": "什么山没有石?", "completion": "冰山"}
{"prompt": "为什么天上下雨地面一点不湿?", "completion": "下的是流星雨。"}
{"prompt": "蚊子咬在什么地方你不会觉得痒?", "completion": "别人身上"}
{"prompt": "什么东西只有一只脚,却能跑遍屋子的所有角落?", "completion": "扫帚"}
{"prompt": "最好吃的饭是什么饭?", "completion": "软饭"}
{"prompt": "太阳和月亮在一起是哪一天?", "completion": "明天"}

OLLAMA modelfile 设置

https://ollama.com/library/qwen2.5:3b 可以设置为modelfile

modelfile ollama

FROM /Users/may/new_xxxxx.gguf
#导入gguf文件 下面的必须设置

TEMPLATE """{{- if .Messages }}
{{- if or .System .Tools }}<|im_start|>system
{{- if .System }}
{{ .System }}
{{- end }}
{{- if .Tools }}

# Tools

You may call one or more functions to assist with the user query.

You are provided with function signatures within <tools></tools> XML tags:
<tools>
{{- range .Tools }}
{"type": "function", "function": {{ .Function }}}
{{- end }}
</tools>

For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call>
{{- end }}<|im_end|>
{{ end }}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 -}}
{{- if eq .Role "user" }}<|im_start|>user
{{ .Content }}<|im_end|>
{{ else if eq .Role "assistant" }}<|im_start|>assistant
{{ if .Content }}{{ .Content }}
{{- else if .ToolCalls }}<tool_call>
{{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
{{ end }}</tool_call>
{{- end }}{{ if not $last }}<|im_end|>
{{ end }}
{{- else if eq .Role "tool" }}<|im_start|>user
<tool_response>
{{ .Content }}
</tool_response><|im_end|>
{{ end }}
{{- if and (ne .Role "assistant") $last }}<|im_start|>assistant
{{ end }}
{{- end }}
{{- else }}
{{- if .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}{{ if .Prompt }}<|im_start|>user
{{ .Prompt }}<|im_end|>
{{ end }}<|im_start|>assistant
{{ end }}{{ .Response }}{{ if .Response }}<|im_end|>{{ end }}"""

SYSTEM "你是一个友好的人工智能助手,回答问题时要简洁明了。"

LLAMA.CPP转换GGUF

python convert_hf_to_gguf.py /Users/may/mlx/mlx-examples/lora/qwen2.5-0.5B-test_1 --outtype bf16  --outfile ../qwen.gguf

llama.cpp 转safetensors为GGUF,导入LM studio

安装llama.cpp conda下不知道怎么一直安装失败

git clone https://github.com/ggerganov/llama.cpp.git

#或者
git clone https://bgithub.xyz/ggerganov/llama.cpp.git

cd llama.cpp



pip install -r requirements.txt  -i https://mirrors.aliyun.com/pypi/simple/
python convert_hf_to_gguf.py /Users/may/mlx/mlx-examples/lora/qwen2.5-0.5B-test_1 --outtype bf16  --outfile ../tc.gguf


/Users/may/mlx/mlx-examples/lora/qwen2.5-0.5B-test_1 mlx打包好的地址

../tc.gguf 存放的gguf地址

安装LM STUDIO 倒入文件夹可以识别GGUF模型。

可以创建API 也可以直接调用

MLX微调好的模型 加入到ollama运行

git clone https://github.com/ggerganov/llama.cpp

git clone https://bgithub.xyz/ggerganov/llama.cpp

cd llama.cpp
make -j  # 这里使用多线程编译,具体线程数根据你的CPU核心数来定

pip install -r requirements.txt -i 
https://mirrors.aliyun.com/pypi/simple/

Conda 下MLX LORA

安装CONDA

Download Anaconda Distribution | Anaconda

首先安装好,win64 下 960MB

pip install mlx-lm transformers numpy huggingface_hub
#下不了的用阿里的镜像


pip install mlx-lm transformers numpy huggingface_hub -i https://mirrors.aliyun.com/pypi/simple/


export HF_ENDPOINT=https://hf-mirror.com 
#设置镜像
huggingface-cli download --resume-download Qwen/Qwen2.5-0.5B-Instruct --local-dir qwen2.5-0.5B
#下载Qwen2.5 0.5B

#下载MLX example
git clone https://github.com/ml-explore/mlx-examples.git
#若下载失败可以使用
git clone https://bgithub.xyz/ml-explore/mlx-examples.git

#修改其中lora/data/train.jsonl


#微调 lora
mlx_lm.lora --model ../../qwen2.5-0.5B --train --data ./data

#合并
mlx_lm.fuse --model ../../qwen2.5-0.5B --adapter-path adapters --save-path qwen2.5-0.5B-test_1

#调用微调好的模型
mlx_lm.generate --model qwen2.5-0.5B-test_1 --prompt "蓝牙耳机坏了应该看什么科"

#调用原始的数据
mlx_lm.generate --model ../../qwen2.5-0.5B --prompt "蓝牙耳机坏了应该看什么科"