8 篇文章带有标签 “WikiSQL”

MLX: An array framework for Apple silicon

MLX 是一个为 Apple Silicon 芯片上的机器学习研究设计的 array 框架,由 Apple 机器学习研究团队提供。

  • 熟悉的 API:MLX 拥有一个与 NumPy 紧密对应的 Python API。MLX 还拥有功能齐全的 C++、C 和 Swift API,这些 API 也紧密地反映了 Python API。MLX 拥有更高级别的包,如 mlx.nn 和 mlx.optimizers,它们的 API 紧密跟随 PyTorch,以简化构建更复杂模型的过程。
  • 统一内存:MLX 与其他框架的一个显著区别在于其统一内存模型。MLX 中的数组存在于共享内存中。可以在任何支持的设备类型上执行 MLX 数组的操作,无需数据传输。
  • MLX 的设计受到了像 NumPyPyTorchJaxArrayFire 这样的框架的启发。
  • conda
conda install -c conda-forge mlx
conda install -c conda-forge mlx-lm
pip install sentence_transformers   # Mistral requires
pip install jinja2                  # Mistral requires
pip install tiktoken                # Qwen requires

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(八):使用 LoRA 基于 TinyLlama 微调

  • 输入
<|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|>
  • 输出
[
  {
    "generated_text": "<|system|>\nYou are a chatbot who can help code!</s>\n<|user|>\nWrite me a function to calculate the first 10 digits of the fibonacci sequence in Python and print it out to the CLI.</s>\n<|assistant|>\nHere's a Python function that calculates the first 10 digits of the Fib"
  }
]
  • 生成

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(七):MLX 微调的模型转换为 GGUF 模型

将 MLX 微调的模型转换为 GGUF 模型最大的意义是可以融入 GGUF 的生态系统,可以在更多的平台上使用。

修改脚本 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
pip install mlx-lm

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(五):对比使用 LoRA 和 QLoRA 基于 Mistral-7B 微调的效果

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

计算测试集困惑度(PPL)和交叉熵损失(Loss)。

Iteration LoRA Test Loss LoRA Test PPL QLoRA Test Loss QLoRA Test PPL
600 1.351 3.863 1.396 4.040
Prompt: table: students
columns: Name, Age, School, Grade, Height, Weight
Q: What is Wang Junjian's name?
A: 

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(四):使用 QLoRA 基于 Mistral-7B 微调

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

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(二):使用 LoRA 基于 Mistral-7B 微调

本次微调的模型我已经上传到了 HuggingFace Hub 上,大家可以进行尝试。

pip install mlx-lm

📌 没有使用模型的标注格式生成数据集,导致不能结束,直到生成最大的 Tokens 数量。

这次我们来解决这个问题。

执行脚本 data/wikisql.py 生成数据集。

table: 1-10753917-1
columns: Season, Driver, Team, Engine, Poles, Wins, Podiums, Points, Margin of defeat
Q: Which podiums did the alfa romeo team have?
A: SELECT Podiums FROM 1-10753917-1 WHERE Team = 'Alfa Romeo'</s>

在 MLX 上使用 LoRA / QLoRA 微调 Text2SQL(一):使用 LoRA 基于 Mistral-7B 微调

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