ChatLM-mini-Chinese is an open-source project for training a small Chinese generative dialogue model from scratch. The model is ChatLM-Chinese-0.2B, with a parameter scale of 0.2B (about 210M counting shared weights). The project aims to lower the threshold for large language model training and publicly discloses the complete process code for dataset sources, data cleaning, tokenizer training, model pretraining, SFT instruction fine-tuning, RLHF optimization, and more.
[Purpose] The project is used to train a small Chinese dialogue model from scratch, covering the complete process of data cleaning, tokenizer training, model pretraining, SFT instruction fine-tuning, RLHF optimization, and more, and supports downstream task fine-tuning.
[Main Capabilities]
- Pretraining: integrated into end-to-end Text-to-Text pretraining, not mask prediction pretraining; open-source data cleaning (such as normalization, document deduplication based on mini_hash, etc.), dataset construction, and dataset loading optimization processes; tokenizer multi-process word frequency statistics, supporting tokenizer training for sentencepiece and huggingface tokenizers; supports resuming training from checkpoints at any position; streaming loading of large datasets (GB level), supports shuffling of buffered data, does not use memory or hard disk as cache, effectively reducing memory and disk usage. With batch_size=1 and max_len=320 configured, it supports pretraining at minimum on a machine with 16GB memory + 4GB VRAM.
- SFT fine-tuning: open-source SFT datasets and data processing procedures; self-implemented trainer supports prompt instruction fine-tuning and supports resuming training from any checkpoint; supports sequence to sequence fine-tuning with Hugging Face trainer; supports traditional fine-tuning with a low learning rate and training only the decoder layers.
- RLHF preference optimization: uses DPO for full preference optimization; supports preference optimization using peft lora; supports model merging, which can merge the Lora adapter into the original model.
- Downstream task fine-tuning: finetune_examples provides a fine-tuning example for the triple information extraction task, and the fine-tuned model still retains its dialogue capability.
[Usage] The project is based on the Hugging Face NLP framework, including transformers, accelerate, trl, peft, etc. Python 3.10 is recommended. The training process includes: tokenizer training, Text-to-Text pretraining, SFT fine-tuning, RLHF (DPO) optimization, and downstream task fine-tuning.
Regarding the model, the T5 model (Text-to-Text Transfer Transformer) source code comes from Hugging Face's T5ForConditionalGeneration. The model configuration is shown in model_config.json. The official T5-base has 12 encoder and decoder layers each, and this project modifies it to 10 layers. The model has 0.2B parameters and a vocabulary size of 29298, containing only Chinese and a small amount of English.
Pretraining data: All datasets come from publicly available single-turn dialogue datasets on the internet. After data cleaning and formatting, they are saved as parquet files. The data processing procedure is shown in utils/raw_data_process.py. The main datasets include the JSON version of the community question-answering dataset webtext2019zh, baike_qa2019 encyclopedia-style question answering, a Chinese medical field question-answering dataset, financial industry question-answering data, Zhihu question-answering data, BELLE's open-source instruction training data, Wikipedia entry data, etc. The total number of datasets is 10.23 million: 9.3 million for the Text-to-Text pretraining set, 25,000 for the evaluation set, and 900,000 for the test set.
Training configuration: The pretraining learning rate is a dynamic learning rate from 1e-4 to 5e-3, and the pretraining time is 8 days; prompt supervised fine-tuning (SFT) uses the BELLE instruction training dataset (both instruction and response lengths are below 512), with a dynamic learning rate from 1e-7 to 5e-5, and fine-tuning time of 2 days; in DPO direct preference optimization, the dataset alpaca-gpt4-data-zh is used as the chosen text, and the SFT model batch-generates prompts to obtain the rejected text, taking 1 day. The DPO full preference optimization learning rate is 1e-5, half precision fp16, for a total of 2 epochs, taking 3h.
The SFT dataset comes from BELLE, namely generated_chat_0.4M, train_0.5M_CN, and train_2M_CN. After cleaning, about 137 remains Ten thousand lines. The dataset format is a parquet file with two columns, one column for prompt text and one column for response text. For fine-tuning details, see the train method under model/trainer.py. When is_finetune is set to True, fine-tuning is performed, and by default the embedding layer and encoder layer are frozen, with only the decoder layer trained.
The RLHF stage introduces two preference methods, PPO and DPO. This project uses DPO fine-tuning, which saves more GPU memory. The dpo dataset processing procedure is in utils/dpo_data_process.py, and after merging there are a total of 80,000 dpo data entries.
Downstream task fine-tuning takes triple information extraction from text as an example. The original dataset is the Baidu triple extraction dataset, the training dataset has about 17,000 entries, the learning rate is 5e-5, and training epochs are 5. The sft_train.py script can be used directly for fine-tuning, and the script finetune_IE_task.ipynb contains the detailed decoding process.
[Applicable Scenarios] Suitable for researchers and developers who want to train from scratch and fine-tune a small Chinese dialogue model on consumer-grade hardware; suitable for practice in learning the complete training workflow of large language models (data cleaning, tokenizer, pretraining, SFT, RLHF); suitable for downstream task fine-tuning experiments such as triple information extraction. You can also refer to Phi2-mini-Chinese by the same project author for retrieval-augmented generation (RAG) based on small models.
[Limitations] The pretraining dataset contains only more than 9 million entries, and the model parameters are only 0.2B, so it cannot cover all aspects, and there will be situations such as answering irrelevantly or generating useless text. The model itself was not trained on a larger dataset, nor was it fine-tuned for instructions to answer multiple-choice questions, so the C-Eval score is basically at the baseline level. The model in this project is a TextToText model, and in the prompt, response, and other fields during the pretraining, SFT, and RLHF stages, the [EOS] end-of-sequence token needs to be added. If you cannot connect to Hugging Face, you can use modelscope.snapshot_download to download the model files from modelscope. This project does not assume responsibility for data security, public opinion risks, or any risks and responsibilities arising from open-source models and code causing models to be misled, abused, disseminated, or improperly used.