LLM: Fine Tine Ollama deepseek-r1:1.5b
- Langkah-langkah Fine-Tuning Model DeepSeek-Coder-1.3B dengan Dataset JSONL untuk Ollama
Berdasarkan informasi yang tersedia, model yang Anda maksud kemungkinan besar adalah **DeepSeek-Coder-1.3B-Instruct** (dari Hugging Face: `deepseek-ai/deepseek-coder-1.3b-instruct`), karena tidak ada varian resmi "deepseek-r1:1.5b". Model ini berukuran sekitar 1.3B parameter dan sangat cocok untuk fine-tuning lokal karena ringan. Fine-tuning dilakukan menggunakan script resmi dari repo DeepSeek-Coder, dengan input dataset dalam format **JSONL** (setiap baris adalah JSON object dengan field `instruction` dan `output`).
Setelah fine-tuning, model output dalam format Hugging Face (HF) perlu dikonversi ke **GGUF** menggunakan `llama.cpp` agar bisa langsung dijalankan di **Ollama**. Berikut panduan langkah demi langkah. Asumsi: Anda punya environment Python 3.10+ dengan GPU (untuk percepatan), dan dataset JSONL siap (contoh format di bawah).
- 1. **Persiapan Dataset JSONL**
- Format dataset: Setiap baris adalah string JSON dengan dua field wajib: `instruction` (prompt/instruksi) dan `output` (respon yang diharapkan).
- Contoh file `data.jsonl`:
```
{"instruction": "Write a Python function to calculate factorial.", "output": "def factorial(n):\n if n == 0:\n return 1\n return n * factorial(n-1)"}
{"instruction": "Explain recursion in simple terms.", "output": "Recursion is a function that calls itself to solve smaller instances of the same problem."}
```
- Sumber: Format ini direkomendasikan oleh DeepSeek untuk fine-tuning.
- 2. **Setup Environment dan Instal Dependencies**
- Clone repo DeepSeek-Coder:
```
git clone https://github.com/deepseek-ai/DeepSeek-Coder
cd DeepSeek-Coder
```
- Instal dependencies untuk fine-tuning (termasuk DeepSpeed untuk efisiensi):
```
pip install -r finetune/requirements.txt
```
- Dependencies utama: `transformers`, `datasets`, `deepspeed`, `torch`, `accelerate`.
- Untuk konversi GGUF nanti, instal `llama.cpp`:
```
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make
pip install -r requirements.txt # Untuk converter Python
```
- 3. **Fine-Tuning Model**
- Gunakan script `finetune/finetune_deepseekcoder.py` dari repo. Ini mendukung DeepSpeed untuk training efisien.
- Contoh command (sesuaikan path dan hyperparams; model 1.3B bisa dijalankan di GPU 8GB+):
```
DATA_PATH="path/to/your/data.jsonl"
OUTPUT_PATH="path/to/output_model"
MODEL="deepseek-ai/deepseek-coder-1.3b-instruct"
cd finetune
deepspeed finetune_deepseekcoder.py \
--model_name_or_path $MODEL \
--data_path $DATA_PATH \
--output_dir $OUTPUT_PATH \
--num_train_epochs 3 \
--model_max_length 1024 \
--per_device_train_batch_size 8 \
--gradient_accumulation_steps 4 \
--evaluation_strategy "no" \
--save_strategy "steps" \
--save_steps 100 \
--learning_rate 2e-5 \
--warmup_steps 10 \
--logging_steps 1 \
--lr_scheduler_type "cosine" \
--gradient_checkpointing True \
--report_to "tensorboard" \
--deepspeed configs/ds_config_zero3.json \
--bf16 True
```
- **Penjelasan hyperparams**:
- `num_train_epochs`: Jumlah epoch (mulai dari 1-3 untuk dataset kecil).
- `per_device_train_batch_size`: Sesuaikan dengan VRAM (kurangi jika OOM).
- `model_max_length`: Panjang maksimal sequence (1024 aman untuk coder).
- Output: Folder `$OUTPUT_PATH` berisi model HF yang sudah di-fine-tune (checkpoint terakhir di `checkpoint-final`).
- Tips: Jika dataset besar, gunakan LoRA/PEFT untuk efisiensi (bisa ditambahkan via `--use_lora`). Sumber script dan command: Repo resmi DeepSeek-Coder.
- 4. **Konversi Model HF ke GGUF**
- Pindah ke folder `llama.cpp`.
- Convert model fine-tuned ke GGUF (f16 dulu, lalu quantize jika perlu):
```
python convert_hf_to_gguf.py $OUTPUT_PATH/checkpoint-final --outfile deepseek-finetuned.gguf --outtype f16
```
- Ini menghasilkan file `deepseek-finetuned.gguf`.
- Quantize untuk ukuran lebih kecil (misal Q4_K_M untuk keseimbangan kualitas/ukuran):
```
./llama-quantize deepseek-finetuned.gguf deepseek-finetuned-q4.gguf Q4_K_M
```
- Sumber: Converter dari llama.cpp.
- 5. **Import ke Ollama**
- Buat file `Modelfile` di folder yang sama dengan GGUF:
```
FROM ./deepseek-finetuned-q4.gguf
TEMPLATE """Template:If .System<|im_start|>system
Template:.System<|im_end|>
Template:EndTemplate:If .Prompt<|im_start|>user
Template:.Prompt<|im_end|>
Template:End<|im_start|>assistant
"""
PARAMETER stop "<|im_end|>"
PARAMETER stop "<|im_start|>"
SYSTEM "You are a helpful coding assistant fine-tuned on custom data."
```
- Template ini disesuaikan untuk format chat DeepSeek (dengan `<|im_start|>` dll.).
- Buat model di Ollama:
```
ollama create deepseek-custom -f Modelfile
```
- Jalankan:
```
ollama run deepseek-custom
```
- Sekarang model siap digunakan! Tes dengan prompt seperti "Write a Python function...".
- Sumber: Panduan Ollama untuk custom GGUF.
- Catatan Tambahan
- **Waktu & Hardware**: Fine-tuning 1.3B dengan 1k sampel JSONL butuh ~1-2 jam di RTX 3060. Gunakan Colab jika lokal lemah. - **Troubleshooting**: Jika error OOM, kurangi batch size atau gunakan QLoRA. Untuk dataset lebih kompleks, lihat tutorial LLaMA-Factory untuk UI no-code. - **Alternatif Mudah**: Jika ingin tanpa script custom, gunakan Hugging Face `trl` + `peft` untuk SFTTrainer dengan dataset JSONL (load via `load_dataset('json', data_files='data.jsonl')`). - Jika model bukan 1.3B, konfirmasi nama tepat di Hugging Face atau Ollama library.
Jika butuh bantuan lebih detail (misal error log), beri tahu!