ChatGLM-6B is an open-source, bilingual Chinese-English dialogue language model based on the General Language Model (GLM) architecture, with 6.2 billion parameters. Combined with model quantization technology, users can deploy it locally on consumer-grade graphics cards (as low as 6GB of VRAM at the INT4 quantization level). ChatGLM-6B uses technology similar to ChatGPT and has been optimized for Chinese question answering and dialogue. After about 1T tokens of Chinese-English bilingual training, supplemented by supervised fine-tuning, feedback self-bootstrapping, reinforcement learning from human feedback, and other technologies, the 6.2 billion parameter ChatGLM-6B can already generate responses that quite match human preferences.
To facilitate downstream developers in customizing the model for their own application scenarios, the project also implements an efficient parameter fine-tuning method based on P-Tuning v2, which can start fine-tuning with as little as 7GB of VRAM at the INT4 quantization level.
ChatGLM-6B weights are fully open for academic research, and after filling out a questionnaire for registration, free commercial use is also permitted.
Usage:
- Install dependencies: pip install -r requirements.txt. The recommended version of the transformers library is 4.27.1, but in theory any version not lower than 4.23.1 is acceptable. If you need to run the quantized model on CPU, you also need to install gcc and openmp.
- Call the model to generate dialogue: The model can be loaded through the transformers library; see the official repository for example code. The model implementation is still changing. If you need to fix the version, you can add the revision="v1.1.0" parameter in the from_pretrained call.
- Web Demo: After installing Gradio, run web_demo.py. The program will run a Web Server and output the address.
- Command-line Demo: Run cli_demo.py to conduct interactive dialogue on the command line. Enter clear to clear the dialogue history, and enter stop to terminate the program.
- API deployment: After installing fastapi and uvicorn, run api.py. By default, it is deployed locally on port 8000 and called via the POST method.
- Quantized loading: Loading by default with FP16 precision requires about 13GB of VRAM. You can try loading the model in a quantized way. Under 8-bit quantization, GPU VRAM usage is about 10GB, requiring only 6GB usage under 4-bit quantization.
- CPU inference: If there is no GPU, inference can also be performed on the CPU, but it is slower and requires about 32GB of memory.
- Mac deployment: For Macs equipped with Apple Silicon or AMD GPUs, the MPS backend can be used to run on the GPU, and PyTorch-Nightly needs to be installed.
- Multi-GPU deployment: If there are multiple GPUs but the memory of each GPU is insufficient to hold the complete model, you can install accelerate and then split the model across multiple GPUs.
- Fine-tuning: Efficient parameter fine-tuning based on P-tuning v2. For specific usage, see ptuning/README.md.
Applicable scenarios:
- Locally deployed Chinese-English bilingual dialogue applications
- Academic research and education
- Downstream developers customizing models
- Question answering based on an extensible knowledge base (such as combined with langchain)
- ChatPDF-like functionality
- Integration into chat platforms
Limitations:
- Small model capacity: The small capacity of 6B determines its relatively weak model memory and language ability. When facing many factual knowledge tasks, it may generate incorrect information, and it is also not good at solving logical problems (such as mathematics and programming).
- Generating harmful instructions or biased content: It is only a preliminary language model aligned with human intent and may generate harmful or biased content.
- Insufficient English ability: During training, most of the instructions/responses used are in Chinese, with only a very small portion of English content. Therefore, the quality of responses to English instructions is far inferior to that of Chinese, and may even contradict the content under Chinese instructions, and mixed Chinese and English may appear.
- Easily misled, weak dialogue ability: The dialogue ability is still relatively weak, and there are problems with "self-cognition", and it is very easy to be misled and produce erroneous statements.
- Model quantization will bring a certain performance loss, but after testing, natural and fluent generation can still be performed under 4-bit quantization.
- Due to the use of relative position encoding, it theoretically supports an unlimited context-length, but after the total length exceeds 2048 (the training length), performance will gradually decline.
Open source license: The code in this repository is open-sourced under the Apache-2.0 license, while the use of the ChatGLM-6B model weights needs to comply with the Model License.