A professional, production-ready framework for fine-tuning ChatGLM3-6B models on Chinese text data using LoRA (Low-Rank Adaptation). This project provides a complete end-to-end solution: from downloading and processing Chinese Wikipedia data to training and deploying fine-tuned models.
This project enables you to:
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install --upgrade pip
pip install -r requirements.txt
Option A: Use Wikipedia Data (Recommended)
# Download and process Chinese Wikipedia (5000 articles)
python prepare_data.py --download --max-articles 5000
# This will:
# 1. Download Wikipedia dump (~3GB, one-time)
# 2. Extract and clean articles
# 3. Create training dataset: data/chinese_wiki_articles.jsonl
Option B: Use Existing Data
If you already have data/chinese_wiki_5000.jsonl, you can skip this step!
Option C: Use Your Own Data
Place your JSONL file in data/ directory with format:
{"text": "你的中文文本内容..."}
Edit configs/training_config.yaml to customize:
Or use command-line arguments (see below).
# Train with default configuration
python train.py
# Or customize via command-line
python train.py --epochs 2 --learning-rate 1e-3 --batch-size 4
# Interactive mode
python inference.py
# Or single prompt
python inference.py --prompt "中国的首都是"
That's it! Your fine-tuned model is ready to use.
1. Download Wikipedia Dump
↓
2. Extract & Clean Articles
↓
3. Create Training Dataset (JSONL)
↓
4. Fine-tune ChatGLM3 with LoRA
↓
5. Generate Chinese Text
prepare_data.py)[[links]], {{templates}})train.py)outputs/inference.py)cd fine_tune
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install --upgrade pip
pip install -r requirements.txt
python -c "import torch; print(f'PyTorch: {torch.__version__}'); print(f'MPS available: {torch.backends.mps.is_available()}')"
# 1. Prepare data from Wikipedia
python prepare_data.py --download --max-articles 5000
# 2. Train the model
python train.py
# 3. Test the model
python inference.py --prompt "人工智能是指"
# Download latest Wikipedia dump and process 5000 articles
python prepare_data.py --download --max-articles 5000
# Use existing dump file
python prepare_data.py --dump-file data/zhwiki-20250320-pages-articles.xml.bz2 --max-articles 5000
# Show statistics for existing dataset
python prepare_data.py --stats data/chinese_wiki_5000.jsonl
Create a JSONL file in data/ directory:
{"text": "第一段中文文本..."}
{"text": "第二段中文文本..."}
{"text": "第三段中文文本..."}
Then update configs/training_config.yaml:
data:
train_file: "data/your_data.jsonl"
# Use default configuration
python train.py
# Use custom config file
python train.py --config configs/my_config.yaml
# Override specific parameters
python train.py --epochs 2 --learning-rate 1e-3 --batch-size 4
| Option | Description | Default |
|---|---|---|
--config | Path to config file | configs/training_config.yaml |
--epochs | Number of training epochs | From config |
--learning-rate | Learning rate | From config |
--batch-size | Batch size per device | From config |
--output-dir | Output directory | From config |
python inference.py
Example session:
You: 中国的首都是
Model: 北京。北京是中华人民共和国的首都...
You: 人工智能是什么
Model: 人工智能(AI)是指由机器展现出的智能...
python inference.py --prompt "人工智能是指"
python inference.py \
--model-path ./outputs/chatglm3_finetuned \
--prompt "你的问题" \
--max-length 200 \
--temperature 0.8
The training configuration is managed through YAML files. The main configuration file is configs/training_config.yaml.
model:
name: "THUDM/chatglm3-6b"
cache_dir: "./cache"
torch_dtype: "float16"
lora:
enabled: true
r: 4 # Rank: 4 (stable), 8 (quality), 16 (maximum - may cause issues)
lora_alpha: 8
lora_dropout: 0.1
target_modules:
- "query_key_value"
- "dense"
- "dense_h_to_4h"
- "dense_4h_to_h"
training:
num_train_epochs: 4
per_device_train_batch_size: 2
learning_rate: 2e-3
lr_scheduler_type: "constant"
max_grad_norm: 1.0
data:
train_file: "data/chinese_wiki_5000.jsonl"
max_length: 96
test_size: 0.1
See configs/training_config.yaml for all available options and detailed comments.
r=4, max_length=96, learning_rate=2e-3r=8, max_length=128, learning_rate=2e-3 (may require more memory)r=4, max_length=64, fewer epochs# Build the Docker image
docker-compose build
# Start training
docker-compose up
# Run in detached mode
docker-compose up -d
# View logs
docker-compose logs -f
# Stop training
docker-compose down
# Override command
docker-compose run --rm training python train.py --epochs 2
For GPU support, uncomment the GPU section in docker-compose.yml:
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [gpu]
Then rebuild:
docker-compose build
docker-compose up
fine_tune/
├── src/ # Source code
│ ├── data/ # Data loading utilities
│ │ ├── dataset_loader.py # Load JSONL datasets
│ │ └── wikipedia_processor.py # Download & process Wikipedia
│ ├── training/ # Training modules
│ │ └── trainer.py
│ └── utils/ # Utility functions
│ ├── config_loader.py
│ └── device_utils.py
├── configs/ # Configuration files
│ ├── default_config.yaml
│ └── training_config.yaml
├── data/ # Data directory
│ ├── *.jsonl # Processed training data
│ └── zhwiki-*.xml.bz2 # Wikipedia dumps (if downloaded)
├── outputs/ # Training outputs (models, checkpoints)
├── logs/ # Training logs
├── cache/ # Model cache
├── docs/ # Documentation
│ ├── USAGE.md
│ └── DATA_PREPARATION.md
├── scripts/ # Utility scripts
├── prepare_data.py # Data preparation script (Wikipedia)
├── train.py # Main training script
├── inference.py # Inference script
├── setup.py # Setup script
├── test_setup.py # Verification script
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose configuration
├── Makefile # Common tasks
├── requirements.txt # Python dependencies
└── README.md # This file
Solution: Reduce batch size or sequence length in config:
training:
per_device_train_batch_size: 1
data:
max_length: 64
Solution: Ensure you're using float16 and low_cpu_mem_usage: true:
model:
torch_dtype: "float16"
low_cpu_mem_usage: true
Solution: Use lower LoRA rank and gradient clipping:
lora:
r: 4 # Instead of 8 or 16
training:
max_grad_norm: 1.0
Solution:
max_length: 64)r: 4)Solution: Ensure these settings in config:
training:
fp16: false
bf16: false
dataloader_pin_memory: false
dataloader_num_workers: 0
Solution:
--date 20240101--dump-file optionlogs/ directoryconfigs/training_config.yamldocs/DATA_PREPARATION.md for data preparation helpdocs/USAGE.md for detailed usage instructionsWith default configuration on 5000 Chinese Wikipedia articles:
Training progress is logged to:
training.log filetensorboard --logdir logsThis project is licensed under the MIT License - see the LICENSE file for details.
For questions or issues, please open an issue on GitHub.
Last Updated: 2025
Version: 1.0.0
Status: ✅ Production Ready
A professional, production-ready framework for fine-tuning ChatGLM3-6B models on Chinese text data using LoRA (Low-Rank Adaptation). This project provides a complete end-to-end solution: from downloading and processing Chinese Wikipedia data to training and deploying fine-tuned models.
This project enables you to:
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install --upgrade pip
pip install -r requirements.txt
Option A: Use Wikipedia Data (Recommended)
# Download and process Chinese Wikipedia (5000 articles)
python prepare_data.py --download --max-articles 5000
# This will:
# 1. Download Wikipedia dump (~3GB, one-time)
# 2. Extract and clean articles
# 3. Create training dataset: data/chinese_wiki_articles.jsonl
Option B: Use Existing Data
If you already have data/chinese_wiki_5000.jsonl, you can skip this step!
Option C: Use Your Own Data
Place your JSONL file in data/ directory with format:
{"text": "你的中文文本内容..."}
Edit configs/training_config.yaml to customize:
Or use command-line arguments (see below).
# Train with default configuration
python train.py
# Or customize via command-line
python train.py --epochs 2 --learning-rate 1e-3 --batch-size 4
# Interactive mode
python inference.py
# Or single prompt
python inference.py --prompt "中国的首都是"
That's it! Your fine-tuned model is ready to use.
1. Download Wikipedia Dump
↓
2. Extract & Clean Articles
↓
3. Create Training Dataset (JSONL)
↓
4. Fine-tune ChatGLM3 with LoRA
↓
5. Generate Chinese Text
prepare_data.py)[[links]], {{templates}})train.py)outputs/inference.py)cd fine_tune
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install --upgrade pip
pip install -r requirements.txt
python -c "import torch; print(f'PyTorch: {torch.__version__}'); print(f'MPS available: {torch.backends.mps.is_available()}')"
# 1. Prepare data from Wikipedia
python prepare_data.py --download --max-articles 5000
# 2. Train the model
python train.py
# 3. Test the model
python inference.py --prompt "人工智能是指"
# Download latest Wikipedia dump and process 5000 articles
python prepare_data.py --download --max-articles 5000
# Use existing dump file
python prepare_data.py --dump-file data/zhwiki-20250320-pages-articles.xml.bz2 --max-articles 5000
# Show statistics for existing dataset
python prepare_data.py --stats data/chinese_wiki_5000.jsonl
Create a JSONL file in data/ directory:
{"text": "第一段中文文本..."}
{"text": "第二段中文文本..."}
{"text": "第三段中文文本..."}
Then update configs/training_config.yaml:
data:
train_file: "data/your_data.jsonl"
# Use default configuration
python train.py
# Use custom config file
python train.py --config configs/my_config.yaml
# Override specific parameters
python train.py --epochs 2 --learning-rate 1e-3 --batch-size 4
| Option | Description | Default |
|---|---|---|
--config | Path to config file | configs/training_config.yaml |
--epochs | Number of training epochs | From config |
--learning-rate | Learning rate | From config |
--batch-size | Batch size per device | From config |
--output-dir | Output directory | From config |
python inference.py
Example session:
You: 中国的首都是
Model: 北京。北京是中华人民共和国的首都...
You: 人工智能是什么
Model: 人工智能(AI)是指由机器展现出的智能...
python inference.py --prompt "人工智能是指"
python inference.py \
--model-path ./outputs/chatglm3_finetuned \
--prompt "你的问题" \
--max-length 200 \
--temperature 0.8
The training configuration is managed through YAML files. The main configuration file is configs/training_config.yaml.
model:
name: "THUDM/chatglm3-6b"
cache_dir: "./cache"
torch_dtype: "float16"
lora:
enabled: true
r: 4 # Rank: 4 (stable), 8 (quality), 16 (maximum - may cause issues)
lora_alpha: 8
lora_dropout: 0.1
target_modules:
- "query_key_value"
- "dense"
- "dense_h_to_4h"
- "dense_4h_to_h"
training:
num_train_epochs: 4
per_device_train_batch_size: 2
learning_rate: 2e-3
lr_scheduler_type: "constant"
max_grad_norm: 1.0
data:
train_file: "data/chinese_wiki_5000.jsonl"
max_length: 96
test_size: 0.1
See configs/training_config.yaml for all available options and detailed comments.
r=4, max_length=96, learning_rate=2e-3r=8, max_length=128, learning_rate=2e-3 (may require more memory)r=4, max_length=64, fewer epochs# Build the Docker image
docker-compose build
# Start training
docker-compose up
# Run in detached mode
docker-compose up -d
# View logs
docker-compose logs -f
# Stop training
docker-compose down
# Override command
docker-compose run --rm training python train.py --epochs 2
For GPU support, uncomment the GPU section in docker-compose.yml:
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [gpu]
Then rebuild:
docker-compose build
docker-compose up
fine_tune/
├── src/ # Source code
│ ├── data/ # Data loading utilities
│ │ ├── dataset_loader.py # Load JSONL datasets
│ │ └── wikipedia_processor.py # Download & process Wikipedia
│ ├── training/ # Training modules
│ │ └── trainer.py
│ └── utils/ # Utility functions
│ ├── config_loader.py
│ └── device_utils.py
├── configs/ # Configuration files
│ ├── default_config.yaml
│ └── training_config.yaml
├── data/ # Data directory
│ ├── *.jsonl # Processed training data
│ └── zhwiki-*.xml.bz2 # Wikipedia dumps (if downloaded)
├── outputs/ # Training outputs (models, checkpoints)
├── logs/ # Training logs
├── cache/ # Model cache
├── docs/ # Documentation
│ ├── USAGE.md
│ └── DATA_PREPARATION.md
├── scripts/ # Utility scripts
├── prepare_data.py # Data preparation script (Wikipedia)
├── train.py # Main training script
├── inference.py # Inference script
├── setup.py # Setup script
├── test_setup.py # Verification script
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose configuration
├── Makefile # Common tasks
├── requirements.txt # Python dependencies
└── README.md # This file
Solution: Reduce batch size or sequence length in config:
training:
per_device_train_batch_size: 1
data:
max_length: 64
Solution: Ensure you're using float16 and low_cpu_mem_usage: true:
model:
torch_dtype: "float16"
low_cpu_mem_usage: true
Solution: Use lower LoRA rank and gradient clipping:
lora:
r: 4 # Instead of 8 or 16
training:
max_grad_norm: 1.0
Solution:
max_length: 64)r: 4)Solution: Ensure these settings in config:
training:
fp16: false
bf16: false
dataloader_pin_memory: false
dataloader_num_workers: 0
Solution:
--date 20240101--dump-file optionlogs/ directoryconfigs/training_config.yamldocs/DATA_PREPARATION.md for data preparation helpdocs/USAGE.md for detailed usage instructionsWith default configuration on 5000 Chinese Wikipedia articles:
Training progress is logged to:
training.log filetensorboard --logdir logsThis project is licensed under the MIT License - see the LICENSE file for details.
For questions or issues, please open an issue on GitHub.
Last Updated: 2025
Version: 1.0.0
Status: ✅ Production Ready