Views
No views yet
CyberGong/
├── configs/
│ └── infer.yaml # Inference configuration file
├── models/
│ ├── Qwen2.5-1.5B/ # Base model directory
│ ├── your-persona-lora/ # LoRA adapter directory
│ └── merged-persona-model/ # Merged model
├── src/
│ ├── download_model.py # Model download script
│ ├── load_model.py # Model loading script
│ ├── chat.py # Chat/inference script
│ ├── merge_lora.py # LoRA merging script
│ └── utils.py # Utility functions
├── data/
│ └── prompts.jsonl # Test prompts
└── README.md # This filepip install transformers peft bitsandbytes torch huggingface-hub pyyaml1python src/download_model.py \
2 --model-id Qwen/Qwen2.5-1.5B \
3 --output-dir ./models/Qwen2.5-1.5B--model-id: HuggingFace model ID (default: Qwen/Qwen2.5-1.5B)--output-dir: Local output directory--cache-dir: Optional cache directory--max-workers: Concurrent download workers (default: 4)--use-cli: Use huggingface-cli to downloadpython src/chat.py --model-dir ./models/Qwen2.5-1.5B--model-dir: Model directory--no-4bit: Disable 4-bit quantization--device-map: Device allocation (default: auto)adapter_config.jsonadapter_model.safetensors1python src/chat.py \
2 --model-dir ./models/Qwen2.5-1.5B \
3 --adapter ./models/your-persona-lora1python src/merge_lora.py \
2 --model-dir ./models/Qwen2.5-1.5B \
3 --adapter-dir ./models/your-persona-lora \
4 --output-dir ./models/merged-persona-modelpython src/chat.py --model-dir ./models/merged-persona-modelpython src/chat.py --model-dir ./models/Qwen2.5-1.5Bexit: Exit chatclear: Clear chat historysave: Save conversation to conversation.json1python src/chat.py \
2 --model-dir ./models/Qwen2.5-1.5B \
3 --prompt "Hello, please introduce yourself"configs/infer.yaml to configure parameters:1model:
2 base_model_dir: ./models/Qwen2.5-1.5B
3 adapter_dir: ./models/your-persona-lora
4
5generation:
6 max_new_tokens: 256
7 temperature: 0.7
8 top_p: 0.9
9
10chat:
11 system_prompt: "You are a helpful assistant."
12 max_history_length: 10python src/chat.py --config ./configs/infer.yamlload_model.py)1from load_model import load_base_model
2
3model, tokenizer = load_base_model(
4 model_dir="./models/Qwen2.5-1.5B",
5 use_4bit=True,
6 device_map="auto"
7)1from load_model import load_persona_adapter
2
3model_with_adapter = load_persona_adapter(
4 base_model=model,
5 adapter_dir="./models/your-persona-lora"
6)chat.py)1from chat import PersonaChat
2from load_model import load_base_model
3
4model, tokenizer = load_base_model("./models/Qwen2.5-1.5B")
5chat = PersonaChat(model, tokenizer)
6
7# Single-turn conversation
8response = chat.generate("Hello")
9
10# Get history
11history = chat.get_history()
12
13# Save conversation
14chat.save_conversation("output.json")merge_lora.py)1from merge_lora import merge_and_unload
2
3success = merge_and_unload(
4 base_model_dir="./models/Qwen2.5-1.5B",
5 adapter_dir="./models/your-persona-lora",
6 output_dir="./models/merged-persona-model"
7)1quantization:
2 use_4bit: true # Enable 4-bit quantization
3 compute_dtype: bfloat16 # Compute precision
4 quant_type: nf4 # Quantization type1generation:
2 max_new_tokens: 256 # Maximum tokens to generate
3 temperature: 0.7 # Temperature (higher = more random)
4 top_p: 0.9 # Top-P sampling
5 top_k: 50 # Top-K sampling
6 repetition_penalty: 1.1 # Repetition penalty
7 do_sample: true # Whether to use sampling
8 num_beams: 1 # Beam search (1 = disabled)torch.cuda.OutOfMemoryError--no-4bitmax_new_tokensmax_history_lengthFileNotFoundError: Model directory does not existpython src/download_model.pymodel_inference.logInvalid adapter directoryadapter_config.json existsadapter_model.safetensors exists| Configuration | VRAM Usage | Inference Speed | Notes |
|---|---|---|---|
| Base (no quantization) | ~5.5GB | Fast | May run out of VRAM |
| Base (4-bit) | ~2GB | Fast | Recommended |
| Base + Adapter | +0.5GB | Slightly slower | Depends on adapter size |
| Merged model | Same as base | Same as base | Same as base |
model_inference.log:2024-01-15 10:30:45,123 - __main__ - INFO - Start loading base model
2024-01-15 10:30:46,456 - load_model - INFO - ✓ Tokenizer loaded successfully
...download_model.pydevice_map="cpu" or "auto" instead of "cuda:0"1# 1. Download model (first time, requires network)
2python src/download_model.py
3
4# 2. Test base model
5python src/chat.py --prompt "test"
6
7# 3. Develop adapter
8# Use external tools to train adapter...
9
10# 4. Test adapter
11python src/chat.py --adapter ./models/your-persona-lora
12
13# 5. Merge model
14python src/merge_lora.py1# Directly load merged model
2python src/chat.py --model-dir ./models/merged-persona-model1pip install gradio
2
3# Create src/gradio_app.py1pip install fastapi uvicorn
2
3# Create src/api_server.pyhf_model_id in config.yaml. Just ensure it's compatible with AutoModelForCausalLM.peft official documentation.num_beams=1 (disable beam search)max_new_tokensdevice_map="auto" for automatic allocation