DEV Community

matias yoon
matias yoon

Posted on

로컬 LLM 셋업 가이드 (v28)

로컬 LLM 셋업 가이드 (v28)

Overview & Prerequisites

로컬 LLM 실행은 인터넷 연결 없이 AI 기능을 사용하고, 데이터 보안을 유지하는 최적의 방법입니다. 이 가이드는 다양한 하드웨어에서 성능 최적화된 로컬 LLM 셋업을 위한 실용적인 접근법을 제공합니다.

필수 사양:

  • Linux 시스템 (Ubuntu 20.04 이상 권장)
  • 16GB 이상 RAM (32GB 이상 권장)
  • NVIDIA GPU (CUDA 11.8 이상 권장)
  • 10GB 이상 여유 저장공간

GPU 없이 실행 가능: CPU만으로도 작동하지만 성능은 현저히 저하됩니다.

Framework Comparison

프레임워크 장점 단점 추천 사용 사례
llama.cpp 최적화된 C++ 구현, 다양한 퀀타이제이션, 최대한의 성능 복잡한 설치 과정 고성능 요구, 최적화된 서버
Ollama 간단한 설치, 관리 용이, Docker 기반 고급 커스터마이징 부족 빠른 테스트, 개발용
vLLM 높은 성능, 대규모 모델 지원 복잡한 구성, 메모리 요구량 많음 고성능 서버, 대규모 추론
LocalAI 다양한 API 호환성, RESTful API 성능은 낮음 API 통합, 클라우드 호환성

Recommended Setup: llama.cpp with Q4_K_M Quantization

llama.cpp는 현재 가장 안정적이고 성능이 우수한 로컬 LLM 실행 프레임워크입니다. 우리는 Q4_K_M 퀀타이제이션을 사용하여 메모리 사용량을 줄이고 성능을 최적화합니다.

Step-by-Step Installation

# 1. 필수 패키지 설치
sudo apt update
sudo apt install build-essential git cmake python3 python3-pip

# 2. llama.cpp 클론
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp

# 3. 빌드
make clean
make

# 4. 모델 다운로드
mkdir -p models
cd models
wget https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/main/llama-2-7b-chat.Q4_K_M.gguf

# 5. 실행
./server -m ./models/llama-2-7b-chat.Q4_K_M.gguf -c 2048 --port 8080
Enter fullscreen mode Exit fullscreen mode

Model Selection Guide

모델 용도 추천 하드웨어 메모리 사용량
Llama-2-7B-Chat 일반 챗봇, 개발 도움 16GB RAM 이상 4-5GB
Mistral-7B-Instruct 강력한 추론, 코드 생성 16GB 이상 4-5GB
Phi-3-mini 경량, 빠른 추론 8GB 이상 2-3GB
Qwen-1.5-7B 중국어, 다국어 처리 16GB 이상 4-5GB

Quantization Types Explained

# 퀀타이제이션 종류별 성능 비교
# Q4_K_M: 최적의 성능-크기 균형 (권장)
./server -m ./models/llama-2-7b-chat.Q4_K_M.gguf

# Q5_K_M: 더 높은 정확도 (더 많은 메모리 필요)
./server -m ./models/llama-2-7b-chat.Q5_K_M.gguf

# Q8_0: 높은 정확도, 메모리 효율성 낮음
./server -m ./models/llama-2-7b-chat.Q8_0.gguf
Enter fullscreen mode Exit fullscreen mode

성능 기준 (GPU: RTX 3090, 12GB):

퀀타이제이션 추론 속도 (tokens/sec) 메모리 사용량 정확도
Q4_K_M 35 4.2GB 92%
Q5_K_M 32 5.1GB 94%
Q8_0 28 7.3GB 96%

API Setup and Integration

llama.cpp는 HTTP API를 제공하여 기존 도구와 통합 가능합니다.

# 서버 실행 (API 포트 8080)
./server -m ./models/llama-2-7b-chat.Q4_K_M.gguf --port 8080

# API 테스트
curl http://localhost:8080/completion \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Hello, how are you?",
    "n_predict": 128,
    "temperature": 0.7,
    "stop": ["\n"]
  }'
Enter fullscreen mode Exit fullscreen mode

Systemd Service for 24/7 Operation

# /etc/systemd/system/llama.service 생성
sudo nano /etc/systemd/system/llama.service

# 서비스 파일 내용
[Unit]
Description=Local LLM Server
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/llama.cpp
ExecStart=/home/ubuntu/llama.cpp/server -m /home/ubuntu/llama.cpp/models/llama-2-7b-chat.Q4_K_M.gguf --port 8080
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

# 서비스 활성화 및 시작
sudo systemctl daemon-reload
sudo systemctl enable llama.service
sudo systemctl start llama.service
sudo systemctl status llama.service
Enter fullscreen mode Exit fullscreen mode

Monitoring and Performance Tuning

성능 모니터링 스크립트

#!/bin/bash
# monitor.sh

while true; do
  echo "=== GPU Memory ==="
  nvidia-smi --query-gpu=memory.used,memory.total --format=csv -l 1 | tail -n 1

  echo "=== CPU Usage ==="
  top -b -n 1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1

  echo "=== Memory Usage ==="
  free -h | grep "Mem"

  sleep 30
done
Enter fullscreen mode Exit fullscreen mode

고성능 설정

# 성능 최적화 실행 옵션
./server \
  -m ./models/llama-2-7b-chat.Q4_K_M.gguf \
  -c 2048 \
  -n 128 \
  --threads 16 \
  --port 8080 \
  --log-disable \
  --no-display-progress
Enter fullscreen mode Exit fullscreen mode

Real Command Examples

1. 모델 다운로드 및 변환

# Hugging Face 모델 다운로드
wget https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/main/llama-2-7b-chat.Q4_K_M.gguf

# 퀀타이제이션 변환 (다른 퀀타이제이션 사용)
python3 convert-hf-to-gguf.py ./models/llama-2-7b-chat/ --outtype q4_k_m --outfile ./models/llama-2-7b-chat.Q4_K_M.gguf
Enter fullscreen mode Exit fullscreen mode

2. API 통합 예제

import requests

def chat_with_llm(prompt):
    response = requests.post(
        "http://localhost:8080/completion",
        json={
            "prompt": prompt,
            "n_predict": 128,
            "temperature": 0.7,
            "stop": ["\n"]
        }
    )
    return response.json()['content']

# 사용 예
result = chat_with_llm("Python에서 Flask로 API를 만드는 방법은?")
print(result)
Enter fullscreen mode Exit fullscreen mode

3. 성능 테스트

# wrk 성능 테스트
wrk -t4 -c10 -d30s http://localhost:8080/completion \
  --header="Content-Type: application/json" \
  --data='{"prompt":"test","n_predict":64}'
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

공통 문제 해결

  1. 메모리 부족: Q5_K_M 또는 Q8_0 퀀타이제

📥 Get the full guide on Gumroad: https://gumroad.com/l/auto ($7)

Top comments (0)