How to Fine-tune DeepSeek Locally with LoRA: A Comprehensive Guide

ic_writer ds66
ic_date 2025-01-01
blogs

Introduction

Fine-tuning large language models (LLMs) such as DeepSeek R1 using Low-Rank Adaptation (LoRA) has revolutionized the customization of open-weight models. LoRA enables efficient and cost-effective fine-tuning by injecting a small number of trainable parameters into a frozen pre-trained model. This guide will walk you through the process of fine-tuning DeepSeek locally with LoRA, covering hardware requirements, environment setup, dataset preparation, training steps, evaluation, and deployment.

62166_uzby_3407.png

Section 1: Prerequisites

Hardware Requirements

  • CPU: 16-core or higher

  • RAM: 64–128 GB

  • GPU: At least one NVIDIA RTX 3090 / A100 / H100 (24GB+ VRAM)

  • Storage: 500GB+ SSD

  • Operating System: Ubuntu 22.04 recommended

Software Requirements

  • Python 3.10+

  • CUDA Toolkit 11.7+

  • PyTorch with GPU support

  • transformers, peft, datasets, accelerate, bitsandbytes

Install essentials:

pip install torch transformers datasets accelerate peft bitsandbytes

Section 2: Download Pretrained DeepSeek Model

Use Hugging Face to download a LoRA-compatible version of DeepSeek R1:

from transformers import AutoTokenizer, AutoModelForCausalLM

model_name = "deepseek-ai/deepseek-llm-67b-chat"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, load_in_8bit=True, device_map="auto")

If your system supports 16-bit precision, consider using load_in_4bit=True with bitsandbytes for efficiency.

Section 3: Dataset Preparation

Format Requirements

Fine-tuning requires dataset in the following format:

{
  "prompt": "What is quantum computing?",
  "completion": "Quantum computing is..."
}

You can use open-source instruction datasets like:

  • Alpaca

  • OpenAssistant

  • ShareGPT

Convert your dataset to the Hugging Face datasets format:

from datasets import load_dataset

train_data = load_dataset("json", data_files="/path/to/dataset.json", split="train")

Section 4: Apply LoRA with PEFT

Configure LoRA Adapter

Use PEFT (Parameter-Efficient Fine-Tuning):

from peft import prepare_model_for_kbit_training, LoraConfig, get_peft_model

model = prepare_model_for_kbit_training(model)
lora_config = LoraConfig(
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "v_proj"],
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM"
)
model = get_peft_model(model, lora_config)

Training Arguments

from transformers import TrainingArguments

training_args = TrainingArguments(
    output_dir="./deepseek-lora",
    per_device_train_batch_size=2,
    gradient_accumulation_steps=8,
    warmup_steps=100,
    max_steps=1000,
    learning_rate=2e-4,
    fp16=True,
    logging_dir="./logs",
    logging_steps=10,
    save_strategy="steps",
    save_steps=200,
    save_total_limit=2,
)

Trainer Setup

from transformers import Trainer
from transformers import DataCollatorForLanguageModeling

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_data,
    data_collator=DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False),
)

trainer.train()

Section 5: Save and Merge LoRA Weights

After training:

model.save_pretrained("./lora-trained")
tokenizer.save_pretrained("./lora-trained")

Optionally, merge LoRA adapter into the base model for deployment:

from peft import PeftModel

merged_model = PeftModel.from_pretrained(model, "./lora-trained")
merged_model.merge_and_unload()
merged_model.save_pretrained("./merged-deepseek")

Section 6: Evaluation

Evaluate the fine-tuned model on benchmarks:

prompt = "Explain the benefits of renewable energy."
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens=200)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

You can also use metrics like BLEU, ROUGE, and accuracy on a validation dataset.

Section 7: Deployment

Local Inference API (vLLM)

Fine-tuned weights can be deployed using vLLM or FastAPI:

pip install vllm
python -m vllm.entrypoints.openai.api_server \
  --model ./merged-deepseek \
  --tensor-parallel-size 2

Docker Container (Optional)

Package your environment using Docker for reproducibility and easy deployment.

Conclusion

Fine-tuning DeepSeek R1 with LoRA unlocks the full potential of this 67B-parameter model without incurring massive hardware costs. With LoRA, you can customize DeepSeek for domain-specific tasks, improve its instruction-following behavior, or adapt it for low-resource languages — all while maintaining training efficiency.

This guide demonstrates that with minimal VRAM and the right tools, DeepSeek can be locally fine-tuned and deployed for real-world applications. Stay updated as the open-source AI community continues to enhance tools like PEFT and DeepSeek for scalable LLM customization.