Align-Anything aims to align any modality large models (any-to-any models), including LLMs, VLMs, and others, with human intentions and values. More details about the definition and milestones of alignment for Large Models can be found in AI Alignment. Overall, this framework has the following characteristics:
- Highly Modular Framework. Its versatility stems from the abstraction of different algorithm types and well-designed APIs, allowing users to easily modify and customize the code for different tasks (see framework design).
- Support for Various Modality Model Fine-Tuning. This framework includes fine-tuning capabilities for models such as LLaMA3.2, LLaVA, Gemma, Qwen2Audio, Baichuan, and others (see Model Zoo).
- Support Different Alignment Methods. The framework supports different alignment algorithms, including SFT, DPO, PPO, and others (see scripts).
- Support Multi-Modal CLI. The framework supports multi-modal CLI for image, audio, and video modalities (see multi-modal CLI).
Note: We provide a quick start guide for users to quickly get the code structure and development details.
promptSmall white toilet sitting in a small corner next to a wall. |
promptA close up of a neatly made bed with two night stands |
promptA pizza is sitting on a plate at a restaurant. |
promptA girl in a dress next to a piece of luggage and flowers. |
|
---|---|---|---|---|
Before Alignment (Chameleon-7B) | ||||
After Alignment (Chameleon 7B Plus) |
Alignment fine-tuning can significantly enhance the instruction-following capabilities of large multimodal models. After fine-tuning, Chameleon 7B Plus generates images that are more relevant to the prompt.
Please cite the repo if you find the data or code in this repo useful 😊
@inproceedings{ji2024align,
title={Align Anything: Training All-Modality Models to Follow Instructions with Language Feedback},
author={Jiaming Ji and Jiayi Zhou and Hantao Lou and Boyuan Chen and Donghai Hong and Xuyao Wang and Wenqi Chen and Kaile Wang and Rui Pan and Jiahao Li and Mohan Wang and Josef Dai and Tianyi Qiu and Hua Xu and Dong Li and Weipeng Chen and Jun Song and Bo Zheng and Yaodong Yang},
year={2024},
url={https://arxiv.org/abs/2412.15838}
}
# clone the repository
git clone [email protected]:PKU-Alignment/align-anything.git
cd align-anything
# create virtual env
conda create -n align-anything python==3.11
conda activate align-anything
[Optional]
We recommend installing CUDA in the conda environment and set the environment variable.
# We tested on the H800 computing cluster, and this version of CUDA works well.
# You can adjust this version according to the actual situation of the computing cluster.
conda install nvidia/label/cuda-12.2.0::cuda
export CUDA_HOME=$CONDA_PREFIX
If your CUDA installed in a different location, such as
/usr/local/cuda/bin/nvcc
, you can set the environment variables as follows:
export CUDA_HOME="/usr/local/cuda"
Finally, install align-anything
by:
# We prepare quick installation for training and evaluation.
# If you only need to use the training or evaluation module,
# you can install the corresponding dependencies.
pip install -e .[train] # install the training dependencies
pip install -e .[evaluate] # install the evaluation dependencies
# If you need to install all dependencies, you can use the following command:
pip install -e .[all]
Other Dependencies
pip install -e .[text-to-audio]
: Install the text-to-audio dependencies.pip install -e .[minicpmv]
: Install the minicpmv dependencies.
We provide some scripts for quick start, you can find them in the ./scripts
directory. These scripts would automatically download the model and dataset, and run the training or evaluation.
For example, scripts/llava_dpo.sh
is the script for Text + Image -> Text
modality, you can run it by:
cd scripts
bash llava_dpo.sh
After training, you can evaluate the model by running the scripts/llava_eval.sh
script.
cd scripts
bash llava_eval.sh
You can simply modify the parameters in the script to suit your needs, e.g., the MODEL_NAME_OR_PATH
for your own model or TRAIN_DATASETS
for your own dataset. For more details please refer to the Advanced Usage section.
We support basic alignment algorithms for different modalities, each of which may involve additional algorithms. For instance, in the text modality, we have also implemented SimPO, KTO, and others.
Modality | SFT | RM | DPO | PPO |
---|---|---|---|---|
Text -> Text (t2t) |
✔️ | ✔️ | ✔️ | ✔️ |
Text+Image -> Text (ti2t) |
✔️ | ✔️ | ✔️ | ✔️ |
Text+Image -> Text+Image (ti2ti) |
✔️ | ✔️ | ✔️ | ✔️ |
Text+Audio -> Text (ta2t) |
✔️ | ✔️ | ✔️ | ✔️ |
Text+Video -> Text (tv2t) |
✔️ | ✔️ | ✔️ | ✔️ |
Text -> Image (t2i) |
✔️ | ⚒️ | ✔️ | ⚒️ |
Text -> Video (t2v) |
✔️ | ⚒️ | ✔️ | ⚒️ |
Text -> Audio (t2a) |
✔️ | ⚒️ | ✔️ | ⚒️ |
We support evaluation datasets for Text -> Text
, Text+Image -> Text
and Text -> Image
.
Modality | Supported Benchmarks |
---|---|
t2t |
ARC, BBH, Belebele, CMMLU, GSM8K, HumanEval, MMLU, MMLU-Pro, MT-Bench, PAWS-X, RACE, TruthfulQA |
ti2t |
A-OKVQA, LLaVA-Bench(COCO), LLaVA-Bench(wild), MathVista, MM-SafetyBench, MMBench, MME, MMMU, MMStar, MMVet, POPE, ScienceQA, SPA-VL, TextVQA, VizWizVQA |
tv2t |
MVBench, Video-MME |
ta2t |
AIR-Bench |
t2i |
ImageReward, HPSv2, COCO-30k(FID) |
t2v |
ChronoMagic-Bench |
t2a |
AudioCaps(FAD) |
- ⚒️ : coming soon.
We support wandb
logging. By default, it is set to offline. If you need to view wandb logs online, you can specify the environment variables of WANDB_API_KEY
before starting the training:
export WANDB_API_KEY="..." # your W&B API key here
Q (Training Model Registration): What models are supported for training? What should I pay attention to if I want to use my own model?
A: The models registration of align-anything is 2 folds:
- The model has been manually supported by the align-anything team, they are:
Modality | Models |
---|---|
Text -> Text |
meta-llama/Llama-3.1-8B-Instruct series (Llama3, Llama2 is also supported) |
Text+Image -> Text |
LLaVA series, LLaVA-Next series, openbmb/MiniCPM-V and LLaMA-3.2-Vision-Instruct |
Text+Image -> Text+Image |
facebook/chameleon-7b |
Text+Audio -> Text |
Qwen/Qwen2-Audio-7B-Instruct |
Text+Video -> Text |
Qwen/Qwen2-VL-7B-Instruct |
Text -> Image |
CompVis/stable-diffusion-v1-4 |
Text -> Video |
ali-vilab/text-to-video-ms-1.7b |
Text -> Audio |
cvssp/audioldm-s-full-v2 |
Besides, you can also use your own model for training, you can refer to the here (sorry, corresponding docs will be uploaded later) for the model registration.
Q (Training Dataset Registration): What datasets are supported for training? What should I pay attention to if I want to use my own dataset?
A: We prepare datasets_formatter
for dataset registration. Its core function is to mapping the dataset key to conversation format.
Basically, we support 3 types of dataset format:
Type | Description |
---|---|
format_supervised_sample |
Mapping the dataset to the supervised training format (For SFT). |
format_preference_sample |
Mapping the dataset to the preference training format (For RM, DPO, KTO, etc.). |
format_prompt_only_sample |
Mapping the dataset to the unique prompt only training format (For PPO). |
We introduce the following example below, and you can refer to here for more details.
format_supervised_sample
:
Click to expand
@register_template('Alpaca')
class Alpaca(BaseFormatter):
def format_supervised_sample(self, raw_sample: dict[str, Any]) -> tuple[list[dict[str, Any]], dict]:
prompt = ' '.join((raw_sample['instruction'], raw_sample['input']))
response = raw_sample['output']
return [
{"role": "user", "content": prompt},
{"role": "assistant", "content": response},
], {}
format_preference_sample
:
Click to expand
@register_template('AA_TI2T')
class AA_TI2T(BaseFormatter):
system_prompt: str = ""
def format_preference_sample(self, raw_sample: dict[str, Any]) -> tuple[list[dict[str, Any]], list[dict[str, Any]], dict[str, Any]]:
better_id = int(raw_sample['overall_response'])
worse_id = 2 if better_id==1 else 1
if better_id not in [1, 2] or worse_id not in [1, 2]:
return [], [], {}
raw_better_response = raw_sample[f'response_{better_id}']
raw_worse_response = raw_sample[f'response_{worse_id}']
prompt = raw_sample['question']
image = raw_sample['image'].convert('RGBA')
better_conversation = [
{'role': 'user', 'content': [
{'type': 'image'},
{'type': 'text', 'text': prompt},
]
},
{'role': 'assistant', 'content': [{'type': 'text', 'text': raw_better_response}]},
]
worse_conversation = [
{'role': 'user', 'content': [
{'type': 'image'},
{'type': 'text', 'text': prompt},
]
},
{'role': 'assistant', 'content': [{'type': 'text', 'text': raw_worse_response}]},
]
meta_info = {
'image': image,
'better_response': raw_better_response,
'worse_response': raw_worse_response,
}
return better_conversation, worse_conversation, meta_info
format_prompt_only_sample
:
Click to expand
@register_template('AA_TA2T')
class AA_TA2T(BaseFormatter):
system_prompt: str = 'You are a helpful assistant.'
def format_prompt_only_sample(self, raw_sample: dict[str, Any]) -> dict[str, Any]:
prompt = raw_sample['prompt']
audio_path = raw_sample['audio_path']
conversation = [
{'role': 'system', 'content': [{'type': 'text', 'text': self.system_prompt}]},
{'role': 'user', 'content': [
{'type': 'audio', 'audio_url': audio_path},
{'type': 'text', 'text': prompt},
]},
]
return conversation, {'audio_path': audio_path}
Q (Evaluation Model Registration): What models are supported for evaluation? What should I pay attention to if I want to use my own model?
A: Register your model to use align-anything for evaluation is easy, you only need to add your model special token to the ./align_anything/configs/eval_template.py
file.
For example, if you want to use liuhaotian/llava-v1.5-7b for evaluation, you need to add the following template for it to the ./align_anything/configs/eval_template.py
file:
@register_template('Llava')
class Llava:
system_prompt: str = ''
user_prompt: str = 'USER: \n<image>{input}'
assistant_prompt: str = '\nASSISTANT:{output}'
split_token: str = 'ASSISTANT:'
separator: str = '###'
All evaluation scripts can be found in the ./scripts
. The ./scripts/evaluate.sh
script runs model evaluation on the benchmarks, and parameters that require user input have been left empty. The corresponding script is as follow:
You can modify the configuration files for the benchmarks in this directory to suit specific evaluation tasks and models, and adjust inference parameters for vLLM or DeepSpeed based on your generation backend. For more details about the evaluation pipeline, refer to the here.
# Image inference
python3 -m align_anything.serve.multi_modal_cli --model_name_or_path llava-hf/llava-1.5-7b-hf --modality image
# Audio inference
python3 -m align_anything.serve.multi_modal_cli --model_name_or_path Qwen/Qwen2-Audio-7B-Instruct --modality audio
# Video inference
python3 -m align_anything.serve.multi_modal_cli --model_name_or_path llava-hf/LLaVA-NeXT-Video-7B-hf --modality video
python3 -m align_anything.serve.cli --model_name_or_path your_model_name_or_path
python3 -m align_anything.serve.arena \
--red_corner_model_name_or_path your_red_model_name_or_path \
--blue_corner_model_name_or_path your_blue_model_name_or_path
If you have any questions in the process of using align-anything, don't hesitate to ask your questions on the GitHub issue page, we will reply to you in 2-3 working days.
align-anything is released under Apache License 2.0.