此儲存庫包含方便的腳本,用於微調 LLaMa3-8B(或任何其他基礎模型)以進行任何語言(非英語)的聊天。背後的基本原理是,LLaMa3 主要是在英語資料上進行訓練的,雖然它在一定程度上適用於其他語言,但與英語相比,其表現較差。
將微調的力量與 RAG 的力量相結合 - 查看 RAG 上的 RAG Me Up 存儲庫,它可以在使用 LLaMa2Lang 調整的模型之上使用。
pip install -r requirements.txt
# Translate OASST1 to target language
python translate.py m2m target_lang checkpoint_location
# Combine the checkpoint files into a dataset
python combine_checkpoints.py input_folder output_location
# Finetune
python finetune.py tuned_model dataset_name instruction_prompt
# Optionally finetune with DPO (RLHF)
python finetune_dpo.py tuned_model dataset_name instruction_prompt
# Run inference
python run_inference.py model_name instruction_prompt input
我們為特定語言調整基礎模型(例如 LLaMa3)的流程如下:
以下內容已經過測試,但可能還有更多內容可以使用
上述過程可以在免費的Google Colab T4 GPU上完全運作。然而,最後一步只能在足夠短的上下文視窗和最多2 個批次的情況下才能成功運行。應分多個步驟運行想要堅持使用免費的 Google Colab GPU。
我們的第 5 步微調模型是使用vast.ai 上的 A40 執行的,每個模型的成本不到一美元,大約在 1.5 小時內完成。
確保 pytorch 已安裝並適用於您的環境(最好使用 CUDA):https://pytorch.org/get-started/locally/
克隆存儲庫並安裝要求。
pip install -r requirements.txt
usage: translate.py [-h] [--quant8] [--quant4] [--base_dataset BASE_DATASET] [--base_dataset_text_field BASE_DATASET_TEXT_FIELD] [--base_dataset_lang_field BASE_DATASET_LANG_FIELD]
[--checkpoint_n CHECKPOINT_N] [--batch_size BATCH_SIZE] [--max_length MAX_LENGTH] [--cpu] [--source_lang SOURCE_LANG]
{opus,mbart,madlad,m2m,nllb,seamless_m4t_v2,towerinstruct} ... target_lang checkpoint_location
Translate an instruct/RLHF dataset to a given target language using a variety of translation models
positional arguments:
{opus,mbart,madlad,m2m,nllb,seamless_m4t_v2,towerinstruct}
The model/architecture used for translation.
opus Translate the dataset using HelsinkiNLP OPUS models.
mbart Translate the dataset using mBART.
madlad Translate the dataset using Google's MADLAD models.
m2m Translate the dataset using Facebook's M2M models.
nllb Translate the dataset using Facebook's NLLB models.
seamless_m4t_v2 Translate the dataset using Facebook's SeamlessM4T-v2 multimodal models.
towerinstruct Translate the dataset using Unbabel's Tower Instruct. Make sure your target language is in the 10 languages supported by the model.
target_lang The target language. Make sure you use language codes defined by the translation model you are using.
checkpoint_location The folder the script will write (JSONized) checkpoint files to. Folder will be created if it doesn't exist.
options:
-h, --help show this help message and exit
--quant8 Optional flag to load the translation model in 8 bits. Decreases memory usage, increases running time
--quant4 Optional flag to load the translation model in 4 bits. Decreases memory usage, increases running time
--base_dataset BASE_DATASET
The base dataset to translate, defaults to OpenAssistant/oasst1
--base_dataset_text_field BASE_DATASET_TEXT_FIELD
The base dataset's column name containing the actual text to translate. Defaults to text
--base_dataset_lang_field BASE_DATASET_LANG_FIELD
The base dataset's column name containing the language the source text was written in. Defaults to lang
--checkpoint_n CHECKPOINT_N
An integer representing how often a checkpoint file will be written out. To start off, 400 is a reasonable number.
--batch_size BATCH_SIZE
The batch size for a single translation model. Adjust based on your GPU capacity. Default is 10.
--max_length MAX_LENGTH
How much tokens to generate at most. More tokens might be more accurate for lengthy input but creates a risk of running out of memory. Default is unlimited.
--cpu Forces usage of CPU. By default GPU is taken if available.
--source_lang SOURCE_LANG
Source language to select from OASST based on lang property of dataset
如果您想要不同翻譯模型的更多參數,請執行:
python translate.py [MODEL] -h
在指定上面清單中的通用參數之前,請務必先指定特定於型號的參數。呼叫範例:
# Using M2M with 4bit quantization and differen batch sizes to translate Dutch
python translate.py m2m nl ./output_nl --quant4 --batch_size 20
# Using madlad 7B with 8bit quantization for German with different max_length
python translate.py madlad --model_size 7b de ./output_de --quant8 --batch_size 5 --max_length 512
# Be sure to use target language codes that the model you use understands
python translate.py mbart xh_ZA ./output_xhosa
python translate.py nllb nld_Latn ./output_nl
HF_TOKEN
環境變數。 usage: combine_checkpoints.py [-h] input_folder output_location
Combine checkpoint files from translation.
positional arguments:
input_folder The checkpoint folder used in translation, with the target language appended.
Example: "./output_nl".
output_location Where to write the Huggingface Dataset. Can be a disk location or a Huggingface
Dataset repository.
options:
-h, --help show this help message and exit
usage: finetune.py [-h] [--base_model BASE_MODEL] [--base_dataset_text_field BASE_DATASET_TEXT_FIELD] [--base_dataset_rank_field BASE_DATASET_RANK_FIELD] [--base_dataset_id_field BASE_DATASET_ID_FIELD] [--base_dataset_parent_field BASE_DATASET_PARENT_FIELD]
[--base_dataset_role_field BASE_DATASET_ROLE_FIELD] [--quant8] [--noquant] [--max_seq_length MAX_SEQ_LENGTH] [--num_train_epochs NUM_TRAIN_EPOCHS] [--batch_size BATCH_SIZE] [--threads_output_name THREADS_OUTPUT_NAME] [--thread_template THREAD_TEMPLATE]
[--padding PADDING]
tuned_model dataset_name instruction_prompt
Finetune a base instruct/chat model using (Q)LoRA and PEFT
positional arguments:
tuned_model The name of the resulting tuned model.
dataset_name The name of the dataset to use for fine-tuning. This should be the output of the combine_checkpoints script.
instruction_prompt An instruction message added to every prompt given to the chatbot to force it to answer in the target language. Example: "You are a generic chatbot that always answers in English."
options:
-h, --help show this help message and exit
--base_model BASE_MODEL
The base foundation model. Default is "NousResearch/Meta-Llama-3-8B-Instruct".
--base_dataset_text_field BASE_DATASET_TEXT_FIELD
The dataset's column name containing the actual text to translate. Defaults to text
--base_dataset_rank_field BASE_DATASET_RANK_FIELD
The dataset's column name containing the rank of an answer given to a prompt. Defaults to rank
--base_dataset_id_field BASE_DATASET_ID_FIELD
The dataset's column name containing the id of a text. Defaults to message_id
--base_dataset_parent_field BASE_DATASET_PARENT_FIELD
The dataset's column name containing the parent id of a text. Defaults to parent_id
--base_dataset_role_field BASE_DATASET_ROLE_FIELD
The dataset's column name containing the role of the author of the text (eg. prompter, assistant). Defaults to role
--quant8 Finetunes the model in 8 bits. Requires more memory than the default 4 bit.
--noquant Do not quantize the finetuning. Requires more memory than the default 4 bit and optional 8 bit.
--max_seq_length MAX_SEQ_LENGTH
The maximum sequence length to use in finetuning. Should most likely line up with your base model's default max_seq_length. Default is 512.
--num_train_epochs NUM_TRAIN_EPOCHS
Number of epochs to use. 2 is default and has been shown to work well.
--batch_size BATCH_SIZE
The batch size to use in finetuning. Adjust to fit in your GPU vRAM. Default is 4
--threads_output_name THREADS_OUTPUT_NAME
If specified, the threads created in this script for finetuning will also be saved to disk or HuggingFace Hub.
--thread_template THREAD_TEMPLATE
A file containing the thread template to use. Default is threads/template_fefault.txt
--padding PADDING What padding to use, can be either left or right.
6.1 [可選] 使用 DPO 進行微調(類似 RLHF)
usage: finetune_dpo.py [-h] [--base_model BASE_MODEL] [--base_dataset_text_field BASE_DATASET_TEXT_FIELD] [--base_dataset_rank_field BASE_DATASET_RANK_FIELD] [--base_dataset_id_field BASE_DATASET_ID_FIELD] [--base_dataset_parent_field BASE_DATASET_PARENT_FIELD] [--quant8]
[--noquant] [--max_seq_length MAX_SEQ_LENGTH] [--max_prompt_length MAX_PROMPT_LENGTH] [--num_train_epochs NUM_TRAIN_EPOCHS] [--batch_size BATCH_SIZE] [--threads_output_name THREADS_OUTPUT_NAME] [--thread_template THREAD_TEMPLATE] [--max_steps MAX_STEPS]
[--padding PADDING]
tuned_model dataset_name instruction_prompt
Finetune a base instruct/chat model using (Q)LoRA and PEFT using DPO (RLHF)
positional arguments:
tuned_model The name of the resulting tuned model.
dataset_name The name of the dataset to use for fine-tuning. This should be the output of the combine_checkpoints script.
instruction_prompt An instruction message added to every prompt given to the chatbot to force it to answer in the target language. Example: "You are a generic chatbot that always answers in English."
options:
-h, --help show this help message and exit
--base_model BASE_MODEL
The base foundation model. Default is "NousResearch/Meta-Llama-3-8B-Instruct".
--base_dataset_text_field BASE_DATASET_TEXT_FIELD
The dataset's column name containing the actual text to translate. Defaults to text
--base_dataset_rank_field BASE_DATASET_RANK_FIELD
The dataset's column name containing the rank of an answer given to a prompt. Defaults to rank
--base_dataset_id_field BASE_DATASET_ID_FIELD
The dataset's column name containing the id of a text. Defaults to message_id
--base_dataset_parent_field BASE_DATASET_PARENT_FIELD
The dataset's column name containing the parent id of a text. Defaults to parent_id
--quant8 Finetunes the model in 8 bits. Requires more memory than the default 4 bit.
--noquant Do not quantize the finetuning. Requires more memory than the default 4 bit and optional 8 bit.
--max_seq_length MAX_SEQ_LENGTH
The maximum sequence length to use in finetuning. Should most likely line up with your base model's default max_seq_length. Default is 512.
--max_prompt_length MAX_PROMPT_LENGTH
The maximum length of the prompts to use. Default is 512.
--num_train_epochs NUM_TRAIN_EPOCHS
Number of epochs to use. 2 is default and has been shown to work well.
--batch_size BATCH_SIZE
The batch size to use in finetuning. Adjust to fit in your GPU vRAM. Default is 4
--threads_output_name THREADS_OUTPUT_NAME
If specified, the threads created in this script for finetuning will also be saved to disk or HuggingFace Hub.
--thread_template THREAD_TEMPLATE
A file containing the thread template to use. Default is threads/template_fefault.txt
--max_steps MAX_STEPS
The maximum number of steps to run DPO for. Default is -1 which will run the data through fully for the number of epochs but this will be very time-consuming.
--padding PADDING What padding to use, can be either left or right.
6.1 [可選] 使用 ORPO 進行微調(類似 RLHF)
usage: finetune_orpo.py [-h] [--base_model BASE_MODEL] [--base_dataset_text_field BASE_DATASET_TEXT_FIELD] [--base_dataset_rank_field BASE_DATASET_RANK_FIELD] [--base_dataset_id_field BASE_DATASET_ID_FIELD] [--base_dataset_parent_field BASE_DATASET_PARENT_FIELD] [--quant8]
[--noquant] [--max_seq_length MAX_SEQ_LENGTH] [--max_prompt_length MAX_PROMPT_LENGTH] [--num_train_epochs NUM_TRAIN_EPOCHS] [--batch_size BATCH_SIZE] [--threads_output_name THREADS_OUTPUT_NAME] [--thread_template THREAD_TEMPLATE] [--max_steps MAX_STEPS]
[--padding PADDING]
tuned_model dataset_name instruction_prompt
Finetune a base instruct/chat model using (Q)LoRA and PEFT using ORPO (RLHF)
positional arguments:
tuned_model The name of the resulting tuned model.
dataset_name The name of the dataset to use for fine-tuning. This should be the output of the combine_checkpoints script.
instruction_prompt An instruction message added to every prompt given to the chatbot to force it to answer in the target language. Example: "You are a generic chatbot that always answers in English."
options:
-h, --help show this help message and exit
--base_model BASE_MODEL
The base foundation model. Default is "NousResearch/Meta-Llama-3-8B-Instruct".
--base_dataset_text_field BASE_DATASET_TEXT_FIELD
The dataset's column name containing the actual text to translate. Defaults to text
--base_dataset_rank_field BASE_DATASET_RANK_FIELD
The dataset's column name containing the rank of an answer given to a prompt. Defaults to rank
--base_dataset_id_field BASE_DATASET_ID_FIELD
The dataset's column name containing the id of a text. Defaults to message_id
--base_dataset_parent_field BASE_DATASET_PARENT_FIELD
The dataset's column name containing the parent id of a text. Defaults to parent_id
--quant8 Finetunes the model in 8 bits. Requires more memory than the default 4 bit.
--noquant Do not quantize the finetuning. Requires more memory than the default 4 bit and optional 8 bit.
--max_seq_length MAX_SEQ_LENGTH
The maximum sequence length to use in finetuning. Should most likely line up with your base model's default max_seq_length. Default is 512.
--max_prompt_length MAX_PROMPT_LENGTH
The maximum length of the prompts to use. Default is 512.
--num_train_epochs NUM_TRAIN_EPOCHS
Number of epochs to use. 2 is default and has been shown to work well.
--batch_size BATCH_SIZE
The batch size to use in finetuning. Adjust to fit in your GPU vRAM. Default is 4
--threads_output_name THREADS_OUTPUT_NAME
If specified, the threads created in this script for finetuning will also be saved to disk or HuggingFace Hub.
--thread_template THREAD_TEMPLATE
A file containing the thread template to use. Default is threads/template_fefault.txt
--max_steps MAX_STEPS
The maximum number of steps to run ORPO for. Default is -1 which will run the data through fully for the number of epochs but this will be very time-consuming.
--padding PADDING What padding to use, can be either left or right.
usage: run_inference.py [-h] model_name instruction_prompt input
Script to run inference on a tuned model.
positional arguments:
model_name The name of the tuned model that you pushed to Huggingface in the previous
step.
instruction_prompt An instruction message added to every prompt given to the chatbot to force
it to answer in the target language.
input The actual chat input prompt. The script is only meant for testing purposes
and exits after answering.
options:
-h, --help show this help message and exit
我如何知道要為我的目標語言選擇哪種翻譯模型?
我們為您提供了benchmark.py
腳本,該腳本有助於做出一些好的猜測(我們使用的資料集與訓練 OPUS 模型的資料集相同,因此結果總是有利於 OPUS)。有關使用方法,請參閱下面該腳本的說明。模型以 4 位元量化加載,並在 OPUS 書籍子集的小樣本上運行。
請務必使用基礎資料集中最常見的語言作為來源語言,並使用目標翻譯語言作為目標語言。例如,對於 OASST1,請確保至少執行en
和es
作為來源語言。
usage: benchmark.py [-h] [--cpu] [--start START] [--n N] [--max_length MAX_LENGTH] source_language target_language included_models
Benchmark all the different translation models for a specific source and target language to find out which performs best. This uses 4bit quantization to limit GPU usage. Note:
the outcomes are indicative - you cannot assume corretness of the BLEU and CHRF scores but you can compare models against each other relatively.
positional arguments:
source_language The source language you want to test for. Check your dataset to see which occur most prevalent or use English as a good start.
target_language The source language you want to test for. This should be the language you want to apply the translate script on. Note: in benchmark, we use 2-character
language codes, in constrast to translate.py where you need to specify whatever your model expects.
included_models Comma-separated list of models to include. Allowed values are: opus, m2m_418m, m2m_1.2b, madlad_3b, madlad_7b, madlad_10b, madlad_7bbt, mbart,
nllb_distilled600m, nllb_1.3b, nllb_distilled1.3b, nllb_3.3b, seamless
options:
-h, --help show this help message and exit
--cpu Forces usage of CPU. By default GPU is taken if available.
--start START The starting offset to include sentences from the OPUS books dataset from. Defaults to 0.
--n N The number of sentences to benchmark on. Defaults to 100.
--max_length MAX_LENGTH
How much tokens to generate at most. More tokens might be more accurate for lengthy input but creates a risk of running out of memory. Default is 512.
我們已經創建並將繼續創建大量資料集和模型。想幫助法學碩士的民主化嗎?克隆儲存庫並為其他語言建立資料集和模型,然後建立 PR。
荷蘭語理解Ling/oasst1_nl | 西班牙文理解Ling/oast1_es | 法文理解Ling/oast1_fr | 德語理解Ling/oast1_de |
加泰隆尼亞語 xaviviro/oasst1_ca | 葡萄牙語理解Ling/oasst1_pt | 阿拉伯語 HeshamHaroon/oasst-arabic | 義大利語理解Ling/oast1_it |
俄語理解Ling/oast1_ru | 印地語理解Ling/oasst1_hi | 中文理解Ling/oasst1_zh | 波蘭基督徒/oast1_pl |
日文理解Ling/oast1_jap | 巴斯克 xezpeleta/oasst1_eu | 孟加拉語 UnderstandingLing/oasst1_bn | 土耳其語理解Ling/oast1_tr |
在使用這些模型之前,請確保您有權存取 Meta 的 LLaMa3-8B 模型並設定您的 HF_TOKEN。
UnderstandingLing/Llama-3-8B-Instruct-nl 荷蘭語 | UnderstandingLing/Llama-3-8B-Instruct-es 西班牙語 | 了解Ling/Llama-3-8B-Instruct-fr 法語 | UnderstandingLing/Llama-3-8B-Instruct-de 德語 |
了解Ling/Llama-3-8B-Instruct-pt 葡萄牙語 | UnderstandingLing/Llama-3-8B-Instruct-it 義大利語 | UnderstandingLing/Llama-3-8B-Instruct-hi 印地語 | 了解Ling/Llama-3-8B-Instruct-ru 俄語 |
荷蘭語 UnderstandingLing/oasst1_nl_threads | 西班牙語 UnderstandingLing/oast1_es_threads | 法文理解Ling/oast1_fr_threads | 德語理解Ling/oast1_de_threads |
加泰隆尼亞語 xaviviro/oasst1_ca_threads | 葡萄牙語理解Ling/oast1_pt_threads | 阿拉伯語 HeshamHaroon/oasst-arabic_threads | 義大利語理解Ling/oast1_it_threads |
俄語理解Ling/oast1_ru_threads | 印地語理解Ling/oasst1_hi_threads | 中文理解Ling/oasst1_zh_threads | 波蘭基督徒/oasst1_pl_threads |
日文理解Ling/oast1_jap_threads | 巴斯克 xezpeleta/oasst1_eu_threads | 孟加拉語 UnderstandingLing/oasst1_bn_threads | 土耳其語 UnderstandingLing/oast1_tr_threads |
了解Ling/llama-2-7b-chat-nl 荷蘭語 | 了解Ling/llama-2-7b-chat-es 西班牙語 | 了解Ling/llama-2-7b-chat-fr 法語 | 了解Ling/llama-2-7b-chat-de 德語 |
xaviviro/llama-2-7b-chat-a 加泰隆尼亞語 | 了解Ling/llama-2-7b-chat-pt 葡萄牙語 | HeshamHaroon/llama-2-7b-chat-ar 阿拉伯語 | 了解Ling/llama-2-7b-chat-it 義大利語 |
了解Ling/llama-2-7b-chat-ru 俄語 | 了解Ling/llama-2-7b-chat-hi 印地語 | 了解Ling/llama-2-7b-chat-zh 中文 | chrystians/llama-2-7b-chat-pl-polish-polski 波蘭語 |
xezpeleta/llama-2-7b-chat-eu 巴斯克語 | UnderstandingLing/llama-2-7b-chat-bn 孟加拉語 | 了解Ling/llama-2-7b-chat-tr 土耳其語 |
UnderstandingLing/Mistral-7B-Instruct-v0.2-nl 荷蘭語 | UnderstandingLing/Mistral-7B-Instruct-v0.2-es 西班牙語 | UnderstandingLing/Mistral-7B-Instruct-v0.2-de 德語 |
了解Ling/llama-2-13b-chat-nl 荷蘭語 | 了解Ling/llama-2-13b-chat-es 西班牙語 | 了解Ling/llama-2-13b-chat-fr 法語 |
UnderstandingLing/Mixtral-8x7B-Instruct-nl 荷蘭語 |
[INST] <
[INST] <[INST] Hoeveel inwoners heeft die stad? [/INST] 850 duizend inwoners (2023)
[INST] <[INST] Hoeveel inwoners heeft die stad? [/INST] 850 duizend inwoners (2023)[INST] In welke provincie ligt die stad? [/INST] In de provincie Noord-Holland
[INST] <
Q:為什麼要先翻譯完整的 OASST1/2 資料集?只翻譯排名最高的線程不是更快嗎?
答:雖然透過先創建線程然後翻譯它們可以在吞吐時間方面獲得相當多的收益,但我們向社區提供完整的 OASST1/2 翻譯,因為我們相信它們本身就很有用。
Q:與原版 LLaMa3 相比,微調的表現如何?
答:雖然我們沒有正式的基準,但讓 LLaMa3 始終說英語以外的另一種語言即使不是不可能,也是具有挑戰性的。它產生的非英語語言通常在語法上是錯誤的。我們的微調並沒有表現出這種行為。
Q:我可以使用其他框架來微調嗎?
答:是的,可以,我們使用 Axolotl 進行多 GPU 設定上的訓練。
Q:我可以混合使用不同的翻譯模型嗎?
答:當然,我們認為透過多個模型完成翻譯甚至可能會提高效能。您可以透過提前停止翻譯並透過使用不同的翻譯模型重新執行翻譯腳本從檢查點繼續來實現此目的。
我們正在積極尋找資金來實現人工智慧的民主化並推進其應用。如果您想投資,請透過 [email protected] 與我們聯繫。