
Dieses Projekt basiert auf Chinese-Llama-Alpaca V3.1 für Anweisungen. Chinesisch-Llama-Alpaca hat die chinesische Expansion und Verbesserung der chinesischen Basis auf der Grundlage von Lama entwickelt. Basierend auf dem ursprünglichen Lama erweiterte es die chinesische Vokabularliste und verwendete chinesische Daten für die sekundäre Vorausbildung, wodurch die grundlegende semantische Verständnisfähigkeit von Chinesen weiter verbessert wurde.
Projektzusammensetzung : <quellen>
.
├── README.md # 使用说明文件
├── SHA256.md # LLaMA模型SHA值对比文件
├── notebooks
│ ├── convert_and_quantize_chinese_alpaca_plus.ipynb
│ └── convert_and_quantize_chinese_llama.ipynb
├── requirements.txt # 依赖文件
└── scripts
├── chinese_sp.model # 中文词表文件
├── crawl_prompt.py # 1. 通过OpenAI的大模型(如ChatGPT、GPT4等)生成可用于微调的数据
├── inference_hf.py # 5. 对微调训练产生的LoRA模型和原始LLaMA模型做推理
├── merge_llama_with_chinese_lora.py # 4. 合并模型权重
├── merge_tokenizers.py # 2. 词表扩充
└── run_clm_pt_with_peft.py # 3. 对模型进行训练或者微调Unabhängig davon, ob Sie vor dem Training oder zur Feinabstimmung möchten, müssen Sie Daten erstellen. Es gibt zwei Möglichkeiten, Daten vorzubereiten:
scripts/crawl_prompt.py verwenden, um die entsprechenden Daten zu generieren. Die Grundidee besteht darin, ChatGPT oder andere openAI -effiziente Modelle für die Datenerzeugung zu verwenden. # tokenizer
wget https : // agi . gpt4 . org / llama / LLaMA / tokenizer . model - O . / tokenizer . model
wget https : // agi . gpt4 . org / llama / LLaMA / tokenizer_checklist . chk - O . / tokenizer_checklist . chk
# 7B
wget https : // agi . gpt4 . org / llama / LLaMA / 7 B / consolidated . 00. pth - O . / 7 B / consolidated . 00. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 7 B / params . json - O . / 7 B / params . json
wget https : // agi . gpt4 . org / llama / LLaMA / 7 B / checklist . chk - O . / 7 B / checklist . chk
# 13B
wget https : // agi . gpt4 . org / llama / LLaMA / 13 B / consolidated . 00. pth - O . / 13 B / consolidated . 00. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 13 B / consolidated . 01. pth - O . / 13 B / consolidated . 01. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 13 B / params . json - O . / 13 B / params . json
wget https : // agi . gpt4 . org / llama / LLaMA / 13 B / checklist . chk - O . / 13 B / checklist . chk
# 30B
wget https : // agi . gpt4 . org / llama / LLaMA / 30 B / consolidated . 00. pth - O . / 30 B / consolidated . 00. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 30 B / consolidated . 01. pth - O . / 30 B / consolidated . 01. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 30 B / consolidated . 02. pth - O . / 30 B / consolidated . 02. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 30 B / consolidated . 03. pth - O . / 30 B / consolidated . 03. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 30 B / params . json - O . / 30 B / params . json
wget https : // agi . gpt4 . org / llama / LLaMA / 30 B / checklist . chk - O . / 30 B / checklist . chk
# 65B
wget https : // agi . gpt4 . org / llama / LLaMA / 65 B / consolidated . 00. pth - O . / 65 B / consolidated . 00. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 65 B / consolidated . 01. pth - O . / 65 B / consolidated . 01. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 65 B / consolidated . 02. pth - O . / 65 B / consolidated . 02. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 65 B / consolidated . 03. pth - O . / 65 B / consolidated . 03. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 65 B / consolidated . 04. pth - O . / 65 B / consolidated . 04. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 65 B / consolidated . 05. pth - O . / 65 B / consolidated . 05. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 65 B / consolidated . 06. pth - O . / 65 B / consolidated . 06. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 65 B / consolidated . 07. pth - O . / 65 B / consolidated . 07. pth
wget https : // agi . gpt4 . org / llama / LLaMA / 65 B / params . json - O . / 65 B / params . json
wget https : // agi . gpt4 . org / llama / LLaMA / 65 B / checklist . chk - O . / 65 B / checklist . chkSie müssen die Lama -Modellgewichte verschiedener Größenparameter herunterladen. Je größer die Parameter, desto größer das Gewicht desto größer ist das Gewicht, desto besser die Genauigkeit und die Feinabstimmung und Trainingszeit sind ebenfalls relativ lang. Im Allgemeinen ist das 7B- oder 13B -Modell die Wahl der meisten Menschen.
Stellen Sie sicher, dass Sie die Integrität des Lama -Basismodells bestätigen und prüfen, ob es mit dem in SHA256.MD gezeigten Wert übereinstimmt, andernfalls kann der Zusammenführungsvorgang nicht durchgeführt werden.
# 安装依赖库
pip install git + https : // github . com / huggingface / transformers
# 转化HF权重
python - m transformers . models . llama . convert_llama_weights_to_hf
- - input_dir llama - weights
- - model_size 7 B
- - output_dir llama - hf - weights
> python - m transformers . models . llama . convert_llama_weights_to_hf - - input_dir . / - - model_size 7 B - - output_dir . / output / 7 B - hf Wenn Sie sich nicht manuell konvertieren möchten, können Sie auch das Lama-HF-Modell verwenden, das andere konvertiert haben. Pinkmanlove hat das Gewicht des umgebauten Lama-HF in Umarmung. Wenn es fehlschlägt, können Sie nach anderen Personen suchen, die Sie bei HuggingFace-Models gut konvertieren können.
Der gesamte Trainings- und Feinabstimmungsprozess besteht aus drei Schritten:
python scripts / merge_tokenizers . py
- - llama_tokenizer_dir llama_tokenizer_dir
- - chinese_sp_model_file chinese_sp_model_file
> python scripts / merge_tokenizers . py - - llama_tokenizer_dir output / 7 B - hf - - chinese_sp_model_file scripts / chinese_sp . modelParameterbeschreibung:
llama_tokenizer_dir : Zeigen Sie auf das Verzeichnis, in dem der ursprüngliche Lama -Tokenizer gespeichert ist;chinese_sp_model_file : Zeigen Sie auf die chinesische Vokabulardatei (chinesisch_sp.model) mit Satzstück;Notiz
Es gibt zwei Hauptmethoden, um die Vokabularliste zu erweitern: (1) die Liste der Wortschatze zusammenführen und erweitern; (2) eine große Vokabularliste finden und nutzlose Wörter löschen, um eine Vokabularliste zu erhalten.
Während der Vorausbildung wird der allgemeine chinesische Korpus verwendet, um auf der Grundlage der ursprünglichen Lama-Gewichte weiter vorzugeben. Der Prozess ist in zwei Phasen unterteilt:
Das Modell konvergiert langsam in der ersten Stufe der Vorausbildung. Wenn es nicht besonders viele Zeit- und Rechenressourcen gibt, wird empfohlen, diese Phase zu überspringen. Die zweite Stufe des Trainings vor dem Training ist wie folgt (Single-Player-Einzelkarte):
########参数设置########
lr = 2e-4
lora_rank = 8
lora_alpha = 32
lora_trainable = "q_proj,v_proj,k_proj,o_proj,gate_proj,down_proj,up_proj"
modules_to_save = "embed_tokens,lm_head"
lora_dropout = 0.05
pretrained_model = path / to / hf / llama / dir
chinese_tokenizer_path = path / to / chinese / llama / tokenizer / dir
dataset_dir = path / to / pt / data / dir
data_cache = temp_data_cache_dir
per_device_train_batch_size = 1
per_device_eval_batch_size = 1
training_steps = 100
gradient_accumulation_steps = 1
output_dir = output_dir
deepspeed_config_file = ds_zero2_no_offload . json
########启动命令########
torchrun - - nnodes 1 - - nproc_per_node 1 run_clm_pt_with_peft . py
- - deepspeed ${ deepspeed_config_file }
- - model_name_or_path ${ pretrained_model }
- - tokenizer_name_or_path ${ chinese_tokenizer_path }
- - dataset_dir ${ dataset_dir }
- - data_cache_dir ${ data_cache }
- - validation_split_percentage 0.001
- - per_device_train_batch_size ${ per_device_train_batch_size }
- - per_device_eval_batch_size ${ per_device_eval_batch_size }
- - do_train
- - seed $ RANDOM
- - fp16
- - max_steps ${ training_steps }
- - lr_scheduler_type cosine
- - learning_rate ${ lr }
- - warmup_ratio 0.05
- - weight_decay 0.01
- - logging_strategy steps
- - logging_steps 10
- - save_strategy steps
- - save_total_limit 3
- - save_steps 500
- - gradient_accumulation_steps ${ gradient_accumulation_steps }
- - preprocessing_num_workers 8
- - block_size 512
- - output_dir ${ output_dir }
- - overwrite_output_dir
- - ddp_timeout 30000
- - logging_first_step True
- - lora_rank ${ lora_rank }
- - lora_alpha ${ lora_alpha }
- - trainable ${ lora_trainable }
- - modules_to_save ${ modules_to_save }
- - lora_dropout ${ lora_dropout }
- - torch_dtype float16
- - gradient_checkpointing
- - ddp_find_unused_parameters FalseParameterbeschreibung:
--model_name_or_path : Das Verzeichnis, in dem sich das ursprüngliche HF-Format-Lama-Modell befindet;--tokenizer_name_or_path : Das Verzeichnis, in dem sich Chinese-Llama-Tokenizer befindet (Ergebnis von merge_tokenizers.py-Synthese);--dataset_dir : Ein Verzeichnis mit vorgebliebenen Daten, das mehrere in TXT enden mehrere Klartextdateien enthalten kann;--data_cache_dir : Geben Sie ein Verzeichnis an, in dem Datencache-Dateien gespeichert werden.Mehrere Maschinen und mehrere Karten:
torchrun
- - nnodes ${ num_nodes }
- - nproc_per_node ${ num_gpu_per_node }
- - node_rank ${ node_rank }
- - master_addr ${ master_addr }
- - master_port ${ master_port }
run_clm_pt_with_peft . py
...Das chinesische Lama-Modell hat die chinesische Vokabularliste basierend auf der Originalversion erweitert und chinesische allgemeine Klartextdaten für die sekundäre Vorausbildung verwendet. Hier bietet der Autor zwei Möglichkeiten, diese Gewichte vor dem Training herunterzuladen, ohne dass wir Ressourcen für die Schulung ausgeben müssen:
| Modellname | Trainingsdaten | Das Modell neu aufstellen | Größe | Lora Download |
|---|---|---|---|---|
| Chinese-Llama-7b | General 20g | Original Lama-7b | 770 m | [Baidu NetDisk] [Google Drive] |
| Chinese-Llama-plus-7b ️ | Allgemeiner Zweck 120g | Original Lama-7b | 790 m | [Baidu NetDisk] [Google Drive] |
| Chinese-Llama-13b | General 20g | Original Lama-13b | 1g | [Baidu NetDisk] [Google Drive] |
| Chinese-Llama-plus-13b ️ | Allgemeiner Zweck 120g | Original Lama-13b | 1g | [Baidu NetDisk] [Google Drive] |
.from_pretrained() angegebenen Modellnamen.| Modellname | Modellanrufname | Link |
|---|---|---|
| Chinese-Llama-7b | Ziqingyang/chinesisch-llama-lora-7b | Modell Hub Link |
| Chinese-Llama-plus-7b | Ziqingyang/chinesisch-llama-plus-lora-7b | Modell Hub Link |
| Chinese-Llama-13b | Ziqingyang/chinesisch-llama-lora-13b | Modell Hub Link |
| Chinese-Llama-plus-13b | Ziqingyang/chinesisch-llama-plus-lora-13b | Modell Hub Link |
Das Trainingsschema verwendet auch LORA für eine effiziente Feinanpassung und erhöht die Anzahl der trainierbaren Parameter weiter.
Single-Player-Einzelkarte:
########参数部分########
lr = 1e-4
lora_rank = 8
lora_alpha = 32
lora_trainable = "q_proj,v_proj,k_proj,o_proj,gate_proj,down_proj,up_proj"
modules_to_save = "embed_tokens,lm_head"
lora_dropout = 0.05
pretrained_model = path / to / hf / llama / or / merged / llama / dir / or / model_id
chinese_tokenizer_path = path / to / chinese / llama / tokenizer / dir
dataset_dir = path / to / sft / data / dir
per_device_train_batch_size = 1
per_device_eval_batch_size = 1
training_steps = 100
gradient_accumulation_steps = 1
output_dir = output_dir
peft_model = path / to / peft / model / dir
validation_file = validation_file_name
deepspeed_config_file = ds_zero2_no_offload . json
########启动命令########
torchrun - - nnodes 1 - - nproc_per_node 1 run_clm_sft_with_peft . py
- - deepspeed ${ deepspeed_config_file }
- - model_name_or_path ${ pretrained_model }
- - tokenizer_name_or_path ${ chinese_tokenizer_path }
- - dataset_dir ${ dataset_dir }
- - validation_split_percentage 0.001
- - per_device_train_batch_size ${ per_device_train_batch_size }
- - per_device_eval_batch_size ${ per_device_eval_batch_size }
- - do_train
- - do_eval
- - seed $ RANDOM
- - fp16
- - max_steps ${ training_steps }
- - lr_scheduler_type cosine
- - learning_rate ${ lr }
- - warmup_ratio 0.03
- - weight_decay 0
- - logging_strategy steps
- - logging_steps 10
- - save_strategy steps
- - save_total_limit 3
- - evaluation_strategy steps
- - eval_steps 250
- - save_steps 500
- - gradient_accumulation_steps ${ gradient_accumulation_steps }
- - preprocessing_num_workers 8
- - max_seq_length 512
- - output_dir ${ output_dir }
- - overwrite_output_dir
- - ddp_timeout 30000
- - logging_first_step True
- - lora_rank ${ lora_rank }
- - lora_alpha ${ lora_alpha }
- - trainable ${ lora_trainable }
- - modules_to_save ${ modules_to_save }
- - lora_dropout ${ lora_dropout }
- - torch_dtype float16
- - validation_file ${ validation_file }
- - peft_path ${ peft_model }
- - gradient_checkpointing
- - ddp_find_unused_parameters FalseParameterbeschreibung:
--tokenizer_name_or_path : Das Verzeichnis, in dem sich Chinese-Alpaca-Tokenizer befindet (Ergebnis von merge_tokenizers.py-Synthese);--dataset_dir : Ein Verzeichnis für die Feinabstimmung von Anweisungen, die einen oder mehrere Befehlsfeindatendateien im Stanford Alpaca-Format in JSON enden;--validation_file : Eine einzelne Befehlsfeineinstellungsdatei, die als Validierungssatz verwendet wird, das in JSON endet, folgt auch dem Stanford Alpaca-Format.Das sogenannte Stanford Alpaca-Format lautet:
[
{ "instruction" : ...,
"input" : ...,
"output" : ... },
...
]Die Daten hier können auch mit chinesisch-llama-alpaca-usage-/# Vorbereitungsmethoden erstellt werden.
Konfigurationsanweisungen:
Wenn Sie die Lora-Gewichte des chinesischen Alpaka-Modells weiter ausbilden möchten:
--model_name_or_path : Das ursprüngliche HF-Format-Lama-Modell (wenn Sie weiterhin Nicht-Plus-Alpaka-Modelle trainieren) oder chinesisch-llama-plus-lora zusammenführen (wenn Sie weiterhin Plus-Modelle trainieren);--peft_path : Lora-Gewichtsverzeichnis des Chinese-Alpakas; Keine Notwendigkeit, --lora_rank , --lora_alpha , --lora_dropout , --trainable und - --modules_to_save -Parameter anzugeben.
Wenn Sie Lora-Gewichte basierend auf dem chinesischen Training für das Chinesisch-Llama fein abstellen möchten:
--model_name_or_path : Fusion des HF-Formates Chinese-Llama-Modell nach chinesisch-llama-lora (unabhängig davon, ob es sich um ein Plus-Modell handelt oder nicht);--peft_path : Geben Sie diesen Parameter nicht an und löschen Sie --peft_path aus dem Skript; --lora_rank , --lora_alpha , --lora_dropout , --trainable und --modules_to_save -Parameter müssen angegeben werden.
Mehrere Maschinen und mehrere Karten:
torchrun
- - nnodes ${ num_nodes }
- - nproc_per_node ${ num_gpu_per_node }
- - node_rank ${ node_rank }
- - master_addr ${ master_addr }
- - master_port ${ master_port }
run_clm_sft_with_peft . py
...Geeignet für Chinese-Llama, Chinese-Llama-Plus, Chinese-Alpaca
python scripts / merge_llama_with_chinese_lora . py
- - base_model path_to_original_llama_hf_dir
- - lora_model path_to_chinese_llama_or_alpaca_lora
- - output_type [ pth | huggingface ]
- - output_dir path_to_output_dir Parameterbeschreibung:
--base_model : Das Verzeichnis, in dem Lama-Modellgewichte und Konfigurationsdateien im HF-Format gespeichert sind;--lora_model : Das Verzeichnis, in dem die Datei in chinesischen Lama/Alpaca Lora dekomprimiert wird, können Sie auch den Modell-Call-Namen des Modells verwenden.--output_type : Gibt das Ausgangsformat an, das pth oder huggingface sein kann. Wenn nicht angegeben, ist der Standardpth pth ;--output_dir : Gibt das Verzeichnis an, das die vollständigen Modellgewichte spart, die Standardeinstellung ist ./ ;--offload_dir : Für Benutzer mit niedrigem Memory müssen Sie einen Offload-Cache-Pfad angeben. Weitere Erläuterung zu output_type :
.pth -Dateien können zur Quantisierung und Bereitstellung von: lama.cpp -Tools verwendet werden;.bin -Dateien können verwendet werden für: Transformatoren für die Argumentation; Text-Generation-Webui für den Bau von Schnittstellen;Eine einzelne LORA -Gewichtsabfertigung wird online durchgeführt und quantifiziert sie gleichzeitig:
Das Zusammenführen von Chinese-Alpaca-Plus erfordert zwei Lora-Gewichte, nämlich chinesisch-llama-plus-lora und chinesisch-alpaca-plus-lora.
python scripts / merge_llama_with_chinese_lora . py
- - base_model path_to_original_llama_hf_dir
- - lora_model path_to_chinese_llama_plus_lora , path_to_chinese_alpaca_plus_lora
- - output_type [ pth | huggingface ]
- - output_dir path_to_output_dir Die Multi-Lora-Gewichtsbeziehung wird online durchgeführt und quantifiziert sie gleichzeitig:
CUDA_VISIBLE_DEVICES = 0 python scripts / inference_hf . py
- - base_model path_to_original_llama_hf_dir
- - lora_model path_to_chinese_llama_or_alpaca_lora
- - with_prompt
- - interactive Wenn das Skript merge_llama_with_chinese_lora_to_hf.py zuvor ausgeführt wurde, um Lora -Gewichte zusammenzuführen, müssen Sie nicht angeben --lora_model , und die Startmethode ist einfacher:
CUDA_VISIBLE_DEVICES = 0 python scripts / inference_hf . py
- - base_model path_to_merged_llama_or_alpaca_hf_dir
- - with_prompt
- - interactive Wenn CUDA_VISIBLE_DEVICES=0 gelöscht wird, ist es der CPU -Argumentationsmodus. Natürlich können Sie es auch in Webui ausführen und bereitstellen. <quellen>
Die Modelle in diesem Projekt unterstützen hauptsächlich die folgenden Methoden zur Quantifizierung, Argumentation und Bereitstellung.
| Argumentations- und Bereitstellungsmethoden | Merkmale | Plattform | CPU | GPU | Quantitative Belastung | Grafikschnittstelle | Tutorial |
|---|---|---|---|---|---|---|---|
| lama.cpp | Reiche quantitative Optionen und effiziente lokale Argumentation | Allgemein | ✅ | ✅ | ✅ | Link | |
| ? Transformatoren | Native Transformers Inference Interface | Allgemein | ✅ | ✅ | ✅ | ✅ | Link |
| Text-Generation-Webui | So bereitstellen Sie die Front-End-Web-UI-Schnittstelle | Allgemein | ✅ | ✅ | ✅ | ✅ | Link |
| Llamachat | Grafische Interaktionsschnittstelle unter macOS (muss mit dem Lama.cpp -Modell abgestimmt werden) | Macos | ✅ | ✅ | ✅ | Link |