SuperAPIAI Directory

Phi2-mini-Chinese

github.com · AI developer platforms

Phi2-mini-Chinese is an open-source experimental project for training a small Chinese Phi2 model from scratch, with the model named Phi2-Chinese-0.2B. The project open-sources the code and model weights, and supports integration with LangChain to load a local knowledge base for retrieval-augmented generation (RAG). The main contents include data cleaning, Tokenizer training, CLM causal model pretraining, SFT fine-tuning, DPO preference optimization, and a LangChain RAG integration example. The project is experimental in nature, has relatively little pretraining data, and may change at any time. For better results, refer to ChatLM-mini-Chinese.

Open website Alternatives
PricingOpen source开源项目(可免费自部署)

About this tool

Phi2-mini-Chinese is an open-source experimental project for training a small Chinese Phi2 model from scratch. The repository name is charent/Phi2-mini-Chinese, and the model name is Phi2-Chinese-0.2B. The project open-sources the code and model weights, and supports integration with LangChain to load a local knowledge base for retrieval-augmented generation (RAG).

Purpose This project is used to train your own small Chinese Phi2 dialogue model and demonstrate how to integrate it with LangChain for retrieval-augmented generation over a local knowledge base. The project is positioned as an experimental project with relatively little pretraining data. If you need a small Chinese model with better performance, you can refer to the same author's ChatLM-mini-Chinese project. The project code and model structure may undergo major changes at any time, including training data, model structure, file directory structure, etc.; the first version of the model and code can be viewed under tag v1.0.

Main capabilities

  1. Data preprocessing: including adding a period at the end of sentences, converting Traditional Chinese to Simplified Chinese, removing repeated punctuation marks (for example, a large number of "。。。。。。" appearing in some dialogue corpora), NFKC Unicode normalization (mainly handling full-width to half-width conversion and the \u3000 \xa0 issues in web data), etc. For the specific data cleaning process, refer to the ChatLM-mini-Chinese project.
  2. Tokenizer training: use a byte-level BPE tokenizer, and also provide training code for both char-level and byte-level tokenizers. After training, check whether the vocabulary contains common special symbols such as \t, \n, etc., and verify whether they can be restored through encode and decode; if missing, they can be added using the add_tokens function. Use len(tokenizer) to get the vocabulary size; tokenizer.vocab_size does not count characters added through add_tokens. Tokenizer training is very memory-intensive: byte-level training on 100 million characters requires at least 32G of memory (32G may still frequently trigger swap), and training on a 13600K takes about 1 hour; char-level training on 650 million characters (about the data volume of Chinese Wikipedia) requires at least 32G of memory, and because swap is triggered multiple times, the actual usage far exceeds 32G, with training on a 13600K taking about half an hour. For large datasets (GB level), it is recommended to sample from the dataset before trainingTrain the tokenizer.
  3. CLM causal model pretraining: use a large amount of text for unsupervised pretraining, mainly using the BELLE open-source dataset. The dataset format is one sample per sentence; if too long, it can be truncated into multiple samples. During CLM pretraining, the model input and output are the same, and when calculating the cross-entropy loss, it needs to be shifted by one position. When processing encyclopedia corpora, it is recommended to add an '[EOS]' marker after the end of each entry; other corpora are processed similarly, and an '[EOS]' marker should be added at the end of a doc (end of article or end of paragraph); the start marker '[BOS]' may be added or not.
  4. SFT supervised fine-tuning: when calculating the loss, the model will ignore the part before the marker "##回答:" (the "##回答:" itself will also be ignored), and calculation starts after "##回答:". Remember to add the EOS end-of-sentence special token, otherwise the model will not know when to stop during decode; the BOS start-of-sentence marker may be filled in or not.
  5. DPO preference optimization: adopt a simpler DPO preference optimization method that saves more memory. Fine-tune the SFT model according to personal preferences. The dataset needs to construct three columns: prompt, chosen, and rejected; part of the data in the rejected column can be generated from the initial model in the SFT stage (for example, SFT trained for 4 epochs, take the model at the 0.5 epoch checkpoint). If the similarity between the generated rejected and chosen is above 0.9, do not use this data item. The DPO process requires two models: one is the model to be trained, and the other is the reference model; when loading, they are actually the same model, except that the reference model does not participate in parameter updates.
  6. LangChain RAG: supports connecting to LangChain to load a local knowledge base for retrieval-augmented generation. For specific code, see rag_with_langchain.ipynb.

Other information The project supports flash attention 2 acceleration. The model weights are hosted in the Hugging Face repository Phi2-Chinese-0.2B. The project declares that it does not assume responsibility for data security, public opinion risks caused by the open-source model and code, or risks and liabilities arising from the model being misled, abused, propagated, or improperly used. If you find the project helpful, citations are welcome.

Limitations The project is an experimental project, with relatively little pretraining data and limited results; the content may undergo major changes at any time; training the Tokenizer has relatively high memory requirements; other details such as fees, pricing, and platform supportNot provided yet.

Up/Down to navigate · Enter to open · ESC to closeBrowse all tools