/magpie

Official repository for "Alignment Data Synthesis from Scratch by Prompting Aligned LLMs with Nothing"

Primary LanguageShellMIT LicenseMIT

Magpie

arXiv License: MIT Data License Spaces

This is the official repository for "Alignment Data Synthesis from Scratch by Prompting Aligned LLMs with Nothing".

You can try the no-code Magpie demo 🤗 here to generate instruction-response pairs. Thanks a lot for the quick implementation from @davanstrien!

Magpie Supports

Currently, Magpie has been tested on the Llama-3 and Qwen2 series. Feel free to submit a pull request to configs/model_configs.json with more model support.

Model Family Magpie Magpie Script Dataset
Llama 3 8B,70B 8B,70B
Qwen2 7B,72B 72B
Phi 3 mini,small,medium medium
Llama 2 ⭕️ 7B,70B
Gemma ⭕️ 7B
Mistral ⭕️ 7B
Yi ⭕️ 34B
  • ✅: Works so great!
  • ⭕️: Partially work. We can get something interesting, but may apply a powerful filter and/or a logits processor.
  • ❌: Not work.
  • ❓: Untested.

We hope Magpie can contribute to the democratization of AI. With your help, we can create more data and enhance the transparency of model alignment processes!

Abstract

High-quality instruction data is critical for aligning large language models (LLMs). Although some models, such as Llama-3-Instruct, have open weights, their alignment data remain private, which hinders the democratization of AI. High human labor costs and a limited, predefined scope for prompting prevent existing open-source data creation methods from scaling effectively, potentially limiting the diversity and quality of public alignment datasets. Is it possible to synthesize high-quality instruction data at scale by extracting it directly from an aligned LLM? We present a self-synthesis method for generating large-scale alignment data named Magpie. Our key observation is that aligned LLMs like Llama-3-Instruct can generate a user query when we input only the left-side templates up to the position reserved for user messages, thanks to their auto-regressive nature. We use this method to prompt Llama-3-Instruct and generate 4 million instructions along with their corresponding responses. We perform a comprehensive analysis of the extracted data and select 300K high-quality instances. To compare Magpie data with other public instruction datasets, we fine-tune Llama-3-8B-Base with each dataset and evaluate the performance of the fine-tuned models. Our results indicate that in some tasks, models fine-tuned with Magpie perform comparably to the official Llama-3-8B-Instruct, despite the latter being enhanced with 10 million data points through supervised fine-tuning (SFT) and subsequent feedback learning. We also show that using Magpie solely for SFT can surpass the performance of previous public datasets utilized for both SFT and preference optimization, such as direct preference optimization with UltraFeedback. This advantage is evident on alignment benchmarks such as AlpacaEval, ArenaHard, and WildBench.

Overview

Overview

Installation

Build environment

git clone https://github.com/magpie-align/magpie.git
cd magpie
conda create -n magpie python=3.10
conda activate magpie
pip install -r requirements.txt

Get access to Llama-3 models from 🤗 Huggingface

You can apply for Llama-3 model access here. To login in the terminal, enter:

huggingface-cli login

then enter your Huggingface private key beginning with "hf_".

Toy Example

Play with Jupyter Notebook

The toy example can be found in demo.ipynb. Have fun!

Open In Colab

Batched Data Generation

To run batched generation using Llama-3-8B-Instruct, you can simply run:

cd scripts
bash magpie.sh

The script will generate both instructions and responses in the data folder. It has been tested on an RTX 4090 24G GPU. If you are using GPUs with less memory, consider implementing quantization.

We also provide scripts for other models in the scripts folder. You can use this navigation to find specific Magpie scripts. Note that for model sizes greater than 8B, you may need 4*A100 GPUs to run the scripts.

Dataset Tagging and Removing Repetition

Tagging

To tag the generated instruction-response pairs in the previous step, e.g., ***_ins_res.json, you can run:

cd scripts
bash unitag.sh ***_ins_res.json all

This script will automatically generate quality, difficulty, task category, safety, and reward for the generated dataset. You can also generate one tag at a time. For example, if you just want to generate the safety label using device 0, you can run:

cd scripts
bash unitag.sh ***_ins_res.json safety 0

Data Concatenation and Convert for Fine-tuning

You may generate datasets with different generation configurations. We provide a Jupyter notebook here for concatenating all datasets and converting them to ShareGPT format, which is fully supported by Axolotl for fine-tuning.

Removing Repetition

Once you have a full dataset converted to ShareGPT format, you can calculate the minimum neighbor distance of each instruction and remove repetitions. To do so, run:

cd exp
python gen_dis.py --input_file ***_sharegpt.jsonl

where ***_sharegpt.jsonl is the dataset path obtained in the previous step. The Python script will take care of building the FAISS index and calculating the minimum distance.

Citation

If you find the model, data, or code useful, please cite our paper:

@misc{xu2024magpie,
    title={Magpie: Alignment Data Synthesis from Scratch by Prompting Aligned LLMs with Nothing}, 
    author={Zhangchen Xu and Fengqing Jiang and Luyao Niu and Yuntian Deng and Radha Poovendran and Yejin Choi and Bill Yuchen Lin},
    year={2024},
    eprint={2406.08464},
    archivePrefix={arXiv},
    primaryClass={cs.CL}
}