• Stars
    star
    1,006
  • Rank 45,677 (Top 0.9 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created 9 months ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The official implementation of Self-Play Fine-Tuning (SPIN)


🤗 Models | 🤗 Datasets

Self-Play Fine-Tuning (SPIN)

Mistral-7B Open LLM MT-Bench

This repository contains the official code for the paper "Self-Play Fine-Tuning Converts Weak Language Models to Strong Language Models".

Authors: Zixiang Chen*, Yihe Deng*, Huizhuo Yuan*, Kaixuan Ji, Quanquan Gu

[Webpage] [Huggingface]

🔔 News

Note: We notice that the Alignment Handbook has updated their configuration and SFT checkpoint since our experiments. The configuration and SFT model from the Alignment Handbook that we used in our experiments for data generation and fine-tuning are the older version (Config, Model). The model checkpoint on Hugging Face (alignment-handbook/zephyr-7b-sft-full) has been updated once with the new config. If you wish to use the newest SFT model, you need to either load the checkpoint with revision=ac6e600eefcce74f5e8bae1035d4f66019e93190 or generate your own data instead of using the datasets we provided on Hugging Face.

Table of Contents

🌀 About SPIN

SPIN utilizes a self-play mechanism, allowing an LLM to improve itself by playing against its previous iterations, without needing additional human-annotated preference data than the SFT dataset itself. More specifically, the LLM generates its own training data from its previous iterations, refining its policy by discerning these self-generated responses from the original SFT data.


Average score of SPIN at different iterations on the HuggingFace Open LLM leaderboard.

SPIN can significantly enhance the performance of an LLM after SFT across various benchmarks, outperforming the model trained with direct preference optimization (DPO) on labelled preference datasets. The approach is theoretically grounded, ensuring that the LLM aligns with the target data distribution, and empirically validated through extensive evaluations on multiple datasets.


Performance comparison with DPO training across the six benchmark datasets. SPIN at iteration 0 achieves comparable performance to DPO training with 62k new data. At iteration 1, SPIN has already surpassed DPO training on the majority of datasets.

For more details, you can check our paper here.

Setup

The following steps provide the necessary setup to run our codes.

  1. Create a Python virtual environment with Conda:
conda create -n myenv python=3.10
conda activate myenv
  1. Install PyTorch v2.1.0 with compatible cuda version, following instructions from PyTorch Installation Page. For example with cuda 11:
pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 --index-url https://download.pytorch.org/whl/cu118
  1. Install the following Python dependencies to run the codes.
python -m pip install .
python -m pip install flash-attn --no-build-isolation
  1. Login to your huggingface account for downloading models
huggingface-cli login --token "${your_access_token}"

Data

We provide the data used in our experiments along with the synthetic data we generated in this repo as well as on HuggingFace. These data is converted to .parquet format for fine-tuning.

Dataset Download
SPIN_iter0 🤗 HuggingFace
SPIN_iter1 🤗 HuggingFace
SPIN_iter2 🤗 HuggingFace
SPIN_iter3 🤗 HuggingFace

The input data for our code is required to be of the same format where each data contains the following attributes, as similar to HuggingFaceH4/ultrafeedback_binarized:

{
    "real": [{"role": "user", "content": <prompt>}, 
               {"role": "assistant", "content": <ground truth>}],
    "generated": [{"role": "user", "content": <prompt>}, 
                 {"role": "assistant", "content": <generation>}]
}

🔍 Note: During data generation, the content for generated response can be empty, as we only uses prompt to generate model responses.

Model

We also provide our model checkpoints at iteration 0,1,2,3 on HuggingFace.

Model Download
zephyr-7b-sft-full-SPIN-iter0 🤗 HuggingFace
zephyr-7b-sft-full-SPIN-iter1 🤗 HuggingFace
zephyr-7b-sft-full-SPIN-iter2 🤗 HuggingFace
zephyr-7b-sft-full-SPIN-iter3 🤗 HuggingFace

🔍 Note: With the provided data, you can directly jump to Step 2: Fine-tuning without doing data generation on your own. You may also start from any iteration to reproduce our results using our open-sourced model checkpoints.

Usage

For SPIN, we generate all synthetic data at once for an iteration, and fine-tune the LLM based on the real and synthetic data pairs.

Step 0 (optional): Reformatting SFT dataset

python spin/reformat.py [options]

Options

  • --data: directory to the SFT dataset (local or huggingface)
    • default: HuggingFaceH4/ultrachat_200k
  • --output_dir: local directory to the reformated data files
    • default: UCLA-AGI/SPIN_iter0

🔍 Note: If choosing to use SPIN on the entire dataset of HuggingFaceH4/ultrachat_200k instead of our 50k subset, one can reformat the original data with spin/reformat.py. To use other datasets, simply convert the data into the same format and resume with the following steps.

Step 1: Generation

accelerate launch spin/generate.py [options]

Options

  • --model: load model checkpoint for generation.
    • default: alignment-handbook/zephyr-7b-sft-full
  • --input_dir: directory to the data files with prompts for generation
    • The code is for generation based on data of the format given below.
    • default: UCLA-AGI/SPIN_iter0
  • --output_dir: directory to save the output data.
  • --batch_size: per device batch size
    • default: 16
  • --data_frac: break data into fractions for generations across server.
    • --frac_len: length of the data fraction. Default is 0 which uses the entire dataset for generation. Set frac_len to a positive number to generate only for a fraction of data. Note: we recommend using a smaller frac_len (e.g. 800) to generate data by small batches to avoid unexpected crashes as data generation can be very time-consuming.
    • Setting data_frac to be 0, 1, 2... to generate for different fractions of length frac_len.
    • Note: maintain the same frac length when doing generation using data_frac. It's recommended to set a smaller frac_len to 800.
  • --split: choose the split for data generation
    • default: train

The generated data is in json format where each data contains the following attributes:

{
    "real": [{"role": "user", "content": <prompt>}, 
               {"role": "assistant", "content": <ground truth>}],
    "generated": [{"role": "user", "content": <prompt>}, 
                 {"role": "assistant", "content": <generation>}]
}

Note: the iteration number for data generation solely depends on which model is used for generation (e.g. use original SFT model for iter0 data generation and SPIN iter0 model for iter1 data generation). The generate.py script will only use the prompts/questions for the data model.

Example. The following code generates 8k synthetic data for iteration 0.

bash scripts/generate.sh

🚀 Faster generation with vLLM

Alternatively, you could use the following example script to generate LLM responses with speedup. Larger frac_len can be used with vllm.

bash scripts/generate_vllm.sh

Thanks to @sumo43 for implementing vLLM for generation.

Step 1.5: Gather generations and convert data type

python spin/convert_data.py [options]

Options

  • --num_fracs: number of files to load in.
  • --input_dir: directory to the generated data files.
  • --output_dir: directory for the unified data that will be used for fine-tuning.

The code will generate the two final data files including train_prefs-00000-of-00001.parquet and test_prefs-00000-of-00001.parquet, which will be used for fine-tuning.

Note: make sure to collect the generated data filed into the same directory of --input_dir.

Example.

python spin/convert_data.py --output_dir new_data/iter0 --input_dir generated/iter0 --num_fracs 63

Step 2: Fine-tuning

accelerate launch --config_file configs/multi_gpu.yaml --num_processes=8 --main_process_port 29500 spin/run_spin.py configs/config.yaml

You might need to change the configuration in configs/config.yaml. Here are some key configs you might need to customize:

  • model_name_or_path: load model checkpoint for finetuning.
    • default: alignment-handbook/zephyr-7b-sft-full
  • dataset_mixer: choose data to mix for fine-tuning.
    • default: UCLA-AGI/SPIN_iter0: 1.0
    • For SPIN at iteration 1 and 2, we included both the current iteration and the previous iteration (e.g. for iteration 1 we included both UCLA-AGI/SPIN_iter0: 1.0 and UCLA-AGI/SPIN_iter1: 1.0, summing to 100k data.)
  • output_dir: the output directory of finetuned model and checkpoints.
    • default: outputs
  • per_device_train_batch_size: batch size on one GPU.
    • default: 16
  • gradient_accumulation_steps: make sure that the product per_device_train_batch_size*num_processes*gradient_accumulation_steps equals to your true batch size.
  • num_train_epochs: the training epochs of this iteration.
    • default: 3
  • beta: beta in SPIN.
    • default: 0.1

In our experiments, we do full fine-tuning on a multi-GPU machine with DeepSpeed ZeRO-3 (requires A100 (80GB)).

Example.

bash scripts/finetune.sh

Evaluation

For our evaluation on the Open LLM Leaderboard, please use the lm-evaluation-harness repository at v0.4.0. Also, note that we set the number of few shot examples to be the same as instructed on the Leaderboard. Different evaluation versions results in different scores, but the trend will remain the same.

Citation

If you find this repo useful for your research, please consider citing the paper

@misc{chen2024selfplay,
      title={Self-Play Fine-Tuning Converts Weak Language Models to Strong Language Models}, 
      author={Zixiang Chen and Yihe Deng and Huizhuo Yuan and Kaixuan Ji and Quanquan Gu},
      year={2024},
      eprint={2401.01335},
      archivePrefix={arXiv},
      primaryClass={cs.LG}
}

Acknowledgement

This repo is built upon The Alignment Handbook. We thank the authors for their great work.

More Repositories

1

SPPO

The official implementation of Self-Play Preference Optimization (SPPO)
Python
436
star
2

Rephrase-and-Respond

Official repo of Respond-and-Respond: data, code, and evaluation
Python
95
star
3

RayS

RayS: A Ray Searching Method for Hard-label Adversarial Attack (KDD2020)
Python
54
star
4

Padam

Partially Adaptive Momentum Estimation method in the paper "Closing the Generalization Gap of Adaptive Gradient Methods in Training Deep Neural Networks" (accepted by IJCAI 2020)
Python
39
star
5

NeuralUCB

Python
28
star
6

PDE

Official repo of Progressive Data Expansion: data, code and evaluation
Jupyter Notebook
26
star
7

MoE

Towards Understanding the Mixture-of-Experts Layer in Deep Learning
Jupyter Notebook
19
star
8

ucla-covid19-forecasts

Python
14
star
9

Frank-Wolfe-AdvML

A Frank-Wolfe Framework for Efficient and Effective Adversarial Attacks (AAAI'20)
Python
11
star
10

NeuralTS

Python
8
star
11

svrg-lowrank

fast nonconvex stochastic variance reduced gradient for low-rank matrix estimation
MATLAB
7
star
12

CS161-Winter2022

CS161-Winter2022
5
star
13

CS269-Winter2019

4
star
14

nonconvex-RPCA

fast nonconvex algorithm for robust PCA
MATLAB
4
star
15

nonconvex-IMC

fast nonconvex algorithm for inductive matrix completion
MATLAB
3
star
16

FedLinUCB

A Simple and Provably Efficient Algorithm for Asynchronous Federated Contextual Linear Bandits
Jupyter Notebook
2
star
17

CS260-Fall18

Machine Learning
HTML
2
star
18

FLASH

Fast Local minimA finding with third-order SmootHness (FLASH)
Python
2
star
19

CS269-Spring2021

Foundations of Deep Learning
2
star
20

CS161-Winter2020

Fundamentals of Artificial Intelligence
2
star
21

GFA-RFE

Uncertainty-Aware Reward-Free Exploration with General Function Approximation
Python
2
star
22

PhyGCN

Python
2
star
23

VACDB

Variance-aware Contextual Dueling Bandits
Python
2
star
24

CS161-Winter2023

CS161-Winter2023 Fundamentals of Artificial Intelligence
2
star
25

pretrain-finetune-SGD

The Power and Limitation of Pretraining-Finetuning for Linear Regression under Covariate Shift
Jupyter Notebook
1
star
26

LDP-UCRL-VTR

Locally Differentially Private Reinforcement Learning for Linear Mixture Markov Decision Processes
Python
1
star
27

CS260-Spring2020

Machine Learning
1
star
28

Benign-Overfitting-CNN

Benign Overfitting in Two-layer Convolutional Neural Networks
Jupyter Notebook
1
star
29

CS260-Fall2021

1
star
30

CS260-Fall2022

1
star
31

CS269-Spring2022

1
star
32

CP-time-series-clustering

High-dimensional Time Series Clustering via Cross-Predictability
MATLAB
1
star
33

Benign_ReLU_CNN

Python
1
star
34

CW-OFUL

Nearly Optimal Algorithms for Linear Contextual Bandits with Adversarial Corruptions
Jupyter Notebook
1
star
35

nonconvex-LVGGM

fast nonconvex algorithm for latent variable Gaussian graphical models
MATLAB
1
star