Screenshot from the article

Introduction

Large Language Models (LLMs) like GPT, LLaMA, and Falcon have revolutionized AI-powered applications, enabling chatbots, content generation, and more. This guide walks through a real-world implementation of an LLM, covering:

  • Pre-training and fine-tuning an LLM
  • Deploying with Kubernetes
  • Optimizing inference for performance and cost
  • Scaling for production use
  • Best practices and troubleshooting

1. Training an LLM: From Scratch vs. Fine-Tuning

1.1. Pre-training vs. Fine-Tuning

  • Pre-training: Training a model from scratch requires vast compute resources and datasets.
  • Fine-tuning: Adapting a pre-trained model for a specific use case is more practical.

1.2. Fine-Tuning an Open-Source LLM (LLaMA-2)

Setup and Dependencies

Install necessary libraries:

pip install transformers datasets peft bitsandbytes accelerate torch

Loading a Pre-Trained Model

from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "meta-llama/Llama-2-7b"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

Preparing a Custom Dataset for Fine-Tuning

from datasets import load_dataset
dataset = load_dataset("json", data_files="custom_data.json")
dataset = dataset["train"].shuffle().select(range(10000))

Fine-Tuning with PEFT (Parameter Efficient Fine-Tuning)

from peft import LoraConfig, get_peft_model
config = LoraConfig(r=8, lora_alpha=32, target_modules=["q_proj", "v_proj"])
model = get_peft_model(model, config)

Train the model with Accelerate:

accelerate launch train.py --model_name meta-llama/Llama-2-7b --dataset custom_data.json

2. Deploying LLM on Kubernetes

2.1. Containerizing the Model with FastAPI

Create app.py for serving the model:

from fastapi import FastAPI
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
app = FastAPI()
model_name = "fine-tuned-llama-2"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
def generate_text(prompt):
    inputs = tokenizer(prompt, return_tensors="pt")
    outputs = model.generate(**inputs)
    return tokenizer.decode(outputs[0])
@app.post("/generate")
def generate(request: dict):
    return {"response": generate_text(request["prompt"])}

Create a Dockerfile:

FROM python:3.9
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]

Build and push the Docker image:

docker build -t my-llm-app .
docker tag my-llm-app myrepo/my-llm-app:v1
docker push myrepo/my-llm-app:v1

2.2. Deploying to Kubernetes

Create a deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: llm-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: llm
  template:
    metadata:
      labels:
        app: llm
    spec:
      containers:
      - name: llm
        image: myrepo/my-llm-app:v1
        ports:
        - containerPort: 8080

Apply the deployment:

kubectl apply -f deployment.yaml

Expose the service:

apiVersion: v1
kind: Service
metadata:
  name: llm-service
spec:
  selector:
    app: llm
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

Apply the service:

kubectl apply -f service.yaml

Test API:

curl -X POST "http://<EXTERNAL-IP>/generate" -H "Content-Type: application/json" -d '{"prompt": "Hello, AI!"}'

3. Optimizing Performance and Scaling

3.1. Using Quantization to Reduce Model Size

from transformers import BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=quantization_config)

3.2. Autoscaling LLM Pods

Enable Kubernetes autoscaler:

kubectl autoscale deployment llm-app --cpu-percent=50 --min=2 --max=10

4. FAQs and Troubleshooting

❓ How to reduce inference latency?

✅ Use tensor parallelism and model quantization to optimize inference.

❓ How to handle out-of-memory (OOM) errors?

✅ Reduce batch size, enable gradient checkpointing, or use a smaller model variant.

❓ How to deploy LLM on serverless architecture?

✅ Use AWS Lambda with Amazon SageMaker for cost-effective scaling.

❓ Can I use LLMs on edge devices?

✅ Yes! Use ONNX Runtime or TensorFlow Lite for optimized models on mobile or edge devices.

Conclusion

This guide demonstrated a real-world LLM implementation, from fine-tuning to deploying on Kubernetes and optimizing inference performance. Whether you’re building a chatbot, code assistant, or document summarizer, these strategies help in scaling LLMs efficiently. 🚀

📢 Have questions or feedback? Drop a comment below or connect with me on Twitter/X@spysood!

Originally published on Medium.