Ce référentiel contient des scripts pratiques pour affiner LLaMa3-8B (ou tout autre modèle de base) pour discuter dans n'importe quelle langue (autre que l'anglais). La raison derrière cela est que LLaMa3 est formé principalement sur des données anglaises et bien qu'il fonctionne dans une certaine mesure pour d'autres langues, ses performances sont médiocres par rapport à l'anglais.
Combinez la puissance du réglage fin avec la puissance de RAG - consultez notre référentiel RAG Me Up sur RAG qui peut être utilisé sur vos modèles réglés avec 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
Le processus que nous suivons pour régler un modèle de base tel que LLaMa3 pour un langage spécifique est le suivant :
Les éléments suivants ont été testés mais d'autres fonctionneront potentiellement
Le processus ci-dessus peut être entièrement exécuté sur un GPU Google Colab T4 gratuit. Cependant, la dernière étape ne peut être exécutée avec succès qu'avec des fenêtres contextuelles suffisamment courtes et un lot d'au plus 2. De plus, la traduction à l'étape 2 prend environ 36 heures au total pour une langue donnée et doit donc être exécutée en plusieurs étapes si vous vous souhaitez vous en tenir à un GPU Google Colab gratuit.
Nos modèles affinés pour l'étape 5 ont été réalisés à l'aide d'un A40 sur Vast.ai et nous ont coûté moins d'un dollar pour chaque modèle, en 1,5 heure environ.
Assurez-vous que pytorch est installé et fonctionne pour votre environnement (utilisation de CUDA préférable) : https://pytorch.org/get-started/locally/
Clonez le dépôt et installez les exigences.
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
Si vous souhaitez plus de paramètres pour les différents modèles de traduction, exécutez :
python translate.py [MODEL] -h
Assurez-vous de spécifier d'abord les paramètres spécifiques au modèle avant de spécifier les paramètres communs de la liste ci-dessus. Exemples d'appels :
# 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
est configurée conformément à la documentation. 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 [OPTIONNEL] Réglage fin à l'aide de DPO (similaire à 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 [OPTIONNEL] Réglage fin à l'aide d'ORPO (similaire à 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
Comment savoir quel modèle de traduction choisir pour ma langue cible ?
Nous vous proposons notre script benchmark.py
qui permet de faire une bonne estimation (l'ensemble de données que nous utilisons est le même que celui sur lequel les modèles OPUS sont formés, donc les résultats sont toujours favorables à OPUS). Pour l'utilisation, consultez l'aide de ce script ci-dessous. Les modèles sont chargés en quantification 4 bits et exécutés sur un petit échantillon du sous-ensemble de livres OPUS.
Assurez-vous d'utiliser les langues les plus courantes dans votre ensemble de données de base comme langue_source et votre langue de traduction cible comme langue_cible. Pour OASST1 par exemple, assurez-vous d'exécuter au moins en
et es
comme langues sources.
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.
Nous avons déjà créé et continuerons de créer de nombreux ensembles de données et modèles. Vous voulez contribuer à démocratiser les LLM ? Clonez le dépôt et créez des ensembles de données et des modèles pour d'autres langages, puis créez un PR.
Comprendre le néerlandais/oasst1_nl | Compréhension de l'espagnol/oasst1_es | Français ComprendreLing/oasst1_fr | Comprendre l'allemand/oasst1_de |
Catalan xaviviro/oasst1_ca | Comprendre le portugaisLing/oasst1_pt | Arabe HeshamHaroon/oasst-arabe | Compréhension de l'italien/oasst1_it |
Comprendre le russeLing/oasst1_ru | Comprendre l'hindiLing/oasst1_hi | Compréhension du chinoisLing/oasst1_zh | christiens polonais/oasst1_pl |
Compréhension du japonaisLing/oasst1_jap | Xezpeleta basque/oasst1_eu | Bengali UnderstandingLing/oasst1_bn | Comprendre le turcLing/oasst1_tr |
Assurez-vous d'avoir accès au modèle LLaMa3-8B de Meta et définissez votre HF_TOKEN avant d'utiliser ces modèles.
UnderstandLing/Llama-3-8B-Instruct-nl néerlandais | Comprendre/Llama-3-8B-Instruct-es Espagnol | UnderstandLing/Llama-3-8B-Instruct-fr Français | UnderstandingLing/Llama-3-8B-Instruct-de Allemand |
UnderstandLing/Llama-3-8B-Instruct-pt Portugais | Comprendre/Llama-3-8B-Instruct-it italien | Comprendre/Llama-3-8B-Instruct-salut hindi | UnderstandingLing/Llama-3-8B-Instruct-ru russe |
Néerlandais UnderstandingLing/oasst1_nl_threads | Espagnol UnderstandingLing/oasst1_es_threads | Français ComprendreLing/oasst1_fr_threads | Comprendre l'allemand/oasst1_de_threads |
Catalan xaviviro/oasst1_ca_threads | Portugais UnderstandingLing/oasst1_pt_threads | Arabe HeshamHaroon/oasst-arabic_threads | Comprendre l'italien/oasst1_it_threads |
Comprendre le russe/oasst1_ru_threads | Hindi UnderstandingLing/oasst1_hi_threads | Comprendre le chinoisLing/oasst1_zh_threads | Chrystiens polonais/oasst1_pl_threads |
Comprendre le japonais/oasst1_jap_threads | Xezpeleta basque/oasst1_eu_threads | Bengali UnderstandingLing/oasst1_bn_threads | Comprendre le turc/oasst1_tr_threads |
ComprendreLing/llama-2-7b-chat-nl néerlandais | ComprendreLing/llama-2-7b-chat-es espagnol | UnderstandLing/llama-2-7b-chat-fr Français | ComprendreLing/llama-2-7b-chat-de allemand |
xaviviro/llama-2-7b-chat-ca catalan | ComprendreLing/llama-2-7b-chat-pt portugais | HeshamHaroon/llama-2-7b-chat-ar arabe | ComprendreLing/llama-2-7b-chat-it italien |
ComprendreLing/llama-2-7b-chat-ru russe | ComprendreLing/llama-2-7b-chat-salut hindi | ComprendreLing/llama-2-7b-chat-zh chinois | chrystians/llama-2-7b-chat-pl-polish-polski polonais |
xezpeleta/llama-2-7b-chat-eu basque | ComprendreLing/llama-2-7b-chat-bn Bengali | ComprendreLing/llama-2-7b-chat-tr turc |
UnderstandLing/Mistral-7B-Instruct-v0.2-nl Néerlandais | UnderstandLing/Mistral-7B-Instruct-v0.2-es espagnol | UnderstandingLing/Mistral-7B-Instruct-v0.2-de allemand |
ComprendreLing/llama-2-13b-chat-nl néerlandais | UnderstandingLing/llama-2-13b-chat-es espagnol | UnderstandLing/llama-2-13b-chat-fr Français |
UnderstandLing/Mixtral-8x7B-Instruct-nl Néerlandais |
<s>[INST] <<SYS>> Je bent een generieke chatbot die altijd in het Nederlands antwoord geeft. <</SYS>> Wat is de hoofdstad van Nederland? [/INST] Amsterdam</s>
<s>[INST] <<SYS>> Je bent een generieke chatbot die altijd in het Nederlands antwoord geeft. <</SYS>> Wat is de hoofdstad van Nederland? [/INST] Amsterdam</s><s>[INST] Hoeveel inwoners heeft die stad? [/INST] 850 duizend inwoners (2023)</s>
<s>[INST] <<SYS>> Je bent een generieke chatbot die altijd in het Nederlands antwoord geeft. <</SYS>> Wat is de hoofdstad van Nederland? [/INST] Amsterdam</s><s>[INST] Hoeveel inwoners heeft die stad? [/INST] 850 duizend inwoners (2023)</s><s>[INST] In welke provincie ligt die stad? [/INST] In de provincie Noord-Holland</s>
<s>[INST] <<SYS>> Je bent een generieke chatbot die altijd in het Nederlands antwoord geeft. <</SYS>> Wie is de minister-president van Nederland? [/INST] Mark Rutte is sinds 2010 minister-president van Nederland. Hij is meerdere keren herkozen.</s>
Q : Pourquoi traduisez-vous d'abord l'ensemble de données OASST1/2 complet ? Ne serait-il pas plus rapide de traduire uniquement les sujets les mieux classés ?
R : Bien que vous puissiez gagner beaucoup en termes de temps de traitement en créant d'abord les fils de discussion, puis en les traduisant, nous fournissons des traductions OASST1/2 complètes à la communauté car nous pensons qu'elles peuvent être utiles en elles-mêmes.
Q : Dans quelle mesure les réglages fins fonctionnent-ils par rapport à Vanilla LLaMa3 ?
R : Bien que nous n'ayons pas de références formelles, faire en sorte que LLaMa3 parle systématiquement une autre langue que l'anglais est un défi, voire impossible. La langue non anglaise qu’il produit est souvent grammaticalement brisée. Nos réglages fins ne montrent pas ce comportement.
Q : Puis-je utiliser d’autres frameworks pour affiner les réglages ?
R : Oui, vous pouvez, nous utilisons Axolotl pour la formation sur les configurations multi-GPU.
Q : Puis-je mélanger différents modèles de traduction ?
R : Absolument, nous pensons que cela pourrait même augmenter les performances si la traduction était effectuée par plusieurs modèles. Vous pouvez y parvenir en arrêtant plus tôt une traduction et en continuant à partir des points de contrôle en réexécutant le script de traduction avec un modèle de traduction différent.
Nous recherchons activement des financements pour démocratiser l’IA et faire progresser ses applications. Contactez-nous à [email protected] si vous souhaitez investir.