Hy-SimulMT-30B-A3B 本地实时同传 · 研究与实践HTML
把 30B 的混元同传模型塞进 M2 Max 笔记本:绕开官方 CUDA 封装,给 llama-mtmd-cli 加 --stream 改流式,自建音频链路与账本,做到 255 ms 字幕、1150 ms 配音,并完整复盘五轮静默失败排查。
把 30B 的混元同传模型塞进 M2 Max 笔记本:绕开官方 CUDA 封装,给 llama-mtmd-cli 加 --stream 改流式,自建音频链路与账本,做到 255 ms 字幕、1150 ms 配音,并完整复盘五轮静默失败排查。
oMLX 是 Apple Silicon 上的本地推理服务,把 KV 缓存做成跨重启的内容寻址存储;三层缓存、4096 块粒度与连续批处理解掉智能体的 prefill 税,并附实测数据、反证据清单与四步验收方法。
该文本提供了一个关于PyTorch二分类神经网络的实现与性能分析的全面概述。首先,它通过具体代码示例展示了如何构建、训练、评估和保存一个基础的神经网络模型,并演示了如何加载模型进行推理。其次,文章深入探讨了不同模型参数规模下,Apple的MPS(Metal Performance Shaders)框架与CPU在训练时间上的性能对比,通过表格数据清晰地呈现了MPS在处理大型模型时相较于CPU的显著优势,并指出了性能的“转折点”。
我的电脑是 Apple MacBook Pro M2 Max 16寸 64G内存
PyTorch 二分类神经网络实现与训练示例 import torch import torch.nn.functional as F from torch.utils.data import Dataset from torch.utils.data import DataLoader # 模型网络 class NeuralNetwork(torch.nn.Module): def init(self, num_inputs, num_outputs): super().init() self.layers = torch.nn.Sequential( torch.nn.Linear(num_inputs, 30), torch.nn.ReLU(), torch.
MLX 介绍
MLX 是一个为 Apple Silicon 芯片上的机器学习研究设计的 array 框架,由 Apple 机器学习研究团队提供。
安装
pip install mlx
pip install mlx-lm
conda install -c conda-forge mlx
conda install -c conda-forge mlx-lm
TinyLlama/TinyLlama-1.1B-Chat-v1.0
<|system|>
You are a chatbot who can help code!</s>
<|user|>
Write me a function to calculate the first 10 digits of the fibonacci sequence in Python and print it out to the CLI.</s>
<|assistant|>
将 MLX 微调的模型转换为 GGUF 模型最大的意义是可以融入 GGUF 的生态系统,可以在更多的平台上使用。
LoRA 微调
大模型 Mistral-7B-v0.1
数据集 WikiSQL
修改脚本 mlx-examples/lora/data/wikisql.py
if __name__ == "__main__":
# ......
for dataset, name, size in datasets:
with open(f"data/{name}.jsonl", "w") as fid:
for e, t in zip(range(size), dataset):
t = t[3:]
json.dump({"text": t}, fid)
fid.write("\n")
执行脚本 data/wikisql.py 生成数据集。
data/wikisql.py
安装 mlx-lm
pip install mlx-lm
微调
大模型 Deepseek-Coder-7B
数据集 WikiSQL
修改脚本 mlx-examples/lora/data/wikisql.py if name == "main": # ...... for dataset, name, size in datasets: with open(f"data/{name}.jsonl", "w") as fid: for e, t in zip(range(size), dataset): # deepseek-ai/deepseek-coder-7b-instruct-v1.5 # 去掉开头的 <|begin▁of▁sentence|>,因为 tokenizer 会自动添加 <|begin▁of▁sentence|> t = t[3:-4] + "<|end▁of▁sentence|>" json.dump({"text": t}, fid) fid.
使用 LoRA 和 QLoRA 基于 Mistral-7B 微调的实验
LoRA 和 QLoRA 对比
微调
| Iteration | LoRA Train Loss | LoRA Val Loss | LoRA Tokens/sec | QLoRA Train Loss | QLoRA Val Loss | QLoRA Tokens/sec |
|---|---|---|---|---|---|---|
| 1 | 2.343 | 2.420 | ||||
| 100 | 1.204 | 221.348 | 1.216 | 166.377 | ||
| 200 | 1.091 | 1.111 | 207.353 | 1.095 | 1.130 | 187.795 |
| 300 | 0.818 | 234.182 | 1.065 | 194.826 | ||
| 400 | 0.837 | 1.076 | 207.763 | 0.998 | 1.006 | 170.072 |
| 500 | 0.774 | 223.036 | 0.726 | 189.288 | ||
| 600 | 0.609 | 1.001 | 218.118 | 0.607 | 1.015 | 186.397 |
微调的参数量 LoRA 微调万分之 2.35 (1.704M / 7243.436M * 10000)的模型参数。 QLoRA 微调万分之 13.
预训练模型 mistralai/Mistral-7B-v0.1
量化
QLoRA 微调需要量化,生成 4 位量化的 Mistral 7B 并默认将其存储在 mlx_model 目录中
python convert.py --hf-path mistralai/Mistral-7B-v0.1 -q
mlx_model 目录结构如下:
mlx_model
├── config.json
├── model.safetensors
├── special_tokens_map.json
├── tokenizer.json
├── tokenizer.model
├── tokenizer_config.json
└── weights.00.safetensors
量化后的模型 8.0G
微调
QLoRA 微调
mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL
安装 mlx-lm
pip install mlx-lm
生成 SQL
python -m mlx_lm.generate --model mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL \
--max-tokens 50 \
--prompt "table: students
columns: Name, Age, School, Grade, Height, Weight
Q: Which school did Wang Junjian come from?
A: "
SELECT School FROM Students WHERE Name = 'Wang Junjian'
上传模型到 HuggingFace Hub
mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL
本次微调的模型我已经上传到了 HuggingFace Hub 上,大家可以进行尝试。
安装 mlx-lm
pip install mlx-lm
生成 SQL
python -m mlx_lm.generate --model mlx-community/Mistral-7B-v0.1-LoRA-Text2SQL \
--max-tokens 50 \
--prompt "table: students
columns: Name, Age, School, Grade, Height, Weight
Q: Which school did Wang Junjian come from?
A: "
SELECT School FROM Students WHERE Name = 'Wang Junjian'
在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(一):使用 LoRA 基于 Mistral-7B 微调
📌 没有使用模型的标注格式生成数据集,导致不能结束,直到生成最大的 Tokens 数量。
这次我们来解决这个问题。
数据集 WikiSQL
修改脚本 mlx-examples/lora/data/w
安装
git clone https://github.com/ml-explore/mlx-examples.git
cd mlx-examples/lora
pip install -r requirements.txt
下载模型
pip install huggingface_hub hf_transfer
export HF_HUB_ENABLE_HF_TRANSFER=1
huggingface-cli download \
--local-dir-use-symlinks False \
--local-dir mistralai/Mistral-7B-v0.1 \
mistralai/Mistral-7B-v0.1
数据集 WikiSQL
样本格式
NEON & MPS 🆚 CoreML
下载模型(large-v3)
models/download-ggml-model.sh large-v3
NEON & MPS
编译
make clean
make -j
main 帮助 ./main --help usage: ./main [options] file0.wav file1.wav ...
克隆代码
git clone https://github.com/ml-explore/mlx-examples
cd mlx-examples
创建虚拟环境
python -m venv env
source env/bin/activate
pip install -r llms/phi2/requirements.txt
pip install -r llms/qwen/requirements.txt
创建大模型链接 mkdir llms/phi2/microsoft ln -s /Users/junjian/HuggingFace/microsoft/phi-2 llms/phi2/microsoft/phi-2 mkdir llms/qwen/Qwen ln -s /Users/junjian/HuggingFace/Qwen/Qwen-14B-Chat llms/qwen/Qwen/Qwen-14B-Chat ln -s /Users/junjian/HuggingFace/Qwen/Qwen-1_8B llms/qwen/Qwen/Qwen-1_8B ln -s /Users/junjian/HuggingFace/Qwen/Qwen-1_8B-Chat llms/qwen/Qwen/Qwen-1_8
统一内存:与 MLX 和其他框架的显着区别是统一内存模型。 MLX 中的数组位于共享内存中。 MLX 阵列上的操作可以在任何支持的设备类型上执行,而无需传输数据。
创建虚拟环境
mkdir ml-explore && cd ml-explore
git clone https://github.com/ml-explore/mlx
git clone https://github.com/ml-explore/mlx-examples
python -m venv env
source env/bin/activate
cd llms/phi2
pip install -r requirements.txt
使用已经下载的模型
mkdir microsoft
ln -s /Users/junjian/HuggingFace/microsoft/phi-2 microsoft/phi-2
转换模型
python convert.py
这将生成 MLX 可以读取的 weights.npz 文件。
-rw-r--r-- 1 junjian staff 5.2G 12 20 20:36 weights.npz
Phi-2: The surprising power of small language models
创建虚拟环境
conda create -n huggingface python==3.10.9
conda activate huggingface
安装依赖包
conda install pytorch torchvision -c pytorch
pip install transformers
pip install einops
下载模型
huggingface-cli download microsoft/phi-2 --local-dir microsoft/phi-2 --local-dir-use-symlinks False
代码 import torch from transformers import AutoModelForCausalLM, AutoTokenizer torch.set_default_device("mps") model = AutoModelForCausalLM.
下载代码
git clone https://github.com/Stability-AI/generative-models.git Stability-AI/generative-models
cd Stability-AI/generative-models/
创建虚拟环境
python -m venv env
source env/bin/activate
pip install -r requirements/pt2.txt
pip install .
Apple Silicon 上没有安装成功,安装包 triton 不支持
下载模型
pip install "huggingface_hub[cli]"
huggingface-cli download stabilityai/sdxl-turbo --local-dir checkpoints --local-dir-use-symlinks False
CLIP huggingface-cli download openai/clip-vit-large-patch14 --lo
ChatGLM2-6B 是开源中英双语对话模型 ChatGLM-6B 的第二代版本,在保留了初代模型对话流畅、部署门槛较低等众多优秀特性的基础之上,ChatGLM2-6B 引入了如下新特性:
LLaMA-13B 在大多数基准上的表现优于 GPT-3(175B),LLaMA-65B 与最好的型号 Chinchilla-70B 和 PaLM-540B 具有竞争力。
克隆
git clone https://github.com/facebookresearch/llama
cd llama
下载模型
修改 download.sh,配置下载模型的 地址(PRESIGNED_URL) 和 下载目录(TARGET_FOLDER)。
vim download.sh
PRESIGNED_URL="https://agi.gpt4.org/llama/LLaMA/*" # replace with presigned url from email
TARGET_FOLDER="./" # where all files should end up
bash download.sh
构建
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make
拷贝 LLaMA 模型到当前目录 ls .
没有找到匹配的文章