{"id":15031011,"url":"https://github.com/openlmlab/moss-rlhf","last_synced_at":"2025-05-16T02:10:03.224Z","repository":{"id":180044274,"uuid":"662617050","full_name":"OpenLMLab/MOSS-RLHF","owner":"OpenLMLab","description":"Secrets of RLHF in Large Language Models Part I: PPO","archived":false,"fork":false,"pushed_at":"2024-03-03T04:56:49.000Z","size":2623,"stargazers_count":1350,"open_issues_count":39,"forks_count":98,"subscribers_count":31,"default_branch":"main","last_synced_at":"2025-04-08T13:06:20.946Z","etag":null,"topics":["ai-safety","alignment","rlhf"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OpenLMLab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-05T14:11:03.000Z","updated_at":"2025-04-08T11:56:58.000Z","dependencies_parsed_at":"2024-09-21T05:30:56.396Z","dependency_job_id":"ee2e0d34-1977-42c2-8513-80134d306bfc","html_url":"https://github.com/OpenLMLab/MOSS-RLHF","commit_stats":null,"previous_names":["openlmlab/moss-rlhf"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenLMLab%2FMOSS-RLHF","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenLMLab%2FMOSS-RLHF/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenLMLab%2FMOSS-RLHF/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenLMLab%2FMOSS-RLHF/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OpenLMLab","download_url":"https://codeload.github.com/OpenLMLab/MOSS-RLHF/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247847608,"owners_count":21006099,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai-safety","alignment","rlhf"],"created_at":"2024-09-24T20:14:43.715Z","updated_at":"2025-04-08T13:06:33.927Z","avatar_url":"https://github.com/OpenLMLab.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MOSS-RLHF\n\n**Congratulations**🎉🎉🎉 We received **the best paper award** at NIPS 2023 Workshop on Instruction Tuning and Instruction Following!\n\n庆祝🎉🎉🎉, \"Secrets of RLHF in Large Language Models Part I: PPO\" 荣获 NIPS 2023 Workshop on Instruction Tuning and Instruction Following 最佳论文奖！！！\n\n\u003cp align=\"center\" width=\"100%\"\u003e\n\u003ca href=\"https://arxiv.org/abs/2307.04964\" target=\"_blank\"\u003e\u003cimg src=\"./assets/img/moss.png\" alt=\"MOSS\" style=\"width: 50%; min-width: 300px; display: block; margin: auto;\"\u003e\u003c/a\u003e\n\n[![Code License](https://img.shields.io/badge/Code%20License-Apache_2.0-brightgreen.svg)](./LICENSE)\n[![Data License](https://img.shields.io/badge/Data%20License-CC%20BY--NC%204.0-blue.svg)](./DATA_LICENSE)\n[![Model License](https://img.shields.io/badge/Model%20License-GNU%20AGPL%203.0-red.svg)](./MODEL_LICENSE)\n\n### *MOSS-RLHF  👉 \u003ca href=\"https://openlmlab.github.io/MOSS-RLHF/\" target=\"_blank\"\u003e[Home page]*\n\n### *\"Secrets of RLHF in Large Language Models Part I: PPO\" 👉 \u003ca href=\"https://arxiv.org/abs/2307.04964\" target=\"_blank\"\u003e[Technical report I]\u003c/a\u003e*\n\n### *\"Secrets of RLHF in Large Language Models Part II: Reward Modeling\" 👉 \u003ca href=\"https://arxiv.org/abs/2401.06080\" target=\"_blank\"\u003e[Technical report II]\u003c/a\u003e*\n\n## 🌟🌟🌟 Breaking News\n\n👉 Mon, 15. January 2024. We have released the code for training the reward model and the annotated hh-rlhf dataset([hh-rlhf-strength-cleaned)](https://huggingface.co/datasets/fnlp/hh-rlhf-strength-cleaned \"https://huggingface.co/datasets/fnlp/hh-rlhf-strength-cleaned\")!\n\n👉 Fri, 12. January 2024. We have released the second paper **\"Secrets of RLHF in Large Language Models Part II: Reward Modeling\"**!\n\n## 🌟 News\n\n👉 Wed, 12. July 2023. We have released Chinese reward model based OpenChineseLlama-7B!\n[moss-rlhf-reward-model-7B-zh](https://huggingface.co/Ablustrund/moss-rlhf-reward-model-7B-zh/tree/main)\n`\u003cbr\u003e`\n\n👉 Thu, 13. July 2023. We have released English reward model and SFT model based Llama-7B!\n[moss-rlhf-reward-model-7B-en](https://huggingface.co/fnlp/moss-rlhf-reward-model-7B-en)\n\n[moss-rlhf-sft-model-7B-en](https://huggingface.co/fnlp/moss-rlhf-sft-model-7B-en)\n`\u003cbr\u003e`\n\n👉 Wait a minute ! Thu, 14. July 2023. We have released English policy model after aligning with RLHF!\n[moss-rlhf-policy-model-7B-en](https://huggingface.co/fnlp/moss-rlhf-policy-model-7B-en)\n`\u003cbr\u003e`\n\n## 🧾 Open-source List\n\n### RL related\n\n- [X] Open source code for RL training in large language models.\n- [X] A 7B Chinese reward model based on openChineseLlama.\n- [X] A 7B English reward model based on Llama-7B.\n- [X] SFT model for English.\n- [X] Policy model for English after RLHF.\n\n### RM related\n\n- [X] Open source code for reward model training in large language models.\n- [X] HH-RLHF dataset with preference strength annotation.\n- [X] HH-RLHF validation set cleaned by GPT-4.\n\n- ...\n\n## 🌠 Introduction\n\nDue to the challenges of reward design, environment interaction, and agent training, coupled with huge trial and error cost of large language models, there is a significant barrier for AI researchers to motivate the development of technical alignment and safe landing of LLMs. The stable training of RLHF has still been a puzzle.\nIn this technical report, we intend to help researchers to train their models stably with human feedback.\n\nContributions are summarized as follows:\n\n1) We release competitive Chinese and English reward models, respectively, which have good cross-model generalization ability, alleviating the cost of relabeling human preference data;\n2) We conduct in-depth analysis on the inner workings of PPO algorithm and propose the PPO-max algorithm to ensure stable model training;\n3) We release the complete PPO-max codes to ensure that the LLMs in the current SFT stage can be better aligned with humans.\n\n\u003cdiv align=\"center\" width=\"100%\"\u003e\n\u003cimg style=\"width: 80%; min-width: 500px; display: block; margin: auto; margin-bottom: 20px\" alt=\"MOSS-RLHF\" src=\"./assets/img/img1.jpg\"\u003e\n\u003c/div\u003e\n\n\u003cdiv align=\"center\" width=\"100%\"\u003e\n\u003cimg style=\"width: 80%; min-width: 500px; display: block; margin: auto; margin-bottom: 20px\" alt=\"MOSS-RLHF\" src=\"./assets/img/img2.jpg\"\u003e\n\u003c/div\u003e\n\n## 🔩 Requirements \u0026 Setup\n\nThis repository works on Python 3.8 and PyTorch 1.13.1.\n\nWe recommend using the **conda** virtual environment to run the code.\n\n#### Step 1: Create a new Python virtual environment\n\n```bash\nconda update conda -n base -c defaults\nconda create -n rlhf python=3.8\nconda activate rlhf\n```\n\n#### Step 2: Install PyTorch and TensorBoard\n\n```bash\nconda install pytorch==1.13.1 pytorch-cuda=11.7 tensorboard -c pytorch -c nvidia\n```\n\n#### Step 3: Install the remaining dependencies\n\n```bash\nconda install datasets accelerate safetensors chardet cchardet -c huggingface -c conda-forge\npip3 install transformers sentencepiece einops triton==1.0.0 rouge jionlp==1.4.14 nltk sacrebleu cpm_kernels\n\napt install libaio-dev\nDS_BUILD_OPS=1 pip install deepspeed\n```\n\n## ✨ Start training your own model!\n\n### Training PPO model\n\nRun code in a few steps.\n\n#### Step 1: Recover Reward model weights\n\nWe can not directly release the full weight of the reward model because of protocol restrictions.\nYou can merge the diff weight with original Llama-7B to recover the reward model we used.\n\nWe upload the diff models, thanks to tatsu-lab, you can recover the reward model follow these steps:\n\n```bash\n1) Download the weight diff into your local machine. The weight diff is located at:\n# For English:\n# SFT model\nhttps://huggingface.co/fnlp/moss-rlhf-sft-model-7B-en\n# Reward model\nhttps://huggingface.co/fnlp/moss-rlhf-reward-model-7B-en\n# Policy model\nhttps://huggingface.co/fnlp/moss-rlhf-policy-model-7B-en\n\n# For Chinese:\nhttps://huggingface.co/Ablustrund/moss-rlhf-reward-model-7B-zh/tree/main\n\n2) Merge the weight diff with the original Llama-7B:\n# For English:\n# Reward model\npython merge_weight_en.py recover --path_raw decapoda-research/llama-7b-hf --path_diff ./models/moss-rlhf-reward-model-7B-en/diff --path_tuned ./models/moss-rlhf-reward-model-7B-en/recover --model_type reward\n# SFT model\npython merge_weight_en.py recover --path_raw decapoda-research/llama-7b-hf --path_diff ./models/moss-rlhf-sft-model-7B-en/diff --path_tuned ./models/moss-rlhf-sft-model-7B-en/recover --model_type sft\n# Policy model\npython merge_weight_en.py recover --path_raw decapoda-research/llama-7b-hf --path_diff ./models/moss-rlhf-policy-model-7B-en/diff --path_tuned ./models/moss-rlhf-policy-model-7B-en/recover --model_type policy\n# For Chinese:\npython merge_weight_zh.py recover --path_raw decapoda-research/llama-7b-hf --path_diff ./models/moss-rlhf-reward-model-7B-zh/diff --path_tuned ./models/moss-rlhf-reward-model-7B-zh/recover\n```\n\n#### Step 2: Select your own SFT model.\n\nBecause of some limitations, we can not release the **Chinese** SFT model (Currently).\nYou can use your own SFT model, or a strong base model instead of our SFT model.\n\n#### Step 3: Start training\n\nRun the command below.\n\n```\n# For Chinese:\n# You need to use your own sft model currently.\nbash train_ppo_zh.sh\n\n# For English:\n# We have loaded the sft model and reward model to huggingface.\nbash train_ppo_en.sh\n\n```\n\n### Training reward model\n\nTo train the reward model, you need to specify the initial model (`--hf_model_name_or_path`) for the reward model (e.g., meta-llama/Llama-2-7b-hf) and preference dataset(`--data_path`) (such as hh-rlhf, or you can use our provided [annotated hh-rlhf](https://huggingface.co/datasets/fnlp/hh-rlhf-strength-cleaned \"https://huggingface.co/datasets/fnlp/hh-rlhf-strength-cleaned\") which has a format consistent with the training code), and run the command below.\n\n```\n# annotated dataset: https://huggingface.co/datasets/fnlp/hh-rlhf-strength-cleaned\n# Assuming you have specified the --hf_model_name_or_path and --data_path parameters.\nbash train_rm.sh\n```\n\n## Citation\n\n```bibtex\n@article{zheng2023secrets,\n      title={Secrets of RLHF in Large Language Models Part I: PPO}, \n      author={Rui Zheng and Shihan Dou and Songyang Gao and Wei Shen and Binghai Wang and Yan Liu and Senjie Jin and Qin Liu and Limao Xiong and Lu Chen and Zhiheng Xi and Yuhao Zhou and Nuo Xu and Wenbin Lai and Minghao Zhu and Rongxiang Weng and Wensen Cheng and Cheng Chang and Zhangyue Yin and Yuan Hua and Haoran Huang and Tianxiang Sun and Hang Yan and Tao Gui and Qi Zhang and Xipeng Qiu and Xuanjing Huang},\n      year={2023},\n      eprint={2307.04964},\n      archivePrefix={arXiv},\n      primaryClass={cs.CL}\n}\n```\n```bibtex\n@misc{wang2024secrets,\n      title={Secrets of RLHF in Large Language Models Part II: Reward Modeling}, \n      author={Binghai Wang and Rui Zheng and Lu Chen and Yan Liu and Shihan Dou and Caishuang Huang and Wei Shen and Senjie Jin and Enyu Zhou and Chenyu Shi and Songyang Gao and Nuo Xu and Yuhao Zhou and Xiaoran Fan and Zhiheng Xi and Jun Zhao and Xiao Wang and Tao Ji and Hang Yan and Lixing Shen and Zhan Chen and Tao Gui and Qi Zhang and Xipeng Qiu and Xuanjing Huang and Zuxuan Wu and Yu-Gang Jiang},\n      year={2024},\n      eprint={2401.06080},\n      archivePrefix={arXiv},\n      primaryClass={cs.AI}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenlmlab%2Fmoss-rlhf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenlmlab%2Fmoss-rlhf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenlmlab%2Fmoss-rlhf/lists"}