From a707f5b502b21243a8de87d804a86931fd973c90 Mon Sep 17 00:00:00 2001 From: hiyouga Date: Sat, 22 Jul 2023 14:29:22 +0800 Subject: [PATCH] update readme, fix web ui postprocess Former-commit-id: ba51ab3379100108f7b52a3c2444ccdd99e8a6ef --- README.md | 16 ++--- README_zh.md | 105 ++++++++++++++++---------------- data/README.md | 20 +++--- data/README_zh.md | 4 +- src/llmtuner/webui/chat.py | 8 ++- src/llmtuner/webui/interface.py | 2 +- 6 files changed, 81 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index ee61d7fa..efc7b7ff 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ [23/07/18] Now we develop an all-in-one Web UI for training, evaluation and inference. Try `train_web.py` to fine-tune models in your Web browser. Thank [@KanadeSiina](https://github.com/KanadeSiina) and [@codemayq](https://github.com/codemayq) for their efforts in the development. -[23/07/11] Now we support training the **Baichuan-13B** model in this repo. Please replace the Baichuan-13B model file with `tests/modeling_baichuan.py` and try `--model_name_or_path path_to_baichuan_model` and `--lora_target W_pack` arguments to train the Baichuan-13B model. Remember to use `--prompt_template baichuan` argument when you are using the Baichuan-13B-Chat model. +[23/07/11] Now we support training the **Baichuan-13B** model in this repo. Try `--model_name_or_path baichuan-inc/Baichuan-13B-Base` and `--lora_target W_pack` arguments to train the Baichuan-13B model. Remember to use `--prompt_template baichuan` argument when you are using the Baichuan-13B-Chat model. [23/07/09] Now we release [FastEdit](https://github.com/hiyouga/FastEdit)⚡🩹, an easy-to-use package for editing the factual knowledge of large language models efficiently. Please follow [FastEdit](https://github.com/hiyouga/FastEdit) if you are interested. @@ -108,12 +108,6 @@ huggingface-cli login And **powerful GPUs**! -If you want to enable quantized LoRA (QLoRA) on the Windows platform, you should install a pre-built version of `bitsandbytes` library, which supports CUDA 11.1 to 12.1. - -```bash -pip install https://github.com/jllllll/bitsandbytes-windows-webui/releases/download/wheels/bitsandbytes-0.39.1-py3-none-win_amd64.whl -``` - ## Getting Started ### Data Preparation (optional) @@ -132,12 +126,20 @@ cd LLaMA-Efficient-Tuning pip install -r requirements.txt ``` +If you want to enable the quantized LoRA (QLoRA) on the Windows platform, you will be required to install a pre-built version of `bitsandbytes` library, which supports CUDA 11.1 to 12.1. + +```bash +pip install https://github.com/jllllll/bitsandbytes-windows-webui/releases/download/wheels/bitsandbytes-0.39.1-py3-none-win_amd64.whl +``` + ### All-in-one Web UI ```bash python src/train_web.py ``` +Currently the web UI only supports training on a single GPU. + ### (Continually) Pre-Training ```bash diff --git a/README_zh.md b/README_zh.md index 6caef44f..f6272fe3 100644 --- a/README_zh.md +++ b/README_zh.md @@ -12,29 +12,29 @@ ## 更新日志 -[23/07/19] 现在我们在该仓库中提供了对于 **LLaMA-2** 模型的训练支持. 试试 `--model_name_or_path meta-llama/Llama-2-7b-hf` 参数来使用 LLaMA-2 模型. 使用 LLaMA-2-chat 模型时记得使用 `--prompt_template llama2` 参数. +[23/07/19] 现在我们支持了 **LLaMA-2** 模型的训练。请尝试使用 `--model_name_or_path meta-llama/Llama-2-7b-hf` 参数。请注意使用 LLaMA-2-chat 模型需要添加 `--prompt_template llama2` 参数。 -[23/07/18] 我们开发了一个用于训练、评估和推理的 all-in-one Web UI, . 试试 `train_web.py` 在浏览器中微调模型. 感谢 [@KanadeSiina](https://github.com/KanadeSiina) 和 [@codemayq](https://github.com/codemayq) 在项目发展中做出的努力. +[23/07/18] 我们开发了支持训练和测试的浏览器一键微调界面。请尝试使用 `train_web.py` 在您的浏览器中微调模型。感谢 [@KanadeSiina](https://github.com/KanadeSiina) 和 [@codemayq](https://github.com/codemayq) 在该功能开发中付出的努力。 -[23/07/11] 现在我们在该仓库中提供了对于 **Baichuan-13B** 模型的训练支持. 训练 Baichuan-13B 时使用 `tests/modeling_baichuan.py` , 然后试试 `--model_name_or_path path_to_baichuan_model` 和 `--lora_target W_pack` 参数来训练 Baichuan-13B 模型. 使用 Baichuan-13B-Chat 模型时记得使用 `--prompt_template baichuan` 参数. +[23/07/11] 现在我们支持了 **Baichuan-13B** 模型的训练。请尝试使用 `--model_name_or_path path_to_baichuan_model` 和 `--lora_target W_pack` 参数。请注意使用 Baichuan-13B-Chat 模型需要添加 `--prompt_template baichuan` 参数。 [23/07/09] 我们开源了 [FastEdit](https://github.com/hiyouga/FastEdit)⚡🩹,一个简单易用的、能迅速编辑大模型事实记忆的工具包。如果您感兴趣请关注我们的 [FastEdit](https://github.com/hiyouga/FastEdit) 项目。 -[23/07/07] 现在我们在该仓库中提供了对于 **InternLM-7B** 模型的训练支持. 试试 `--model_name_or_path internlm/internlm-7b` 参数来使用 InternLM 模型. 使用 InternLM-chat 模型时记得使用 `--prompt_template intern` 参数. +[23/07/07] 现在我们支持了 **InternLM-7B** 模型的训练。请尝试使用 `--model_name_or_path internlm/internlm-7b` 参数。请注意使用 InternLM-chat 模型需要添加 `--prompt_template intern` 参数。 -[23/07/05] 现在我们在该仓库中提供了对于 **Falcon-7B/40B** 模型的训练支持. 试试 `--model_name_or_path tiiuae/falcon-7b` 和 `--lora_target query_key_value` 参数来使用 Falcon 模型. +[23/07/05] 现在我们支持了 **Falcon-7B/40B** 模型的训练。请尝试使用 `--model_name_or_path tiiuae/falcon-7b` 和 `--lora_target query_key_value` 参数。 -[23/06/29] 我们提供了一个使用 instruction-following 数据集训练聊天模型的 **可复现的示例** , 更多细节请看这里 [Hugging Face Repo](https://huggingface.co/hiyouga/baichuan-7b-sft). +[23/06/29] 我们提供了一个**可复现的**指令模型微调示例,详细内容请查阅 [Hugging Face 项目](https://huggingface.co/hiyouga/baichuan-7b-sft)。 -[23/06/22] 我们对齐了[示例 API](src/api_demo.py) 与 [OpenAI API](https://platform.openai.com/docs/api-reference/chat) 的格式,您可以将微调模型接入任意基于 ChatGPT 的应用中. +[23/06/22] 我们对齐了[示例 API](src/api_demo.py) 与 [OpenAI API](https://platform.openai.com/docs/api-reference/chat) 的格式,您可以将微调模型接入任意基于 ChatGPT 的应用中。 -[23/06/15] 现在我们在该仓库中提供了对于 **Baichuan-7B** 模型的训练支持. 试试 `--model_name_or_path baichuan-inc/Baichuan-7B` 和 `--lora_target W_pack` 参数来使用 Baichuan-7B 模型. +[23/06/15] 现在我们支持了 **Baichuan-7B** 模型的训练。请尝试使用 `--model_name_or_path baichuan-inc/Baichuan-7B` 和 `--lora_target W_pack` 参数。 -[23/06/03] 现在我们支持了量化训练和推理 (也叫作 **[QLoRA](https://github.com/artidoro/qlora)**). 试试 `--quantization_bit 4/8` 参数来处理量化模型. (实验性功能) +[23/06/03] 现在我们实现了 4 比特的 LoRA 训练(也称 [QLoRA](https://github.com/artidoro/qlora))。请尝试使用 `--quantization_bit 4` 参数进行 4 比特量化微调。 -[23/05/31] 现在我们在该仓库中提供了对于 **BLOOM & BLOOMZ** 模型的训练支持. 试试 `--model_name_or_path bigscience/bloomz-7b1-mt` 和 `--lora_target query_key_value` 参数来使用 BLOOMZ 模型. +[23/05/31] 现在我们支持了 **BLOOM & BLOOMZ** 模型的训练。请尝试使用 `--model_name_or_path bigscience/bloomz-7b1-mt` 和 `--lora_target query_key_value` 参数。 -## 支持的模型 +## 模型 - [LLaMA](https://github.com/facebookresearch/llama) (7B/13B/33B/65B) - [LLaMA-2](https://huggingface.co/meta-llama) (7B/13B/70B) @@ -43,27 +43,27 @@ - [Baichuan](https://huggingface.co/baichuan-inc/baichuan-7B) (7B/13B) - [InternLM](https://github.com/InternLM/InternLM) (7B) -## 支持的训练方法 +## 微调方法 -- [(Continually) pre-training](https://s3-us-west-2.amazonaws.com/openai-assets/research-covers/language-unsupervised/language_understanding_paper.pdf) - - Full-parameter tuning - - Partial-parameter tuning +- [二次预训练](https://s3-us-west-2.amazonaws.com/openai-assets/research-covers/language-unsupervised/language_understanding_paper.pdf) + - 全参数微调 + - 部分参数微调 - [LoRA](https://arxiv.org/abs/2106.09685) - [QLoRA](https://arxiv.org/abs/2305.14314) -- [Supervised fine-tuning](https://arxiv.org/abs/2109.01652) - - Full-parameter tuning - - Partial-parameter tuning +- [指令监督微调](https://arxiv.org/abs/2109.01652) + - 全参数微调 + - 部分参数微调 - [LoRA](https://arxiv.org/abs/2106.09685) - [QLoRA](https://arxiv.org/abs/2305.14314) -- [RLHF](https://arxiv.org/abs/2203.02155) +- [人类反馈的强化学习(RLHF)](https://arxiv.org/abs/2203.02155) - [LoRA](https://arxiv.org/abs/2106.09685) - [QLoRA](https://arxiv.org/abs/2305.14314) -## 提供的训练集 +## 数据集 -- 对预训练: +- 用于二次预训练: - [Wiki Demo (en)](data/wiki_demo.txt) -- 对监督微调: +- 用于指令监督微调: - [Stanford Alpaca (en)](https://github.com/tatsu-lab/stanford_alpaca) - [Stanford Alpaca (zh)](https://github.com/ymcui/Chinese-LLaMA-Alpaca) - [GPT-4 Generated Data (en&zh)](https://github.com/Instruction-Tuning-with-GPT-4/GPT-4-LLM) @@ -84,14 +84,14 @@ - [Web QA (zh)](https://huggingface.co/datasets/suolyer/webqa) - [UltraChat (en)](https://github.com/thunlp/UltraChat) - [WebNovel (zh)](https://huggingface.co/datasets/zxbsmk/webnovel_cn) -- 对奖励模型: +- 用于奖励模型训练: - [HH-RLHF (en)](https://huggingface.co/datasets/Anthropic/hh-rlhf) - [Open Assistant (multilingual)](https://huggingface.co/datasets/OpenAssistant/oasst1) - [GPT-4 Generated Data (en&zh)](https://github.com/Instruction-Tuning-with-GPT-4/GPT-4-LLM) -更多细节请查看 [data/README.md](data/README_zh.md). +使用方法请参考 [data/README.md](data/README_zh.md) 文件。 -部分数据集使用之前需要确认, 因此推荐使用下面的命令登录您的 Huggingface 账户. +部分数据集的使用需要确认,我们推荐使用下述命令登录您的 Hugging Face 账户。 ```bash pip install --upgrade huggingface_hub @@ -106,23 +106,17 @@ huggingface-cli login - gradio 和 matplotlib (用于网页端交互) - uvicorn, fastapi 和 sse-starlette (用于 API) -以及 **强有力的 GPUs**! +以及 **强而有力的 GPU**! -如果要在 Windows 平台上开启量化 LoRA (QLoRA) , 需要安装预编译的 `bitsandbytes` 库, 支持 CUDA 11.1 到 12.1. +## 如何使用 -```bash -pip install https://github.com/jllllll/bitsandbytes-windows-webui/releases/download/wheels/bitsandbytes-0.39.1-py3-none-win_amd64.whl -``` +### 数据准备(可跳过) -## 起步 +关于数据集文件的格式,请参考 `data/example_dataset` 文件夹的内容。构建自定义数据集时,既可以使用单个 `.json` 文件,也可以使用一个[数据加载脚本](https://huggingface.co/docs/datasets/dataset_script)和多个文件。 -### 准备数据 (可选) +注意:使用自定义数据集时,请更新 `data/dataset_info.json` 文件,该文件的格式请参考 `data/README.md`。 -关于数据集文件的格式,请参考 `data/example_dataset` 文件夹的内容. 构建自定义数据集时, 既可以使用单个 `.json` 文件, 也可以使用一个[数据加载脚本](https://huggingface.co/docs/datasets/dataset_script)和多个文件. - -注意:使用自定义数据集时,请更新 `data/dataset_info.json` 文件,该文件的格式请参考 `data/README.md`. - -### 环境安装 (可选) +### 环境搭建(可跳过) ```bash git clone https://github.com/hiyouga/LLaMA-Efficient-Tuning.git @@ -132,13 +126,21 @@ cd LLaMA-Efficient-Tuning pip install -r requirements.txt ``` -### All-in-one Web UI +如果要在 Windows 平台上开启量化 LoRA(QLoRA),需要安装预编译的 `bitsandbytes` 库, 支持 CUDA 11.1 到 12.1. + +```bash +pip install https://github.com/jllllll/bitsandbytes-windows-webui/releases/download/wheels/bitsandbytes-0.39.1-py3-none-win_amd64.whl +``` + +### 浏览器一键微调/测试 ```bash python src/train_web.py ``` -### (Continually) Pre-Training +目前网页 UI 仅支持单卡训练。 + +### 二次预训练 ```bash CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ @@ -160,7 +162,7 @@ CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ --fp16 ``` -### 监督微调 +### 指令监督微调 ```bash CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ @@ -203,7 +205,7 @@ CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ --fp16 ``` -### PPO Training (RLHF) +### RLHF 训练 ```bash CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ @@ -226,14 +228,14 @@ CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ --plot_loss ``` -### 分布式微调 +### 多 GPU 分布式训练 ```bash -accelerate config # configure the environment -accelerate launch src/train_bash.py # arguments (same as above) +accelerate config # 首先配置分布式环境 +accelerate launch src/train_bash.py # 参数同上 ``` -
使用 DeepSpeed ZeRO-2 全量微调的配置示例 +
使用 DeepSpeed ZeRO-2 进行全参数微调的 Accelerate 配置示例 ```yaml compute_environment: LOCAL_MACHINE @@ -277,7 +279,7 @@ CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ --predict_with_generate ``` -我们建议在 4/8-bit 评估中使用 `--per_device_eval_batch_size=1` 和 `--max_target_length 128`. +我们建议在量化模型的评估中使用 `--per_device_eval_batch_size=1` 和 `--max_target_length 128` 参数。 ### 模型预测 @@ -298,6 +300,7 @@ CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \ 如果需要预测的样本没有标签,请首先在 `response` 列中填入一些占位符,以免样本在预处理阶段被丢弃。 ### API 服务 + ```bash python src/api_demo.py \ --model_name_or_path path_to_your_model \ @@ -305,7 +308,7 @@ python src/api_demo.py \ --checkpoint_dir path_to_checkpoint ``` -访问 `http://localhost:8000/docs` 获取 API 文档. +关于 API 文档请见 `http://localhost:8000/docs`。 ### 命令行测试 @@ -337,9 +340,9 @@ python src/export_model.py \ ## 协议 -本仓库采用 [Apache-2.0](LICENSE) 协议开源. +本仓库的代码依照 [Apache-2.0](LICENSE) 协议开源。 -请遵循模型许可证使用相应的模型权重: +使用模型权重时,请遵循对应的模型协议: - [LLaMA](https://github.com/facebookresearch/llama/blob/main/MODEL_CARD.md) - [LLaMA-2](https://ai.meta.com/llama/license/) @@ -350,7 +353,7 @@ python src/export_model.py \ ## 引用 -如果您觉得此项目有帮助,请考虑以下列格式引用: +如果您觉得此项目有帮助,请考虑以下列格式引用 ```bibtex @Misc{llama-efficient-tuning, @@ -363,7 +366,7 @@ python src/export_model.py \ ## 致谢 -该 repo 是 [ChatGLM-Efficient-Tuning](https://github.com/hiyouga/ChatGLM-Efficient-Tuning) 的兄弟产品. 它们都拥有在大规模语言模型上实现高效调优的相似的代码结构。 +本项目是 [ChatGLM-Efficient-Tuning](https://github.com/hiyouga/ChatGLM-Efficient-Tuning) 的同类项目。采用了类似的代码结构和训练方法。 ## Star History diff --git a/data/README.md b/data/README.md index a219fc40..fe0f7e42 100644 --- a/data/README.md +++ b/data/README.md @@ -2,16 +2,16 @@ If you are using a custom dataset, please provide your dataset definition in the ```json "dataset_name": { - "hf_hub_url": "the name of the dataset repository on the HuggingFace hub. (if specified, ignore below 3 arguments)", - "script_url": "the name of the directory containing a dataset loading script. (if specified, ignore below 2 arguments)", - "file_name": "the name of the dataset file in the this directory. (required if above are not specified)", - "file_sha1": "the SHA-1 hash value of the dataset file. (optional)", - "columns": { - "prompt": "the name of the column in the datasets containing the prompts. (default: instruction)", - "query": "the name of the column in the datasets containing the queries. (default: input)", - "response": "the name of the column in the datasets containing the responses. (default: output)", - "history": "the name of the column in the datasets containing the history of chat. (default: None)" - } + "hf_hub_url": "the name of the dataset repository on the HuggingFace hub. (if specified, ignore below 3 arguments)", + "script_url": "the name of the directory containing a dataset loading script. (if specified, ignore below 2 arguments)", + "file_name": "the name of the dataset file in the this directory. (required if above are not specified)", + "file_sha1": "the SHA-1 hash value of the dataset file. (optional)", + "columns": { + "prompt": "the name of the column in the datasets containing the prompts. (default: instruction)", + "query": "the name of the column in the datasets containing the queries. (default: input)", + "response": "the name of the column in the datasets containing the responses. (default: output)", + "history": "the name of the column in the datasets containing the history of chat. (default: None)" + } } ``` diff --git a/data/README_zh.md b/data/README_zh.md index 8a6e782b..3be0c09d 100644 --- a/data/README_zh.md +++ b/data/README_zh.md @@ -1,4 +1,4 @@ -如果您使用自定义数据集,请务必在 dataset_info.json 文件中以如下格式提供您的数据集定义。 +如果您使用自定义数据集,请务必在 `dataset_info.json` 文件中以如下格式提供您的数据集定义。 ```json "数据集名称": { @@ -15,4 +15,4 @@ } ``` -其中 prompt 和 response 列应当是非空的字符串。query 列的内容将会和 prompt 列拼接作为模型输入。history 列应当是一个列表,其中每个元素是一个字符串二元组,分别代表用户请求和模型答复。 +其中 `prompt` 和 `response` 列应当是非空的字符串。`query` 列的内容将会和 `prompt` 列拼接作为模型输入。`history` 列应当是一个列表,其中每个元素是一个字符串二元组,分别代表用户请求和模型答复。 diff --git a/src/llmtuner/webui/chat.py b/src/llmtuner/webui/chat.py index b721ad40..fcbeaa79 100644 --- a/src/llmtuner/webui/chat.py +++ b/src/llmtuner/webui/chat.py @@ -90,6 +90,8 @@ class WebChatModel(ChatModel): yield chatbot, new_history def postprocess(self, response: str) -> str: - response = response.replace("<", "<") - response = response.replace(">", ">") - return response + blocks = response.split("```") + for i, block in enumerate(blocks): + if i % 2 == 0: + blocks[i] = block.replace("<", "<").replace(">", ">") + return "```".join(blocks) diff --git a/src/llmtuner/webui/interface.py b/src/llmtuner/webui/interface.py index 11f1138a..cf9b714b 100644 --- a/src/llmtuner/webui/interface.py +++ b/src/llmtuner/webui/interface.py @@ -55,4 +55,4 @@ def create_ui() -> gr.Blocks: if __name__ == "__main__": demo = create_ui() demo.queue() - demo.launch(server_name="0.0.0.0", share=False, inbrowser=True) + demo.launch(server_name="0.0.0.0", server_port=7860, share=False, inbrowser=True)