mirror of
https://github.com/zenlm/zen-family.git
synced 2026-10-30 14:58:06 +00:00
194 lines
5.1 KiB
Bash
Executable File
194 lines
5.1 KiB
Bash
Executable File
#!/bin/bash
|
|
# Batch conversion script for all Zen models
|
|
# Optimized for Apple Silicon with different quantization options
|
|
|
|
set -e
|
|
|
|
echo "==================================="
|
|
echo "Zen Models MLX Conversion Pipeline"
|
|
echo "==================================="
|
|
|
|
# Color codes for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Check if running on macOS
|
|
if [[ "$OSTYPE" != "darwin"* ]]; then
|
|
echo -e "${RED}Error: This script is designed for macOS with Apple Silicon${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# Install dependencies
|
|
echo -e "${BLUE}Installing required packages...${NC}"
|
|
pip install -q -r requirements.txt
|
|
|
|
# Create models directory
|
|
mkdir -p models
|
|
|
|
# Function to convert a model
|
|
convert_model() {
|
|
local model=$1
|
|
local bits=$2
|
|
|
|
echo -e "${BLUE}Converting $model with $bits-bit quantization...${NC}"
|
|
|
|
if python convert.py "$model" --q-bits "$bits" --force; then
|
|
echo -e "${GREEN}✓ $model ($bits-bit) converted successfully${NC}"
|
|
|
|
# Optimize for deployment
|
|
echo -e "${BLUE}Optimizing $model for deployment...${NC}"
|
|
python optimize.py "models/${model}-${bits}bit-mlx"
|
|
|
|
return 0
|
|
else
|
|
echo -e "${RED}✗ Failed to convert $model ($bits-bit)${NC}"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to benchmark a model
|
|
benchmark_model() {
|
|
local model_path=$1
|
|
|
|
echo -e "${BLUE}Benchmarking $model_path...${NC}"
|
|
python inference.py "$model_path" --benchmark
|
|
}
|
|
|
|
# Main conversion process
|
|
echo ""
|
|
echo "Starting batch conversion..."
|
|
echo ""
|
|
|
|
# Convert nano models (4B) - both 4-bit and 8-bit
|
|
for model in "zen-nano-instruct" "zen-nano-thinking"; do
|
|
for bits in 4 8; do
|
|
convert_model "$model" "$bits"
|
|
done
|
|
done
|
|
|
|
# Convert large models (30B) - 4-bit only for memory efficiency
|
|
for model in "zen-omni" "zen-omni-thinking" "zen-omni-captioner"; do
|
|
convert_model "$model" 4
|
|
done
|
|
|
|
# Convert medium models - 4-bit and 8-bit
|
|
for model in "zen-coder" "zen-next"; do
|
|
for bits in 4 8; do
|
|
convert_model "$model" "$bits"
|
|
done
|
|
done
|
|
|
|
echo ""
|
|
echo -e "${GREEN}==================================="
|
|
echo "Conversion Complete!"
|
|
echo "===================================${NC}"
|
|
echo ""
|
|
|
|
# Generate summary report
|
|
echo "Generating conversion summary..."
|
|
|
|
cat > models/README.md << 'EOF'
|
|
# Zen Models - MLX Format
|
|
|
|
## Converted Models
|
|
|
|
All models have been optimized for Apple Silicon (M1/M2/M3/M4) using MLX.
|
|
|
|
### Available Models
|
|
|
|
| Model | Size | 4-bit | 8-bit | Use Case |
|
|
|-------|------|-------|--------|----------|
|
|
| zen-nano-instruct | 4B | ✓ | ✓ | General instruction following |
|
|
| zen-nano-thinking | 4B | ✓ | ✓ | Chain-of-thought reasoning |
|
|
| zen-omni | 30B | ✓ | - | Advanced multimodal tasks |
|
|
| zen-omni-thinking | 30B | ✓ | - | Complex reasoning |
|
|
| zen-omni-captioner | 30B | ✓ | - | Image captioning |
|
|
| zen-coder | 7B | ✓ | ✓ | Code generation |
|
|
| zen-next | 13B | ✓ | ✓ | Next-gen capabilities |
|
|
|
|
### Memory Requirements
|
|
|
|
#### 4-bit Quantization
|
|
- zen-nano models: ~2.5 GB
|
|
- zen-coder: ~4 GB
|
|
- zen-next: ~7 GB
|
|
- zen-omni models: ~16 GB
|
|
|
|
#### 8-bit Quantization
|
|
- zen-nano models: ~5 GB
|
|
- zen-coder: ~8 GB
|
|
- zen-next: ~14 GB
|
|
|
|
### Usage Examples
|
|
|
|
#### Basic Inference
|
|
```bash
|
|
python inference.py models/zen-nano-instruct-4bit-mlx \
|
|
--prompt "Explain quantum computing" \
|
|
--max-tokens 256
|
|
```
|
|
|
|
#### Interactive Chat
|
|
```bash
|
|
python inference.py models/zen-nano-thinking-4bit-mlx --chat
|
|
```
|
|
|
|
#### Streaming Output
|
|
```bash
|
|
python inference.py models/zen-coder-4bit-mlx \
|
|
--prompt "Write a Python function to sort a list" \
|
|
--stream
|
|
```
|
|
|
|
#### Performance Benchmark
|
|
```bash
|
|
python inference.py models/zen-nano-instruct-4bit-mlx --benchmark
|
|
```
|
|
|
|
### Optimization Tips
|
|
|
|
1. **Memory Management**: Use 4-bit models for devices with <16GB unified memory
|
|
2. **Batch Processing**: Process multiple prompts together for efficiency
|
|
3. **LoRA Fine-tuning**: Use low-rank adapters for task-specific tuning
|
|
4. **Model Selection**: Choose nano models for real-time applications
|
|
|
|
### Performance on Apple Silicon
|
|
|
|
Typical inference speeds (tokens/second):
|
|
|
|
| Chip | 4B Model | 7B Model | 13B Model | 30B Model |
|
|
|------|----------|----------|-----------|-----------|
|
|
| M1 | 35-40 | 20-25 | 10-15 | 3-5 |
|
|
| M1 Pro | 50-60 | 30-35 | 15-20 | 5-8 |
|
|
| M1 Max | 70-80 | 40-45 | 25-30 | 8-12 |
|
|
| M2 | 40-45 | 25-30 | 12-18 | 4-6 |
|
|
| M2 Pro | 60-70 | 35-40 | 20-25 | 7-10 |
|
|
| M2 Max | 80-90 | 45-50 | 30-35 | 10-15 |
|
|
| M3 | 45-50 | 30-35 | 15-20 | 5-7 |
|
|
| M3 Pro | 70-80 | 40-45 | 25-30 | 8-12 |
|
|
| M3 Max | 90-100 | 50-60 | 35-40 | 12-18 |
|
|
|
|
EOF
|
|
|
|
echo -e "${GREEN}Summary report generated at models/README.md${NC}"
|
|
|
|
# Optional: Run benchmarks
|
|
read -p "Do you want to run performance benchmarks? (y/n) " -n 1 -r
|
|
echo ""
|
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
echo ""
|
|
echo "Running benchmarks..."
|
|
|
|
# Benchmark a sample of models
|
|
benchmark_model "models/zen-nano-instruct-4bit-mlx"
|
|
benchmark_model "models/zen-coder-4bit-mlx"
|
|
|
|
echo -e "${GREEN}Benchmarks complete!${NC}"
|
|
fi
|
|
|
|
echo ""
|
|
echo -e "${GREEN}All models are ready for deployment!${NC}"
|
|
echo "To use a model, run:"
|
|
echo -e "${BLUE}python inference.py models/<model-name> --chat${NC}" |